8 my %dateformat_to_datestyle = (
 
  10   'yyyy-mm-dd' => 'ISO',
 
  11   'mm/dd/yy'   => 'SQL, US',
 
  12   'dd/mm/yy'   => 'SQL, EUROPEAN',
 
  13   'dd.mm.yy'   => 'GERMAN'
 
  17   my ($self, @args) = @_;
 
  18   @args = $self->get_connect_args if !@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) = @_;
 
  36   my $dbh = $self->_connect(@args);
 
  37   return undef if !$dbh;
 
  39   my $initial_sql = $self->get_initial_sql;
 
  40   $dbh->do($initial_sql) if $initial_sql;
 
  46   my ($self, $dateformat) = @_;
 
  47   return $dateformat_to_datestyle{ $dateformat || $::myconfig{dateformat} // '' };
 
  53   return undef if !%::myconfig || !$::myconfig{dateformat};
 
  55   my $datestyle = $self->get_datestyle;
 
  56   return $datestyle ? qq|SET DateStyle to '${datestyle}'| : '';
 
  59 sub get_connect_args {
 
  60   my ($self, @args)   = @_;
 
  61   my ($domain, $type) = SL::DB::_register_db(SL::DB->default_domain, 'KIVITENDO');
 
  62   my $db_cfg          = SL::DB->registry->entry(domain => $domain, type => $type) || { };
 
  65     'dbi:Pg:dbname=' . $db_cfg->{database} . ';host=' . ($db_cfg->{host} || 'localhost') . ';port=' . ($db_cfg->{port} || 5432),
 
  68     $self->get_options(%{ $db_cfg->{connect_options} || {} }, @args),
 
  91 SL::DBConnect - Connect to database for configured client/user,
 
  92 optionally routing through DBIx::Log4perl
 
  96   # Connect to default database of current user/client, disabling auto
 
  98   my @options_suitable_for_dbi_connect =
 
  99     SL::DBConnect->get_connect_args(AutoCommit => 0);
 
 100   my $dbh = SL::DBConnect->connect(@options_suitable_for_dbi_connect);
 
 102   # Connect to a very specific database:
 
 103   my $dbh = SL::DBConnect->connect('dbi:Pg:dbname=demo', 'user', 'password');
 
 109 =item C<connect [@dbi_args]>
 
 111 Connects to the database. If the configuration parameter
 
 112 C<debug.dbix_log4perl> is set then the call is made through
 
 113 L<DBIx::Log4per/connect>. Otherwise L<DBI/connect> is called directly.
 
 115 In each case C<@dbi_args> is passed through as-is.
 
 117 If C<@dbi_args> are not given they're generated by a call to
 
 118 L</get_connect_args>.
 
 120 =item C<get_connect_args [%options]>
 
 122 Returns an array of database connection settings suitable to a call to
 
 123 L<DBI/connect> or L</connect>. The settings to use are retrieved by
 
 124 calling L<SL::DB/_register_db>.
 
 126 This requires that a client has been set up with
 
 127 L<SL::Auth/set_client> or that C<%::myconfig> contains legacy
 
 130 C<%options> are optional database options like C<AutoCommit> (fourth
 
 131 parameter to L<DBI/connect>). They're merged with default settings by
 
 132 filtering them through L/get_options>.
 
 134 =item C<get_datestyle [$dateformat]>
 
 136 Returns the appropriate value for the C<SET DateStyle to...> SQL call
 
 137 depending on C<$dateformat> (e.g. C<SQL, EUROPEAN> if C<$dateformat>
 
 138 equals C<dd.mm.yy>). If C<$dateformat> is not given then it defaults
 
 139 to C<$::myconfig{dateformat}>.
 
 141 =item C<get_initial_sql>
 
 143 Returns SQL commands that should be executed right after a connection
 
 144 has been established. This is usually the call to configure the
 
 145 C<DateStyle> format used by the database.
 
 147 =item C<get_options [%options]>
 
 149 Returns a hash reference of database options (fourth parameter to
 
 150 L<DBI/connect>) merged with certain default options.
 
 160 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>