From: Lasse Makholm Date: Mon, 29 Sep 2014 16:37:32 +0000 (+0200) Subject: Fix default insert on Oracle ( $rs->create({}) with empty hashref ) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7392e32b3c9d044ebe4caf301bc72bc4ba6dc413;p=dbsrgits%2FDBIx-Class.git Fix default insert on Oracle ( $rs->create({}) with empty hashref ) Oracle does not support the INSERT INTO ... DEFAULT VALUES syntax used by SQLMaker by default. Furthermore, Oracle has no equivalent way of inserting a row without specifying any columns, so the only reasonably fix seems to be to pick the first column and supply the DEFAULT keyword as value for it. --- diff --git a/AUTHORS b/AUTHORS index 180d485..350654c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -105,6 +105,7 @@ jshirley: J. Shirley kaare: Kaare Rasmussen kd: Kieren Diment konobi: Scott McWhirter +Lasse Makholm lejeunerenard: Sean Zellmer littlesavage: Alexey Illarionov lukes: Luke Saunders diff --git a/Changes b/Changes index a65fd89..547e8cb 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for DBIx::Class * Fixes + - Fix $rs->create() with no values on Oracle - Silence with_deferred_fk_checks() warning on PostgreSQL 9.4 * Misc diff --git a/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm b/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm index 2b4ce75..a90755c 100644 --- a/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm +++ b/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm @@ -115,6 +115,21 @@ sub deployment_statements { $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest); } +sub insert { + my ($self, $source, $to_insert) = @_; + + # Oracle does not understand INSERT INTO ... DEFAULT VALUES syntax + # Furthermore it does not have any way to insert without specifying any columns + # We can't fix this in SQLMaker::Oracle because it doesn't know which column to add to the statement + unless (%$to_insert) + { + my ($col) = $source->columns; + $to_insert = { $col => \'DEFAULT' }; + } + + return $self->next::method($source, $to_insert); +} + sub _dbh_last_insert_id { my ($self, $dbh, $source, @columns) = @_; my @ids = (); diff --git a/t/60core.t b/t/60core.t index 1f7e5f9..d848a91 100644 --- a/t/60core.t +++ b/t/60core.t @@ -576,6 +576,9 @@ lives_ok (sub { my $newlink = $newbook->link}, "stringify to false value doesn't { my $new_artist = $schema->resultset('Artist')->new({}); isa_ok( $new_artist, 'DBIx::Class::Row', '$rs->new gives a row object' ); + lives_ok { $new_artist->insert() } 'inserting without specifying any columns works'; + $new_artist->discard_changes; + $new_artist->delete; } # make sure we got rid of the compat shims diff --git a/t/73oracle.t b/t/73oracle.t index a8a1e70..f6da1a1 100644 --- a/t/73oracle.t +++ b/t/73oracle.t @@ -201,6 +201,13 @@ sub _run_tests { like ($seq, qr/\.${q}artist_pk_seq${q}$/, 'Correct PK sequence selected for sqlt-like trigger'); } + lives_ok { + $new = $schema->resultset('Artist')->create({}); + $new->discard_changes; + ok $new->artistid, 'Created row has id' + } 'Create with empty hashref works'; + + # test LIMIT support for (1..6) { $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });