remove @args from DBI::sth, use full arg list
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Storage / DBI.pm
index 02e2e7f..ed79308 100644 (file)
@@ -836,7 +836,9 @@ sub sql_maker {
   return $self->_sql_maker;
 }
 
+# nothing to do by default
 sub _rebless {}
+sub _init {}
 
 sub _populate_dbh {
   my ($self) = @_;
@@ -903,6 +905,8 @@ sub _determine_driver {
 
     $self->_driver_determined(1);
 
+    $self->_init; # run driver-specific initializations
+
     $self->_run_connection_actions
         if $started_unconnected && defined $self->_dbh;
   }
@@ -1324,7 +1328,7 @@ sub insert {
 ## scalar refs, or at least, all the same type as the first set, the statement is
 ## only prepped once.
 sub insert_bulk {
-  my ($self, $source, $cols, $data) = @_;
+  my ($self, $source, $cols, $data, $sth_attr) = @_;
 
 # redispatch to insert_bulk method of storage we reblessed into, if necessary
   if (not $self->_driver_determined) {
@@ -1335,10 +1339,11 @@ sub insert_bulk {
   my %colvalues;
   my $table = $source->from;
   @colvalues{@$cols} = (0..$#$cols);
+# XXX some bulk APIs require column list in database order
   my ($sql, @bind) = $self->sql_maker->insert($table, \%colvalues);
 
   $self->_query_start( $sql, @bind );
-  my $sth = $self->sth($sql);
+  my $sth = $self->sth($sql, 'insert', $sth_attr);
 
 #  @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
 
@@ -1367,7 +1372,7 @@ sub insert_bulk {
     $placeholder_index++;
   }
   my $rv = eval { $sth->execute_array({ArrayTupleStatus => $tuple_status}) };
-  if (my $err = $@) {
+  if (my $err = $@ || $sth->errstr) {
     my $i = 0;
     ++$i while $i <= $#$tuple_status && !ref $tuple_status->[$i];
 
@@ -1379,6 +1384,7 @@ sub insert_bulk {
     local $Data::Dumper::Indent = 1;
     local $Data::Dumper::Useqq = 1;
     local $Data::Dumper::Quotekeys = 0;
+    local $Data::Dumper::Sortkeys = 1;
 
     $self->throw_exception(sprintf "%s for populate slice:\n%s",
       $tuple_status->[$i][1],
@@ -1389,6 +1395,8 @@ sub insert_bulk {
   }
   $self->throw_exception($sth->errstr) if !$rv;
 
+  $sth->finish;
+
   $self->_query_end( $sql, @bind );
   return (wantarray ? ($rv, $sth, @bind) : $rv);
 }
@@ -2052,12 +2060,15 @@ Returns a L<DBI> sth (statement handle) for the supplied SQL.
 =cut
 
 sub _dbh_sth {
-  my ($self, $dbh, $sql) = @_;
+  my ($self, $dbh, $sql, $op, $sth_attr) = @_;
+# $op is ignored right now
+
+  $sth_attr ||= {};
 
   # 3 is the if_active parameter which avoids active sth re-use
   my $sth = $self->disable_sth_caching
-    ? $dbh->prepare($sql)
-    : $dbh->prepare_cached($sql, {}, 3);
+    ? $dbh->prepare($sql, $sth_attr)
+    : $dbh->prepare_cached($sql, $sth_attr, 3);
 
   # XXX You would think RaiseError would make this impossible,
   #  but apparently that's not true :(
@@ -2067,8 +2078,8 @@ sub _dbh_sth {
 }
 
 sub sth {
-  my ($self, $sql) = @_;
-  $self->dbh_do('_dbh_sth', $sql);  # retry over disconnects
+  my ($self, $sql, $op, $sth_attr) = @_;
+  $self->dbh_do('_dbh_sth', $sql, $op, $sth_attr);  # retry over disconnects
 }
 
 sub _dbh_columns_info_for {
@@ -2554,7 +2565,6 @@ Returns the datetime parser class
 sub datetime_parser {
   my $self = shift;
   return $self->{datetime_parser} ||= do {
-    $self->_populate_dbh unless $self->_dbh;
     $self->build_datetime_parser(@_);
   };
 }
@@ -2575,6 +2585,11 @@ See L</datetime_parser>
 =cut
 
 sub build_datetime_parser {
+  if (not $_[0]->_driver_determined) {
+    $_[0]->_determine_driver;
+    goto $_[0]->can('build_datetime_parser');
+  }
+
   my $self = shift;
   my $type = $self->datetime_parser_type(@_);
   $self->ensure_class_loaded ($type);
@@ -2609,10 +2624,12 @@ sub lag_behind_master {
 
 sub DESTROY {
   my $self = shift;
+
   $self->_verify_pid if $self->_dbh;
 
   # some databases need this to stop spewing warnings
   if (my $dbh = $self->_dbh) {
+    local $@;
     eval { $dbh->disconnect };
   }