DBICx::Modeler::Generator::JA - DBIx::Class::SchemaとDBICx::Modelerの動的な生成
この文書はDBICx::Modeler::Generatorのバージョン0.02について記述しています。
0.02
use Orochi; my $container = Orochi->new; $container->inject_literal('/Class/application' => 'MyApp'); $container->inject_literal('/Path/root' => 'examples'); # ... $container->inject_class('DBICx::Modeler::Generator'); $container->inject_class('DBICx::Modeler::Generator::Class'); # ... my $generator = $container->get('/DBICx/Modeler/Generator'); $generator->deploy_database; $generator->update_schemata; $generator->update_models; # 備考: 上記のコードの代わりにDBICx::Modeler::Generator::CLIを使いましょう。
エンタープライズアプリケーションでは、上手なアーキテクチャーを考慮する必要があります。 特に、モデリングはアプリケーションの保守性に多大な影響を及ぼします。
そこで、DBICx::Modelerを使ってスキーマとモデルのモジュールを分離することを推奨します。 この小気味よいモジュールは、DBIx::Classスキーマに対して、Mooseに基づいたレイヤーを提供します。
しかしながら、その分離による恩恵を享受するためには、たくさんのモデルモジュールを定義しなければなりません。 これはとても面倒な作業です。
そこで、以下の機能を提供するこのDBICx::Modeler::Generatorモジュールによって、面倒な共通的定義を自動化しましょう。
DBICx::Modeler::Generator
データベースの動的な配備
DBIx::Classスキーマモジュール群の動的な定義
DBICx::Modelerモデルモジュール群の動的な定義
なお、このDBICx::Modeler::Generatorモジュールは、DBIx::Class::Schema::Loadlerによるスキーマモジュール群の動的な生成を自動化する、単純なラッパーとしても使えます。
MySQL Workbenchアプリケーション(http://www.mysql.com/products/workbench/)を使って、スキーマ群を定義します。
訳注: 日本語版ページはhttp://www-jp.mysql.com/products/workbench/にあります。ただし、このアプリケーションの日本語版が存在するわけではありません。
このアプリケーションは表(テーブル), 列(カラム), 索引(インデックス), 関係(リレーション)などを設計出来ます。
スキーマファイルの実例(サンプル)は、このディストリビューションのexamples/doc/DBDSC_schemata.mwbに同梱されています。 これはDBICx::Modelerのテスト用モジュール群を再現したもので、artist, cd, trackというテーブル(スキーマ)を備えています。
artist
cd
track
文書化のための任意の作業として、MySQL Workbenchを使ってER図(ERD: Entity-Relationship Diagram)を描画します。
ER図として出力した画像ファイルの実例(サンプル)は、このディストリビューションのexamples/doc/DBDERII_Including_Information.pngに同梱されています。
MySQL Workbenchの[Database] - [Forward Engineer...]機能を使って、スキーマからデータベースを動的に配備します。
[Database] - [Forward Engineer...]
もしくは、このモジュールのdeploy_database()メソッドで、MySQL Workbenchの[File] - [Export] - [Forward Engineer SQL CREATE Script...]機能によって生成したデータベース作成用スクリプト(データ定義言語, DDL: Data Definition Language)を使って配備します。
[File] - [Export] - [Forward Engineer SQL CREATE Script...]
生成したデータベース作成用スクリプトの実例(サンプル)は、このディストリビューションのexamples/src/myapp_mysql.sql(MySQL用)やexamples/src/myapp.sql(SQLite用)に同梱されています。
注: MySQL WorkbenchはSQLite export plugin for MySQL Workbenchプラグイン(http://www.henlich.de/software/sqlite-export-plugin-for-mysql-workbench/)によってSQLite用のDDLスクリプトを出力することが出来ます。
追加の定義が必要なスキーマモジュール群を静的に定義します(自分で書くということです)。 追加の定義とは、インフレーション(inflations: データベースから取り出す際にオブジェクト化する処理など), デフレーション(deflations: データベースへ格納する際にオブジェクトを文字列化する処理など), 関係(relationships)などです。
追加の定義のみを記述したスキーマモジュールファイルの実例は、このディストリビューションのexamples/src/lib/MyApp/Schema/Artist.pmに同梱されています。
このモジュールのupdate_schemata()メソッドを使って、スキーマモジュールファイル群を動的に生成します。
これによって、例えばこのディストリビューションのexamples/lib/MyApp/Schema/Artist.pmやexamples/lib/MyApp/Schema/Cd.pm等々にスキーマファイルを生成することが出来ます。
追加の定義が必要なモデルモジュール群を静的に定義します(自分で書くということです)。
追加の定義とは、Mooseのアトリビュート(attributes), メソッド(methods), メソッドモディファイヤー(method modifiers)などです。
追加の定義のみを記述したモデルモジュールファイルの実例は、このディストリビューションのexamples/src/lib/MyApp/Model/Cd.pmに同梱されています。
このモジュールのupdate_models()メソッドを使って、モデルモジュール群を動的に生成します。
これによって、例えばこのディストリビューションのexamples/lib/MyApp/Model/Artist.pmやexamples/lib/MyApp/Model/Cd.pm等々にモデルファイルを生成することが出来ます。
データベースの動的な配備, スキーマ群の動的な定義, モデル群の動的な定義を行うバッチスクリプトを使用することを推奨します。
バッチスクリプトファイルの実例(サンプル)は、このディストリビューションのexamples/src/sbin/maintain_models.plに同梱されています。 実行方法は見本節を参照してください。
MySQL Workbenchでは、ER図の1mmは出力されるPNG画像の5pxに相当します(これはWindows版のバージョン5.1.18 OSS時点での前提です)。
この情報に基づくと、PNG画像の寸法を指定することが出来ます。
例えば、Quad-VGA解像度(幅: 1280px, 高さ: 960px)として出力するために、以下のような設定を施してはいかがでしょうか:
[Paper]
[Size]リストボックスで[A4 (210 mm x 297 mm)]アイテムを選択
[Size]
[A4 (210 mm x 297 mm)]
[Orientation]
[Landscape]ラジオボタンをオンにする
[Landscape]
[Margins]
[Top]テキストボックスへ[10]mmを入力
[Top]
[10]
[Left]テキストボックスへ[10]mmを入力
[Left]
[Bottom]テキストボックスへ[35]mmを入力
[Bottom]
[35]
[Right]テキストボックスへ[12]mmを入力
[Right]
[12]
ご参考までに、サイズ検証スクリプトファイルの実例(サンプル)は、このディストリビューションのexamples/src/confirm_image_size.plとして同梱されています。
$di_container->get('/DBICx/Modeler/Generator')
依存性が注入されたオブジェクトを返します。
オブジェクトを取得するためには、DBICx::Modeler::Generator->new(...)の代わりに上記の概要にあるコードを使ってください。
DBICx::Modeler::Generator->new(...)
詳細は依存性の注入節を参照してください。
DBICx::Modeler::Generator::CLIによって依存性の注入を包み込んだインターフェースを使うことも出来ます。
DBICx::Modeler::Generator::CLI->new_with_options(%init_args)
DBICx::Modeler::Generator::CLIオブジェクトを返します。
ジェネレーターオブジェクトを取得するためには、このインターフェースを使うことを強く推奨します。MooseX::GetoptとMooseX::SimpleConfigを用いて依存性の注入作業を包み込むことが出来るためです。
以下の具体的なコードを参照してください:
my $generator = DBICx::Modeler::Generator::CLI->new_with_options( application => 'MyApp', root => '/path/to/root', driver => 'SQLite', )->generator;
$self->deploy_database()
データベース作成用スクリプトに基づいてデータベースを配備します。
$self->update_models()
モデルモジュール群を更新します。
$self->update_schemata()
スキーマモジュール群を更新します。
このクラスとそのサブクラスでは依存性の注入(DI: Dependency Injection)のためにMooseX::Orochiを使っています。
詳細はこのディストリビューションに同梱されているexamples/src/sbin/maintain_models.plを参照してください。
/DBICx/Modeler/Generator/Class
DBICx::Modeler::Generator::ClassLikeインターフェースを満たす実装クラスのインスタンスです。
このディストリビューションでは、一般的な用途に使える実装クラスとして、DBICx::Modeler::Generator::Driver::Classを同梱しています。
/DBICx/Modeler/Generator/Class/application
アプリケーションルートクラスのクラス名です。
Str
MyApp, My::App, 等
MyApp
My::App
/DBICx/Modeler/Generator/Driver
DBICx::Modeler::Generator::DriverLikeインターフェースを満たす実装クラスのインスタンスです。
このディストリビューションでは、一般的な用途に使える実装クラスとして、DBICx::Modeler::Generator::Driver::MySQLとDBICx::Modeler::Generator::Driver::SQLiteを同梱しています。
/DBICx/Modeler/Generator/Model
DBICx::Modeler::Generator::ModelLikeインターフェースを満たす実装クラスのインスタンスです。
このディストリビューションでは、一般的な用途に使える実装クラスとして、DBICx::Modeler::Generator::Driver::Modelを同梱しています。
/DBICx/Modeler/Generator/Path
DBICx::Modeler::Generator::PathLikeインターフェースを満たす実装クラスのインスタンスです。
このディストリビューションでは、一般的な用途に使える実装クラスとして、DBICx::Modeler::Generator::Driver::Pathを同梱しています。
/DBICx/Modeler/Generator/Path/root
アプリケーションルートディレクトリーのパスです。
Path::Class::Dir (MooseX::Types::Path::Classで型変換されます)
/path/to/root
/DBICx/Modeler/Generator/Schema
DBICx::Modeler::Generator::SchemaLikeインターフェースを満たす実装クラスのインスタンスです。
このディストリビューションでは、一般的な用途に使える実装クラスとして、DBICx::Modeler::Generator::Driver::Schemaを同梱しています。
/DBICx/Modeler/Generator/Tree
DBICx::Modeler::Generator::TreeLikeインターフェースを満たす実装クラスのインスタンスです。
このディストリビューションでは、一般的な用途に使える実装クラスとして、DBICx::Modeler::Generator::Driver::Treeを同梱しています。
/DBICx/Modeler/Generator/Class/base_part
Base
/DBICx/Modeler/Generator/Class/model_part
Model
/DBICx/Modeler/Generator/Class/schema_part
Schema
/DBICx/Modeler/Generator/Driver/bin
mysql (ドライバーの実装クラスがDBICx::Modeler::Generator::Driver::MySQLの場合), sqlite3 (ドライバーの実装クラスがDBICx::Modeler::Generator::Driver::SQLiteの場合), 等
mysql
sqlite3
/DBICx/Modeler/Generator/Driver/database
$application, /$root/$application.$database_extension, 等
$application
/$root/$application.$database_extension
myapp, my_app, /path/to/root/my_app.db, 等
myapp
my_app
/DBICx/Modeler/Generator/Driver/dbd
mysql (ドライバーの実装クラスがDBICx::Modeler::Generator::Driver::MySQLの場合), SQLite (ドライバーの実装クラスがDBICx::Modeler::Generator::Driver::SQLiteの場合), 等
SQLite
/DBICx/Modeler/Generator/Driver/dsn
dbi:$dbd:database=$database, dbi:$dbd:database=$database;host=$host, dbi:$dbd:database=$database;host=$host;port=$port, dbi:$dbd:dbname=$database, 等
dbi:$dbd:database=$database
dbi:$dbd:database=$database;host=$host
dbi:$dbd:database=$database;host=$host;port=$port
dbi:$dbd:dbname=$database
/DBICx/Modeler/Generator/Driver/extension
.dbl (ドライバーの実装クラスがDBICx::Modeler::Generator::Driver::SQLiteの場合), 等
/DBICx/Modeler/Generator/Driver/host
undef (これは大抵のドライバーではlocalhostを意味します)
undef
localhost
/DBICx/Modeler/Generator/Driver/password
foobar
/DBICx/Modeler/Generator/Driver/port
Int
3306, 3307, 等
3306
3307
/DBICx/Modeler/Generator/Driver/username
mysql_user
/DBICx/Modeler/Generator/Path/creation_script
Path::Class::File (MooseX::Types::Path::Classで型変換されます)
/$root/$source/$application.$script_extension
/path/to/root/src/myapp.sql
/DBICx/Modeler/Generator/Path/module_extension
.pm
/DBICx/Modeler/Generator/Path/script_extension
.sql
/DBICx/Modeler/Generator/Schema/components
ArrayRef[Str]
[]
(Don't use DBIx::Class::UTF8Columns, http://perl-users.jp/articles/advent-calendar/2009/hacker/04.htmlも参照)
/DBICx/Modeler/Generator/Schema/is_debug
Bool
0 (false)
0
/DBICx/Modeler/Generator/Tree/application
myapp (アプリケーションクラス名がMyAppの場合), my_app (アプリケーションクラス名がMy::Appの場合), 等
/DBICx/Modeler/Generator/Tree/library
[qw(lib)]
/DBICx/Modeler/Generator/Tree/source
[qw(src)]
/DBICx/Modeler/Generator/Tree/target
このディストリビューションでは、上記の作業手順(ワークフロー)に関する一切のファイルを、見本として同梱しています。
ディストリビューションのルートディレクトリーで、以下のコマンドを実行してください:
perl -Ilib examples/src/sbin/maintain_models.pl \ -a MyApp -r examples -d SQLite
または
perl -Ilib examples/src/sbin/maintain_models.pl \ -a MyApp -r examples -d MySQL -u username -w password \ -l /Path/script_extension=_mysql.sql
perl -Ilib examples/src/sbin/maintain_models.pl \ --configfile examples/src/myapp.yml
DBICx::Modeler
DBIx::Class::Schema::Loader
DBIx::Class
MySQL Workbenchアプリケーションのホームページ, http://www.mysql.com/products/workbench/
Martin Fowler, Patterns of Enterprise Application Architecture, Toronto: Addison-Wesley Professional, 2002, 560p., ISBN 0321127420 / 978-0321127426
(PoEAA, PofEAAとして略されます)
訳注: 翻訳内容に対して色々な意見があるようですが、ともあれ日本語版が刊行されています。
マーチン=ファウラー(著), 長瀬 嘉秀(監訳), 株式会社テクノロジックアート(翻訳), 『(Object Oriented Selection) エンタープライズアプリケーションアーキテクチャパターン』, 東京: 翔泳社, 2005年, 548ページ, ISBN 4798105538 / 978-4798105536
エンタープライズアプリケーションに於いてスキーマとモデルを分離する方法, http://blog.eorzea.asia/2009/10/post_76.html
テストの拡充
テストでのTest::mysqldの使用 (参考: 牧 大輔さん(lestrratさん)による、http://mt.endeworks.jp/d-6/2009/10/things-ive-done-while-using-test-mysqld.htmlの記事)
互換性のない変更点はありません。
バグは報告されていません。
何かバグを発見されたら、機能のご要望がありましたら、または改善のためのご意見がありましたら、メール(bug-dbicx-modeler-generator at rt.cpan.org宛)で報告してください。 または、Webインターフェース(http://rt.cpan.org/Public/Bug/Report.html?Queue=DBICx-Modeler-Generator)を使って報告してください。 これによって、その報告内容が開発者へ通知されます。 さらに、バグや要望の対応状況について、報告者が通知を自動的に受けることも出来ます。
bug-dbicx-modeler-generator at rt.cpan.org
バグを報告いただく際には、もし可能であれば、バグを再現するための出来るだけ少量のサンプルコードを添えてください。 提案やパッチは勿論歓迎します。
このモジュールの文書はperldocコマンドで閲覧出来ます。
perldoc
perldoc DBICx::Modeler::Generator
日本語版はPod::PerldocJpを使ったperldocjpコマンドで閲覧出来ます。
perldocjp
perldocjp DBICx::Modeler::Generator.ja
また、以下の場所も参照してください:
http://rt.cpan.org/Public/Dist/Display.html?Name=DBICx-Modeler-Generator
http://annocpan.org/dist/DBICx-Modeler-Generator
http://search.cpan.org/dist/DBICx-Modeler-Generator
http://cpanratings.perl.org/dist/DBICx-Modeler-Generator
このモジュールはgitを使って保守されています。
最新版はgit://github.com/gardejo/p5-dbicx-modeler-generator.gitにあります。
テストのコード網羅率(コードカバレッジ)を検査するために、Devel::Coverを使っています。 このディストリビューションのテストスートに関するDevel::Coverによる調査結果の概要を以下に示します。
Devel::Cover
---------------------------- ------ ------ ------ ------ ------ ------ ------ File stmt bran cond sub pod time total ---------------------------- ------ ------ ------ ------ ------ ------ ------ ...BICx/Modeler/Generator.pm 100.0 n/a n/a 100.0 100.0 0.0 100.0 .../Modeler/Generator/CLI.pm 100.0 100.0 n/a 100.0 0.0 22.2 98.0 ...odeler/Generator/Class.pm 100.0 n/a n/a 100.0 100.0 0.0 100.0 ...er/Generator/ClassLike.pm 100.0 n/a n/a 100.0 n/a 0.0 100.0 .../Generator/Driver/Base.pm 100.0 100.0 n/a 100.0 100.0 0.0 100.0 ...Generator/Driver/MySQL.pm 100.0 100.0 n/a 100.0 n/a 0.0 100.0 ...enerator/Driver/SQLite.pm 100.0 n/a n/a 100.0 n/a 11.1 100.0 ...r/Generator/DriverLike.pm 100.0 n/a n/a 100.0 n/a 0.0 100.0 ...odeler/Generator/Model.pm 100.0 100.0 n/a 100.0 100.0 44.4 100.0 ...er/Generator/ModelLike.pm 100.0 n/a n/a 100.0 n/a 0.0 100.0 ...Modeler/Generator/Path.pm 100.0 100.0 n/a 100.0 100.0 0.0 100.0 ...ler/Generator/PathLike.pm 100.0 n/a n/a 100.0 n/a 0.0 100.0 ...deler/Generator/Schema.pm 100.0 50.0 n/a 100.0 100.0 22.2 97.6 ...r/Generator/SchemaLike.pm 100.0 n/a n/a 100.0 n/a 0.0 100.0 ...Modeler/Generator/Tree.pm 100.0 n/a n/a 100.0 n/a 0.0 100.0 ...ler/Generator/TreeLike.pm 100.0 n/a n/a 100.0 n/a 0.0 100.0 Total 100.0 94.4 n/a 100.0 91.7 100.0 99.7 ---------------------------- ------ ------ ------ ------ ------ ------ ------
<moriya at cpan dot org>, http://gardejo.org/
<moriya at cpan dot org>
著作権保有 (c) 2009-2010 守屋 雅樹 (Gardejo)
このモジュールはフリーソフトウェアです。 あなたはこれをPerlと同じように自由に再配布・改変することが出来ます。 詳しくはperlgplおよびperlartisticを参照してください。
使用許諾条件の全文はこのディストリビューションに同梱されているLICENSEファイルにあります。
Copyright (c) 2009-2010 MORIYA Masaki, alias Gardejo
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlgpl and perlartistic.
The full text of the license can be found in the LICENSE file included with this distribution.
この文書の訳出にあたっては、英単語のカタカナ表記について、語尾の音引き(katakana prolonged sound mark)を省かずに表記しています(JIS Z 8301で規定されているような省略はしていません)。 例えば、「constructor」を「コンストラクタ」ではなく「コンストラクター」と表記しています。
To install DBICx::Modeler::Generator, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBICx::Modeler::Generator
CPAN shell
perl -MCPAN -e shell install DBICx::Modeler::Generator
For more information on module installation, please visit the detailed CPAN module installation guide.