From: Lasse Makholm Date: Mon, 29 Sep 2014 16:37:32 +0000 (+0200) Subject: Unbreak $rs->create() with empty hashref on Oracle X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=236b59c8;p=dbsrgits%2FDBIx-Class-Historic.git Unbreak $rs->create() with empty hashref on Oracle Oracle does not support the INSERT INTO ... DEFAULT VALUES syntax used by SQLMaker by default. Furthermore, Oracle has no way of inserting a row without specifying any columns, so the only reasonably fix seems to be to pick a column and supply the DEFAULT keyword as value for it. This approach seems to work without problems, even for things like sequence+trigger-as-auto-increment. --- diff --git a/Changes b/Changes index 6676078..d75824c 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for DBIx::Class + * Fixes + - Fix $rs->create() with no values on Oracle + 0.082800 2014-09-25 14:45 (UTC) * Known Issues - Passing large amounts of objects with stringification overload 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 f92159b..7930fce 100644 --- a/t/60core.t +++ b/t/60core.t @@ -576,9 +576,11 @@ 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 SKIP: { my $remove_version = 0.083; diff --git a/t/73oracle.t b/t/73oracle.t index bdcc732..87be799 100644 --- a/t/73oracle.t +++ b/t/73oracle.t @@ -200,6 +200,12 @@ 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) {