Cleanup exception handling
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
index f6073cb..2e01e8e 100644 (file)
@@ -195,12 +195,6 @@ 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) = @_;
@@ -358,32 +352,13 @@ sub _insert {
 
 sub update {
   my $self = shift;
-  my ($source, $fields, $where, @rest) = @_;
+  my ($source, $fields, $where) = @_;
 
   my $wantarray = wantarray;
-
   my $blob_cols = $self->_remove_blob_cols($source, $fields);
 
-  my $table = $source->name;
-
-  my $identity_col = List::Util::first
-    { $source->column_info($_)->{is_auto_increment} }
-    $source->columns;
-
-  my $is_identity_update = $identity_col && defined $fields->{$identity_col};
-
   if (not $blob_cols) {
-    $self->_set_identity_insert($table, 'update')   if $is_identity_update;
     return $self->next::method(@_);
-    $self->_unset_identity_insert($table, 'update') if $is_identity_update;
-  }
-
-# 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
@@ -391,32 +366,18 @@ sub update {
 
   my $guard = $self->txn_scope_guard;
 
-# 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;
-
-  $self->next::method($source, \%blobs_to_empty, $where, @rest);
-
-# 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 @res;
-  if (%$fields) {
-    $self->_set_identity_insert($table, 'update')   if $is_identity_update;
-
-    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;
+  if ($wantarray) {
+    @res    = $self->next::method(@_);
+  }
+  elsif (defined $wantarray) {
+    $res[0] = $self->next::method(@_);
   }
+  else {
+    $self->next::method(@_);
+  }
+
+  $self->_update_blobs($source, $blob_cols, $where);
 
   $guard->commit;
 
@@ -426,23 +387,16 @@ sub update {
 ### the insert_bulk stuff stolen from DBI/MSSQL.pm
 
 sub _set_identity_insert {
-  my ($self, $table, $op) = @_;
+  my ($self, $table) = @_;
 
   my $sql = sprintf (
-    'SET IDENTITY_%s %s ON',
-    (uc($op) || 'INSERT'),
+    'SET IDENTITY_INSERT %s ON',
     $self->sql_maker->_quote ($table),
   );
 
-  $self->_query_start($sql);
-
   my $dbh = $self->_get_dbh;
   eval { $dbh->do ($sql) };
-  my $exception = $@;
-
-  $self->_query_end($sql);
-
-  if ($exception) {
+  if ($@) {
     $self->throw_exception (sprintf "Error executing '%s': %s",
       $sql,
       $dbh->errstr,
@@ -451,20 +405,15 @@ sub _set_identity_insert {
 }
 
 sub _unset_identity_insert {
-  my ($self, $table, $op) = @_;
+  my ($self, $table) = @_;
 
   my $sql = sprintf (
-    'SET IDENTITY_%s %s OFF',
-    (uc($op) || 'INSERT'),
+    'SET IDENTITY_INSERT %s OFF',
     $self->sql_maker->_quote ($table),
   );
 
-  $self->_query_start($sql);
-
   my $dbh = $self->_get_dbh;
   $dbh->do ($sql);
-
-  $self->_query_end($sql);
 }
 
 # XXX this should use the DBD::Sybase bulk API, where possible
@@ -492,8 +441,6 @@ 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) = @_;
 
@@ -501,14 +448,8 @@ sub _remove_blob_cols {
 
   for my $col (keys %$fields) {
     if ($self->_is_lob_type($source->column_info($col)->{data_type})) {
-      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 '';
-      }
+      $blob_cols{$col} = delete $fields->{$col};
+      $fields->{$col} = \"''";
     }
   }
 
@@ -520,7 +461,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
@@ -550,17 +491,16 @@ sub _insert_blobs {
   my ($self, $source, $blob_cols, $row) = @_;
   my $dbh = $self->_get_dbh;
 
-  my $table = $source->name;
+  my $table = $source->from;
 
   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};
@@ -571,18 +511,6 @@ sub _insert_blobs {
     $cursor->next;
     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";
-    }
-
     eval {
       do {
         $sth->func('CS_GET', 1, 'ct_data_info') or die $sth->errstr;
@@ -606,12 +534,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);
       }
     }
   }