done with basic overall slides
[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>
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">
141 <li>needed MSSQL Order by support, they helped me add support</li>
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>
01b7b88c 182<pre>my $book = $book_rs-&gt;create({
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>
188 </ul>
52d980cb 189 </div>
190
191 <div class="slide">
7365a110 192 <h1>SQL: Read</h1>
193<pre>my $sth = $dbh-&gt;prepare('
194 SELECT title,
195 authors.name as author_name
196 FROM books, authors
197 WHERE books.author = authors.id
198');</pre>
199 </div>
200
201 <div class="slide">
202 <h1>SQL: Read</h1>
203<pre>while( my $book = $sth-&gt;fetchrow_hashref() ) {
204 print 'Author of '
205 . $book-&gt;{title}
206 . ' is '
207 . $book-&gt;{author_name}
208 . "\n";
209}</pre>
52d980cb 210 </div>
211
212 <div class="slide">
5cac74c7 213 <h1>DBIC: Read</h1>
01b7b88c 214<pre>my $book = $book_rs-&gt;find($book_id);
52d980cb 215
01b7b88c 216my $book = $book_rs-&gt;search({
52d980cb 217 title =&gt; 'A book title',
5cac74c7 218}, { rows =&gt; 1 })-&gt;single;
52d980cb 219
01b7b88c 220my @books = $book_rs-&gt;search({
52d980cb 221 author =&gt; $author_id,
222})-&gt;all;</pre>
da0b46fd 223 <ul class="incremental">
224 <li>TMTOWTDI</li>
225 </ul>
52d980cb 226 </div>
227
228 <div class="slide">
5cac74c7 229 <h1>DBIC: Read</h1>
230<pre>while( my $book = $books_rs-&gt;next ) {
52d980cb 231 print 'Author of '
232 . $book-&gt;title
233 . ' is '
234 . $book-&gt;author-&gt;name
235 . "\n";
236}</pre>
237 </div>
238
239 <div class="slide">
7365a110 240 <h1>SQL: Update</h1>
241<pre>my $update = $dbh-&gt;prepare('
242 UPDATE books
243 SET title = ?
244 WHERE id = ?
245');
246
247$update-&gt;execute(
248 'New title',<strong>$book_id</strong>
249);</pre>
250 </div>
251
252 <div class="slide">
5cac74c7 253 <h1>DBIC: Update</h1>
52d980cb 254<pre>$book-&gt;update({
255 title =&gt; 'New title',
256});</pre>
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>
52d980cb 271<pre>$book-&gt;delete;</pre>
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">
291 <li>find_or_create</li>
292 <li>create_or_update</li>
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>
322<pre>my $schema = Foo::Schema-&gt;connect($dsn, $user, $pass);
323$schema-&gt;deploy
324</pre>
325<p>See also: <a href="http://search.cpan.org/perldoc?DBIx::Class::DeploymentHandler">DBIx::Class::DeploymentHandler</a></p>
326 </div>
327
328 <div class="slide">
52d980cb 329 <h1>Schema::Loader</h1>
9158dee5 330 <p>DB -&gt; Perl</p>
451b57c2 331<pre>package Foo::Schema;
c7321880 332use strict; use warnings;
333use base 'DBIx::Class::Schema::Loader';
334__PACKAGE__-&gt;loader_options({
335 naming =&gt; 'v7',
336 debug =&gt; $ENV{DBIC_TRACE},
337});
3381;
339
340# elsewhere...
341
451b57c2 342my $schema = Foo::Schema-&gt;connect($dsn, $user, $pass);
c7321880 343</pre>
52d980cb 344 </div>
345
346 <div class="slide">
da0b46fd 347 <h1>Populate</h1>
348 <p>Made for inserting lots of rows very quicky into database</p>
349<pre>$schema-&gt;populate([ Users =&gt;
350 [qw( username password )],
351 [qw( frew &gt;=4char$ )],
352 [qw( ... )],
353 [qw( ... )],
354);
355</pre>
356 <ul class="incremental">
357 <li>I use this to <a href="http://blog.afoolishmanifesto.com/archives/1255">export our whole (200M~) db to SQLite</a></li>
358 </ul>
52d980cb 359 </div>
360
361 <div class="slide">
da0b46fd 362 <h1>Multicreate</h1>
363 <p>Create an object and all of it's related objects all at once</p>
364<pre>$schema-&gt;resultset('Author')-&gt;create({
365 name =&gt; 'Stephen King',
366 books =&gt; [{ title =&gt; 'The Dark Tower' }],
367 address =&gt; {
368 street =&gt; '123 Turtle Back Lane',
369 state =&gt; { abbreviation =&gt; 'ME' },
370 city =&gt; { name =&gt; 'Lowell' },
371 },
7e77ca11 372});
da0b46fd 373</pre>
374 <div class="notes">
375 <ul>
376 <li>books is a has_many</li>
377 <li>address is a belongs_to which in turn belongs to state and city each</li>
378 <li>for this to work right state and city must mark abbreviation and name as unique</li>
379 </ul>
380 </div>
381 </div>
382
383 <div class="slide">
384 <h1>Extensible</h1>
385 <p>DBIx::Class helped pioneer fast MI in Perl 5 with Class::C3, so it is made
386 to allow extensions to nearly every part of it.</p>
387 </div>
388
389 <div class="slide">
390 <h1>Extensible: DBIC::Helpers</h1>
391 <ul class="incremental">
392 <li>DBIx::Class::Helper::ResultSet::IgnoreWantarray</li>
393 <li>DBIx::Class::Helper::ResultSet::Random</li>
394 <li>DBIx::Class::Helper::ResultSet::SetOperations</li>
395 <li>DBIx::Class::Helper::Row::JoinTable</li>
396 <li>DBIx::Class::Helper::Row::NumifyGet</li>
397 <li>DBIx::Class::Helper::Row::SubClass</li>
398 <li>DBIx::Class::Helper::Row::ToJSON</li>
399 </ul>
400 </div>
401
402 <div class="slide">
403 <h1>Extensible: DBIC::TimeStamp</h1>
404 <ul class="incremental">
405 <li>Cross DB</li>
406 <li>set_on_create</li>
407 <li>set_on_update</li>
408 </ul>
409 </div>
410
411 <div class="slide">
412 <h1>Extensible: DBIx::Class::Schema::KiokuDB</h1>
413 <ul class="incremental">
414 <li>Kioku is the new hotness</li>
415 <li>Mix RDBMS with Object DB</li>
416 <li>beta ( == sexy )</li>
417 </ul>
418 </div>
419
420 <div class="slide">
421 <h1>SQL::Abstract</h1>
7e77ca11 422<pre>my $resultset = $book_rs-&gt;search({
423 name =&gt; { -like =&gt; "%$nick%" },
424});</pre>
da0b46fd 425 <ul class="incremental">
426 <li>(kinda) introspectible</li>
427 <li>Prettier than SQL</li>
428 </ul>
429 </div>
430
431 <div class="slide">
432 <h1>Result vs ResultSet</h1>
433 <ul class="incremental">
434 <li>Result == Row</li>
435 <li>ResultSet == Query</li>
436 <li>(less important but...)</li>
437 <li>ResultSource == Table</li>
438 <li>Storage == Database</li>
439 </ul>
440 </div>
441
442 <div class="slide">
443 <h1>ResultSet methods</h1>
444<pre>package MyApp::Schema::ResultSet::Book;
445use base 'DBIx::Class::ResultSet';
446sub good {
7e77ca11 447 $_[0]-&gt;search({
da0b46fd 448 rating =&gt; { '&gt;=' =&gt; 4 },
449 })
450};
451sub cheap {
7e77ca11 452 $_[0]-&gt;search({
da0b46fd 453 price =&gt; { '&lt;=' =&gt; 5}
454 })
455};
456# ...
4571;
458 </pre>
459 <ul class="incremental">
460 <li>All searches should be ResultSet methods</li>
461 <li>Name has obvious meaning</li>
462 </ul>
463 </div>
464
465 <div class="slide">
466 <h1>ResultSet method in Action</h1>
467 <pre>$schema-&gt;resultset('Book')-&gt;good</pre>
468 </div>
469
470 <div class="slide">
471 <h1>ResultSet Chaining</h1>
472<pre>$schema-&gt;resultset('Book')
473 -&gt;good
474 -&gt;cheap
475 -&gt;recent
6e5edefe 476</pre>
52d980cb 477 </div>
478
479 <div class="slide">
7e77ca11 480 <h1>search_related</h1>
481 <pre>*get example from work*</pre>
52d980cb 482 </div>
483
484 <div class="slide">
7e77ca11 485 <h1>bonus rel methods</h1>
486<pre>my $book = $author-&gt;create_related(
487 <strong>books</strong> =&gt; {
488 title =&gt; 'Another Discworld book',
489});
52d980cb 490
7e77ca11 491my $book2 = $pratchett-&gt;add_to_<strong>books</strong>({
492 title =&gt; 'MOAR Discworld book',
493});</pre>
494 <ul class="incremental">
495 <li>Automaticaly fills in foreign key for you</li>
496 </ul>
52d980cb 497 </div>
498
499 <div class="slide">
7e77ca11 500 <h1>Excellent Transaction Support</h1>
501<pre>$schema-&gt;txn_do(sub {
502 ...
503});
52d980cb 504
7e77ca11 505my $guard = $schema-&gt;txn_scope_guard;
506# ...
507$guard-&gt;commit;
52d980cb 508
7e77ca11 509$schema-&gt;txn_begin; # &lt;-- low level
510# ...
511$schema-&gt;txn_commit;
512</pre>
52d980cb 513 </div>
514
515 <div class="slide">
7e77ca11 516 <h1>InflateColumn</h1>
2a65778d 517<pre>package Foo::Schema::Result::Book;
518use base 'DBIx::Class::Core';
451b57c2 519# Result code here
451b57c2 520__PACKAGE__-&gt;load_components('InflateColumn');
52d980cb 521use DateTime::Format::MySQL;
52d980cb 522__PACKAGE__-&gt;<strong>inflate_column</strong>(
523 <strong>date_published</strong> =&gt; {
524 inflate =&gt; sub { DateTime::Format::MySQL-&gt;parse_date(shift) },
525 deflate =&gt; sub { shift-&gt;ymd},
451b57c2 526 },
52d980cb 527);
528# Automatic see: DBIx::Class::InflateColumn::DateTime</pre>
529 </div>
530
531 <div class="slide">
7e77ca11 532 <h1>InflateColumn: deflation</h1>
52d980cb 533<pre>$book-&gt;date_published(DateTime-&gt;now);
534$book-&gt;update;</pre>
535 </div>
536
537 <div class="slide">
7e77ca11 538 <h1>InflateColumn: inflation</h1>
52d980cb 539<pre>my $date_published = $book-&gt;date_published;
540print $date_published-&gt;month_abbr;</pre>
541
542<strong><em>Nov</em></strong>
543 </div>
544
545 <div class="slide">
7e77ca11 546 <h1>FilterColumn</h1>
547<pre>package Foo::Schema::Result::Book;
2a65778d 548use base 'DBIx::Class::Core';
7e77ca11 549# Result code here
550__PACKAGE__-&gt;load_components('FilterColumn');
52d980cb 551
7e77ca11 552__PACKAGE__-&gt;<strong>filter_column</strong>(
553 <strong>length</strong> =&gt; {
554 to_storage =&gt; 'to_metric',
555 from_storage =&gt; 'to_imperial',
556 },
52d980cb 557);
52d980cb 558
7e77ca11 559sub to_metric { $_[1] * .305 }
560sub to_imperial { $_[1] * 3.28 }
561# Automatic see: DBIx::Class::InflateColumn::DateTime</pre>
52d980cb 562 </div>
563
564 <div class="slide">
7e77ca11 565 <h1>ResultSetColumn</h1>
566<pre>my $rsc = $schema-&gt;resultset('Book')-&gt;get_column('price');
567$rsc-&gt;min;
568$rsc-&gt;max;
569$rsc-&gt;sum;
6548782a 570</pre>
52d980cb 571 </div>
572
573 <div class="slide">
7e77ca11 574 <h1>Aggregates</h1>
575<pre>my @res = $rs-&gt;search({}, {
576 select =&gt; [qw(price genre), { max =&gt; price }, { avg =&gt; price }],
577 as =&gt; [qw(price genre max_price avg_price)],
578 group_by =&gt; [qw(price genre)],
579});
580for (@res) {
581 say $_-&gt;price . ' ' . $_-&gt;genre;
582 say $_-&gt;get_column('max_price');
583 say $_-&gt;get_column('min_price');
584}</pre>
585 <ul class="incremental">
586 <li>Careful, get_column can basicaly mean THREE things</li>
587 <li>private for get what you should use an accessor for</li>
588 <li>public for what there is no accessor for</li>
589 <li>public for get resultset column (prev slide)</li>
590 </ul>
52d980cb 591 </div>
592
593 <div class="slide">
7e77ca11 594 <h1>HRI</h1>
595<pre>$rs-&gt;search({}, {
596 result_class =&gt; 'DBIx::Class::ResultClass::HashRefInflator',
52d980cb 597});</pre>
7e77ca11 598 <ul class="incremental">
599 <li>Easy on memory</li>
600 <li>Mega fast</li>
601 <li>Great for quick debugging</li>
602 <li>Great for performance tuning (we went from 2m to &lt; 3s)</li>
52d980cb 603 </ul>
604 </div>
605
606 <div class="slide">
7e77ca11 607 <h1>Subquery Support</h1>
608<pre> my $inside_rs = $schema-&gt;resultset('Artist')-&gt;search({
609 name =&gt; [ 'Billy Joel', 'Brittany Spears' ],
610});
9158dee5 611
7e77ca11 612my $rs = $schema-&gt;resultset('CD')-&gt;search({
613 artist_id =&gt; { -in =&gt; $inside_rs-&gt;get_column('id')-&gt;as_query },
614});</pre>
52d980cb 615 </div>
616
617 <div class="slide">
7e77ca11 618 <h1>Bare SQL w/ Placeholders</h1>
619<pre>$rs-&gt;update({
620 price =&gt; \"price + $inc", # !!! SQL INJECTION VECTOR
621});
622
623$rs-&gt;update({
624 price =&gt; \['price + ?', [inc =&gt; $inc]],
625});
626</pre>
52d980cb 627 </div>
628
629 <div class="slide">
7e77ca11 630 <h1>Questions?</h1>
52d980cb 631 </div>
632
633 <div class="slide">
7e77ca11 634 <h1>END</h1>
52d980cb 635 </div>
52d980cb 636</div>
637</body>
638</html>