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>
88 <li>How many people have used any ORM?<ul>
98 <li>(N)Hibernate?</li>
106 <p>The purpose of this talk is to show you as many features of
107 DBIx::Class in 40 minutes so that when you need to do something with
108 it later you will know what's possible</p>
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->create({
183 title => 'A book title',
184 author_id => $author_id,
186 <ul class="incremental">
187 <li>No need to pair placeholders and values</li>
193 <pre>my $sth = $dbh->prepare('
195 authors.name as author_name
197 WHERE books.author = authors.id
203 <pre>while( my $book = $sth->fetchrow_hashref() ) {
207 . $book->{author_name}
214 <pre>my $book = $book_rs->find($book_id);
216 my $book = $book_rs->search({
217 title => 'A book title',
218 }, { rows => 1 })->single;
220 my @books = $book_rs->search({
221 author => $author_id,
223 <ul class="incremental">
230 <pre>while( my $book = $books_rs->next ) {
234 . $book->author->name
241 <pre>my $update = $dbh->prepare('
248 'New title',<strong>$book_id</strong>
253 <h1>DBIC: Update</h1>
254 <pre>$book->update({
255 title => 'New title',
261 <pre>my $delete = $dbh->prepare('
266 $delete->execute(<strong>$book_id</strong>);</pre>
270 <h1>DBIC: Delete</h1>
271 <pre>$book->delete;</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>find_or_create</li>
292 <li>create_or_update</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($dsn, $user, $pass);
325 <p>See also: <a href="http://search.cpan.org/perldoc?DBIx::Class::DeploymentHandler">DBIx::Class::DeploymentHandler</a></p>
329 <h1>Schema::Loader</h1>
331 <pre>package Foo::Schema;
332 use strict; use warnings;
333 use base 'DBIx::Class::Schema::Loader';
334 __PACKAGE__->loader_options({
336 debug => $ENV{DBIC_TRACE},
342 my $schema = Foo::Schema->connect($dsn, $user, $pass);
348 <p>Made for inserting lots of rows very quicky into database</p>
349 <pre>$schema->populate([ Users =>
350 [qw( username password )],
351 [qw( frew >=4char$ )],
356 <ul class="incremental">
357 <li>I use this to <a href="http://blog.afoolishmanifesto.com/archives/1255">export our whole (200M~) db to SQLite</a></li>
363 <p>Create an object and all of it's related objects all at once</p>
364 <pre>$schema->resultset('Author')->create({
365 name => 'Stephen King',
366 books => [{ title => 'The Dark Tower' }],
368 street => '123 Turtle Back Lane',
369 state => { abbreviation => 'ME' },
370 city => { name => 'Lowell' },
376 <li>books is a has_many</li>
377 <li>address is a belongs_to which in turn belongs to state and city each</li>
378 <li>for this to work right state and city must mark abbreviation and name as unique</li>
385 <p>DBIx::Class helped pioneer fast MI in Perl 5 with Class::C3, so it is made
386 to allow extensions to nearly every part of it.</p>
390 <h1>Extensible: DBIC::Helpers</h1>
391 <ul class="incremental">
392 <li>DBIx::Class::Helper::ResultSet::IgnoreWantarray</li>
393 <li>DBIx::Class::Helper::ResultSet::Random</li>
394 <li>DBIx::Class::Helper::ResultSet::SetOperations</li>
395 <li>DBIx::Class::Helper::Row::JoinTable</li>
396 <li>DBIx::Class::Helper::Row::NumifyGet</li>
397 <li>DBIx::Class::Helper::Row::SubClass</li>
398 <li>DBIx::Class::Helper::Row::ToJSON</li>
403 <h1>Extensible: DBIC::TimeStamp</h1>
404 <ul class="incremental">
406 <li>set_on_create</li>
407 <li>set_on_update</li>
412 <h1>Extensible: DBIx::Class::Schema::KiokuDB</h1>
413 <ul class="incremental">
414 <li>Kioku is the new hotness</li>
415 <li>Mix RDBMS with Object DB</li>
416 <li>beta ( == sexy )</li>
421 <h1>SQL::Abstract</h1>
422 <pre>my $resultset = $book_rs->search({
423 name => { -like => "%$nick%" },
425 <ul class="incremental">
426 <li>(kinda) introspectible</li>
427 <li>Prettier than SQL</li>
432 <h1>Result vs ResultSet</h1>
433 <ul class="incremental">
434 <li>Result == Row</li>
435 <li>ResultSet == Query</li>
436 <li>(less important but...)</li>
437 <li>ResultSource == Table</li>
438 <li>Storage == Database</li>
443 <h1>ResultSet methods</h1>
444 <pre>package MyApp::Schema::ResultSet::Book;
445 use base 'DBIx::Class::ResultSet';
448 rating => { '>=' => 4 },
453 price => { '<=' => 5}
459 <ul class="incremental">
460 <li>All searches should be ResultSet methods</li>
461 <li>Name has obvious meaning</li>
466 <h1>ResultSet method in Action</h1>
467 <pre>$schema->resultset('Book')->good</pre>
471 <h1>ResultSet Chaining</h1>
472 <pre>$schema->resultset('Book')
480 <h1>search_related</h1>
481 <pre>*get example from work*</pre>
485 <h1>bonus rel methods</h1>
486 <pre>my $book = $author->create_related(
487 <strong>books</strong> => {
488 title => 'Another Discworld book',
491 my $book2 = $pratchett->add_to_<strong>books</strong>({
492 title => 'MOAR Discworld book',
494 <ul class="incremental">
495 <li>Automaticaly fills in foreign key for you</li>
500 <h1>Excellent Transaction Support</h1>
501 <pre>$schema->txn_do(sub {
505 my $guard = $schema->txn_scope_guard;
509 $schema->txn_begin; # <-- low level
511 $schema->txn_commit;
516 <h1>InflateColumn</h1>
517 <pre>package Foo::Schema::Result::Book;
518 use base 'DBIx::Class::Core';
520 __PACKAGE__->load_components('InflateColumn');
521 use DateTime::Format::MySQL;
522 __PACKAGE__-><strong>inflate_column</strong>(
523 <strong>date_published</strong> => {
524 inflate => sub { DateTime::Format::MySQL->parse_date(shift) },
525 deflate => sub { shift->ymd},
528 # Automatic see: DBIx::Class::InflateColumn::DateTime</pre>
532 <h1>InflateColumn: deflation</h1>
533 <pre>$book->date_published(DateTime->now);
534 $book->update;</pre>
538 <h1>InflateColumn: inflation</h1>
539 <pre>my $date_published = $book->date_published;
540 print $date_published->month_abbr;</pre>
542 <strong><em>Nov</em></strong>
546 <h1>FilterColumn</h1>
547 <pre>package Foo::Schema::Result::Book;
548 use base 'DBIx::Class::Core';
550 __PACKAGE__->load_components('FilterColumn');
552 __PACKAGE__-><strong>filter_column</strong>(
553 <strong>length</strong> => {
554 to_storage => 'to_metric',
555 from_storage => 'to_imperial',
559 sub to_metric { $_[1] * .305 }
560 sub to_imperial { $_[1] * 3.28 }
561 # Automatic see: DBIx::Class::InflateColumn::DateTime</pre>
565 <h1>ResultSetColumn</h1>
566 <pre>my $rsc = $schema->resultset('Book')->get_column('price');
575 <pre>my @res = $rs->search({}, {
576 select => [qw(price genre), { max => price }, { avg => price }],
577 as => [qw(price genre max_price avg_price)],
578 group_by => [qw(price genre)],
581 say $_->price . ' ' . $_->genre;
582 say $_->get_column('max_price');
583 say $_->get_column('min_price');
585 <ul class="incremental">
586 <li>Careful, get_column can basicaly mean THREE things</li>
587 <li>private for get what you should use an accessor for</li>
588 <li>public for what there is no accessor for</li>
589 <li>public for get resultset column (prev slide)</li>
595 <pre>$rs->search({}, {
596 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
598 <ul class="incremental">
599 <li>Easy on memory</li>
601 <li>Great for quick debugging</li>
602 <li>Great for performance tuning (we went from 2m to < 3s)</li>
607 <h1>Subquery Support</h1>
608 <pre> my $inside_rs = $schema->resultset('Artist')->search({
609 name => [ 'Billy Joel', 'Brittany Spears' ],
612 my $rs = $schema->resultset('CD')->search({
613 artist_id => { -in => $inside_rs->get_column('id')->as_query },
618 <h1>Bare SQL w/ Placeholders</h1>
619 <pre>$rs->update({
620 price => \"price + $inc", # !!! SQL INJECTION VECTOR
624 price => \['price + ?', [inc => $inc]],