Subject: | $rs->update fails when $rs contains "-or" with table-qualified sub-conditions |
Code of this sort fails:
$db->resultset(...)->search({
-or => [
{ 'me.col1' => $value1 },
{ 'me.col2' => $value2 },
],
})->update(...)
In particular, the table name qualifier isn't removed from the nested
conditions before the resultset is transformed into an update query, so
the DBMS complains about the qualifier.
A minimal test case and patch are attached, though I suspect there are
other related issues not yet tested. (I'm particularly thinking of
"-nest".)
Subject: | dbic_strip_or_cond_qual.diff |
From 1e19acfe59968a280f75b6a5a1e58dd84e413a28 Mon Sep 17 00:00:00 2001
From: Aaron Crane <arc@cpan.org>
Date: Thu, 7 Oct 2010 15:58:33 +0100
Subject: Fix bug in update of resultset using qualified condition in "-or"
DBIx::Class::Storage::DBIHacks::_strip_cond_qualifiers was failing to
recurse down "-or" conditions. Add minimal support for that, including a
test.
---
lib/DBIx/Class/Storage/DBIHacks.pm | 9 +++++++--
t/resultset/update_delete.t | 21 +++++++++++++++++++++
2 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/lib/DBIx/Class/Storage/DBIHacks.pm b/lib/DBIx/Class/Storage/DBIHacks.pm
index c46b0e3..d665816 100644
--- a/lib/DBIx/Class/Storage/DBIHacks.pm
+++ b/lib/DBIx/Class/Storage/DBIHacks.pm
@@ -524,8 +524,13 @@ sub _strip_cond_qualifiers {
}
else {
foreach my $key (keys %$where) {
- $key =~ /([^.]+)$/;
- $cond->{$1} = $where->{$key};
+ if ($key eq '-or' && ref $where->{$key} eq 'ARRAY') {
+ $cond->{$key} = $self->_strip_cond_qualifiers($where->{$key});
+ }
+ else {
+ $key =~ /([^.]+)$/;
+ $cond->{$1} = $where->{$key};
+ }
}
}
}
diff --git a/t/resultset/update_delete.t b/t/resultset/update_delete.t
index a016d46..539ae64 100644
--- a/t/resultset/update_delete.t
+++ b/t/resultset/update_delete.t
@@ -106,6 +106,27 @@ is_deeply (
'Only two rows incremented (where => scalarref works)',
);
+{
+ my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
+ {
+ -or => [
+ { 'me.pilot_sequence' => 12 },
+ { 'me.autopilot' => 'b' },
+ ],
+ }
+ );
+ lives_ok { $rs->update({ autopilot => 'z' }) }
+ 'Update with table name qualifier in -or conditions lives';
+ is_deeply (
+ [ $tkfks->search ({ pilot_sequence => [12, 22]})
+ ->get_column ('autopilot')->all
+ ],
+ [qw/z z/],
+ '... and yields the right data',
+ );
+}
+
+
$sub_rs->delete;
is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
--
1.7.3