Commit | Line | Data |
75f025cf |
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 = ? ) |
75f025cf |
31 | ) me |
32 | WHERE |
33 | ( |
34 | SELECT COUNT(*) |
35 | FROM books rownum__emulation |
36 | WHERE rownum__emulation.title < me.title |
37 | ) < 2 |
b13d2248 |
38 | ORDER BY me.title |
75f025cf |
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" = ? ) |
75f025cf |
72 | ) "me" |
73 | WHERE |
74 | ( |
75 | SELECT COUNT(*) |
76 | FROM "books" "rownum__emulation" |
77 | WHERE "rownum__emulation"."title" > "me"."title" |
78 | ) BETWEEN 1 AND 3 |
b13d2248 |
79 | ORDER BY "title" DESC |
75f025cf |
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" = ? ) |
75f025cf |
107 | ) "me" |
108 | WHERE |
109 | ( |
110 | SELECT COUNT(*) |
111 | FROM "books" "rownum__emulation" |
112 | WHERE "rownum__emulation"."title" < "me"."title" |
113 | ) BETWEEN 1 AND 4294967295 |
b13d2248 |
114 | ORDER BY "title" |
75f025cf |
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; |