S::L code
[dbsrgits/dbix-class-introduction-presentation.git] / slideshow.html
index c058356..ad49e67 100644 (file)
@@ -77,8 +77,8 @@
       <h1>Authors</h1>
       <h4>Originally Leo Lapworth @ LPW 2009</h4>
       <h4>Matthew S. Trout</h4>
-      <h4>Arthur Axel "fREW" Schmidt</h4>
       <h4>Justin D. Hunter</h4>
+      <h4>Arthur Axel "fREW" Schmidt</h4>
    </div>
 
    <div class="slide">
    </div>
 
    <div class="slide">
-      <h1>why this talk?</h1>
-      <ul>
-         <li>Help avoid mistakes I made!</li>
-         <li>Help learn DBIx::Class faster</li>
-         <li>Make your coding easier</li>
-      </ul>
-   </div>
-
-   <div class="slide">
       <h1>point of note</h1>
-      <p><em>"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian W. Kernighan</em></p>
-      <p>This talk is about making it easy so we are less likely to get confused</p>
-   </div>
+      <p><em>"Debugging is twice as hard as writing the code in the first
+      place. Therefore, if you write the code as cleverly as possible,
+      you are, by definition, not smart enough to debug it." - Brian
+      W. Kernighan</em></p>
 
-   <div class="slide">
-      <h1>table setup</h1>
+      <p>This talk is about making it easy so we are less likely to get
+      confused</p>
    </div>
 
    <div class="slide">
-      <h1>example...</h1>
+      <h1>Examples Table Setup</h1>
       <ul>
          <li>Authors</li>
          <li>Books</li>
       </ul>
+      <em>MySQL not recommended</em>
    </div>
 
    <div class="slide">
    <div class="slide">
       <h1>tips</h1>
       <p>Name tables as simple plurals (<strong>add an S</strong>) - makes relationships easier to understand</p>
-      <p>(issue: Matt Trout "Tables should not be plural as gives you plurals for Result:: package names which represent a single row" - talk may be rewritten in future to reflect this as this is better once you understand the relationship setup - either way, consistency is important)</p>
       <p>Use a character set (<strong>UTF8</strong>) from the start (for international characters)</p>
    </div>
 
    <div class="slide">
       <h1>books table</h1>
 <pre>CREATE TABLE books(
-   id     int(8) primary key auto_increment,
-   title  varchar(255),
-   author int(8),foreign key (author)
+   id        int(8) primary key auto_increment,
+   title     varchar(255),
+   author_id int(8),foreign key (author)
       references authors(id)
 ) engine = InnoDB DEFAULT CHARSET=utf8;</pre>
    </div>
    <div class="slide">
       <h1>tips</h1>
       <p>Name link fields as singular</p>
-      <p>Check foreign key is the same field and type and size in both tables</p>
+      <p>Ensure foreign key is the same type and size in both tables</p>
    </div>
 
    <div class="slide">
       <h1>books table</h1>
 <pre>CREATE TABLE books(
-   id     int(8) primary key auto_increment,
-   title  varchar(255),
-   author <strong>int(8)</strong>,<strong>foreign key (<em>author</em>)</strong>
+   id        int(8) primary key auto_increment,
+   title     varchar(255),
+   author_id <strong>int(8)</strong>,<strong>foreign key (<em>author</em>)</strong>
       <strong>references <em>authors(id)</em></strong>
 ) engine = InnoDB DEFAULT CHARSET=utf8;</pre>
    </div>
    </div>
 
    <div class="slide">
