1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
9 use DBICTest ':DiffSQL';
10 use DBIx::Class::SQLMaker::LimitDialects;
11 my ($ROWS, $TOTAL, $OFFSET) = (
12 DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
13 DBIx::Class::SQLMaker::LimitDialects->__total_bindtype,
14 DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
18 my $schema = DBICTest->init_schema;
20 $schema->storage->_sql_maker->limit_dialect ('GenericSubQ');
22 my $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
23 '+columns' => [{ owner_name => 'owner.name' }],
26 order_by => 'me.title',
32 SELECT me.id, me.source, me.owner, me.title, me.price,
35 SELECT me.id, me.source, me.owner, me.title, me.price,
36 owner.name AS owner_name
38 JOIN owners owner ON owner.id = me.owner
44 FROM books rownum__emulation
45 WHERE rownum__emulation.title < me.title
50 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
56 [ $rs->get_column ('title')->all ],
57 ['Best Recipe Cookbook', 'Dynamical Systems'],
58 'Correct columns selected with rows',
61 $schema->storage->_sql_maker->quote_char ('"');
62 $schema->storage->_sql_maker->name_sep ('.');
64 $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
65 order_by => { -desc => 'title' },
66 '+select' => ['owner.name'],
67 '+as' => ['owner.name'],
76 SELECT "me"."id", "me"."source", "me"."owner", "me"."title", "me"."price",
79 SELECT "me"."id", "me"."source", "me"."owner", "me"."title", "me"."price",
80 "owner"."name" AS "owner__name"
82 JOIN "owners" "owner" ON "owner"."id" = "me"."owner"
83 WHERE ( "source" = ? )
88 FROM "books" "rownum__emulation"
89 WHERE "rownum__emulation"."title" > "me"."title"
91 ORDER BY "me"."title" DESC
94 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
101 [ $rs->get_column ('title')->all ],
102 [ 'Dynamical Systems', 'Best Recipe Cookbook' ],
103 'Correct columns selected with rows',
106 $rs = $schema->resultset ('BooksInLibrary')->search ({}, {
108 'select' => ['owner.name'],
109 'as' => ['owner_name'],
119 SELECT "owner"."name" AS "owner_name", "me"."title"
121 JOIN "owners" "owner" ON "owner"."id" = "me"."owner"
122 WHERE ( "source" = ? )
127 FROM "books" "rownum__emulation"
128 WHERE "rownum__emulation"."title" < "me"."title"
130 ORDER BY "me"."title" ASC
133 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
135 [ $TOTAL => 2147483647 ],
140 [ $rs->get_column ('owner_name')->all ],
142 'Correct columns selected with rows',
145 $rs = $schema->resultset('CD')->search({}, {
146 columns => [qw( me.cdid me.title me.genreid me.year tracks.position tracks.title )],
149 order_by => [ { -asc => 'me.genreid' }, { -desc => 'year' }, 'me.title', \ 'single_track DESC', { -desc => [qw( me.cdid tracks.position )] } ],
152 my @full_res = @{$rs->all_hri};
154 is (@full_res, 5, 'Expected amount of CDs');
159 { cdid => 2, genreid => undef, title => "Forkful of bees", year => 2001, tracks => [
160 { position => 3, title => "Sticky Honey" },
161 { position => 2, title => "Stripy" },
162 { position => 1, title => "Stung with Success" },
164 { cdid => 4, genreid => undef, title => "Generic Manufactured Singles", year => 2001, tracks => [
165 { position => 3, title => "No More Ideas" },
166 { position => 2, title => "Boring Song" },
167 { position => 1, title => "Boring Name" },
169 { cdid => 5, genreid => undef, title => "Come Be Depressed With Us", year => 1998, tracks => [
170 { position => 3, title => "Suicidal" },
171 { position => 2, title => "Under The Weather" },
172 { position => 1, title => "Sad" },
174 { cdid => 3, genreid => undef, title => "Caterwaulin' Blues", year => 1997, tracks => [
175 { position => 3, title => "Fowlin" },
176 { position => 2, title => "Howlin" },
177 { position => 1, title => "Yowlin" },
179 { cdid => 1, genreid => 1, title => "Spoonful of bees", year => 1999, tracks => [
180 { position => 3, title => "Beehind You" },
181 { position => 2, title => "Apiary" },
182 { position => 1, title => "The Bees Knees" },
185 'Complex ordered gensubq limited cds and tracks in expected sqlite order'
196 my $rownum_cmp_op = $slice->[0]
202 $rs->slice(@$slice)->all_hri,
203 [ @full_res[ $slice->[0] .. min($#full_res, $slice->[1]) ] ],
204 "Expected array slice on complex ordered limited gensubq ($slice->[0] : $slice->[1])",
208 $rs->slice(@$slice)->as_query,
210 SELECT "me"."cdid", "me"."title", "me"."genreid", "me"."year",
211 "tracks"."position", "tracks"."title"
213 SELECT "me"."cdid", "me"."title", "me"."genreid", "me"."year", "me"."single_track"
215 SELECT "me"."cdid", "me"."title", "me"."genreid", "me"."year", "me"."single_track"
217 LEFT JOIN "track" "tracks"
218 ON "tracks"."cd" = "me"."cdid"
219 GROUP BY "me"."cdid", "me"."title", "me"."genreid", "me"."year", "me"."single_track"
223 FROM cd "rownum__emulation"
225 ( "me"."genreid" IS NOT NULL AND "rownum__emulation"."genreid" IS NULL )
228 "rownum__emulation"."genreid" < "me"."genreid"
230 "me"."genreid" IS NOT NULL
232 "rownum__emulation"."genreid" IS NOT NULL
237 "me"."genreid" = "rownum__emulation"."genreid"
239 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
242 "rownum__emulation"."year" > "me"."year"
247 "me"."genreid" = "rownum__emulation"."genreid"
249 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
252 "me"."year" = "rownum__emulation"."year"
254 "rownum__emulation"."title" < "me"."title"
259 "me"."genreid" = "rownum__emulation"."genreid"
261 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
264 "me"."year" = "rownum__emulation"."year"
266 "me"."title" = "rownum__emulation"."title"
269 ("me"."single_track" IS NULL AND "rownum__emulation"."single_track" IS NOT NULL )
272 "rownum__emulation"."single_track" > "me"."single_track"
274 "me"."single_track" IS NOT NULL
276 "rownum__emulation"."single_track" IS NOT NULL
283 "me"."genreid" = "rownum__emulation"."genreid"
285 ( "me"."genreid" IS NULL AND "rownum__emulation"."genreid" IS NULL )
288 "me"."year" = "rownum__emulation"."year"
290 "me"."title" = "rownum__emulation"."title"
293 ( "me"."single_track" = "rownum__emulation"."single_track" )
295 ( "me"."single_track" IS NULL AND "rownum__emulation"."single_track" IS NULL )
298 "rownum__emulation"."cdid" > "me"."cdid"
302 ORDER BY "me"."genreid" ASC, "me"."year" DESC, "me"."title" ASC, "me"."single_track" DESC, "me"."cdid" DESC
304 LEFT JOIN "track" "tracks"
305 ON "tracks"."cd" = "me"."cdid"
306 ORDER BY "me"."genreid" ASC, "year" DESC, "me"."title", single_track DESC, "me"."cdid" DESC, "tracks"."position" DESC
309 ( $slice->[0] ? [ $OFFSET => $slice->[0] ] : () ),
310 [ $TOTAL => $slice->[1] + ($slice->[0] ? 0 : 1 ) ],
312 "Expected sql on complex ordered limited gensubq ($slice->[0] : $slice->[1])",
317 $rs = $schema->resultset('Artist')->search({}, {
318 columns => 'artistid',
320 order_by => 'artistid',
322 local $rs->result_source->{name} = "weird \n newline/multi \t \t space containing \n table";
325 ${$rs->as_query}->[0],
326 qr| weird \s \n \s newline/multi \s \t \s \t \s space \s containing \s \n \s table|x,
327 'Newlines/spaces preserved in final sql',