X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB.pm;h=5a22c5da52ecaaff388f697b0c3f30b129b96fc8;hb=82c4717d48bbdb8d30c9671e71ecb0d6d8eac963;hp=67888ef5942f34174b5a7a45dc7986e40f4ed6a7;hpb=fe68756cfbfd869d83067ff15d97f9a1b0ef5379;p=kivitendo-erp.git diff --git a/SL/DB.pm b/SL/DB.pm index 67888ef59..5a22c5da5 100644 --- a/SL/DB.pm +++ b/SL/DB.pm @@ -4,11 +4,14 @@ use strict; use Carp; use Data::Dumper; +use SL::DBConnect; 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); @@ -16,24 +19,14 @@ my (%_db_registered, %_initial_sql_executed); sub dbi_connect { shift; - return DBI->connect(@_) unless $::lx_office_conf{debug} && $::lx_office_conf{debug}->{dbix_log4perl}; - - require Log::Log4perl; - require DBIx::Log4perl; - - my $filename = $LXDebug::file_name; - my $config = $::lx_office_conf{debug}->{dbix_log4perl_config}; - $config =~ s/LXDEBUGFILE/${filename}/g; - - Log::Log4perl->init(\$config); - return DBIx::Log4perl->connect(@_); + 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); @@ -45,9 +38,7 @@ sub create { 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' ); @@ -74,7 +65,8 @@ sub _register_db { } else { my $european_dates = 0; if ($::myconfig{dateformat}) { - $european_dates = 1 if $_dateformats{ $::myconfig{dateformat} } =~ m/european/i; + $european_dates = 1 if $_dateformats{ $::myconfig{dateformat} } + && $_dateformats{ $::myconfig{dateformat} } =~ m/european/i; } %connect_settings = ( driver => $::myconfig{dbdriver} || 'Pg', @@ -91,7 +83,7 @@ sub _register_db { my %flattened_settings = _flatten_settings(%connect_settings); $domain = 'LXOFFICE' if $type =~ m/^LXOFFICE/; - $type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_}) } sort keys %flattened_settings); + $type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort keys %flattened_settings); my $idx = "${domain}::${type}"; if (!$_db_registered{$idx}) { @@ -134,4 +126,79 @@ sub _flatten_settings { 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