X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fsearch%2Fsubquery.t;h=2d96605db0241b14b292c93e2e829f04a2f847d6;hb=639cf8f9d212c3e950ae5bae680133762c1df0aa;hp=bb11a396b10004c90bdd425673ae20701da2e2a3;hpb=18c72236adec093c680be1c00df7297c42b907eb;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/t/search/subquery.t b/t/search/subquery.t index bb11a39..2d96605 100644 --- a/t/search/subquery.t +++ b/t/search/subquery.t @@ -6,12 +6,13 @@ use warnings FATAL => 'all'; use Data::Dumper; use Test::More; + +plan ( tests => 8 ); + use lib qw(t/lib); use DBICTest; use DBIC::SqlMakerTest; -plan tests => 4; - my $schema = DBICTest->init_schema(); my $art_rs = $schema->resultset('Artist'); my $cdrs = $schema->resultset('CD'); @@ -25,21 +26,36 @@ my $cdrs = $schema->resultset('CD'); my ($query, @bind) = @{$$arr}; is_same_sql_bind( $query, \@bind, - "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 )", + "( 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 ) )", [], ); } -TODO: { -# local $TODO = "'+select' doesn't work with as_query yet."; +{ my $rs = $art_rs->search( {}, { - '+select' => [ + 'select' => [ $cdrs->search({}, { rows => 1 })->get_column('id')->as_query, ], - '+as' => [ - 'cdid', + }, + ); + + my $arr = $rs->as_query; + my ($query, @bind) = @{$$arr}; + is_same_sql_bind( + $query, \@bind, + "( SELECT (SELECT id FROM cd me LIMIT 1) FROM artist me )", + [], + ); +} + +{ + my $rs = $art_rs->search( + {}, + { + '+select' => [ + $cdrs->search({}, { rows => 1 })->get_column('id')->as_query, ], }, ); @@ -48,13 +64,13 @@ TODO: { my ($query, @bind) = @{$$arr}; is_same_sql_bind( $query, \@bind, - "SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cds LIMIT 1) AS cdid FROM artist me", + "( SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cd me LIMIT 1) FROM artist me )", [], ); } -TODO: { -# local $TODO = "'from' doesn't work with as_query yet."; +# simple from +{ my $rs = $cdrs->search( {}, { @@ -69,13 +85,72 @@ TODO: { my ($query, @bind) = @{$$arr}; is_same_sql_bind( $query, \@bind, - "SELECT me.artistid, me.name, me.rank, me.charfield FROM (SELECT me.artistid, me.name, me.rank, me.charfield FROM cds me WHERE id > 20) cd2", - [], + "( 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' } ] ] + }); + + my $arr = $art_rs2->as_query; + my ($query, @bind) = @{$$arr}; + is_same_sql_bind( + $query, \@bind, + "( 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 )", [] ); + + } -TODO: { -# local $TODO = "The subquery isn't being wrapped in parens for some reason."; +# nested subquery in from +{ + my $rs = $cdrs->search( + {}, + { + alias => 'cd2', + from => [ + { cd2 => $cdrs->search( + { id => { '>' => 20 } }, + { + alias => 'cd3', + from => [ + { cd3 => $cdrs->search( { id => { '<' => 40 } } )->as_query } + ], + }, )->as_query }, + ], + }, + ); + + my $arr = $rs->as_query; + my ($query, @bind) = @{$$arr}; + is_same_sql_bind( + $query, \@bind, + "( 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( @@ -88,11 +163,33 @@ TODO: { my ($query, @bind) = @{$$arr}; is_same_sql_bind( $query, \@bind, - "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)", + "( 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( undef, { alias => 'foo' } ); -warn ${$rs->as_query}->[0], $/; +{ + my $rs = $cdrs->search( + {}, + { + alias => 'cd2', + from => [ + { cd2 => $cdrs->search({ title => 'Thriller' })->as_query }, + ], + }, + ); + + my $arr = $rs->as_query; + my ($query, @bind) = @{$$arr}; + is_same_sql_bind( + $query, \@bind, + "(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' + ] + ], + ); +} + __END__