From: Rafael Kitover Date: Sun, 7 Mar 2010 10:46:02 +0000 (+0000) Subject: fix RETURNING for empty INSERT X-Git-Tag: v0.08121~73^2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=commitdiff_plain;h=28d2890368880d8e0c39df0be2609bac1a9e1f44;hp=d314b84d347d3340bd83a9889c9a171786cdfc70 fix RETURNING for empty INSERT --- diff --git a/Changes b/Changes index 4495626..536a54b 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for DBIx::Class + - Support for Firebird RDBMS with DBD::InterBase and ODBC - DBIx::Class::InflateColumn::File entered deprecated state - DBIx::Class::Optional::Dependencies left experimental state - Add req_group_list to Opt::Deps (RT#55211) diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index 13f0d97..d2cd569 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -360,7 +360,14 @@ sub insert { # which is sadly understood only by MySQL. Change default behavior here, # until SQLA2 comes with proper dialect support if (! $_[0] or (ref $_[0] eq 'HASH' and !keys %{$_[0]} ) ) { - return "INSERT INTO ${table} DEFAULT VALUES" + my $sql = "INSERT INTO ${table} DEFAULT VALUES"; + + if (my @returning = @{ ($_[1]||{})->{returning} || [] }) { + $sql .= ' RETURNING (' . (join ', ' => map $self->_quote($_), @returning) + . ')'; + } + + return $sql; } $self->SUPER::insert($table, @_); diff --git a/lib/DBIx/Class/Storage/DBI/InterBase.pm b/lib/DBIx/Class/Storage/DBI/InterBase.pm index 8002b4a..a416fa7 100644 --- a/lib/DBIx/Class/Storage/DBI/InterBase.pm +++ b/lib/DBIx/Class/Storage/DBI/InterBase.pm @@ -42,6 +42,7 @@ sub _prep_for_execute { if ($op eq 'insert') { $self->_auto_incs([]); + my %pk; @pk{$ident->primary_columns} = (); my @auto_inc_cols = grep { diff --git a/t/750firebird.t b/t/750firebird.t index 55ba209..7e4db43 100644 --- a/t/750firebird.t +++ b/t/750firebird.t @@ -14,7 +14,11 @@ my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_FIREBIRD_ODBC_${_}" } qw/DSN U plan skip_all => <<'EOF' unless $dsn || $dsn2; Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN}, -_USER and _PASS to run these tests +_USER and _PASS to run these tests. + +WARNING: this test creates and drops the tables "artist", "bindtype_test" and +"sequence_test"; the generators "gen_artist_artistid", "pkid1_seq", "pkid2_seq" +and "nonpkid_seq" and the trigger "artist_bi". EOF my @info = ( @@ -190,11 +194,20 @@ EOF } # test empty insert + lives_and { + my $row = $ars->create({}); + ok $row->artistid; + } 'empty insert works'; + +# test inferring the generator from the trigger source and using it with +# auto_nextval { - local $ars->result_source->column_info('artistid')->{is_auto_increment} = 0; + local $ars->result_source->column_info('artistid')->{auto_nextval} = 1; - lives_ok { $ars->create({}) } - 'empty insert works'; + lives_and { + my $row = $ars->create({ name => 'introspecting generator' }); + ok $row->artistid; + } 'inferring generator from trigger source works'; } # test blobs (stolen from 73oracle.t)