X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fsqlahacks%2Flimit_dialects%2Fgeneric_subq.t;fp=t%2Fsqlahacks%2Flimit_dialects%2Fgeneric_subq.t;h=7f924d1f011d555873131157783b52124cf01afb;hb=75f025cf247e75869dcdfe46b37e24d4e0b15e8b;hp=0000000000000000000000000000000000000000;hpb=7ad315356c82a425365b88b6146617485897a4a0;p=dbsrgits%2FDBIx-Class.git diff --git a/t/sqlahacks/limit_dialects/generic_subq.t b/t/sqlahacks/limit_dialects/generic_subq.t new file mode 100644 index 0000000..7f924d1 --- /dev/null +++ b/t/sqlahacks/limit_dialects/generic_subq.t @@ -0,0 +1,125 @@ +use strict; +use warnings; + +use Test::More; +use lib qw(t/lib); +use DBICTest; +use DBIC::SqlMakerTest; + +my $schema = DBICTest->init_schema; + +$schema->storage->_sql_maker->limit_dialect ('GenericSubQ'); + +my $rs = $schema->resultset ('BooksInLibrary')->search ({}, { + '+columns' => [{ owner_name => 'owner.name' }], + join => 'owner', + rows => 2, + order_by => 'me.title', +}); + +is_same_sql_bind( + $rs->as_query, + '( + SELECT id, source, owner, title, price, + owner_name + FROM ( + SELECT me.id, me.source, me.owner, me.title, me.price, + owner.name AS owner_name + FROM books me + JOIN owners owner ON owner.id = me.owner + WHERE ( source = ? ) + ORDER BY me.title + ) me + WHERE + ( + SELECT COUNT(*) + FROM books rownum__emulation + WHERE rownum__emulation.title < me.title + ) < 2 + )', + [ [ 'source', 'Library' ] ], +); + +is_deeply ( + [ $rs->get_column ('title')->all ], + ['Best Recipe Cookbook', 'Dynamical Systems'], + 'Correct columns selected with rows', +); + +$schema->storage->_sql_maker->quote_char ('"'); +$schema->storage->_sql_maker->name_sep ('.'); + +$rs = $schema->resultset ('BooksInLibrary')->search ({}, { + order_by => { -desc => 'title' }, + '+select' => ['owner.name'], + '+as' => ['owner.name'], + join => 'owner', + rows => 3, + offset => 1, +}); + +is_same_sql_bind( + $rs->as_query, + '( + SELECT "id", "source", "owner", "title", "price", + "owner__name" + FROM ( + SELECT "me"."id", "me"."source", "me"."owner", "me"."title", "me"."price", + "owner"."name" AS "owner__name" + FROM "books" "me" + JOIN "owners" "owner" ON "owner"."id" = "me"."owner" + WHERE ( "source" = ? ) + ORDER BY "title" DESC + ) "me" + WHERE + ( + SELECT COUNT(*) + FROM "books" "rownum__emulation" + WHERE "rownum__emulation"."title" > "me"."title" + ) BETWEEN 1 AND 3 + )', + [ [ 'source', 'Library' ] ], +); + +is_deeply ( + [ $rs->get_column ('title')->all ], + [ 'Dynamical Systems', 'Best Recipe Cookbook' ], + 'Correct columns selected with rows', +); + +$rs = $schema->resultset ('BooksInLibrary')->search ({}, { + order_by => 'title', + 'select' => ['owner.name'], + 'as' => ['owner_name'], + join => 'owner', + offset => 1, +}); + +is_same_sql_bind( + $rs->as_query, + '( + SELECT "owner_name" + FROM ( + SELECT "owner"."name" AS "owner_name", "title" + FROM "books" "me" + JOIN "owners" "owner" ON "owner"."id" = "me"."owner" + WHERE ( "source" = ? ) + ORDER BY "title" + ) "me" + WHERE + ( + SELECT COUNT(*) + FROM "books" "rownum__emulation" + WHERE "rownum__emulation"."title" < "me"."title" + ) BETWEEN 1 AND 4294967295 + )', + [ [ 'source', 'Library' ] ], +); + +is_deeply ( + [ $rs->get_column ('owner_name')->all ], + [ ('Newton') x 2 ], + 'Correct columns selected with rows', +); + +done_testing;