$sql = join " ", $self->_sqlcase('insert into'), $table, $sql;
if ($options->{returning}) {
- my ($s, @b) = $self->_insert_returning ($options);
+ my ($s, @b) = $self->_returning ($options);
$sql .= $s;
push @bind, @b;
}
return wantarray ? ($sql, @bind) : $sql;
}
-sub _insert_returning {
+sub _returning {
my ($self, $options) = @_;
my $f = $options->{returning};
sub update {
- my $self = shift;
- my $table = $self->_table(shift);
- my $data = shift || return;
- my $where = shift;
+ my $self = shift;
+ my $table = $self->_table(shift);
+ my $data = shift || return;
+ my $where = shift;
+ my $options = shift;
# first build the 'SET' part of the sql statement
my (@set, @all_bind);
push @all_bind, @where_bind;
}
+ if ($options->{returning}) {
+ my ($returning_sql, @returning_bind) = $self->_returning ($options);
+ $sql .= $returning_sql;
+ push @all_bind, @returning_bind;
+ }
+
return wantarray ? ($sql, @all_bind) : $sql;
}
=back
-=head2 update($table, \%fieldvals, \%where)
+=head2 update($table, \%fieldvals, \%where, \%options)
This takes a table, hashref of field/value pairs, and an optional
hashref L<WHERE clause|/WHERE CLAUSES>. It returns an SQL UPDATE function and a list
L</"Inserting and Updating SQL"> for information on how to insert
with those data types.
+The optional C<\%options> hash reference may contain additional
+options to generate the update SQL. Currently supported options
+are:
+
+=over 4
+
+=item returning
+
+See the C<returning> option to
+L<insert|/insert($table, \@values || \%fieldvals, \%options)>.
+
+=back
+
=head2 select($source, $fields, $where, $order)
This returns a SQL SELECT statement and associated list of bind values, as
esc => '\\',
bind => [],
},
+ {
+ func => 'update',
+ args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => 'id' }],
+ stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING id',
+ stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING `id`',
+ bind => [42, 32],
+ },
+ {
+ func => 'update',
+ args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => \'*' }],
+ stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING *',
+ stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING *',
+ bind => [42, 32],
+ },
+ {
+ func => 'update',
+ args => ['mytable', { foo => 42 }, { baz => 32 }, { returning => ['id','created_at'] }],
+ stmt => 'UPDATE mytable SET foo = ? WHERE baz = ? RETURNING id, created_at',
+ stmt_q => 'UPDATE `mytable` SET `foo` = ? WHERE `baz` = ? RETURNING `id`, `created_at`',
+ bind => [42, 32],
+ },
);
# check is( not) => undef