]> Frank Brehm's Git Trees - cookbook.git/commitdiff
Vorbereitungsmethoden angefangen
authorFrank Brehm <frank@brehm-online.com>
Sat, 11 Aug 2007 12:13:49 +0000 (12:13 +0000)
committerFrank Brehm <frank@brehm-online.com>
Sat, 11 Aug 2007 12:13:49 +0000 (12:13 +0000)
git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@24 191103c4-1d37-0410-b3e5-d8c2315c0aac

lib/CookBook/Db/Recipes.pm
sbin/initial_import.pl

index 07576fed05170669e10d8b3d5accc7f620ff49a3..337d195aaddeba0c56f8bdadfc91d3db5829fecf 100644 (file)
@@ -39,13 +39,15 @@ __PACKAGE__->add_columns(
         'is_auto_increment' => 1,
         'extras'            => { 'unsigned' => 1 },
     },
-    "rid"          => { 'data_type' => "CHAR",    'default_value' => "",  'is_nullable' => 0, 'size' => 32, },
-    "title"        => { 'data_type' => "VARCHAR", 'default_value' => "",  'is_nullable' => 0, 'size' => 250, },
-    "published"    => { 'data_type' => "ENUM",    'default_value' => "n", 'is_nullable' => 0, 'size' => 1, },
-    "deleted"      => { 'data_type' => "ENUM",    'default_value' => "n", 'is_nullable' => 0, 'size' => 1, },
-    "locked"       => { 'data_type' => "ENUM",    'default_value' => "n", 'is_nullable' => 0, 'size' => 1, },
-    "preface"      => { 'data_type' => "TEXT",    'default_value' => "",  'is_nullable' => 1, 'size' => 65535, },
-    "instructions" => { 'data_type' => "TEXT",    'default_value' => "",  'is_nullable' => 0, 'size' => 65535, },
+    "rid"              => { 'data_type' => "CHAR",     'default_value' => "",    'is_nullable' => 0, 'size' => 32, },
+    "title"            => { 'data_type' => "VARCHAR",  'default_value' => "",    'is_nullable' => 0, 'size' => 250, },
+    "published"        => { 'data_type' => "ENUM",     'default_value' => "n",   'is_nullable' => 0, 'size' => 1, },
+    "deleted"          => { 'data_type' => "ENUM",     'default_value' => "n",   'is_nullable' => 0, 'size' => 1, },
+    "locked"           => { 'data_type' => "ENUM",     'default_value' => "n",   'is_nullable' => 0, 'size' => 1, },
+    "edit_session_id"  => { 'data_type' => "VARCHAR",  'default_value' => undef, 'is_nullable' => 1, 'size' => 72, },
+    "last_edit_action" => { 'data_type' => "DATETIME", 'default_value' => undef, 'is_nullable' => 1, 'size' => 19, },
+    "preface"          => { 'data_type' => "TEXT",     'default_value' => "",    'is_nullable' => 1, 'size' => 65535, },
+    "instructions"     => { 'data_type' => "TEXT",     'default_value' => "",    'is_nullable' => 0, 'size' => 65535, },
     "yield_amount" => { 'data_type' => "FLOAT", 'default_value' => 0, 'is_nullable' => 0, 'size' => 32, 'extras' => { 'unsigned' => 1 }, },
     "yield_amount_offset" =>
         { 'data_type' => "FLOAT", 'default_value' => undef, 'is_nullable' => 1, 'size' => 32, 'extras' => { 'unsigned' => 1 }, },
