From: Matt S Trout Date: Sun, 10 Jan 2010 02:46:03 +0000 (+0000) Subject: add delete code X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c51eabc5e365e02962c64af5f1fe21dd342a53bc;p=dbsrgits%2FDBIx-Data-Store-old.git add delete code --- diff --git a/lib/DBIx/Data/Collection/Set.pm b/lib/DBIx/Data/Collection/Set.pm index f934ec6..4d90294 100644 --- a/lib/DBIx/Data/Collection/Set.pm +++ b/lib/DBIx/Data/Collection/Set.pm @@ -34,11 +34,17 @@ method _build__member_cache { } method _add_to_member_cache ($to_add) { - return unless $self->_member_cache_built; + return $to_add unless $self->_member_cache_built; push @{$self->_member_cache}, $to_add; $to_add } +method _remove_from_member_cache ($to_remove) { + return $to_remove unless $self->_member_cache_built; + @{$self->_member_cache} = grep $_ ne $to_remove, @{$self->_member_cache}; + $to_remove +} + ## key cache - by primary/unique key has _key_cache => (is => 'ro', default => sub { {} }); @@ -48,6 +54,11 @@ method _add_to_key_cache ($to_add) { $to_add } +method _remove_from_key_cache ($to_remove) { + # should return $to_remove + delete $self->_key_cache->{$self->_object_to_id($to_remove)} +} + method _key_cache_has_raw ($raw) { exists $self->_key_cache->{$self->_raw_to_id($raw)} } @@ -70,7 +81,7 @@ method _key_cache_get_object ($obj) { ## loading data method _new_raw_stream { - $self->_store->new_select_command([])->execute; + $self->_store->new_select_command([])->execute } ## thunking between the store representation and the set representation @@ -82,7 +93,7 @@ method _new_raw_stream { method _inflate ($raw) { bless($raw, $self->_class) if $self->_has_class; - $raw; + $raw } method _deflate ($obj) { @@ -91,7 +102,7 @@ method _deflate ($obj) { method _merge ($obj, $raw) { @{$obj}{keys %$raw} = values %$raw; - $obj; + $obj } ## methods to get ids @@ -102,9 +113,11 @@ method _raw_to_id ($raw) { } method _object_to_id ($obj) { - $self->_raw_to_id($self->_deflate($obj)); + $self->_raw_to_id($self->_deflate($obj)) } +## array-ish operations - i.e. get all members + method flatten { @{$self->_member_cache}; } @@ -113,16 +126,18 @@ method as_stream { Data::Perl::Stream::Array->new(array => $self->_member_cache); } +## add to set + method add ($new) { $self->_add_to_store($new); $self->_add_to_caches($new); - $new; + $new } method _add_to_store ($new) { my $new_raw = $self->_deflate($new); $self->_merge($new, $self->_store->new_insert_command($new_raw)->execute); - $new; + $new } method _add_to_caches ($new) { @@ -131,4 +146,22 @@ method _add_to_caches ($new) { $new } +## remove from set + +method remove ($old) { + $self->_remove_from_store($old); + $self->_remove_from_caches($old); + $old +} + +method _remove_from_store ($old) { + $self->_store->new_delete_command($self->_deflate($old))->execute; +} + +method _remove_from_caches ($old) { + $self->_remove_from_member_cache($old); + $self->_remove_from_key_cache($old); + $old +} + 1; diff --git a/t/01basic_collection.t b/t/01basic_collection.t index 548ff89..65e55ce 100644 --- a/t/01basic_collection.t +++ b/t/01basic_collection.t @@ -80,7 +80,9 @@ $set = make_set {}, { raw_store => $self->raw_store, insert_call_command => $self->raw_store->new_call_command(@_) ); - } + }, + delete_sql => q{DELETE FROM person WHERE id = ?}, + delete_argument_order => [ 'id' ], }; my $doug = $set->add({ name => 'Doug' }); @@ -93,4 +95,12 @@ ok($set_doug, 'new row exists in flatten'); cmp_ok(refaddr($doug), '==', refaddr($set_doug), 'Same hashref returned'); +$set->remove($doug); + +is_deeply([ sort_set $set->flatten ], \@expect, 'new row gone after remove'); + +$set = make_set; + +is_deeply([ sort_set $set->flatten ], \@expect, 'new row still gone on reload'); + done_testing;