use Carp;
use Data::Dumper;
-use SL::DBConnect;
use English qw(-no_match_vars);
use Rose::DB;
use Rose::DBx::Cache::Anywhere;
+use Scalar::Util qw(blessed);
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(@_);
}
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',
- 'dd/mm/yy' => 'SQL, 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;
+ 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 => $::locale && $::locale->is_utf8,
+ pg_enable_utf8 => 1,
},
);
- if ($::myconfig{dateformat}) {
- $common_connect_settings{european_dates} = 1 if ($_dateformats{ $::myconfig{dateformat} } || '') =~ m/european/i;
- }
-
if (($type eq 'KIVITENDO_AUTH') && $::auth && $::auth->{DB_config} && $::auth->session_tables_present) {
%specific_connect_settings = (
database => $::auth->{DB_config}->{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 = ();
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;
- }
+ 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;
=item C<with_transaction $code_ref, @args>
Executes C<$code_ref> with parameters C<@args> within a transaction,
-starting one if none is currently active. Example:
+starting one only if none is currently active. Example:
return $self->db->with_transaction(sub {
# do stuff with $self
});
-One big difference to L<Rose::DB/do_transaction> is the return code
-handling. If a transaction is already active then C<with_transcation>
-simply returns the result of calling C<$code_ref> as-is.
+This is a wrapper around L<Rose::DB/do_transaction> that does a few additional
+things, and should always be used in favour of the other:
-Otherwise the return value depends on the result of the underlying
-transaction. If the transaction fails then C<undef> 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.
+=over 4
+
+=item Composition of transactions
+
+When C<with_transaction> 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<with_transaction> can be safely used
+within another C<with_transaction>, whereas L<Rose::DB/do_transaction> can not.
+
+=item Return values
+
+C<with_transaction> adopts the behaviour of C<eval> in that it returns the
+result of the inner block, and C<undef> if an error occured. This way you can
+use the same pattern you would normally use with C<eval> for
+C<with_transaction>:
+
+ 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.
-So if you want to differentiate between "transaction failed" and
-"succeeded" then your C<$code_ref> should never return C<undef>
-itself.
+=item Error handling
+
+The original L<Rose::DB/do_transaction> gobbles up all execptions and expects
+the caller to manually check 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<with_transaction> only gobbles up exception 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 typoes,
+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 rollbacked 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