Skip Menu |

This queue is for tickets about the DBIx-Class CPAN distribution.

Report information
The Basics
Id: 61964
Status: resolved
Priority: 0/
Queue: DBIx-Class

People
Owner: Nobody in particular
Requestors: arc [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 0.08124



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
On Thu Oct 07 11:03:39 2010, ARC wrote: Show quoted text
> 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".)
Yes, there is a generic solution to this using subqueries in[1], but it needs a yak-sized refactor of the subquery re-aliasing code (so that one can subquery a resultset that fetches identically named columns from multiple tables). Then the fact whether the condition is collapsible or not becomes irrelevant, as we can just subquery the entire thing. Applied at [2]. Please let me know if you want to change your contributors entry[3] (I went by the information in the patch). Cheers! [1] http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git;a=shortlog;h=refs/heads/cleanup/rs_update_delete [2] http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git;a=commit;h=59ac6523753f5ef410732194452fe8a0c4ac99e3 [3] http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git;a=blobdiff;f=lib/DBIx/Class.pm;h=ff2dc6c759fc5cf9e021a5367e1f274577a1bd7b;hp=ba81cc4518cab7be9a5342af485a3eb61de4199b;hb=59ac6523753f5ef410732194452fe8a0c4ac99e3;hpb=f121db2ead727832c55a6d4435722612648515c8