8 use DBIC::SqlMakerTest;
9 use DBIx::Class::SQLMaker::LimitDialects;
10 my ($ROWS, $TOTAL, $OFFSET) = (
11 DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
12 DBIx::Class::SQLMaker::LimitDialects->__total_bindtype,
13 DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
17 my $schema = DBICTest->init_schema;
19 $schema->storage->_sql_maker->limit_dialect ('GenericSubQ');
21 my $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
22 '+columns' => [{ owner_name => 'owner.name' }],
25 order_by => 'me.title',
31 SELECT me.id, me.source, me.owner, me.title, me.price,
34 SELECT me.id, me.source, me.owner, me.title, me.price,
35 owner.name AS owner_name
37 JOIN owners owner ON owner.id = me.owner
43 FROM books rownum__emulation
44 WHERE rownum__emulation.title < me.title
49 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
55 [ $rs->get_column ('title')->all ],
56 ['Best Recipe Cookbook', 'Dynamical Systems'],
57 'Correct columns selected with rows',
60 $schema->storage->_sql_maker->quote_char ('"');
61 $schema->storage->_sql_maker->name_sep ('.');
63 $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
64 order_by => { -desc => 'title' },
65 '+select' => ['owner.name'],
66 '+as' => ['owner.name'],
75 SELECT "me"."id", "me"."source", "me"."owner", "me"."title", "me"."price",
78 SELECT "me"."id", "me"."source", "me"."owner", "me"."title", "me"."price",
79 "owner"."name" AS "owner__name"
81 JOIN "owners" "owner" ON "owner"."id" = "me"."owner"
82 WHERE ( "source" = ? )
87 FROM "books" "rownum__emulation"
88 WHERE "rownum__emulation"."title" > "me"."title"
90 ORDER BY "me"."title" DESC
93 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
100 [ $rs->get_column ('title')->all ],
101 [ 'Dynamical Systems', 'Best Recipe Cookbook' ],
102 'Correct columns selected with rows',
105 $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
107 'select' => ['owner.name'],
108 'as' => ['owner_name'],
118 SELECT "owner"."name" AS "owner_name", "me"."title"
120 JOIN "owners" "owner" ON "owner"."id" = "me"."owner"
121 WHERE ( "source" = ? )
126 FROM "books" "rownum__emulation"
127 WHERE "rownum__emulation"."title" < "me"."title"
129 ORDER BY "me"."title" ASC
132 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
134 [ $TOTAL => 2147483647 ],
139 [ $rs->get_column ('owner_name')->all ],
141 'Correct columns selected with rows',
144 $rs = $schema->resultset('CD')->search({}, {
145 columns => [qw( me.cdid me.title me.genreid me.year tracks.position tracks.title )],
148 order_by => [ { -asc => 'me.genreid' }, { -desc => 'year' }, 'me.title', \ 'single_track DESC', { -desc => [qw( me.cdid tracks.position )] } ],
151 my @full_res = @{$rs->all_hri};
153 is (@full_res, 5, 'Expected amount of CDs');
158 { cdid => 2, genreid => undef, title => "Forkful of bees", year => 2001, tracks => [
159 { position => 3, title => "Sticky Honey" },
160 { position => 2, title => "Stripy" },
161 { position => 1, title => "Stung with Success" },
163 { cdid => 4, genreid => undef, title => "Generic Manufactured Singles", year => 2001, tracks => [
164 { position => 3, title => "No More Ideas" },
165 { position => 2, title => "Boring Song" },
166 { position => 1, title => "Boring Name" },
168 { cdid => 5, genreid => undef, title => "Come Be Depressed With Us", year => 1998, tracks => [
169 { position => 3, title => "Suicidal" },
170 { position => 2, title => "Under The Weather" },
171 { position => 1, title => "Sad" },
173 { cdid => 3, genreid => undef, title => "Caterwaulin' Blues", year => 1997, tracks => [
174 { position => 3, title => "Fowlin" },
175 { position => 2, title => "Howlin" },
176 { position => 1, title => "Yowlin" },
178 { cdid => 1, genreid => 1, title => "Spoonful of bees", year => 1999, tracks => [
179 { position => 3, title => "Beehind You" },
180 { position => 2, title => "Apiary" },
181 { position => 1, title => "The Bees Knees" },
184 'Complex ordered gensubq limited cds and tracks in expected sqlite order'
195 my $rownum_cmp_op = $slice->[0]
201 $rs->slice(@$slice)->all_hri,
202 [ @full_res[ $slice->[0] .. min($#full_res, $slice->[1]) ] ],
203 "Expected array slice on complex ordered limited gensubq ($slice->[0] : $slice->[1])",
207 $rs->slice(@$slice)->as_query,
209 SELECT "me"."cdid", "me"."title", "me"."genreid", "me"."year",
210 "tracks"."position", "tracks"."title"
212 SELECT "me"."cdid", "me"."title", "me"."genreid", "me"."year", "me"."single_track"
214 SELECT "me"."cdid", "me"."title", "me"."genreid", "me"."year", "me"."single_track"
216 LEFT JOIN "track" "tracks"
217 ON "tracks"."cd" = "me"."cdid"
218 GROUP BY "me"."cdid", "me"."title", "me"."genreid", "me"."year", "me"."single_track"
222 FROM cd "rownum__emulation"
224 ( "me"."genreid" IS NOT NULL AND "rownum__emulation"."genreid" IS NULL )
227 "rownum__emulation"."genreid" < "me"."genreid"
229 "me"."genreid" IS NOT NULL
231 "rownum__emulation"."genreid" IS NOT NULL
236 "me"."genreid" = "rownum__emulation"."genreid"
238 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
241 "rownum__emulation"."year" > "me"."year"
246 "me"."genreid" = "rownum__emulation"."genreid"
248 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
251 "me"."year" = "rownum__emulation"."year"
253 "rownum__emulation"."title" < "me"."title"
258 "me"."genreid" = "rownum__emulation"."genreid"
260 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
263 "me"."year" = "rownum__emulation"."year"
265 "me"."title" = "rownum__emulation"."title"
268 ("me"."single_track" IS NULL AND "rownum__emulation"."single_track" IS NOT NULL )
271 "rownum__emulation"."single_track" > "me"."single_track"
273 "me"."single_track" IS NOT NULL
275 "rownum__emulation"."single_track" IS NOT NULL
282 "me"."genreid" = "rownum__emulation"."genreid"
284 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
287 "me"."year" = "rownum__emulation"."year"
289 "me"."title" = "rownum__emulation"."title"
292 ( "me"."single_track" = "rownum__emulation"."single_track" )
294 ( "me"."single_track" IS NULL AND "rownum__emulation"."single_track" IS NULL )
297 "rownum__emulation"."cdid" > "me"."cdid"
301 ORDER BY "me"."genreid" ASC, "me"."year" DESC, "me"."title" ASC, "me"."single_track" DESC, "me"."cdid" DESC
303 LEFT JOIN "track" "tracks"
304 ON "tracks"."cd" = "me"."cdid"
305 ORDER BY "me"."genreid" ASC, "year" DESC, "me"."title", single_track DESC, "me"."cdid" DESC, "tracks"."position" DESC
308 ( $slice->[0] ? [ $OFFSET => $slice->[0] ] : () ),
309 [ $TOTAL => $slice->[1] + ($slice->[0] ? 0 : 1 ) ],
311 "Expected sql on complex ordered limited gensubq ($slice->[0] : $slice->[1])",
316 $rs = $schema->resultset('Artist')->search({}, {
317 columns => 'artistid',
319 order_by => 'artistid',
321 local $rs->result_source->{name} = "weird \n newline/multi \t \t space containing \n table";
324 ${$rs->as_query}->[0],
325 qr| weird \s \n \s newline/multi \s \t \s \t \s space \s containing \s \n \s table|x,
326 'Newlines/spaces preserved in final sql',