From 5d8cb8b2ee508deaa272f9b7bfa36e73235c957c Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 3 Sep 2015 15:11:21 +0100 Subject: [PATCH] Bug 14778: Get rid of DBIx::Connector Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- C4/Context.pm | 51 +------------------------------- C4/Installer/PerlDependencies.pm | 5 ---- Koha/Database.pm | 48 ++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 58 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 26eeb40885..bc95baf2d4 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -97,7 +97,6 @@ BEGIN { $VERSION = '3.07.00.049'; } -use DBIx::Connector; use Encode; use ZOOM; use XML::Simple; @@ -726,55 +725,7 @@ sub _new_Zconn { sub _new_dbh { - ## $context - ## correct name for db_scheme - my $db_driver = $context->{db_driver}; - - my $db_name = $context->config("database"); - my $db_host = $context->config("hostname"); - my $db_port = $context->config("port") || ''; - my $db_user = $context->config("user"); - my $db_passwd = $context->config("pass"); - # MJR added or die here, as we can't work without dbh - my $dbh = DBIx::Connector->connect( - "dbi:$db_driver:dbname=$db_name;host=$db_host;port=$db_port", - $db_user, $db_passwd, - { - 'RaiseError' => $ENV{DEBUG} ? 1 : 0 - } - ); - - # Check for the existence of a systempreference table; if we don't have this, we don't - # have a valid database and should not set RaiseError in order to allow the installer - # to run; installer will not run otherwise since we raise all db errors - - eval { - local $dbh->{PrintError} = 0; - local $dbh->{RaiseError} = 1; - $dbh->do(qq{SELECT * FROM systempreferences WHERE 1 = 0 }); - }; - - if ($@) { - $dbh->{RaiseError} = 0; - } - - if ( $db_driver eq 'mysql' ) { - $dbh->{mysql_auto_reconnect} = 1; - } - - my $tz = $ENV{TZ}; - if ( $db_driver eq 'mysql' ) { - # Koha 3.0 is utf-8, so force utf8 communication between mySQL and koha, whatever the mysql default config. - # this is better than modifying my.cnf (and forcing all communications to be in utf8) - $dbh->{'mysql_enable_utf8'}=1; #enable - $dbh->do("set NAMES 'utf8'"); - ($tz) and $dbh->do(qq(SET time_zone = "$tz")); - } - elsif ( $db_driver eq 'Pg' ) { - $dbh->do( "set client_encoding = 'UTF8';" ); - ($tz) and $dbh->do(qq(SET TIME ZONE = "$tz")); - } - return $dbh; + Koha::Database->schema->storage->dbh; } =head2 dbh diff --git a/C4/Installer/PerlDependencies.pm b/C4/Installer/PerlDependencies.pm index c2bc7e658e..f93e26624f 100644 --- a/C4/Installer/PerlDependencies.pm +++ b/C4/Installer/PerlDependencies.pm @@ -64,11 +64,6 @@ our $PERL_DEPS = { 'required' => '1', 'min_ver' => '0.07039' }, - 'DBIx::Connector' => { - 'usage' => 'Core', - 'required' => '1', - 'min_ver' => '0.47' - }, 'Net::Z3950::ZOOM' => { 'usage' => 'Core', 'required' => '1', diff --git a/Koha/Database.pm b/Koha/Database.pm index 5587570e23..15023643b9 100644 --- a/Koha/Database.pm +++ b/Koha/Database.pm @@ -48,11 +48,53 @@ __PACKAGE__->mk_accessors(qw( )); sub _new_schema { require Koha::Schema; + my $context = C4::Context->new(); - # we are letting C4::Context->dbh not set the RaiseError handle attribute - # for now for compatbility purposes - my $schema = Koha::Schema->connect( sub { C4::Context->dbh }, { unsafe => 1 } ); + my $db_driver = $context->{db_driver}; + + my $db_name = $context->config("database"); + my $db_host = $context->config("hostname"); + my $db_port = $context->config("port") || ''; + my $db_user = $context->config("user"); + my $db_passwd = $context->config("pass"); + + my ( %encoding_attr, $encoding_query, $tz_query ); + my $tz = $ENV{TZ}; + if ( $db_driver eq 'mysql' ) { + %encoding_attr = ( mysql_enable_utf8 => 1 ); + $encoding_query = "set NAMES 'utf8'"; + $tz_query = qq(SET time_zone = "$tz") if $tz; + } + elsif ( $db_driver eq 'Pg' ) { + $encoding_query = "set client_encoding = 'UTF8';"; + $tz_query = qq(SET TIME ZONE = "$tz") if $tz; + } + my $schema = Koha::Schema->connect( + { + dsn => "dbi:$db_driver:database=$db_name;host=$db_host;port=$db_port", + user => $db_user, + password => $db_passwd, + %encoding_attr, + RaiseError => $ENV{DEBUG} ? 1 : 0, + unsafe => 1, + on_connect_do => [ + $encoding_query || (), + $tz_query || (), + ] + } + ); + + my $dbh = $schema->storage->dbh; + eval { + $dbh->{RaiseError} = 1; + $dbh->do(q| + SELECT * FROM systempreferences WHERE 1 = 0 | + ); + $dbh->{RaiseError} = $ENV{DEBUG} ? 1 : 0; + }; + $dbh->{RaiseError} = 0 if $@; + return $schema; } -- 2.39.5