X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDBConnect.pm;h=9142f1c71b6a6b50365adf2cd6967299277cee67;hb=90c31393ba44d8c9f52bea079f57582958421cf2;hp=e67d8d095ee0cc8d822a0d7fd6ba72bf0bbea600;hpb=beb61b2e270836ccd11e00ce68ff884cfb7a530d;p=kivitendo-erp.git diff --git a/SL/DBConnect.pm b/SL/DBConnect.pm index e67d8d095..9142f1c71 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,13 +30,39 @@ 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'); my $db_cfg = SL::DB->registry->entry(domain => $domain, type => $type) || { }; return ( - 'dbi:' . $db_cfg->{dbi_driver} . ':dbname=' . $db_cfg->{database} . ';host=' . ($db_cfg->{host} || 'localhost') . ';port=' . ($db_cfg->{port} || 5432), + 'dbi:Pg:dbname=' . $db_cfg->{database} . ';host=' . ($db_cfg->{host} || 'localhost') . ';port=' . ($db_cfg->{port} || 5432), $db_cfg->{username}, $db_cfg->{password}, $self->get_options(%{ $db_cfg->{connect_options} || {} }, @args), @@ -38,7 +72,7 @@ sub get_connect_args { sub get_options { my $self = shift; my $options = { - pg_enable_utf8 => $::locale->is_utf8, + pg_enable_utf8 => 1, @_ }; @@ -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