From: Moritz Bunkus Date: Thu, 13 Jun 2013 14:50:02 +0000 (+0200) Subject: Handling vom initialen SQL (SET DateStyle ...) zentralisiert X-Git-Tag: release-3.1.0beta1~331^2~14 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=576c2a14f72af8f2cfe5679b6c264f13aff39ddf;p=kivitendo-erp.git Handling vom initialen SQL (SET DateStyle ...) zentralisiert Damit auch das Datumsformat für $::form->get_standard_dbh und Konsorten gefixt. --- diff --git a/SL/DB.pm b/SL/DB.pm index 2d394f690..54214c8ca 100644 --- a/SL/DB.pm +++ b/SL/DB.pm @@ -14,7 +14,7 @@ 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; @@ -30,18 +30,9 @@ 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; @@ -49,15 +40,12 @@ sub _register_db { 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 => $::locale && $::locale->is_utf8, }, ); - if ($::myconfig{dateformat}) { - $common_connect_settings{european_dates} = 1 if ($_dateformats{ $::myconfig{dateformat} } || '') =~ m/european/i; - } - if (($type eq 'KIVITENDO_AUTH') && $::auth && $::auth->{DB_config} && $::auth->session_tables_present) { %specific_connect_settings = ( database => $::auth->{DB_config}->{db}, @@ -109,19 +97,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 = (); diff --git a/SL/DBConnect.pm b/SL/DBConnect.pm index aeb8719da..afdf2c9e1 100644 --- a/SL/DBConnect.pm +++ b/SL/DBConnect.pm @@ -5,7 +5,15 @@ use strict; use DBI; use SL::DB; -sub connect { +my %dateformat_to_datestyle = ( + 'yy-mm-dd' => 'ISO', + 'yyyy-mm-dd' => 'ISO', + 'mm/dd/yy' => 'SQL, US', + 'dd/mm/yy' => 'SQL, EUROPEAN', + 'dd.mm.yy' => 'GERMAN' +); + +sub _connect { my ($self, @args) = @_; @args = $self->get_connect_args if !@args; @@ -22,6 +30,32 @@ sub connect { return DBIx::Log4perl->connect(@args); } +sub connect { + my ($self, @args) = @_; + + my $dbh = $self->_connect(@args); + return undef if !$dbh; + + my $initial_sql = $self->get_initial_sql; + $dbh->do($initial_sql) if $initial_sql; + + return $dbh; +} + +sub get_datestyle { + my ($self, $dateformat) = @_; + return $dateformat_to_datestyle{ $dateformat || $::myconfig{dateformat} }; +} + +sub get_initial_sql { + my ($self) = @_; + + return undef if !%::myconfig || !$::myconfig{dateformat}; + + my $datestyle = $self->get_datestyle; + return $datestyle ? qq|SET DateStyle to '${datestyle}'| : ''; +} + sub get_connect_args { my ($self, @args) = @_; my ($domain, $type) = SL::DB::_register_db(SL::DB->default_domain, 'KIVITENDO'); @@ -97,6 +131,19 @@ C<%options> are optional database options like C (fourth parameter to L). They're merged with default settings by filtering them through L/get_options>. +=item C + +Returns the appropriate value for the C SQL call +depending on C<$dateformat> (e.g. C if C<$dateformat> +equals C). If C<$dateformat> is not given then it defaults +to C<$::myconfig{dateformat}>. + +=item C + +Returns SQL commands that should be executed right after a connection +has been established. This is usually the call to configure the +C format used by the database. + =item C Returns a hash reference of database options (fourth parameter to