From: Peter Rabbitson Date: Thu, 28 Jan 2010 11:14:12 +0000 (+0000) Subject: optional functionality for INSERT X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=02288357062ef90241f9e09ca72cf3f2368da4e3;p=scpubgit%2FQ-Branch.git optional functionality for INSERT implementation of INSERT ... RETURNING --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index e8bb100..c1da181 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -109,13 +109,24 @@ sub new { #====================================================================== sub insert { - my $self = shift; - my $table = $self->_table(shift); - my $data = shift || return; + my $self = shift; + my $table = $self->_table(shift); + my $data = shift || return; + my $options = shift; my $method = $self->_METHOD_FOR_refkind("_insert", $data); - my ($sql, @bind) = $self->$method($data); + my ($sql, @bind) = $self->$method($data); $sql = join " ", $self->_sqlcase('insert into'), $table, $sql; + + if (my $fields = $options->{returning}) { + my $f = $self->_SWITCH_refkind($fields, { + ARRAYREF => sub {join ', ', map { $self->_quote($_) } @$fields;}, + SCALAR => sub {$self->_quote($fields)}, + SCALARREF => sub {$$fields}, + }); + $sql .= join " ", $self->_sqlcase(' returning'), $f; + } + return wantarray ? ($sql, @bind) : $sql; } @@ -1675,7 +1686,7 @@ See section L for details. =back -=head2 insert($table, \@values || \%fieldvals) +=head2 insert($table, \@values || \%fieldvals, \%options) This is the simplest function. You simply give it a table name and either an arrayref of values or hashref of field/value pairs. @@ -1684,6 +1695,23 @@ See the sections on L and L for information on how to insert with those data types. +The optional C<\%options> hash reference may contain additional +options to generate the insert SQL. Currently supported options +are: + +=over 4 + +=item returning + +Takes either a scalar of raw SQL fields, or an array reference of +field names, and adds on an SQL C statement at the end. +This allows you to return data generated by the insert statement +(such as row IDs) without performing another C