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") {
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 ) );
}
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;
}
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";
--- /dev/null
+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');