7 use English qw(-no_match_vars);
9 use Rose::DBx::Cache::Anywhere;
11 use base qw(Rose::DB);
13 __PACKAGE__->db_cache_class('Rose::DBx::Cache::Anywhere');
14 __PACKAGE__->use_private_registry;
21 # runtime require to break circular include
22 require SL::DBConnect;
23 return SL::DBConnect->connect(@_);
27 my $domain = shift || SL::DB->default_domain;
28 my $type = shift || SL::DB->default_type;
30 ($domain, $type) = _register_db($domain, $type);
32 my $db = __PACKAGE__->new_or_cached(domain => $domain, type => $type);
41 require SL::DBConnect;
42 my %specific_connect_settings;
43 my %common_connect_settings = (
45 european_dates => ((SL::DBConnect->get_datestyle || '') =~ m/european/i) ? 1 : 0,
51 if (($type eq 'KIVITENDO_AUTH') && $::auth && $::auth->{DB_config} && $::auth->session_tables_present) {
52 %specific_connect_settings = (
53 database => $::auth->{DB_config}->{db},
54 host => $::auth->{DB_config}->{host} || 'localhost',
55 port => $::auth->{DB_config}->{port} || 5432,
56 username => $::auth->{DB_config}->{user},
57 password => $::auth->{DB_config}->{password},
60 } elsif ($::auth && $::auth->client) {
61 my $client = $::auth->client;
62 %specific_connect_settings = (
63 database => $client->{dbname},
64 host => $client->{dbhost} || 'localhost',
65 port => $client->{dbport} || 5432,
66 username => $client->{dbuser},
67 password => $client->{dbpasswd},
70 } elsif (%::myconfig && $::myconfig{dbname}) {
71 %specific_connect_settings = (
72 database => $::myconfig{dbname},
73 host => $::myconfig{dbhost} || 'localhost',
74 port => $::myconfig{dbport} || 5432,
75 username => $::myconfig{dbuser},
76 password => $::myconfig{dbpasswd},
80 $type = 'KIVITENDO_EMPTY';
83 my %connect_settings = (%common_connect_settings, %specific_connect_settings);
84 my %flattened_settings = _flatten_settings(%connect_settings);
86 $domain = 'KIVITENDO' if $type =~ m/^KIVITENDO/;
87 $type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort grep { $_ ne 'dbpasswd' } keys %flattened_settings);
88 my $idx = "${domain}::${type}";
90 if (!$_db_registered{$idx}) {
91 $_db_registered{$idx} = 1;
93 __PACKAGE__->register_db(domain => $domain,
99 return ($domain, $type);
102 sub _flatten_settings {
106 while (my ($key, $value) = each %settings) {
107 if ('HASH' eq ref $value) {
108 %flattened = ( %flattened, _flatten_settings(%{ $value }) );
110 $flattened{$key} = $value;
117 sub with_transaction {
118 my ($self, $code, @args) = @_;
120 return $code->(@args) if $self->in_transaction;
123 return $self->do_transaction(sub { @result = $code->(@args) }) ? @result : ();
127 return $self->do_transaction(sub { $result = $code->(@args) }) ? $result : undef;
140 SL::DB - Database access class for all RDB objects
146 =item C<create $domain, $type>
148 Registers the database information with Rose, creates a cached
149 connection and executes initial SQL statements. Those can include
150 setting the time & date format to the user's preferences.
152 =item C<dbi_connect $dsn, $login, $password, $options>
154 Forwards the call to L<SL::DBConnect/connect> which connects to the
155 database. This indirection allows L<SL::DBConnect/connect> to route
156 the calls through L<DBIx::Log4Perl> if this is enabled in the
159 =item C<with_transaction $code_ref, @args>
161 Executes C<$code_ref> with parameters C<@args> within a transaction,
162 starting one if none is currently active. Example:
164 return $self->db->with_transaction(sub {
165 # do stuff with $self
168 One big difference to L<Rose::DB/do_transaction> is the return code
169 handling. If a transaction is already active then C<with_transcation>
170 simply returns the result of calling C<$code_ref> as-is.
172 Otherwise the return value depends on the result of the underlying
173 transaction. If the transaction fails then C<undef> is returned in
174 scalar context and an empty list in list context. If the transaction
175 succeeds then the return value of C<$code_ref> is returned preserving
178 So if you want to differentiate between "transaction failed" and
179 "succeeded" then your C<$code_ref> should never return C<undef>
190 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>