__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;
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 %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},
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 = ();
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;
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');
parameter to L<DBI/connect>). They're merged with default settings by
filtering them through L/get_options>.
+=item C<get_datestyle [$dateformat]>
+
+Returns the appropriate value for the C<SET DateStyle to...> SQL call
+depending on C<$dateformat> (e.g. C<SQL, EUROPEAN> if C<$dateformat>
+equals C<dd.mm.yy>). If C<$dateformat> is not given then it defaults
+to C<$::myconfig{dateformat}>.
+
+=item C<get_initial_sql>
+
+Returns SQL commands that should be executed right after a connection
+has been established. This is usually the call to configure the
+C<DateStyle> format used by the database.
+
=item C<get_options [%options]>
Returns a hash reference of database options (fourth parameter to