$VERSION = '3.07.00.049';
}
-use DBIx::Connector;
use Encode;
use ZOOM;
use XML::Simple;
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
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;
}