X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=slideshow.html;fp=slideshow.html;h=f580d1beba1d9d71b6dd579d7d986af1637db48c;hb=d1ed303d4c400d1942c1cb1ea08b3a01b3a453fe;hp=4ea1daf2b47d4bd4ca749f22b3785e3eef11cd6d;hpb=3ab0ef72df802ee2746a733be73a1e4c407f49b5;p=dbsrgits%2Fdbix-class-introduction-presentation.git
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
- - DBIx::Class::Helper::ResultSet::IgnoreWantarray
- - DBIx::Class::Helper::ResultSet::Random
- - DBIx::Class::Helper::ResultSet::SetOperations
- - DBIx::Class::Helper::Row::JoinTable
- - DBIx::Class::Helper::Row::NumifyGet
- - DBIx::Class::Helper::Row::SubClass
- - DBIx::Class::Helper::Row::ToJSON
+ - DBIC::Helper::ResultSet::IgnoreWantarray
+ - DBIC::Helper::ResultSet::Random
+ - DBIC::Helper::ResultSet::SetOperations
+ - DBIC::Helper::Row::JoinTable
+ - DBIC::Helper::Row::NumifyGet
+ - DBIC::Helper::Row::SubClass
+ - DBIC::Helper::Row::ToJSON
@@ -414,8 +418,9 @@ my $schema = Foo::Schema->connect($dsn, $user, $pass);
-
Extensible: DBIx::Class::Schema::KiokuDB
+
Extensible: Kioku
+ - DBIx::Class::Schema::KiokuDB
- Kioku is the new hotness
- Mix RDBMS with Object DB
- beta ( == sexy )
@@ -451,21 +456,30 @@ my $schema = Foo::Schema->connect($dsn, $user, $pass);
package MyApp::Schema::ResultSet::Book;
use base 'DBIx::Class::ResultSet';
sub good {
- $_[0]->search({
- rating => { '>=' => 4 },
+ my $self = shift;
+ $self->search({
+ $self->current_source_alias .
+ '.rating' => { '>=' => 4 },
})
};
sub cheap {
- $_[0]->search({
- price => { '<=' => 5}
+ my $self = shift;
+ $self->search({
+ $self->current_source_alias .
+ '.price' => { '<=' => 5}
})
};
# ...
1;
+
+
+
+
ResultSet method notes
- All searches should be ResultSet methods
- Name has obvious meaning
+ - current_source_alias helps things to work no matter what
@@ -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({