make insert work as a nested transaction too
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
index ef4616c..b320242 100644 (file)
@@ -11,6 +11,10 @@ use mro 'c3';
 use Carp::Clan qw/^DBIx::Class/;
 use List::Util ();
 
+__PACKAGE__->mk_group_accessors('simple' =>
+    qw/_identity _blob_log_on_update/
+);
+
 =head1 NAME
 
 DBIx::Class::Storage::DBI::Sybase - Sybase support for DBIx::Class
@@ -28,7 +32,7 @@ will be reblessed to L<DBIx::Class::Storage::DBI::Sybase::NoBindVars>. You can
 also enable that driver explicitly, see the documentation for more details.
 
 With this driver there is unfortunately no way to get the C<last_insert_id>
-without doing a C<select max(col)>.
+without doing a C<SELECT MAX(col)>.
 
 But your queries will be cached.
 
@@ -44,10 +48,6 @@ A recommended L<DBIx::Class::Storage::DBI/connect_info> setting:
 
 =cut
 
-__PACKAGE__->mk_group_accessors('simple' =>
-    qw/_blob_log_on_update/
-);
-
 sub _rebless {
   my $self = shift;
 
@@ -95,6 +95,14 @@ EOF
   }
 }
 
+# Make sure we have CHAINED mode turned on, we don't know how DBD::Sybase was
+# compiled.
+sub _populate_dbh {
+  my $self = shift;
+  $self->next::method(@_);
+  $self->_dbh->{syb_chained_txn} = 1;
+}
+
 sub _using_freetds {
   my $self = shift;
 
@@ -173,26 +181,81 @@ sub _is_lob_type {
 #  return (@non_blobs, @blobs);
 #}
 
-# override to handle TEXT/IMAGE
-sub insert {
-  my ($self, $source, $to_insert) = splice @_, 0, 3;
-  my $dbh = $self->_dbh;
+# the select-piggybacking-on-insert trick stolen from odbc/mssql
+sub _prep_for_execute {
+  my $self = shift;
+  my ($op, $extra_bind, $ident, $args) = @_;
 
-  my $blob_cols = $self->_remove_blob_cols($source, $to_insert);
+  my ($sql, $bind) = $self->next::method (@_);
 
-# check if we need to set IDENTITY_INSERT
-  my $identity_insert = 0;
-  my %col_info = map { ($_, $source->column_info($_)) } keys %$to_insert;
-  my $table    = $source->from;
+  if ($op eq 'insert') {
+    my $table = $ident->from;
 
-  if (List::Util::first { $_->{is_auto_increment} } (values %col_info)) {
-    $identity_insert = 1;
-    $dbh->do("SET IDENTITY_INSERT $table ON");
+    my $bind_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]);
+    my $identity_col =
+List::Util::first { $bind_info->{$_}{is_auto_increment} } (keys %$bind_info);
+
+    if ($identity_col) {
+      $sql =
+"SET IDENTITY_INSERT $table ON\n" .
+"$sql\n" .
+"SET IDENTITY_INSERT $table OFF"
+    } else {
+      $identity_col = List::Util::first {
+        $ident->column_info($_)->{is_auto_increment}
+      } $ident->columns;
+    }
+
+    if ($identity_col) {
+      $sql =
+        "$sql\n" .
+        $self->_fetch_identity_sql($ident, $identity_col) . "\n";
+    }
   }
 
-  my $updated_cols = $self->next::method($source, $to_insert, @_);
+  return ($sql, $bind);
+}
 
-  $dbh->do("SET IDENTITY_INSERT $table OFF") if $identity_insert;
+sub _fetch_identity_sql {
+  my ($self, $source, $col) = @_;
+
+  return "SELECT MAX($col) FROM ".$source->from;
+}
+
+sub _execute {
+  my $self = shift;
+  my ($op) = @_;
+
+  my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
+
+  if ($op eq 'insert') {
+    $self->_identity($sth->fetchrow_array);
+    $sth->finish;
+  }
+
+  return wantarray ? ($rv, $sth, @bind) : $rv;
+}
+
+sub last_insert_id { shift->_identity }
+
+# override to handle TEXT/IMAGE and nested txn
+sub insert {
+  my ($self, $source, $to_insert) = splice @_, 0, 3;
+  my $dbh = $self->_dbh;
+
+  my $blob_cols = $self->_remove_blob_cols($source, $to_insert);
+
+# Sybase has nested transactions fortunately, because 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 {
+    local $self->{auto_savepoint} = 1;
+    my $args = \@_;
+    my $method = $self->next::can;
+    $self->txn_do(
+      sub { $self->$method($source, $to_insert, @$args) }
+    );
+  };
 
   $self->_insert_blobs($source, $blob_cols, $to_insert) if %$blob_cols;
 
@@ -375,17 +438,6 @@ C<SMALLDATETIME> columns only have minute precision.
 
 sub datetime_parser_type { "DateTime::Format::Sybase" }
 
-sub _dbh_last_insert_id {
-  my ($self, $dbh, $source, $col) = @_;
-
-  # sorry, there's no other way!
-  my $sth = $self->sth("select max($col) from ".$source->from);
-  my ($id) = $dbh->selectrow_array($sth);
-  $sth->finish;
-
-  return $id;
-}
-
 # savepoint support using ASE syntax
 
 sub _svp_begin {