From: Rafael Kitover Date: Thu, 24 Sep 2009 13:57:58 +0000 (+0000) Subject: fix insert with all defaults X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cd048330042413dacf872868cda3cbad40d083ac;p=dbsrgits%2FDBIx-Class-Historic.git fix insert with all defaults --- diff --git a/lib/DBIx/Class/Storage/DBI/Sybase.pm b/lib/DBIx/Class/Storage/DBI/Sybase.pm index 5747cb9..e96d8fd 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase.pm @@ -347,11 +347,21 @@ sub insert { my $self = shift; my ($source, $to_insert) = @_; - my $blob_cols = $self->_remove_blob_cols($source, $to_insert); - - my $identity_col = List::Util::first + my $identity_col = (List::Util::first { $source->column_info($_)->{is_auto_increment} } - $source->columns; + $source->columns) || ''; + + # check for empty insert + # INSERT INTO foo DEFAULT VALUES -- does not work with Sybase + # try to insert explicit 'DEFAULT's instead (except for identity) + if (not %$to_insert) { + for my $col ($source->columns) { + next if $col eq $identity_col; + $to_insert->{$col} = \'DEFAULT'; + } + } + + my $blob_cols = $self->_remove_blob_cols($source, $to_insert); # do we need the horrific SELECT MAX(COL) hack? my $dumb_last_insert_id = @@ -392,7 +402,8 @@ sub _insert { my $updated_cols = $self->$next ($source, $to_insert); my $final_row = { - $identity_col => $self->last_insert_id($source, $identity_col), + ($identity_col ? + ($identity_col => $self->last_insert_id($source, $identity_col)) : ()), %$to_insert, %$updated_cols, }; diff --git a/t/746sybase.t b/t/746sybase.t index 81982af..de00366 100644 --- a/t/746sybase.t +++ b/t/746sybase.t @@ -12,7 +12,7 @@ require DBIx::Class::Storage::DBI::Sybase::NoBindVars; my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/}; -my $TESTS = 62 + 2; +my $TESTS = 63 + 2; if (not ($dsn && $user)) { plan skip_all => @@ -500,18 +500,27 @@ SQL } 'blob update to NULL'; } -# test MONEY column support +# test MONEY column support (and some other misc. stuff) $schema->storage->dbh_do (sub { my ($storage, $dbh) = @_; eval { $dbh->do("DROP TABLE money_test") }; $dbh->do(<<'SQL'); CREATE TABLE money_test ( id INT IDENTITY PRIMARY KEY, - amount MONEY NULL + amount MONEY DEFAULT $999.99 NULL ) SQL }); + my $rs = $schema->resultset('Money'); + +# test insert with defaults + lives_and { + $rs->create({}); + is((grep $_->amount == 999.99, $rs->all), 1); + } 'insert with all defaults works'; + $rs->delete; + # test insert transaction when there's an active cursor { my $artist_rs = $schema->resultset('Artist'); @@ -543,8 +552,6 @@ SQL } # Now test money values. - my $rs = $schema->resultset('Money'); - my $row; lives_ok { $row = $rs->create({ amount => 100 });