From: Arthur Axel 'fREW' Schmidt Date: Sat, 12 Jun 2010 07:13:38 +0000 (-0500) Subject: 1024x768 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2Fdbix-class-introduction-presentation.git;a=commitdiff_plain;h=d1ed303d4c400d1942c1cb1ea08b3a01b3a453fe 1024x768 --- diff --git a/slideshow.html b/slideshow.html index 4ea1daf..f580d1b 100644 --- a/slideshow.html +++ b/slideshow.html @@ -324,7 +324,9 @@ $delete->execute($book_id);

->deploy

Perl -> DB

-
my $schema = Foo::Schema->connect($dsn, $user, $pass);
+
my $schema = Foo::Schema->connect(
+   $dsn, $user, $pass
+);
 $schema->deploy
 

See also: DBIx::Class::DeploymentHandler

@@ -344,7 +346,9 @@ __PACKAGE__->loader_options({ # elsewhere... -my $schema = Foo::Schema->connect($dsn, $user, $pass); +my $schema = Foo::Schema->connect( + $dsn, $user, $pass +);
@@ -353,7 +357,7 @@ my $schema = Foo::Schema->connect($dsn, $user, $pass);

Made for inserting lots of rows very quicky into database

$schema->populate([ Users =>
    [qw( username password )],
-   [qw( frew >=4char$  )],
+   [qw( frew     >=4char$ )],
    [qw(      ...          )],
    [qw(      ...          )],
 );
@@ -372,7 +376,7 @@ my $schema = Foo::Schema->connect($dsn, $user, $pass);
    address => {
       street => '123 Turtle Back Lane',
       state  => { abbreviation => 'ME' },
-      city   => { name => 'Lowell' },
+      city   => { name => 'Lowell'     },
    },
 });
 
@@ -394,13 +398,13 @@ my $schema = Foo::Schema->connect($dsn, $user, $pass);

Extensible: DBIC::Helpers

@@ -414,8 +418,9 @@ my $schema = Foo::Schema->connect($dsn, $user, $pass);
-

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

+

Extensible: Kioku

+ +
+

ResultSet method notes

@@ -485,31 +499,30 @@ sub cheap {

search_related

-
my @test_rs = ($schema->resultset('User')->search({'me.userid' => 'marial'})
+
my $score = $schema->resultset('User')
+   ->search({'me.userid' => 'frew'})
    ->related_resultset('access')
    ->related_resultset('mgmt')
-   ->related_resultset('orders')->search({'orders.reporttype' => 0})
-   ->related_resultset('shops')->search({'shops.status' => 'Completed', 'shops.datecompleted' => {-between => ['2009-10-01','2009-10-08']}})
-   ->related_resultset('rpt_score')->get_column('raw_scores')->first);
+   ->related_resultset('orders')
+   ->telephone
+   ->search_related( shops => {
+      'shops.datecompleted' => {
+         -between => ['2009-10-01','2009-10-08']
+      }
+   })->completed
+   ->related_resultset('rpt_score')
+   ->get_column('raw_scores')
+   ->first;
 
-

related_resultset

-
my @screens = $user->roles
-   ->related_resultset('role_permissions')
-   ->related_resultset('permission')
-   ->related_resultset('permission_screens')
-   ->related_resultset('screen')
-   ->search(undef, { distinct => 1 })->all;
-
- -

bonus rel methods

my $book = $author->create_related(
-  books => {
-   title => 'Another Discworld book',
-});
+   books => {
+      title => 'Another Discworld book',
+   }
+);
 
 my $book2 = $pratchett->add_to_books({
    title => 'MOAR Discworld book',
@@ -539,16 +552,20 @@ $schema->txn_commit;
       

InflateColumn

package Foo::Schema::Result::Book;
 use base 'DBIx::Class::Core';
+use DateTime::Format::MySQL;
 # Result code here
 __PACKAGE__->load_components('InflateColumn');
-use DateTime::Format::MySQL;
 __PACKAGE__->inflate_column(
    date_published => {
-      inflate => sub { DateTime::Format::MySQL->parse_date(shift) },
-      deflate => sub { shift->ymd},
+      inflate => sub {
+         DateTime::Format::MySQL->parse_date(
+            shift
+         )
+      },
+      deflate => sub { shift->ymd },
    },
 );
-# Automatic see: DBIx::Class::InflateColumn::DateTime
+# Automatic see: DBIC::InflateColumn::DateTime
@@ -559,8 +576,7 @@ $book->update;

InflateColumn: inflation

-
my $date_published = $book->date_published;
-print $date_published->month_abbr;
+
say $book->date_published->month_abbr;
Nov
@@ -581,12 +597,12 @@ __PACKAGE__->filter_column( sub to_metric { $_[1] * .305 } sub to_imperial { $_[1] * 3.28 } -# Automatic see: DBIx::Class::InflateColumn::DateTime

ResultSetColumn

-
my $rsc = $schema->resultset('Book')->get_column('price');
+
my $rsc = $schema->resultset('Book')
+   ->get_column('price');
 $rsc->min;
 $rsc->max;
 $rsc->sum;
@@ -596,8 +612,15 @@ $rsc->sum;
    

Aggregates

my @res = $rs->search({}, {
-   select   => [qw(price genre), { max => price }, { avg => price }],
-   as       => [qw(price genre max_price avg_price)],
+   select   => [
+      'price',
+      'genre',
+      { max => price },
+      { avg => price },
+   ],
+   as       => [
+      qw(price genre max_price avg_price)
+   ],
    group_by => [qw(price genre)],
 });
 for (@res) {
@@ -605,6 +628,10 @@ for (@res) {
    say $_->get_column('max_price');
    say $_->get_column('min_price');
 }
+
+ +
+

Aggregates Notes

  • Careful, get_column can basicaly mean THREE things
  • private for get what you should use an accessor for
  • @@ -616,7 +643,8 @@ for (@res) {

    HRI

    $rs->search({}, {
    -   result_class => 'DBIx::Class::ResultClass::HashRefInflator',
    +  result_class =>
    +    'DBIx::Class::ResultClass::HashRefInflator',
     });
    • Easy on memory
    • @@ -628,19 +656,21 @@ for (@res) {

      Subquery Support

      -
        my $inside_rs = $schema->resultset('Artist')->search({
      +
      my $inside_query = $schema->resultset('Artist')
      +   ->search({
           name => [ 'Billy Joel', 'Brittany Spears' ],
      -});
      +})->get_column('id')->as_query;
       
       my $rs = $schema->resultset('CD')->search({
      -    artist_id => { -in => $inside_rs->get_column('id')->as_query },
      +    artist_id => { -in => $inside_query },
       });

      Bare SQL w/ Placeholders

      $rs->update({
      -   price => \"price + $inc", # !!! SQL INJECTION VECTOR
      +   # !!! SQL INJECTION VECTOR
      +   price => \"price + $inc",
       });
       
       $rs->update({