]> Frank Brehm's Git Trees - cookbook.git/commitdiff
Inhalt der Tabelle authors importiert
authorFrank Brehm <frank@brehm-online.com>
Wed, 8 Aug 2007 15:47:32 +0000 (15:47 +0000)
committerFrank Brehm <frank@brehm-online.com>
Wed, 8 Aug 2007 15:47:32 +0000 (15:47 +0000)
git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@11 191103c4-1d37-0410-b3e5-d8c2315c0aac

sbin/initial_import.pl

index de3a9df2767b7fc411f833a5e063db54685c0552..55ba817a1205d970f24d948ad4ff38d6b5050e39 100755 (executable)
@@ -82,7 +82,7 @@ print "Lokale Konfiguration: " . Dumper($local_conf) if $debug;
 # 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;
 
@@ -134,7 +134,7 @@ print "\n";
 
 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;
@@ -327,7 +327,7 @@ sub drop_target_tables {
 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); };
@@ -337,12 +337,12 @@ sub optimize_target_tables {
             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
 
 #-------------------------------------------------------------
 
@@ -355,7 +355,7 @@ Erstellt alle notwendigen Ziel-Tabellen ...
 sub create_target_tables {
 
     my %create_method = (
-        'users' => \&create_user_table,
+        'users'   => \&create_user_table,
         'authors' => \&create_author_table,
     );
 
@@ -419,6 +419,78 @@ END_SQL
     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;
+
 }
 
 #-------------------------------------------------------------