Bug 22045: Compiled CSS
[koha.git] / Koha / Database.pm
index 4c6cae6..ea8a10a 100644 (file)
@@ -35,9 +35,10 @@ Koha::Database
 use Modern::Perl;
 use Carp;
 use C4::Context;
-use Koha::Schema;
 use base qw(Class::Accessor);
 
+use vars qw($database);
+
 __PACKAGE__->mk_accessors(qw( ));
 
 # _new_schema
@@ -45,23 +46,78 @@ __PACKAGE__->mk_accessors(qw( ));
 # database connection from the data given in the current context, and
 # returns it.
 sub _new_schema {
-    my $db_driver;
+
+    require Koha::Schema;
+
     my $context = C4::Context->new();
-    if ( $context->config("db_scheme") ) {
-        $db_driver = $context->db_scheme2dbi( $context->config("db_scheme") );
-    }
-    else {
-        $db_driver = "mysql";
-    }
+
+    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 $schema    = Koha::Schema->connect(
-        "DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port",
-        $db_user, $db_passwd );
+    my $tls = $context->config("tls");
+    my $tls_options;
+    if( $tls && $tls eq 'yes' ) {
+        my $ca = $context->config('ca');
+        my $cert = $context->config('cert');
+        my $key = $context->config('key');
+        $tls_options = ";mysql_ssl=1;mysql_ssl_client_key=".$key.";mysql_ssl_client_cert=".$cert.";mysql_ssl_ca_file=".$ca;
+    }
+
+
+
+    my ( %encoding_attr, $encoding_query, $tz_query, $sql_mode_query );
+    my $tz = C4::Context->timezone;
+    $tz = q{} if ( $tz eq 'local' );
+    if ( $db_driver eq 'mysql' ) {
+        %encoding_attr = ( mysql_enable_utf8 => 1 );
+        $encoding_query = "set NAMES 'utf8mb4'";
+        $tz_query = qq(SET time_zone = "$tz") if $tz;
+        if ( ( exists $ENV{_} && $ENV{_} =~ m|prove| ) or C4::Context->config('strict_sql_modes') ) {
+            $sql_mode_query = q{SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'};
+        } else {
+            $sql_mode_query = q{SET sql_mode = 'IGNORE_SPACE,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'};
+        }
+    }
+    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".($tls_options? $tls_options : ""),
+            user => $db_user,
+            password => $db_passwd,
+            %encoding_attr,
+            RaiseError => $ENV{DEBUG} ? 1 : 0,
+            PrintError => 1,
+            unsafe => 1,
+            quote_names => 1,
+            on_connect_do => [
+                $encoding_query || (),
+                $tz_query || (),
+                $sql_mode_query || (),
+            ]
+        }
+    );
+
+    my $dbh = $schema->storage->dbh;
+    eval {
+        $dbh->{RaiseError} = 1;
+        if ( $ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} ) {
+            $dbh->{RaiseError} = 0;
+            $dbh->{PrintError} = 0;
+        }
+        $dbh->do(q|
+            SELECT * FROM systempreferences WHERE 1 = 0 |
+        );
+        $dbh->{RaiseError} = $ENV{DEBUG} ? 1 : 0;
+    };
+    $dbh->{RaiseError} = 0 if $@;
+
     return $schema;
 }
 
@@ -82,15 +138,14 @@ possibly C<&set_schema>.
 
 sub schema {
     my $self = shift;
-    my $sth;
-    if ( defined( $self->{"schema"} ) && $self->{"schema"}->storage->connected() ) {
-        return $self->{"schema"};
-    }
+    my $params = shift;
 
-    # No database handle or it died . Create one.
-    $self->{"schema"} = &_new_schema();
+    unless ( $params->{new} ) {
+        return $database->{schema} if defined $database->{schema};
+    }
 
-    return $self->{"schema"};
+    $database->{schema} = &_new_schema();
+    return $database->{schema};
 }
 
 =head2 new_schema
@@ -138,8 +193,8 @@ sub set_schema {
     # We assume that $new_schema is all good: if the caller wants to
     # screw himself by passing an invalid handle, that's fine by
     # us.
-    push @{ $self->{"schema_stack"} }, $self->{"schema"};
-    $self->{"schema"} = $new_schema;
+    push @{ $database->{schema_stack} }, $database->{schema};
+    $database->{schema} = $new_schema;
 }
 
 =head2 restore_schema
@@ -154,19 +209,36 @@ C<$database-E<gt>set_schema>.
 sub restore_schema {
     my $self = shift;
 
-    if ( $#{ $self->{"schema_stack"} } < 0 ) {
+    if ( $#{ $database->{schema_stack} } < 0 ) {
 
         # Stack underflow
         die "SCHEMA stack underflow";
     }
 
     # Pop the old database handle and set it.
-    $self->{"schema"} = pop @{ $self->{"schema_stack"} };
+    $database->{schema} = pop @{ $database->{schema_stack} };
 
     # FIXME - If it is determined that restore_context should
     # 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 EXPORT
 
 None by default.