From: Arthur Axel 'fREW' Schmidt Date: Wed, 9 Jun 2010 06:47:38 +0000 (-0500) Subject: major additions for new orientation of presentation X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=da0b46fdf0b90e932d2ca0d7874b9e70091cb323;p=dbsrgits%2Fdbix-class-introduction-presentation.git major additions for new orientation of presentation --- diff --git a/slideshow.html b/slideshow.html index 34c2e4f..76917e9 100644 --- a/slideshow.html +++ b/slideshow.html @@ -76,7 +76,7 @@

Authors

Originally Leo Lapworth @ LPW 2009

-

Matthew S. Trout

+

Amiri Barksdale

Justin D. Hunter

Arthur Axel "fREW" Schmidt

@@ -85,17 +85,16 @@

What's up guys?

@@ -103,26 +102,61 @@
+

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

+
+ +

DBIx::Class?

-

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

+

Meta

+

These are reasons that are not technical or inherent to + the code of DBIC, but are totally awesome things about it.

+
+ +
+

Large Community

+

Currently there are 88 people listed as contributors to DBIC. That + ranges from documentation help, to test help, to added features, + to entire database support.

+
+ +
+

Active Community

+

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 ebbs and flows.

+
+ +
+

Responsive Community

+ +
+ +
+

General ORM

+

These are things that are in most other ORMs, but are still reasons + to use DBIC over raw SQL.

+
+ +
+

Cross DB

+

The vast majority of code should run on all databases without needing tweaking

Basic CRUD

-
- -
-

DBIC: Create

-
my $pratchett = $author_rs->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',
-});
-

Automaticaly fills in foreign key for you

+
@@ -204,7 +220,9 @@ my $book = $book_rs->search({ my @books = $book_rs->search({ author => $author_id, })->all; -

TMTOWTDI

+
@@ -219,15 +237,6 @@ my @books = $book_rs->search({
-

DBIC: Read

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

Search takes SQL::Abstract formatted queries

-
> perldoc SQL::Abstract

-
- -

SQL: Update

my $update = $dbh->prepare('
    UPDATE books
@@ -263,58 +272,48 @@ $delete->execute($book_id);
-

Creating models

+

OO Overidability

+ +
+

I got yelled at about this before by people, so + we don't get EVERYTHING from OO, but we do get a lot + so :-P

+
-
package Foo::Schema::Result::Author;
-__PACKAGE__->table('authors');
-__PACKAGE__->add_columns(
-  id => {
-    data_type         => 'int',
-    is_auto_increment => 1
-  },
-  title => {
-    data_type   => 'varchar',
-    is_nullable => 1,
-    size        => 255,
-  },
-);
-__PACKAGE__->set_primary_key('id');
-__PACKAGE__->has_many( books =>
-   'Foo::Schema::Result::Book', 'author_id'
-);
-1;
-
+

Convenience Methods

+
-
package Foo::Schema::Result::Book;
-use strict; use warnings;
-__PACKAGE__->table('books');
-__PACKAGE__->add_columns(
-  id => {
-    data_type         => 'int',
-    is_auto_increment => 1
-  },
-  name => {
-    data_type   => 'varchar',
-    is_nullable => 1,
-    size        => 255,
-  },
-
-
-  author_id => {
-    data_type   => 'int',
-    size        => 8,
-  },
-);
-__PACKAGE__->set_primary_key('id');
-__PACKAGE__->belongs_to( author =>
-   'Foo::Schema::Result::Author', 'author_id'
-);
-1;
-
+

Non-column methods

+

Need a method to get a user's gravatar URL? Add a + gravatar_url method to their Result class

+
+ +
+

RELATIONSHIPS

+ +
+ +
+

DBIx::Class Specific Features

+

These things may be in other ORM's, but they are very specific, so doubtful

@@ -345,25 +344,136 @@ my $schema = Foo::Schema->connect($dsn, $user, $pass);
-

Splitting Logic Cleanly

-

Foo::Schema::Result::Bar = individual row

-

Foo::Schema::ResultSet::Bar = searches / table

+

Populate

+

Made for inserting lots of rows very quicky into database

+
$schema->populate([ Users =>
+   [qw( username password )],
+   [qw( frew >=4char$  )],
+   [qw(      ...          )],
+   [qw(      ...          )],
+);
+
+
-

Using your Schema

-
#!perl
-use strict; use warnings;
-use lib 'lib';
-use Foo::Schema;
-my $schema = Foo::Schema->connect($dns, $user, $pass);
-my $author_rs = $schema->resultset('Author');
-my $author    = $author_rs->create({
-   name => 'Douglas Adams',
-});
-my $book = $author->add_to_books({
-   title => '42',
-});
+      

Multicreate

+

Create an object and all of it's related objects all at once

+
$schema->resultset('Author')->create({
+   name => 'Stephen King',
+   books => [{ title => 'The Dark Tower' }],
+   address => {
+      street => '123 Turtle Back Lane',
+      state  => { abbreviation => 'ME' },
+      city   => { name => 'Lowell' },
+   },
+
+
+
    +
  • books is a has_many
  • +
  • address is a belongs_to which in turn belongs to state and city each
  • +
  • for this to work right state and city must mark abbreviation and name as unique
  • +
+
+
+ +
+

Extensible

+

DBIx::Class helped pioneer fast MI in Perl 5 with Class::C3, so it is made + to allow extensions to nearly every part of it.

+
+ +
+

Extensible: DBIC::Helpers

+ +
+ +
+

Extensible: DBIC::TimeStamp

+ +
+ +
+

Extensible: DBIx::Class::Schema::KiokuDB

+ +
+ +
+

SQL::Abstract

+
my $resultset = $book_rs->search({
+         name => { -like => "%$nick%" },
+      });
+ +
+ +
+

Result vs ResultSet

+ +
+ +
+

ResultSet methods

+
package MyApp::Schema::ResultSet::Book;
+use base 'DBIx::Class::ResultSet';
+sub good {
+   my $self = shift;
+   $self->search({
+      rating => { '>=' => 4 },
+   })
+};
+sub cheap {
+   my $self = shift;
+   $self->search({
+      price => { '<=' => 5}
+   })
+};
+# ...
+1;
+      
+ +
+ +
+

ResultSet method in Action

+
$schema->resultset('Book')->good
+
+ +
+

ResultSet Chaining

+
$schema->resultset('Book')
+   ->good
+   ->cheap
+   ->recent