diff options
Diffstat (limited to 'Bugzilla/DB.pm')
-rw-r--r-- | Bugzilla/DB.pm | 64 |
1 files changed, 33 insertions, 31 deletions
diff --git a/Bugzilla/DB.pm b/Bugzilla/DB.pm index 15acfd0d9..ec0f058b9 100644 --- a/Bugzilla/DB.pm +++ b/Bugzilla/DB.pm @@ -8,13 +8,22 @@ package Bugzilla::DB; use 5.10.1; -use strict; -use warnings; +use Moo; use DBI; - -# Inherit the DB class from DBI::db. -use base qw(DBI::db); +use DBIx::Connector; +our %Connector; + +has 'dbh' => ( + 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 + ] + ], +); use Bugzilla::Constants; use Bugzilla::Install::Requirements; @@ -27,11 +36,14 @@ use Bugzilla::Error; use Bugzilla::DB::Schema; use Bugzilla::Version; -use Bugzilla::Metrics::Mysql; - use List::Util qw(max); use Storable qw(dclone); +has [qw(dsn user pass attrs)] => ( + is => 'ro', + required => 1, +); + ##################################################################### # Constants ##################################################################### @@ -89,7 +101,7 @@ use constant INDEX_DROPS_REQUIRE_FK_DROPS => 1; sub quote { my $self = shift; - my $retval = $self->SUPER::quote(@_); + my $retval = $self->dbh->quote(@_); trick_taint($retval) if defined $retval; return $retval; } @@ -138,11 +150,6 @@ sub _connect { # instantiate the correct DB specific module - # BMO - enable instrumentation of db calls - if (Bugzilla->metrics_enabled) { - $pkg_module = 'Bugzilla::Metrics::Mysql'; - } - my $dbh = $pkg_module->new($params); return $dbh; @@ -206,7 +213,6 @@ sub bz_check_server_version { my ($self, $db, $output) = @_; my $sql_vers = $self->bz_server_version; - $self->disconnect; my $sql_want = $db->{db_version}; my $version_ok = vers_cmp($sql_vers, $sql_want) > -1 ? 1 : 0; @@ -262,7 +268,6 @@ sub bz_create_database { } } - $dbh->disconnect; } # A helper for bz_create_database and bz_check_requirements. @@ -271,6 +276,7 @@ sub _get_no_db_connection { my $dbh; my %connect_params = %{ Bugzilla->localconfig }; $connect_params{db_name} = ''; + local %Connector = (); my $conn_success = eval { $dbh = _connect(\%connect_params); }; @@ -1247,14 +1253,14 @@ sub bz_rollback_transaction { # Subclass Helpers ##################################################################### -sub db_new { - my ($class, $params) = @_; +sub _build_dbh { + my ($self) = @_; my ($dsn, $user, $pass, $override_attrs) = - @$params{qw(dsn user pass attrs)}; + map { $self->$_ } qw(dsn user pass attrs); # set up default attributes used to connect to the database # (may be overridden by DB driver implementations) - my $attributes = { RaiseError => 0, + my $attributes = { RaiseError => 1, AutoCommit => 1, PrintError => 0, ShowErrorStatement => 1, @@ -1272,20 +1278,16 @@ sub db_new { $attributes->{$key} = $override_attrs->{$key}; } } + my $class = ref $self; + if ($class->can('on_dbi_connected')) { + $attributes->{Callbacks} = { + connected => sub { $class->on_dbi_connected(@_); return }, + } + } - # connect using our known info to the specified db - my $self = DBI->connect($dsn, $user, $pass, $attributes) - or die "\nCan't connect to the database.\nError: $DBI::errstr\n" - . " Is your database installed and up and running?\n Do you have" - . " the correct username and password selected in localconfig?\n\n"; - - # RaiseError was only set to 0 so that we could catch the - # above "die" condition. - $self->{RaiseError} = 1; - - bless ($self, $class); + my $connector = $Connector{"$user.$dsn"} //= DBIx::Connector->new($dsn, $user, $pass, $attributes); - return $self; + return $connector->dbh; } ##################################################################### |