• Save
DBIx::Skinnyと仲間たち
Upcoming SlideShare
Loading in...5
×
 

DBIx::Skinnyと仲間たち

on

  • 4,583 views

casual-perl talks #1

casual-perl talks #1

Statistics

Views

Total Views
4,583
Views on SlideShare
4,190
Embed Views
393

Actions

Likes
0
Downloads
0
Comments
0

4 Embeds 393

http://d.hatena.ne.jp 334
http://webcache.googleusercontent.com 48
http://www.slideshare.net 9
http://translate.googleusercontent.com 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

DBIx::Skinnyと仲間たち DBIx::Skinnyと仲間たち Presentation Transcript

  • DBIx::Skinny Ryo Miyake - nekoya - (id:studio-m)
  • ,. -‐'''''""¨ (. _,,,... - | |i i| }! }} / | |l { j} /,, // 10 LT i|:! _ u {:}// | u' } , _,!V, | ´f _{ {, ' , 55 /' | | {´,) `/ |< i , | _ ) iL u' | | ! ⊇ ' :} V::::: 5 LT / 7'T'' u' __ /:::::::/ /'´r -— ‐ ´ '"´ :::: - / // ¨´ /' ::::: ´ '/ :::::` - ___ ::::: / } _ /:::::::::::::::::::::::::: ` -{:::...
  • • nekoya • id:studio-m • http://twitter.com/nekoya • http://wassr.jp/user/nekoya •
  • DBIx::SKinny by nekokak
  • SQL DBIx::Skinny::Manual::JA::Intro
  • • CPAN • github • http://github.com/nekokak/p5-dbix-skinny
  • DBI ORM • SQL • Row
  • • MyApp::DB, MyApp::DB::Schema
  • • Row DBIx::Skinny::Row • MyApp::DB::Row::{Table}
  • rule inflate/deflate inflate/deflate install_inflate_rule '^.+_at$' => callback { inflate { my $value = shift; return $value; }; deflate { my $value = shift; return $value; }; };
  • 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); }; };
  • trigger trigger
  • 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; }; };
  • • inflate/deflate • trigger trigger created_at pre_insert
  • common_trigger (ry
  • trigger trigger common_trigger install_common_trigger pre_insert => sub { my ($self, $args) = @_; $args->{created_at} ||= DateTime->now; }; • trigger
  • 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
  • nekokak YAPC • http://nekokak.org/presen/yapcasia2009-dbix- skinny/ github • http://github.com/nekokak/p5-dbix-skinny/blob/ master/lib/DBIx/Skinny/Manual/JA/
  • DBIx::Skinny::Schema::Loader
  • MyApp::DB::Schema install_table • pk columns DB • http://github.com/nekoya/p5-dbix-skinny-schema- loader
  • : load_schema package Your::DB::Schema; use base qw/DBIx::Skinny::Schema::Loader/; __PACKAGE__->load_schema;
  • : 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;
  • : 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 );
  • : make_schema_at $ perl publish_schema.pl > Your/DB/Schema.pm • DBIC • Skinny Schema 1 •
  • 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; }; ...
  • make_schema_at print make_schema_at( 'Mock::DB::Schema', { before_template => $before, }, [ 'dbi:SQLite:test.db', '', '' ] ); • after_template
  • table_template • install_table • TT table, pk, columns install_table [% table %] => schema { pk '[% pk %]'; columns qw/[% columns %]/; };
  • DBIx::Skinny:: InflateColumn::DateTime
  • 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
  • DBIx::Skinny::InflateColumn::DateTime • MySQL • timestamp now() • insert select • Perl • …
  • DBIx::Skinny::InflateColumn::DateTime package Your::DB::Schema; use DBIx::Skinny::Schema; use DBIx::Skinny::InflateColumn::DateTime; install_table table1 => { pk 'id'; columns qw/id name created_at updated_at/; }; install_table table2 => { pk 'id'; columns qw/id name booked_on created_on updated_on/; };
  • Ark::Plugin::Authentication:: Store::DBIx::Skinny
  • Ark by typester • Catalyst • http://typester.stfuawsc.com/slides/yapsasia2009- ark/ • Ark • Ark + Skinny
  • 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 };
  • config 'Plugin::Authentication::Store::DBIx::Skinny' => { model => 'db', table => 'members', user_field => 'name', }; • DBIC •
  • DBIx::Skinny::AR
  • DBIx::Skinny::AR • Any::Moose • ActiveRecord • http://github.com/nekoya/p5-dbix-skinny-ar
  • DBIx::Skinny::AR • Model • DB
  • DBIx::Skinny::AR DBIC • Row use base 'DBIx::Class' • Model DB • DBIC • pixis …
  • DBIx::Skinny::AR DBIx::Skinny (ry
  • DBIx::Skinny::AR package MyApp::DB; use DBIx::Skinny; package MyApp::DB::Schema; use DBIx::Skinny::Schema; install_table books => schema { pk 'id'; columns qw/id author_id title/ };
  • • MyApp::DB::Schema MyApp/DB.pm • MyApp::DB use DBIx::Skinny setup => { dsn => 'dbi:SQLite:test.db', username => '', password => '', }; MyApp::DB->connect($conf->{ database });
  • DBIx::Skinny::AR package MyApp::AR; use Any::Moose; extends 'DBIx::Skinny::AR'; __PACKAGE__->setup('MyApp::DB'); 1;
  • package Mock::Book; use Any::Moose; extends 'Mock::AR'; use Carp; has 'id' => ( is => 'rw', isa => 'Undef | Int', ); has 'author_id' => ( is => 'rw', isa => 'Undef | Int', );
  • has 'title' => ( is => 'rw', isa => 'Str', traits => [qw/Unique/], ); __PACKAGE__->belongs_to('author'); 1;
  • 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' } } );
  • Relationships • belongs_to • has_one • has_many • many_to_many
  • Relationships • $book->author • has 'author' => ( is => 'ro', isa => 'MyApp::Author', clearer => 'clear_author', lazy => 1, default => sub { … } );
  • Relationships DB • $book->author->clear_author • $book->author->reload
  • Trait::Unique has 'title' => ( is => 'rw', isa => 'Str', traits => [qw/Unique/], ); • • • SQL
  • • perldoc DBIx::Skinny • • SQLite •
  • IRC #dbix-skinny at perl.org ( irc.perl.org ) #perl-casual at Freenode ( irc.freenode.net )
  • Skinny Skinny [eof]