Merge branch 'master' of git@lx-office.linet-services.de:lx-office-erp
[kivitendo-erp.git] / SL / DB.pm
index 4a78738..f90a9c6 100644 (file)
--- a/SL/DB.pm
+++ b/SL/DB.pm
@@ -13,6 +13,22 @@ __PACKAGE__->use_private_registry;
 
 my (%_db_registered, %_initial_sql_executed);
 
+sub dbi_connect {
+  shift;
+
+  return DBI->connect(@_) unless $::lx_office_conf{debug} && $::lx_office_conf{debug}->{dbix_log4perl};
+
+  require Log::Log4perl;
+  require DBIx::Log4perl;
+
+  my $filename =  $LXDebug::file_name;
+  my $config   =  $::lx_office_conf{debug}->{dbix_log4perl_config};
+  $config      =~ s/LXDEBUGFILE/${filename}/g;
+
+  Log::Log4perl->init(\$config);
+  return DBIx::Log4perl->connect(@_);
+}
+
 sub create {
   my $domain = shift || SL::DB->default_domain;
   my $type   = shift || SL::DB->default_type;
@@ -21,14 +37,26 @@ sub create {
 
   my $db = __PACKAGE__->new_or_cached(domain => $domain, type => $type);
 
+  _execute_initial_sql($db);
+
   return $db;
 }
 
+my %_dateformats = ( 'yy-mm-dd'   => 'ISO',
+                     'yyyy-mm-dd' => 'ISO',
+                     'mm/dd/yy'   => 'SQL, US',
+                     'mm-dd-yy'   => 'POSTGRES, US',
+                     'dd/mm/yy'   => 'SQL, EUROPEAN',
+                     'dd-mm-yy'   => 'POSTGRES, EUROPEAN',
+                     'dd.mm.yy'   => 'GERMAN'
+                   );
+
 sub _register_db {
   my $domain = shift;
   my $type   = shift;
 
   my %connect_settings;
+  my $initial_sql;
 
   if (!%::myconfig) {
     $type = 'LXOFFICE_EMPTY';
@@ -44,6 +72,11 @@ sub _register_db {
                           connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8,
                                              });
   } else {
+    my $european_dates = 0;
+    if ($::myconfig{dateformat}) {
+      $european_dates = 1 if $_dateformats{ $::myconfig{dateformat} } =~ m/european/i;
+    }
+
     %connect_settings = ( driver          => $::myconfig{dbdriver} || 'Pg',
                           database        => $::myconfig{dbname},
                           host            => $::myconfig{dbhost},
@@ -51,7 +84,8 @@ sub _register_db {
                           username        => $::myconfig{dbuser},
                           password        => $::myconfig{dbpasswd},
                           connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8,
-                                             });
+                                             },
+                          european_dates  => $european_dates);
   }
 
   my %flattened_settings = _flatten_settings(%connect_settings);
@@ -72,6 +106,19 @@ sub _register_db {
   return ($domain, $type);
 }
 
+sub _execute_initial_sql {
+  my ($db) = @_;
+
+  return if $_initial_sql_executed{$db} || !%::myconfig || !$::myconfig{dateformat};
+
+  $_initial_sql_executed{$db} = 1;
+
+  # Don't rely on dboptions being set properly. Chose them from
+  # dateformat instead.
+  my $pg_dateformat = $_dateformats{ $::myconfig{dateformat} };
+  $db->dbh->do("set DateStyle to '${pg_dateformat}'") if $pg_dateformat;
+}
+
 sub _flatten_settings {
   my %settings  = @_;
   my %flattened = ();