clarification + contact info
[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">
a7b4c670 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">
0cffcad7 86 <h1>Authors</h1>
87 <h4>Originally Leo Lapworth @ LPW 2009</h4>
da0b46fd 88 <h4>Amiri Barksdale</h4>
0cffcad7 89 <h4>Justin D. Hunter</h4>
5cac74c7 90 <h4>Arthur Axel "fREW" Schmidt</h4>
52d980cb 91 </div>
92
93 <div class="slide">
7365a110 94 <h1>What's up guys?</h1>
63d0b680 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>
a7b4c670 101 <li>Fey::ORM?</li>
63d0b680 102 <li>Others?</li>
0cffcad7 103 </ul></li>
63d0b680 104 <li>AR?</li>
105 <li>(N)Hibernate?</li>
106 </ul></li>
107 </ul>
52d980cb 108 </div>
109
110 <div class="slide">
da0b46fd 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>
78c1762e 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>
da0b46fd 118 </div>
119
120 <div class="slide">
52d980cb 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>
52d980cb 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">
da0b46fd 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">
78c1762e 150 <li>needed MSSQL order-by support, they helped me add support</li>
da0b46fd 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>
52d980cb 164 </div>
165
166 <div class="slide">
7365a110 167 <h1>Basic CRUD</h1>
da0b46fd 168 <ul class="incremental">
52d980cb 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">
5cac74c7 177 <h1>SQL: Create</h1>
52d980cb 178<pre>my $sth = $dbh-&gt;prepare('
179 INSERT INTO books
01b7b88c 180 (title, author_id)
52d980cb 181 values (?,?)
182');
183
184$sth-&gt;execute(
185 'A book title', $author_id
186);</pre>
187 </div>
188
189 <div class="slide">
5cac74c7 190 <h1>DBIC: Create</h1>
78c1762e 191<pre>my $book = $book_rs-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#create">create</a>({
01b7b88c 192 title =&gt; 'A book title',
193 author_id =&gt; $author_id,
52d980cb 194});</pre>
da0b46fd 195 <ul class="incremental">
196 <li>No need to pair placeholders and values</li>
ec535d03 197 <li>Automatically gets autoincremented id for you</li>
a33b5750 198 <li>Transparently uses INSERT ... RETURNING for databases that support it</li>
da0b46fd 199 </ul>
52d980cb 200 </div>
201
202 <div class="slide">
7365a110 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
63d0b680 209');
7365a110 210
63d0b680 211while( my $book = $sth-&gt;fetchrow_hashref() ) {
7365a110 212 print 'Author of '
213 . $book-&gt;{title}
214 . ' is '
215 . $book-&gt;{author_name}
216 . "\n";
217}</pre>
52d980cb 218 </div>
219
220 <div class="slide">
5cac74c7 221 <h1>DBIC: Read</h1>
78c1762e 222<pre>my $book = $book_rs-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#find">find</a>($book_id);
52d980cb 223
78c1762e 224my $book = $book_rs-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#search">search</a>({
52d980cb 225 title =&gt; 'A book title',
78c1762e 226}, { rows =&gt; 1 })-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#next">next</a>;
52d980cb 227
01b7b88c 228my @books = $book_rs-&gt;search({
52d980cb 229 author =&gt; $author_id,
63d0b680 230})-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#all">all</a>;
52d980cb 231
63d0b680 232while( my $book = $books_rs-&gt;next ) {
52d980cb 233 print 'Author of '
234 . $book-&gt;title
235 . ' is '
236 . $book-&gt;author-&gt;name
237 . "\n";
63d0b680 238}
239</pre>
240 <ul class="incremental">
241 <li>TMTOWTDI</li>
242 </ul>
52d980cb 243 </div>
244
245 <div class="slide">
7365a110 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">
5cac74c7 259 <h1>DBIC: Update</h1>
78c1762e 260<pre>$book-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Row#update">update</a>({
52d980cb 261 title =&gt; 'New title',
262});</pre>
513517e4 263 <ul class="incremental">
a33b5750 264 <li>Won't update unless value changes</li>
513517e4 265 </ul>
52d980cb 266 </div>
267
268 <div class="slide">
7365a110 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">
5cac74c7 279 <h1>DBIC: Delete</h1>
78c1762e 280<pre>$book-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Row#delete">delete</a>;</pre>
52d980cb 281 </div>
282
283 <div class="slide">
da0b46fd 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>
52d980cb 295 </div>
296
297 <div class="slide">
da0b46fd 298 <h1>Convenience Methods</h1>
299 <ul class="incremental">
78c1762e 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>
da0b46fd 302 </ul>
492be2ae 303 </div>
304
305 <div class="slide">
da0b46fd 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>
52d980cb 326 </div>
327
328 <div class="slide">
9158dee5 329 <h1>-&gt;deploy</h1>
330 <p>Perl -&gt; DB</p>
d1ed303d 331<pre>my $schema = Foo::Schema-&gt;connect(
332 $dsn, $user, $pass
333);
78c1762e 334$schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#deploy">deploy</a>
9158dee5 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">
52d980cb 340 <h1>Schema::Loader</h1>
9158dee5 341 <p>DB -&gt; Perl</p>
451b57c2 342<pre>package Foo::Schema;
c7321880 343use strict; use warnings;
78c1762e 344use 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>({
c7321880 346 naming =&gt; 'v7',
347 debug =&gt; $ENV{DBIC_TRACE},
348});
3491;
350
351# elsewhere...
352
d1ed303d 353my $schema = Foo::Schema-&gt;connect(
354 $dsn, $user, $pass
355);
c7321880 356</pre>
52d980cb 357 </div>
358
359 <div class="slide">
da0b46fd 360 <h1>Populate</h1>
361 <p>Made for inserting lots of rows very quicky into database</p>
78c1762e 362<pre>$schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#populate">populate</a>([ Users =&gt;
da0b46fd 363 [qw( username password )],
d1ed303d 364 [qw( frew &gt;=4char$ )],
da0b46fd 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>
52d980cb 372 </div>
373
374 <div class="slide">
da0b46fd 375 <h1>Multicreate</h1>
376 <p>Create an object and all of it's related objects all at once</p>
78c1762e 377<pre>$schema-&gt;resultset('Author')-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#create">create</a>({
da0b46fd 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' },
d1ed303d 383 city =&gt; { name =&gt; 'Lowell' },
da0b46fd 384 },
7e77ca11 385});
da0b46fd 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">
78c1762e 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>
da0b46fd 412 </ul>
413 </div>
414
415 <div class="slide">
78c1762e 416 <h1>Extensible: <a href="http://search.cpan.org/perldoc?DBIx::Class::TimeStamp">DBIC::TimeStamp</a></h1>
da0b46fd 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">
d1ed303d 425 <h1>Extensible: Kioku</h1>
da0b46fd 426 <ul class="incremental">
78c1762e 427 <li><a href="http://search.cpan.org/perldoc?DBIx::Class::Schema::KiokuDB">DBIx::Class::Schema::KiokuDB</a></li>
da0b46fd 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">
78c1762e 435 <h1><a href="http://search.cpan.org/perldoc?SQL::Abstract">SQL::Abstract</a></h1>
7e77ca11 436<pre>my $resultset = $book_rs-&gt;search({
437 name =&gt; { -like =&gt; "%$nick%" },
438});</pre>
da0b46fd 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>
513517e4 449 <li>ResultSet == Query Plan<ul class="incremental">
450 <li>Internal Join Optimizer for all DB's (!!!)</li>
451 </ul></li>
da0b46fd 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">
78c1762e 459 <h1><a href="http://search.cpan.org/perldoc?DBIx::Class::Manual::Cookbook#Predefined_searches">ResultSet methods</a></h1>
da0b46fd 460<pre>package MyApp::Schema::ResultSet::Book;
461use base 'DBIx::Class::ResultSet';
462sub good {
d1ed303d 463 my $self = shift;
464 $self-&gt;search({
465 $self-&gt;current_source_alias .
466 '.rating' =&gt; { '&gt;=' =&gt; 4 },
da0b46fd 467 })
468};
469sub cheap {
d1ed303d 470 my $self = shift;
471 $self-&gt;search({
472 $self-&gt;current_source_alias .
473 '.price' =&gt; { '&lt;=' =&gt; 5}
da0b46fd 474 })
475};
476# ...
4771;
478 </pre>
d1ed303d 479 </div>
480
481 <div class="slide">
482 <h1>ResultSet method notes</h1>
da0b46fd 483 <ul class="incremental">
484 <li>All searches should be ResultSet methods</li>
485 <li>Name has obvious meaning</li>
78c1762e 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>
da0b46fd 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
6e5edefe 501</pre>
52d980cb 502 </div>
503
504 <div class="slide">
7e77ca11 505 <h1>search_related</h1>
d1ed303d 506<pre>my $score = $schema-&gt;resultset('User')
507 -&gt;search({'me.userid' =&gt; 'frew'})
78c1762e 508 -&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#related_resultset">related_resultset</a>('access')
3ab0ef72 509 -&gt;related_resultset('mgmt')
d1ed303d 510 -&gt;related_resultset('orders')
511 -&gt;telephone
78c1762e 512 -&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#search_related">search_related</a>( shops =&gt; {
d1ed303d 513 'shops.datecompleted' =&gt; {
514 -between =&gt; ['2009-10-01','2009-10-08']
515 }
516 })-&gt;completed
517 -&gt;related_resultset('rpt_score')
78c1762e 518 -&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#get_column">get_column</a>('raw_scores')
d1ed303d 519 -&gt;first;
3ab0ef72 520</pre>
521 </div>
522
523 <div class="slide">
7e77ca11 524 <h1>bonus rel methods</h1>
78c1762e 525<pre>my $book = $author-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship::Base#create_related">create_related</a>(
d1ed303d 526 <strong>books</strong> =&gt; {
527 title =&gt; 'Another Discworld book',
528 }
529);
52d980cb 530
78c1762e 531my $book2 = $pratchett-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Relationship::Base#add_to_$rel">add_to_<strong>books</strong></a>({
7e77ca11 532 title =&gt; 'MOAR Discworld book',
533});</pre>
534 <ul class="incremental">
535 <li>Automaticaly fills in foreign key for you</li>
536 </ul>
52d980cb 537 </div>
538
539 <div class="slide">
7e77ca11 540 <h1>Excellent Transaction Support</h1>
78c1762e 541<pre>$schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_do">txn_do</a>(sub {
7e77ca11 542 ...
543});
52d980cb 544
78c1762e 545my $guard = $schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_scope_guard">txn_scope_guard</a>;
7e77ca11 546# ...
547$guard-&gt;commit;
52d980cb 548
78c1762e 549$schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_begin">txn_begin</a>; # &lt;-- low level
7e77ca11 550# ...
78c1762e 551$schema-&gt;<a href="http://search.cpan.org/perldoc?DBIx::Class::Schema#txn_commit">txn_commit</a>;
7e77ca11 552</pre>
52d980cb 553 </div>
554
555 <div class="slide">
7e77ca11 556 <h1>InflateColumn</h1>
2a65778d 557<pre>package Foo::Schema::Result::Book;
558use base 'DBIx::Class::Core';
d1ed303d 559use DateTime::Format::MySQL;
451b57c2 560# Result code here
78c1762e 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>(
52d980cb 563 <strong>date_published</strong> =&gt; {
d1ed303d 564 inflate =&gt; sub {
565 DateTime::Format::MySQL-&gt;parse_date(
566 shift
567 )
568 },
569 deflate =&gt; sub { shift-&gt;ymd },
451b57c2 570 },
52d980cb 571);
d1ed303d 572# Automatic see: DBIC::InflateColumn::DateTime</pre>
52d980cb 573 </div>
574
575 <div class="slide">
7e77ca11 576 <h1>InflateColumn: deflation</h1>
52d980cb 577<pre>$book-&gt;date_published(DateTime-&gt;now);
578$book-&gt;update;</pre>
579 </div>
580
581 <div class="slide">
7e77ca11 582 <h1>InflateColumn: inflation</h1>
d1ed303d 583<pre>say $book-&gt;date_published-&gt;month_abbr;</pre>
52d980cb 584
585<strong><em>Nov</em></strong>
586 </div>
587
588 <div class="slide">
7e77ca11 589 <h1>FilterColumn</h1>
590<pre>package Foo::Schema::Result::Book;
2a65778d 591use base 'DBIx::Class::Core';
7e77ca11 592# Result code here
78c1762e 593__PACKAGE__-&gt;load_components('<a href="http://search.cpan.org/perldoc?DBIx::Class::FilterColumn">FilterColumn</a>');
52d980cb 594
78c1762e 595__PACKAGE__-&gt;<strong><a href="http://search.cpan.org/perldoc?DBIx::Class::FilterColumn#filter_column">filter_column</a></strong>(
7e77ca11 596 <strong>length</strong> =&gt; {
597 to_storage =&gt; 'to_metric',
598 from_storage =&gt; 'to_imperial',
599 },
52d980cb 600);
52d980cb 601
7e77ca11 602sub to_metric { $_[1] * .305 }
603sub to_imperial { $_[1] * 3.28 }
52d980cb 604 </div>
605
606 <div class="slide">
78c1762e 607 <h1><a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSetColumn">ResultSetColumn</a></h1>
d1ed303d 608<pre>my $rsc = $schema-&gt;resultset('Book')
609 -&gt;get_column('price');
63d0b680 610$rsc-&gt;first;
611$rsc-&gt;all;
7e77ca11 612$rsc-&gt;min;
613$rsc-&gt;max;
614$rsc-&gt;sum;
6548782a 615</pre>
52d980cb 616 </div>
617
618 <div class="slide">
7e77ca11 619 <h1>Aggregates</h1>
620<pre>my @res = $rs-&gt;search({}, {
78c1762e 621 <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#select">select</a> =&gt; [
d1ed303d 622 'price',
623 'genre',
624 { max =&gt; price },
625 { avg =&gt; price },
626 ],
78c1762e 627 <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#as">as</a> =&gt; [
d1ed303d 628 qw(price genre max_price avg_price)
629 ],
78c1762e 630 <a href="http://search.cpan.org/perldoc?DBIx::Class::ResultSet#group_by">group_by</a> =&gt; [qw(price genre)],
7e77ca11 631});
632for (@res) {
633 say $_-&gt;price . ' ' . $_-&gt;genre;
634 say $_-&gt;get_column('max_price');
635 say $_-&gt;get_column('min_price');
636}</pre>
d1ed303d 637 </div>
638
639 <div class="slide">
640 <h1>Aggregates Notes</h1>
7e77ca11 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>
52d980cb 647 </div>
648
649 <div class="slide">
7e77ca11 650 <h1>HRI</h1>
651<pre>$rs-&gt;search({}, {
78c1762e 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>',
52d980cb 654});</pre>
7e77ca11 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>
52d980cb 660 </ul>
661 </div>
662
663 <div class="slide">
78c1762e 664 <h1><a href="http://search.cpan.org/perldoc?DBIx::Class::Manual::Cookbook#Subqueries">Subquery</a> Support</h1>
d1ed303d 665<pre>my $inside_query = $schema-&gt;resultset('Artist')
666 -&gt;search({
7e77ca11 667 name =&gt; [ 'Billy Joel', 'Brittany Spears' ],
d1ed303d 668})-&gt;get_column('id')-&gt;as_query;
9158dee5 669
7e77ca11 670my $rs = $schema-&gt;resultset('CD')-&gt;search({
d1ed303d 671 artist_id =&gt; { -in =&gt; $inside_query },
7e77ca11 672});</pre>
52d980cb 673 </div>
674
675 <div class="slide">
78c1762e 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>
7e77ca11 677<pre>$rs-&gt;update({
d1ed303d 678 # !!! SQL INJECTION VECTOR
679 price =&gt; \"price + $inc",
7e77ca11 680});
681
682$rs-&gt;update({
683 price =&gt; \['price + ?', [inc =&gt; $inc]],
684});
685</pre>
52d980cb 686 </div>
687
688 <div class="slide">
7e77ca11 689 <h1>Questions?</h1>
52d980cb 690 </div>
691
692 <div class="slide">
7e77ca11 693 <h1>END</h1>
52d980cb 694 </div>
52d980cb 695</div>
696</body>
697</html>