Fix INSERT RETURNING with DEFAULT VALUES for SQL Server wip/mssql-insert-returning
Dagfinn Ilmari Mannsåker [Sat, 20 Jul 2013 22:14:41 +0000 (23:14 +0100)]
There's no need to duplicate the RETURNING logic for the DEFAULT VALUES
case, ::SQLMaker only does that because it delegates the normal case to
SQL::Abstract.

Also build the SQL the right way around in the first place, instead of
building it wrong and then regex-mangling it.

lib/DBIx/Class/SQLMaker/MSSQL.pm

index 943bf4d..1a61297 100644 (file)
@@ -20,32 +20,23 @@ sub insert {
   my $data    = shift || return;
   my $options = shift;
 
-  if (! $data or (ref $data eq 'HASH' and !keys %{$data} ) ) {
-    my @bind;
-    my $sql = sprintf(
-      'INSERT INTO %s DEFAULT VALUES', $_[0]->_quote($table)
-    );
-
-    if ( ($options||{})->{returning} ) {
-      my $s;
-      ($s, @bind) = $self->_insert_returning ($options);
-      $sql .= $s;
-    }
+  my ($sql, @bind);
 
-    return ($sql, @bind);
+  if (! $data or (ref $data eq 'HASH' and !keys %{$data} ) ) {
+    $sql = $self->_sqlcase('default values');
+  } else {
+    my $method = $self->_METHOD_FOR_refkind("_insert", $data);
+    ($sql, @bind) = $self->$method($data);
   }
 
-  my $method = $self->_METHOD_FOR_refkind("_insert", $data);
-  my ($sql, @bind) = $self->$method($data);
-
-  $sql = join " ", $self->_sqlcase('insert into'), $table, $sql;
-
-  if ($options->{returning}) {
+  if ( ($options||{})->{returning} ) {
     my ($s, @b) = $self->_insert_returning ($options);
-    $sql =~ s/\bVALUES\b/$s VALUES/;
+    $sql = join ' ', $s, $sql;
     @bind = (@b, @bind);
   }
 
+  $sql = join " ", $self->_sqlcase('insert into'), $table, $sql;
+
   return wantarray ? ($sql, @bind) : $sql;
 }