From: Arthur Axel 'fREW' Schmidt Date: Fri, 28 May 2010 04:17:20 +0000 (-0500) Subject: rework some stuff, remove useless slides X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2Fdbix-class-introduction-presentation.git;a=commitdiff_plain;h=5cac74c74533a12ebb17faa6abdef8017ee073be rework some stuff, remove useless slides --- diff --git a/slideshow.html b/slideshow.html index 68f2a07..f751727 100644 --- a/slideshow.html +++ b/slideshow.html @@ -77,8 +77,8 @@

Authors

Originally Leo Lapworth @ LPW 2009

Matthew S. Trout

-

Arthur Axel "fREW" Schmidt

Justin D. Hunter

+

Arthur Axel "fREW" Schmidt

@@ -115,30 +115,23 @@
-

why this talk?

- -
- -

point of note

-

"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

-

This talk is about making it easy so we are less likely to get confused

-
+

"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

-
-

table setup

+

This talk is about making it easy so we are less likely to get + confused

-

example...

+

Examples Table Setup

+ MySQL not recommended
@@ -152,7 +145,6 @@

tips

Name tables as simple plurals (add an S) - makes relationships easier to understand

-

(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)

Use a character set (UTF8) from the start (for international characters)

@@ -167,9 +159,9 @@

books table

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;
@@ -178,15 +170,15 @@

tips

Name link fields as singular

-

Check foreign key is the same field and type and size in both tables

+

Ensure foreign key is the same type and size in both tables

books table

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;
@@ -206,7 +198,7 @@
-

manual: create

+

SQL: Create

my $sth = $dbh->prepare('
    INSERT INTO books
    (title, author)
@@ -219,20 +211,7 @@ $sth->execute(
    
-

manual: create

-
my $sth = $dbh->prepare('
-   INSERT INTO books
-   (title, author)
-   values (?,?)
-');
-
-$sth->execute(
-  'A book title', $author_id
-);
-
- -
-

manual: retrieve

+

SQL: Read

my $sth = $dbh->prepare('
    SELECT title,
    authors.name as author_name
@@ -242,7 +221,7 @@ $sth->execute(
    
-

manual: retrieve

+

SQL: Read

while( my $book = $sth->fetchrow_hashref() ) {
   print 'Author of '
      . $book->{title}
@@ -253,7 +232,7 @@ $sth->execute(
    
-

manual: update

+

SQL: Update

my $update = $dbh->prepare('
    UPDATE books
    SET title = ?
@@ -266,7 +245,7 @@ $update->execute(
    
-

manual: delete

+

SQL: Delete

my $delete = $dbh->prepare('
    DELETE FROM books
    WHERE id = ?
@@ -280,44 +259,23 @@ $delete->execute($book_id);
-

DBIC: create

+

DBIC: Create

my $book = $book_model->create({
    title  => 'A book title',
    author => $author_id,
 });

Look ma, no SQL!

-

Tip: 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.

-

DBIC: create

-
my $book = $book_model->create({
-   title  => 'A book title',
-   author => $author_id,
-});
-
- -
-

DBIC: create

+

DBIC: Create

my $pratchett = $author_model->create({
    name => 'Terry Pratchett',
 });
-

DBIC: create

-
my $book = $pratchett->create_related(
-  books => {
-   title => 'Another Discworld book',
-});
-or -
my $book = $pratchett->add_to_books({
-   title => 'Another Discworld book',
-});
-
- -
-

DBIC: create

+

DBIC: Create

my $book = $pratchett->create_related(
   books => {
    title => 'Another Discworld book',
@@ -329,18 +287,18 @@ $delete->execute($book_id);
-

DBIC: retrieve

+

DBIC: Read

DBIx::Class - Lots of ways to do the same thing...

"There is more than one way to do it (TIMTOWTDI, usually pronounced "Tim Toady") is a Perl motto"

-

DBIC: retrieve

+

DBIC: Read

my $book = $book_model->find($book_id);
 
 my $book = $book_model->search({
    title => 'A book title',
-})->single;
+}, { rows => 1 })->single;
 
 my @books = $book_model->search({
    author => $author_id,
@@ -348,8 +306,8 @@ my @books = $book_model->search({
    
-

DBIC: retrieve

-
while( my $book = $books_rs->next() ) {
+      

DBIC: Read

+
while( my $book = $books_rs->next ) {
  print 'Author of '
     . $book->title
     . ' is '
@@ -359,8 +317,8 @@ my @books = $book_model->search({
    
-

DBIC: retrieve

-
my $books_rs = $book_model->search({
+      

DBIC: Read

+
my $books_rs = $book_rs->search({
    author => $author_id,
 });

Search takes SQL::Abstract formatted queries

@@ -368,14 +326,14 @@ my @books = $book_model->search({
-

DBIC: update

+

DBIC: Update

$book->update({
   title => 'New title',
 });
-

DBIC: delete

+

DBIC: Delete

$book->delete;
@@ -393,7 +351,6 @@ my @books = $book_model->search({

too much typing! too much maintenance!

-

too much maintenance!