index 6715e7aa108324f5665778c2d05f9fc9b2977981..7c94a87259ec52ee2bd503209bb4bdea29e61bfd 100755 (executable)
@@ -37,6 +37,7 @@ my @target_tables = qw(
     unit_types
     units
     properties
+    prep_methods
     categories
     ingredients
     ingredient_groups
@@ -45,25 +46,28 @@ my @target_tables = qw(
     recipe_authors
     recipe_categories
     recipe_ingredients
+    ingredient_prep_methods
 );
 
 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,
-    'yield_types'           => \&create_yield_types_table,
-    'recipe_authors'        => \&create_recipe_authors_table,
-    'ingredients'           => \&create_ingredients_table,
-    'ingredient_groups'     => \&create_ingredient_groups_table,
-    'recipe_ingredients'    => \&create_recipe_ingredients_table,
-    'unit_types'            => \&create_unit_types_table,
-    'units'                 => \&create_units_table,
-    'categories'            => \&create_categories_table,
-    'recipe_categories'     => \&create_recipe_categories_table,
-    'properties'            => \&create_properties_table,
-    'ingredient_properties' => \&create_ingredient_properties_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,
+    'ingredients'             => \&create_ingredients_table,
+    'ingredient_groups'       => \&create_ingredient_groups_table,
+    'recipe_ingredients'      => \&create_recipe_ingredients_table,
+    'unit_types'              => \&create_unit_types_table,
+    'units'                   => \&create_units_table,
+    'categories'              => \&create_categories_table,
+    'recipe_categories'       => \&create_recipe_categories_table,
+    'properties'              => \&create_properties_table,
+    'ingredient_properties'   => \&create_ingredient_properties_table,
+    'prep_methods'            => \&create_prep_methods_table,
+    'ingredient_prep_methods' => \&create_ingredient_prep_methods_table,
 );
 
 my @import_tables = qw(
@@ -72,6 +76,7 @@ my @import_tables = qw(
     units
     categories
     properties
+    prep_methods
     ingredients
     ingredient_groups
     ingredient_properties
@@ -79,21 +84,24 @@ my @import_tables = qw(
     recipe_authors
     recipe_categories
     recipe_ingredients
+    ingredient_prep_methods
 );
 
 my %import_method = (
-    'authors'               => \&import_author_table,
-    'categories'            => \&import_categories_table,
-    'recipe_categories'     => \&import_recipe_categories_table,
-    'yield_types'           => \&import_yield_types_table,
-    'units'                 => \&import_units_table,
-    'ingredients'           => \&import_ingredients_table,
-    'ingredient_groups'     => \&import_ingredient_groups_table,
-    'ingredient_properties' => \&import_ingredient_properties_table,
-    'recipes'               => \&import_recipes_table,
-    'recipe_authors'        => \&import_recipe_authors_table,
-    'recipe_ingredients'    => \&import_recipe_ingredients_table,
-    'properties'            => \&import_properties_table,
+    'authors'                 => \&import_author_table,
+    'categories'              => \&import_categories_table,
+    'recipe_categories'       => \&import_recipe_categories_table,
+    'yield_types'             => \&import_yield_types_table,
+    'units'                   => \&import_units_table,
+    'ingredients'             => \&import_ingredients_table,
+    'ingredient_groups'       => \&import_ingredient_groups_table,
+    'ingredient_properties'   => \&import_ingredient_properties_table,
+    'recipes'                 => \&import_recipes_table,
+    'recipe_authors'          => \&import_recipe_authors_table,
+    'recipe_ingredients'      => \&import_recipe_ingredients_table,
+    'properties'              => \&import_properties_table,
+    'prep_methods'            => \&import_prep_methods_table,
+    'ingredient_prep_methods' => \&import_ingredient_prep_methods_table,
 );
 
 my $admin_data = {
@@ -488,13 +496,11 @@ sub create_author_table {
 CREATE TABLE `authors` (
   `author_id`     int(10) unsigned NOT NULL auto_increment,
   `user_id`       int(10) unsigned,
-  `old_author_id` int(11)                   COMMENT 'alte ID aus Kreceipes',
   `author_name`   varchar(100)     NOT NULL COMMENT 'Name des Autors',
   `date_created`  datetime         NOT NULL,
   PRIMARY KEY                   (`author_id`),
   UNIQUE KEY   `author_name`    (`author_name`),
   KEY          `user_id`        (`user_id`),
-  KEY          `old_author_id`  (`old_author_id`),
   FULLTEXT KEY `author_name_ft` (`author_name`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Kochrezept-Autoren'
 END_SQL
@@ -503,9 +509,9 @@ END_SQL
 
     $sql = <<END_SQL;
 INSERT INTO `authors`
-    ( `author_id`, `user_id`, `old_author_id`, `author_name`, `date_created` )
+    ( `author_id`, `user_id`, `author_name`, `date_created` )
   VALUES
-    ( 1, 1, NULL, %s, now() )
+    ( 1, 1, %s, now() )
 END_SQL
 
     $sql = sprintf( $sql, $target_dbh->quote( $admin_data->{'vorname'} . " " . $admin_data->{'nachname'} ) );
@@ -524,7 +530,7 @@ END_SQL
 sub import_author_table {
 
     my $sql = 'SELECT `id`, `name` FROM `authors` order by `name`';
-    my ( $source_sth, $target_sth, $author, $row, $count, $qparams, $do_insert );
+    my ( $source_sth, $target_sth, $author, $row, $count, $qparams );
 
     unless ( $source_sth = $source_dbh->prepare($sql) ) {
         return undef;
@@ -533,6 +539,7 @@ sub import_author_table {
     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) {
@@ -546,25 +553,20 @@ sub import_author_table {
         $row   = $target_sth->fetchrow_hashref();
         $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` = ?';
+            $map_author_id{ $author->{'id'} } = 1;
         }
         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) {
+            $qparams = [ $author->{'name'} ];
+            $sql     = 'INSERT INTO `authors` ( `author_name`, `date_created` ) VALUES ( ?, now() )';
+            unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+                $source_sth->finish();
+                return undef;
+            }
             $map_author_id{ $author->{'id'} } = $target_dbh->{'mysql_insertid'};
         }
-        else {
-            $map_author_id{ $author->{'id'} } = 1;
-        }
+
     } ## end while ( $author = $source_sth->fetchrow_hashref...
 
     return 1;
@@ -891,6 +893,163 @@ END_SQL
 
 #-------------------------------------------------------------
 
+=head2 create_ingredient_prep_methods_table( )
+
+=cut
+
+sub create_ingredient_prep_methods_table {
+
+    my $sql = <<END_SQL;
+CREATE TABLE IF NOT EXISTS `ingredient_prep_methods` (
+  `ingredient_prep_method_id` int(10) unsigned NOT NULL auto_increment,
+  `recipe_ingredient_id`      int(10) unsigned NOT NULL COMMENT 'Verknüpfung zur Zutat eines Rezepts',
+  `prep_method_id`            int(10) unsigned NOT NULL COMMENT 'Verknüpfung zu Vorbereitungsmethoden',
+  `order_index`               int(10) unsigned NOT NULL COMMENT 'Sortierreihenfolge',
+  `date_created`              datetime         NOT NULL,
+  `date_changed`              datetime         NOT NULL,
+  PRIMARY KEY                  ( `ingredient_prep_method_id` ),
+  UNIQUE KEY  `r_p_id`         ( `recipe_ingredient_id`, `prep_method_id` ),
+  KEY         `prep_method_id` ( `prep_method_id` )
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Vorbereitungsmethoden einer Zutat eines Rezepts'
+END_SQL
+
+    return undef unless $target_dbh->do($sql);
+    return 1;
+
+} ## end sub create_ingredient_prep_methods_table
+
+#-------------------------------------------------------------
+
+=head2 import_ingredient_prep_methods_table( )
+
+=cut
+
+sub import_ingredient_prep_methods_table {
+
+    my $sql = <<END_SQL;
+SELECT `ingredient_list_id`, `prep_method_id`, `order_index`
+  FROM `prep_method_list`
+ ORDER BY `ingredient_list_id`, `order_index`
+END_SQL
+
+    my ( $source_sth, $target_sth, $row, $qparams );
+
+    unless ( $source_sth = $source_dbh->prepare($sql) ) {
+        return undef;
+    }
+
+    return undef unless $source_sth->execute();
+
+    $sql = <<END_SQL;
+INSERT INTO `ingredient_prep_methods` (
+        `recipe_ingredient_id`, `prep_method_id`, `order_index`, `date_created`, `date_changed`
+    ) VALUES ( ?, ?, ?, now(), now() )
+END_SQL
+    $target_sth = $target_dbh->prepare($sql);
+    unless ($target_sth) {
+        $source_sth->finish();
+        return undef;
+    }
+
+    my $i = 0;
+
+    while ( $row = $source_sth->fetchrow_hashref() ) {
+
+        next unless $row->{'ingredient_list_id'};
+        next unless $row->{'prep_method_id'};
+
+        $i++;
+
+        $qparams = [];
+        push @$qparams, $row->{'ingredient_list_id'};
+        push @$qparams, $row->{'prep_method_id'};
+        push @$qparams, $row->{'order_index'};
+
+        unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+            $source_sth->finish();
+            return undef;
+        }
+
+        print "." unless $i % 100;
+
+    } ## end while ( $row = $source_sth->fetchrow_hashref(...
+
+    return 1;
+
+} ## end sub import_ingredient_prep_methods_table
+
+#-------------------------------------------------------------
+
+=head2 create_prep_methods_table( )
+
+=cut
+
+sub create_prep_methods_table {
+
+    my $sql = <<END_SQL;
+CREATE TABLE IF NOT EXISTS `prep_methods` (
+  `prep_method_id`   int(10) unsigned NOT NULL auto_increment,
+  `prep_method_name` varchar(30)      NOT NULL COMMENT 'Name der Vorbereitungsmethode',
+  `date_created`     datetime         NOT NULL,
+  `date_changed`     datetime         NOT NULL,
+  PRIMARY KEY                     (`prep_method_id`),
+  UNIQUE KEY   `prep_method_name` (`prep_method_name`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Vorbereitungsmethoden'
+END_SQL
+
+    return undef unless $target_dbh->do($sql);
+    return 1;
+
+} ## end sub create_prep_methods_table
+
+#-------------------------------------------------------------
+
+=head2 import_prep_methods_table( )
+
+=cut
+
+sub import_prep_methods_table {
+
+    my $sql = <<END_SQL;
+SELECT `id`, `name`
+  FROM `prep_methods`
+ 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 `prep_methods` ( `prep_method_id`, `prep_method_name`, `date_created`, `date_changed` )
+    VALUES ( ?, ?, now(), 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 sub import_prep_methods_table
+
+#-------------------------------------------------------------
+
 =head2 create_properties_table( )
 
 =cut
@@ -1032,12 +1191,13 @@ sub create_recipes_table {
     my $sql = <<END_SQL;
 CREATE TABLE `recipes` (
   `recipe_id`           int(10) unsigned NOT NULL auto_increment,
-  `old_recipe_id`       int(11)                   default NULL COMMENT 'alte ID aus Kreceipes',
   `rid`                 char(32)         NOT NULL              COMMENT 'MD5-ID des Rezeptes',
   `title`               varchar(250)     NOT NULL              COMMENT 'Rezepttitel',
   `published`           enum('n','y')    NOT NULL default 'n'  COMMENT 'Ist das Rezept veröffentlicht?',
   `deleted`             enum('n','y')    NOT NULL              COMMENT 'Wurde dieses Rezept gelöscht?',
   `locked`              enum('n','y')    NOT NULL default 'n'  COMMENT 'Ist das Rezept von einem Administrator gesperrt?',
+  `edit_session_id`     varchar(72)               default NULL COMMENT 'Sitzungs-Id des aktuell Bearbeitenden',
+  `last_edit_action`    datetime                  default NULL COMMENT 'Zeitpunkt der letzten Editieraktion',
   `preface`             text                                   COMMENT 'Vorwort zum Rezept',
   `instructions`        text             NOT NULL              COMMENT 'Zubereitungsanleitung',
   `yield_amount`        float unsigned   NOT NULL default '0'  COMMENT 'Ergebnismenge',
@@ -1050,7 +1210,6 @@ CREATE TABLE `recipes` (
   `user_changed`        int(10) unsigned NOT NULL              COMMENT 'User-Id des letzten Änderers',
   PRIMARY KEY                    (`recipe_id`),
   UNIQUE KEY   `rid`             (`rid`),
-  KEY          `old_recipe_id`   (`old_recipe_id`),
   KEY          `title`           (`title`),
   FULLTEXT KEY `title_ft`        (`title`),
   FULLTEXT KEY `instructions_ft` (`instructions`),
@@ -1088,12 +1247,12 @@ END_SQL
 
     $sql = <<END_SQL;
 INSERT INTO `recipes` (
-       `old_recipe_id`, `rid`, `title`, `published`, `deleted`, `locked`, `preface`, `instructions`,
+       `rid`, `title`, `published`, `deleted`, `locked`, `preface`, `instructions`,
        `yield_amount`, `yield_amount_offset`, `yield_type_id`, `prep_time`,
        `date_created`, `user_created`, `date_changed`, `user_changed`
     )
     VALUES (
-        ?, ?, ?, 'y', 'n', 'n', NULL, ?,
+        ?, ?, 'y', 'n', 'n', NULL, ?,
         ?, ?, ?, ?,
         ?, 1, ?, 1
     )
@@ -1113,7 +1272,6 @@ END_SQL
         my $digest = md5_hex($digest_source);
 
         $qparams = [
-            $recipe->{'id'},
             $digest,
             $recipe->{'title'},
             $recipe->{'instructions'},
@@ -1304,13 +1462,13 @@ sub create_recipe_ingredients_table {
     my $sql = <<END_SQL;
 CREATE TABLE `recipe_ingredients` (
   `recipe_ingredient_id`  int(10) unsigned NOT NULL auto_increment,
-  `recipe_id`             int(10) unsigned NOT NULL              COMMENT 'Verknüfung zu recipes',
-  `ingredient_id`         int(10) unsigned          default NULL COMMENT 'Verknüfung zu ingredients',
+  `recipe_id`             int(10) unsigned NOT NULL              COMMENT 'Verknüfung zu Rezepte',
+  `ingredient_id`         int(10) unsigned          default NULL COMMENT 'Verknüfung zu Zutaten',
   `amount`                float   unsigned          default NULL COMMENT 'Menge der Zutat',
   `amount_offset`         float   unsigned          default NULL COMMENT 'Mengen-Spielraum',
   `unit_id`               int(10) unsigned          default NULL COMMENT 'Verknüfung zu units',
   `order_index`           int(10) unsigned NOT NULL default '0'  COMMENT 'Sortierreihenfolge im Rezept',
-  `ingredient_group_id`   int(10) unsigned          default NULL COMMENT 'Verknüfung zu ingredient_groups',
+  `ingredient_group_id`   int(10) unsigned          default NULL COMMENT 'Verknüfung zu Zutat-Gruppierungen',
   `date_created`          datetime         NOT NULL,
   `date_changed`          datetime         NOT NULL,
   PRIMARY KEY                         ( `recipe_ingredient_id`),
@@ -1339,7 +1497,7 @@ SELECT `id`, `recipe_id`, `ingredient_id`, `amount`, `amount_offset`, `unit_id`,
  ORDER BY `recipe_id`, `order_index`
 END_SQL
 
-    my ( $source_sth, $target_sth, $row, $qparams, $i, $old_rid );
+    my ( $source_sth, $target_sth, $row, $qparams, $i, $old_rid, $j );
 
     unless ( $source_sth = $source_dbh->prepare($sql) ) {
         return undef;
@@ -1361,6 +1519,7 @@ END_SQL
 
     $old_rid = 0;
     $i       = 0;
+    $j       = 0;
 
     while ( $row = $source_sth->fetchrow_hashref() ) {
 
@@ -1371,6 +1530,7 @@ END_SQL
         else {
             $i++;
         }
+        $j++;
 
         $qparams = [];
         push @$qparams, $row->{'id'};
@@ -1389,6 +1549,8 @@ END_SQL
             return undef;
         }
 
+        print "." unless $j % 100;
+
     } ## end while ( $row = $source_sth->fetchrow_hashref(...
 
     return 1;