From: Frank Brehm Date: Sat, 11 Aug 2007 11:17:29 +0000 (+0000) Subject: Tabelle ingredient_properties dazu X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=e9aef8188d2e4abf42f68b6e4b7909e361a63ac7;p=cookbook.git Tabelle ingredient_properties dazu git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@23 191103c4-1d37-0410-b3e5-d8c2315c0aac --- diff --git a/lib/CookBook/Db.pm b/lib/CookBook/Db.pm index a8f4708..11014e7 100644 --- a/lib/CookBook/Db.pm +++ b/lib/CookBook/Db.pm @@ -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 index 0000000..7f0aacd --- /dev/null +++ b/lib/CookBook/Db/IngredientProperties.pm @@ -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__ + diff --git a/sbin/initial_import.pl b/sbin/initial_import.pl index 2988500..6715e7a 100755 --- a/sbin/initial_import.pl +++ b/sbin/initial_import.pl @@ -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 = <do($sql); + return 1; + +} ## end sub create_ingredient_properties_table + +#------------------------------------------------------------- + +=head2 import_ingredient_properties_table( ) + +=cut + +sub import_ingredient_properties_table { + + my $sql = <prepare($sql) ) { + return undef; + } + + return undef unless $source_sth->execute(); + + $sql = <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 = <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'};