fix _insert_dbh code to only connect when needed, doc update
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
index 46915ba..68c2e20 100644 (file)
@@ -12,7 +12,7 @@ use Carp::Clan qw/^DBIx::Class/;
 use List::Util ();
 
 __PACKAGE__->mk_group_accessors('simple' =>
-    qw/_identity _blob_log_on_update insert_txn/
+    qw/_identity _blob_log_on_update unsafe_insert _insert_dbh/
 );
 
 =head1 NAME
@@ -65,10 +65,6 @@ sub _rebless {
     } else { # real Sybase
       my $no_bind_vars = 'DBIx::Class::Storage::DBI::Sybase::NoBindVars';
 
-# This is reset to 0 in ::NoBindVars, only necessary because we use max(col) to
-# get the identity.
-      $self->insert_txn(1);
-
       if ($self->using_freetds) {
         carp <<'EOF' unless $ENV{DBIC_SYBASE_FREETDS_NOWARN};
 
@@ -181,13 +177,13 @@ L<DBIx::Class::Storage::DBI/connect_info>,
 
 To manipulate this setting at runtime, use:
 
-  $schema->storage->insert_txn(0); # 1 to re-enable
+  $schema->storage->unsafe_insert(0|1);
 
 =cut
 
 sub connect_call_unsafe_insert {
   my $self = shift;
-  $self->insert_txn(0);
+  $self->unsafe_insert(1);
 }
 
 sub _is_lob_type {
@@ -262,7 +258,7 @@ sub _native_data_type {
   my ($self, $type) = @_;
 
   $type = lc $type;
-  $type =~ s/ identity//;
+  $type =~ s/\s* identity//x;
 
   return uc($TYPE_MAPPING{$type} || $type);
 }
@@ -297,7 +293,7 @@ sub insert {
   my $blob_cols = $self->_remove_blob_cols($source, $to_insert);
 
 # insert+blob insert done atomically
-  my $guard = $self->txn_scope_guard if %$blob_cols;
+  my $guard = $self->txn_scope_guard if $blob_cols;
 
   my $need_last_insert_id = 0;
 
@@ -313,11 +309,16 @@ sub insert {
   # We have to do the insert in a transaction to avoid race conditions with the
   # SELECT MAX(COL) identity method used when placeholders are enabled.
   my $updated_cols = do {
-    if ($need_last_insert_id && $self->insert_txn &&
-        (not $self->{transaction_depth})) {
+    if (
+      $need_last_insert_id && !$self->unsafe_insert && !$self->{transaction_depth}
+    ) {
+      $self->_insert_dbh($self->_connect(@{ $self->_dbi_connect_info }))
+        unless $self->_insert_dbh;
+      local $self->{_dbh} = $self->_insert_dbh;
       my $guard = $self->txn_scope_guard;
       my $upd_cols = $self->next::method (@_);
       $guard->commit;
+      $self->_insert_dbh($self->_dbh);
       $upd_cols;
     }
     else {
@@ -325,7 +326,7 @@ sub insert {
     }
   };
 
-  $self->_insert_blobs($source, $blob_cols, $to_insert) if %$blob_cols;
+  $self->_insert_blobs($source, $blob_cols, $to_insert) if $blob_cols;
 
   $guard->commit if $guard;
 
@@ -341,7 +342,7 @@ sub update {
   my $blob_cols = $self->_remove_blob_cols($source, $fields);
 
 # update+blob update(s) done atomically
-  my $guard = $self->txn_scope_guard if %$blob_cols;
+  my $guard = $self->txn_scope_guard if $blob_cols;
 
   my @res;
   if ($wantarray) {
@@ -354,9 +355,9 @@ sub update {
     $self->next::method(@_);
   }
 
-  $self->_update_blobs($source, $blob_cols, $where) if %$blob_cols;
+  $self->_update_blobs($source, $blob_cols, $where) if $blob_cols;
 
-  $guard->commit if %$blob_cols;
+  $guard->commit if $guard;
 
   return $wantarray ? @res : $res[0];
 }
@@ -373,7 +374,7 @@ sub _remove_blob_cols {
     }
   }
 
-  return \%blob_cols;
+  return keys %blob_cols ? \%blob_cols : undef;
 }
 
 sub _update_blobs {
@@ -621,19 +622,12 @@ C<next> or C<first> but has not been exhausted or
 L<reset|DBIx::Class::ResultSet/reset>.
 
 Transactions done for inserts in C<AutoCommit> mode when placeholders are in use
-are also affected, so this won't work:
-
-  while (my $row = $rs1->next) {
-    $rs2->create({ foo => $row->foo });
-  }
+are not affected, as they use an extra database handle to do the insert.
 
 Some workarounds:
 
 =over 4
 
-=item * set C<< $schema->storage->insert_txn(0) >> temporarily (see
-L</connect_call_unsafe_insert>)
-
 =item * use L<DBIx::Class::Storage::DBI::Replicated>
 
 =item * L<connect|DBIx::Class::Schema/connect> another L<Schema|DBIx::Class::Schema>