[Bast-commits] r7728 - in DBIx-Class/0.08/branches/sybase_bulkinsert_support: lib/DBIx/Class/Storage/DBI t

caelum at dev.catalyst.perl.org caelum at dev.catalyst.perl.org
Thu Sep 24 13:57:59 GMT 2009


Author: caelum
Date: 2009-09-24 13:57:58 +0000 (Thu, 24 Sep 2009)
New Revision: 7728

Modified:
   DBIx-Class/0.08/branches/sybase_bulkinsert_support/lib/DBIx/Class/Storage/DBI/Sybase.pm
   DBIx-Class/0.08/branches/sybase_bulkinsert_support/t/746sybase.t
Log:
fix insert with all defaults

Modified: DBIx-Class/0.08/branches/sybase_bulkinsert_support/lib/DBIx/Class/Storage/DBI/Sybase.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase_bulkinsert_support/lib/DBIx/Class/Storage/DBI/Sybase.pm	2009-09-24 12:45:04 UTC (rev 7727)
+++ DBIx-Class/0.08/branches/sybase_bulkinsert_support/lib/DBIx/Class/Storage/DBI/Sybase.pm	2009-09-24 13:57:58 UTC (rev 7728)
@@ -347,12 +347,22 @@
   my $self = shift;
   my ($source, $to_insert) = @_;
 
+  my $identity_col = (List::Util::first
+    { $source->column_info($_)->{is_auto_increment} }
+    $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);
 
-  my $identity_col = List::Util::first
-    { $source->column_info($_)->{is_auto_increment} }
-    $source->columns;
-
   # do we need the horrific SELECT MAX(COL) hack?
   my $dumb_last_insert_id =
        $identity_col
@@ -392,7 +402,8 @@
   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,
   };

Modified: DBIx-Class/0.08/branches/sybase_bulkinsert_support/t/746sybase.t
===================================================================
--- DBIx-Class/0.08/branches/sybase_bulkinsert_support/t/746sybase.t	2009-09-24 12:45:04 UTC (rev 7727)
+++ DBIx-Class/0.08/branches/sybase_bulkinsert_support/t/746sybase.t	2009-09-24 13:57:58 UTC (rev 7728)
@@ -12,7 +12,7 @@
 
 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 @@
     } '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 @@
   }
 
 # Now test money values.
-  my $rs = $schema->resultset('Money');
-
   my $row;
   lives_ok {
     $row = $rs->create({ amount => 100 });




More information about the Bast-commits mailing list