]> Frank Brehm's Git Trees - cookbook.git/commitdiff
Tabelle ingredient_properties dazu
authorFrank Brehm <frank@brehm-online.com>
Sat, 11 Aug 2007 11:17:29 +0000 (11:17 +0000)
committerFrank Brehm <frank@brehm-online.com>
Sat, 11 Aug 2007 11:17:29 +0000 (11:17 +0000)
git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@23 191103c4-1d37-0410-b3e5-d8c2315c0aac

lib/CookBook/Db.pm
lib/CookBook/Db/IngredientProperties.pm [new file with mode: 0644]
sbin/initial_import.pl

index a8f47089ba395e22268d511dc914dd6d00ef07f2..11014e7dc40fe80cbe621f0fd85f842c2ed51c7b 100644 (file)
@@ -28,6 +28,7 @@ __PACKAGE__->load_classes(
         Categories
         Ingredients
         IngredientGroups
+        IngredientProperties
         Properties
         RecipeAuthors
         RecipeCategories
diff --git a/lib/CookBook/Db/IngredientProperties.pm b/lib/CookBook/Db/IngredientProperties.pm
new file mode 100644 (file)
index 0000000..7f0aacd
--- /dev/null
@@ -0,0 +1,66 @@
+package CookBook::Db::IngredientProperties;
+
+# $Id$
+# $URL$
+
+=head1 NAME
+
+CookBook::Db::IngredientProperties
+
+=head1 DESCRIPTION
+
+Module for abstract database access to the table 'ingredient_properties'
+
+=cut
+
+#---------------------------------------------------------------------------
+
+use strict;
+use warnings;
+
+use CookBook::Common;
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(
+    qw/
+        PK::Auto
+        Core
+        /
+);
+
+__PACKAGE__->table('ingredient_properties');
+
+__PACKAGE__->add_columns(
+    "ingredient_property_id" => {
+        'data_type'         => "INT",
+        'default_value'     => undef,
+        'is_nullable'       => 0,
+        'size'              => 10,
+        'is_auto_increment' => 1,
+        'extras'            => { 'unsigned' => 1 },
+    },
+    "ingredient_id" =>
+        { 'data_type' => "INT", 'default_value' => undef, 'is_nullable' => 1, 'size' => 10, 'extras' => { 'unsigned' => 1 } },
+    "property_id" => { 'data_type' => "INT", 'default_value' => 0, 'is_nullable' => 0, 'size' => 10, 'extras' => { 'unsigned' => 1 } },
+    "amount" => { 'data_type' => "FLOAT", 'default_value' => 0, 'is_nullable' => 0, 'size' => 32, 'extras' => { 'unsigned' => 1 }, },
+    "per_amount" => { 'data_type' => "FLOAT", 'default_value' => 0, 'is_nullable' => 0, 'size' => 32, 'extras' => { 'unsigned' => 1 }, },
+    "per_unit_id" => { 'data_type' => "INT", 'default_value' => undef, 'is_nullable' => 1, 'size' => 10, 'extras' => { 'unsigned' => 1 } },
+    "date_created" => { 'data_type' => "DATETIME", 'default_value' => "", 'is_nullable' => 0, 'size' => 19 },
+    "date_changed" => { 'data_type' => "DATETIME", 'default_value' => "", 'is_nullable' => 0, 'size' => 19 },
+);
+
+__PACKAGE__->set_primary_key("ingredient_property_id");
+__PACKAGE__->add_unique_constraint( "i_p_id", [ "ingredient_id", "property_id", ] );
+
+__PACKAGE__->belongs_to( 'ingredient'       => 'CookBook::Db::Ingredients',      'ingredient_id', );
+__PACKAGE__->belongs_to( 'property'         => 'CookBook::Db::Properties',       'property_id', );
+__PACKAGE__->belongs_to( 'per_unit'         => 'CookBook::Db::Units',            'per_unit_id', );
+
+#----------------------------------------------------------------------------------------
+
+1;
+
+#----------------------------------------------------------------------------------------
+
+__END__
+
index 2988500a056b7559ba16fd21637da75a81f7cf24..6715e7aa108324f5665778c2d05f9fc9b2977981 100755 (executable)
@@ -40,6 +40,7 @@ my @target_tables = qw(
     categories
     ingredients
     ingredient_groups
+    ingredient_properties
     recipes
     recipe_authors
     recipe_categories
@@ -47,21 +48,22 @@ my @target_tables = qw(
 );
 
 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,
+    '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,
 );
 
 my @import_tables = qw(
@@ -72,6 +74,7 @@ my @import_tables = qw(
     properties
     ingredients
     ingredient_groups
+    ingredient_properties
     recipes
     recipe_authors
     recipe_categories
@@ -79,17 +82,18 @@ my @import_tables = qw(
 );
 
 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,
-    '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,
 );
 
 my $admin_data = {
@@ -591,7 +595,7 @@ END_SQL
     return undef unless $target_dbh->do($sql);
     return 1;
 
-} ## end sub create_ingredients_table
+} ## end sub create_categories_table
 
 #-------------------------------------------------------------
 
@@ -637,11 +641,11 @@ END_SQL
             return undef;
         }
 
