DBICã®ãªã¬ãªã¬ResultSetãä½ã£ããã ãã©
ResultSetã«ã¡ã½ãããçãããã訳ã§ããResultSourceã®ã¡ã½ããã«resolve_prefetchã£ã¦ããã®ããã£ã¦ãprefetchã¨ãjoinã§æå®ããrelationã®ãªãã¡ã¬ã³ã¹ã渡ãã¨columnãè¿ãã¦ããããã®ãããã¾ãã{ blogs => 'user_id' }ã¿ãããªãã¤ã§ãããã ããã®ã¾ã¾ã§ã¯lookupã«ä½¿ããªãã®ã¨èªåãç¡ãã®ã§ã¡ãã£ã¨Wrapããã訳ã§ãã
å··ã§ã¯ResultSetManager使ãããããã§ãããLoader使ã£ã¦ãã®ã§ä½¿ãã¾ãããã§ãèªåã®ResultSetãä½ã£ã¦
my $source = $schema->resultset('Moge')->result_source; $source->resultset_class('MyApp::ResultSet');
ã¿ãããªæãã§resultset_classãæå®ãã¦èªåã®å¥½ããªãã¨ãã訳ã§ãã
ã§ããã®æå®ããæ¹æ³ãªãã§ããã©ã©ãã«ç½®ãããããã®ãããããªãã
ã¨ããããæåã¯
package MyApp::Schema; use strict; use base qw/DBIx::Class::Schema::Loader/; __PACKAGE__->loader_options( relationships => 1, ); __PACKAGE__->connection('dbi:mysql:test', agile => 'pass'); for my $moniker ( __PACKAGE__->sources ) { my $class = __PACKAGE__->class($moniker); $class->resultset_class('MyApp::ResultSet'); }
ã¿ããã«Schemaã«connect_infoæ¸ãã¨è¨ãä½ã¨ãæ°æã¡æªãæ¹æ³ã§ã
ãµã¨dmakiさんのページãè¦ã¦å¥éºã«ãããã¨æã£ã¦ããããããã§ãã
package MyApp::Schema; use strict; use base qw(DBIx::Class::Schema::Loader); __PACKAGE__->loader_options( relationships => 1, ); sub connection { my $class = shift; my $schema = $class->SUPER::connection(@_); for my $moniker ( $schema->sources ) { my $class = __PACKAGE__->class($moniker); $class->resultset_class('MyApp::ResultSet'); } $schema; }
ã¨ããããããã§æ®éã«ã¯åããã§ããã©ãresult_source_instanceã¤ãã£ã¦èªåã®ãã¥ã¼ãä½ã£ãã¯ã©ã¹ã ã¨ãã¾ãè¡ããªãï¼ä¸ååã®ãã¤ã ã¨èªåã®ãã¥ã¼ããªã¬ãªã¬ResultSetãåããã§ããã©ãã
http://www.removabletype.net/pods/DBIx/Class/Manual/Cookbook.html#arbitrary_sql_through_a_custom_resultsource
ããã§ãããselect * from table tblã®tblé¨åã«SQLãæ¸ãã¦èªåã®SQLã«ããã
ã§ããªãã ããã æ©ãã§ããã§ããã©ãçµå±ã¯
#èªåã®çããããã¡ã½ãã sub DBIx::Class::ResultSet::test { warn "Hello!" }
çµå±ãããSchemaã«æ¸ãã¦ããã°ãããã§ã¯ãªããã¨è¨ãçµè«ã«ããªããç´å¾ãããªããã©ã¨ããããããã§ããããããã®ããªãã
ã¨ãããèªåã®ãã¥ã¼ä½ãã³ã¼ãã¯ã©ãç½®ãã°ãããã ãããSchemaã¯staticã«æ¸ãåºããæ¹ãããã®ããªãã