DBIx::Skinny

     Ryo Miyake - nekoya -
         (id:studio-m)
,. -‐'''''""¨
                                (.     _,,,... -        |
                              |i i|      }! }} / |...
•               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 ...
rule                      inflate/deflate
my $timezone = DateTime::TimeZone->new(name => 'Asia/Tokyo');
install_inflate_rule ...
trigger

trigger
trigger
install_table 'user' => schema {
   pk 'id';
   columns qw/id name mail created_at updated_at/;
   trigger pre_ins...
• inflate/deflate
• trigger
    trigger

    created_at    pre_insert
common_trigger (ry
trigger
trigger                common_trigger

install_common_trigger pre_insert => sub {
   my ($self, $args) = @_;
   $a...
trigger

install_common_trigger pre_insert => sub {
   my ($self, $args, $table) = @_;
   my $columns = $self->schema->sch...
nekokak        YAPC

• http://nekokak.org/presen/yapcasia2009-dbix-
  skinny/


github

• http://github.com/nekokak/p5-dbi...
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 {
   tri...
: make_schema_at
publish_schema.pl
   use DBIx::Skinny::Schema::Loader
         qw/make_schema_at/;


   print make_schema...
: 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_trigg...
make_schema_at

print make_schema_at(
     'Mock::DB::Schema',
     {
         before_template => $before,
     },
     [ ...
table_template

 • install_table
 • TT         table, pk, columns

install_table [% table %] => schema {
     pk '[% pk %]...
DBIx::Skinny::
InflateColumn::DateTime
DBIx::Skinny::InflateColumn::DateTime

• _at, _on            DateTime           inflate/deflate

• created_xx, updated_xx
   ...
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::D...
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...
config 'Plugin::Authentication::Store::DBIx::Skinny' => {
   model     => 'db',
   table    => 'members',
   user_field => '...
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...
• MyApp::DB::Schema          MyApp/DB.pm
• MyApp::DB
   use DBIx::Skinny setup => {
      dsn => 'dbi:SQLite:test.db',
   ...
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 '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;
...
Relationships
• belongs_to
• has_one
• has_many
• many_to_many
Relationships
• $book->author
•
   has 'author' => (
      is    => 'ro',
      isa => 'MyApp::Author',
      clearer => '...
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]
DBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たち
Upcoming SlideShare
Loading in...5
×

DBIx::Skinnyと仲間たち

3,663

Published on

casual-perl talks #1

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,663
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • DBIx::Skinnyと仲間たち

    1. 1. DBIx::Skinny Ryo Miyake - nekoya - (id:studio-m)
    2. 2. ,. -‐'''''""¨ (. _,,,... - | |i i| }! }} / | |l { j} /,, // 10 LT i|:! _ u {:}// | u' } , _,!V, | ´f _{ {, ' , 55 /' | | {´,) `/ |< i , | _ ) iL u' | | ! ⊇ ' :} V::::: 5 LT / 7'T'' u' __ /:::::::/ /'´r -— ‐ ´ '"´ :::: - / // ¨´ /' ::::: ´ '/ :::::` - ___ ::::: / } _ /:::::::::::::::::::::::::: ` -{:::...
    3. 3. • nekoya • id:studio-m • http://twitter.com/nekoya • http://wassr.jp/user/nekoya •
    4. 4. DBIx::SKinny by nekokak
    5. 5. SQL DBIx::Skinny::Manual::JA::Intro
    6. 6. • CPAN • github • http://github.com/nekokak/p5-dbix-skinny
    7. 7. DBI ORM • SQL • Row
    8. 8. • MyApp::DB, MyApp::DB::Schema
    9. 9. • Row DBIx::Skinny::Row • MyApp::DB::Row::{Table}
    10. 10. rule inflate/deflate inflate/deflate install_inflate_rule '^.+_at$' => callback { inflate { my $value = shift; return $value; }; deflate { my $value = shift; return $value; }; };
    11. 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. 12. trigger trigger
    13. 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. 14. • inflate/deflate • trigger trigger created_at pre_insert
    15. 15. common_trigger (ry
    16. 16. trigger trigger common_trigger install_common_trigger pre_insert => sub { my ($self, $args) = @_; $args->{created_at} ||= DateTime->now; }; • trigger
    17. 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. 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. 19. DBIx::Skinny::Schema::Loader
    20. 20. MyApp::DB::Schema install_table • pk columns DB • http://github.com/nekoya/p5-dbix-skinny-schema- loader
    21. 21. : load_schema package Your::DB::Schema; use base qw/DBIx::Skinny::Schema::Loader/; __PACKAGE__->load_schema;
    22. 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. 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. 24. : make_schema_at $ perl publish_schema.pl > Your/DB/Schema.pm • DBIC • Skinny Schema 1 •
    25. 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. 26. make_schema_at print make_schema_at( 'Mock::DB::Schema', { before_template => $before, }, [ 'dbi:SQLite:test.db', '', '' ] ); • after_template
    27. 27. table_template • install_table • TT table, pk, columns install_table [% table %] => schema { pk '[% pk %]'; columns qw/[% columns %]/; };
    28. 28. DBIx::Skinny:: InflateColumn::DateTime
    29. 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. 30. DBIx::Skinny::InflateColumn::DateTime • MySQL • timestamp now() • insert select • Perl • …
    31. 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. 32. Ark::Plugin::Authentication:: Store::DBIx::Skinny
    33. 33. Ark by typester • Catalyst • http://typester.stfuawsc.com/slides/yapsasia2009- ark/ • Ark • Ark + Skinny
    34. 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. 35. config 'Plugin::Authentication::Store::DBIx::Skinny' => { model => 'db', table => 'members', user_field => 'name', }; • DBIC •
    36. 36. DBIx::Skinny::AR
    37. 37. DBIx::Skinny::AR • Any::Moose • ActiveRecord • http://github.com/nekoya/p5-dbix-skinny-ar
    38. 38. DBIx::Skinny::AR • Model • DB
    39. 39. DBIx::Skinny::AR DBIC • Row use base 'DBIx::Class' • Model DB • DBIC • pixis …
    40. 40. DBIx::Skinny::AR DBIx::Skinny (ry
    41. 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. 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. 43. DBIx::Skinny::AR package MyApp::AR; use Any::Moose; extends 'DBIx::Skinny::AR'; __PACKAGE__->setup('MyApp::DB'); 1;
    44. 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. 45. has 'title' => ( is => 'rw', isa => 'Str', traits => [qw/Unique/], ); __PACKAGE__->belongs_to('author'); 1;
    46. 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. 47. Relationships • belongs_to • has_one • has_many • many_to_many
    48. 48. Relationships • $book->author • has 'author' => ( is => 'ro', isa => 'MyApp::Author', clearer => 'clear_author', lazy => 1, default => sub { … } );
    49. 49. Relationships DB • $book->author->clear_author • $book->author->reload
    50. 50. Trait::Unique has 'title' => ( is => 'rw', isa => 'Str', traits => [qw/Unique/], ); • • • SQL
    51. 51. • perldoc DBIx::Skinny • • SQLite •
    52. 52. IRC #dbix-skinny at perl.org ( irc.perl.org ) #perl-casual at Freenode ( irc.freenode.net )
    53. 53. Skinny Skinny [eof]

    ×