sub insert {
my $self = shift;
- my $table = $self->_table(shift);
+ my ($table_sql, @all_bind) = $self->_table(shift);
my $data = shift || return;
my $options = shift;
my $method = $self->_METHOD_FOR_refkind("_insert", $data);
- my ($sql, @bind) = $self->$method($data);
- $sql = join " ", $self->_sqlcase('insert into'), $table, $sql;
+ my ($values_sql, @values_bind) = $self->$method($data);
+ my $sql = join " ", $self->_sqlcase('insert into'), $table_sql, $values_sql;
+ push @all_bind, @values_bind;
if ($options->{returning}) {
- my ($s, @b) = $self->_insert_returning($options);
- $sql .= $s;
- push @bind, @b;
+ my ($returning_sql, @returning_bind) = $self->_insert_returning($options);
+ $sql .= $returning_sql;
+ push @all_bind, @returning_bind;
}
- return wantarray ? ($sql, @bind) : $sql;
+ return wantarray ? ($sql, @all_bind) : $sql;
}
# So that subclasses can override INSERT ... RETURNING separately from
sub update {
my $self = shift;
- my $table = $self->_table(shift);
+ my ($table_sql, @all_bind) = $self->_table(shift);
my $data = shift || return;
my $where = shift;
my $options = shift;
puke "Unsupported data type specified to \$sql->update"
unless ref $data eq 'HASH';
- my ($sql, @all_bind) = $self->_update_set_values($data);
- $sql = $self->_sqlcase('update ') . $table . $self->_sqlcase(' set ')
- . $sql;
+ my ($values_sql, @values_bind) = $self->_update_set_values($data);
+ my $sql = $self->_sqlcase('update ') . $table_sql . $self->_sqlcase(' set ')
+ . $values_sql;
+ push @all_bind, @values_bind;
if ($where) {
my($where_sql, @where_bind) = $self->where($where);
sub select {
my $self = shift;
- my $table = $self->_table(shift);
+ my ($table_sql, @table_bind) = $self->_table(shift);
my $fields = shift || '*';
my $where = shift;
my $order = shift;
- my($where_sql, @bind) = $self->where($where, $order);
+ my($where_sql, @where_bind) = $self->where($where, $order);
my $f = (ref $fields eq 'ARRAY') ? join ', ', map { $self->_quote($_) } @$fields
: $fields;
my $sql = join(' ', $self->_sqlcase('select'), $f,
- $self->_sqlcase('from'), $table)
+ $self->_sqlcase('from'), $table_sql)
. $where_sql;
- return wantarray ? ($sql, @bind) : $sql;
+ return wantarray ? ($sql, @table_bind, @where_bind) : $sql;
}
#======================================================================
sub delete {
my $self = shift;
- my $table = $self->_table(shift);
+ my ($table_sql, @all_bind) = $self->_table(shift);
my $where = shift;
my $options = shift;
- my($where_sql, @bind) = $self->where($where);
- my $sql = $self->_sqlcase('delete from ') . $table . $where_sql;
+ my($where_sql, @where_bind) = $self->where($where);
+ my $sql = $self->_sqlcase('delete from ') . $table_sql . $where_sql;
+ push @all_bind, @where_bind;
if ($options->{returning}) {
my ($returning_sql, @returning_bind) = $self->_delete_returning($options);
$sql .= $returning_sql;
- push @bind, @returning_bind;
+ push @all_bind, @returning_bind;
}
- return wantarray ? ($sql, @bind) : $sql;
+ return wantarray ? ($sql, @all_bind) : $sql;
}
# So that subclasses can override DELETE ... RETURNING separately from
my $from = shift;
$self->_SWITCH_refkind($from, {
ARRAYREF => sub {join ', ', map { $self->_quote($_) } @$from;},
+ ARRAYREFREF => sub {
+ my ($sql, @bind) = @$$from;
+ $self->_assert_bindval_matches_bindtype(@bind);
+ return ($sql, @bind);
+ },
SCALAR => sub {$self->_quote($from)},
SCALARREF => sub {$$from},
});
The argument can be either a plain scalar (interpreted as a table
name, will be quoted), or an arrayref (interpreted as a list
of table names, joined by commas, quoted), or a scalarref
-(literal SQL, not quoted).
+(literal SQL, not quoted), or a reference to an arrayref
+(literal SQL and bind values).
=item $fields
stmt_q => 'DELETE FROM `test` WHERE ( `requestor` IS NULL ) RETURNING `id`, `created_at`',
bind => []
},
+ {
+ func => 'select',
+ args => [\['my_srf(?)', 42], [qw(foo bar)], {foo => 37}],
+ stmt => 'select foo, bar from my_srf(?) where foo = ?',
+ stmt_q => 'select `foo`, `bar` from my_srf(?) where `foo` = ?',
+ bind => [42, 37],
+ },
+ {
+ func => 'delete',
+ args => [\['my_srf(?)', 42], {foo => 37}],
+ stmt => 'delete from my_srf(?) where foo = ?',
+ stmt_q => 'delete from my_srf(?) where `foo` = ?',
+ bind => [42, 37],
+ },
+ {
+ func => 'update',
+ args => [\['my_srf(?)', 42], {foo => 37}, {bar => 53}],
+ stmt => 'update my_srf(?) set foo = ? where bar = ?',
+ stmt_q => 'update my_srf(?) set `foo` = ? where `bar` = ?',
+ bind => [42, 37, 53],
+ },
);
# check is( not) => undef