7 use DBICTest ':DiffSQL';
8 use DBIx::Class::SQLMaker::LimitDialects;
9 my ($ROWS, $TOTAL, $OFFSET) = (
10 DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
11 DBIx::Class::SQLMaker::LimitDialects->__total_bindtype,
12 DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
16 my $schema = DBICTest->init_schema;
18 $schema->storage->_sql_maker->limit_dialect ('GenericSubQ');
20 my $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
21 '+columns' => [{ owner_name => 'owner.name' }],
24 order_by => 'me.title',
30 SELECT me.id, me.source, me.owner, me.title, me.price,
33 SELECT me.id, me.source, me.owner, me.title, me.price,
34 owner.name AS owner_name
36 JOIN owners owner ON owner.id = me.owner
42 FROM books rownum__emulation
43 WHERE rownum__emulation.title < me.title
48 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
54 [ $rs->get_column ('title')->all ],
55 ['Best Recipe Cookbook', 'Dynamical Systems'],
56 'Correct columns selected with rows',
59 $schema->storage->_sql_maker->quote_char ('"');
60 $schema->storage->_sql_maker->name_sep ('.');
62 $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
63 order_by => { -desc => 'title' },
64 '+select' => ['owner.name'],
65 '+as' => ['owner.name'],
74 SELECT "me"."id", "me"."source", "me"."owner", "me"."title", "me"."price",
77 SELECT "me"."id", "me"."source", "me"."owner", "me"."title", "me"."price",
78 "owner"."name" AS "owner__name"
80 JOIN "owners" "owner" ON "owner"."id" = "me"."owner"
81 WHERE ( "source" = ? )
86 FROM "books" "rownum__emulation"
87 WHERE "rownum__emulation"."title" > "me"."title"
89 ORDER BY "me"."title" DESC
92 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
99 [ $rs->get_column ('title')->all ],
100 [ 'Dynamical Systems', 'Best Recipe Cookbook' ],
101 'Correct columns selected with rows',
104 $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
106 'select' => ['owner.name'],
107 'as' => ['owner_name'],
117 SELECT "owner"."name" AS "owner_name", "me"."title"
119 JOIN "owners" "owner" ON "owner"."id" = "me"."owner"
120 WHERE ( "source" = ? )
125 FROM "books" "rownum__emulation"
126 WHERE "rownum__emulation"."title" < "me"."title"
128 ORDER BY "me"."title" ASC
131 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
133 [ $TOTAL => 2147483647 ],
138 [ $rs->get_column ('owner_name')->all ],
140 'Correct columns selected with rows',
143 $rs = $schema->resultset('CD')->search({}, {
144 columns => [qw( me.cdid me.title me.genreid me.year tracks.position tracks.title )],
147 order_by => [ { -asc => 'me.genreid' }, { -desc => 'year' }, 'me.title', \ 'single_track DESC', { -desc => [qw( me.cdid tracks.position )] } ],
150 my @full_res = @{$rs->all_hri};
152 is (@full_res, 5, 'Expected amount of CDs');
157 { cdid => 2, genreid => undef, title => "Forkful of bees", year => 2001, tracks => [
158 { position => 3, title => "Sticky Honey" },
159 { position => 2, title => "Stripy" },
160 { position => 1, title => "Stung with Success" },
162 { cdid => 4, genreid => undef, title => "Generic Manufactured Singles", year => 2001, tracks => [
163 { position => 3, title => "No More Ideas" },
164 { position => 2, title => "Boring Song" },
165 { position => 1, title => "Boring Name" },
167 { cdid => 5, genreid => undef, title => "Come Be Depressed With Us", year => 1998, tracks => [
168 { position => 3, title => "Suicidal" },
169 { position => 2, title => "Under The Weather" },
170 { position => 1, title => "Sad" },
172 { cdid => 3, genreid => undef, title => "Caterwaulin' Blues", year => 1997, tracks => [
173 { position => 3, title => "Fowlin" },
174 { position => 2, title => "Howlin" },
175 { position => 1, title => "Yowlin" },
177 { cdid => 1, genreid => 1, title => "Spoonful of bees", year => 1999, tracks => [
178 { position => 3, title => "Beehind You" },
179 { position => 2, title => "Apiary" },
180 { position => 1, title => "The Bees Knees" },
183 'Complex ordered gensubq limited cds and tracks in expected sqlite order'
194 my $rownum_cmp_op = $slice->[0]
200 $rs->slice(@$slice)->all_hri,
201 [ @full_res[ $slice->[0] .. min($#full_res, $slice->[1]) ] ],
202 "Expected array slice on complex ordered limited gensubq ($slice->[0] : $slice->[1])",
206 $rs->slice(@$slice)->as_query,
208 SELECT "me"."cdid", "me"."title", "me"."genreid", "me"."year",
209 "tracks"."position", "tracks"."title"
211 SELECT "me"."cdid", "me"."title", "me"."genreid", "me"."year", "me"."single_track"
213 SELECT "me"."cdid", "me"."title", "me"."genreid", "me"."year", "me"."single_track"
215 LEFT JOIN "track" "tracks"
216 ON "tracks"."cd" = "me"."cdid"
217 GROUP BY "me"."cdid", "me"."title", "me"."genreid", "me"."year", "me"."single_track"
221 FROM cd "rownum__emulation"
223 ( "me"."genreid" IS NOT NULL AND "rownum__emulation"."genreid" IS NULL )
226 "rownum__emulation"."genreid" < "me"."genreid"
228 "me"."genreid" IS NOT NULL
230 "rownum__emulation"."genreid" IS NOT NULL
235 "me"."genreid" = "rownum__emulation"."genreid"
237 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
240 "rownum__emulation"."year" > "me"."year"
245 "me"."genreid" = "rownum__emulation"."genreid"
247 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
250 "me"."year" = "rownum__emulation"."year"
252 "rownum__emulation"."title" < "me"."title"
257 "me"."genreid" = "rownum__emulation"."genreid"
259 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
262 "me"."year" = "rownum__emulation"."year"
264 "me"."title" = "rownum__emulation"."title"
267 ("me"."single_track" IS NULL AND "rownum__emulation"."single_track" IS NOT NULL )
270 "rownum__emulation"."single_track" > "me"."single_track"
272 "me"."single_track" IS NOT NULL
274 "rownum__emulation"."single_track" IS NOT NULL
281 "me"."genreid" = "rownum__emulation"."genreid"
283 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
286 "me"."year" = "rownum__emulation"."year"
288 "me"."title" = "rownum__emulation"."title"
291 ( "me"."single_track" = "rownum__emulation"."single_track" )
293 ( "me"."single_track" IS NULL AND "rownum__emulation"."single_track" IS NULL )
296 "rownum__emulation"."cdid" > "me"."cdid"
300 ORDER BY "me"."genreid" ASC, "me"."year" DESC, "me"."title" ASC, "me"."single_track" DESC, "me"."cdid" DESC
302 LEFT JOIN "track" "tracks"
303 ON "tracks"."cd" = "me"."cdid"
304 ORDER BY "me"."genreid" ASC, "year" DESC, "me"."title", single_track DESC, "me"."cdid" DESC, "tracks"."position" DESC
307 ( $slice->[0] ? [ $OFFSET => $slice->[0] ] : () ),
308 [ $TOTAL => $slice->[1] + ($slice->[0] ? 0 : 1 ) ],
310 "Expected sql on complex ordered limited gensubq ($slice->[0] : $slice->[1])",
315 $rs = $schema->resultset('Artist')->search({}, {
316 columns => 'artistid',
318 order_by => 'artistid',
320 local $rs->result_source->{name} = "weird \n newline/multi \t \t space containing \n table";
323 ${$rs->as_query}->[0],
324 qr| weird \s \n \s newline/multi \s \t \s \t \s space \s containing \s \n \s table|x,
325 'Newlines/spaces preserved in final sql',