Revision beb61b2e
Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt
SL/DBConnect.pm | ||
---|---|---|
3 | 3 |
use strict; |
4 | 4 |
|
5 | 5 |
use DBI; |
6 |
use SL::DB; |
|
6 | 7 |
|
7 | 8 |
sub connect { |
8 |
shift; |
|
9 |
my ($self, @args) = @_; |
|
10 |
@args = $self->get_connect_args if !@args; |
|
9 | 11 |
|
10 |
# print STDERR "Starting full caller dump:\n"; |
|
11 |
# my $level = 0; |
|
12 |
# while (my ($dummy, $filename, $line, $subroutine) = caller $level) { |
|
13 |
# print STDERR " ${subroutine} from ${filename}:${line}\n"; |
|
14 |
# $level++; |
|
15 |
# } |
|
16 |
|
|
17 |
return DBI->connect(@_) unless $::lx_office_conf{debug} && $::lx_office_conf{debug}->{dbix_log4perl}; |
|
12 |
return DBI->connect(@args) unless $::lx_office_conf{debug} && $::lx_office_conf{debug}->{dbix_log4perl}; |
|
18 | 13 |
|
19 | 14 |
require Log::Log4perl; |
20 | 15 |
require DBIx::Log4perl; |
... | ... | |
24 | 19 |
$config =~ s/LXDEBUGFILE/${filename}/g; |
25 | 20 |
|
26 | 21 |
Log::Log4perl->init(\$config); |
27 |
return DBIx::Log4perl->connect(@_); |
|
22 |
return DBIx::Log4perl->connect(@args); |
|
23 |
} |
|
24 |
|
|
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) || { }; |
|
29 |
|
|
30 |
return ( |
|
31 |
'dbi:' . $db_cfg->{dbi_driver} . ':dbname=' . $db_cfg->{database} . ';host=' . ($db_cfg->{host} || 'localhost') . ';port=' . ($db_cfg->{port} || 5432), |
|
32 |
$db_cfg->{username}, |
|
33 |
$db_cfg->{password}, |
|
34 |
$self->get_options(%{ $db_cfg->{connect_options} || {} }, @args), |
|
35 |
); |
|
28 | 36 |
} |
29 | 37 |
|
30 | 38 |
sub get_options { |
... | ... | |
38 | 46 |
} |
39 | 47 |
|
40 | 48 |
1; |
49 |
__END__ |
|
50 |
|
|
51 |
=pod |
|
52 |
|
|
53 |
=encoding utf8 |
|
54 |
|
|
55 |
=head1 NAME |
|
56 |
|
|
57 |
SL::DBConnect - Connect to database for configured client/user, |
|
58 |
optionally routing through DBIx::Log4perl |
|
59 |
|
|
60 |
=head1 SYNOPSIS |
|
61 |
|
|
62 |
# Connect to default database of current user/client, disabling auto |
|
63 |
# commit mode: |
|
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); |
|
67 |
|
|
68 |
# Connect to a very specific database: |
|
69 |
my $dbh = SL::DBConnect->connect('dbi:Pg:dbname=demo', 'user', 'password'); |
|
70 |
|
|
71 |
=head1 FUNCTIONS |
|
72 |
|
|
73 |
=over 4 |
|
74 |
|
|
75 |
=item C<connect [@dbi_args]> |
|
76 |
|
|
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. |
|
80 |
|
|
81 |
In each case C<@dbi_args> is passed through as-is. |
|
82 |
|
|
83 |
If C<@dbi_args> are not given they're generated by a call to |
|
84 |
L</get_connect_args>. |
|
85 |
|
|
86 |
=item C<get_connect_args [%options]> |
|
87 |
|
|
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>. |
|
91 |
|
|
92 |
This requires that a client has been set up with |
|
93 |
L<SL::Auth/set_client> or that C<%::myconfig> contains legacy |
|
94 |
connection settings. |
|
95 |
|
|
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>. |
|
99 |
|
|
100 |
=item C<get_options [%options]> |
|
101 |
|
|
102 |
Returns a hash reference of database options (fourth parameter to |
|
103 |
L<DBI/connect>) merged with certain default options. |
|
104 |
|
|
105 |
=back |
|
106 |
|
|
107 |
=head1 BUGS |
|
108 |
|
|
109 |
Nothing here yet. |
|
110 |
|
|
111 |
=head1 AUTHOR |
|
112 |
|
|
113 |
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt> |
|
114 |
|
|
115 |
=cut |
Auch abrufbar als: Unified diff
SL::DBConnect: zu verwendende Datenbankinfos via SL::DB->_register_db erhalten