DBIx::Skinnyと仲間たち

  • 3,434 views
Uploaded on

casual-perl talks #1

casual-perl talks #1

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
3,434
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. DBIx::Skinny Ryo Miyake - nekoya - (id:studio-m)
  • 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 •
  • 4. DBIx::SKinny by nekokak
  • 5. SQL DBIx::Skinny::Manual::JA::Intro
  • 6. • CPAN • github • http://github.com/nekokak/p5-dbix-skinny
  • 7. DBI ORM • SQL • Row
  • 8. • MyApp::DB, MyApp::DB::Schema
  • 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); }; };
  • 12. trigger trigger
  • 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; }; };
  • 14. • inflate/deflate • trigger trigger created_at pre_insert
  • 15. common_trigger (ry
  • 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/
  • 19. DBIx::Skinny::Schema::Loader
  • 20. MyApp::DB::Schema install_table • pk columns DB • http://github.com/nekoya/p5-dbix-skinny-schema- loader
  • 21. : load_schema package Your::DB::Schema; use base qw/DBIx::Skinny::Schema::Loader/; __PACKAGE__->load_schema;
  • 22. : 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;
  • 23. : 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 );
  • 24. : make_schema_at $ perl publish_schema.pl > Your/DB/Schema.pm • DBIC • Skinny Schema 1 •
  • 25. 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; }; ...
  • 26. make_schema_at print make_schema_at( 'Mock::DB::Schema', { before_template => $before, }, [ 'dbi:SQLite:test.db', '', '' ] ); • after_template
  • 27. table_template • install_table • TT table, pk, columns install_table [% table %] => schema { pk '[% pk %]'; columns qw/[% columns %]/; };
  • 28. DBIx::Skinny:: InflateColumn::DateTime
  • 29. 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
  • 30. DBIx::Skinny::InflateColumn::DateTime • MySQL • timestamp now() • insert select • Perl • …
  • 31. 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/; };
  • 32. Ark::Plugin::Authentication:: Store::DBIx::Skinny
  • 33. Ark by typester • Catalyst • http://typester.stfuawsc.com/slides/yapsasia2009- ark/ • Ark • Ark + Skinny
  • 34. 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 };
  • 35. config 'Plugin::Authentication::Store::DBIx::Skinny' => { model => 'db', table => 'members', user_field => 'name', }; • DBIC •
  • 36. DBIx::Skinny::AR
  • 37. DBIx::Skinny::AR • Any::Moose • ActiveRecord • http://github.com/nekoya/p5-dbix-skinny-ar
  • 38. DBIx::Skinny::AR • Model • DB
  • 39. DBIx::Skinny::AR DBIC • Row use base 'DBIx::Class' • Model DB • DBIC • pixis …
  • 40. DBIx::Skinny::AR DBIx::Skinny (ry
  • 41. 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/ };
  • 42. • MyApp::DB::Schema MyApp/DB.pm • MyApp::DB use DBIx::Skinny setup => { dsn => 'dbi:SQLite:test.db', username => '', password => '', }; MyApp::DB->connect($conf->{ database });
  • 43. DBIx::Skinny::AR package MyApp::AR; use Any::Moose; extends 'DBIx::Skinny::AR'; __PACKAGE__->setup('MyApp::DB'); 1;
  • 44. 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', );
  • 45. has 'title' => ( is => 'rw', isa => 'Str', traits => [qw/Unique/], ); __PACKAGE__->belongs_to('author'); 1;
  • 46. 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' } } );
  • 47. Relationships • belongs_to • has_one • has_many • many_to_many
  • 48. Relationships • $book->author • has 'author' => ( is => 'ro', isa => 'MyApp::Author', clearer => 'clear_author', lazy => 1, default => sub { … } );
  • 49. Relationships DB • $book->author->clear_author • $book->author->reload
  • 50. Trait::Unique has 'title' => ( is => 'rw', isa => 'Str', traits => [qw/Unique/], ); • • • SQL
  • 51. • perldoc DBIx::Skinny • • SQLite •
  • 52. IRC #dbix-skinny at perl.org ( irc.perl.org ) #perl-casual at Freenode ( irc.freenode.net )
  • 53. Skinny Skinny [eof]