use DBIx::Connector;
our %Connector;
-has 'dbh' => (
+has 'connector' => (
is => 'lazy',
- handles => [
- qw[
- begin_work column_info commit disconnect do errstr get_info last_insert_id ping prepare
- primary_key quote_identifier rollback selectall_arrayref selectall_hashref
- selectcol_arrayref selectrow_array selectrow_arrayref selectrow_hashref table_info
- ]
- ],
+ handles => [ qw( dbh ) ],
);
use Bugzilla::Constants;
required => 1,
);
+
+# Install proxy methods to the DBI object.
+# We can't use handles() as DBIx::Connector->dbh has to be called each
+# time we need a DBI handle to ensure the connection is alive.
+{
+ my @DBI_METHODS = qw(
+ begin_work column_info commit disconnect do errstr get_info last_insert_id ping prepare
+ primary_key quote_identifier rollback selectall_arrayref selectall_hashref
+ selectcol_arrayref selectrow_array selectrow_arrayref selectrow_hashref table_info
+ );
+ my $stash = Package::Stash->new(__PACKAGE__);
+
+ foreach my $method (@DBI_METHODS) {
+ my $symbol = '&' . $method;
+ $stash->add_symbol(
+ $symbol => sub {
+ my $self = shift;
+ return $self->dbh->$method(@_);
+ }
+ );
+ }
+}
+
#####################################################################
# Constants
#####################################################################
# instantiate the correct DB specific module
- my $dbh = $pkg_module->new($params);
-
- return $dbh;
+ return $pkg_module->new($params);
}
sub _handle_error {
# Subclass Helpers
#####################################################################
-sub _build_dbh {
+sub _build_connector {
my ($self) = @_;
my ($dsn, $user, $pass, $override_attrs) =
map { $self->$_ } qw(dsn user pass attrs);
}
}
- my $connector = $Connector{"$user.$dsn"} //= DBIx::Connector->new($dsn, $user, $pass, $attributes);
-
- return $connector->dbh;
+ return $Connector{"$user.$dsn"} //= DBIx::Connector->new($dsn, $user, $pass, $attributes);
}
#####################################################################
'Moo' => '2.002004',
'MooX::StrictConstructor' => '0.008',
'Mozilla::CA' => '20160104',
+ 'Package::Stash' => '0.37',
'Parse::CPAN::Meta' => '1.44',
'Role::Tiny' => '2.000003',
'Scope::Guard' => '0.21',