Merge 'sybase_insert_bulk' into 'sybase_support'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
index a11c24f..9851303 100644 (file)
@@ -18,6 +18,9 @@ __PACKAGE__->mk_group_accessors('simple' =>
 );
 
 my @also_proxy_to_writer_storage = qw/
+  connect_call_set_auto_cast auto_cast connect_call_blob_setup
+  connect_call_datetime_setup
+
   disconnect _connect_info _sql_maker _sql_maker_opts disable_sth_caching
   auto_savepoint unsafe cursor_class debug debugobj schema
 /;
@@ -124,12 +127,14 @@ sub _init {
 
   $writer_storage->_is_writer_storage(1);
   $writer_storage->connect_info($self->connect_info);
+  $writer_storage->auto_cast($self->auto_cast);
 
   $self->_writer_storage($writer_storage);
 }
 
 for my $method (@also_proxy_to_writer_storage) {
   no strict 'refs';
+  no warnings 'redefine';
 
   my $replaced = __PACKAGE__->can($method);
 
@@ -195,6 +200,12 @@ sub _is_lob_type {
   $type && $type =~ /(?:text|image|lob|bytea|binary|memo)/i;
 }
 
+sub _is_lob_column {
+  my ($self, $source, $column) = @_;
+
+  return $self->_is_lob_type($source->column_info($column)->{data_type});
+}
+
 sub _prep_for_execute {
   my $self = shift;
   my ($op, $extra_bind, $ident, $args) = @_;
@@ -352,7 +363,7 @@ sub _insert {
 
 sub update {
   my $self = shift;
-  my ($source, $fields, $where) = @_;
+  my ($source, $fields, $where, @rest) = @_;
 
   my $wantarray = wantarray;
 
@@ -372,73 +383,51 @@ sub update {
     $self->_unset_identity_insert($table, 'update') if $is_identity_update;
   }
 
-# check if condition and fields allow for a 2-step update
-  $self->_assert_blob_update_possible($source, $fields, $where);
+# check that we're not updating a blob column that's also in $where
+  for my $blob (grep $self->_is_lob_column($source, $_), $source->columns) {
+    if (exists $where->{$blob} && exists $fields->{$blob}) {
+      croak
+'Update of TEXT/IMAGE column that is also in search condition impossible';
+    }
+  }
 
 # update+blob update(s) done atomically on separate connection
   $self = $self->_writer_storage;
 
   my $guard = $self->txn_scope_guard;
 
-  $self->_set_identity_insert($table, 'update')   if $is_identity_update;
+# First update the blob columns to be updated to '' (taken from $fields, where
+# it is originally put by _remove_blob_cols .)
+  my %blobs_to_empty = map { ($_ => delete $fields->{$_}) } keys %$blob_cols;
 
-  my @res;
-  if ($wantarray) {
-    @res    = $self->next::method(@_);
-  }
-  elsif (defined $wantarray) {
-    $res[0] = $self->next::method(@_);
-  }
-  else {
-    $self->next::method(@_);
-  }
+  $self->next::method($source, \%blobs_to_empty, $where, @rest);
 
-  $self->_unset_identity_insert($table, 'update') if $is_identity_update;
+# Now update the blobs before the other columns in case the update of other
+# columns makes the search condition invalid.
+  $self->_update_blobs($source, $blob_cols, $where);
 
-  my %new_where = map { $_ => ($fields->{$_} || $where->{$_}) } keys %$where;
+  my @res;
+  if (%$fields) {
+    $self->_set_identity_insert($table, 'update')   if $is_identity_update;
 
-  $self->_update_blobs($source, $blob_cols, \%new_where);
+    if ($wantarray) {
+      @res    = $self->next::method(@_);
+    }
+    elsif (defined $wantarray) {
+      $res[0] = $self->next::method(@_);
+    }
+    else {
+      $self->next::method(@_);
+    }
+
+    $self->_unset_identity_insert($table, 'update') if $is_identity_update;
+  }
 
   $guard->commit;
 
   return $wantarray ? @res : $res[0];
 }
 
-sub _assert_blob_update_possible {
-  my ($self, $source, $fields, $where) = @_;
-
-  my $table = $source->name;
-
-# If $where condition is mutually exclusive from $fields (what gets updated)
-# then update is safe.
-  my %count;
-  $count{$_}++ foreach keys %$where, keys %$fields;
-  return 1 unless List::Util::first { $_ == 2 } values %count;
-
-# Otherwise check that what is updated includes either a primary or unique key.
-  my (@primary_cols) = $source->primary_columns;
-  return 1 if (grep exists $fields->{$_}, @primary_cols) == @primary_cols;
-
-  my %unique_constraints = $source->unique_constraints;
-  for my $uniq_constr (values %unique_constraints) {
-    return 1 if (grep exists $fields->{$_}, @$uniq_constr) == @$uniq_constr;
-  }
-
-# otherwise throw exception
-  require Data::Dumper;
-  local $Data::Dumper::Terse = 1;
-  local $Data::Dumper::Indent = 1;
-  local $Data::Dumper::Useqq = 1;
-  local $Data::Dumper::Quotekeys = 0;
-  local $Data::Dumper::Sortkeys = 1;
-
-  croak sprintf
-"2-step TEXT/IMAGE update on table '$table' impossible for condition: \n%s\n".
-"Setting columns: \n%s\n",
-    Data::Dumper::Dumper($where),
-    Data::Dumper::Dumper($fields);
-}
-
 ### the insert_bulk stuff stolen from DBI/MSSQL.pm
 
 sub _set_identity_insert {
@@ -483,6 +472,9 @@ sub _unset_identity_insert {
   $self->_query_end($sql);
 }
 
+# for tests
+sub _can_insert_bulk { 1 }
+
 # XXX this should use the DBD::Sybase bulk API, where possible
 sub insert_bulk {
   my $self = shift;
@@ -508,6 +500,8 @@ sub insert_bulk {
 
 ### end of stolen insert_bulk section
 
+# Make sure blobs are not bound as placeholders, and return any non-empty ones
+# as a hash.
 sub _remove_blob_cols {
   my ($self, $source, $fields) = @_;
 
@@ -515,8 +509,14 @@ sub _remove_blob_cols {
 
   for my $col (keys %$fields) {
     if ($self->_is_lob_type($source->column_info($col)->{data_type})) {
-      $blob_cols{$col} = delete $fields->{$col};
-      $fields->{$col} = \"''";
+      my $blob_val = delete $fields->{$col};
+      if (not defined $blob_val) {
+        $fields->{$col} = \'NULL';
+      }
+      else {
+        $fields->{$col} = \"''";
+        $blob_cols{$col} = $blob_val unless $blob_val eq '';
+      }
     }
   }
 
@@ -528,7 +528,7 @@ sub _update_blobs {
 
   my (@primary_cols) = $source->primary_columns;
 
-  croak "Cannot update TEXT/IMAGE column(s) without a primary key"
+  $self->throw_exception('Cannot update TEXT/IMAGE column(s) without a primary key')
     unless @primary_cols;
 
 # check if we're updating a single row by PK
@@ -563,12 +563,11 @@ sub _insert_blobs {
   my %row = %$row;
   my (@primary_cols) = $source->primary_columns;
 
-  croak "Cannot update TEXT/IMAGE column(s) without a primary key"
+  $self->throw_exception('Cannot update TEXT/IMAGE column(s) without a primary key')
     unless @primary_cols;
 
-  if ((grep { defined $row{$_} } @primary_cols) != @primary_cols) {
-    croak "Cannot update TEXT/IMAGE column(s) without primary key values";
-  }
+  $self->throw_exception('Cannot update TEXT/IMAGE column(s) without primary key values')
+    if ((grep { defined $row{$_} } @primary_cols) != @primary_cols);
 
   for my $col (keys %$blob_cols) {
     my $blob = $blob_cols->{$col};
@@ -580,15 +579,11 @@ sub _insert_blobs {
     my $sth = $cursor->sth;
 
     if (not $sth) {
-      require Data::Dumper;
-      local $Data::Dumper::Terse = 1;
-      local $Data::Dumper::Indent = 1;
-      local $Data::Dumper::Useqq = 1;
-      local $Data::Dumper::Quotekeys = 0;
-      local $Data::Dumper::Sortkeys = 1;
-
-      croak "\nCould not find row in table '$table' for blob update:\n".
-        Data::Dumper::Dumper(\%where)."\n";
+
+      $self->throw_exception(
+          "Could not find row in table '$table' for blob update:\n"
+        . $self->_pretty_print (\%where)
+      );
     }
 
     eval {
@@ -614,12 +609,12 @@ sub _insert_blobs {
     $sth->finish if $sth;
     if ($exception) {
       if ($self->using_freetds) {
-        croak (
+        $self->throw_exception (
           'TEXT/IMAGE operation failed, probably because you are using FreeTDS: '
           . $exception
         );
       } else {
-        croak $exception;
+        $self->throw_exception($exception);
       }
     }
   }