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}]);
- }
-
- # 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]);
- }
- }
+ my ($sql, @bind) = $self->_insert_values($data);
# assemble SQL
$_ = $self->_quote($_) foreach @fields;
$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;
}
ARRAYREFREF => sub { # literal SQL with bind
my ($sql, @bind) = @${$v};
+ $self->_assert_bindval_matches_bindtype(@bind);
push @values, $sql;
push @all_bind, @bind;
},
#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
SCALAR_or_UNDEF => sub {
push @values, '?';
- push @all_bind, $v;
+ push @all_bind, $self->_bindtype($column, $v);
},
});
}
-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
}
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";
($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"
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");
ARRAYREFREF => sub { # literal SQL with bind
my ($sql, @bind) = @$$vals;
+ $self->_assert_bindval_matches_bindtype(@bind);
return ("$label $op ( $sql )", @bind);
},
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) = @_;
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 = ?";
# 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 {
use warnings;
use Test::More;
use Test::Warn;
+use Test::Exception;
use SQL::Abstract::Test import => ['is_same_sql_bind'];
{
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\]/,
},
);
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
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});
+ }
}
}