--- /dev/null
+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__
+
categories
ingredients
ingredient_groups
+ ingredient_properties
recipes
recipe_authors
recipe_categories
);
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(
properties
ingredients
ingredient_groups
+ ingredient_properties
recipes
recipe_authors
recipe_categories
);
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 = {
return undef unless $target_dbh->do($sql);
return 1;
-} ## end sub create_ingredients_table
+} ## end sub create_categories_table
#-------------------------------------------------------------
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
#-------------------------------------------------------------
#-------------------------------------------------------------
+=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
return undef unless $target_dbh->do($sql);
return 1;
-} ## end sub create_recipe_authors_table
+} ## end sub create_properties_table
#-------------------------------------------------------------
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
#-------------------------------------------------------------
return undef unless $target_dbh->do($sql);
return 1;
-} ## end sub create_recipe_authors_table
+} ## end sub create_recipe_categories_table
#-------------------------------------------------------------
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'}, ];
return 1;
-} ## end sub import_recipe_authors_table
+} ## end sub import_recipe_categories_table
#-------------------------------------------------------------
$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) {
}
$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'};