X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB.pm;h=0adac9ca8eee99028ddebee9058c296cc16286cc;hb=0fe023a4ee7e467bfb5f23a87800b8723c8ccacd;hp=bcbf2e146a1f9ca91cea9e96049fd0a2278173a8;hpb=a97d97a009197f5b5b72897e0b09fb5889e2eb90;p=kivitendo-erp.git diff --git a/SL/DB.pm b/SL/DB.pm index bcbf2e146..0adac9ca8 100644 --- a/SL/DB.pm +++ b/SL/DB.pm @@ -6,18 +6,28 @@ use Carp; use Data::Dumper; use English qw(-no_match_vars); use Rose::DB; +use Rose::DBx::Cache::Anywhere; use base qw(Rose::DB); +__PACKAGE__->db_cache_class('Rose::DBx::Cache::Anywhere'); __PACKAGE__->use_private_registry; -my (%_db_registered, %_initial_sql_executed); +my (%_db_registered); + +sub dbi_connect { + shift; + + # runtime require to break circular include + require SL::DBConnect; + return SL::DBConnect->connect(@_); +} sub create { my $domain = shift || SL::DB->default_domain; my $type = shift || SL::DB->default_type; - my ($domain, $type) = _register_db($domain, $type); + ($domain, $type) = _register_db($domain, $type); my $db = __PACKAGE__->new_or_cached(domain => $domain, type => $type); @@ -28,35 +38,54 @@ sub _register_db { my $domain = shift; my $type = shift; - my %connect_settings; - - if (!%::myconfig) { - $type = 'LXOFFICE_EMPTY'; - %connect_settings = ( driver => 'Pg' ); - - } elsif ($type eq 'LXOFFICE_AUTH') { - %connect_settings = ( driver => $::myconfig{dbdriver} || 'Pg', - database => $::auth->{DB_config}->{db}, - host => $::auth->{DB_config}->{host}, - port => $::auth->{DB_config}->{port} || 5432, - username => $::auth->{DB_config}->{user}, - password => $::auth->{DB_config}->{password}, - 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 { - %connect_settings = ( 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, - }); + $type = 'KIVITENDO_EMPTY'; } - $domain = 'LXOFFICE' if $type =~ m/^LXOFFICE/; - $type .= join($SUBSCRIPT_SEPARATOR, map { $::connect_setings{$_} } sort keys %connect_settings); - my $idx = "${domain}::${type}"; + 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 'dbpasswd' } keys %flattened_settings); + my $idx = "${domain}::${type}"; if (!$_db_registered{$idx}) { $_db_registered{$idx} = 1; @@ -70,4 +99,94 @@ sub _register_db { 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; + if (wantarray) { + my @result; + return $self->do_transaction(sub { @result = $code->(@args) }) ? @result : (); + + } else { + my $result; + return $self->do_transaction(sub { $result = $code->(@args) }) ? $result : undef; + } +} + 1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB - Database access class for all RDB objects + +=head1 FUNCTIONS + +=over 4 + +=item C + +Registers the database information with Rose, creates a cached +connection and executes initial SQL statements. Those can include +setting the time & date format to the user's preferences. + +=item C + +Forwards the call to L which connects to the +database. This indirection allows L to route +the calls through L if this is enabled in the +configuration. + +=item C + +Executes C<$code_ref> with parameters C<@args> within a transaction, +starting one if none is currently active. Example: + + return $self->db->with_transaction(sub { + # do stuff with $self + }); + +One big difference to L is the return code +handling. If a transaction is already active then C +simply returns the result of calling C<$code_ref> as-is. + +Otherwise the return value depends on the result of the underlying +transaction. If the transaction fails then C is returned in +scalar context and an empty list in list context. If the transaction +succeeds then the return value of C<$code_ref> is returned preserving +context. + +So if you want to differentiate between "transaction failed" and +"succeeded" then your C<$code_ref> should never return C +itself. + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut