Initial revision (but working :).
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Storage / DBI.pm
index 37b586e..ff7c9cd 100644 (file)
@@ -304,6 +304,8 @@ sub new {
   $new->cursor("DBIx::Class::Storage::DBI::Cursor");
   $new->transaction_depth(0);
   $new->_sql_maker_opts({});
+  $new->{_in_dbh_do} = 0;
+  $new->{_dbh_gen} = 0;
 
   $new;
 }
@@ -482,11 +484,12 @@ sub dbh_do {
   my $self = shift;
   my $coderef = shift;
 
-  return $coderef->($self, $self->_dbh, @_) if $self->{_in_txn_do};
-
   ref $coderef eq 'CODE' or $self->throw_exception
     ('$coderef must be a CODE reference');
 
+  return $coderef->($self, $self->_dbh, @_) if $self->{_in_dbh_do};
+  local $self->{_in_dbh_do} = 1;
+
   my @result;
   my $want_array = wantarray;
 
@@ -517,7 +520,7 @@ sub dbh_do {
 
 # This is basically a blend of dbh_do above and DBIx::Class::Storage::txn_do.
 # It also informs dbh_do to bypass itself while under the direction of txn_do,
-#  via $self->{_in_txn_do} (this saves some redundant eval and errorcheck, etc)
+#  via $self->{_in_dbh_do} (this saves some redundant eval and errorcheck, etc)
 sub txn_do {
   my $self = shift;
   my $coderef = shift;
@@ -525,7 +528,7 @@ sub txn_do {
   ref $coderef eq 'CODE' or $self->throw_exception
     ('$coderef must be a CODE reference');
 
-  local $self->{_in_txn_do} = 1;
+  local $self->{_in_dbh_do} = 1;
 
   my @result;
   my $want_array = wantarray;
@@ -588,6 +591,7 @@ sub disconnect {
     $self->_dbh->rollback unless $self->_dbh->{AutoCommit};
     $self->_dbh->disconnect;
     $self->_dbh(undef);
+    $self->{_dbh_gen}++;
   }
 }
 
@@ -596,7 +600,9 @@ sub connected {
 
   if(my $dbh = $self->_dbh) {
       if(defined $self->_conn_tid && $self->_conn_tid != threads->tid) {
-          return $self->_dbh(undef);
+          $self->_dbh(undef);
+          $self->{_dbh_gen}++;
+          return;
       }
       else {
           $self->_verify_pid;
@@ -616,6 +622,7 @@ sub _verify_pid {
 
   $self->_dbh->{InactiveDestroy} = 1;
   $self->_dbh(undef);
+  $self->{_dbh_gen}++;
 
   return;
 }
@@ -700,6 +707,7 @@ sub _connect {
        $dbh = DBI->connect(@info);
        $dbh->{RaiseError} = 1;
        $dbh->{PrintError} = 0;
+       $dbh->{PrintWarn} = 0;
     }
   };
 
@@ -784,22 +792,31 @@ sub txn_rollback {
   }
 }
 
-sub _execute {
+# This used to be the top-half of _execute.  It was split out to make it
+#  easier to override in NoBindVars without duping the rest.  It takes up
+#  all of _execute's args, and emits $sql, @bind.
+sub _prep_for_execute {
   my ($self, $op, $extra_bind, $ident, @args) = @_;
+
   my ($sql, @bind) = $self->sql_maker->$op($ident, @args);
   unshift(@bind, @$extra_bind) if $extra_bind;
+  @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
+
+  return ($sql, @bind);
+}
+
+sub _execute {
+  my $self = shift;
+
+  my ($sql, @bind) = $self->_prep_for_execute(@_);
+
   if ($self->debug) {
       my @debug_bind = map { defined $_ ? qq{'$_'} : q{'NULL'} } @bind;
       $self->debugobj->query_start($sql, @debug_bind);
   }
-  my $sth = eval { $self->sth($sql,$op) };
 
-  if (!$sth || $@) {
-    $self->throw_exception(
-      'no sth generated via sql (' . ($@ || $self->_dbh->errstr) . "): $sql"
-    );
-  }
-  @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
+  my $sth = $self->sth($sql);
+
   my $rv;
   if ($sth) {
     my $time = time();
@@ -903,7 +920,10 @@ Returns a L<DBI> sth (statement handle) for the supplied SQL.
 sub _dbh_sth {
   my ($self, $dbh, $sql) = @_;
   # 3 is the if_active parameter which avoids active sth re-use
-  $dbh->prepare_cached($sql, {}, 3);
+  $dbh->prepare_cached($sql, {}, 3) or
+    $self->throw_exception(
+      'no sth generated via sql (' . ($@ || $dbh->errstr) . "): $sql"
+    );
 }
 
 sub sth {