From: Ash Berlin Date: Wed, 7 Feb 2007 15:10:32 +0000 (+0000) Subject: quoting tests X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=28f137a08dc43c7207e0455ab7b4e94c6d9f7487;p=scpubgit%2FQ-Branch.git quoting tests r2299@metis (orig r21): matthewt | 2007-02-07 13:46:26 +0000 r33851@cain (orig r10): matthewt | 2006-11-18 06:58:26 +0000 --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index cb37b1a..4cdbc3a 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -264,6 +264,8 @@ sub _quote { return $label if $label eq '*'; + return $$label if ref($label) eq 'SCALAR'; + return $label unless $self->{quote_char}; if (ref $self->{quote_char} eq "ARRAY") { @@ -273,7 +275,9 @@ sub _quote { my $sep = $self->{name_sep}; return join($self->{name_sep}, - map { $self->{quote_char}->[0] . $_ . $self->{quote_char}->[1] } + map { $_ eq '*' + ? $_ + : $self->{quote_char}->[0] . $_ . $self->{quote_char}->[1] } split( /\Q$sep\E/, $label ) ); } @@ -282,7 +286,7 @@ sub _quote { if !defined $self->{name_sep}; return join $self->{name_sep}, - map { $self->{quote_char} . $_ . $self->{quote_char} } + map { $_ eq '*' ? $_ : $self->{quote_char} . $_ . $self->{quote_char} } split /\Q$self->{name_sep}\E/, $label; } @@ -847,7 +851,7 @@ sub _order_by { my $ref = ref $_[0]; my @vals = $ref eq 'ARRAY' ? @{$_[0]} : - $ref eq 'SCALAR' ? ${$_[0]} : + $ref eq 'SCALAR' ? $_[0] : $ref eq '' ? $_[0] : puke "Unsupported data struct $ref for ORDER BY"; diff --git a/t/05quotes.t b/t/05quotes.t new file mode 100644 index 0000000..105ee94 --- /dev/null +++ b/t/05quotes.t @@ -0,0 +1,193 @@ +use strict; +use warnings; + +use vars qw($TESTING); +$TESTING = 1; +use Test; + +# use a BEGIN block so we print our plan before SQL::Abstract is loaded +BEGIN { plan tests => 4 } + +use SQL::Abstract; + +sub is { + my ($got, $expect, $msg) = @_; + ok($got eq $expect) or + warn "got [${got}]\ninstead of [${expect}]\nfor test ${msg}\n\n"; +} + +my $sql_maker = SQL::Abstract->new; + +$sql_maker->{quote_char} = '`'; +$sql_maker->{name_sep} = '.'; + +my ($sql,) = $sql_maker->select( + [ + { + 'me' => 'cd' + }, + [ + { + 'artist' => 'artist', + '-join_type' => '' + }, + { + 'artist.artistid' => 'me.artist' + } + ] + ], + [ + #{ + # 'count' => '*' + #} + \'COUNT( * )' + ], + { + 'artist.name' => 'Caterwauler McCrae', + 'me.year' => 2001 + }, + [], + undef, + undef +); + +is($sql, + q/SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )/, + 'got correct SQL for count query with quoting'); + +($sql,) = $sql_maker->select( + [ + { + 'me' => 'cd' + } + ], + [ + 'me.cdid', + 'me.artist', + 'me.title', + 'me.year' + ], + undef, + [ + 'year DESC' + ], + undef, + undef +); + +#TODO: { +# local $TODO = "order_by with quoting needs fixing (ash/castaway)"; +# +# is($sql, +# q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC/, +# 'quoted ORDER BY with DESC okay'); +#} + +($sql,) = $sql_maker->select( + [ + { + 'me' => 'cd' + } + ], + [ + 'me.*' + ], + undef, + [], + undef, + undef +); + +is($sql, q/SELECT `me`.* FROM `cd` `me`/, 'select attr with me.* is right'); + +($sql,) = $sql_maker->select( + [ + { + 'me' => 'cd' + } + ], + [ + 'me.cdid', + 'me.artist', + 'me.title', + 'me.year' + ], + undef, + [ + \'year DESC' + ], + undef, + undef +); + +is($sql, + q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, + 'did not quote ORDER BY with scalarref'); + +my %data = ( + name => 'Bill', + order => 12 +); + +my @binds; + +($sql,@binds) = $sql_maker->update( + 'group', + { + 'order' => '12', + 'name' => 'Bill' + } +); + +is($sql, + q/UPDATE `group` SET `name` = ?, `order` = ?/, + 'quoted table names for UPDATE'); + +$sql_maker->{quote_char} = [qw/[ ]/]; + +($sql,) = $sql_maker->select( + [ + { + 'me' => 'cd' + }, + [ + { + 'artist' => 'artist', + '-join_type' => '' + }, + { + 'artist.artistid' => 'me.artist' + } + ] + ], + [ + #{ + # 'count' => '*' + #} + \'COUNT( * )' + ], + { + 'artist.name' => 'Caterwauler McCrae', + 'me.year' => 2001 + }, + [], + undef, + undef +); + +is($sql, + q/SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/, + 'got correct SQL for count query with bracket quoting'); + + +($sql,@binds) = $sql_maker->update( + 'group', + { + 'order' => '12', + 'name' => 'Bill' + } +); + +is($sql, + q/UPDATE [group] SET [name] = ?, [order] = ?/, + 'bracket quoted table names for UPDATE');