X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=Koha%2FDatabase.pm;h=376f910c988526e6f25641bfd6b88a59ac0f28fe;hb=689446fda0fa665202c9e32c50705ba828337c94;hp=15023643b9abceeedc8f1b50d413b07f21c4f750;hpb=5d8cb8b2ee508deaa272f9b7bfa36e73235c957c;p=koha-ffzg.git diff --git a/Koha/Database.pm b/Koha/Database.pm index 15023643b9..376f910c98 100644 --- a/Koha/Database.pm +++ b/Koha/Database.pm @@ -5,18 +5,18 @@ package Koha::Database; # # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 3 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with Koha; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . =head1 NAME @@ -25,21 +25,102 @@ Koha::Database =head1 SYNOPSIS use Koha::Database; - my $database = Koha::Database->new(); - my $schema = $database->schema(); + my $schema = Koha::Database->schema(); =head1 FUNCTIONS =cut use Modern::Perl; -use Carp; -use C4::Context; -use base qw(Class::Accessor); +use DBI; +use Koha::Config; -use vars qw($database); +our $database; + +=head2 new + + $schema = Koha::Database->new->schema; + + FIXME: It is useless to have a Koha::Database object since all methods + below act as class methods + Koha::Database->new->schema is exactly the same as Koha::Database->schema + We should use Koha::Database->schema everywhere and remove the `new` method + +=cut + +sub new { bless {}, shift } + +=head2 dbh + + Returns a database handler without loading the DBIx::Class schema. + +=cut + +sub dbh { + my $config = Koha::Config->get_instance; + my $driver = db_scheme2dbi($config->get('db_scheme')); + my $user = $config->get("user"), + my $pass = $config->get("pass"), + my $dsn = sprintf( + 'dbi:%s:database=%s;host=%s;port=%s', + $driver, + $config->get("database_test") || $config->get("database"), + $config->get("hostname"), + $config->get("port") || '', + ); + + my $attr = { + RaiseError => 1, + PrintError => 1, + }; + + if ($driver eq 'mysql') { + my $tls = $config->get("tls"); + if ($tls && $tls eq 'yes') { + $dsn .= sprintf( + ';mysql_ssl=1;mysql_ssl_client_key=%s;mysql_ssl_client_cert=%s;mysql_ssl_ca_file=%s', + $config->get('key'), + $config->get('cert'), + $config->get('ca'), + ); + } + + $attr->{mysql_enable_utf8} = 1; + } + + my $dbh = DBI->connect($dsn, $user, $pass, $attr); + + if ($dbh) { + my @queries; + my $tz = $config->timezone; + $tz = '' if $tz eq 'local'; + + if ($driver eq 'mysql') { + push @queries, "SET NAMES 'utf8mb4'"; + push @queries, qq{SET time_zone = "$tz"} if $tz; + if ( $config->get('strict_sql_modes') + || ( exists $ENV{_} && $ENV{_} =~ m|prove| ) + || $ENV{KOHA_TESTING} + ) { + push @queries, q{ + SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' + }; + } else { + push @queries, q{SET sql_mode = 'IGNORE_SPACE,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'} + } + } elsif ($driver eq 'Pg') { + push @queries, qq{SET TIME ZONE = "$tz"} if $tz; + push @queries, q{set client_encoding = 'UTF8'}; + } + + foreach my $query (@queries) { + $dbh->do($query); + } + } + + return $dbh; +} -__PACKAGE__->mk_accessors(qw( )); # _new_schema # Internal helper function (not a method!). This creates a new @@ -49,58 +130,35 @@ sub _new_schema { require Koha::Schema; - my $context = C4::Context->new(); - - 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 $schema = Koha::Schema->connect({ + dbh_maker => \&Koha::Database::dbh, + quote_names => 1, + auto_savepoint => 1, + }); my $dbh = $schema->storage->dbh; eval { - $dbh->{RaiseError} = 1; + my $HandleError = $dbh->{HandleError}; + if ( $ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} ) { + $dbh->{HandleError} = sub { return 1 }; + } $dbh->do(q| SELECT * FROM systempreferences WHERE 1 = 0 | ); - $dbh->{RaiseError} = $ENV{DEBUG} ? 1 : 0; + $dbh->{HandleError} = $HandleError; }; - $dbh->{RaiseError} = 0 if $@; + + if ( $@ ) { + $dbh->{HandleError} = sub { warn $_[0]; return 1 }; + } return $schema; } =head2 schema - $schema = $database->schema; + $schema = Koha::Database->schema; + $schema = Koha::Database->schema({ new => 1 }); Returns a database handle connected to the Koha database for the current context. If no connection has yet been made, this method @@ -114,14 +172,12 @@ possibly C<&set_schema>. =cut sub schema { - my $self = shift; - my $sth; + my ($class, $params) = @_; - if ( defined( $database->{schema} ) and $database->{schema}->storage->connected() ) { - return $database->{schema}; + unless ( $params->{new} ) { + return $database->{schema} if defined $database->{schema}; } - # No database handle or it died . Create one. $database->{schema} = &_new_schema(); return $database->{schema}; } @@ -200,6 +256,39 @@ sub restore_schema { # return something, then this function should, too. } +=head2 get_schema_cached + +=cut + +sub get_schema_cached { + return $database->{schema}; +} + +=head2 flush_schema_cache + +=cut + +sub flush_schema_cache { + delete $database->{schema}; + return 1; +} + +=head2 db_scheme2dbi + + my $dbd_driver_name = Koha::Database::db_scheme2dbi($scheme); + +This routines translates a database type to part of the name +of the appropriate DBD driver to use when establishing a new +database connection. It recognizes 'mysql' and 'Pg'; if any +other scheme is supplied it defaults to 'mysql'. + +=cut + +sub db_scheme2dbi { + my $scheme = shift // ''; + return $scheme eq 'Pg' ? $scheme : 'mysql'; +} + =head2 EXPORT None by default.