X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=slideshow.html;h=485e759559ff4eb4b23ac3b7ee6559d7874927c2;hb=refs%2Fheads%2Fmaster;hp=f580d1beba1d9d71b6dd579d7d986af1637db48c;hpb=d1ed303d4c400d1942c1cb1ea08b3a01b3a453fe;p=dbsrgits%2Fdbix-class-introduction-presentation.git diff --git a/slideshow.html b/slideshow.html index f580d1b..485e759 100644 --- a/slideshow.html +++ b/slideshow.html @@ -74,6 +74,15 @@
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
+my $book = $book_rs->create({ +my $book = $book_rs->create({ title => 'A book title', author_id => $author_id, });
while( my $book = $sth->fetchrow_hashref() ) { +while( my $book = $sth->fetchrow_hashref() ) { print 'Author of ' . $book->{title} . ' is ' @@ -213,29 +219,27 @@ $sth->execute(+})->all; -DBIC: Read
-my $book = $book_rs->find($book_id); +my $book = $book_rs->find($book_id); -my $book = $book_rs->search({ +my $book = $book_rs->search({ title => 'A book title', -}, { rows => 1 })->next; +}, { rows => 1 })->next; my @books = $book_rs->search({ author => $author_id, -})->all;--
-- TMTOWTDI
--DBIC: Read
-while( my $book = $books_rs->next ) { +while( my $book = $books_rs->next ) { print 'Author of ' . $book->title . ' is ' . $book->author->name . "\n"; -}+} + ++
- TMTOWTDI
+@@ -253,11 +257,11 @@ $update->execute(@@ -273,7 +277,34 @@ $delete->execute($book_id); + +DBIC: Update
-$book->update({ +$book->update({ title => 'New title', });-
- Lazy Update
+- Won't update unless value changes
++ +SQL: Search
+my $sth = $dbh->prepare(' + SELECT title, + authors.name as author_name + FROM books + WHERE books.name LIKE "%monte cristo%" AND + books.topic = "jailbreak" +'); +++DBIC: Search
++my $book = $book_rs->search({ + 'me.name' => { -like => '%monte cristo%' }, + 'me.topic' => 'jailbreak', +})->next; +++
- SQL::Abstract
+- (kinda) introspectible
+- Prettier than SQL
+@@ -293,8 +324,8 @@ $delete->execute($book_id);@@ -337,8 +368,8 @@ $schema->deploy@@ -327,7 +358,7 @@ $delete->execute($book_id);Convenience Methods
-
- find_or_create
-- create_or_update
+- find_or_create
+- update_or_create
my $schema = Foo::Schema->connect( $dsn, $user, $pass ); -$schema->deploy +$schema->deploySee also: DBIx::Class::DeploymentHandler
DB -> Perl
package Foo::Schema; use strict; use warnings; -use base 'DBIx::Class::Schema::Loader'; -__PACKAGE__->loader_options({ +use base 'DBIx::Class::Schema::Loader'; +__PACKAGE__->loader_options({ naming => 'v7', debug => $ENV{DBIC_TRACE}, }); @@ -355,7 +386,7 @@ my $schema = Foo::Schema->connect(Populate
Made for inserting lots of rows very quicky into database
-$schema->populate([ Users => +$schema->populate([ Users => [qw( username password )], [qw( frew >=4char$ )], [qw( ... )], @@ -370,7 +401,7 @@ my $schema = Foo::Schema->connect(Multicreate
Create an object and all of it's related objects all at once
-$schema->resultset('Author')->create({ +$schema->resultset('Author')->create({ name => 'Stephen King', books => [{ title => 'The Dark Tower' }], address => { @@ -398,18 +429,18 @@ my $schema = Foo::Schema->connect(Extensible: DBIC::Helpers
-
- 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
+- 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
-Extensible: DBIC::TimeStamp
+Extensible: DBIC::TimeStamp
- Cross DB
- set_on_create
@@ -420,7 +451,7 @@ my $schema = Foo::Schema->connect(Extensible: Kioku
-
- DBIx::Class::Schema::KiokuDB
+- DBIx::Class::Schema::KiokuDB
- Kioku is the new hotness
- Mix RDBMS with Object DB
- beta ( == sexy )
@@ -428,17 +459,6 @@ my $schema = Foo::Schema->connect(-- -SQL::Abstract
-my $resultset = $book_rs->search({ - name => { -like => "%$nick%" }, -});--
-- (kinda) introspectible
-- Prettier than SQL
-Result vs ResultSet
- Result == Row
@@ -452,7 +472,7 @@ my $schema = Foo::Schema->connect(-@@ -501,30 +521,30 @@ sub cheap {ResultSet methods
+ResultSet methods
package MyApp::Schema::ResultSet::Book; use base 'DBIx::Class::ResultSet'; sub good { @@ -479,7 +499,7 @@ sub cheap {
- All searches should be ResultSet methods
- Name has obvious meaning
-- current_source_alias helps things to work no matter what
+- current_source_alias helps things to work no matter what
search_related
my $score = $schema->resultset('User') ->search({'me.userid' => 'frew'}) - ->related_resultset('access') + ->related_resultset('access') ->related_resultset('mgmt') ->related_resultset('orders') ->telephone - ->search_related( shops => { + ->search_related( shops => { 'shops.datecompleted' => { -between => ['2009-10-01','2009-10-08'] } })->completed ->related_resultset('rpt_score') - ->get_column('raw_scores') + ->get_column('raw_scores') ->first;bonus rel methods
-my $book = $author->create_related( +my $book = $author->create_related( books => { title => 'Another Discworld book', } ); -my $book2 = $pratchett->add_to_books({ +my $book2 = $pratchett->add_to_books({ title => 'MOAR Discworld book', });@@ -534,17 +554,17 @@ my $book2 = $pratchett->add_to_books({
@@ -554,8 +574,8 @@ $schema->txn_commit; use base 'DBIx::Class::Core'; use DateTime::Format::MySQL; # Result code here -__PACKAGE__->load_components('InflateColumn'); -__PACKAGE__->inflate_column( +__PACKAGE__->load_components('InflateColumn'); +__PACKAGE__->inflate_column( date_published => { inflate => sub { DateTime::Format::MySQL->parse_date( @@ -586,9 +606,9 @@ $book->update;Excellent Transaction Support
-$schema->txn_do(sub { +$schema->txn_do(sub { ... }); -my $guard = $schema->txn_scope_guard; +my $guard = $schema->txn_scope_guard; # ... $guard->commit; -$schema->txn_begin; # <-- low level +$schema->txn_begin; # <-- low level # ... -$schema->txn_commit; +$schema->txn_commit;package Foo::Schema::Result::Book; use base 'DBIx::Class::Core'; # Result code here -__PACKAGE__->load_components('FilterColumn'); +__PACKAGE__->load_components('FilterColumn'); -__PACKAGE__->filter_column( +__PACKAGE__->filter_column( length => { to_storage => 'to_metric', from_storage => 'to_imperial', @@ -600,9 +620,11 @@ sub to_imperial { $_[1] * 3.28 }-ResultSetColumn
+ResultSetColumn
my $rsc = $schema->resultset('Book') ->get_column('price'); +$rsc->first; +$rsc->all; $rsc->min; $rsc->max; $rsc->sum; @@ -612,16 +634,16 @@ $rsc->sum;Aggregates
my @res = $rs->search({}, { - select => [ + select => [ 'price', 'genre', { max => price }, { avg => price }, ], - as => [ + as => [ qw(price genre max_price avg_price) ], - group_by => [qw(price genre)], + group_by => [qw(price genre)], }); for (@res) { say $_->price . ' ' . $_->genre; @@ -643,8 +665,8 @@ for (@res) {HRI
$rs->search({}, { - result_class => - 'DBIx::Class::ResultClass::HashRefInflator', + result_class => + 'DBIx::Class::ResultClass::HashRefInflator', });
- Easy on memory
@@ -655,7 +677,7 @@ for (@res) {-Subquery Support
+Subquery Support
my $inside_query = $schema->resultset('Artist') ->search({ name => [ 'Billy Joel', 'Brittany Spears' ], @@ -667,7 +689,7 @@ my $rs = $schema->resultset('CD')->search({-Bare SQL w/ Placeholders
+Bare SQL w/ Placeholders
$rs->update({ # !!! SQL INJECTION VECTOR price => \"price + $inc",