From: Dagfinn Ilmari Mannsåker Date: Sat, 20 Jul 2013 22:14:41 +0000 (+0100) Subject: Fix INSERT RETURNING with DEFAULT VALUES for SQL Server X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=449e2dc1868543468a89800bb6a4c406087b36af;p=dbsrgits%2FDBIx-Class.git Fix INSERT RETURNING with DEFAULT VALUES for SQL Server 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. --- diff --git a/lib/DBIx/Class/SQLMaker/MSSQL.pm b/lib/DBIx/Class/SQLMaker/MSSQL.pm index 943bf4d..1a61297 100644 --- a/lib/DBIx/Class/SQLMaker/MSSQL.pm +++ b/lib/DBIx/Class/SQLMaker/MSSQL.pm @@ -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; }