From: Matt S Trout Date: Sat, 9 Jan 2010 21:54:10 +0000 (+0000) Subject: cleanup last insert id handling X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=de9534fa36a965cd35e5b212d093dee64e78e8b9;p=dbsrgits%2FDBIx-Data-Store-old.git cleanup last insert id handling --- diff --git a/lib/DBIx/Data/Collection/Set.pm b/lib/DBIx/Data/Collection/Set.pm index 1c812fc..f934ec6 100644 --- a/lib/DBIx/Data/Collection/Set.pm +++ b/lib/DBIx/Data/Collection/Set.pm @@ -25,17 +25,18 @@ method _build__member_cache { if (my ($obj) = $self->_key_cache_get_raw($raw)) { $self->_merge($obj, $raw) } else { - $self->_inflate($raw) + $self->_add_to_key_cache($self->_inflate($raw)) } }; push @cache, $obj; } - \@cache; + \@cache } method _add_to_member_cache ($to_add) { return unless $self->_member_cache_built; push @{$self->_member_cache}, $to_add; + $to_add } ## key cache - by primary/unique key @@ -44,7 +45,7 @@ has _key_cache => (is => 'ro', default => sub { {} }); method _add_to_key_cache ($to_add) { $self->_key_cache->{$self->_object_to_id($to_add)} = $to_add; - return + $to_add } method _key_cache_has_raw ($raw) { @@ -121,13 +122,13 @@ method add ($new) { method _add_to_store ($new) { my $new_raw = $self->_deflate($new); $self->_merge($new, $self->_store->new_insert_command($new_raw)->execute); - $self->_add_to_caches($new); - return + $new; } method _add_to_caches ($new) { $self->_add_to_member_cache($new); $self->_add_to_key_cache($new); + $new } 1; diff --git a/lib/DBIx/Data/Store.pm b/lib/DBIx/Data/Store.pm index 2713c2c..b956b85 100644 --- a/lib/DBIx/Data/Store.pm +++ b/lib/DBIx/Data/Store.pm @@ -6,6 +6,7 @@ use DBIx::Connector; use DBIx::Data::Store::Command::Call; use DBIx::Data::Store::Command::Row; use DBIx::Data::Store::Command::Stream; +use DBIx::Data::Store::Command::Closure; has 'connection' => (is => 'ro', lazy_build => 1); # , isa => 'DBIx::Connector' @@ -41,6 +42,12 @@ method new_stream_command ($sql, $args, $column_order) { ); } +method new_closure_command ($code) { + return DBIx::Data::Store::Command::Closure->new( + run => $code, against => $self->connection + ); +} + __PACKAGE__->meta->make_immutable; 1; diff --git a/lib/DBIx/Data/Store/Command/Closure.pm b/lib/DBIx/Data/Store/Command/Closure.pm new file mode 100644 index 0000000..1ecd326 --- /dev/null +++ b/lib/DBIx/Data/Store/Command/Closure.pm @@ -0,0 +1,14 @@ +package DBIx::Data::Store::Command::Closure; + +use Moose; +use Method::Signatures::Simple; + +has [ 'run', 'against' ] => (is => 'ro', required => 1); + +method execute { + $self->against->run($self->run); +} + +__PACKAGE__->meta->make_immutable; + +1; diff --git a/lib/DBIx/Data/Store/Command/Insert/LastInsertId.pm b/lib/DBIx/Data/Store/Command/Insert/LastInsertId.pm index eb9d904..03a8c36 100644 --- a/lib/DBIx/Data/Store/Command/Insert/LastInsertId.pm +++ b/lib/DBIx/Data/Store/Command/Insert/LastInsertId.pm @@ -7,21 +7,21 @@ has 'id_column' => (is => 'ro', required => 1); has 'insert_call_command' => (is => 'ro', required => 1); +has '_last_insert_id_command' => (is => 'ro', lazy_build => 1); + has 'raw_store' => (is => 'ro', required => 1); +method _build__last_insert_id_command { + $self->raw_store->new_closure_command(sub { + +{ $self->id_column => shift->last_insert_id((undef) x 4) } + }); +} + method execute { $self->insert_call_command->execute; - return { $self->id_column => $self->_get_last_insert_id } + return $self->_last_insert_id_command->execute; }; -method _get_last_insert_id { - # this should probably involve some sort of call to the raw - # store to get the command object from it, but meh - $self->raw_store->connection->run(sub { - shift->last_insert_id((undef) x 4) - }); -} - __PACKAGE__->meta->make_immutable; 1; diff --git a/t/01basic_collection.t b/t/01basic_collection.t index 0c14444..548ff89 100644 --- a/t/01basic_collection.t +++ b/t/01basic_collection.t @@ -3,6 +3,9 @@ use DBIx::Data::Store; use DBIx::Data::Store::CRUD; use DBIx::Data::Collection::Set; use DBI; +use Scalar::Util qw(refaddr); + +use Devel::Dwarn; use strict; use warnings FATAL => 'all'; @@ -82,8 +85,12 @@ $set = make_set {}, { my $doug = $set->add({ name => 'Doug' }); -use Devel::Dwarn; +ok($doug->{id}, 'id filled out in new row'); + +my ($set_doug) = grep $_->{name} eq 'Doug', $set->flatten; + +ok($set_doug, 'new row exists in flatten'); -Dwarn $doug; +cmp_ok(refaddr($doug), '==', refaddr($set_doug), 'Same hashref returned'); done_testing;