7 use DBICTest ':DiffSQL';
9 my $schema = DBICTest->init_schema();
11 my $sql_maker = $schema->storage->sql_maker;
13 $sql_maker->quote_char('`');
14 $sql_maker->name_sep('.');
16 my ($sql, @bind) = $sql_maker->select(
27 'artist.artistid' => 'me.artist'
33 '-join_type' => 'left'
36 'tracks.cd' => 'me.cdid'
42 { count => 'tracks.cd' },
43 { min => 'me.year', -as => 'minyear' },
46 'artist.name' => 'Caterwauler McCrae',
57 SELECT `me`.`cdid`, COUNT( `tracks`.`cd` ), MIN( `me`.`year` ) AS `minyear`
59 JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` )
60 LEFT JOIN `tracks` `tracks` ON ( `tracks`.`cd` = `me`.`cdid` )
61 WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )
63 [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
64 'got correct SQL and bind parameters for complex select query with quoting'
68 ($sql, @bind) = $sql_maker->select(
81 { order_by => 'year DESC' },
88 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`/, [],
89 'scalar ORDER BY okay (single value)'
93 ($sql, @bind) = $sql_maker->select(
116 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`, `title ASC`/, [],
117 'scalar ORDER BY okay (multiple values)'
121 ($sql, @bind) = $sql_maker->select(
134 { order_by => { -desc => 'year' } },
141 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC/, [],
142 'hashref ORDER BY okay (single value)'
146 ($sql, @bind) = $sql_maker->select(
169 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC, `title` ASC/, [],
170 'hashref ORDER BY okay (multiple values)'
176 ($sql, @bind) = $sql_maker->select(
189 { order_by => \'year DESC' },
196 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
197 'did not quote ORDER BY with scalarref (single value)'
201 ($sql, @bind) = $sql_maker->select(
224 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC, title ASC/, [],
225 'did not quote ORDER BY with scalarref (multiple values)'
229 ($sql, @bind) = $sql_maker->select(
231 [qw/ me.cdid me.artist me.title /],
232 { cdid => \['rlike ?', [cdid => 'X'] ] },
233 { group_by => 'title', having => \['count(me.artist) > ?', [ cnt => 2] ] },
238 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title` FROM `cd` `me` WHERE ( `cdid` rlike ? ) GROUP BY `title` HAVING count(me.artist) > ?/,
239 [ [ cdid => 'X'], ['cnt' => '2'] ],
240 'Quoting works with where/having arrayrefsrefs',
244 ($sql, @bind) = $sql_maker->select(
246 [qw/ me.cdid me.artist me.title /],
247 { cdid => \'rlike X' },
248 { group_by => 'title', having => \'count(me.artist) > 2' },
253 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title` FROM `cd` `me` WHERE ( `cdid` rlike X ) GROUP BY `title` HAVING count(me.artist) > 2/,
255 'Quoting works with where/having scalarrefs',
259 ($sql, @bind) = $sql_maker->update(
269 q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
270 'quoted table names for UPDATE'
274 ($sql, @bind) = $sql_maker->select(
291 q/SELECT `me`.* FROM `cd` `me`/, [],
292 'select attr with me.* is right'
297 $sql_maker->quote_char([qw/[ ]/]);
299 ($sql, @bind) = $sql_maker->select(
306 'artist' => 'artist',
310 'artist.artistid' => 'me.artist'
326 'artist.name' => 'Caterwauler McCrae',
336 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] ],
337 'got correct SQL and bind parameters for count query with bracket quoting'
341 ($sql, @bind) = $sql_maker->update(
351 q/UPDATE [group] SET [name] = ?, [order] = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
352 'bracket quoted table names for UPDATE'