-      <h1>manual: create</h1>
+      <h1>SQL: Create</h1>
 <pre>my $sth = $dbh-&gt;prepare('
    INSERT INTO books
    (title, author)
@@ -219,20 +211,7 @@ $sth-&gt;execute(
    </div>
 
    <div class="slide">
-      <h1>manual: create</h1>
-<pre>my $sth = $dbh-&gt;prepare('
-   <strong>INSERT INTO books
-   (title, author)
-   values (?,?)</strong>
-');
-
-$sth-&gt;execute(
-  'A book title', <strong><em>$author_id</em></strong>
-);</pre>
-   </div>
-
-   <div class="slide">
-      <h1>manual: retrieve</h1>
+      <h1>SQL: Read</h1>
 <pre>my $sth = $dbh-&gt;prepare('
    SELECT title,
    authors.name as author_name
@@ -242,7 +221,7 @@ $sth-&gt;execute(
    </div>
 
    <div class="slide">
-      <h1>manual: retrieve</h1>
+      <h1>SQL: Read</h1>
 <pre>while( my $book = $sth-&gt;fetchrow_hashref() ) {
   print 'Author of '
      . $book-&gt;{title}
@@ -253,7 +232,7 @@ $sth-&gt;execute(
    </div>
 
    <div class="slide">
-      <h1>manual: update</h1>
+      <h1>SQL: Update</h1>
 <pre>my $update = $dbh-&gt;prepare('
    UPDATE books
    SET title = ?
@@ -266,7 +245,7 @@ $update-&gt;execute(
    </div>
 
    <div class="slide">
-      <h1>manual: delete</h1>
+      <h1>SQL: Delete</h1>
 <pre>my $delete = $dbh-&gt;prepare('
    DELETE FROM books
    WHERE id = ?
@@ -280,44 +259,23 @@ $delete-&gt;execute(<strong>$book_id</strong>);</pre>
    </div>
 
    <div class="slide">
-      <h1>DBIC: create</h1>
+      <h1>DBIC: Create</h1>
 <pre>my $book = $book_model-&gt;create({
    title  =&gt; 'A book title',
    author =&gt; $author_id,
 });</pre>
       <p>Look ma, no SQL!</p>
-      <p><strong>Tip:</strong> do not pass in primary_key field, even if it's empty/undef as the object returned will have an empty id, even if your field is auto increment.</p>
-   </div>
-
-   <div class="slide">
-      <h1>DBIC: create</h1>
-<pre>my $book = $book_model-&gt;create({
-   title  =&gt; 'A book title',
-   author =&gt; <strong>$author_id</strong>,
-});</pre>
    </div>
 
    <div class="slide">
-      <h1>DBIC: create</h1>
+      <h1>DBIC: Create</h1>
 <pre>my $pratchett = $author_model-&gt;create({
    name =&gt; 'Terry Pratchett',
 });</pre>
    </div>
 
    <div class="slide">
-      <h1>DBIC: create</h1>
-<pre>my $book = $pratchett-&gt;create_related(
-  books =&gt; {
-   title =&gt; 'Another Discworld book',
-});</pre>
-<strong>or</strong>
-<pre>my $book = $pratchett-&gt;add_to_books({
-   title =&gt; 'Another Discworld book',
-});</pre>
-   </div>
-
-   <div class="slide">
-      <h1>DBIC: create</h1>
+      <h1>DBIC: Create</h1>
 <pre>my $book = $pratchett-&gt;create_related(
   <strong>books</strong> =&gt; {
    title =&gt; 'Another Discworld book',
@@ -329,18 +287,18 @@ $delete-&gt;execute(<strong>$book_id</strong>);</pre>
    </div>
 
    <div class="slide">
-      <h1>DBIC: retrieve</h1>
+      <h1>DBIC: Read</h1>
       <p>DBIx::Class - Lots of ways to do the same thing...</p>
       <p><em>"There is more than one way to do it (TIMTOWTDI, usually pronounced "Tim Toady") is a Perl motto"</em></p>
    </div>
 
    <div class="slide">
-      <h1>DBIC: retrieve</h1>
+      <h1>DBIC: Read</h1>
 <pre>my $book = $book_model-&gt;find($book_id);
 
 my $book = $book_model-&gt;search({
    title =&gt; 'A book title',
-})-&gt;single;
+}, { rows =&gt; 1 })-&gt;single;
 
 my @books = $book_model-&gt;search({
    author =&gt; $author_id,
@@ -348,8 +306,8 @@ my @books = $book_model-&gt;search({
    </div>
 
    <div class="slide">
-      <h1>DBIC: retrieve</h1>
-<pre>while( my $book = $books_rs-&gt;next() ) {
+      <h1>DBIC: Read</h1>
+<pre>while( my $book = $books_rs-&gt;next ) {
  print 'Author of '
     . $book-&gt;title
     . ' is '
@@ -359,8 +317,8 @@ my @books = $book_model-&gt;search({
    </div>
 
    <div class="slide">
-      <h1>DBIC: retrieve</h1>
-<pre>my $books_rs = $book_model-&gt;search({
+      <h1>DBIC: Read</h1>
+<pre>my $books_rs = $book_rs-&gt;search({
    author =&gt; $author_id,
 });</pre>
       <p>Search takes SQL::Abstract formatted queries</p>
@@ -368,14 +326,14 @@ my @books = $book_model-&gt;search({
    </div>
 
    <div class="slide">
-      <h1>DBIC: update</h1>
+      <h1>DBIC: Update</h1>
 <pre>$book-&gt;update({
   title =&gt; 'New title',
 });</pre>
    </div>
 
    <div class="slide">
-      <h1>DBIC: delete</h1>
+      <h1>DBIC: Delete</h1>
 <pre>$book-&gt;delete;</pre>
    </div>
 
@@ -384,27 +342,78 @@ my @books = $book_model-&gt;search({
    </div>
 
    <div class="slide">
-      <pre>Example of a DBIC Result</pre>
-   </div>
-
-   <div class="slide">
-      <pre>Example of a DBIC Result</pre>
+<pre>package MyApp::Schema::Result::Author;
+use strict; use warnings;
+__PACKAGE__-&gt;table('authors');
+__PACKAGE__-&gt;add_columns(
+  id =&gt; {
+    data_type =&gt; 'int',
+    size      =&gt; 8,
+  },
+  title =&gt; {
+    data_type   =&gt; 'varchar',
+    is_nullable =&gt; 1,
+    size        =&gt; 255,
+  },
+);
+__PACKAGE__-&gt;set_primary_key('id');
+__PACKAGE__-&gt;has_many( books =&gt;
+   'MyApp::Schema::Result::Book', 'author_id'
+);
+1;
+</pre>
    </div>
 
    <div class="slide">
-      <p>too much typing!  too much maintenance!</p>
-      <p>too much maintenance!</p>
+<pre>package MyApp::Schema::Result::Book;
+use strict; use warnings;
+__PACKAGE__-&gt;table('books');
+__PACKAGE__-&gt;add_columns(
+  id =&gt; {
+    data_type =&gt; 'int',
+    size      =&gt; 8,
+  },
+  name =&gt; {
+    data_type   =&gt; 'varchar',
+    is_nullable =&gt; 1,
+    size        =&gt; 255,
+  },
+  author_id =&gt; {
+    data_type   =&gt; 'int',
+    size        =&gt; 8,
+    is_nullable =&gt; 1, # &lt;-- probably should be 0
+  },
+);
+__PACKAGE__-&gt;set_primary_key('id');
+__PACKAGE__-&gt;belongs_to( author =&gt;
+   'MyApp::Schema::Result::Author', 'author_id'
+);
+1;
+</pre>
    </div>
 
    <div class="slide">
       <h1>Schema::Loader</h1>
-      <pre>code for S::L here</pre>
+      <p>DB -&gt; Perl vs Perl -&gt; DB</p>
+<pre>package MyApp::Schema;
+use strict; use warnings;
+use base 'DBIx::Class::Schema::Loader';
+__PACKAGE__-&gt;loader_options({
+   naming =&gt; 'v7',
+   debug  =&gt; $ENV{DBIC_TRACE},
+});
+1;
+
+# elsewhere...
+
+my $schema = MyApp::Schema-&gt;connect($dsn, $user, $pass);
+</pre>
    </div>
 
    <div class="slide">
       <h1>splitting logic cleanly</h1>
-      <p>Foo::DBIC::Result::Foo = an individual row</p>
-      <p>Foo::DBIC::ResultSet::Foo = searches / results</p>
+      <p>Foo::Schema::Result::Foo = an individual row</p>
+      <p>Foo::Schema::ResultSet::Foo = searches / results</p>
    </div>
 
    <div class="slide">
@@ -419,8 +428,8 @@ my @books = $book_model-&gt;search({
 
    <div class="slide">
       <h1>Schema::Loader</h1>
-<pre>Foo::DBIC::Result::Authors-&gt;table("authors");
-Foo::DBIC::Result::Authors-&gt;add_columns(
+<pre>Foo::Schema::Result::Authors-&gt;table("authors");
+Foo::Schema::Result::Authors-&gt;add_columns(
    id =&gt; {
       data_type     =&gt; "INT",
       default_value =&gt; undef,
@@ -434,13 +443,13 @@ Foo::DBIC::Result::Authors-&gt;add_columns(
       size          =&gt; 255,
    },
 );
-Foo::DBIC::Result::Authors-&gt;set_primary_key("id");</pre>
+Foo::Schema::Result::Authors-&gt;set_primary_key("id");</pre>
    </div>
 
    <div class="slide">
       <h1>Schema::Loader</h1>
-<pre>Foo::DBIC::Result::Books->table("books");
-Foo::DBIC::Result::Books->add_columns(
+<pre>Foo::Schema::Result::Books->table("books");
+Foo::Schema::Result::Books->add_columns(
    id =&gt; {
       data_type     =&gt; "INT",
       default_value =&gt; undef,
@@ -460,15 +469,15 @@ Foo::DBIC::Result::Books->add_columns(
       size          =&gt; 8
    },
 );
-Foo::DBIC::Result::Books-&gt;set_primary_key("id");</pre>
+Foo::Schema::Result::Books-&gt;set_primary_key("id");</pre>
    </div>
 
    <div class="slide">
       <h1>Schema::Loader</h1>
-<pre>Foo::DBIC::Result::Authors-&gt;has_many(books =&gt; "Foo::DBIC::Books",
+<pre>Foo::Schema::Result::Authors-&gt;has_many(books =&gt; "Foo::Schema::Books",
    { "foreign.author" =&gt; "self.id" });
 
-Foo::DBIC::Result::Books-&gt;belongs_to(author =&gt; "Foo::DBIC::Authors",
+Foo::Schema::Result::Books-&gt;belongs_to(author =&gt; "Foo::Schema::Authors",
    { id =&gt; "author" });</pre>
    </div>
 
@@ -483,15 +492,15 @@ INSERT INTO books (author, title)
 
    <div class="slide">
       <h1>overloading</h1>
-<pre>Foo::DBIC::Result::Books
-Foo::DBIC::ResultSet::Books
-Foo::DBIC::Result::Authors
-Foo::DBIC::ResultSet::Books</pre>
+<pre>Foo::Schema::Result::Books
+Foo::Schema::ResultSet::Books
+Foo::Schema::Result::Authors
+Foo::Schema::ResultSet::Books</pre>
    </div>
 
    <div class="slide">
       <h1>Result::</h1>
-<pre>package Foo::DBIC::Result::Books;
+<pre>package Foo::Schema::Result::Books;
 use base 'DBIx::Class';
 use strict;
 use warnings;
@@ -517,7 +526,7 @@ sub isbn {
 
    <div class="slide">
       <h1>Result:: (inflating)</h1>
-<pre>package Foo::DBIC::Result::Books;
+<pre>package Foo::Schema::Result::Books;
 use base 'DBIx::Class';
 use strict;
 use warnings;
@@ -535,7 +544,7 @@ __PACKAGE__-&gt;<strong>inflate_column</strong>(
 
    <div class="slide">
       <h1>Result:: (inflating)</h1>
-<pre>package Foo::DBIC::Result::Books;
+<pre>package Foo::Schema::Result::Books;
 use base 'DBIx::Class';
 use strict;
 use warnings;
@@ -569,7 +578,7 @@ print $date_published-&gt;month_abbr;</pre>
 
    <div class="slide">
       <h1>ResultSets::</h1>
-<pre>package Foo::DBIC::ResultSet::Books;
+<pre>package Foo::Schema::ResultSet::Books;
 use base 'DBIx::Class::ResultSet';
 sub the_ultimate_books {
    my $self = shift;
@@ -585,7 +594,7 @@ sub by_author {
 
    <div class="slide">
       <h1>ResultSets::</h1>
-<pre>package Foo::DBIC::<strong>ResultSet::Books</strong>;
+<pre>package Foo::Schema::<strong>ResultSet::Books</strong>;
 use base '<strong>DBIx::Class::ResultSet</strong>';
 sub the_ultimate_books {
    my $self = shift;
@@ -601,7 +610,7 @@ sub by_author {
 
    <div class="slide">
       <h1>ResultSets::</h1>
-<pre>package Foo::DBIC::ResultSet::Books;
+<pre>package Foo::Schema::ResultSet::Books;
 use base 'DBIx::Class::ResultSet';
 sub the_ultimate_books {
    my $self = shift;
@@ -617,17 +626,17 @@ sub by_author {
 
    <div class="slide">
       <h1>ResultSets::</h1>
-<pre>use Foo::DBIC;
-my $book_model = Foo::DBIC-&gt;resultset('Books');
+<pre>use Foo::Schema;
+my $book_model = Foo::Schema-&gt;resultset('Books');
 my $book_rs    = $book_model-&gt;the_ultimate_books;
 my @books      = $book_rs-&gt;all;</pre>
    </div>
 
    <div class="slide">
       <h1>ResultSets::chaining</h1>
-<pre>use Foo::DBIC;
-my $book_model   = Foo::DBIC-&gt;resultset('Books');
-my $author_model = Foo::DBIC-&gt;resultset('Authors');
+<pre>use Foo::Schema;
+my $book_model   = Foo::Schema-&gt;resultset('Books');
+my $author_model = Foo::Schema-&gt;resultset('Authors');
 my $author       = $author_model-&gt;search({ name =&gt; 'Douglas Adams' })-&gt;single;
 my $book_rs      = $book_model-&gt;the_ultimate_books-&gt;by_author($author);
 my @books        = $book_rs-&gt;all;</pre>
@@ -669,7 +678,7 @@ my @books = $rs-&gt;all;</pre>
 
    <div class="slide">
       <h1>overloading before new record</h1>
-      <pre>package Foo::DBIC::Result::Authors;
+      <pre>package Foo::Schema::Result::Authors;
 use base 'DBIx::Class';
 
 sub new {
@@ -731,9 +740,9 @@ ALTER TABLE `books` DROP `author`</pre>
 
    <div class="slide">
       <h1>has_many</h1>
-<pre>package Foo::DBIC::<strong>Result::Books</strong>;
+<pre>package Foo::Schema::<strong>Result::Books</strong>;
 
-__PACKAGE__-&gt;has_many( author_and_books =&gt; "Foo::DBIC::Result::AuthorAndBooks",
+__PACKAGE__-&gt;has_many( author_and_books =&gt; "Foo::Schema::Result::AuthorAndBooks",
     { "foreign.book" =&gt; "self.id" },
 );
 
@@ -742,11 +751,11 @@ __PACKAGE__-&gt;has_many( author_and_books =&gt; "Foo::DBIC::Result::AuthorAndBo
 
    <div class="slide">
       <h1>has_many</h1>
-<pre>package Foo::DBIC::<strong>Result::Books</strong>;
+<pre>package Foo::Schema::<strong>Result::Books</strong>;
 
 __PACKAGE__-&gt;has_many(
 author_and_books =&gt; <strong># name of accessor</strong>
-"Foo::DBIC::Result::AuthorAndBooks", <strong># related class</strong>
+"Foo::Schema::Result::AuthorAndBooks", <strong># related class</strong>
     { "foreign.book" =&gt; "self.id" } <strong># Relationship (magic often works if not
                                         # specified, but avoid!)</strong>
 );
@@ -759,11 +768,11 @@ author_and_books =&gt; <strong># name of accessor</strong>
 
    <div class="slide">
       <h1>belongs_to</h1>
-<pre>package Foo::DBIC::<strong>Result::AuthorAndBooks</strong>;
+<pre>package Foo::Schema::<strong>Result::AuthorAndBooks</strong>;
 
 __PACKAGE__-&gt;belongs_to(
    book =&gt; <strong># Accessor name</strong>
-   "Foo::DBIC::Result::Books", <strong># Related class</strong>
+   "Foo::Schema::Result::Books", <strong># Related class</strong>
    { id =&gt; "book" } <strong># relationship</strong>
 );
 </pre>
@@ -783,7 +792,7 @@ __PACKAGE__-&gt;belongs_to(
 
    <div class="slide">
       <h1>many_to_many</h1>
-      <pre>package Foo::DBIC::<strong>Result::Books</strong>;
+      <pre>package Foo::Schema::<strong>Result::Books</strong>;
 use base 'DBIx::Class';
 
 __PACKAGE__-&gt;many_to_many(
@@ -797,7 +806,7 @@ __PACKAGE__-&gt;many_to_many(
 
    <div class="slide">
       <h1>many_to_many</h1>
-      <pre>package Foo::DBIC::<strong>Result::Books</strong>;
+      <pre>package Foo::Schema::<strong>Result::Books</strong>;
 use base 'DBIx::Class';
 
 __PACKAGE__-&gt;many_to_many(
@@ -811,7 +820,7 @@ __PACKAGE__-&gt;many_to_many(
 
    <div class="slide">
       <h1>many_to_many</h1>
-      <pre>package Foo::DBIC::Result::Authors;
+      <pre>package Foo::Schema::Result::Authors;
 use base 'DBIx::Class';
 
 __PACKAGE__-&gt;many_to_many(
@@ -829,9 +838,9 @@ __PACKAGE__-&gt;many_to_many(
       <h1>using many_to_many</h1>
       <pre>#!/usr/bin/perl
 
-use Foo::DBIC;
+use Foo::Schema;
 
-my $author_model = Foo::DBIC-&gt;resultset('Authors');
+my $author_model = Foo::Schema-&gt;resultset('Authors');
 my $author       = $author_model-&gt;search({
    name =&gt; 'Douglas Adams',
 })-&gt;single;
@@ -875,12 +884,12 @@ $book-&gt;add_to_authors($author_2);</pre>
 
    <div class="slide">
       <h1>error messages</h1>
-      <pre>DBIx::Class::Schema::Loader::connection(): Failed to load external class definition for 'Foo::DBIC::Result::Authors': Can't locate object method "many_to_many" via package "Foo::DBIC::Result::Author" at lib/Foo/DBIC/Result/Authors.pm line 9.Compilation failed in require at /Library/Perl/5.8.8/DBIx/Class/Schema/Loader/Base.pm line 292.</pre>
+      <pre>DBIx::Class::Schema::Loader::connection(): Failed to load external class definition for 'Foo::Schema::Result::Authors': Can't locate object method "many_to_many" via package "Foo::Schema::Result::Author" at lib/Foo/Schema/Result/Authors.pm line 9.Compilation failed in require at /Library/Perl/5.8.8/DBIx/Class/Schema/Loader/Base.pm line 292.</pre>
    </div>
 
    <div class="slide">
       <h1>error messages</h1>
-   <pre>DBIx::Class::Schema::Loader::connection(): Failed to load external class definition for 'Foo::DBIC::Result::Authors': Can't locate object method "many_to_many" via package "Foo::DBIC::<strong>Result::Author</strong>" at lib/Foo/DBIC/<strong>Result/Authors.pm</strong> line 9.Compilation failed in require at /Library/Perl/5.8.8/DBIx/Class/Schema/Loader/Base.pm line 292.</pre>
+   <pre>DBIx::Class::Schema::Loader::connection(): Failed to load external class definition for 'Foo::Schema::Result::Authors': Can't locate object method "many_to_many" via package "Foo::Schema::<strong>Result::Author</strong>" at lib/Foo/Schema/<strong>Result/Authors.pm</strong> line 9.Compilation failed in require at /Library/Perl/5.8.8/DBIx/Class/Schema/Loader/Base.pm line 292.</pre>
    </div>
 
    <div class="slide">
@@ -911,13 +920,13 @@ $book-&gt;add_to_authors($author_2);</pre>
    <div class="slide">
       <h1>Catalyst</h1>
       <pre>package Your::App::Model::<strong>Foo</strong>;
-use base qw(<strong>Catalyst::Model::DBIC::Schema</strong>);
+use base qw(<strong>Catalyst::Model::Schema::Schema</strong>);
 
 use strict;
 use warnings;
 
 __PACKAGE__-&gt;config(
-  schema_class =&gt; '<strong>Foo::DBIC</strong>',
+  schema_class =&gt; '<strong>Foo::Schema</strong>',
 );
 
 1;</pre>
@@ -929,7 +938,7 @@ __PACKAGE__-&gt;config(
 <pre>sub action_name : Local {
   my ($self, $c) = @_;
 
-  my $model = $c-&gt;model('DBIC::Foo');
+  my $model = $c-&gt;model('Schema::Foo');
   my $author_model = $model-&gt;resultset('Authors');
 
 }