- 追加された行はこの色です。
- 削除された行はこの色です。
* 構成例 [#k203f830]
* 全体の流れ [#k203f830]
** データベーススキーマ [#vad6e51f]
-テーブル t1
-- num int (PK)
-テーブル t2
-- num int (PK)
-- str text
** データベースの作成 [#vad6e51f]
*** スキーマ [#gc8e50ce]
ここでは以下のようなスキーマのDBをSQLiteで作る事にする。
-テーブル player
-- player_id int (PK)
-- player_name text
-- team_id int (FK)
-テーブル team
-- team_id int (PK)
-- team_name text
*** シェルから実行 [#k958e5e4]
*** シェルから作成実行 [#jf1c9d9e]
以下のようにシェルからコマンドを実行し、DBを作成する。
cat > test.sql
create table t1 ( num int primary key );
create table t2 ( num int primary key, str text );
insert into t1 values ( 1 );
insert into t1 values ( 100 );
insert into t1 values ( 20 );
insert into t2 values ( 10, 'abc' );
insert into t2 values ( 1, 'xyz' );
(CTRL+D)
create table player ( player_id int primary key, player_name text, team_id );
create table team ( team_id int primary key, team_name text );
insert into player values ( 1 , 'a', 1);
insert into player values ( 2 , 'b', 1);
insert into player values ( 3 , 'c', 2);
insert into player values ( 4 , 'd', 2);
insert into player values ( 5 , 'e', 1);
insert into team values ( 1, 'x');
insert into team values ( 2, 'y');
sqlite3 test.db < test.sql
** ファイル構成 [#va03ff47]
|-- lib
| |-- DB
| | |-- T1.pm
| | `-- T2.pm
| `-- DB.pm
|-- main.pl
`-- test.db
** スキーマクラス・テーブルクラスの作成 [#b658349a]
*** シェルから作成実行 [#xf4181d2]
DBIx::Class::Schema::Loaderのmake_shema_atを使って、スキーマクラス・テーブルクラスを作成する。http://search.cpan.org/perldoc?DBIx::Class::Schema::Loader
- DB.pm スキーマクラス
- T1.pm テーブルクラス
- T2.pm テーブルクラス
perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib
-e 'make_schema_at "MyDB::Schema", {relationships => 1, debug => 1}, ["dbi:SQLite:test.db"]'
** ファイル詳細 [#e63ee31a]
*** main.pl [#ff6fd8d6]
#!/usr/local/bin/perl
*** 作成されたファイルの構成 [#va03ff47]
lib
`-- MyDB
|-- Schema
| |-- Player.pm テーブルクラス DBIx::Class
| `-- Team.pm テーブルクラス DBIx::Class
`-- Schema.pm スキーマクラス DBIx::Class::Schema
*** Schema.pm [#a29ae65b]
package MyDB::Schema;
use strict;
use warnings;
use lib 'lib';
use DB;
use base 'DBIx::Class::Schema';
my $schema = DB->connect('dbi:SQLite:dbname=test.db', '', '');
my $rs = $schema->resultset('T2');
my @rows = $rs->all;
foreach my $r (@rows) {
print $r->num," : ",$r->str,"\n";
}
__PACKAGE__->load_classes;
*** lib/DB.pm [#b34898d5]
package DB;
*** Player.pm [#e4098674]
package MyDB::Schema::Player;
use strict;
use warnings;
use base qw/DBIx::Class::Schema/;
use base 'DBIx::Class';
__PACKAGE__->load_classes(qw/T1 T2/);
1;
__PACKAGE__->load_components("Core");
__PACKAGE__->table("player");
__PACKAGE__->add_columns(
"player_id",
{ data_type => "int", default_value => undef, is_nullable => 1, size => undef },
"player_name",
{
data_type => "text",
default_value => undef,
is_nullable => 1,
size => undef,
},
"team_id",
{ data_type => "", default_value => undef, is_nullable => 1, size => undef },
);
__PACKAGE__->set_primary_key("player_id");
*** lib/DB/T1.pm [#h5c5b30c]
package DB::T1;
テーブルジョイン出来るように以下の行をPlayer.pmに追加する。http://search.cpan.org/perldoc?DBIx::Class::Manual::Joining
__PACKAGE__->belongs_to('team', 'MyDB::Schema::Team', 'team_id');
*** Team.pm [#j8f59a78]
package MyDB::Schema::Team;
use strict;
use warnings;
use base qw/DBIx::Class/;
use base 'DBIx::Class';
__PACKAGE__->load_components(qw/PK::Auto Core/);
__PACKAGE__->table('t1');
__PACKAGE__->add_columns(qw/ num /);
__PACKAGE__->set_primary_key('num');
1;
__PACKAGE__->load_components("Core");
__PACKAGE__->table("team");
__PACKAGE__->add_columns(
"team_id",
{ data_type => "int", default_value => undef, is_nullable => 1, size => undef },
"team_name",
{
data_type => "text",
default_value => undef,
is_nullable => 1,
size => undef,
},
);
__PACKAGE__->set_primary_key("team_id");
*** lib/DB/T2.pm [#x97ca8e4]
package DB::T2;
use strict;
use warnings;
use base qw/DBIx::Class/;
__PACKAGE__->load_components(qw/PK::Auto Core/);
__PACKAGE__->table('t2');
__PACKAGE__->add_columns(qw/ num str /);
__PACKAGE__->set_primary_key('num');
1;
*** 注意 [#p70892a0]
スキーマクラスで、load_classes()ではなく、load_namespaces()を使う場合、DBディレクトリ内にResultディレクトリを作成し、その中にT1.pmとT2.pmを置く。現在はResultディレクトリを使う構成の方が推奨されているようだ。
スキーマクラスで、load_classes()ではなく、load_namespaces()を使う場合、MyDB/Schemaディレクトリ内にResultディレクトリを作成し、その中にPlayer.pmとTeam.pmを置く。現在はResultディレクトリを使う構成の方が推奨されているようだ。
詳しくは、http://search.cpan.org/perldoc?DBIx::Class::Schema 。
** DB操作スクリプト例 [#r7c9ecc4]
use lib './lib';
use MyDB::Schema;
my $schema = MyDB::Schema->connect('dbi:SQLite:dbname=test.db', '', '');
my $rs = $schema->resultset('Player');
my @rows = $rs->all;
foreach my $r (@rows) {
print join(':', ($r->player_id,$r->player_name,$r->team->team_name)),"\n";
}