• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Introduction to DBIx::Class
 

Introduction to DBIx::Class

on

  • 1,771 views

 

Statistics

Views

Total Views
1,771
Views on SlideShare
1,771
Embed Views
0

Actions

Likes
3
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

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

    Introduction to DBIx::Class Introduction to DBIx::Class Presentation Transcript

    • Introduction to DBIx::ClassDoran BartonBluehostdoran@bluehost.com
    • Background: DBI● DBI is awesome– Abstraction layer between code and databasebackend– “Easy” to migrate code from one RDBMS to another
    • DBIx::Class● Is an ORM class
    • ORM: Object Relational Mapper● Database tables become objects● Table data and relationships between tables becomeobject methods● Example ORMs:– Hibernate (Java)– SQLAlchemy (Python)– CakePHP (PHP)– DBIx::Class (Perl)
    • DBIx::Class● Founded by Matt S. Trout (mst)● Website: http://www.dbix-class.org/
    • DBIx::Class Benefits● No more writing SQL– (You still can, if needed.)● Ease of use– Put junior developers to work faster
    • DBIx::Class Caveats● No SQL– Figuring out how to express complex queries can beirksome● Startup overhead– DBIx::Class leverages lots of other CPAN modules● Result class code– Rewriting CREATE TABLEs in DBIx::Class result classcode may seem redundant
    • Using DBIx::Class● Write your schema class● Write a result class for each table
    • Example: Simple blog schemablog_post commentauthor
    • Example: Schema classpackage MyBlog::Schema;use base qw/DBIx::Class::Schema/;__PACKAGE__­>load_namespaces();1; 
    • Example: blog_postCREATE TABLE blog_post (    post_id         SERIAL          PRIMARY KEY,    headline        VARCHAR(100)    NOT NULL,    post_timestamp  TIMESTAMP       NOT NULL DEFAULT NOW(),    author_id       INTEGER         NOT NULL        REFERENCES author(author_id),    body            TEXT            NOT NULL);package MyBlog::Schema::Result::BlogPost;use base qw/ DBIx::Class::Core /;__PACKAGE__­>table(blog_post);__PACKAGE__­>add_columns(    qw/post_id headline post_timestamp author_id body/);__PACKAGE__­>set_primary_key(post_id);__PACKAGE__­>belongs_to(    author => MyBlog::Schema::Result::Author,    author_id);__PACKAGE__­>has_many(    comments => MyBlog::Schema::Result::Comment,    post_id);
    • Example: authorCREATE TABLE author (    author_id       SERIAL          PRIMARY KEY,    username        VARCHAR(20)     UNIQUE NOT NULL,    enc_password    VARCHAR(100)    NOT NULL,    first_name      VARCHAR(100)    NOT NULL,    last_name       VARCHAR(100)    NOT NULL,    email           VARCHAR(300)    NOT NULL,    can_blog        BOOLEAN         NOT NULL DEFAULT f);package MyBlog::Schema::Result::Author;use base qw/ DBIx::Class::Core /;__PACKAGE__­>table(author);__PACKAGE__­>add_columns(    qw/author_id username enc_password first_name last_name       email can_blog/);__PACKAGE__­>set_primary_key(author_id);__PACKAGE__­>has_many(    posts => MyBlog::Schema::Result::BlogPost,    author_id);__PACKAGE__­>has_many(    comments => MyBlog::Schema::Result::Comment,    author_id);1;
    • Example: commentCREATE TABLE comment (    comment_id      SERIAL      PRIMARY KEY,    post_id         INTEGER     NOT NULL        REFERENCES blog_post(post_id),    author_id       INTEGER     NOT NULL        REFERENCES author(author_id),    comment_dt      TIMESTAMP   NOT NULL DEFAULT NOW(),    body            TEXT        NOT NULL);package MyBlog::Schema::Result::Comment;use base qw/ DBIx::Class::Core /;__PACKAGE__­>table(comment);__PACKAGE__­>add_columns(    qw/comment_id post_id author_id comment_dt body/);__PACKAGE__­>set_primary_key(comment_id);__PACKAGE__­>belongs_to(    author => MyBlog::Schema::Result::Author,    author_id);__PACKAGE__­>belongs_to(    post => MyBlog::Schema::Result::BlogPost,    post_id);1;
    • Using DBIC classesuse feature say;use MyBlog::Schema;use Lingua::EN::Inflect qw/PL_N PL_V/;my $schema = MyBlog::Schema­>connect(dbi:Pg:dbname=myblog);my @posts = $schema­>resultset(BlogPost)­>search(    { author.username => fozz, },    { prefetch          =>  author},)­>all;say "There ",     PL_V("is", scalar @posts), " ",     scalar @posts, " ",     PL_N("post", scalar @posts), " by this author.";
    • DBIx::Class::Schema::Loader● Creates result classes for you– On-the-fly or one-time● Can also generate CREATE TABLE commands fromschema classes
    • Example: dbicdump● $ dbicdump -o dump_directory=./lib -o components=["InflateColumn::DateTime"] MyBlog::Schema dbi:Pg:dbname=myblog
    • Dynamic DBIC● In Schema class:package MyBlog::Schema;use base qw/DBIx::Class::Schema::Loader/;
    • Components/Inflating● DBIx::Class makes it easy to coerce a table field into anobject.– __PACKAGE__­>load_components("InflateColumn::DateTime");● Then, specify the column type as datetime– __PACKAGE__­>add_columns(    ...    post_timestamp => { type => datetime },    ...);● For more info, see DBIx::Class::InflateColumn::DateTime
    • Other inflators● DBIx::Class::InflateColumn::IP– Coerces field into NetAddr::IP● DBIx::Class::InflateColumn::URI– Coerces field into URI● DBIx::Class::InflateColumn::Currency– Coerces field into Data::Currency● And you can roll your own!
    • Learning more● Excellent POD– DBIx::Class::Manual::DocMap– DBIx::Class::Manual::Intro– DBIx::Class::Manual::Cookbook● http://www.dbix-class.org/● Mailing list– http://lists.scsys.co.uk/mailman/listinfo/dbix-class● IRC– irc.perl.org#dbix-class