[Bast-commits] r5495 - in SQL-Abstract/1.x/branches/1.50_RC: .
lib/SQL t
norbi at dev.catalyst.perl.org
norbi at dev.catalyst.perl.org
Tue Feb 17 19:20:53 GMT 2009
Author: norbi
Date: 2009-02-17 19:20:53 +0000 (Tue, 17 Feb 2009)
New Revision: 5495
Modified:
SQL-Abstract/1.x/branches/1.50_RC/
SQL-Abstract/1.x/branches/1.50_RC/lib/SQL/Abstract.pm
SQL-Abstract/1.x/branches/1.50_RC/t/01generate.t
Log:
r5612 at vger: mendel | 2009-02-17 20:20:45 +0100
* Fixed behaviour of 'literal SQL with bind' feature (ie. \[$sql, @bind]) to expect @bind in the same format as returned by ->where().
* Added some tests for the above feature.
Property changes on: SQL-Abstract/1.x/branches/1.50_RC
___________________________________________________________________
Name: svk:merge
- 4d5fae46-8e6a-4e08-abee-817e9fb894a2:/local/bast/SQL-Abstract/1.x/branches/1.50_RC:5599
4d5fae46-8e6a-4e08-abee-817e9fb894a2:/local/bast/SQL-Abstract/1.x/branches/1.50_RC-extraparens:5308
+ 4d5fae46-8e6a-4e08-abee-817e9fb894a2:/local/bast/SQL-Abstract/1.x/branches/1.50_RC:5612
4d5fae46-8e6a-4e08-abee-817e9fb894a2:/local/bast/SQL-Abstract/1.x/branches/1.50_RC-extraparens:5308
Modified: SQL-Abstract/1.x/branches/1.50_RC/lib/SQL/Abstract.pm
===================================================================
--- SQL-Abstract/1.x/branches/1.50_RC/lib/SQL/Abstract.pm 2009-02-17 08:12:34 UTC (rev 5494)
+++ SQL-Abstract/1.x/branches/1.50_RC/lib/SQL/Abstract.pm 2009-02-17 19:20:53 UTC (rev 5495)
@@ -110,19 +110,8 @@
my @fields = sort keys %$data;
- my ($sql, @bind);
- { # get values (need temporary override of bindtype to avoid an error)
- local $self->{bindtype} = 'normal';
- ($sql, @bind) = $self->_insert_ARRAYREF([@{$data}{@fields}]);
- }
+ my ($sql, @bind) = $self->_insert_values($data);
- # if necessary, transform values according to 'bindtype'
- if ($self->{bindtype} eq 'columns') {
- for my $i (0 .. $#fields) {
- ($bind[$i]) = $self->_bindtype($fields[$i], $bind[$i]);
- }
- }
-
# assemble SQL
$_ = $self->_quote($_) foreach @fields;
$sql = "( ".join(", ", @fields).") ".$sql;
@@ -137,18 +126,48 @@
$self->{bindtype} ne 'columns'
or belch "can't do 'columns' bindtype when called with arrayref";
+ # fold the list of values into a hash of column name - value pairs
+ # (where the column names are artificially generated, and their
+ # lexicographical ordering keep the ordering of the original list)
+ my $i = "a"; # incremented values will be in lexicographical order
+ my $data_in_hash = { map { ($i++ => $_) } @$data };
+
+ return $self->_insert_values($data_in_hash);
+}
+
+sub _insert_ARRAYREFREF { # literal SQL with bind
+ my ($self, $data) = @_;
+
+ my ($sql, @bind) = @${$data};
+ $self->_assert_bindval_matches_bindtype(@bind);
+
+ return ($sql, @bind);
+}
+
+
+sub _insert_SCALARREF { # literal SQL without bind
+ my ($self, $data) = @_;
+
+ return ($$data);
+}
+
+sub _insert_values {
+ my ($self, $data) = @_;
+
my (@values, @all_bind);
- for my $v (@$data) {
+ foreach my $column (sort keys %$data) {
+ my $v = $data->{$column};
$self->_SWITCH_refkind($v, {
ARRAYREF => sub {
if ($self->{array_datatypes}) { # if array datatype are activated
push @values, '?';
- push @all_bind, $v;
+ push @all_bind, $self->_bindtype($column, $v);
}
else { # else literal SQL with bind
my ($sql, @bind) = @$v;
+ $self->_assert_bindval_matches_bindtype(@bind);
push @values, $sql;
push @all_bind, @bind;
}
@@ -156,6 +175,7 @@
ARRAYREFREF => sub { # literal SQL with bind
my ($sql, @bind) = @${$v};
+ $self->_assert_bindval_matches_bindtype(@bind);
push @values, $sql;
push @all_bind, @bind;
},
@@ -165,7 +185,7 @@
#TODO in SQLA >= 2.0 it will die instead
belch "HASH ref as bind value in insert is not supported";
push @values, '?';
- push @all_bind, $v;
+ push @all_bind, $self->_bindtype($column, $v);
},
SCALARREF => sub { # literal SQL without bind
@@ -174,7 +194,7 @@
SCALAR_or_UNDEF => sub {
push @values, '?';
- push @all_bind, $v;
+ push @all_bind, $self->_bindtype($column, $v);
},
});
@@ -186,20 +206,7 @@
}
-sub _insert_ARRAYREFREF { # literal SQL with bind
- my ($self, $data) = @_;
- return @${$data};
-}
-
-sub _insert_SCALARREF { # literal SQL without bind
- my ($self, $data) = @_;
-
- return ($$data);
-}
-
-
-
#======================================================================
# UPDATE methods
#======================================================================
@@ -229,14 +236,16 @@
}
else { # literal SQL with bind
my ($sql, @bind) = @$v;
+ $self->_assert_bindval_matches_bindtype(@bind);
push @set, "$label = $sql";
- push @all_bind, $self->_bindtype($k, @bind);
+ push @all_bind, @bind;
}
},
ARRAYREFREF => sub { # literal SQL with bind
my ($sql, @bind) = @${$v};
+ $self->_assert_bindval_matches_bindtype(@bind);
push @set, "$label = $sql";
- push @all_bind, $self->_bindtype($k, @bind);
+ push @all_bind, @bind;
},
SCALARREF => sub { # literal SQL without bind
push @set, "$label = $$v";
@@ -536,18 +545,19 @@
($sql, @bind) = $self->_where_field_op_ARRAYREF($k, $op, $val);
},
- SCALARREF => sub { # CASE: col => {op => \$scalar}
+ SCALARREF => sub { # CASE: col => {op => \$scalar} (literal SQL without bind)
$sql = join ' ', $self->_convert($self->_quote($k)),
$self->_sqlcase($op),
$$val;
},
- ARRAYREFREF => sub { # CASE: col => {op => \[$sql, @bind]}
+ ARRAYREFREF => sub { # CASE: col => {op => \[$sql, @bind]} (literal SQL with bind)
my ($sub_sql, @sub_bind) = @$$val;
+ $self->_assert_bindval_matches_bindtype(@sub_bind);
$sql = join ' ', $self->_convert($self->_quote($k)),
$self->_sqlcase($op),
$sub_sql;
- @bind = $self->_bindtype($k, @sub_bind);
+ @bind = @sub_bind;
},
UNDEF => sub { # CASE: col => {op => undef} : sql "IS (NOT)? NULL"
@@ -613,15 +623,17 @@
return ($sql);
}
+# literal SQL with bind
sub _where_hashpair_ARRAYREFREF {
my ($self, $k, $v) = @_;
$self->_debug("REF($k) means literal SQL: @${$v}");
my ($sql, @bind) = @${$v};
+ $self->_assert_bindval_matches_bindtype(@bind);
$sql = $self->_quote($k) . " " . $sql;
- @bind = $self->_bindtype($k, @bind);
return ($sql, @bind );
}
+# literal SQL without bind
sub _where_hashpair_SCALAR {
my ($self, $k, $v) = @_;
$self->_debug("NOREF($k) means simple key=val: $k $self->{cmp} $v");
@@ -718,6 +730,7 @@
ARRAYREFREF => sub { # literal SQL with bind
my ($sql, @bind) = @$$vals;
+ $self->_assert_bindval_matches_bindtype(@bind);
return ("$label $op ( $sql )", @bind);
},
@@ -869,6 +882,20 @@
return $self->{bindtype} eq 'columns' ? map {[$col, $_]} @vals : @vals;
}
+# Dies if any element of @bind is not in [colname => value] format
+# if bindtype is 'columns'.
+sub _assert_bindval_matches_bindtype {
+ my ($self, @bind) = @_;
+
+ if ($self->{bindtype} eq 'columns') {
+ foreach my $val (@bind) {
+ if (!defined $val || ref($val) ne 'ARRAY' || @$val != 2) {
+ die "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
+ }
+ }
+ }
+}
+
sub _join_sql_clauses {
my ($self, $logic, $clauses_aref, $bind_aref) = @_;
@@ -979,13 +1006,13 @@
my $r = ref $v;
my $label = $self->_quote($k);
if ($r eq 'ARRAY') {
- # SQL included for values
- my @bind = @$v;
- my $sql = shift @bind;
+ # literal SQL with bind
+ my ($sql, @bind) = @$v;
+ $self->_assert_bindval_matches_bindtype(@bind);
push @sqlq, "$label = $sql";
- push @sqlv, $self->_bindtype($k, @bind);
+ push @sqlv, @bind;
} elsif ($r eq 'SCALAR') {
- # embedded literal SQL
+ # literal SQL without bind
push @sqlq, "$label = $$v";
} else {
push @sqlq, "$label = ?";
@@ -997,11 +1024,12 @@
# unlike insert(), assume these are ONLY the column names, i.e. for SQL
for my $v (@$_) {
my $r = ref $v;
- if ($r eq 'ARRAY') {
- my @val = @$v;
- push @sqlq, shift @val;
- push @sqlv, @val;
- } elsif ($r eq 'SCALAR') {
+ if ($r eq 'ARRAY') { # literal SQL with bind
+ my ($sql, @bind) = @$v;
+ $self->_assert_bindval_matches_bindtype(@bind);
+ push @sqlq, $sql;
+ push @sqlv, @bind;
+ } elsif ($r eq 'SCALAR') { # literal SQL without bind
# embedded literal SQL
push @sqlq, $$v;
} else {
Modified: SQL-Abstract/1.x/branches/1.50_RC/t/01generate.t
===================================================================
--- SQL-Abstract/1.x/branches/1.50_RC/t/01generate.t 2009-02-17 08:12:34 UTC (rev 5494)
+++ SQL-Abstract/1.x/branches/1.50_RC/t/01generate.t 2009-02-17 19:20:53 UTC (rev 5495)
@@ -4,6 +4,7 @@
use warnings;
use Test::More;
use Test::Warn;
+use Test::Exception;
use SQL::Abstract::Test import => ['is_same_sql_bind'];
@@ -406,38 +407,66 @@
{
func => 'insert',
new => {bindtype => 'columns'},
- args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
+ args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
stmt => 'INSERT INTO test (a, b) VALUES (?, to_date(?, \'MM/DD/YY\'))',
stmt_q => 'INSERT INTO `test` (`a`, `b`) VALUES (?, to_date(?, \'MM/DD/YY\'))',
- bind => [[a => '1'], [b => '02/02/02']],
+ bind => [[a => '1'], [dummy => '02/02/02']],
},
#45
{
func => 'update',
new => {bindtype => 'columns'},
- args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
+ args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, {a => {'between', [1,2]}}],
stmt => 'UPDATE test SET a = ?, b = to_date(?, \'MM/DD/YY\') WHERE ( a BETWEEN ? AND ? )',
stmt_q => 'UPDATE `test` SET `a` = ?, `b` = to_date(?, \'MM/DD/YY\') WHERE ( `a` BETWEEN ? AND ? )',
- bind => [[a => '1'], [b => '02/02/02'], [a => '1'], [a => '2']],
+ bind => [[a => '1'], [dummy => '02/02/02'], [a => '1'], [a => '2']],
},
#46
{
func => 'select',
new => {bindtype => 'columns'},
- args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
+ args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}],
stmt => q{SELECT * FROM test WHERE ( a = to_date(?, 'MM/DD/YY') )},
stmt_q => q{SELECT * FROM `test` WHERE ( `a` = to_date(?, 'MM/DD/YY') )},
- bind => [[a => '02/02/02']],
+ bind => [[dummy => '02/02/02']],
},
#47
{
func => 'select',
new => {bindtype => 'columns'},
- args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
+ args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", [dummy => '02/02/02']]}, b => 8 }],
stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
- bind => [[a => '02/02/02'], [b => 8]],
+ bind => [[dummy => '02/02/02'], [b => 8]],
},
+ #48
+ {
+ func => 'insert',
+ new => {bindtype => 'columns'},
+ args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}],
+ exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
+ },
+ #49
+ {
+ func => 'update',
+ new => {bindtype => 'columns'},
+ args => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, {a => {'between', [1,2]}}],
+ exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
+ },
+ #49
+ {
+ func => 'select',
+ new => {bindtype => 'columns'},
+ args => ['test', '*', { a => \["= to_date(?, 'MM/DD/YY')", '02/02/02']}],
+ exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
+ },
+ #50
+ {
+ func => 'select',
+ new => {bindtype => 'columns'},
+ args => ['test', '*', { a => {'<' => \["to_date(?, 'MM/DD/YY')", '02/02/02']}, b => 8 }],
+ exception_like => qr/bindtype 'columns' selected, you need to pass: \[column_name => bind_value\]/,
+ },
);
@@ -459,12 +488,16 @@
my $test = sub {
($stmt, @bind) = $sql->$func(@{$_->{args}})
};
- if ($_->{warning_like}) {
- warning_like { &$test } $_->{warning_like}, "throws the expected warning ($_->{warning_like})";
+ if ($_->{exception_like}) {
+ throws_ok { &$test } $_->{exception_like}, "throws the expected exception ($_->{exception_like})";
} else {
- &$test;
+ if ($_->{warning_like}) {
+ warning_like { &$test } $_->{warning_like}, "throws the expected warning ($_->{warning_like})";
+ } else {
+ &$test;
+ }
+ is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
}
- is_same_sql_bind($stmt, \@bind, $_->{stmt}, $_->{bind});
}
# test with quoted labels
@@ -476,12 +509,16 @@
my $test = sub {
($stmt_q, @bind_q) = $sql_q->$func_q(@{$_->{args}})
};
- if ($_->{warning_like}) {
- warning_like { &$test } $_->{warning_like}, "throws the expected warning ($_->{warning_like})";
+ if ($_->{exception_like}) {
+ throws_ok { &$test } $_->{exception_like}, "throws the expected exception ($_->{exception_like})";
} else {
- &$test;
+ if ($_->{warning_like}) {
+ warning_like { &$test } $_->{warning_like}, "throws the expected warning ($_->{warning_like})";
+ } else {
+ &$test;
+ }
+
+ is_same_sql_bind($stmt_q, \@bind_q, $_->{stmt_q}, $_->{bind});
}
-
- is_same_sql_bind($stmt_q, \@bind_q, $_->{stmt_q}, $_->{bind});
}
}
More information about the Bast-commits
mailing list