7 use DBIC::SqlMakerTest;
11 my $schema = DBICTest->init_schema();
13 my $sql_maker = $schema->storage->sql_maker;
15 $sql_maker->quote_char('`');
16 $sql_maker->name_sep('.');
18 my ($sql, @bind) = $sql_maker->select(
29 'artist.artistid' => 'me.artist'
35 '-join_type' => 'left'
38 'tracks.cd' => 'me.cdid'
44 { count => 'tracks.cd' },
45 { min => 'me.year', -as => 'me.minyear' },
48 'artist.name' => 'Caterwauler McCrae',
59 SELECT `me`.`cdid`, COUNT( `tracks`.`cd` ), MIN( `me`.`year` ) AS `me`.`minyear`
61 JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` )
62 LEFT JOIN `tracks` `tracks` ON ( `tracks`.`cd` = `me`.`cdid` )
63 WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )
65 [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
66 'got correct SQL and bind parameters for complex select query with quoting'
70 ($sql, @bind) = $sql_maker->select(
90 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`/, [],
91 'scalar ORDER BY okay (single value)'
95 ($sql, @bind) = $sql_maker->select(
118 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`, `title ASC`/, [],
119 'scalar ORDER BY okay (multiple values)'
123 ($sql, @bind) = $sql_maker->select(
143 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC/, [],
144 'hashref ORDER BY okay (single value)'
148 ($sql, @bind) = $sql_maker->select(
171 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC, `title` ASC/, [],
172 'hashref ORDER BY okay (multiple values)'
178 ($sql, @bind) = $sql_maker->select(
198 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
199 'did not quote ORDER BY with scalarref (single value)'
203 ($sql, @bind) = $sql_maker->select(
226 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC, title ASC/, [],
227 'did not quote ORDER BY with scalarref (multiple values)'
231 ($sql, @bind) = $sql_maker->select(
233 [qw/ me.cdid me.artist me.title /],
234 { cdid => \['rlike ?', [cdid => 'X'] ] },
235 { group_by => 'title', having => \['count(me.artist) > ?', [ cnt => 2] ] },
240 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title` FROM `cd` `me` WHERE ( `cdid` rlike ? ) GROUP BY `title` HAVING count(me.artist) > ?/,
241 [ [ cdid => 'X'], ['cnt' => '2'] ],
242 'Quoting works with where/having arrayrefsrefs',
246 ($sql, @bind) = $sql_maker->select(
248 [qw/ me.cdid me.artist me.title /],
249 { cdid => \'rlike X' },
250 { group_by => 'title', having => \'count(me.artist) > 2' },
255 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title` FROM `cd` `me` WHERE ( `cdid` rlike X ) GROUP BY `title` HAVING count(me.artist) > 2/,
257 'Quoting works with where/having scalarrefs',
261 ($sql, @bind) = $sql_maker->update(
271 q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
272 'quoted table names for UPDATE'
276 ($sql, @bind) = $sql_maker->select(
293 q/SELECT `me`.* FROM `cd` `me`/, [],
294 'select attr with me.* is right'
299 $sql_maker->quote_char([qw/[ ]/]);
301 ($sql, @bind) = $sql_maker->select(
308 'artist' => 'artist',
312 'artist.artistid' => 'me.artist'
328 'artist.name' => 'Caterwauler McCrae',
338 q/SELECT MAX ( [rank] ) AS [max_rank], [rank], COUNT( * ) AS [cnt] FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
339 'got correct SQL and bind parameters for count query with bracket quoting'
343 ($sql, @bind) = $sql_maker->update(
353 q/UPDATE [group] SET [name] = ?, [order] = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
354 'bracket quoted table names for UPDATE'