From: Peter Rabbitson Date: Sun, 24 May 2009 09:42:01 +0000 (+0000) Subject: Make sure we do not clobber search attributes when using subqueries X-Git-Tag: v0.08103~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=69989ea9caa5b8231451a2d715f2d40f60b338f1;p=dbsrgits%2FDBIx-Class.git Make sure we do not clobber search attributes when using subqueries --- diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index 65e1a4f..b38dbf5 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -340,8 +340,11 @@ sub _recurse_from { sub _fold_sqlbind { my ($self, $sqlbind) = @_; - my $sql = shift @$$sqlbind; - push @{$self->{from_bind}}, @$$sqlbind; + + my @sqlbind = @$$sqlbind; # copy + my $sql = shift @sqlbind; + push @{$self->{from_bind}}, @sqlbind; + return $sql; } diff --git a/t/search/subquery.t b/t/search/subquery.t index aa01183..72eaeec 100644 --- a/t/search/subquery.t +++ b/t/search/subquery.t @@ -1,13 +1,12 @@ #!/usr/bin/perl use strict; -use warnings FATAL => 'all'; +use warnings; use Data::Dumper; use Test::More; -plan ( tests => 8 ); use lib qw(t/lib); use DBICTest; @@ -17,95 +16,70 @@ my $schema = DBICTest->init_schema(); my $art_rs = $schema->resultset('Artist'); my $cdrs = $schema->resultset('CD'); -{ - my $cdrs2 = $cdrs->search({ - artist_id => { 'in' => $art_rs->search({}, { rows => 1 })->get_column( 'id' )->as_query }, - }); - - is_same_sql_bind( - $cdrs2->as_query, - "( SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE artist_id IN ( SELECT id FROM artist me LIMIT 1 ) )", - [], - ); -} +my @tests = ( + { + rs => $cdrs, + search => { + artist_id => { 'in' => $art_rs->search({}, { rows => 1 })->get_column( 'id' )->as_query }, + }, + sqlbind => \[ + "( SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE artist_id IN ( SELECT id FROM artist me LIMIT 1 ) )", + ], + }, -{ - my $rs = $art_rs->search( - {}, - { + { + rs => $art_rs, + attrs => { 'select' => [ $cdrs->search({}, { rows => 1 })->get_column('id')->as_query, ], }, - ); - - is_same_sql_bind( - $rs->as_query, - "( SELECT (SELECT id FROM cd me LIMIT 1) FROM artist me )", - [], - ); -} + sqlbind => \[ + "( SELECT (SELECT id FROM cd me LIMIT 1) FROM artist me )", + ], + }, -{ - my $rs = $art_rs->search( - {}, - { + { + rs => $art_rs, + attrs => { '+select' => [ $cdrs->search({}, { rows => 1 })->get_column('id')->as_query, ], }, - ); - - is_same_sql_bind( - $rs->as_query, - "( SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cd me LIMIT 1) FROM artist me )", - [], - ); -} + sqlbind => \[ + "( SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cd me LIMIT 1) FROM artist me )", + ], + }, -# simple from -{ - my $rs = $cdrs->search( - {}, - { + { + rs => $cdrs, + attrs => { alias => 'cd2', from => [ { cd2 => $cdrs->search({ id => { '>' => 20 } })->as_query }, ], }, - ); - - is_same_sql_bind( - $rs->as_query, - "( SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE id > ?) cd2 )", - [ + sqlbind => \[ + "( SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE id > ?) cd2 )", [ 'id', 20 ] ], - ); -} + }, -# nested from -{ - my $art_rs2 = $schema->resultset('Artist')->search({}, { - from => [ { 'me' => 'artist' }, - [ { 'cds' => $cdrs->search({},{ 'select' => [\'me.artist as cds_artist' ]})->as_query }, - { 'me.artistid' => 'cds_artist' } ] ] - }); - - is_same_sql_bind( - $art_rs2->as_query, - "( SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me JOIN (SELECT me.artist as cds_artist FROM cd me) cds ON me.artistid = cds_artist )", [] - ); - - -} + rs => $art_rs, + attrs => { + from => [ { 'me' => 'artist' }, + [ { 'cds' => $cdrs->search({},{ 'select' => [\'me.artist as cds_artist' ]})->as_query }, + { 'me.artistid' => 'cds_artist' } ] ] + }, + sqlbind => \[ + "( SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me JOIN (SELECT me.artist as cds_artist FROM cd me) cds ON me.artistid = cds_artist )" + ], + }, -# nested subquery in from -{ - my $rs = $cdrs->search( - {}, - { + { + rs => $cdrs, + attrs => { alias => 'cd2', from => [ { cd2 => $cdrs->search( @@ -118,61 +92,62 @@ my $cdrs = $schema->resultset('CD'); }, )->as_query }, ], }, - ); - - is_same_sql_bind( - $rs->as_query, - "( SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track - FROM - (SELECT cd3.cdid,cd3.artist,cd3.title,cd3.year,cd3.genreid,cd3.single_track - FROM - (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track - FROM cd me WHERE id < ?) cd3 - WHERE id > ?) cd2 - )", - [ - [ 'id', 40 ], + sqlbind => \[ + "( SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track + FROM + (SELECT cd3.cdid,cd3.artist,cd3.title,cd3.year,cd3.genreid,cd3.single_track + FROM + (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track + FROM cd me WHERE id < ?) cd3 + WHERE id > ?) cd2 + )", + [ 'id', 40 ], [ 'id', 20 ] ], - ); - -} + }, -{ - my $rs = $cdrs->search({ - year => { - '=' => $cdrs->search( - { artistid => { '=' => \'me.artistid' } }, - { alias => 'inner' } - )->get_column('year')->max_rs->as_query, + { + rs => $cdrs, + search => { + year => { + '=' => $cdrs->search( + { artistid => { '=' => \'me.artistid' } }, + { alias => 'inner' } + )->get_column('year')->max_rs->as_query, + }, }, - }); - - is_same_sql_bind( - $rs->as_query, - "( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE year = (SELECT MAX(inner.year) FROM cd inner WHERE artistid = me.artistid) )", - [], - ); -} + sqlbind => \[ + "( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE year = (SELECT MAX(inner.year) FROM cd inner WHERE artistid = me.artistid) )", + ], + }, -{ - my $rs = $cdrs->search( - {}, - { + { + rs => $cdrs, + attrs => { alias => 'cd2', from => [ { cd2 => $cdrs->search({ title => 'Thriller' })->as_query }, ], }, - ); - - is_same_sql_bind( - $rs->as_query, - "(SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE title = ?) cd2)", - [ + sqlbind => \[ + "(SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE title = ?) cd2)", [ 'title', 'Thriller' ] ], - ); + }, +); + + +plan tests => @tests * 2; + +for my $i (0 .. $#tests) { + my $t = $tests[$i]; + for my $p (1, 2) { # repeat everything twice, make sure we do not clobber search arguments + is_same_sql_bind ( + $t->{rs}->search ($t->{search}, $t->{attrs})->as_query, + $t->{sqlbind}, + sprintf 'Testcase %d, pass %d', $i+1, $p, + ); + } }