11f0704e20407214dbc24796d4c914aefaa9ba39
[dbsrgits/dbix-class-introduction-presentation.git] / slideshow.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
4 <html xmlns="http://www.w3.org/1999/xhtml">
5
6 <head>
7 <title>DBIx::Class (aka DBIC)</title>
8 <!-- metadata -->
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" />
13
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;}
51 .red {color: red;}
52 .grey {color: gray;}
53 </style>
54 <!-- S5 JS -->
55 <script src="ui/scala_utf/slides.js" type="text/javascript"></script>
56 </head>
57 <body>
58
59 <div class="layout">
60    <div id="controls"><!-- DO NOT EDIT --></div>
61    <div id="currentSlide"><!-- DO NOT EDIT --></div>
62    <div id="header"></div>
63    <div id="footer">
64       <h1>DBIx::Class Introduction</h1>
65       <h2>YAPC::NA 2010</h2>
66    </div>
67 </div>
68
69 <div class="presentation">
70
71    <div class="slide">
72       <h1>DBIX::Class (aka DBIC)</h1>
73       <h3>for (advanced) beginners</h3>
74    </div>
75
76    <div class="slide">
77       <h1>Contact Info</h1>
78       <ul>
79          <li>IRC: irc.perl.org #dbix-class</li>
80          <li>ML: <a href="http://lists.scsys.co.uk/mailman/listinfo/dbix-class">http://lists.scsys.co.uk/mailman/listinfo/dbix-class</a></li>
81          <li><a href="http://search.cpan.org/perldoc?DBIx::Class">DBIx::Class</a> yo</li>
82       </ul>
83    </div>
84
85    <div class="slide">
86       <h1>Authors</h1>
87       <h4>Originally Leo Lapworth @ LPW 2009</h4>
88       <h4>Amiri Barksdale</h4>
89       <h4>Justin D. Hunter</h4>
90       <h4>Arthur Axel "fREW" Schmidt</h4>
91    </div>
92
93    <div class="slide">
94       <h1>What's up guys?</h1>
95       <ul class="incremental">
96          <li>How many people have used any ORM?</li><ul class="incremental">
97             <li>In Perl?<ul class="incremental">
98                <li>DBIC?</li>
99                <li>Class::DBI?</li>
100                <li>Rose::DB?</li>
101                <li>Fey::ORM?</li>
102                <li>Others?</li>
103             </ul></li>
104             <li>AR?</li>
105             <li>(N)Hibernate?</li>
106          </ul></li>
107       </ul>
108    </div>
109
110    <div class="slide">
111       <h1>Purpose</h1>
112       <p>The purpose of this talk is to show you as many features of
113       DBIx::Class in 40 minutes so that when you need to do something with
114       it later you will know what's possible</p>
115       <ul class="incremental">
116          <li>Note: links in slides are so you can find docs for what I'm talking about later</li>
117       </ul>
118    </div>
119
120    <div class="slide">
121       <h1>DBIx::Class?</h1>
122       <ul>
123          <li>ORM (object relational mapper)</li>
124          <li>SQL &lt;-&gt; OO (using objects instead of SQL)</li>
125          <li>There are many ORMs, DBIx::Class just happens to be the best in Perl (personal opinion)</li>
126       </ul>
127    </div>
128
129    <div class="slide">
130       <h1>Meta</h1>
131       <p>These are reasons that are not technical or inherent to
132       the code of DBIC, but are totally awesome things about it.</p>
133    </div>
134
135    <div class="slide">
136       <h1>Large Community</h1>
137       <p>Currently there are 88 people listed as contributors to DBIC.  That
138       ranges from documentation help, to test help, to added features,
139       to entire database support.</p>
140    </div>
141
142    <div class="slide">
143       <h1>Active Community</h1>
144       <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>
145    </div>
146
147    <div class="slide">
148       <h1>Responsive Community</h1>
149       <ul class="incremental">
150          <li>needed MSSQL order-by support, they helped me add support</li>
151          <li>generally very welcoming of people willing to help</li>
152       </ul>
153    </div>
154
155    <div class="slide">
156       <h1>General ORM</h1>
157       <p>These are things that are in most other ORMs, but are still reasons
158       to use DBIC over raw SQL.</p>
159    </div>
160
161    <div class="slide">
162       <h1>Cross DB</h1>
163       <p>The vast majority of code should run on all databases without needing tweaking</p>
164    </div>
165
166    <div class="slide">
167       <h1>Basic CRUD</h1>
168       <ul class="incremental">
169          <li><strong>C</strong> - Create</li>
170          <li><strong>R</strong> - Read</li>
171          <li><strong>U</strong> - Update</li>
172          <li><strong>D</strong> - Delete</li>
173       </ul>
174    </div>
175
176    <div class="slide">
177       <h1>SQL: Create</h1>
178 <pre>my $sth = $dbh-&gt;prepare('
179    INSERT INTO books
180    (title, author_id)
181    values (?,?)
182 ');
183
184 $sth-&gt;execute(
185   'A book title', $author_id
186 );</pre>
187    </div>
188
189    <div class="slide">
190       <h1>DBIC: Create</h1>
191 <pre>my $book = $book_rs-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#create">create</a>({
192    title     =&gt; 'A book title',
193    author_id =&gt; $author_id,
194 });</pre>
195       <ul class="incremental">
196          <li>No need to pair placeholders and values</li>
197          <li>Automatically gets autoincremented id for you</li>
198          <li>Transparently uses INSERT ... RETURNING for databases that support it</li>
199       </ul>
200    </div>
201
202    <div class="slide">
203       <h1>SQL: Read</h1>
204 <pre>my $sth = $dbh-&gt;prepare('
205    SELECT title,
206    authors.name as author_name
207    FROM books, authors
208    WHERE books.author = authors.id
209 ');
210
211 while( my $book = $sth-&gt;fetchrow_hashref() ) {
212   print 'Author of '
213      . $book-&gt;{title}
214      . ' is '
215      . $book-&gt;{author_name}
216      . "\n";
217 }</pre>
218    </div>
219
220    <div class="slide">
221       <h1>DBIC: Read</h1>
222 <pre>my $book = $book_rs-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#find">find</a>($book_id);
223
224 my $book = $book_rs-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#search">search</a>({
225    title =&gt; 'A book title',
226 }, { rows =&gt; 1 })-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#next">next</a>;
227
228 my @books = $book_rs-&gt;search({
229    author =&gt; $author_id,
230 })-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#all">all</a>;
231
232 while( my $book = $books_rs-&gt;next ) {
233  print 'Author of '
234     . $book-&gt;title
235     . ' is '
236     . $book-&gt;author-&gt;name
237     . "\n";
238 }
239 </pre>
240       <ul class="incremental">
241          <li>TMTOWTDI</li>
242       </ul>
243    </div>
244
245    <div class="slide">
246       <h1>SQL: Update</h1>
247 <pre>my $update = $dbh-&gt;prepare('
248    UPDATE books
249    SET title = ?
250    WHERE id = ?
251 ');
252
253 $update-&gt;execute(
254   'New title',<strong>$book_id</strong>
255 );</pre>
256    </div>
257
258    <div class="slide">
259       <h1>DBIC: Update</h1>
260 <pre>$book-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Row#update">update</a>({
261   title =&gt; 'New title',
262 });</pre>
263       <ul class="incremental">
264          <li>Won't update unless value changes</li>
265       </ul>
266    </div>
267
268    <div class="slide">
269       <h1>SQL: Delete</h1>
270 <pre>my $delete = $dbh-&gt;prepare('
271    DELETE FROM books
272    WHERE id = ?
273 ');
274
275 $delete-&gt;execute(<strong>$book_id</strong>);</pre>
276    </div>
277
278    <div class="slide">
279       <h1>DBIC: Delete</h1>
280 <pre>$book-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Row#delete">delete</a>;</pre>
281    </div>
282
283    <div class="slide">
284       <h1>OO Overidability</h1>
285       <ul class="incremental">
286          <li>Override new if you want to do validation</li>
287          <li>Override delete if you want to disable deletion</li>
288          <li>and on and on</li>
289       </ul>
290       <div class="notes">
291          <p>I got yelled at about this before by people, so
292          we don't get EVERYTHING from OO, but we do get a lot
293          so :-P</p>
294       </div>
295    </div>
296
297    <div class="slide">
298       <h1>Convenience Methods</h1>
299       <ul class="incremental">
300          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#find_or_create">find_or_create</a></li>
301          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#update_or_create">update_or_create</a></li>
302       </ul>
303    </div>
304
305    <div class="slide">
306       <h1>Non-column methods</h1>
307       <p>Need a method to get a user's gravatar URL?  Add a
308       gravatar_url method to their Result class</p>
309    </div>
310
311    <div class="slide">
312       <h1>RELATIONSHIPS</h1>
313       <ul class="incremental">
314          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship#belongs_to">belongs_to</a></li>
315          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship#has_many">has_many</a></li>
316          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship#might_have">might_have</a></li>
317          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship#has_one">has_one</a></li>
318          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship#many_to_many">many_to_many</a> (technically not a relationship)</li>
319          <li>SET AND FORGET</li>
320       </ul>
321    </div>
322
323    <div class="slide">
324       <h1>DBIx::Class Specific Features</h1>
325       <p>These things may be in other ORM's, but they are very specific, so doubtful</p>
326    </div>
327
328    <div class="slide">
329       <h1>-&gt;deploy</h1>
330       <p>Perl -&gt; DB</p>
331 <pre>my $schema = Foo::Schema-&gt;connect(
332    $dsn, $user, $pass
333 );
334 $schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#deploy">deploy</a>
335 </pre>
336 <p>See also: <a href="http://search.cpan.org/perldoc?DBIx::Class::DeploymentHandler">DBIx::Class::DeploymentHandler</a></p>
337    </div>
338
339    <div class="slide">
340       <h1>Schema::Loader</h1>
341       <p>DB -&gt; Perl</p>
342 <pre>package Foo::Schema;
343 use strict; use warnings;
344 use base '<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema::Loader">DBIx::Class::Schema::Loader</a>';
345 __PACKAGE__-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema::Loader::Base#CONSTRUCTOR_OPTIONS">loader_options</a>({
346    naming =&gt; 'v7',
347    debug  =&gt; $ENV{DBIC_TRACE},
348 });
349 1;
350
351 # elsewhere...
352
353 my $schema = Foo::Schema-&gt;connect(
354    $dsn, $user, $pass
355 );
356 </pre>
357    </div>
358
359    <div class="slide">
360       <h1>Populate</h1>
361       <p>Made for inserting lots of rows very quicky into database</p>
362 <pre>$schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#populate">populate</a>([ Users =&gt;
363    [qw( username password )],
364    [qw( frew     &gt;=4char$ )],
365    [qw(      ...          )],
366    [qw(      ...          )],
367 );
368 </pre>
369       <ul class="incremental">
370          <li>I use this to <a href="http://blog.afoolishmanifesto.com/archives/1255">export our whole (200M~) db to SQLite</a></li>
371       </ul>
372    </div>
373
374    <div class="slide">
375       <h1>Multicreate</h1>
376       <p>Create an object and all of it's related objects all at once</p>
377 <pre>$schema-&gt;resultset('Author')-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#create">create</a>({
378    name =&gt; 'Stephen King',
379    books =&gt; [{ title =&gt; 'The Dark Tower' }],
380    address =&gt; {
381       street =&gt; '123 Turtle Back Lane',
382       state  =&gt; { abbreviation =&gt; 'ME' },
383       city   =&gt; { name =&gt; 'Lowell'     },
384    },
385 });
386 </pre>
387       <div class="notes">
388          <ul>
389             <li>books is a has_many</li>
390             <li>address is a belongs_to which in turn belongs to state and city each</li>
391             <li>for this to work right state and city must mark abbreviation and name as unique</li>
392          </ul>
393       </div>
394    </div>
395
396    <div class="slide">
397       <h1>Extensible</h1>
398       <p>DBIx::Class helped pioneer fast MI in Perl 5 with Class::C3, so it is made
399       to allow extensions to nearly every part of it.</p>
400    </div>
401
402    <div class="slide">
403       <h1>Extensible: DBIC::Helpers</h1>
404       <ul class="incremental">
405          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::IgnoreWantarray">DBIC::Helper::ResultSet::IgnoreWantarray</a></li>
406          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::Random">DBIC::Helper::ResultSet::Random</a></li>
407          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::SetOperations">DBIC::Helper::ResultSet::SetOperations</a></li>
408          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::JoinTable">DBIC::Helper::Row::JoinTable</a></li>
409          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::NumifyGet">DBIC::Helper::Row::NumifyGet</a></li>
410          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::SubClass">DBIC::Helper::Row::SubClass</a></li>
411          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Helper::ResultSet::ToJSON">DBIC::Helper::Row::ToJSON</a></li>
412       </ul>
413    </div>
414
415    <div class="slide">
416       <h1>Extensible: <a href="http://search.cpan.org/perldoc?DBIx::Class::TimeStamp">DBIC::TimeStamp</a></h1>
417       <ul class="incremental">
418          <li>Cross DB</li>
419          <li>set_on_create</li>
420          <li>set_on_update</li>
421       </ul>
422    </div>
423
424    <div class="slide">
425       <h1>Extensible: Kioku</h1>
426       <ul class="incremental">
427          <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Schema::KiokuDB">DBIx::Class::Schema::KiokuDB</a></li>
428          <li>Kioku is the new hotness</li>
429          <li>Mix RDBMS with Object DB</li>
430          <li>beta ( == sexy )</li>
431       </ul>
432    </div>
433
434    <div class="slide">
435       <h1><a href="http://search.cpan.org/perldoc?SQL::Abstract">SQL::Abstract</a></h1>
436 <pre>my $resultset = $book_rs-&gt;search({
437    name =&gt; { -like =&gt; "%$nick%" },
438 });</pre>
439       <ul class="incremental">
440          <li>(kinda) introspectible</li>
441          <li>Prettier than SQL</li>
442       </ul>
443    </div>
444
445    <div class="slide">
446       <h1>Result vs ResultSet</h1>
447          <ul class="incremental">
448             <li>Result == Row</li>
449             <li>ResultSet == Query Plan<ul class="incremental">
450                <li>Internal Join Optimizer for all DB's (!!!)</li>
451             </ul></li>
452             <li>(less important but...)</li>
453             <li>ResultSource == Table</li>
454             <li>Storage == Database</li>
455          </ul>
456    </div>
457
458    <div class="slide">
459       <h1><a href="http://search.cpan.org/perldoc?DBIx::Class::Manual::Cookbook#Predefined_searches">ResultSet methods</a></h1>
460 <pre>package MyApp::Schema::ResultSet::Book;
461 use base 'DBIx::Class::ResultSet';
462 sub good {
463    my $self = shift;
464    $self-&gt;search({
465       $self-&gt;current_source_alias .
466          '.rating' =&gt; { '&gt;=' =&gt; 4 },
467    })
468 };
469 sub cheap {
470    my $self = shift;
471    $self-&gt;search({
472       $self-&gt;current_source_alias .
473          '.price' =&gt; { '&lt;=' =&gt; 5}
474    })
475 };
476 # ...
477 1;
478       </pre>
479    </div>
480
481    <div class="slide">
482       <h1>ResultSet method notes</h1>
483       <ul class="incremental">
484          <li>All searches should be ResultSet methods</li>
485          <li>Name has obvious meaning</li>
486          <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>
487       </ul>
488    </div>
489
490    <div class="slide">
491       <h1>ResultSet method in Action</h1>
492       <pre>$schema-&gt;resultset('Book')-&gt;good</pre>
493    </div>
494
495    <div class="slide">
496       <h1>ResultSet Chaining</h1>
497 <pre>$schema-&gt;resultset('Book')
498    -&gt;good
499    -&gt;cheap
500    -&gt;recent
501 </pre>
502    </div>
503
504    <div class="slide">
505       <h1>search_related</h1>
506 <pre>my $score = $schema-&gt;resultset('User')
507    -&gt;search({'me.userid' =&gt; 'frew'})
508    -&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#related_resultset">related_resultset</a>('access')
509    -&gt;related_resultset('mgmt')
510    -&gt;related_resultset('orders')
511    -&gt;telephone
512    -&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#search_related">search_related</a>( shops =&gt; {
513       'shops.datecompleted' =&gt; {
514          -between =&gt; ['2009-10-01','2009-10-08']
515       }
516    })-&gt;completed
517    -&gt;related_resultset('rpt_score')
518    -&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#get_column">get_column</a>('raw_scores')
519    -&gt;first;
520 </pre>
521    </div>
522
523    <div class="slide">
524       <h1>bonus rel methods</h1>
525 <pre>my $book = $author-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship::Base#create_related">create_related</a>(
526    <strong>books</strong> =&gt; {
527       title =&gt; 'Another Discworld book',
528    }
529 );
530
531 my $book2 = $pratchett-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship::Base#add_to_$rel">add_to_<strong>books</strong></a>({
532    title =&gt; 'MOAR Discworld book',
533 });</pre>
534       <ul class="incremental">
535          <li>Automaticaly fills in foreign key for you</li>
536       </ul>
537    </div>
538
539    <div class="slide">
540       <h1>Excellent Transaction Support</h1>
541 <pre>$schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_do">txn_do</a>(sub {
542    ...
543 });
544
545 my $guard = $schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_scope_guard">txn_scope_guard</a>;
546 # ...
547 $guard-&gt;commit;
548
549 $schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_begin">txn_begin</a>; # &lt;-- low level
550 # ...
551 $schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_commit">txn_commit</a>;
552 </pre>
553    </div>
554
555    <div class="slide">
556       <h1>InflateColumn</h1>
557 <pre>package Foo::Schema::Result::Book;
558 use base 'DBIx::Class::Core';
559 use DateTime::Format::MySQL;
560 # Result code here
561 __PACKAGE__-&gt;load_components('<a href="http://search.cpan.org/perldoc?DBIx::Class::InflateColumn">InflateColumn</a>');
562 __PACKAGE__-&gt;<strong><a href="http://search.cpan.org/perldoc?DBIx::Class::InflateColumn#inflate_column">inflate_column</a></strong>(
563    <strong>date_published</strong> =&gt; {
564       inflate =&gt; sub {
565          DateTime::Format::MySQL-&gt;parse_date(
566             shift
567          )
568       },
569       deflate =&gt; sub { shift-&gt;ymd },
570    },
571 );
572 # Automatic see: DBIC::InflateColumn::DateTime</pre>
573    </div>
574
575    <div class="slide">
576       <h1>InflateColumn: deflation</h1>
577 <pre>$book-&gt;date_published(DateTime-&gt;now);
578 $book-&gt;update;</pre>
579    </div>
580
581    <div class="slide">
582       <h1>InflateColumn: inflation</h1>
583 <pre>say $book-&gt;date_published-&gt;month_abbr;</pre>
584
585 <strong><em>Nov</em></strong>
586    </div>
587
588    <div class="slide">
589       <h1>FilterColumn</h1>
590 <pre>package Foo::Schema::Result::Book;
591 use base 'DBIx::Class::Core';
592 # Result code here
593 __PACKAGE__-&gt;load_components('<a href="http://search.cpan.org/perldoc?DBIx::Class::FilterColumn">FilterColumn</a>');
594
595 __PACKAGE__-&gt;<strong><a href="http://search.cpan.org/perldoc?DBIx::Class::FilterColumn#filter_column">filter_column</a></strong>(
596    <strong>length</strong> =&gt; {
597       to_storage   =&gt; 'to_metric',
598       from_storage =&gt; 'to_imperial',
599    },
600 );
601
602 sub to_metric   { $_[1] * .305 }
603 sub to_imperial { $_[1] * 3.28 }
604    </div>
605
606    <div class="slide">
607       <h1><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSetColumn">ResultSetColumn</a></h1>
608 <pre>my $rsc = $schema-&gt;resultset('Book')
609    -&gt;get_column('price');
610 $rsc-&gt;first;
611 $rsc-&gt;all;
612 $rsc-&gt;min;
613 $rsc-&gt;max;
614 $rsc-&gt;sum;
615 </pre>
616    </div>
617
618    <div class="slide">
619       <h1>Aggregates</h1>
620 <pre>my @res = $rs-&gt;search({}, {
621    <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#select">select</a>   =&gt; [
622       'price',
623       'genre',
624       { max =&gt; price },
625       { avg =&gt; price },
626    ],
627    <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#as">as</a>       =&gt; [
628       qw(price genre max_price avg_price)
629    ],
630    <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#group_by">group_by</a> =&gt; [qw(price genre)],
631 });
632 for (@res) {
633    say $_-&gt;price . ' ' . $_-&gt;genre;
634    say $_-&gt;get_column('max_price');
635    say $_-&gt;get_column('min_price');
636 }</pre>
637    </div>
638
639    <div class="slide">
640       <h1>Aggregates Notes</h1>
641       <ul class="incremental">
642          <li>Careful, get_column can basicaly mean THREE things</li>
643          <li>private for get what you should use an accessor for</li>
644          <li>public for what there is no accessor for</li>
645          <li>public for get resultset column (prev slide)</li>
646       </ul>
647    </div>
648
649    <div class="slide">
650       <h1>HRI</h1>
651 <pre>$rs-&gt;search({}, {
652   <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#result_class">result_class</a> =&gt;
653     '<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultClass::HashRefInflator">DBIx::Class::ResultClass::HashRefInflator</a>',
654 });</pre>
655       <ul class="incremental">
656          <li>Easy on memory</li>
657          <li>Mega fast</li>
658          <li>Great for quick debugging</li>
659          <li>Great for performance tuning (we went from 2m to &lt; 3s)</li>
660       </ul>
661    </div>
662
663    <div class="slide">
664       <h1><a href="http://search.cpan.org/perldoc?DBIx::Class::Manual::Cookbook#Subqueries">Subquery</a> Support</h1>
665 <pre>my $inside_query = $schema-&gt;resultset('Artist')
666    -&gt;search({
667     name =&gt; [ 'Billy Joel', 'Brittany Spears' ],
668 })-&gt;get_column('id')-&gt;as_query;
669
670 my $rs = $schema-&gt;resultset('CD')-&gt;search({
671     artist_id =&gt; { -in =&gt; $inside_query },
672 });</pre>
673    </div>
674
675    <div class="slide">
676       <h1><a href="http://search.cpan.org/perldoc?SQL::Abstract#Literal_SQL_with_placeholders_and_bind_values_(subqueries)">Bare SQL w/ Placeholders</a></h1>
677 <pre>$rs-&gt;update({
678    # !!! SQL INJECTION VECTOR
679    price =&gt; \"price + $inc",
680 });
681
682 $rs-&gt;update({
683    price =&gt; \['price + ?', [inc =&gt; $inc]],
684 });
685 </pre>
686    </div>
687
688    <div class="slide">
689       <h1>Questions?</h1>
690    </div>
691
692    <div class="slide">
693       <h1>END</h1>
694    </div>
695 </div>
696 </body>
697 </html>