[Bast-commits] r7604 - in DBIx-Class/0.08/branches/sybase: lib/DBIx/Class/Storage/DBI lib/DBIx/Class/Storage/DBI/Sybase lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server t

caelum at dev.catalyst.perl.org caelum at dev.catalyst.perl.org
Wed Sep 9 00:15:55 GMT 2009


Author: caelum
Date: 2009-09-09 00:15:54 +0000 (Wed, 09 Sep 2009)
New Revision: 7604

Modified:
   DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase.pm
   DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Common.pm
   DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm
   DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server/NoBindVars.pm
   DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm
   DBIx-Class/0.08/branches/sybase/t/746sybase.t
Log:
remove unsafe_insert

Modified: DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Common.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Common.pm	2009-09-08 18:13:29 UTC (rev 7603)
+++ DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Common.pm	2009-09-09 00:15:54 UTC (rev 7604)
@@ -28,7 +28,18 @@
   my $dbh = $self->_dbh or return 0;
 
   local $dbh->{RaiseError} = 1;
+  local $dbh->{PrintError} = 0;
+  local $@;
+
+  if ($dbh->{syb_no_child_con}) {
+# ping is impossible with an active statement, we return false if so
+    my $ping = eval { $dbh->ping };
+    return $@ ? 0 : $ping;
+  }
+
   eval {
+# XXX if the main connection goes stale, does opening another for this statement
+# really determine anything?
     $dbh->do('select 1');
   };
 
@@ -72,14 +83,16 @@
   $dbh->do("SET TEXTSIZE $bytes");
 
 Takes the number of bytes, or uses the C<LongReadLen> value from your
