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