Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to DBIx::Class

3,213 views

Published on

Published in: Technology
  • Be the first to comment

Introduction to DBIx::Class

  1. 1. Introduction to DBIx::ClassDoran BartonBluehostdoran@bluehost.com
  2. 2. Background: DBI● DBI is awesome– Abstraction layer between code and databasebackend– “Easy” to migrate code from one RDBMS to another
  3. 3. DBIx::Class● Is an ORM class
  4. 4. 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)
  5. 5. DBIx::Class● Founded by Matt S. Trout (mst)● Website: http://www.dbix-class.org/
  6. 6. DBIx::Class Benefits● No more writing SQL– (You still can, if needed.)● Ease of use– Put junior developers to work faster
  7. 7. 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
  8. 8. Using DBIx::Class● Write your schema class● Write a result class for each table
  9. 9. Example: Simple blog schemablog_post commentauthor
  10. 10. Example: Schema classpackage MyBlog::Schema;use base qw/DBIx::Class::Schema/;__PACKAGE__­>load_namespaces();1; 
  11. 11. 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);
  12. 12. 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;
  13. 13. 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;
  14. 14. 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.";
  15. 15. DBIx::Class::Schema::Loader● Creates result classes for you– On-the-fly or one-time● Can also generate CREATE TABLE commands fromschema classes
  16. 16. Example: dbicdump● $ dbicdump -o dump_directory=./lib -o components=["InflateColumn::DateTime"] MyBlog::Schema dbi:Pg:dbname=myblog
  17. 17. Dynamic DBIC● In Schema class:package MyBlog::Schema;use base qw/DBIx::Class::Schema::Loader/;
  18. 18. 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
  19. 19. 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!
  20. 20. 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

×