From: Nigel Metheringham Date: Thu, 6 Apr 2006 14:20:32 +0000 (+0000) Subject: Applied mst fixes for delete on resultsetin [839] to update. Factored out common... X-Git-Tag: v0.06001~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0f57d214e83ec8b31825c924f996a6e50f73499e;p=dbsrgits%2FDBIx-Class.git Applied mst fixes for delete on resultsetin [839] to update. Factored out common code --- diff --git a/Changes b/Changes index 7f51a67..bbf21f9 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for DBIx::Class 0.06001 - Added fix for quoting with single table + - Added fix for update on resultset 0.06000 - Lots of documentation improvements diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index ba310b0..08e81b5 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -783,6 +783,59 @@ sub first { return $_[0]->reset->next; } +# _cond_for_update_delete +# +# update/delete require the condition to be modified to handle +# the differing SQL syntax available. This transforms the $self->{cond} +# appropriately, returning the new condition + +sub _cond_for_update_delete { + my ($self) = @_; + my $cond = {}; + + if (!ref($self->{cond})) { + # No-op. No condition, we're update/deleting everything + } + elsif (ref $self->{cond} eq 'ARRAY') { + $cond = [ + map { + my %hash; + foreach my $key (keys %{$_}) { + $key =~ /([^.]+)$/; + $hash{$1} = $_->{$key}; + } + \%hash; + } @{$self->{cond}} + ]; + } + elsif (ref $self->{cond} eq 'HASH') { + if ((keys %{$self->{cond}})[0] eq '-and') { + $cond->{-and} = [ + map { + my %hash; + foreach my $key (keys %{$_}) { + $key =~ /([^.]+)$/; + $hash{$1} = $_->{$key}; + } + \%hash; + } @{$self->{cond}{-and}} + ]; + } + else { + foreach my $key (keys %{$self->{cond}}) { + $key =~ /([^.]+)$/; + $cond->{$1} = $self->{cond}{$key}; + } + } + } + else { + $self->throw_exception( + "Can't update/delete on resultset with condition unless hash or array"); + } + return $cond; +} + + =head2 update =over 4 @@ -803,8 +856,11 @@ sub update { my ($self, $values) = @_; $self->throw_exception("Values for update must be a hash") unless ref $values eq 'HASH'; + + my $cond = $self->_cond_for_update_delete; + return $self->result_source->storage->update( - $self->result_source->from, $values, $self->{cond} + $self->result_source->from, $values, $cond ); } @@ -853,43 +909,9 @@ sub delete { my ($self) = @_; my $del = {}; - if (!ref($self->{cond})) { - - # No-op. No condition, we're deleting everything - - } elsif (ref $self->{cond} eq 'ARRAY') { - - $del = [ map { my %hash; - foreach my $key (keys %{$_}) { - $key =~ /([^.]+)$/; - $hash{$1} = $_->{$key}; - }; \%hash; } @{$self->{cond}} ]; - - } elsif (ref $self->{cond} eq 'HASH') { - - if ((keys %{$self->{cond}})[0] eq '-and') { - - $del->{-and} = [ map { my %hash; - foreach my $key (keys %{$_}) { - $key =~ /([^.]+)$/; - $hash{$1} = $_->{$key}; - }; \%hash; } @{$self->{cond}{-and}} ]; - - } else { - - foreach my $key (keys %{$self->{cond}}) { - $key =~ /([^.]+)$/; - $del->{$1} = $self->{cond}{$key}; - } - } - - } else { - $self->throw_exception( - "Can't delete on resultset with condition unless hash or array" - ); - } + my $cond = $self->_cond_for_update_delete; - $self->result_source->storage->delete($self->result_source->from, $del); + $self->result_source->storage->delete($self->result_source->from, $cond); return 1; } diff --git a/t/run/06relationship.tl b/t/run/06relationship.tl index 9d2669e..04d1e36 100644 --- a/t/run/06relationship.tl +++ b/t/run/06relationship.tl @@ -3,7 +3,7 @@ my $schema = shift; use strict; use warnings; -plan tests => 21; +plan tests => 25; # has_a test my $cd = $schema->resultset("CD")->find(4); @@ -137,6 +137,15 @@ my $trackset = $artist->cds->search_related('tracks'); # LEFT join means we also see the trackless additional album... cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist"); +# now see about updating eveything that belongs to artist 2 to artist 3 +$artist = $schema->resultset("Artist")->find(2); +my $nartist = $schema->resultset("Artist")->find(3); +cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist"); +cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist"); +$artist->cds->update({artist => $nartist->id}); +cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist"); +cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist"); + } 1;