DBIx::Class::Schema::Loader
小規模のシステムを即席で作る必要が出たので
最近流行のDBIx::Class::Schema::Loaderを使って楽に開発をやろうと思い調査中。
- OS:WindowsXP
- DB:sqlite3
- Perl:This is perl, v5.8.9 built for MSWin32-x86-multi-thread
- DBIx::Class:0.08112
- DBIx::Class::Schema::Loader:0.04006
CREATE TABLE agent ( agent_id INTEGER PRIMARY KEY, agent_name VARCHAR(50) ); --- --- Load some sample data --- INSERT INTO agent VALUES (0, 'takao0'); INSERT INTO agent VALUES (1, 'takao1'); INSERT INTO agent VALUES (2, 'takao2'); INSERT INTO agent VALUES (3, 'takao3'); INSERT INTO agent VALUES (4, 'takao4'); INSERT INTO agent VALUES (5, 'takao5'); INSERT INTO agent VALUES (6, 'takao6'); INSERT INTO agent VALUES (7, 'takao7'); INSERT INTO agent VALUES (8, 'takao8'); INSERT INTO agent VALUES (9, 'takao9');
test.pl
#!/usr/local/bin/perl use strict; use warnings; use lib 'lib'; # スキーマクラスを use use Test::SchemaLoader; # スキーマクラスのインスタンスを作成 my $schema = Test::SchemaLoader->connect('dbi:SQLite:dbname=db/test.db'); # ResultSet を作成し全レコード取得 # resultsetの引数はagentではなくAgentと書く必要がある # DBIx::Class::Schema::Loaderがテーブルをモジュールに変換するときにTest::SchemaLoader::Agentとしているため my $rs = $schema->resultset('Agent')->search; # 件数を出力 warn $rs->count, " rows"; while (my $row = $rs->next) { warn sprintf "* %s: %s", $row->agent_id, $row->agent_name; }
Test::SchemaLoader.pm
package Test::SchemaLoader; use strict; use warnings; use base qw/DBIx::Class::Schema::Loader/; __PACKAGE__->loader_options( debug => 1, ); 1;
実行。
C:\home\takao\catalyst\test>sqlite3 db\test.db < db\schema_sqlite.sql C:\home\takao\catalyst\test>set DBIC_TRACE=1 C:\home\takao\catalyst\test>perl test.pl Test::SchemaLoader::Agent->load_components("Core"); Test::SchemaLoader::Agent->table("agent"); Test::SchemaLoader::Agent->add_columns( "agent_id", { data_type => "INTEGER", default_value => undef, is_nullable => 1, size => undef, }, "agent_name", { data_type => "VARCHAR", default_value => undef, is_nullable => 1, size => 50, }, ); Test::SchemaLoader::Agent->set_primary_key("agent_id"); SELECT COUNT( * ) FROM agent me: 10 rows --- at test.pl line 16. SELECT me.agent_id, me.agent_name FROM agent me: * 0: takao0 at test.pl line 20. * 1: takao1 at test.pl line 20. * 2: takao2 at test.pl line 20. * 3: takao3 at test.pl line 20. * 4: takao4 at test.pl line 20. * 5: takao5 at test.pl line 20. * 6: takao6 at test.pl line 20. * 7: takao7 at test.pl line 20. * 8: takao8 at test.pl line 20. * 9: takao9 at test.pl line 20.