X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F95sql_maker_quote.t;h=6b27c7b3e1fff168704d6a3b0974d1b98fbaa7e1;hb=a7daf36a4a1cdd5e53d22a3f7a2a743858a9319f;hp=5dd71a8208dd99a3bd96744df1a037d171a69d22;hpb=8682bb0758d4dd03517a8f0e4d0f9dcaf3b9ac44;p=dbsrgits%2FDBIx-Class.git diff --git a/t/95sql_maker_quote.t b/t/95sql_maker_quote.t index 5dd71a8..6b27c7b 100644 --- a/t/95sql_maker_quote.t +++ b/t/95sql_maker_quote.t @@ -2,18 +2,17 @@ use strict; use warnings; use Test::More; -use SQL::Abstract::Test import => ['is_same_sql_bind']; +use lib qw(t/lib); +use DBIC::SqlMakerTest; BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 8 ); + : ( tests => 12 ); } -use lib qw(t/lib); - use_ok('DBICTest'); my $schema = DBICTest->init_schema(); @@ -36,12 +35,23 @@ my ($sql, @bind) = $sql_maker->select( { 'artist.artistid' => 'me.artist' } - ] + ], + [ + { + 'tracks' => 'tracks', + '-join_type' => 'left' + }, + { + 'tracks.cd' => 'me.cdid' + } + ], ], [ - { - 'count' => '*' - } + 'me.cdid', + { count => 'tracks.cd' }, + { -select => 'me.artist' }, + { -select => 'me.title', -as => 'name' }, + { -select => { min => 'me.year' }, -as => 'me.minyear' }, ], { 'artist.name' => 'Caterwauler McCrae', @@ -54,10 +64,43 @@ my ($sql, @bind) = $sql_maker->select( is_same_sql_bind( $sql, \@bind, - q/SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ], - 'got correct SQL and bind parameters for count query with quoting' + q/ + SELECT `me`.`cdid`, COUNT( `tracks`.`cd` ), `me`.`artist`, `me`.`title` AS `name`, MIN( `me`.`year` ) AS `me`.`minyear` + FROM `cd` `me` + JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) + LEFT JOIN `tracks` `tracks` ON ( `tracks`.`cd` = `me`.`cdid` ) + WHERE ( `artist`.`name` = ? AND `me`.`year` = ? ) + /, + [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ], + 'got correct SQL and bind parameters for complex select query with quoting' +); + + +($sql, @bind) = $sql_maker->select( + [ + { + 'me' => 'cd' + } + ], + [ + 'me.cdid', + 'me.artist', + 'me.title', + 'me.year' + ], + undef, + 'year DESC', + undef, + undef +); + +is_same_sql_bind( + $sql, \@bind, + q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`/, [], + 'scalar ORDER BY okay (single value)' ); + ($sql, @bind) = $sql_maker->select( [ { @@ -72,7 +115,8 @@ is_same_sql_bind( ], undef, [ - { -desc => 'year' } + 'year DESC', + 'title ASC' ], undef, undef @@ -80,32 +124,91 @@ is_same_sql_bind( is_same_sql_bind( $sql, \@bind, - q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC/, [], - 'hashref ORDER BY okay' + q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`, `title ASC`/, [], + 'scalar ORDER BY okay (multiple values)' ); +{ + ($sql, @bind) = $sql_maker->select( + [ + { + 'me' => 'cd' + } + ], + [ + 'me.cdid', + 'me.artist', + 'me.title', + 'me.year' + ], + undef, + { -desc => 'year' }, + undef, + undef + ); + + is_same_sql_bind( + $sql, \@bind, + q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC/, [], + 'hashref ORDER BY okay (single value)' + ); + + + ($sql, @bind) = $sql_maker->select( + [ + { + 'me' => 'cd' + } + ], + [ + 'me.cdid', + 'me.artist', + 'me.title', + 'me.year' + ], + undef, + [ + { -desc => 'year' }, + { -asc => 'title' } + ], + undef, + undef + ); + + is_same_sql_bind( + $sql, \@bind, + q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC, `title` ASC/, [], + 'hashref ORDER BY okay (multiple values)' + ); + +} + ($sql, @bind) = $sql_maker->select( - [ - { - 'me' => 'cd' - } - ], - [ - 'me.*' - ], - undef, - [], - undef, - undef + [ + { + 'me' => 'cd' + } + ], + [ + 'me.cdid', + 'me.artist', + 'me.title', + 'me.year' + ], + undef, + \'year DESC', + undef, + undef ); is_same_sql_bind( $sql, \@bind, - q/SELECT `me`.* FROM `cd` `me`/, [], - 'select attr with me.* is right' + q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [], + 'did not quote ORDER BY with scalarref (single value)' ); + ($sql, @bind) = $sql_maker->select( [ { @@ -120,7 +223,8 @@ is_same_sql_bind( ], undef, [ - \'year DESC' + \'year DESC', + \'title ASC' ], undef, undef @@ -128,10 +232,11 @@ is_same_sql_bind( is_same_sql_bind( $sql, \@bind, - q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [], - 'did not quote ORDER BY with scalarref' + q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC, title ASC/, [], + 'did not quote ORDER BY with scalarref (multiple values)' ); + ($sql, @bind) = $sql_maker->update( 'group', { @@ -146,6 +251,30 @@ is_same_sql_bind( 'quoted table names for UPDATE' ); +{ + ($sql, @bind) = $sql_maker->select( + [ + { + 'me' => 'cd' + } + ], + [ + 'me.*' + ], + undef, + [], + undef, + undef + ); + + is_same_sql_bind( + $sql, \@bind, + q/SELECT `me`.* FROM `cd` `me`/, [], + 'select attr with me.* is right' + ); +} + + $sql_maker->quote_char([qw/[ ]/]); ($sql, @bind) = $sql_maker->select(