7 use SL::DBConnect::Cache;
9 my %dateformat_to_datestyle = (
11 'yyyy-mm-dd' => 'ISO',
12 'mm/dd/yy' => 'SQL, US',
13 'dd/mm/yy' => 'SQL, EUROPEAN',
14 'dd.mm.yy' => 'GERMAN'
18 my ($self, @args) = @_;
20 return DBI->connect(@args) unless $::lx_office_conf{debug} && $::lx_office_conf{debug}->{dbix_log4perl};
22 require Log::Log4perl;
23 require DBIx::Log4perl;
25 my $filename = $::lxdebug->file;
26 my $config = $::lx_office_conf{debug}->{dbix_log4perl_config};
27 $config =~ s/LXDEBUGFILE/${filename}/g;
29 Log::Log4perl->init(\$config);
30 return DBIx::Log4perl->connect(@args);
34 my ($self, @args) = @_;
35 @args = $self->get_connect_args if !@args;
36 my $initial_sql = $self->get_initial_sql;
38 if (my $cached_dbh = SL::DBConnect::Cache->get(@args, $initial_sql)) {
42 my $dbh = $self->_connect(@args);
43 return undef if !$dbh;
46 $dbh->do($initial_sql);
47 $dbh->commit if !$dbh->{AutoCommit};
49 SL::DBConnect::Cache->store($dbh, @args, $initial_sql);
55 my ($self, $dateformat) = @_;
56 return $dateformat_to_datestyle{ $dateformat || $::myconfig{dateformat} // '' };
62 return undef if !%::myconfig || !$::myconfig{dateformat};
64 my $datestyle = $self->get_datestyle;
65 return $datestyle ? qq|SET DateStyle to '${datestyle}'| : '';
68 sub get_connect_args {
69 my ($self, @args) = @_;
70 my ($domain, $type) = SL::DB::_register_db(SL::DB->default_domain, 'KIVITENDO');
71 my $db_cfg = SL::DB->registry->entry(domain => $domain, type => $type) || { };
74 'dbi:Pg:dbname=' . $db_cfg->{database} . ';host=' . ($db_cfg->{host} || 'localhost') . ';port=' . ($db_cfg->{port} || 5432),
77 $self->get_options(%{ $db_cfg->{connect_options} || {} }, @args),
100 SL::DBConnect - Connect to database for configured client/user,
101 optionally routing through DBIx::Log4perl
105 # Connect to default database of current user/client, disabling auto
107 my @options_suitable_for_dbi_connect =
108 SL::DBConnect->get_connect_args(AutoCommit => 0);
109 my $dbh = SL::DBConnect->connect(@options_suitable_for_dbi_connect);
111 # Connect to a very specific database:
112 my $dbh = SL::DBConnect->connect('dbi:Pg:dbname=demo', 'user', 'password');
118 =item C<connect [@dbi_args]>
120 Connects to the database. If the configuration parameter
121 C<debug.dbix_log4perl> is set then the call is made through
122 L<DBIx::Log4per/connect>. Otherwise L<DBI/connect> is called directly.
124 In each case C<@dbi_args> is passed through as-is.
126 If C<@dbi_args> are not given they're generated by a call to
127 L</get_connect_args>.
129 =item C<get_connect_args [%options]>
131 Returns an array of database connection settings suitable to a call to
132 L<DBI/connect> or L</connect>. The settings to use are retrieved by
133 calling L<SL::DB/_register_db>.
135 This requires that a client has been set up with
136 L<SL::Auth/set_client> or that C<%::myconfig> contains legacy
139 C<%options> are optional database options like C<AutoCommit> (fourth
140 parameter to L<DBI/connect>). They're merged with default settings by
141 filtering them through L/get_options>.
143 =item C<get_datestyle [$dateformat]>
145 Returns the appropriate value for the C<SET DateStyle to...> SQL call
146 depending on C<$dateformat> (e.g. C<SQL, EUROPEAN> if C<$dateformat>
147 equals C<dd.mm.yy>). If C<$dateformat> is not given then it defaults
148 to C<$::myconfig{dateformat}>.
150 =item C<get_initial_sql>
152 Returns SQL commands that should be executed right after a connection
153 has been established. This is usually the call to configure the
154 C<DateStyle> format used by the database.
156 =item C<get_options [%options]>
158 Returns a hash reference of database options (fourth parameter to
159 L<DBI/connect>) merged with certain default options.
169 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>