# DSN's und Nutzerangaben ermitteln:
my ( $target_dsn, $target_user, $target_passwd, $source_dsn, $source_user, $source_passwd );
-my ( $target_dbh, $target_sth, $source_dbh, $source_sth );
+my ( $target_dbh, $source_dbh );
my $port;
exit 7 unless drop_target_tables();
exit 8 unless create_target_tables();
-
+exit 77 unless import_data();
exit 99 unless optimize_target_tables();
exit 0;
sub optimize_target_tables {
print green_star() . " Optimiere alle Zieltabellen ...\n";
- for my $table ( @target_tables ) {
+ for my $table (@target_tables) {
my $sql = "OPTIMIZE TABLE `" . $table . "`";
print " - " . $table . " ";
eval { die "Nicht optimiert.\n" unless $target_dbh->do($sql); };
return undef;
}
print ok() . "\n";
- } ## end for my $table ( reverse(@target_tables) )
+ } ## end for my $table (@target_tables)
print "\n";
return 1;
-}
+} ## end sub optimize_target_tables
#-------------------------------------------------------------
sub create_target_tables {
my %create_method = (
- 'users' => \&create_user_table,
+ 'users' => \&create_user_table,
'authors' => \&create_author_table,
);
return undef unless $target_dbh->do($sql);
return 1;
+} ## end sub create_author_table
+
+#-------------------------------------------------------------
+
+=head2 import_data( )
+
+=cut
+
+sub import_data {
+
+ print green_star() . " Importiere Tabellen ...\n";
+
+ print " - authors ... ";
+ eval { die "Tabelle 'authors' wurde nicht importiert.\n" unless import_author_table(); };
+ if ($@) {
+ print not_ok() . "\n";
+ warn $@ . "\n";
+ return undef;
+ }
+ print ok() . "\n";
+
+ print "\n";
+ return 1;
+
+}
+
+#-------------------------------------------------------------
+
+=head2 import_author_table( )
+
+=cut
+
+sub import_author_table {
+
+ my $sql = 'SELECT `id`, `name` FROM `authors` order by `name`';
+ my ( $source_sth, $target_sth, $author, $row, $count, $qparams );
+
+ unless ( $source_sth = $source_dbh->prepare($sql) ) {
+ return undef;
+ }
+
+ return undef unless $source_sth->execute();
+
+ while ( $author = $source_sth->fetchrow_hashref() ) {
+ $sql = 'SELECT count(*) AS `count` FROM `authors` WHERE `author_name` = ?';
+ $target_sth = $target_dbh->prepare($sql);
+ unless ( $target_sth ) {
+ $source_sth->finish();
+ return undef;
+ }
+ unless ( $target_sth->execute( $author->{'name'} ) ) {
+ $source_sth->finish();
+ return undef;
+ }
+ $row = $target_sth->fetchrow_hashref();
+ $count = $row->{'count'};
+ $target_sth->finish();
+ $qparams = [ $author->{'id'}, $author->{'name'} ];
+ if ( $count ) {
+ $sql = 'UPDATE `authors` SET `old_author_id` = ? WHERE `author_name` = ?';
+ }
+ else {
+ $sql = 'INSERT INTO `authors` ( `old_author_id`, `author_name`, `date_created` ) VALUES ( ?, ?, now() )';
+ }
+ unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+ $source_sth->finish();
+ return undef;
+ }
+ }
+
+ return 1;
+
}
#-------------------------------------------------------------