- my $idx = "${domain}::${type}";
- return if $_db_registered{$idx};
-
- $_db_registered{$idx} = 1;
-
- __PACKAGE__->register_db(domain => $domain,
- type => $type,
- driver => $::myconfig{dbdriver} || 'Pg',
- database => $::myconfig{dbname},
- host => $::myconfig{dbhost},
- port => $::myconfig{dbport} || 5432,
- username => $::myconfig{dbuser},
- password => $::myconfig{dbpasswd},
- connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8,
- },
- );
+ require SL::DBConnect;
+ my %specific_connect_settings;
+ my %common_connect_settings = (
+ driver => 'Pg',
+ european_dates => ((SL::DBConnect->get_datestyle || '') =~ m/european/i) ? 1 : 0,
+ connect_options => {
+ pg_enable_utf8 => 1,
+ },
+ );
+
+ if (($type eq 'KIVITENDO_AUTH') && $::auth && $::auth->{DB_config} && $::auth->session_tables_present) {
+ %specific_connect_settings = (
+ database => $::auth->{DB_config}->{db},
+ host => $::auth->{DB_config}->{host} || 'localhost',
+ port => $::auth->{DB_config}->{port} || 5432,
+ username => $::auth->{DB_config}->{user},
+ password => $::auth->{DB_config}->{password},
+ );
+
+ } elsif ($::auth && $::auth->client) {
+ my $client = $::auth->client;
+ %specific_connect_settings = (
+ database => $client->{dbname},
+ host => $client->{dbhost} || 'localhost',
+ port => $client->{dbport} || 5432,
+ username => $client->{dbuser},
+ password => $client->{dbpasswd},
+ );
+
+ } elsif (%::myconfig && $::myconfig{dbname}) {
+ %specific_connect_settings = (
+ database => $::myconfig{dbname},
+ host => $::myconfig{dbhost} || 'localhost',
+ port => $::myconfig{dbport} || 5432,
+ username => $::myconfig{dbuser},
+ password => $::myconfig{dbpasswd},
+ );
+
+ } else {
+ $type = 'KIVITENDO_EMPTY';
+ }
+
+ my %connect_settings = (%common_connect_settings, %specific_connect_settings);
+ my %flattened_settings = _flatten_settings(%connect_settings);
+
+ $domain = 'KIVITENDO' if $type =~ m/^KIVITENDO/;
+ $type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort grep { $_ ne 'password' } keys %flattened_settings);
+ my $idx = "${domain}::${type}";
+
+ if (!$_db_registered{$idx}) {
+ $_db_registered{$idx} = 1;
+
+ __PACKAGE__->register_db(domain => $domain,
+ type => $type,
+ %connect_settings,
+ );
+ }
+
+ return ($domain, $type);
+}
+
+sub _flatten_settings {
+ my %settings = @_;
+ my %flattened = ();
+
+ while (my ($key, $value) = each %settings) {
+ if ('HASH' eq ref $value) {
+ %flattened = ( %flattened, _flatten_settings(%{ $value }) );
+ } else {
+ $flattened{$key} = $value;
+ }
+ }
+
+ return %flattened;
+}
+
+sub with_transaction {
+ my ($self, $code, @args) = @_;
+
+ return $code->(@args) if $self->in_transaction;
+
+ my (@result, $result);
+ my $rv = 1;
+
+ local $@;
+ my $return_array = wantarray;
+ eval {
+ $return_array
+ ? $self->do_transaction(sub { @result = $code->(@args) })
+ : $self->do_transaction(sub { $result = $code->(@args) });
+ } or do {
+ my $error = $self->error;
+ if (blessed $error) {
+ if ($error->isa('SL::X::DBError')) {
+ # gobble the exception
+ } else {
+ $error->rethrow;
+ }
+ } else {
+ die $self->error;
+ }
+ };
+
+ return $return_array ? @result : $result;