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
#
# 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 = [
$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}}) {
}
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;
}
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
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;