$c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
my $storage = $c->stash->{'storage'};
+ $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
my $save_func = sub {
return undef unless $book_id;
+ save_buch_autoren( $c, $book_id, $book->{'autoren'} );
+ save_buch_kategorien( $c, $book_id, $book->{'kategorien'} );
+ save_buch_serien( $c, $book_id, $book->{'serien'} );
+
return $book_id;
}
my $K = ( caller(0) )[3] . "(): ";
-
+ my $storage = $c->stash->{'storage'};
+ $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
+ my $dbh = $storage->dbh();
+ $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2;
+
+ my $anzahl = 0;
+ $anzahl = scalar( @$autor_ids ) if $autor_ids and ref($autor_ids) and ref($autor_ids) eq 'ARRAY';
+
+ my $in = '';
+ $in = '(' . join( ", ", map { $dbh->quote($_) } @$autor_ids ) . ")" if $anzahl;
+
+ my $sql;
+ $sql = <<ENDE;
+DELETE FROM `autor2buch`
+ WHERE `buch_id` = ?
+ENDE
+ $sql .= " AND `autor_id` NOT IN " . $in . "\n" if $anzahl;
+
+ my $qparams = [];
+ push @$qparams, $book_id;
+
+ if ( $storage->debug() ) {
+ my $text = $sql;
+ $text =~ s/\s+$//;
+ $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+ warn $text;
+ }
+
+ my $sth = $dbh->prepare($sql);
+ $sth->execute( @$qparams );
+
+ return 1 unless $anzahl;
+
+ my $i = 0;
+
+ $sql = <<ENDE;
+INSERT INTO `autor2buch` (
+ `buch_id`, `autor_id`, `ord_num`
+ ) VALUES (
+ ?, ?, ? )
+ ON DUPLICATE KEY UPDATE
+ `ord_num` = ?
+ENDE
+
+ $sth = $dbh->prepare($sql);
+
+ for my $autor_id ( @$autor_ids ) {
+
+ $qparams = [];
+ push @$qparams, $book_id;
+ push @$qparams, $autor_id;
+ push @$qparams, $i;
+ push @$qparams, $i;
+
+ if ( $storage->debug() ) {
+ my $text = $sql;
+ $text =~ s/\s+$//;
+ $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+ warn $text;
+ }
+
+ $sth->execute( @$qparams );
+
+ $i++;
+ }
+
+}
+
+#-----------------------------------------------------------------------------------
+
+=head2 save_buch_serien( $c, $book_id, @$serien_ids )
+
+=cut
+
+sub save_buch_serien {
+
+ my $c = shift;
+ my $book_id = shift;
+ my $serien_ids = shift;
+
+ my $K = ( caller(0) )[3] . "(): ";
+
+ my $storage = $c->stash->{'storage'};
+ $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
+ my $dbh = $storage->dbh();
+ $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2;
+
+ my $anzahl = 0;
+ $anzahl = scalar( @$serien_ids ) if $serien_ids and ref($serien_ids) and ref($serien_ids) eq 'ARRAY';
+
+ my $in = '';
+ $in = '(' . join( ", ", map { $dbh->quote($_) } @$serien_ids ) . ")" if $anzahl;
+
+ my $sql;
+ $sql = <<ENDE;
+DELETE FROM `buch2serie`
+ WHERE `buch_id` = ?
+ENDE
+ $sql .= " AND `serien_id` NOT IN " . $in . "\n" if $anzahl;
+
+ my $qparams = [];
+ push @$qparams, $book_id;
+
+ if ( $storage->debug() ) {
+ my $text = $sql;
+ $text =~ s/\s+$//;
+ $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+ warn $text;
+ }
+
+ my $sth = $dbh->prepare($sql);
+ $sth->execute( @$qparams );
+
+ return 1 unless $anzahl;
+
+ my $i = 1;
+
+ $sql = <<ENDE;
+INSERT INTO `buch2serie` (
+ `buch_id`, `serien_id`, `ord_num`
+ ) VALUES (
+ ?, ?, ? )
+ ON DUPLICATE KEY UPDATE
+ `ord_num` = IFNULL( `ord_num`, ? )
+ENDE
+
+ $sth = $dbh->prepare($sql);
+
+ for my $serien_id ( @$serien_ids ) {
+
+ $qparams = [];
+ push @$qparams, $book_id;
+ push @$qparams, $serien_id;
+ push @$qparams, $i;
+ push @$qparams, $i;
+
+ if ( $storage->debug() ) {
+ my $text = $sql;
+ $text =~ s/\s+$//;
+ $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+ warn $text;
+ }
+
+ $sth->execute( @$qparams );
+
+ $i++;
+ }
+
+}
+
+#-----------------------------------------------------------------------------------
+
+=head2 save_buch_kategorien( $c, $book_id, @$kategorie_ids )
+
+=cut
+
+sub save_buch_kategorien {
+
+ my $c = shift;
+ my $book_id = shift;
+ my $kategorie_ids = shift;
+
+ my $K = ( caller(0) )[3] . "(): ";
+
+ my $storage = $c->stash->{'storage'};
+ $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
+ my $dbh = $storage->dbh();
+ $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2;
+
+ my $anzahl = 0;
+ $anzahl = scalar( @$kategorie_ids ) if $kategorie_ids and ref($kategorie_ids) and ref($kategorie_ids) eq 'ARRAY';
+
+ my $in = '';
+ $in = '(' . join( ", ", map { $dbh->quote($_) } @$kategorie_ids ) . ")" if $anzahl;
+
+ my $sql;
+ $sql = <<ENDE;
+DELETE FROM `buch2kategorie`
+ WHERE `buch_id` = ?
+ENDE
+ $sql .= " AND `kategorie_id` NOT IN " . $in . "\n" if $anzahl;
+
+ my $qparams = [];
+ push @$qparams, $book_id;
+
+ if ( $storage->debug() ) {
+ my $text = $sql;
+ $text =~ s/\s+$//;
+ $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+ warn $text;
+ }
+
+ my $sth = $dbh->prepare($sql);
+ $sth->execute( @$qparams );
+
+ return 1 unless $anzahl;
+
+ $sql = <<ENDE;
+INSERT INTO `buch2kategorie` (
+ `buch_id`, `kategorie_id`
+ ) VALUES (
+ ?, ? )
+ ON DUPLICATE KEY UPDATE
+ `kategorie_id` = `kategorie_id`
+ENDE
+
+ $sth = $dbh->prepare($sql);
+
+ for my $kategorie_id ( @$kategorie_ids ) {
+
+ $qparams = [];
+ push @$qparams, $book_id;
+ push @$qparams, $kategorie_id;
+
+ if ( $storage->debug() ) {
+ my $text = $sql;
+ $text =~ s/\s+$//;
+ $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+ warn $text;
+ }
+
+ $sth->execute( @$qparams );
+
+ }
}