From: Ash Berlin Date: Wed, 6 Dec 2006 23:05:30 +0000 (+0000) Subject: Join condition patch from Bernhard Graf X-Git-Tag: v0.08010~209 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=635b9634263f99a0b88a7d0db4998fcc01fcad45;p=dbsrgits%2FDBIx-Class.git Join condition patch from Bernhard Graf --- diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index cb61070..b5caa90 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -232,9 +232,18 @@ sub _join_condition { if (ref $cond eq 'HASH') { my %j; for (keys %$cond) { - my $x = '= '.$self->_quote($cond->{$_}); $j{$_} = \$x; + my $v = $cond->{$_}; + if (ref $v) { + # XXX no throw_exception() in this package and croak() fails with strange results + Carp::croak(ref($v) . qq{ reference arguments are not supported in JOINS - try using \"..." instead'}) + if ref($v) ne 'SCALAR'; + $j{$_} = $v; + } + else { + my $x = '= '.$self->_quote($v); $j{$_} = \$x; + } }; - return $self->_recurse_where(\%j); + return scalar($self->_recurse_where(\%j)); } elsif (ref $cond eq 'ARRAY') { return join(' OR ', map { $self->_join_condition($_) } @$cond); } else { diff --git a/t/76joins.t b/t/76joins.t index 9401502..806185c 100644 --- a/t/76joins.t +++ b/t/76joins.t @@ -16,7 +16,7 @@ BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 50 ); + : ( tests => 52 ); } # figure out if we've got a version of sqlite that is older than 3.2.6, in @@ -89,6 +89,26 @@ $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON (' ; is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok'); +my @j5 = ( + { child => 'person' }, + [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ], + [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], +); +$match = 'person child JOIN person father ON ( father.person_id != ' + . 'child.father_id ) JOIN person mother ON ( mother.person_id ' + . '= child.mother_id )' + ; +is( $sa->_recurse_from(@j5), $match, 'join 5 (SCALAR reference for ON statement) ok' ); + +my @j6 = ( + { child => 'person' }, + [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ], + [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], +); +$match = qr/^\QHASH reference arguments are not supported in JOINS - try using \"..." instead\E/; +eval { $sa->_recurse_from(@j6) }; +like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' ); + my $rs = $schema->resultset("CD")->search( { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, { from => [ { 'me' => 'cd' },