]> Frank Brehm's Git Trees - cookbook.git/commitdiff
Weitergemacht
authorFrank Brehm <frank@brehm-online.com>
Thu, 9 Aug 2007 13:14:01 +0000 (13:14 +0000)
committerFrank Brehm <frank@brehm-online.com>
Thu, 9 Aug 2007 13:14:01 +0000 (13:14 +0000)
git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@13 191103c4-1d37-0410-b3e5-d8c2315c0aac

sbin/initial_import.pl

index 17740d18f0320d37614cb55a72a042604a9ec42c..dbcaaeb85cd1e81d1cb45420538c1503fdc3b557 100755 (executable)
@@ -33,7 +33,9 @@ my @target_tables = qw(
     session
     session_log
     authors
+    yield_types
     recipes
+    recipe_authors
 );
 
 my $admin_data = {
@@ -88,6 +90,9 @@ print "Lokale Konfiguration: " . Dumper($local_conf) if $debug;
 my ( $target_dsn, $target_user, $target_passwd, $source_dsn, $source_user, $source_passwd );
 my ( $target_dbh, $source_dbh );
 
+my %map_recipe_id;
+my %map_author_id;
+
 my $port;
 
 $target_dsn = 'dbi:mysql:host=%s;database=%s';
@@ -359,11 +364,13 @@ Erstellt alle notwendigen Ziel-Tabellen ...
 sub create_target_tables {
 
     my %create_method = (
-        'users'       => \&create_user_table,
-        'authors'     => \&create_author_table,
-        'session'     => \&create_session_table,
-        'session_log' => \&create_session_log_table,
-        'recipes'     => \&create_recipes_table,
+        'users'          => \&create_user_table,
+        'authors'        => \&create_author_table,
+        'session'        => \&create_session_table,
+        'session_log'    => \&create_session_log_table,
+        'recipes'        => \&create_recipes_table,
+        'yield_types'    => \&create_yield_types_table,
+        'recipe_authors' => \&create_recipe_authors_table,
     );
 
     print green_star() . " Erstelle Ziel-Tabellen ...\n";
@@ -447,6 +454,15 @@ sub import_data {
     }
     print ok() . "\n";
 
+    print "   - yield_types ... ";
+    eval { die "Tabelle 'yield_types' wurde nicht importiert.\n" unless import_yield_types_table(); };
+    if ($@) {
+        print not_ok() . "\n";
+        warn $@ . "\n";
+        return undef;
+    }
+    print ok() . "\n";
+
     print "   - recipes ... ";
     eval { die "Tabelle 'recipes' wurde nicht importiert.\n" unless import_receipes_table(); };
     if ($@) {
@@ -456,6 +472,14 @@ sub import_data {
     }
     print ok() . "\n";
 
+    print "   - recipe_authors ... ";
+    eval { die "Tabelle 'recipes' wurde nicht importiert.\n" unless import_recipe_authors_table(); };
+    if ($@) {
+        print not_ok() . "\n";
+        warn $@ . "\n";
+        return undef;
+    }
+    print ok() . "\n";
     print "\n";
     return 1;
 
@@ -470,7 +494,7 @@ sub import_data {
 sub import_author_table {
 
     my $sql = 'SELECT `id`, `name` FROM `authors` order by `name`';
-    my ( $source_sth, $target_sth, $author, $row, $count, $qparams );
+    my ( $source_sth, $target_sth, $author, $row, $count, $qparams, $do_insert );
 
     unless ( $source_sth = $source_dbh->prepare($sql) ) {
         return undef;
@@ -493,16 +517,24 @@ sub import_author_table {
         $count = $row->{'count'};
         $target_sth->finish();
         $qparams = [ $author->{'id'}, $author->{'name'} ];
+        $do_insert = 0;
         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() )';
+            $do_insert = 1;
         }
         unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
             $source_sth->finish();
             return undef;
         }
+        if ( $do_insert ) {
+            $map_author_id{$author->{'id'}} = $target_dbh->{'mysql_insertid'};
+        }
+        else {
+            $map_author_id{$author->{'id'}} = 1;
+        }
     }
 
     return 1;
@@ -615,15 +647,8 @@ END_SQL
 sub import_receipes_table {
 
     my $sql = <<END_SQL;
-SELECT `id`,
-       `title`,
-       `yield_amount`,
-       `yield_amount_offset`,
-       `yield_type_id`,
-       `instructions`,
-       `prep_time`,
-       `ctime`,
-       `mtime`
+SELECT `id`, `title`, `yield_amount`, `yield_amount_offset`, `yield_type_id`, `instructions`,
+       `prep_time`, `ctime`, `mtime`
   FROM `recipes`
  ORDER BY `ctime`
 END_SQL
@@ -679,13 +704,100 @@ END_SQL
             $source_sth->finish();
             return undef;
         }
+
+        $map_recipe_id{$recipe->{'id'}} = $target_dbh->{'mysql_insertid'};
+
     }
 
     return 1;
 
+}
+
+#-------------------------------------------------------------
+
+=head2 create_recipe_authors_table( )
+
+=cut
+
+sub create_recipe_authors_table {
+
+    my $sql = <<END_SQL;
+CREATE TABLE `recipe_authors` (
+  `recipe_author_id` int(10) unsigned NOT NULL auto_increment,
+  `recipe_id`        int(10) unsigned NOT NULL COMMENT 'Verknüpfung zum Rezept',
+  `author_id`        int(10) unsigned NOT NULL COMMENT 'Verknüpfung zum Autor',
+  `order_index`      int(10) unsigned NOT NULL COMMENT 'Sortierreihenfolge',
+  `date_created`     datetime         NOT NULL,
+  PRIMARY KEY             (`recipe_author_id`),
+  KEY         `recipe_id` (`recipe_id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Autoren eines Rezepts'
+END_SQL
 
+    return undef unless $target_dbh->do($sql);
+    return 1;
 
 }
+
+#-------------------------------------------------------------
+
+=head2 import_recipe_authors_table( )
+
+=cut
+
+sub import_recipe_authors_table {
+
+    my $sql = <<END_SQL;
+SELECT `recipe_id`, `author_id`
+  FROM `author_list`
+ ORDER BY `recipe_id`, `author_id`
+END_SQL
+
+    my ( $source_sth, $target_sth, $row, $qparams, $i, $old_rid );
+
+    unless ( $source_sth = $source_dbh->prepare($sql) ) {
+        return undef;
+    }
+
+    return undef unless $source_sth->execute();
+
+    $sql = <<END_SQL;
+INSERT INTO `recipe_authors` ( `recipe_id`, `author_id`, `order_index`, `date_created` )
+    VALUES ( ?, ?, ?, now() )
+END_SQL
+    $target_sth = $target_dbh->prepare($sql);
+    unless ( $target_sth ) {
+        $source_sth->finish();
+        return undef;
+    }
+
+    $old_rid = 0;
+    $i = 0;
+
+    while ( $row = $source_sth->fetchrow_hashref() ) {
+
+        if ( $old_rid != $row->{'recipe_id'} ) {
+            $i = 1;
+        }
+        else {
+            $i++;
+        }
+
+        $qparams = [
+            $map_recipe_id{$row->{'recipe_id'}},
+            $map_author_id{$row->{'author_id'}},
+            $i,
+        ];
+
+        unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+            $source_sth->finish();
+            return undef;
+        }
+    }
+
+    return 1;
+
+}
+
 #-------------------------------------------------------------
 
 =head2 create_user_table( )
@@ -749,5 +861,77 @@ END_SQL
 
 #-------------------------------------------------------------
 
+=head2 create_yield_types_table( )
+
+=cut
+
+sub create_yield_types_table {
+
+    my $sql = <<END_SQL;
+CREATE TABLE IF NOT EXISTS `yield_types` (
+  `yield_type_id`   int(10) unsigned NOT NULL auto_increment,
+  `yield_type_name` varchar(30)      NOT NULL COMMENT 'Name des Ergebnismengentyps',
+  `date_created`    datetime         NOT NULL,
+  PRIMARY KEY                    (`yield_type_id`),
+  UNIQUE KEY   `yield_type_name` (`yield_type_name`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Ergebnismengentypen'
+END_SQL
+
+    return undef unless $target_dbh->do($sql);
+    return 1;
+
+}
+
+#-------------------------------------------------------------
+
+=head2 import_yield_types_table( )
+
+=cut
+
+sub import_yield_types_table {
+
+    my $sql = <<END_SQL;
+SELECT `id`, `name`
+  FROM `yield_types`
+ ORDER BY `name`
+END_SQL
+
+    my ( $source_sth, $target_sth, $type, $qparams );
+
+    unless ( $source_sth = $source_dbh->prepare($sql) ) {
+        return undef;
+    }
+
+    return undef unless $source_sth->execute();
+
+    $sql = <<END_SQL;
+INSERT INTO `yield_types` ( `yield_type_id`, `yield_type_name`, `date_created` )
+    VALUES ( ?, ?, now() )
+END_SQL
+    $target_sth = $target_dbh->prepare($sql);
+    unless ( $target_sth ) {
+        $source_sth->finish();
+        return undef;
+    }
+
+    while ( $type = $source_sth->fetchrow_hashref() ) {
+
+        $qparams = [
+            $type->{'id'},
+            $type->{'name'},
+        ];
+
+        unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+            $source_sth->finish();
+            return undef;
+        }
+    }
+
+    return 1;
+
+}
+
+#-------------------------------------------------------------
+
 __END__