From: Michael Leuchtenburg Date: Mon, 21 Aug 2006 15:33:04 +0000 (+0000) Subject: Change _cond_for_update_delete to handle more complicated queries through recursing... X-Git-Tag: v0.07002~30 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=057d4713af8f97b17a351bca5d11fe55e5a8b88a;p=dbsrgits%2FDBIx-Class.git Change _cond_for_update_delete to handle more complicated queries through recursing on internal hashes. Add a test which should succeed and fails without this change. --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 663f7ad..a508ce9 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -997,13 +997,14 @@ sub first { # appropriately, returning the new condition. sub _cond_for_update_delete { - my ($self) = @_; + my ($self, $full_cond) = @_; my $cond = {}; + $full_cond ||= $self->{cond}; # No-op. No condition, we're updating/deleting everything - return $cond unless ref $self->{cond}; + return $cond unless ref $full_cond; - if (ref $self->{cond} eq 'ARRAY') { + if (ref $full_cond eq 'ARRAY') { $cond = [ map { my %hash; @@ -1012,36 +1013,33 @@ sub _cond_for_update_delete { $hash{$1} = $_->{$key}; } \%hash; - } @{$self->{cond}} + } @{$full_cond} ]; } - elsif (ref $self->{cond} eq 'HASH') { - if ((keys %{$self->{cond}})[0] eq '-and') { + elsif (ref $full_cond eq 'HASH') { + if ((keys %{$full_cond})[0] eq '-and') { $cond->{-and} = []; - my @cond = @{$self->{cond}{-and}}; + my @cond = @{$full_cond->{-and}}; for (my $i = 0; $i < @cond; $i++) { my $entry = $cond[$i]; - my %hash; + my $hash; if (ref $entry eq 'HASH') { - foreach my $key (keys %{$entry}) { - $key =~ /([^.]+)$/; - $hash{$1} = $entry->{$key}; - } + $hash = $self->_cond_for_update_delete($entry); } else { $entry =~ /([^.]+)$/; - $hash{$1} = $cond[++$i]; + $hash->{$1} = $cond[++$i]; } - push @{$cond->{-and}}, \%hash; + push @{$cond->{-and}}, $hash; } } else { - foreach my $key (keys %{$self->{cond}}) { + foreach my $key (keys %{$full_cond}) { $key =~ /([^.]+)$/; - $cond->{$1} = $self->{cond}{$key}; + $cond->{$1} = $full_cond->{$key}; } } } diff --git a/t/46where_attribute.t b/t/46where_attribute.t index 764d7cc..e17b518 100644 --- a/t/46where_attribute.t +++ b/t/46where_attribute.t @@ -7,7 +7,7 @@ use lib qw(t/lib); use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 14; +plan tests => 16; # select from a class with resultset_attributes my $resultset = $schema->resultset('BooksInLibrary'); @@ -25,6 +25,12 @@ if ($@) { print $@ } ok(!$@, 'find_or_create on resultset with attribute for non-existent entry did not throw'); ok(defined $see_spot, 'successfully did insert on resultset with attribute for non-existent entry'); +my $see_spot_rs = $owner->books->search({ title => "See Spot Run" }); +eval { $see_spot_rs->delete(); }; +if ($@) { print $@ } +ok(!$@, 'delete on resultset with attribute did not throw'); +is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded'); + # many_to_many tests my $collection = $schema->resultset('Collection')->search({collectionid => 1}); my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});