-L<DBIx::Class/connect_info> if omitted.
+L<DBIx::Class/connect_info> if omitted, lastly falls back to the C<32768> which
+is the L<DBD::Sybase> default.
 
 =cut
 
 sub set_textsize {
   my $self = shift;
   my $text_size = shift ||
-    eval { $self->_dbi_connect_info->[-1]->{LongReadLen} };
+    eval { $self->_dbi_connect_info->[-1]->{LongReadLen} } ||
+    32768; # the DBD::Sybase default
 
   return unless defined $text_size;
 

Modified: DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server/NoBindVars.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server/NoBindVars.pm	2009-09-08 18:13:29 UTC (rev 7603)
+++ DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server/NoBindVars.pm	2009-09-09 00:15:54 UTC (rev 7604)
@@ -9,6 +9,12 @@
 /;
 use mro 'c3';
 
+sub new {
+  my $self = shift->next::method(@_);
+  $self->_rebless;
+  return $self;
+}
+
 sub _rebless {
   my $self = shift;
 

Modified: DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm	2009-09-08 18:13:29 UTC (rev 7603)
+++ DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm	2009-09-09 00:15:54 UTC (rev 7604)
@@ -9,6 +9,12 @@
 /;
 use mro 'c3';
 
+sub new {
+  my $self = shift->next::method(@_); 
+  $self->_rebless;
+  return $self;
+}
+
 sub _rebless {
   my $self = shift;
   my $dbh  = $self->_get_dbh;
@@ -21,11 +27,8 @@
 
 # LongReadLen doesn't work with MSSQL through DBD::Sybase, and the default is
 # huge on some versions of SQL server and can cause memory problems, so we
-# fix it up here.
-  my $text_size = eval { $self->_dbi_connect_info->[-1]->{LongReadLen} } ||
-    32768; # the DBD::Sybase default
-
-  $dbh->do("set textsize $text_size");
+# fix it up here (see Sybase/Common.pm .)
+  $self->set_textsize;
 }
 
 1;

Modified: DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm	2009-09-08 18:13:29 UTC (rev 7603)
+++ DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase/NoBindVars.pm	2009-09-09 00:15:54 UTC (rev 7604)
@@ -1,23 +1,26 @@
 package DBIx::Class::Storage::DBI::Sybase::NoBindVars;
 
-use Class::C3;
 use base qw/
   DBIx::Class::Storage::DBI::NoBindVars
   DBIx::Class::Storage::DBI::Sybase
 /;
+use mro 'c3';
 use List::Util ();
 use Scalar::Util ();
 
+sub new {
+  my $self = shift->next::method(@_);
+  $self->_rebless;
+  return $self;
+}
+
 sub _rebless {
   my $self = shift;
   $self->disable_sth_caching(1);
-  $self->unsafe_insert(1);  # there is nothing unsafe as the
-                            # last_insert_id mechanism is different
-                            # without bindvars
+  $self->_identity_method('@@IDENTITY');
 }
 
-# this works when NOT using placeholders
-sub _fetch_identity_sql { 'SELECT @@IDENTITY' }
+sub _fetch_identity_sql { 'SELECT ' . $_[0]->_identity_method }
 
 my $number = sub { Scalar::Util::looks_like_number($_[0]) };
 

Modified: DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase.pm
===================================================================
--- DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase.pm	2009-09-08 18:13:29 UTC (rev 7603)
+++ DBIx-Class/0.08/branches/sybase/lib/DBIx/Class/Storage/DBI/Sybase.pm	2009-09-09 00:15:54 UTC (rev 7604)
@@ -12,7 +12,7 @@
 use List::Util ();
 
 __PACKAGE__->mk_group_accessors('simple' =>
-    qw/_identity _blob_log_on_update unsafe_insert _insert_dbh/
+    qw/_identity _blob_log_on_update _insert_dbh _identity_method/
 );
 
 =head1 NAME
@@ -33,12 +33,8 @@
 
 With this driver there is unfortunately no way to get the C<last_insert_id>
 without doing a C<SELECT MAX(col)>. This is done safely in a transaction
-(locking the table.) The transaction can be turned off if concurrency is not an
-issue, or you don't need the C<IDENTITY> value, see
-L<DBIx::Class::Storage::DBI::Sybase/connect_call_unsafe_insert>.
+(locking the table.) See L</INSERTS WITH PLACEHOLDERS>.
 
-But your queries will be cached.
-
 A recommended L<DBIx::Class::Storage::DBI/connect_info> setting:
 
   on_connect_call => [['datetime_setup'], ['blob_setup', log_on_update => 0]]
@@ -160,32 +156,6 @@
     if exists $args{log_on_update};
 }
 
-=head2 connect_call_unsafe_insert
-
-With placeholders enabled, inserts are done in a transaction so that there are
-no concurrency issues with getting the inserted identity value using
-C<SELECT MAX(col)> when placeholders are enabled.
-
-When using C<DBIx::Class::Storage::DBI::Sybase::NoBindVars> transactions are
-disabled.
-
-To turn off transactions for inserts (for an application that doesn't need
-concurrency, or a loader, for example) use this setting in
-L<DBIx::Class::Storage::DBI/connect_info>,
-
-  on_connect_call => ['unsafe_insert']
-
-To manipulate this setting at runtime, use:
-
-  $schema->storage->unsafe_insert(0|1);
-
-=cut
-
-sub connect_call_unsafe_insert {
-  my $self = shift;
-  $self->unsafe_insert(1);
-}
-
 sub _is_lob_type {
   my $self = shift;
   my $type = shift;
@@ -292,9 +262,19 @@
 
   my $blob_cols = $self->_remove_blob_cols($source, $to_insert);
 
-# insert+blob insert done atomically
-  my $guard = $self->txn_scope_guard if $blob_cols;
+# insert+blob insert done atomically, on _insert_dbh
+  (my ($guard), local ($self->{_dbh})) = do {
+    $self->_insert_dbh($self->_connect(@{ $self->_dbi_connect_info }))
+      unless $self->_insert_dbh;
 
+    my $new_guard = $self->txn_scope_guard;
+
+# _dbh_begin_work may reconnect, if so we need to update _insert_dbh
+    $self->_insert_dbh($self->_dbh);
+
+    ($new_guard, $self->_insert_dbh)
+  } if $blob_cols;
+
   my $need_last_insert_id = 0;
 
   my ($identity_col) =
@@ -309,16 +289,24 @@
   # 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 {
+    no warnings 'uninitialized';
     if (
-      $need_last_insert_id && !$self->unsafe_insert && !$self->{transaction_depth}
+      $need_last_insert_id &&
+      $self->_identity_method ne '@@IDENTITY' &&
+      !$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;
+
+# _dbh_begin_work may reconnect, if so we need to update _insert_dbh
+      $self->_insert_dbh($self->_dbh);
+
       my $upd_cols = $self->next::method (@_);
       $guard->commit;
-      $self->_insert_dbh($self->_dbh);
+
       $upd_cols;
     }
     else {
@@ -613,6 +601,17 @@
 
 Inserts or updates of TEXT/IMAGE columns will B<NOT> work with FreeTDS.
 
+=head1 INSERTS WITH PLACEHOLDERS
+
+With placeholders enabled, inserts are done in a transaction so that there are
+no concurrency issues with getting the inserted identity value using
+C<SELECT MAX(col)>, which is the only way to get the C<IDENTITY> value in this
+mode.
+
+When using C<DBIx::Class::Storage::DBI::Sybase::NoBindVars> transactions are
+disabled, as there are no concurrency issues with C<SELECT @@IDENTITY> as it's a
+session variable.
+
 =head1 TRANSACTIONS
 
 Due to limitations of the TDS protocol, L<DBD::Sybase>, or both; you cannot
@@ -621,6 +620,18 @@
 C<next> or C<first> but has not been exhausted or
 L<reset|DBIx::Class::ResultSet/reset>.
 
+For example, this will not work:
+
+  $schema->txn_do(sub {
+    my $rs = $schema->resultset('Book');
+    while (my $row = $rs->next) {
+      $schema->resultset('MetaData')->create({
+        book_id => $row->id,
+        ...
+      });
+    }
+  });
+
 Transactions done for inserts in C<AutoCommit> mode when placeholders are in use
 are not affected, as they use an extra database handle to do the insert.
 
@@ -634,8 +645,6 @@
 
 =item * load the data from your cursor with L<DBIx::Class::ResultSet/all>
 
-=item * enlarge the scope of the transaction
-
 =back
 
 =head1 MAXIMUM CONNECTIONS

Modified: DBIx-Class/0.08/branches/sybase/t/746sybase.t
===================================================================
--- DBIx-Class/0.08/branches/sybase/t/746sybase.t	2009-09-08 18:13:29 UTC (rev 7603)
+++ DBIx-Class/0.08/branches/sybase/t/746sybase.t	2009-09-09 00:15:54 UTC (rev 7604)
@@ -11,7 +11,7 @@
 
 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
 
-my $TESTS = 39 + 2;
+my $TESTS = 40 + 2;
 
 if (not ($dsn && $user)) {
   plan skip_all =>
@@ -308,21 +308,36 @@
   });
 
 # test insert transaction when there's an active cursor
-  TODO: { 
-#    local $TODO = 'not supported yet or possibly ever';
+  SKIP: {
+    skip 'not testing insert with active cursor if using ::NoBindVars', 1
+      if $storage_type =~ /NoBindVars/i;
 
-    SKIP: {
-      skip 'not testing insert with active cursor if using unsafe_insert', 1
-        if $schema->storage->unsafe_insert;
+    my $artist_rs = $schema->resultset('Artist');
+    $artist_rs->first;
+    lives_ok {
+      my $row = $schema->resultset('Money')->create({ amount => 100 });
+      $row->delete;
+    } 'inserted a row with an active cursor';
+    $ping_count-- if $@; # dbh_do calls ->connected
+  }
 
-      my $artist_rs = $schema->resultset('Artist');
-      $artist_rs->first;
-      lives_ok {
+# test insert in an outer transaction when there's an active cursor
+  TODO: {
+    local $TODO = 'this should work once we have eager cursors';
+
+# clear state, or we get a deadlock on $row->delete
+# XXX figure out why this happens
+    $schema->storage->disconnect;
+
+    lives_ok {
+      $schema->txn_do(sub {
+        my $artist_rs = $schema->resultset('Artist');
+        $artist_rs->first;
         my $row = $schema->resultset('Money')->create({ amount => 100 });
         $row->delete;
-      } 'inserted a row with an active cursor';
-      $ping_count-- if $@; # dbh_do calls ->connected
-    }
+      });
+    } 'inserted a row with an active cursor in outer txn';
+    $ping_count-- if $@; # dbh_do calls ->connected
   }
 
 # Now test money values.




More information about the Bast-commits mailing list