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