Rewrite GenericSubQ from SQLA::L to be actually useful
[dbsrgits/DBIx-Class.git] / t / sqlahacks / limit_dialects / generic_subq.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 use DBIC::SqlMakerTest;
8
9 my $schema = DBICTest->init_schema;
10
11 $schema->storage->_sql_maker->limit_dialect ('GenericSubQ');
12
13 my $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
14   '+columns' => [{ owner_name => 'owner.name' }],
15   join => 'owner',
16   rows => 2,
17   order_by => 'me.title',
18 });
19
20 is_same_sql_bind(
21   $rs->as_query,
22   '(
23     SELECT  id, source, owner, title, price,
24             owner_name
25       FROM (
26         SELECT  me.id, me.source, me.owner, me.title, me.price,
27                 owner.name AS owner_name
28           FROM books me
29           JOIN owners owner ON owner.id = me.owner
30         WHERE ( source = ? )
31         ORDER BY me.title
32       ) me
33     WHERE
34       (
35         SELECT COUNT(*)
36           FROM books rownum__emulation
37         WHERE rownum__emulation.title < me.title
38       ) < 2
39   )',
40   [  [ 'source', 'Library' ] ],
41 );
42
43 is_deeply (
44   [ $rs->get_column ('title')->all ],
45   ['Best Recipe Cookbook', 'Dynamical Systems'],
46   'Correct columns selected with rows',
47 );
48
49 $schema->storage->_sql_maker->quote_char ('"');
50 $schema->storage->_sql_maker->name_sep ('.');
51
52 $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
53   order_by => { -desc => 'title' },
54   '+select' => ['owner.name'],
55   '+as' => ['owner.name'],
56   join => 'owner',
57   rows => 3,
58   offset => 1,
59 });
60
61 is_same_sql_bind(
62   $rs->as_query,
63   '(
64     SELECT  "id", "source", "owner", "title", "price",
65             "owner__name"
66       FROM (
67         SELECT  "me"."id", "me"."source", "me"."owner", "me"."title", "me"."price",
68                 "owner"."name" AS "owner__name"
69           FROM "books" "me"
70           JOIN "owners" "owner" ON "owner"."id" = "me"."owner"
71         WHERE ( "source" = ? )
72         ORDER BY "title" DESC
73       ) "me"
74     WHERE
75       (
76         SELECT COUNT(*)
77           FROM "books" "rownum__emulation"
78         WHERE "rownum__emulation"."title" > "me"."title"
79       ) BETWEEN 1 AND 3
80   )',
81   [ [ 'source', 'Library' ] ],
82 );
83
84 is_deeply (
85   [ $rs->get_column ('title')->all ],
86   [ 'Dynamical Systems', 'Best Recipe Cookbook' ],
87   'Correct columns selected with rows',
88 );
89
90 $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
91   order_by => 'title',
92   'select' => ['owner.name'],
93   'as' => ['owner_name'],
94   join => 'owner',
95   offset => 1,
96 });
97
98 is_same_sql_bind(
99   $rs->as_query,
100   '(
101     SELECT "owner_name"
102       FROM (
103         SELECT "owner"."name" AS "owner_name", "title"
104           FROM "books" "me"
105           JOIN "owners" "owner" ON "owner"."id" = "me"."owner"
106         WHERE ( "source" = ? )
107         ORDER BY "title"
108       ) "me"
109     WHERE
110       (
111         SELECT COUNT(*)
112           FROM "books" "rownum__emulation"
113         WHERE "rownum__emulation"."title" < "me"."title"
114       ) BETWEEN 1 AND 4294967295
115   )',
116   [ [ 'source', 'Library' ] ],
117 );
118
119 is_deeply (
120   [ $rs->get_column ('owner_name')->all ],
121   [ ('Newton') x 2 ],
122   'Correct columns selected with rows',
123 );
124
125 done_testing;