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'
);
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<create $domain, $type>
+
+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<dbi_connect $dsn, $login, $password, $options>
+
+Forwards the call to L<SL::DBConnect/connect> which connects to the
+database. This indirection allows L<SL::DBConnect/connect> to route
+the calls through L<DBIx::Log4Perl> if this is enabled in the
+configuration.
+
+=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:
+
+ 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.
+
+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.
+
+So if you want to differentiate between "transaction failed" and
+"succeeded" then your C<$code_ref> should never return C<undef>
+itself.
+
+=back
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
+
+=cut