From 23749ee405945cdc3b0dd6a3c630f5db4465a85b Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 24 Nov 2008 22:50:05 +0000 Subject: [PATCH] Speichern Autor mit Table-Lock --- lib/FrBr/Books/Util/Author.pm | 99 +++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 28 deletions(-) diff --git a/lib/FrBr/Books/Util/Author.pm b/lib/FrBr/Books/Util/Author.pm index 91a4701..dd7145b 100644 --- a/lib/FrBr/Books/Util/Author.pm +++ b/lib/FrBr/Books/Util/Author.pm @@ -7,6 +7,7 @@ use strict; use warnings; use FrBr::Common; +use FrBr::Books::Util::Locks; # Export-Deklarationen @@ -153,37 +154,47 @@ sub save_autor { my $storage = $c->stash->{'storage'}; my $save_func = sub { - my ( $storage, $dbh, $autor_id, $titel, $vorname, $mittelname, $nachname, $suffix, $desc ) = @_; + + my ( $storage, $dbh, $insert, $autor_id, $titel, $vorname, $mittelname, $nachname, $suffix, $desc ) = @_; $desc = '' unless defined $desc; - my $sql = <debug() ) { my $text = $sql; @@ -194,9 +205,12 @@ ENDE my $sth = $dbh->prepare($sql); $sth->execute( @P ); + }; my @Params = (); + my $search_params = {}; + push @Params, $autor->{'id'}; my $tmp = $autor->{'titel'}; @@ -231,9 +245,38 @@ ENDE push @Params, $autor->{'desc'}; - $storage->dbh_do( $save_func, @Params ); + lock_tables( $c, 'write' => [ 'autoren', 'autoren', 'as', 'me' ] ); + + my $id = undef; + my $do_insert = undef; + if ( $autor->{'id'} ) { + $search_params = { '`id`' => $autor->{'id'}, }; + } + else { + $search_params = { + 'nachname' => $autor->{'nachname'}, + 'vorname' => $autor->{'vorname'}, + 'mittelname' => $autor->{'mittelname'}, + 'name_suffix' => $autor->{'name_suffix'}, + }; + } + for my $autor_rs ( $c->model('Schema::Autoren')->search( $search_params )->all() ) { + $id = $autor_rs->id(); + } + + if ( $id ) { + $autor->{'id'} = $id; + $Params[0] = $id; + } + else { + $do_insert = 1; + } + + $storage->dbh_do( $save_func, $do_insert, @Params ); - return $storage->last_insert_id(); + my $author_id = $storage->last_insert_id(); + unlock_tables($c); + return $author_id; } -- 2.39.5