1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
7 <title>DBIx::Class (aka DBIC)</title>
9 <meta name="contributor" content="youcan[64]netzgesta[46]de" />
10 <meta name="publisher" content="s5.netzgesta.de" />
11 <meta name="description" content="S5 1.3 is a very flexible and lightweight slide show system available for anyone to use (including transitions and scalable fonts and images)" />
12 <meta name="keywords" content="S5, version 1.3, slide show, presentation-mode, projection-mode, powerpoint-like, scala-like, keynote-like, incremental display, scalable fonts, scalable images, transitions, notes, osf, xoxo, css, javascript, xhtml, public domain" />
14 <meta name="generator" content="S5" />
15 <meta name="version" content="1" />
16 <meta name="subject" content="DBIx::Class" />
17 <meta name="author" content="See first slide" />
18 <meta name="company" content="N/A" />
19 <meta name="robots" content="index, follow" />
20 <meta name="revisit-after" content="7 days" />
21 <!-- meta additionally -->
22 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
23 <meta http-equiv="Content-Script-Type" content="text/javascript" />
24 <meta http-equiv="Content-Style-Type" content="text/css" /><!-- configuration parameters -->
25 <meta name="defaultView" content="slideshow" />
26 <meta name="controlVis" content="hidden" />
27 <!-- configuration transition extension -->
28 <meta name="tranSitions" content="true" />
29 <meta name="fadeDuration" content="500" />
30 <meta name="incrDuration" content="250" />
31 <!-- configuration autoplay extension -->
32 <meta name="autoMatic" content="false" />
33 <meta name="playLoop" content="true" />
34 <meta name="playDelay" content="10" />
35 <!-- configuration audio extension -->
36 <meta name="audioSupport" content="false" />
37 <meta name="audioVolume" content="0" />
38 <meta name="audioError" content="false" />
39 <!-- configuration audio debug -->
40 <meta name="audioDebug" content="false" />
41 <!-- style sheet links -->
42 <link rel="stylesheet" href="ui/scala_utf/slides.css" type="text/css" media="projection" id="slideProj" />
43 <link rel="stylesheet" href="ui/scala_utf/outline.css" type="text/css" media="screen" id="outlineStyle" />
44 <link rel="stylesheet" href="ui/scala_utf/print.css" type="text/css" media="print" id="slidePrint" />
45 <link rel="stylesheet" href="ui/scala_utf/opera.css" type="text/css" media="projection" id="operaFix" />
46 <!-- embedded styles -->
47 <style type="text/css" media="all">
48 .imgcon {width: 100%; margin: 0 auto; padding: 0; text-align: center;}
49 #anim {width: 33%; height: 320px; position: relative;}
50 #anim img {position: absolute; top: 0px; left: 0px;}
55 <script src="ui/scala_utf/slides.js" type="text/javascript"></script>
60 <div id="controls"><!-- DO NOT EDIT --></div>
61 <div id="currentSlide"><!-- DO NOT EDIT --></div>
62 <div id="header"></div>
64 <h1>DBIx::Class Introduction</h1>
65 <h2>YAPC::NA 2010</h2>
69 <div class="presentation">
72 <h1>DBIX::Class (aka DBIC)</h1>
73 <h3>for (advanced) beginners</h3>
78 <h4>Originally Leo Lapworth @ LPW 2009</h4>
79 <h4>Amiri Barksdale</h4>
80 <h4>Justin D. Hunter</h4>
81 <h4>Arthur Axel "fREW" Schmidt</h4>
85 <h1>What's up guys?</h1>
86 <ul class="incremental">
87 <li>How many people have used any ORM?</li><ul class="incremental">
88 <li>In Perl?<ul class="incremental">
96 <li>(N)Hibernate?</li>
103 <p>The purpose of this talk is to show you as many features of
104 DBIx::Class in 40 minutes so that when you need to do something with
105 it later you will know what's possible</p>
106 <ul class="incremental">
107 <li>Note: links in slides are so you can find docs for what I'm talking about later</li>
112 <h1>DBIx::Class?</h1>
114 <li>ORM (object relational mapper)</li>
115 <li>SQL <-> OO (using objects instead of SQL)</li>
116 <li>There are many ORMs, DBIx::Class just happens to be the best in Perl (personal opinion)</li>
122 <p>These are reasons that are not technical or inherent to
123 the code of DBIC, but are totally awesome things about it.</p>
127 <h1>Large Community</h1>
128 <p>Currently there are 88 people listed as contributors to DBIC. That
129 ranges from documentation help, to test help, to added features,
130 to entire database support.</p>
134 <h1>Active Community</h1>
135 <p>Currently (June 9, 2010) 6 active branches (commited to in the last two weeks) in git. Last release (0.08122) had 14 new features, and 16 bug fixes. Of course that <a href="http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git;a=blob;f=Changes">ebbs and flows</a>.</p>
139 <h1>Responsive Community</h1>
140 <ul class="incremental">
141 <li>needed MSSQL order-by support, they helped me add support</li>
142 <li>generally very welcoming of people willing to help</li>
148 <p>These are things that are in most other ORMs, but are still reasons
149 to use DBIC over raw SQL.</p>
154 <p>The vast majority of code should run on all databases without needing tweaking</p>
159 <ul class="incremental">
160 <li><strong>C</strong> - Create</li>
161 <li><strong>R</strong> - Read</li>
162 <li><strong>U</strong> - Update</li>
163 <li><strong>D</strong> - Delete</li>
169 <pre>my $sth = $dbh->prepare('
176 'A book title', $author_id
181 <h1>DBIC: Create</h1>
182 <pre>my $book = $book_rs-><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#create">create</a>({
183 title => 'A book title',
184 author_id => $author_id,
186 <ul class="incremental">
187 <li>No need to pair placeholders and values</li>
188 <li>Automatically gets autoincremented id for you</li>
189 <li>Transparently uses INSERT ... RETURNING for databases that support it</li>
195 <pre>my $sth = $dbh->prepare('
197 authors.name as author_name
199 WHERE books.author = authors.id
202 while( my $book = $sth->fetchrow_hashref() ) {
206 . $book->{author_name}
213 <pre>my $book = $book_rs-><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#find">find</a>($book_id);
215 my $book = $book_rs-><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#search">search</a>({
216 title => 'A book title',
217 }, { rows => 1 })-><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#next">next</a>;
219 my @books = $book_rs->search({
220 author => $author_id,
221 })-><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#all">all</a>;
223 while( my $book = $books_rs->next ) {
227 . $book->author->name
231 <ul class="incremental">
238 <pre>my $update = $dbh->prepare('
245 'New title',<strong>$book_id</strong>
250 <h1>DBIC: Update</h1>
251 <pre>$book-><a href="http://search.cpan.org/perldoc?DBIx::Class::Row#update">update</a>({
252 title => 'New title',
254 <ul class="incremental">
255 <li>Won't update unless value changes</li>
261 <pre>my $delete = $dbh->prepare('
266 $delete->execute(<strong>$book_id</strong>);</pre>
270 <h1>DBIC: Delete</h1>
271 <pre>$book-><a href="http://search.cpan.org/perldoc?DBIx::Class::Row#delete">delete</a>;</pre>
275 <h1>OO Overidability</h1>
276 <ul class="incremental">
277 <li>Override new if you want to do validation</li>
278 <li>Override delete if you want to disable deletion</li>
279 <li>and on and on</li>
282 <p>I got yelled at about this before by people, so
283 we don't get EVERYTHING from OO, but we do get a lot
289 <h1>Convenience Methods</h1>
290 <ul class="incremental">
291 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#find_or_create">find_or_create</a></li>
292 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#update_or_create">update_or_create</a></li>
297 <h1>Non-column methods</h1>
298 <p>Need a method to get a user's gravatar URL? Add a
299 gravatar_url method to their Result class</p>
303 <h1>RELATIONSHIPS</h1>
304 <ul class="incremental">
305 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship#belongs_to">belongs_to</a></li>
306 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship#has_many">has_many</a></li>
307 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship#might_have">might_have</a></li>
308 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship#has_one">has_one</a></li>
309 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship#many_to_many">many_to_many</a> (technically not a relationship)</li>
310 <li>SET AND FORGET</li>
315 <h1>DBIx::Class Specific Features</h1>
316 <p>These things may be in other ORM's, but they are very specific, so doubtful</p>
322 <pre>my $schema = Foo::Schema->connect(
325 $schema-><a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#deploy">deploy</a>
327 <p>See also: <a href="http://search.cpan.org/perldoc?DBIx::Class::DeploymentHandler">DBIx::Class::DeploymentHandler</a></p>
331 <h1>Schema::Loader</h1>
333 <pre>package Foo::Schema;
334 use strict; use warnings;
335 use base '<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema::Loader">DBIx::Class::Schema::Loader</a>';
336 __PACKAGE__-><a href="http://search.cpan.org/perldoc?DBIx::Class::Schema::Loader::Base#CONSTRUCTOR_OPTIONS">loader_options</a>({
338 debug => $ENV{DBIC_TRACE},
344 my $schema = Foo::Schema->connect(
352 <p>Made for inserting lots of rows very quicky into database</p>
353 <pre>$schema-><a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#populate">populate</a>([ Users =>
354 [qw( username password )],
355 [qw( frew >=4char$ )],
360 <ul class="incremental">
361 <li>I use this to <a href="http://blog.afoolishmanifesto.com/archives/1255">export our whole (200M~) db to SQLite</a></li>
367 <p>Create an object and all of it's related objects all at once</p>
368 <pre>$schema->resultset('Author')-><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#create">create</a>({
369 name => 'Stephen King',
370 books => [{ title => 'The Dark Tower' }],
372 street => '123 Turtle Back Lane',
373 state => { abbreviation => 'ME' },
374 city => { name => 'Lowell' },
380 <li>books is a has_many</li>
381 <li>address is a belongs_to which in turn belongs to state and city each</li>
382 <li>for this to work right state and city must mark abbreviation and name as unique</li>
389 <p>DBIx::Class helped pioneer fast MI in Perl 5 with Class::C3, so it is made
390 to allow extensions to nearly every part of it.</p>
394 <h1>Extensible: DBIC::Helpers</h1>
395 <ul class="incremental">
396 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::IgnoreWantarray">DBIC::Helper::ResultSet::IgnoreWantarray</a></li>
397 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::Random">DBIC::Helper::ResultSet::Random</a></li>
398 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::SetOperations">DBIC::Helper::ResultSet::SetOperations</a></li>
399 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::JoinTable">DBIC::Helper::Row::JoinTable</a></li>
400 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::NumifyGet">DBIC::Helper::Row::NumifyGet</a></li>
401 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::SubClass">DBIC::Helper::Row::SubClass</a></li>
402 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::ToJSON">DBIC::Helper::Row::ToJSON</a></li>
407 <h1>Extensible: <a href="http://search.cpan.org/perldoc?DBIx::Class::TimeStamp">DBIC::TimeStamp</a></h1>
408 <ul class="incremental">
410 <li>set_on_create</li>
411 <li>set_on_update</li>
416 <h1>Extensible: Kioku</h1>
417 <ul class="incremental">
418 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Schema::KiokuDB">DBIx::Class::Schema::KiokuDB</a></li>
419 <li>Kioku is the new hotness</li>
420 <li>Mix RDBMS with Object DB</li>
421 <li>beta ( == sexy )</li>
426 <h1><a href="http://search.cpan.org/perldoc?SQL::Abstract">SQL::Abstract</a></h1>
427 <pre>my $resultset = $book_rs->search({
428 name => { -like => "%$nick%" },
430 <ul class="incremental">
431 <li>(kinda) introspectible</li>
432 <li>Prettier than SQL</li>
437 <h1>Result vs ResultSet</h1>
438 <ul class="incremental">
439 <li>Result == Row</li>
440 <li>ResultSet == Query Plan<ul class="incremental">
441 <li>Internal Join Optimizer for all DB's (!!!)</li>
443 <li>(less important but...)</li>
444 <li>ResultSource == Table</li>
445 <li>Storage == Database</li>
450 <h1><a href="http://search.cpan.org/perldoc?DBIx::Class::Manual::Cookbook#Predefined_searches">ResultSet methods</a></h1>
451 <pre>package MyApp::Schema::ResultSet::Book;
452 use base 'DBIx::Class::ResultSet';
456 $self->current_source_alias .
457 '.rating' => { '>=' => 4 },
463 $self->current_source_alias .
464 '.price' => { '<=' => 5}
473 <h1>ResultSet method notes</h1>
474 <ul class="incremental">
475 <li>All searches should be ResultSet methods</li>
476 <li>Name has obvious meaning</li>
477 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#current_source_alias">current_source_alias</a> helps things to work no matter what</li>
482 <h1>ResultSet method in Action</h1>
483 <pre>$schema->resultset('Book')->good</pre>
487 <h1>ResultSet Chaining</h1>
488 <pre>$schema->resultset('Book')
496 <h1>search_related</h1>
497 <pre>my $score = $schema->resultset('User')
498 ->search({'me.userid' => 'frew'})
499 -><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#related_resultset">related_resultset</a>('access')
500 ->related_resultset('mgmt')
501 ->related_resultset('orders')
503 -><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#search_related">search_related</a>( shops => {
504 'shops.datecompleted' => {
505 -between => ['2009-10-01','2009-10-08']
508 ->related_resultset('rpt_score')
509 -><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#get_column">get_column</a>('raw_scores')
515 <h1>bonus rel methods</h1>
516 <pre>my $book = $author-><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship::Base#create_related">create_related</a>(
517 <strong>books</strong> => {
518 title => 'Another Discworld book',
522 my $book2 = $pratchett-><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship::Base#add_to_$rel">add_to_<strong>books</strong></a>({
523 title => 'MOAR Discworld book',
525 <ul class="incremental">
526 <li>Automaticaly fills in foreign key for you</li>
531 <h1>Excellent Transaction Support</h1>
532 <pre>$schema-><a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_do">txn_do</a>(sub {
536 my $guard = $schema-><a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_scope_guard">txn_scope_guard</a>;
540 $schema-><a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_begin">txn_begin</a>; # <-- low level
542 $schema-><a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_commit">txn_commit</a>;
547 <h1>InflateColumn</h1>
548 <pre>package Foo::Schema::Result::Book;
549 use base 'DBIx::Class::Core';
550 use DateTime::Format::MySQL;
552 __PACKAGE__->load_components('<a href="http://search.cpan.org/perldoc?DBIx::Class::InflateColumn">InflateColumn</a>');
553 __PACKAGE__-><strong><a href="http://search.cpan.org/perldoc?DBIx::Class::InflateColumn#inflate_column">inflate_column</a></strong>(
554 <strong>date_published</strong> => {
556 DateTime::Format::MySQL->parse_date(
560 deflate => sub { shift->ymd },
563 # Automatic see: DBIC::InflateColumn::DateTime</pre>
567 <h1>InflateColumn: deflation</h1>
568 <pre>$book->date_published(DateTime->now);
569 $book->update;</pre>
573 <h1>InflateColumn: inflation</h1>
574 <pre>say $book->date_published->month_abbr;</pre>
576 <strong><em>Nov</em></strong>
580 <h1>FilterColumn</h1>
581 <pre>package Foo::Schema::Result::Book;
582 use base 'DBIx::Class::Core';
584 __PACKAGE__->load_components('<a href="http://search.cpan.org/perldoc?DBIx::Class::FilterColumn">FilterColumn</a>');
586 __PACKAGE__-><strong><a href="http://search.cpan.org/perldoc?DBIx::Class::FilterColumn#filter_column">filter_column</a></strong>(
587 <strong>length</strong> => {
588 to_storage => 'to_metric',
589 from_storage => 'to_imperial',
593 sub to_metric { $_[1] * .305 }
594 sub to_imperial { $_[1] * 3.28 }
598 <h1><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSetColumn">ResultSetColumn</a></h1>
599 <pre>my $rsc = $schema->resultset('Book')
600 ->get_column('price');
611 <pre>my @res = $rs->search({}, {
612 <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#select">select</a> => [
618 <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#as">as</a> => [
619 qw(price genre max_price avg_price)
621 <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#group_by">group_by</a> => [qw(price genre)],
624 say $_->price . ' ' . $_->genre;
625 say $_->get_column('max_price');
626 say $_->get_column('min_price');
631 <h1>Aggregates Notes</h1>
632 <ul class="incremental">
633 <li>Careful, get_column can basicaly mean THREE things</li>
634 <li>private for get what you should use an accessor for</li>
635 <li>public for what there is no accessor for</li>
636 <li>public for get resultset column (prev slide)</li>
642 <pre>$rs->search({}, {
643 <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#result_class">result_class</a> =>
644 '<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultClass::HashRefInflator">DBIx::Class::ResultClass::HashRefInflator</a>',
646 <ul class="incremental">
647 <li>Easy on memory</li>
649 <li>Great for quick debugging</li>
650 <li>Great for performance tuning (we went from 2m to < 3s)</li>
655 <h1><a href="http://search.cpan.org/perldoc?DBIx::Class::Manual::Cookbook#Subqueries">Subquery</a> Support</h1>
656 <pre>my $inside_query = $schema->resultset('Artist')
658 name => [ 'Billy Joel', 'Brittany Spears' ],
659 })->get_column('id')->as_query;
661 my $rs = $schema->resultset('CD')->search({
662 artist_id => { -in => $inside_query },
667 <h1><a href="http://search.cpan.org/perldoc?SQL::Abstract#Literal_SQL_with_placeholders_and_bind_values_(subqueries)">Bare SQL w/ Placeholders</a></h1>
668 <pre>$rs->update({
669 # !!! SQL INJECTION VECTOR
670 price => \"price + $inc",
674 price => \['price + ?', [inc => $inc]],