From: Luke Saunders Date: Wed, 6 Aug 2008 18:36:01 +0000 (+0000) Subject: added with_deferred_fk_checks functionality to storage X-Git-Tag: v0.08240~385 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e96a93dfeed5d4b22f146f2e2814d83973720b71;p=dbsrgits%2FDBIx-Class.git added with_deferred_fk_checks functionality to storage --- diff --git a/Changes b/Changes index 05e2fad..c7a1ae8 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for DBIx::Class 0.08099_04 2008-07-24 01:00:00 + - Functionality to storage to enable a sub to be run without FK checks - Fixed $schema->clone bug which caused clone and source to share internal hash refs - Added register_extra_source methods for additional sources diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index cf61846..24f177c 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -709,6 +709,28 @@ sub disconnect { } } +=head2 with_deferred_fk_checks + +=over 4 + +=item Arguments: C<$coderef> + +=item Return Value: The return value of $coderef + +=back + +Storage specific method to run the code ref with FK checks deferred or +in MySQL's case disabled entirely. + +=cut + +# Storage subclasses should override this +sub with_deferred_fk_checks { + my ($self, $sub) = @_; + + $sub->(); +} + sub connected { my ($self) = @_; diff --git a/lib/DBIx/Class/Storage/DBI/Pg.pm b/lib/DBIx/Class/Storage/DBI/Pg.pm index bd28e02..937edfb 100644 --- a/lib/DBIx/Class/Storage/DBI/Pg.pm +++ b/lib/DBIx/Class/Storage/DBI/Pg.pm @@ -13,6 +13,13 @@ use base qw/DBIx::Class::Storage::DBI/; warn "DBD::Pg 1.49 is strongly recommended" if ($DBD::Pg::VERSION < 1.49); +sub with_deferred_fk_checks { + my ($self, $sub) = @_; + + $self->dbh->do('SET CONSTRAINTS ALL DEFERRED'); + $sub->(); +} + sub _dbh_last_insert_id { my ($self, $dbh, $seq) = @_; $dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq}); diff --git a/lib/DBIx/Class/Storage/DBI/mysql.pm b/lib/DBIx/Class/Storage/DBI/mysql.pm index dadcbf0..fa0419f 100644 --- a/lib/DBIx/Class/Storage/DBI/mysql.pm +++ b/lib/DBIx/Class/Storage/DBI/mysql.pm @@ -7,6 +7,14 @@ use base qw/DBIx::Class::Storage::DBI/; # __PACKAGE__->load_components(qw/PK::Auto/); +sub with_deferred_fk_checks { + my ($self, $sub) = @_; + + $self->dbh->do('SET foreign_key_checks=0'); + $sub->(); + $self->dbh->do('SET foreign_key_checks=1'); +} + sub _dbh_last_insert_id { my ($self, $dbh, $source, $col) = @_; $dbh->{mysql_insertid};