8 use English qw(-no_match_vars);
10 use Rose::DBx::Cache::Anywhere;
12 use base qw(Rose::DB);
14 __PACKAGE__->db_cache_class('Rose::DBx::Cache::Anywhere');
15 __PACKAGE__->use_private_registry;
22 return SL::DBConnect->connect(@_);
26 my $domain = shift || SL::DB->default_domain;
27 my $type = shift || SL::DB->default_type;
29 ($domain, $type) = _register_db($domain, $type);
31 my $db = __PACKAGE__->new_or_cached(domain => $domain, type => $type);
40 my %specific_connect_settings;
41 my %common_connect_settings = (
43 european_dates => ((SL::DBConnect->get_datestyle || '') =~ m/european/i) ? 1 : 0,
45 pg_enable_utf8 => $::locale && $::locale->is_utf8,
49 if (($type eq 'KIVITENDO_AUTH') && $::auth && $::auth->{DB_config} && $::auth->session_tables_present) {
50 %specific_connect_settings = (
51 database => $::auth->{DB_config}->{db},
52 host => $::auth->{DB_config}->{host} || 'localhost',
53 port => $::auth->{DB_config}->{port} || 5432,
54 username => $::auth->{DB_config}->{user},
55 password => $::auth->{DB_config}->{password},
58 } elsif ($::auth && $::auth->client) {
59 my $client = $::auth->client;
60 %specific_connect_settings = (
61 database => $client->{dbname},
62 host => $client->{dbhost} || 'localhost',
63 port => $client->{dbport} || 5432,
64 username => $client->{dbuser},
65 password => $client->{dbpasswd},
68 } elsif (%::myconfig && $::myconfig{dbname}) {
69 %specific_connect_settings = (
70 database => $::myconfig{dbname},
71 host => $::myconfig{dbhost} || 'localhost',
72 port => $::myconfig{dbport} || 5432,
73 username => $::myconfig{dbuser},
74 password => $::myconfig{dbpasswd},
78 $type = 'KIVITENDO_EMPTY';
81 my %connect_settings = (%common_connect_settings, %specific_connect_settings);
82 my %flattened_settings = _flatten_settings(%connect_settings);
84 $domain = 'KIVITENDO' if $type =~ m/^KIVITENDO/;
85 $type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort grep { $_ ne 'dbpasswd' } keys %flattened_settings);
86 my $idx = "${domain}::${type}";
88 if (!$_db_registered{$idx}) {
89 $_db_registered{$idx} = 1;
91 __PACKAGE__->register_db(domain => $domain,
97 return ($domain, $type);
100 sub _flatten_settings {
104 while (my ($key, $value) = each %settings) {
105 if ('HASH' eq ref $value) {
106 %flattened = ( %flattened, _flatten_settings(%{ $value }) );
108 $flattened{$key} = $value;
115 sub with_transaction {
116 my ($self, $code, @args) = @_;
118 return $code->(@args) if $self->in_transaction;
121 return $self->do_transaction(sub { @result = $code->(@args) }) ? @result : ();
125 return $self->do_transaction(sub { $result = $code->(@args) }) ? $result : undef;
138 SL::DB - Database access class for all RDB objects
144 =item C<create $domain, $type>
146 Registers the database information with Rose, creates a cached
147 connection and executes initial SQL statements. Those can include
148 setting the time & date format to the user's preferences.
150 =item C<dbi_connect $dsn, $login, $password, $options>
152 Forwards the call to L<SL::DBConnect/connect> which connects to the
153 database. This indirection allows L<SL::DBConnect/connect> to route
154 the calls through L<DBIx::Log4Perl> if this is enabled in the
157 =item C<with_transaction $code_ref, @args>
159 Executes C<$code_ref> with parameters C<@args> within a transaction,
160 starting one if none is currently active. Example:
162 return $self->db->with_transaction(sub {
163 # do stuff with $self
166 One big difference to L<Rose::DB/do_transaction> is the return code
167 handling. If a transaction is already active then C<with_transcation>
168 simply returns the result of calling C<$code_ref> as-is.
170 Otherwise the return value depends on the result of the underlying
171 transaction. If the transaction fails then C<undef> is returned in
172 scalar context and an empty list in list context. If the transaction
173 succeeds then the return value of C<$code_ref> is returned preserving
176 So if you want to differentiate between "transaction failed" and
177 "succeeded" then your C<$code_ref> should never return C<undef>
188 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>