use Scalar::Util qw/refaddr weaken reftype blessed/;
use List::Util qw/first/;
use Sub::Name 'subname';
+use Context::Preserve 'preserve_context';
use Try::Tiny;
use overload ();
use Data::Compare (); # no imports!!! guard against insane architecture
my ($old_connect_via, $dbh);
- if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) {
- $old_connect_via = $DBI::connect_via;
- $DBI::connect_via = 'connect';
- }
+ local $DBI::connect_via = 'connect' if $INC{'Apache/DBI.pm'} && $ENV{MOD_PERL};
try {
if(ref $info[0] eq 'CODE') {
}
catch {
$self->throw_exception("DBI Connection failed: $_")
- }
- finally {
- $DBI::connect_via = $old_connect_via if $old_connect_via;
};
$self->_dbh_autocommit($dbh->{AutoCommit});
data => $schema,
);
- my @ret;
- if (wantarray) {
- @ret = $tr->translate;
- }
- else {
- $ret[0] = $tr->translate;
- }
-
- $self->throw_exception( 'Unable to produce deployment statements: ' . $tr->error)
- unless (@ret && defined $ret[0]);
-
- return wantarray ? @ret : $ret[0];
+ return preserve_context {
+ $tr->translate
+ } after => sub {
+ $self->throw_exception( 'Unable to produce deployment statements: ' . $tr->error)
+ unless defined $_[0];
+ };
}
# FIXME deploy() currently does not accurately report sql errors
use Scalar::Util 'reftype';
use Hash::Merge;
use List::Util qw/min max reduce/;
+use Context::Preserve 'preserve_context';
use Try::Tiny;
-use namespace::clean;
use namespace::clean -except => 'meta';
$self->_master_connect_info_opts(\%opts);
- my @res;
- if (wantarray) {
- @res = $self->$next($info, @extra);
- } else {
- $res[0] = $self->$next($info, @extra);
- }
-
- # Make sure master is blessed into the correct class and apply role to it.
- my $master = $self->master;
- $master->_determine_driver;
- Moose::Meta::Class->initialize(ref $master);
-
- DBIx::Class::Storage::DBI::Replicated::WithDSN->meta->apply($master);
+ return preserve_context {
+ $self->$next($info, @extra);
+ } after => sub {
+ # Make sure master is blessed into the correct class and apply role to it.
+ my $master = $self->master;
+ $master->_determine_driver;
+ Moose::Meta::Class->initialize(ref $master);
- # link pool back to master
- $self->pool->master($master);
+ DBIx::Class::Storage::DBI::Replicated::WithDSN->meta->apply($master);
- wantarray ? @res : $res[0];
+ # link pool back to master
+ $self->pool->master($master);
+ };
};
=head1 METHODS
=cut
sub execute_reliably {
- my ($self, $coderef, @args) = @_;
+ my $self = shift;
+ my $coderef = shift;
unless( ref $coderef eq 'CODE') {
$self->throw_exception('Second argument must be a coderef');
}
- ##Get copy of master storage
- my $master = $self->master;
+ ## replace the current read handler for the remainder of the scope
+ local $self->{read_handler} = $self->master;
- ##Get whatever the current read hander is
- my $current = $self->read_handler;
-
- ##Set the read handler to master
- $self->read_handler($master);
-
- ## do whatever the caller needs
- my @result;
- my $want_array = wantarray;
-
- try {
- if($want_array) {
- @result = $coderef->(@args);
- } elsif(defined $want_array) {
- ($result[0]) = ($coderef->(@args));
- } else {
- $coderef->(@args);
- }
+ my $args = \@_;
+ return try {
+ $coderef->(@$args);
} catch {
$self->throw_exception("coderef returned an error: $_");
- } finally {
- ##Reset to the original state
- $self->read_handler($current);
};
-
- return wantarray ? @result : $result[0];
}
=head2 set_reliable_storage