Factor SQL-standard deferred FK checks into a component people/ilmari/oracle-deferred-constraints
Dagfinn Ilmari Mannsåker [Tue, 21 Oct 2014 13:29:27 +0000 (14:29 +0100)]
PostgreSQL, Informix and Oracle all support this standard syntax, so
factor it out in a separate component

lib/DBIx/Class/Storage/DBI/Informix.pm
lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm
lib/DBIx/Class/Storage/DBI/Pg.pm
lib/DBIx/Class/Storage/DBI/SetConstraintsDeferred.pm [new file with mode: 0644]

index b4a2ea8..79996d3 100644 (file)
@@ -2,12 +2,9 @@ package DBIx::Class::Storage::DBI::Informix;
 use strict;
 use warnings;
 
-use base qw/DBIx::Class::Storage::DBI/;
+use base qw/DBIx::Class::Storage::DBI::SetConstraintsDeferred/;
 use mro 'c3';
 
-use Scope::Guard ();
-use Scalar::Util 'weaken';
-use Context::Preserve 'preserve_context';
 use namespace::clean;
 
 __PACKAGE__->sql_limit_dialect ('SkipFirst');
@@ -60,22 +57,6 @@ sub _exec_svp_rollback {
     $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
 }
 
-sub with_deferred_fk_checks {
-  my ($self, $sub) = @_;
-
-  my $txn_scope_guard = $self->txn_scope_guard;
-
-  $self->_do_query('SET CONSTRAINTS ALL DEFERRED');
-
-  weaken($self);
-  return preserve_context {
-    my $sg = Scope::Guard->new(sub {
-      $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE');
-    });
-    $sub->()
-  } after => sub { $txn_scope_guard->commit };
-}
-
 =head2 connect_call_datetime_setup
 
 Used as:
index a90755c..a73b61f 100644 (file)
@@ -2,11 +2,9 @@ package DBIx::Class::Storage::DBI::Oracle::Generic;
 
 use strict;
 use warnings;
-use base qw/DBIx::Class::Storage::DBI/;
+use base qw/DBIx::Class::Storage::DBI::SetConstraintsDeferred/;
 use mro 'c3';
 use DBIx::Class::Carp;
-use Scope::Guard ();
-use Context::Preserve 'preserve_context';
 use Try::Tiny;
 use List::Util 'first';
 use namespace::clean;
@@ -665,35 +663,6 @@ sub relname_to_table_alias {
   return $self->sql_maker->_shorten_identifier($alias);
 }
 
-=head2 with_deferred_fk_checks
-
-Runs a coderef between:
-
-  alter session set constraints = deferred
-  ...
-  alter session set constraints = immediate
-
-to defer foreign key checks.
-
-Constraints must be declared C<DEFERRABLE> for this to work.
-
-=cut
-
-sub with_deferred_fk_checks {
-  my ($self, $sub) = @_;
-
-  my $txn_scope_guard = $self->txn_scope_guard;
-
-  $self->_do_query('alter session set constraints = deferred');
-
-  my $sg = Scope::Guard->new(sub {
-    $self->_do_query('alter session set constraints = immediate');
-  });
-
-  return
-    preserve_context { $sub->() } after => sub { $txn_scope_guard->commit };
-}
-
 =head1 ATTRIBUTES
 
 Following additional attributes can be used in resultsets.
index a67bc2f..ee134c9 100644 (file)
@@ -3,11 +3,8 @@ package DBIx::Class::Storage::DBI::Pg;
 use strict;
 use warnings;
 
-use base qw/DBIx::Class::Storage::DBI/;
+use base qw/DBIx::Class::Storage::DBI::SetConstraintsDeferred/;
 
-use Scope::Guard ();
-use Scalar::Util 'weaken';
-use Context::Preserve 'preserve_context';
 use DBIx::Class::Carp;
 use Try::Tiny;
 use namespace::clean;
@@ -24,22 +21,6 @@ sub _determine_supports_insert_returning {
   ;
 }
 
-sub with_deferred_fk_checks {
-  my ($self, $sub) = @_;
-
-  my $txn_scope_guard = $self->txn_scope_guard;
-
-  $self->_do_query('SET CONSTRAINTS ALL DEFERRED');
-
-  weaken($self);
-  return preserve_context {
-    my $sg = Scope::Guard->new(sub {
-      $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE');
-    });
-    $sub->()
-  } after => sub { $txn_scope_guard->commit };
-}
-
 # only used when INSERT ... RETURNING is disabled
 sub last_insert_id {
   my ($self,$source,@cols) = @_;
diff --git a/lib/DBIx/Class/Storage/DBI/SetConstraintsDeferred.pm b/lib/DBIx/Class/Storage/DBI/SetConstraintsDeferred.pm
new file mode 100644 (file)
index 0000000..6784c7c
--- /dev/null
@@ -0,0 +1,55 @@
+package DBIx::Class::Storage::DBI::SetConstraintsDeferred;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Storage::DBI';
+use mro 'c3';
+
+use Scope::Guard ();
+use Context::Preserve 'preserve_context';
+
+use namespace::clean;
+
+=head1 NAME
+
+DBIx::Class::Storage::DBI::SetConstraintsDeferred - Storage component for deferred constraints via C<SET CONSTRAINTS>
+
+=head1 DESCRIPTION
+
+This component implements L<DBIx::Class::Storage::DBI/with_deferred_fk_checks>
+by wrapping the the coderef in C<SET CONSTRAINTS ALL DEFERRED> and
+C<SET CONSTRAINTS ALL IMMEDIATE>.
+
+=cut
+
+sub with_deferred_fk_checks {
+  my ($self, $sub) = @_;
+
+  my $txn_scope_guard = $self->txn_scope_guard;
+
+  $self->_do_query('SET CONSTRAINTS ALL DEFERRED');
+
+  return preserve_context {
+    my $inner_self = $self; # avoid nested closure leak on 5.8
+    my $sg = Scope::Guard->new(sub {
+      $inner_self->_do_query('SET CONSTRAINTS ALL IMMEDIATE');
+    });
+    $sub->()
+  } after => sub { $txn_scope_guard->commit };
+}
+
+=head1 FURTHER QUESTIONS?
+
+Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
+
+=head1 COPYRIGHT AND LICENSE
+
+This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
+by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
+redistribute it and/or modify it under the same terms as the
+L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
+
+=cut
+
+1;