|
NAMEData::Model::Tutorial::JA - Data::Model::TutorialXXXXData::Model XXid:yappo XXXXXXXXX O/R MapperXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXData::Model XXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXX XXX Data::Model XXXXXXXX Data::Model Track - JPerl Advent Calendar 2009 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX <http://perl-users.jp/articles/advent-calendar/2009/data-model/> XXXXXXXXXXXXXXXX POD XXXXXXXX XXXXXXDBI XXXXXXXXXXData::Model XXData::Model XXXXXXXXXCREATE TABLE XXXXXXXXXXX XXXXXXXXXXXXXXXXX Perl XXXXXX# lib/Neko/DB/User.pm package Neko::DB::User; use strict; use warnings; use base 'Data::Model'; use Data::Model::Schema sugar => 'myapp'; use Neko::Columns; install_model user => schema { # primary key key 'id'; # XXXXX column 'user.id' => { auto_increment => 1 }; utf8_column 'user.name'; }; 1; XXXXXXXXXXXXXXXXXXXXX # lib/Neko/Columns.pm package Neko::Columns; use strict; use warnings; use Data::Model::Schema sugar => 'myapp'; column_sugar 'user.id' => int => { required => 1, unsigned => 1, }; column_sugar 'user.name' => 'varchar' => { required => 1, size => 255, }; 1; XXXXXXXXXXXXXXXXXXXXXXXXX O/R Mapper XXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXX created_on XXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXCREATE TABLE XXXXXXXXX# script/dump_schema.pl use strict; use warnings; use Neko::DB::User; use Data::Model::Driver::DBI; my $dm = Neko::DB::User->new(); do { # XXXXXXXXXXX my $driver = Data::Model::Driver::DBI->new( dsn => 'dbi:SQLite:' ); $dm->set_base_driver($driver); }; for my $target ($dm->schema_names) { for my $sql ($dm->as_sqls($target)) { print "$sql\n"; } } XXXXXXXXdriver XX schema XXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX # script/crud.pl use strict; use warnings; use Test::More tests => 10; use Neko::DB::User; use Data::Model::Driver::DBI; my $dm = Neko::DB::User->new(); # XXXXXXXXXXX { my $driver = Data::Model::Driver::DBI->new( dsn => 'dbi:SQLite:' ); $dm->set_base_driver($driver); } # schema XXXXXXX for my $target ($dm->schema_names) { my $dbh = $dm->get_driver($target)->rw_handle; for my $sql ($dm->as_sqls($target)) { $dbh->do($sql); } } # INSERT XXXX $dm->set( 'user' => { name => 'yappo' }); $dm->set('user' => { name => 'ukonmanaho' }); # SELECT XXXX # XXXXXXXXXXXXXXXXXX { my $iterator = $dm->get('user' => { order => {'id' => 'ASC'} }); my @names; while (my $row = $iterator->next) { push @names, $row->name; } is join(',', @names), 'yappo,ukonmanaho'; } # XXXXXXXXXXXXXXX { my @users = $dm->get('user' => {order => { 'id' => 'DESC' }}); is scalar(@users), 2; is $users[0]->name, 'ukonmanaho'; is $users[1]->name, 'yappo'; } # XXXXXXX { my @users = $dm->get('user' => { where => [ name => 'yappo' ], }); is scalar(@users), 1; is $users[0]->name, 'yappo'; } # update { my ($ukon, ) = $dm->get('user' => { where => [ name => 'ukonmanaho' ], }); is $ukon->name, 'ukonmanaho'; $ukon->name('jack'); $ukon->update; } # delete { my $count_users = sub { scalar(my @users = $dm->get('user')); }; is $count_users->(), 2; my ($jack, ) = $dm->get('user' => { where => [ name => 'jack' ], }); is $jack->name, 'jack'; $jack->delete; is $count_users->(), 1; } XXXXX SELECT/INSERT/UPDATE/DELETE XXXXXXXXXXXX Memcached XXXXXXXData::Model XXXXXXXXXX SQLite XXX memcached protocol XXXXXXX XXXXXXXXXXXXXXXXXXXX hash database XXXXXXXXXXXXX XXXXXXXTokyo Tyrant XXXXXXXXXXXXData::Model XXXXXXXXX XXXXXXXXXMemcached XXXXXXXXXXXXXXXXXXXXXXXXXX # script/memcached.pl use strict; use warnings; use Test::More tests => 2; use Neko::DB::User; use Data::Model::Driver::Memcached; use Cache::Memcached::Fast; my $dm = Neko::DB::User->new(); # XXXXXXXXXXX { my $driver = Data::Model::Driver::Memcached->new( memcached => Cache::Memcached::Fast->new({ servers => [ '127.0.0.1:11211', ], }), ); warn $dm->set_base_driver($driver); } # INSERT warn $dm->set( 'user' => 1, { name => 'yappo' }); warn $dm->set('user' => 2, { name => 'ukonmanaho' }); # SELECT { my ($yappo) = $dm->get('user' => 1); warn $yappo; is $yappo->name, 'yappo'; } { my ($ukonmanaho) = $dm->get('user' => 2); warn $ukonmanaho; is $ukonmanaho->name, 'ukonmanaho'; } XXXXXXXXXXXDBI X Memcached XXXXXXXXXXXXXXXXX Memcached XXXXXXXXX XXXXXXXXXXXXXXQ4MXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXgetlookuplookup_multisetreplaceupdatedeleteMixin XXXXtrigger XXXXXXXXXXXXXXXtokuhirom (original http://github.com/tokuhirom/data-model-tutorial/tree/master)yappo (XXXX)
Visit the GSP FreeBSD Man Page Interface. |