[Bast-commits] r8463 - in SQL-Abstract/1.x/trunk: lib/SQL lib/SQL/Abstract t

ribasushi at dev.catalyst.perl.org ribasushi at dev.catalyst.perl.org
Thu Jan 28 11:14:12 GMT 2010


Author: ribasushi
Date: 2010-01-28 11:14:12 +0000 (Thu, 28 Jan 2010)
New Revision: 8463

Modified:
   SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm
   SQL-Abstract/1.x/trunk/lib/SQL/Abstract/Test.pm
   SQL-Abstract/1.x/trunk/t/01generate.t
Log:
optional functionality for INSERT
implementation of INSERT ... RETURNING 

Modified: SQL-Abstract/1.x/trunk/lib/SQL/Abstract/Test.pm
===================================================================
--- SQL-Abstract/1.x/trunk/lib/SQL/Abstract/Test.pm	2010-01-28 11:03:20 UTC (rev 8462)
+++ SQL-Abstract/1.x/trunk/lib/SQL/Abstract/Test.pm	2010-01-28 11:14:12 UTC (rev 8463)
@@ -49,6 +49,7 @@
   'UNION',
   'INTERSECT',
   'EXCEPT',
+  'RETURNING',
 );
 
 # These are binary operator keywords always a single LHS and RHS

Modified: SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm
===================================================================
--- SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm	2010-01-28 11:03:20 UTC (rev 8462)
+++ SQL-Abstract/1.x/trunk/lib/SQL/Abstract.pm	2010-01-28 11:14:12 UTC (rev 8463)
@@ -109,13 +109,24 @@
 #======================================================================
 
 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 @@
 
 =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 @@
 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 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<RETURNING> statement at the end.
+This allows you to return data generated by the insert statement
+(such as row IDs) without performing another C<SELECT> statement.
+Note, however, this is not part of the SQL standard and may not
+be supported by all database engines.
+
+=back
+
 =head2 update($table, \%fieldvals, \%where)
 
 This takes a table, hashref of field/value pairs, and an optional
@@ -2636,6 +2664,7 @@
     Laurent Dami (internal refactoring, multiple -nest, extensible list of special operators, literal SQL)
     Norbert Buchmuller (support for literal SQL in hashpair, misc. fixes & tests)
     Peter Rabbitson (rewrite of SQLA::Test, misc. fixes & tests)
+    Oliver Charles (support for "RETURNING" after "INSERT")
 
 Thanks!
 

Modified: SQL-Abstract/1.x/trunk/t/01generate.t
===================================================================
--- SQL-Abstract/1.x/trunk/t/01generate.t	2010-01-28 11:03:20 UTC (rev 8462)
+++ SQL-Abstract/1.x/trunk/t/01generate.t	2010-01-28 11:14:12 UTC (rev 8463)
@@ -544,6 +544,46 @@
               stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
               bind   => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
       },
+      #60
+      {
+              func   => 'insert',
+              args   => ['test', [qw/1 2 3 4 5/], { returning => 'id' }],
+              stmt   => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
+              stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`',
+              bind   => [qw/1 2 3 4 5/],
+      },
+      #60
+      {
+              func   => 'insert',
+              args   => ['test', [qw/1 2 3 4 5/], { returning => 'id, foo, bar' }],
+              stmt   => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
+              stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id, foo, bar`',
+              bind   => [qw/1 2 3 4 5/],
+      },
+      #61
+      {
+              func   => 'insert',
+              args   => ['test', [qw/1 2 3 4 5/], { returning => [qw(id  foo  bar) ] }],
+              stmt   => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
+              stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING `id`, `foo`, `bar`',
+              bind   => [qw/1 2 3 4 5/],
+      },
+      #62
+      {
+              func   => 'insert',
+              args   => ['test', [qw/1 2 3 4 5/], { returning => \'id, foo, bar' }],
+              stmt   => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
+              stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id, foo, bar',
+              bind   => [qw/1 2 3 4 5/],
+      },
+      #63
+      {
+              func   => 'insert',
+              args   => ['test', [qw/1 2 3 4 5/], { returning => \'id' }],
+              stmt   => 'INSERT INTO test VALUES (?, ?, ?, ?, ?) RETURNING id',
+              stmt_q => 'INSERT INTO `test` VALUES (?, ?, ?, ?, ?) RETURNING id',
+              bind   => [qw/1 2 3 4 5/],
+      },
 );
 
 




More information about the Bast-commits mailing list