X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB.pm;h=26c01ccbadbe6631325860f5ff6ab459b1d9964a;hb=4e0bef2d1ee1fa29e3fa85a97f34e707fe4e287d;hp=28626ac6655ae226fe3640b38921bf3875056717;hpb=d8ac08282dad52789b8bea785e8cddae44085483;p=kivitendo-erp.git diff --git a/SL/DB.pm b/SL/DB.pm index 28626ac66..26c01ccba 100644 --- a/SL/DB.pm +++ b/SL/DB.pm @@ -4,7 +4,6 @@ use strict; use Carp; use Data::Dumper; -use SL::DBConnect; use English qw(-no_match_vars); use Rose::DB; use Rose::DBx::Cache::Anywhere; @@ -14,11 +13,13 @@ use base qw(Rose::DB); __PACKAGE__->db_cache_class('Rose::DBx::Cache::Anywhere'); __PACKAGE__->use_private_registry; -my (%_db_registered, %_initial_sql_executed); +my (%_db_registered); sub dbi_connect { shift; + # runtime require to break circular include + require SL::DBConnect; return SL::DBConnect->connect(@_); } @@ -30,64 +31,61 @@ 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', - 'dd/mm/yy' => 'SQL, EUROPEAN', - 'dd.mm.yy' => 'GERMAN' - ); - sub _register_db { my $domain = shift; my $type = shift; - my %connect_settings; - my $initial_sql; + require SL::DBConnect; + my %specific_connect_settings; + my %common_connect_settings = ( + driver => 'Pg', + european_dates => ((SL::DBConnect->get_datestyle || '') =~ m/european/i) ? 1 : 0, + connect_options => { + pg_enable_utf8 => 1, + }, + ); if (($type eq 'KIVITENDO_AUTH') && $::auth && $::auth->{DB_config} && $::auth->session_tables_present) { - %connect_settings = ( driver => 'Pg', - database => $::auth->{DB_config}->{db}, - host => $::auth->{DB_config}->{host} || 'localhost', - port => $::auth->{DB_config}->{port} || 5432, - username => $::auth->{DB_config}->{user}, - password => $::auth->{DB_config}->{password}, - connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8, - }); - } - - if (!%connect_settings && %::myconfig) { - my $european_dates = 0; - if ($::myconfig{dateformat}) { - $european_dates = 1 if $_dateformats{ $::myconfig{dateformat} } - && $_dateformats{ $::myconfig{dateformat} } =~ m/european/i; - } - - %connect_settings = ( driver => $::myconfig{dbdriver} || 'Pg', - database => $::myconfig{dbname}, - host => $::myconfig{dbhost} || 'localhost', - port => $::myconfig{dbport} || 5432, - username => $::myconfig{dbuser}, - password => $::myconfig{dbpasswd}, - connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8, - }, - european_dates => $european_dates); - } + %specific_connect_settings = ( + database => $::auth->{DB_config}->{db}, + host => $::auth->{DB_config}->{host} || 'localhost', + port => $::auth->{DB_config}->{port} || 5432, + username => $::auth->{DB_config}->{user}, + password => $::auth->{DB_config}->{password}, + ); + + } elsif ($::auth && $::auth->client) { + my $client = $::auth->client; + %specific_connect_settings = ( + database => $client->{dbname}, + host => $client->{dbhost} || 'localhost', + port => $client->{dbport} || 5432, + username => $client->{dbuser}, + password => $client->{dbpasswd}, + ); + + } elsif (%::myconfig && $::myconfig{dbname}) { + %specific_connect_settings = ( + database => $::myconfig{dbname}, + host => $::myconfig{dbhost} || 'localhost', + port => $::myconfig{dbport} || 5432, + username => $::myconfig{dbuser}, + password => $::myconfig{dbpasswd}, + ); - if (!%connect_settings) { + } else { $type = 'KIVITENDO_EMPTY'; - %connect_settings = ( driver => 'Pg' ); } + my %connect_settings = (%common_connect_settings, %specific_connect_settings); my %flattened_settings = _flatten_settings(%connect_settings); - $domain = 'KIVITENDO' if $type =~ m/^KIVITENDO/; - $type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort grep { $_ ne 'dbpasswd' } keys %flattened_settings); - my $idx = "${domain}::${type}"; + $domain = 'KIVITENDO' if $type =~ m/^KIVITENDO/; + $type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort grep { $_ ne 'dbpasswd' } keys %flattened_settings); + my $idx = "${domain}::${type}"; if (!$_db_registered{$idx}) { $_db_registered{$idx} = 1; @@ -101,19 +99,6 @@ 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 = (); @@ -181,7 +166,7 @@ starting one if none is currently active. Example: }); One big difference to L is the return code -handling. If a transaction is already active then C +handling. If a transaction is already active then C simply returns the result of calling C<$code_ref> as-is. Otherwise the return value depends on the result of the underlying