From: Arthur Axel 'fREW' Schmidt Date: Wed, 9 Jun 2010 05:13:32 +0000 (-0500) Subject: reorg due to new purpose X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7365a1108a4a78eba266d234210a0786e7a98c46;p=dbsrgits%2Fdbix-class-introduction-presentation.git reorg due to new purpose --- diff --git a/slideshow.html b/slideshow.html index 4213975..34c2e4f 100644 --- a/slideshow.html +++ b/slideshow.html @@ -82,9 +82,7 @@
-

assumptions

-

You know a little about Perl and using objects

-

You know a little bit about databases and using foreign keys

+

What's up guys?

  • How many people have designed a database with Foreign Keys?
  • @@ -115,66 +113,15 @@
-

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

+

Purpose

+

The purpose of this talk is to show you as many features of + DBIx::Class in 40 minutes + so that when you need to do something with it later you will + know what's possible

-

Examples Table Setup

-
    -
  • Authors
  • -
  • Books
  • -
- MySQL not recommended -
- -
-

authors table

-
CREATE TABLE authors(
-   id   int(8) primary key auto_increment,
-   name varchar(255)
-) engine = InnoDB DEFAULT CHARSET=utf8;
-
- -
-

tips

-

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

-

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

-
- -
-

authors table

-
CREATE TABLE authors(
-   id   int(8) primary key auto_increment,
-   name varchar(255)
-) engine = InnoDB DEFAULT CHARSET=utf8;
-
- -
-

books table

-
CREATE TABLE books(
-   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;
-
- - -
-

tips

-

Name link fields as singular

-

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

-
- -
-

CRUD compared

+

Basic CRUD

  • C - Create
  • R - Read
  • @@ -184,10 +131,6 @@
-

Manual (SQL)

-
- -

SQL: Create

my $sth = $dbh->prepare('
    INSERT INTO books
@@ -201,60 +144,12 @@ $sth->execute(
    
-

SQL: Read

-
my $sth = $dbh->prepare('
-   SELECT title,
-   authors.name as author_name
-   FROM books, authors
-   WHERE books.author = authors.id
-');
-
- -
-

SQL: Read

-
while( my $book = $sth->fetchrow_hashref() ) {
-  print 'Author of '
-     . $book->{title}
-     . ' is '
-     . $book->{author_name}
-     . "\n";
-}
-
- -
-

SQL: Update

-
my $update = $dbh->prepare('
-   UPDATE books
-   SET title = ?
-   WHERE id = ?
-');
-
-$update->execute(
-  'New title',$book_id
-);
-
- -
-

SQL: Delete

-
my $delete = $dbh->prepare('
-   DELETE FROM books
-   WHERE id = ?
-');
-
-$delete->execute($book_id);
-
- -
-

DBIx::Class

-
- -

DBIC: Create

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

Look ma, no SQL!

+

Don't need to work to pair placeholders and values

@@ -274,11 +169,28 @@ $delete->execute($book_id);
my $book = $pratchett->add_to_books({
    title => 'Another Discworld book',
 });
+

Automaticaly fills in foreign key for you

-

DBIC: Read

-

DBIx::Class - TIMTOWTDI

+

SQL: Read

+
my $sth = $dbh->prepare('
+   SELECT title,
+   authors.name as author_name
+   FROM books, authors
+   WHERE books.author = authors.id
+');
+
+ +
+

SQL: Read

+
while( my $book = $sth->fetchrow_hashref() ) {
+  print 'Author of '
+     . $book->{title}
+     . ' is '
+     . $book->{author_name}
+     . "\n";
+}
@@ -292,6 +204,7 @@ my $book = $book_rs->search({ my @books = $book_rs->search({ author => $author_id, })->all; +

TMTOWTDI

@@ -315,6 +228,19 @@ my @books = $book_rs->search({
+

SQL: Update

+
my $update = $dbh->prepare('
+   UPDATE books
+   SET title = ?
+   WHERE id = ?
+');
+
+$update->execute(
+  'New title',$book_id
+);
+
+ +

DBIC: Update

$book->update({
   title => 'New title',
@@ -322,6 +248,16 @@ my @books = $book_rs->search({
    
+

SQL: Delete

+
my $delete = $dbh->prepare('
+   DELETE FROM books
+   WHERE id = ?
+');
+
+$delete->execute($book_id);
+
+ +

DBIC: Delete

$book->delete;