後学の為のメモとして残します。
まず、Data::ModelをextendsしたMyDBクラス。
$ cat ./lib/MyDB.pm
package MyDB;
use Data::Model::Schema;
install_model member => schema {
key 'id';
columns qw(
id name sex age
);
};
use Mouse;
extends 'Data::Model';
__PACKAGE__->meta->make_immutable( inline_constructor => 0 );
no Mouse;
1;
__END__
MyDBクラスを使ってSQLite上のデータを書き換えるスクリプト。
$ cat hoge.pl
#!/usr/bin/perl
use FindBin;
use lib ("$FindBin::Bin/lib");
use MyDB;
use Data::Dumper;
use Data::Model::Driver::DBI;
my $dbfile = "$FindBin::Bin/mydb.sqlite3";
my $driver = Data::Model::Driver::DBI->new(
dsn => "dbi:SQLite:$dbfile",
);
my $db = MyDB->new;
$db->set_base_driver( $driver );
print Dumper( $db->lookup( member => 1 ) );
my $rec = $db->lookup( member => 1 );
$rec->name( 'YellowTurtle' );
$rec->update;
print Dumper( $db->lookup( member => 1 ) );
実行結果。
$ ./hoge.pl
$VAR1 = bless( {
'alias_values' => {},
'column_values' => {
'name' => 'ytnobody',
'id' => '1',
'sex' => '29',
'age' => '1'
},
'original_cols' => {},
'changed_cols' => {},
'model' => bless( {
'schema_class' => 'MyDB'
}, 'MyDB' )
}, 'MyDB::member' );
$VAR1 = bless( {
'alias_values' => {},
'column_values' => {
'name' => 'YellowTurtle',
'id' => '1',
'sex' => '29',
'age' => '1'
},
'original_cols' => {},
'changed_cols' => {},
'model' => bless( {
'schema_class' => 'MyDB'
}, 'MyDB' )
}, 'MyDB::member' );
0 件のコメント:
コメントを投稿