From: Daniel Westermann-Clark Date: Wed, 12 Apr 2006 15:01:57 +0000 (+0000) Subject: Fix for -and conditions when updating or deleting on a ResultSet X-Git-Tag: v0.06002~15 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=16b4fd269ac7dc8f55ceae1b79f2ccd005f01b3d;p=dbsrgits%2FDBIx-Class.git Fix for -and conditions when updating or deleting on a ResultSet --- diff --git a/Changes b/Changes index d7e4333..d06e1bf 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for DBIx::Class +0.06002 + - fix for -and conditions when updating or deleting on a ResultSet + 0.06001 - minor fix to update in case of undefined rels - fixes for cascade delete diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 08e81b5..e0daae0 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -787,14 +787,14 @@ sub first { # # 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 +# 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 + # No-op. No condition, we're updating/deleting everything } elsif (ref $self->{cond} eq 'ARRAY') { $cond = [ @@ -805,21 +805,31 @@ sub _cond_for_update_delete { $hash{$1} = $_->{$key}; } \%hash; - } @{$self->{cond}} + } @{$self->{cond}} ]; } elsif (ref $self->{cond} eq 'HASH') { if ((keys %{$self->{cond}})[0] eq '-and') { - $cond->{-and} = [ - map { - my %hash; - foreach my $key (keys %{$_}) { + $cond->{-and} = []; + + my @cond = @{$self->{cond}{-and}}; + for (my $i = 0; $i < @cond - 1; $i++) { + my $entry = $cond[$i]; + + my %hash; + if (ref $entry eq 'HASH') { + foreach my $key (keys %{$entry}) { $key =~ /([^.]+)$/; - $hash{$1} = $_->{$key}; + $hash{$1} = $entry->{$key}; } - \%hash; - } @{$self->{cond}{-and}} - ]; + } + else { + $entry =~ /([^.]+)$/; + $hash{$entry} = $cond[++$i]; + } + + push @{$cond->{-and}}, \%hash; + } } else { foreach my $key (keys %{$self->{cond}}) { @@ -830,8 +840,10 @@ sub _cond_for_update_delete { } else { $self->throw_exception( - "Can't update/delete on resultset with condition unless hash or array"); + "Can't update/delete on resultset with condition unless hash or array" + ); } + return $cond; } diff --git a/t/run/01core.tl b/t/run/01core.tl index 9ef60a0..c0505c2 100644 --- a/t/run/01core.tl +++ b/t/run/01core.tl @@ -1,7 +1,7 @@ sub run_tests { my $schema = shift; -plan tests => 44; +plan tests => 46; # figure out if we've got a version of sqlite that is older than 3.2.6, in # which case COUNT(DISTINCT()) doesn't work @@ -181,6 +181,25 @@ cmp_ok($tag->has_column_loaded('tag'), '==', 0, 'Has not tag loaded'); ok($schema->storage(), 'Storage available'); +{ + my $rs = $schema->resultset("Artist")->search({ + -and => [ + artistid => { '>=', 1 }, + artistid => { '<', 3 } + ] + }); + + $rs->update({ name => 'Test _cond_for_update_delete' }); + + my $art; + + $art = $schema->resultset("Artist")->find(1); + is($art->name, 'Test _cond_for_update_delete', 'updated first artist name'); + + $art = $schema->resultset("Artist")->find(2); + is($art->name, 'Test _cond_for_update_delete', 'updated second artist name'); +} + #test cascade_delete thru many_many relations my $art_del = $schema->resultset("Artist")->find({ artistid => 1 }); $art_del->delete;