Bug 14778: Get rid of DBIx::Connector
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 3 Sep 2015 14:11:21 +0000 (15:11 +0100)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 23 Oct 2015 15:01:17 +0000 (12:01 -0300)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
C4/Context.pm
C4/Installer/PerlDependencies.pm
Koha/Database.pm

index 26eeb40..bc95baf 100644 (file)
@@ -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
index c2bc7e6..f93e266 100644 (file)
@@ -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',
index 5587570..1502364 100644 (file)
@@ -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;
 }