-    } ## end while ( $ingr = $source_sth->fetchrow_hashref...
+    } ## end while ( $cat = $source_sth->fetchrow_hashref(...
 
     return 1;
 
-} ## end sub import_ingredients_table
+} ## end sub import_categories_table
 
 #-------------------------------------------------------------
 
@@ -789,6 +793,104 @@ END_SQL
 
 #-------------------------------------------------------------
 
+=head2 create_ingredient_properties_table( )
+
+=cut
+
+sub create_ingredient_properties_table {
+
+    my $sql = <<END_SQL;
+CREATE TABLE `ingredient_properties` (
+  `ingredient_property_id`   int(10) unsigned NOT NULL auto_increment,
+  `ingredient_id`            int(10) unsigned NOT NULL               COMMENT 'Verknüpfung zu Zutaten',
+  `property_id`              int(10) unsigned NOT NULL               COMMENT 'Verknüpfung zu Zutat-Eigenschaften',
+  `amount`                   float   unsigned NOT NULL default '0'   COMMENT 'enthaltene Menge in Berechnungsbasis',
+  `per_amount`               float   unsigned NOT NULL default '100' COMMENT 'Größe der Berechnungsbasis',
+  `per_unit_id`              int(10) unsigned          default NULL  COMMENT 'Maßeinheits-Id der Berechnungsbasis',
+  `date_created`             datetime         NOT NULL,
+  `date_changed`             datetime         NOT NULL,
+  PRIMARY KEY                 ( `ingredient_property_id` ),
+  UNIQUE KEY  `i_p_id`        ( `ingredient_id`, `property_id` ),
+  KEY         `property_id`   ( `property_id` ),
+  KEY         `per_unit_id`   ( `per_unit_id` )
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Liste der Eigenschaften einer Zutat'
+END_SQL
+
+    return undef unless $target_dbh->do($sql);
+    return 1;
+
+} ## end sub create_ingredient_properties_table
+
+#-------------------------------------------------------------
+
+=head2 import_ingredient_properties_table( )
+
+=cut
+
+sub import_ingredient_properties_table {
+
+    my $sql = <<END_SQL;
+SELECT `ingredient_id`, `property_id`, `amount`, `per_units`
+  FROM `ingredient_info`
+ ORDER BY `ingredient_id`, `property_id`
+END_SQL
+
+    my ( $source_sth, $target_sth, $row, $qparams, $amount, $per_unit_id );
+
+    unless ( $source_sth = $source_dbh->prepare($sql) ) {
+        return undef;
+    }
+
+    return undef unless $source_sth->execute();
+
+    $sql = <<END_SQL;
+INSERT INTO `ingredient_properties` (
+        `ingredient_id`, `property_id`, `amount`, `per_amount`, `per_unit_id`, `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_id'} and $map_ingredient_id{ $row->{'ingredient_id'} };
+        next unless $row->{'property_id'};
+
+        $i++;
+
+        $amount = $row->{'amount'} || 0;
+        $amount *= 100 if $row->{'per_units'} and $row->{'per_units'} == 2;
+
+        $per_unit_id = undef;
+        $per_unit_id = $map_unit_id{ $row->{'per_units'} } if $row->{'per_units'};
+
+        $qparams = [];
+        push @$qparams, $map_ingredient_id{ $row->{'ingredient_id'} };
+        push @$qparams, $row->{'property_id'};
+        push @$qparams, $amount;
+        push @$qparams, ( ( $row->{'per_units'} and $row->{'per_units'} == 2 ) ? 100 : 1 );
+        push @$qparams, $per_unit_id;
+
+        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_properties_table
+
+#-------------------------------------------------------------
+
 =head2 create_properties_table( )
 
 =cut
@@ -810,7 +912,7 @@ END_SQL
     return undef unless $target_dbh->do($sql);
     return 1;
 
-} ## end sub create_recipe_authors_table
+} ## end sub create_properties_table
 
 #-------------------------------------------------------------
 
@@ -858,11 +960,11 @@ END_SQL
             return undef;
         }
 
-    } ## end while ( $ingr = $source_sth->fetchrow_hashref...
+    } ## end while ( $prop = $source_sth->fetchrow_hashref...
 
     return 1;
 
-} ## end sub import_ingredients_table
+} ## end sub import_properties_table
 
 #-------------------------------------------------------------
 
@@ -1141,7 +1243,7 @@ END_SQL
     return undef unless $target_dbh->do($sql);
     return 1;
 
-} ## end sub create_recipe_authors_table
+} ## end sub create_recipe_categories_table
 
 #-------------------------------------------------------------
 
@@ -1176,7 +1278,7 @@ END_SQL
 
     while ( $row = $source_sth->fetchrow_hashref() ) {
 
-        next unless $row->{'recipe_id'} and $row->{'recipe_id'} > 0;
+        next unless $row->{'recipe_id'}   and $row->{'recipe_id'} > 0;
         next unless $row->{'category_id'} and $row->{'category_id'} > 0;
 
         $qparams = [ $map_recipe_id{ $row->{'recipe_id'} }, $row->{'category_id'}, ];
@@ -1189,7 +1291,7 @@ END_SQL
 
     return 1;
 
-} ## end sub import_recipe_authors_table
+} ## end sub import_recipe_categories_table
 
 #-------------------------------------------------------------
 
@@ -1247,8 +1349,9 @@ END_SQL
 
     $sql = <<END_SQL;
 INSERT INTO `recipe_ingredients` (
-        `recipe_id`, `ingredient_id`, `amount`, `amount_offset`, `unit_id`, `order_index`, `ingredient_group_id`, `date_created`, `date_changed`
-    ) VALUES ( ?, ?, ?, ?, ?, ?, ?, now(), now() )
+        `recipe_ingredient_id`, `recipe_id`, `ingredient_id`, `amount`, `amount_offset`,
+        `unit_id`, `order_index`, `ingredient_group_id`, `date_created`, `date_changed`
+    ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, now(), now() )
 END_SQL
     $target_sth = $target_dbh->prepare($sql);
     unless ($target_sth) {
@@ -1270,6 +1373,7 @@ END_SQL
         }
 
         $qparams = [];
+        push @$qparams, $row->{'id'};
         push @$qparams, $map_recipe_id{ $row->{'recipe_id'} };
         push @$qparams, ( $row->{'ingredient_id'} and $row->{'ingredient_id'} > 0 ) ? $map_ingredient_id{ $row->{'ingredient_id'} } : undef;
         push @$qparams, $row->{'amount'};