9 my ($self, @args) = @_;
10 @args = $self->get_connect_args if !@args;
12 return DBI->connect(@args) unless $::lx_office_conf{debug} && $::lx_office_conf{debug}->{dbix_log4perl};
14 require Log::Log4perl;
15 require DBIx::Log4perl;
17 my $filename = $::lxdebug->file;
18 my $config = $::lx_office_conf{debug}->{dbix_log4perl_config};
19 $config =~ s/LXDEBUGFILE/${filename}/g;
21 Log::Log4perl->init(\$config);
22 return DBIx::Log4perl->connect(@args);
25 sub get_connect_args {
26 my ($self, @args) = @_;
27 my ($domain, $type) = SL::DB::_register_db(SL::DB->default_domain, 'KIVITENDO');
28 my $db_cfg = SL::DB->registry->entry(domain => $domain, type => $type) || { };
31 'dbi:' . $db_cfg->{dbi_driver} . ':dbname=' . $db_cfg->{database} . ';host=' . ($db_cfg->{host} || 'localhost') . ';port=' . ($db_cfg->{port} || 5432),
34 $self->get_options(%{ $db_cfg->{connect_options} || {} }, @args),
41 pg_enable_utf8 => $::locale->is_utf8,
57 SL::DBConnect - Connect to database for configured client/user,
58 optionally routing through DBIx::Log4perl
62 # Connect to default database of current user/client, disabling auto
64 my @options_suitable_for_dbi_connect =
65 SL::DBConnect->get_connect_args(AutoCommit => 0);
66 my $dbh = SL::DBConnect->connect(@options_suitable_for_dbi_connect);
68 # Connect to a very specific database:
69 my $dbh = SL::DBConnect->connect('dbi:Pg:dbname=demo', 'user', 'password');
75 =item C<connect [@dbi_args]>
77 Connects to the database. If the configuration parameter
78 C<debug.dbix_log4perl> is set then the call is made through
79 L<DBIx::Log4per/connect>. Otherwise L<DBI/connect> is called directly.
81 In each case C<@dbi_args> is passed through as-is.
83 If C<@dbi_args> are not given they're generated by a call to
86 =item C<get_connect_args [%options]>
88 Returns an array of database connection settings suitable to a call to
89 L<DBI/connect> or L</connect>. The settings to use are retrieved by
90 calling L<SL::DB/_register_db>.
92 This requires that a client has been set up with
93 L<SL::Auth/set_client> or that C<%::myconfig> contains legacy
96 C<%options> are optional database options like C<AutoCommit> (fourth
97 parameter to L<DBI/connect>). They're merged with default settings by
98 filtering them through L/get_options>.
100 =item C<get_options [%options]>
102 Returns a hash reference of database options (fourth parameter to
103 L<DBI/connect>) merged with certain default options.
113 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>