DBIx::Customã§ãã¢ãã«ããå©ç¨ãã
Perl
›
ã¢ã¸ã¥ã¼ã«
›
ãã¼ã¿ãã¼ã¹
›
DBIx::Custom
›
here
DBIx::Customã§ãã¢ãã«ããå©ç¨ããæ¹æ³ã解説ãã¾ãã
ã¢ãã«ãçæãã - create_model
ã¢ãã«ãä½æããã«ã¯create_modelã¡ã½ããã使ç¨ãã¾ãã
$dbi->create_model( table => 'book', primary_key => 'id', join => [ 'left outer join company on book.company_id = company.id' ], ctime => 'ctime', mtime => 'mtime' );
ã¢ãã«ã«ã¯ããtable - ãã¼ãã«åããprimary_key - ãã©ã¤ããªã¼ãã¼ããjoin - joinç¯ããctime - ä½ææ¥æããmtime - æ´æ°æ¥æããªã©ãè¨å®ãã¦ãããã¨ãã§ãã¾ãã
ãããã¯ã¢ãã«ããinsert,update,delete,selectã¡ã½ãããå¼ã³åºããã¨ãã«èªåçã«å©ç¨ãããããã«ãªãã¾ãã
ã¢ãã«ã®åå¾
ä½æããã¢ãã«ã¯modelã¡ã½ããã§ãã¼ãã«åãæå®ãã¦åå¾ãããã¨ãã§ãã¾ãã
my $book_model = $dbi->model('book');
ã¢ãã«ã¯DBIx::Custom::Modelãªãã¸ã§ã¯ãã§ããã¢ãã«ããã¯DBIx::Custom::Modelãæã¤ã¡ã½ããã¨ãDBIx::Customãæã¤å ¨ã¦ã®ã¡ã½ãããå¼ã³åºããã¨ãã§ãã¾ãã
ã¢ãã«ã®å©ç¨
ã¢ãã«ã§ã¯insert,update,delete,select,countãªã©ã®ã¡ã½ãããå©ç¨ãããã¨ãã§ãã¾ããtableãprimary_keyãjoinã¨ãã£ããªãã·ã§ã³ãçç¥ãããã¨ãã§ãè¨è¿°ãç°¡æ½ã«ãªãã¾ãã
my $result = $model->select( column => [ {company => [qw/name/]} ] );
ã¾ãã¢ãã«ããã¯DBIx::Customãããã¯DBIã®ãã¹ã¦ã®ã¡ã½ãããå¼ã³åºããã¨ãå¯è½ã§ãã
å¤é¨å®ç¾©ããã¢ãã«ã®åã込㿠- include_model
å¤é¨ã§å®ç¾©ããã¢ãã«ããããã°ã©ã ã«åãè¾¼ãã«ã¯include_modelã¡ã½ããã使ç¨ãã¾ããDBIx::Customã§ã¯ããã°ã©ã ã大ãããªã£ã¦ããã¨ãã«ãã¢ãã«ãå¤é¨ã«å®ç¾©ããä¿å®æ§ãé«ãããã¨ãã§ãã¾ãã
$dbi->include_model('MyModel');
ãã®ä¾ã§ã¯MyModelã¨ããåå空é以ä¸ã«å±ãããã¹ã¦ã®ã¢ãã«ãåãè¾¼ã¿ã¾ããã¾ãMyModelã¨ããååã®ã¢ã¸ã¥ã¼ã«ãå¿ è¦ã§ãããã¨ã«ã注æãã¦ãã ããã
lib / MyModel.pm / MyModel / book.pm / company.pm
ã¢ãã«ã®ãµã³ãã«
MyModel
MyModelã¯ä¸è¬çã«ã¯æ¬¡ã®ããã«ãªãã¾ããDBIx::Custom::Modelãç¶æ¿ããããã«ãã¾ãã
package MyModel; use DBIx::Custom::Model -base; 1;
-baseãªãã·ã§ã³ã¯ç¶æ¿ã®ããã®ãªãã·ã§ã³ã§ãObject::Simpleããå¼ãç¶ãã§ãã¾ãã
MyModel::book
MyModel::bookã¯æ¬¡ã®ããã«ãªãã¾ããMyModelãç¶æ¿ããããã«ãã¾ãã
has primary_key => 'book_id'; has ctime => 'created_time'; has mtime => 'modified_time'; has join => sub { [ 'left join company on book.company_id = company.id', 'left join author on book.author_id = author.id' ] } 1;
hasã¯å±æ§ãå®ç¾©ããããã®ã¡ã½ããã§ãObject::Simpleã®æ©è½ã§ãã
ã¢ãã«ãã»ããã¢ãããã - setup_model
ã¢ãã«ãã»ããã¢ããããã«ã¯setup_modelã使ç¨ãã¾ãããã¼ã¿ãã¼ã¹ã®ãã¼ãã«æ å ±ã調ã¹ã¦ãã¢ãã«(DBIx::Custom::Model)ã®columnsã«ååãèªåçã«è¨å®ããã¾ãã
$dbi->setup_model;
DBIx::Custom::Modelã®columnså±æ§ãè¨å®ããã¦ããã°ãDBIx::Custom::Modelã®mycolumnã¡ã½ããã使ã£ã¦ããã¼ãã«ã«å«ã¾ãããã¹ã¦ã®åãå«ãcolumnç¯ãçæã§ãã¾ãã
my $book_model = $dbi->model('book'); my $column_clause = $book_model->mycolumn;
ãã¹ã¦ã®åãå«ã次ã®ãããªcolumnç¯ãçæããã¾ãã
book.author as author, book.title as title, book.price as price
ã¾ãååãæå®ããªãã§ãDBIx::Customã®columnã¡ã½ããã使ããã¨ãã§ãã¾ãã
my $column_clause = $dbi->column('book');
次ã®ãããªãã¹ã¦ã®åãå«ãcolumnç¯ãçæããã¾ãã
book.author as "book.author", book.title as "book.title", book.price as "book.price"
ã¢ãã«ãåå¾ãã - model
modelã¡ã½ããã使ã£ã¦ãcreate_modelã¡ã½ãããinclude_modelã¡ã½ããã§ä½æããã¢ãã«ãåå¾ãããã¨ãã§ãã¾ãã
my $model = $dbi->model('book');