From 7cfa1f2ab3091d8b44e91aa5ce12c70052250003 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Fri, 3 May 2013 09:48:37 +0200 Subject: [PATCH] SL::DB: Hilfsfunktion 'with_transaction'; Dokumentation --- SL/DB.pm | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/SL/DB.pm b/SL/DB.pm index dd551636b..90e08c582 100644 --- a/SL/DB.pm +++ b/SL/DB.pm @@ -126,4 +126,65 @@ sub _flatten_settings { return %flattened; } +sub with_transaction { + my ($self, $code, @args) = @_; + + return $self->in_transaction ? $code->(@args) : $self->do_transaction(sub { $code->(@args) }); +} + 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> within a transaction, starting one if none is +currently active. This is just a shortcut for the following code: + + # Verbose code in caller (an RDBO instance): + my $worker = sub { + # do stuff with $self + }; + return $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker); + +Now the version using C: + + return $self->db->with_transaction(sub { + # do stuff with $self + }); + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut -- 2.20.1