Más contenido relacionado La actualidad más candente (20) Similar a DBIx::Skinnyと仲間たち (20) DBIx::Skinnyと仲間たち2. ,. -‐'''''""¨
(. _,,,... - |
|i i| }! }} / |
|l { j} /,, // 10 LT
i|:! _ u {:}//
| u' } , _,!V, |
´f _{ {, ' , 55
/' | | {´,) `/ |< i
,
| _
) iL u' | |
! ⊇ ' :} V::::: 5 LT
/ 7'T'' u' __ /:::::::/
/'´r -— ‐ ´ '"´ :::: -
/ // ¨´ /' ::::: ´
'/ :::::` - ___ ::::: / }
_ /:::::::::::::::::::::::::: ` -{:::...
3. • nekoya
• id:studio-m
• http://twitter.com/nekoya
• http://wassr.jp/user/nekoya
•
9. • Row DBIx::Skinny::Row
• MyApp::DB::Row::{Table}
10. rule inflate/deflate
inflate/deflate
install_inflate_rule '^.+_at$' => callback {
inflate {
my $value = shift;
return $value;
};
deflate {
my $value = shift;
return $value;
};
};
11. rule inflate/deflate
my $timezone = DateTime::TimeZone->new(name => 'Asia/Tokyo');
install_inflate_rule '^.+_at$' => callback {
inflate {
my $value = shift;
my $dt = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d %H:%M:%S',
time_zone => $timezone,
)->parse_datetime($value);
return DateTime->from_object( object => $dt );
};
deflate {
my $value = shift;
return DateTime::Format::MySQL->format_datetime($value);
};
};
13. trigger
install_table 'user' => schema {
pk 'id';
columns qw/id name mail created_at updated_at/;
trigger pre_insert => sub {
my ( $class, $args ) = @_;
$args->{created_at} ||= DateTime->now;
};
trigger pre_update => sub {
my ( $class, $args ) = @_;
$args->{updated_at} ||= DateTime->now;
};
};
16. trigger
trigger common_trigger
install_common_trigger pre_insert => sub {
my ($self, $args) = @_;
$args->{created_at} ||= DateTime->now;
};
• trigger
17. trigger
install_common_trigger pre_insert => sub {
my ($self, $args, $table) = @_;
my $columns = $self->schema->schema_info
->{$table}->{columns};
$args->{created_at} ||= DateTime->now
if grep {/^created_at$/} @$columns;
};
trigger
18. nekokak YAPC
• http://nekokak.org/presen/yapcasia2009-dbix-
skinny/
github
• http://github.com/nekokak/p5-dbix-skinny/blob/
master/lib/DBIx/Skinny/Manual/JA/
21. MyApp::DB::Schema install_table
• pk columns DB
• http://github.com/nekoya/p5-dbix-skinny-schema-
loader
23. : load_schema
package Your::DB::Schema;
use base qw/DBIx::Skinny::Schema::Loader/;
install_table books => schema {
trigger pre_insert => sub {
my ($class, $args) = @_;
$args->{ name } = 'HOGE';
}
};
__PACKAGE__->load_schema;
1;
24. : make_schema_at
publish_schema.pl
use DBIx::Skinny::Schema::Loader
qw/make_schema_at/;
print make_schema_at(
'Your::DB::Schema', # Schema class
{ }, # options
[ 'dbi:SQLite:test.db', '', '' ] # connect info
);
26. make_schema_at
my $tmpl = << '...';
# custom template
install_utf8_columns qw/jpname title content/;
install_common_trigger pre_insert => sub {
my ($self, $args) = @_;
$args->{created_at} ||= DateTime->now;
};
...
30. DBIx::Skinny::InflateColumn::DateTime
• _at, _on DateTime inflate/deflate
• created_xx, updated_xx
inspired
DBIx::Class::InflateColumn::DateTime::Auto
(hidek++)
http://blog.hide-k.net/archives/2006/08/
dbixclassauto_i.phpdbixclassauto_i.php
34. Ark by typester
• Catalyst
• http://typester.stfuawsc.com/slides/yapsasia2009-
ark/
• Ark
• Ark + Skinny
35. package MyApp;
use Ark;
use MyApp::Models;
use_model 'MyApp::Models';
use_plugins qw{
Session
Session::State::Cookie
Session::Store::Memory
Authentication
Authentication::Credential::Password
Authentication::Store::DBIx::Skinny
};
43. • MyApp::DB::Schema MyApp/DB.pm
• MyApp::DB
use DBIx::Skinny setup => {
dsn => 'dbi:SQLite:test.db',
username => '',
password => '',
};
MyApp::DB->connect($conf->{ database });
46. has 'title' => (
is => 'rw',
isa => 'Str',
traits => [qw/Unique/],
);
__PACKAGE__->belongs_to('author');
1;
47. find
my $book = MyApp::Book->find(1);
my $book = MyApp::Book->find({ title => ‘hoge’ });
my $books = MyApp::Book->find_all;
my $latests = MyApp::Book->find_all(
{ author_id => $author_id },
{ order_by => { id => 'desc' } }
);