X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB.pm;h=a59fd6fdf503059f05878178fa3524795750f792;hb=94f5cb152afa9210162295b6727801c296af0631;hp=491a03349427ef6514a78ceefc61539c0b927820;hpb=45aeeb1343ff6dd6803fa24f04a38557e514910e;p=kivitendo-erp.git diff --git a/SL/DB.pm b/SL/DB.pm index 491a03349..a59fd6fdf 100644 --- a/SL/DB.pm +++ b/SL/DB.pm @@ -6,16 +6,21 @@ use Carp; use Data::Dumper; use English qw(-no_match_vars); use Rose::DB; +use SL::DB::Helper::Cache; +use Scalar::Util qw(blessed); use base qw(Rose::DB); +__PACKAGE__->db_cache_class('SL::DB::Helper::Cache'); __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(@_); } @@ -27,62 +32,69 @@ sub create { my $db = __PACKAGE__->new_or_cached(domain => $domain, type => $type); - _execute_initial_sql($db); - return $db; } -my %_dateformats = ( 'yy-mm-dd' => 'ISO', - 'yyyy-mm-dd' => 'ISO', - 'mm/dd/yy' => 'SQL, US', - 'mm-dd-yy' => 'POSTGRES, US', - 'dd/mm/yy' => 'SQL, EUROPEAN', - 'dd-mm-yy' => 'POSTGRES, EUROPEAN', - 'dd.mm.yy' => 'GERMAN' - ); +sub client { + create(undef, 'KIVITENDO'); +} + +sub auth { + create(undef, 'KIVITENDO_AUTH'); +} sub _register_db { my $domain = shift; my $type = shift; - my %connect_settings; - my $initial_sql; - - 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} || 'localhost', - 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, - }); - } else { - my $european_dates = 0; - if ($::myconfig{dateformat}) { - $european_dates = 1 if $_dateformats{ $::myconfig{dateformat} } =~ m/european/i; - } + 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}, + ); - %connect_settings = ( driver => $::myconfig{dbdriver} || 'Pg', - database => $::myconfig{dbname}, - host => $::myconfig{dbhost} || 'localhost', - port => $::myconfig{dbport} || 5432, - username => $::myconfig{dbuser}, - password => $::myconfig{dbpasswd}, - connect_options => { pg_enable_utf8 => $::locale && $::locale->is_utf8, - }, - european_dates => $european_dates); + } 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 = 'LXOFFICE' if $type =~ m/^LXOFFICE/; - $type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort keys %flattened_settings); - my $idx = "${domain}::${type}"; + $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; @@ -96,19 +108,6 @@ sub _register_db { return ($domain, $type); } -sub _execute_initial_sql { - my ($db) = @_; - - return if $_initial_sql_executed{$db} || !%::myconfig || !$::myconfig{dateformat}; - - $_initial_sql_executed{$db} = 1; - - # Don't rely on dboptions being set properly. Chose them from - # dateformat instead. - my $pg_dateformat = $_dateformats{ $::myconfig{dateformat} }; - $db->dbh->do("set DateStyle to '${pg_dateformat}'") if $pg_dateformat; -} - sub _flatten_settings { my %settings = @_; my %flattened = (); @@ -124,4 +123,137 @@ sub _flatten_settings { 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; +} + 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 only if none is currently active. Example: + + return $self->db->with_transaction(sub { + # do stuff with $self + }); + +This is a wrapper around L that does a few additional +things, and should always be used in favour of the other: + +=over 4 + +=item Composition of transactions + +When C is called without a running transaction, a new one is +created. If it is called within a running transaction, it performs no +additional handling. This means that C can be safely used +within another C, whereas L can not. + +=item Return values + +C adopts the behaviour of C in that it returns the +result of the inner block, and C if an error occurred. This way you can +use the same pattern you would normally use with C for +C: + + SL::DB->client->with_transaction(sub { + # do stuff + # and return nominal true value + 1; + }) or do { + # transaction error handling + my $error = SL::DB->client->error; + } + +or you can use it to safely calulate things. + +=item Error handling + +The original L gobbles up all exceptions and expects +the caller to manually check the return value and error, and then to process +all exceptions as strings. This is very fragile and generally a step backwards +from proper exception handling. + +C only gobbles up exceptions that are used to signal an +error in the transaction, and returns undef on those. All other exceptions +bubble out of the transaction like normal, so that it is transparent to typos, +runtime exceptions and other generally wanted things. + +If you just use the snippet above, your code will catch everything related to +the transaction aborting, but will not catch other errors that might have been +thrown. The transaction will be rolled back in both cases. + +If you want to play nice in case your transaction is embedded in another +transaction, just rethrow the error: + + $db->with_transaction(sub { + # code deep in the engine + 1; + }) or die $db->error; + +=back + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut