X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fsqlmaker%2Flimit_dialects%2Fgeneric_subq.t;h=5ed89c0ff116efd8d41e5fcdd3012628ea7ce781;hb=7d5810cd5d389cee98fecaf5d27ac7ed2f27dce4;hp=9e771a95d35dd92d1ca8ce26e5c24a8e591687cd;hpb=d763268755f6b9ee1d0d4d8442d989ef7c5a4d7d;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/t/sqlmaker/limit_dialects/generic_subq.t b/t/sqlmaker/limit_dialects/generic_subq.t index 9e771a9..5ed89c0 100644 --- a/t/sqlmaker/limit_dialects/generic_subq.t +++ b/t/sqlmaker/limit_dialects/generic_subq.t @@ -5,6 +5,13 @@ use Test::More; use lib qw(t/lib); use DBICTest; use DBIC::SqlMakerTest; +use DBIx::Class::SQLMaker::LimitDialects; +my ($ROWS, $TOTAL, $OFFSET) = ( + DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype, + DBIx::Class::SQLMaker::LimitDialects->__total_bindtype, + DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype, +); + my $schema = DBICTest->init_schema; @@ -20,7 +27,7 @@ my $rs = $schema->resultset ('BooksInLibrary')->search ({}, { is_same_sql_bind( $rs->as_query, '( - SELECT id, source, owner, title, price, + SELECT me.id, me.source, me.owner, me.title, me.price, owner_name FROM ( SELECT me.id, me.source, me.owner, me.title, me.price, @@ -34,10 +41,13 @@ is_same_sql_bind( SELECT COUNT(*) FROM books rownum__emulation WHERE rownum__emulation.title < me.title - ) < 2 + ) < ? ORDER BY me.title )', - [ [ 'source', 'Library' ] ], + [ + [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ], + [ $ROWS => 2 ], + ], ); is_deeply ( @@ -61,7 +71,7 @@ $rs = $schema->resultset ('BooksInLibrary')->search ({}, { is_same_sql_bind( $rs->as_query, '( - SELECT "id", "source", "owner", "title", "price", + SELECT "me"."id", "me"."source", "me"."owner", "me"."title", "me"."price", "owner__name" FROM ( SELECT "me"."id", "me"."source", "me"."owner", "me"."title", "me"."price", @@ -75,10 +85,14 @@ is_same_sql_bind( SELECT COUNT(*) FROM "books" "rownum__emulation" WHERE "rownum__emulation"."title" > "me"."title" - ) BETWEEN 1 AND 3 + ) BETWEEN ? AND ? ORDER BY "title" DESC )', - [ [ 'source', 'Library' ] ], + [ + [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ], + [ $OFFSET => 1 ], + [ $TOTAL => 3 ], + ], ); is_deeply ( @@ -110,10 +124,14 @@ is_same_sql_bind( SELECT COUNT(*) FROM "books" "rownum__emulation" WHERE "rownum__emulation"."title" < "me"."title" - ) BETWEEN 1 AND 4294967295 + ) BETWEEN ? AND ? ORDER BY "title" )', - [ [ 'source', 'Library' ] ], + [ + [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ], + [ $OFFSET => 1 ], + [ $TOTAL => 2147483647 ], + ], ); is_deeply ( @@ -124,9 +142,9 @@ is_deeply ( { $rs = $schema->resultset('Artist')->search({}, { - columns => 'name', + columns => 'artistid', offset => 1, - order_by => 'name', + order_by => 'artistid', }); local $rs->result_source->{name} = "weird \n newline/multi \t \t space containing \n table"; @@ -137,4 +155,40 @@ is_deeply ( ); } +# this is a nonsensical order_by, we are just making sure the bind-transport is correct +# (not that it'll be useful anywhere in the near future) +my $attr = {}; +my $rs_selectas_rel = $schema->resultset('BooksInLibrary')->search(undef, { + columns => 'me.id', + offset => 3, + rows => 4, + '+columns' => { bar => \['? * ?', [ $attr => 11 ], [ $attr => 12 ]], baz => \[ '?', [ $attr => 13 ]] }, + order_by => [ 'id', \['? / ?', [ $attr => 1 ], [ $attr => 2 ]], \[ '?', [ $attr => 3 ]] ], + having => \[ '?', [ $attr => 21 ] ], +}); + +is_same_sql_bind( + $rs_selectas_rel->as_query, + '( + SELECT "me"."id", "bar", "baz" + FROM ( + SELECT "me"."id", ? * ? AS "bar", ? AS "baz" + FROM "books" "me" + WHERE ( "source" = ? ) + HAVING ? + ) "me" + WHERE ( SELECT COUNT(*) FROM "books" "rownum__emulation" WHERE "rownum__emulation"."id" < "me"."id" ) BETWEEN ? AND ? + ORDER BY "id", ? / ?, ? + )', + [ + [ $attr => 11 ], [ $attr => 12 ], [ $attr => 13 ], + [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ], + [ $attr => 21 ], + [ {%$OFFSET} => 3 ], + [ {%$TOTAL} => 6 ], + [ $attr => 1 ], [ $attr => 2 ], [ $attr => 3 ], + ], + 'Pagination with sub-query in ORDER BY works' +); + done_testing;