]> Frank Brehm's Git Trees - books.git/commitdiff
Speichern Autor mit Table-Lock
authorFrank Brehm <frank@brehm-online.com>
Mon, 24 Nov 2008 22:50:05 +0000 (22:50 +0000)
committerFrank Brehm <frank@brehm-online.com>
Mon, 24 Nov 2008 22:50:05 +0000 (22:50 +0000)
lib/FrBr/Books/Util/Author.pm

index 91a470133b54443125d75b1501c41648e401eb22..dd7145b2861d18f7b28059d95c54a0091f407370 100644 (file)
@@ -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 = <<ENDE;
+
+        my $sql = '';
+        my @P = ();
+
+        if ( $insert ) {
+            if ( $autor_id ) {
+                $sql = <<ENDE;
 INSERT INTO `autoren` (
     `id`, `titel`, `vorname`, `mittelname`, `nachname`, `name_suffix`, `autor_descr` )
   VALUES (
-    ?, ?, ?, ?, ?, ?, ? )
-  ON DUPLICATE KEY UPDATE
-    `id` = LAST_INSERT_ID(`id`),
-    `titel` = ?,
-    `vorname` = ?,
-    `mittelname` = ?,
-    `nachname` = ?,
-    `name_suffix` = ?,
-    `autor_descr` = ?
+    LAST_INSERT_ID(?), ?, ?, ?, ?, ?, ? )
 ENDE
-
-        my @P = ();
-        push @P, $autor_id;
-        push @P, $titel;
-        push @P, $vorname;
-        push @P, $mittelname;
-        push @P, $nachname;
-        push @P, $suffix;
-        push @P, $desc;
-        push @P, $titel;
-        push @P, $vorname;
-        push @P, $mittelname;
-        push @P, $nachname;
-        push @P, $suffix;
-        push @P, $desc;
+                @P = ( $autor_id, $titel, $vorname, $mittelname, $nachname, $suffix, $desc );
+            }
+            else {
+                $sql = <<ENDE;
+INSERT INTO `autoren` (
+    `titel`, `vorname`, `mittelname`, `nachname`, `name_suffix`, `autor_descr` )
+  VALUES (
+    ?, ?, ?, ?, ?, ? )
+ENDE
+                @P = ( $titel, $vorname, $mittelname, $nachname, $suffix, $desc );
+            }
+        }
+        else {
+            $sql = <<ENDE;
+UPDATE `autoren`
+   SET `id` = LAST_INSERT_ID(`id`),
+       `titel` = ?,
+       `vorname` = ?,
+       `mittelname` = ?,
+       `nachname` = ?,
+       `name_suffix` = ?,
+       `autor_descr` = ?
+ WHERE `id` = ?
+ENDE
+            @P = ( $titel, $vorname, $mittelname, $nachname, $suffix, $desc, $autor_id );
+        }
 
         if ( $storage->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;
 
 }