From: Frank Brehm Date: Fri, 10 Aug 2007 09:09:55 +0000 (+0000) Subject: Maßeinheiten dazu X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=d577d29a660c449c9eb4b9e949392b9109788c82;p=cookbook.git Maßeinheiten dazu git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@18 191103c4-1d37-0410-b3e5-d8c2315c0aac --- diff --git a/lib/CookBook/Db.pm b/lib/CookBook/Db.pm index f2440dd..0a12758 100644 --- a/lib/CookBook/Db.pm +++ b/lib/CookBook/Db.pm @@ -27,11 +27,13 @@ __PACKAGE__->load_classes( Authors Ingredients IngredientGroups - Recipes RecipeAuthors RecipeIngredients - Session + Recipes SessionLog + Session + Units + UnitTypes Users YieldTypes / diff --git a/lib/CookBook/Db/RecipeIngredients.pm b/lib/CookBook/Db/RecipeIngredients.pm index abc025f..4a26dfc 100644 --- a/lib/CookBook/Db/RecipeIngredients.pm +++ b/lib/CookBook/Db/RecipeIngredients.pm @@ -58,7 +58,7 @@ __PACKAGE__->set_primary_key("recipe_ingredient_id"); __PACKAGE__->belongs_to( 'recipe' => 'CookBook::Db::Recipes', 'recipe_id', ); __PACKAGE__->belongs_to( 'ingredient' => 'CookBook::Db::Ingredients', 'ingredient_id', ); __PACKAGE__->belongs_to( 'ingredient_group' => 'CookBook::Db::IngredientGroupss', 'ingredient_group_id', ); -#__PACKAGE__->belongs_to( 'unit' => 'CookBook::Db::Units', 'unit_id', ); +__PACKAGE__->belongs_to( 'unit' => 'CookBook::Db::Units', 'unit_id', ); #---------------------------------------------------------------------------------------- diff --git a/lib/CookBook/Db/UnitTypes.pm b/lib/CookBook/Db/UnitTypes.pm new file mode 100644 index 0000000..fff80a4 --- /dev/null +++ b/lib/CookBook/Db/UnitTypes.pm @@ -0,0 +1,56 @@ +package CookBook::Db::UnitTypes; + +# $Id$ +# $URL$ + +=head1 NAME + +CookBook::Db::UnitTypes + +=head1 DESCRIPTION + +Module for abstract database access to the table 'yield_types' + +=cut + +#--------------------------------------------------------------------------- + +use strict; +use warnings; + +use CookBook::Common; +use base qw/DBIx::Class/; + +__PACKAGE__->load_components( + qw/ + PK::Auto + Core + / +); + +__PACKAGE__->table('unit_types'); + +__PACKAGE__->add_columns( + "unit_type_id" => { + 'data_type' => "INT", + 'default_value' => undef, + 'is_nullable' => 0, + 'size' => 10, + 'is_auto_increment' => 1, + 'extras' => { 'unsigned' => 1 }, + }, + "unit_type_name" => { 'data_type' => "VARCHAR", 'default_value' => "", 'is_nullable' => 0, 'size' => 30 }, + "date_created" => { 'data_type' => "DATETIME", 'default_value' => "", 'is_nullable' => 0, 'size' => 19 }, +); + +__PACKAGE__->set_primary_key("yield_type_id"); +__PACKAGE__->add_unique_constraint( "unit_type_name", ["unit_type_name"] ); + +#---------------------------------------------------------------------------------------- + +1; + +#---------------------------------------------------------------------------------------- + +__END__ + diff --git a/lib/CookBook/Db/Units.pm b/lib/CookBook/Db/Units.pm new file mode 100644 index 0000000..aba7696 --- /dev/null +++ b/lib/CookBook/Db/Units.pm @@ -0,0 +1,65 @@ +package CookBook::Db::Units; + +# $Id$ +# $URL$ + +=head1 NAME + +CookBook::Db::Units + +=head1 DESCRIPTION + +Module for abstract database access to the table 'units' + +=cut + +#--------------------------------------------------------------------------- + +use strict; +use warnings; + +use CookBook::Common; +use base qw/DBIx::Class/; + +__PACKAGE__->load_components( + qw/ + PK::Auto + Core + / +); + +__PACKAGE__->table('units'); + +__PACKAGE__->add_columns( + "unit_id" => { + 'data_type' => "INT", + 'default_value' => undef, + 'is_nullable' => 0, + 'size' => 10, + 'is_auto_increment' => 1, + 'extras' => { 'unsigned' => 1 }, + }, + "unit_name" => { 'data_type' => "VARCHAR", 'default_value' => "", 'is_nullable' => 0, 'size' => 50 }, + "unit_name_abbrev" => { 'data_type' => "VARCHAR", 'default_value' => undef, 'is_nullable' => 1, 'size' => 20 }, + "unit_plural" => { 'data_type' => "VARCHAR", 'default_value' => "", 'is_nullable' => 0, 'size' => 50 }, + "unit_plural_abbrev" => { 'data_type' => "VARCHAR", 'default_value' => undef, 'is_nullable' => 1, 'size' => 20 }, + "unit_type_id" => { 'data_type' => "INT", 'default_value' => '0', 'is_nullable' => 0, '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("unit_id"); +__PACKAGE__->add_unique_constraint( "unit_name", ["unit_name"] ); + + +__PACKAGE__->belongs_to( 'unit_type' => 'CookBook::Db::UnitTypes', 'unit_type_id', ); + + +#---------------------------------------------------------------------------------------- + +1; + +#---------------------------------------------------------------------------------------- + +__END__ + diff --git a/sbin/initial_import.pl b/sbin/initial_import.pl index 24240b6..4c8cc65 100755 --- a/sbin/initial_import.pl +++ b/sbin/initial_import.pl @@ -34,6 +34,8 @@ my @target_tables = qw( session_log authors yield_types + unit_types + units ingredients ingredient_groups recipes @@ -41,6 +43,43 @@ my @target_tables = qw( recipe_ingredients ); +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, +); + +my @import_tables = qw( + authors + yield_types + units + ingredients + ingredient_groups + recipes + recipe_authors + recipe_ingredients +); + +my %import_method = ( + 'authors' => \&import_author_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, +); + my $admin_data = { 'login' => 'frank', 'vorname' => 'Frank', @@ -95,6 +134,7 @@ my ( $target_dbh, $source_dbh ); my %map_recipe_id; my %map_author_id; +my %map_unit_id; my %map_ingredient_id; my $port; @@ -367,19 +407,6 @@ 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, - '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, - ); - print green_star() . " Erstelle Ziel-Tabellen ...\n"; for my $table (@target_tables) { @@ -403,6 +430,36 @@ sub create_target_tables { #------------------------------------------------------------- +=head2 import_data( ) + +=cut + +sub import_data { + + print green_star() . " Importiere Tabellen ...\n"; + + for my $table (@import_tables) { + my $method = $import_method{$table}; + unless ($method) { + die "Keine Import-Methode für Tabelle '$method' gefunden.\n"; + } + print " - " . $table . " "; + eval { die "Tabelle '" . $table . "' wurde nicht importiert.\n" unless $method->(); }; + if ($@) { + print not_ok() . "\n"; + warn $@ . "\n"; + return undef; + } + print ok() . "\n"; + } ## end for my $table (@target_tables) + print "\n"; + + return 1; + +} ## end sub import_data + +#------------------------------------------------------------- + =head2 create_author_table( ) Erstellt die Tabelle 'authors' ... @@ -444,85 +501,6 @@ END_SQL #------------------------------------------------------------- -=head2 import_data( ) - -=cut - -sub import_data { - - print green_star() . " Importiere Tabellen ...\n"; - - print " - authors ... "; - eval { die "Tabelle 'authors' wurde nicht importiert.\n" unless import_author_table(); }; - if ($@) { - print not_ok() . "\n"; - warn $@ . "\n"; - return undef; - } - 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 " - ingredients ... "; - eval { die "Tabelle 'ingredients' wurde nicht importiert.\n" unless import_ingredients_table(); }; - if ($@) { - print not_ok() . "\n"; - warn $@ . "\n"; - return undef; - } - print ok() . "\n"; - - print " - ingredient_groups ... "; - eval { die "Tabelle 'ingredient_groups' wurde nicht importiert.\n" unless import_ingredient_groups_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 ($@) { - print not_ok() . "\n"; - warn $@ . "\n"; - return undef; - } - 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 " - recipe_ingredients ... "; - eval { die "Tabelle 'recipe_ingredients' wurde nicht importiert.\n" unless import_recipe_ingredients_table(); }; - if ($@) { - print not_ok() . "\n"; - warn $@ . "\n"; - return undef; - } - print ok() . "\n"; - - - print "\n"; - return 1; - -} ## end sub import_data - -#------------------------------------------------------------- - =head2 import_author_table( ) =cut @@ -820,11 +798,11 @@ END_SQL #------------------------------------------------------------- -=head2 import_receipes_table( ) +=head2 import_recipes_table( ) =cut -sub import_receipes_table { +sub import_recipes_table { my $sql = <{'ingredient_id'} and $row->{'ingredient_id'} > 0 ) ? $map_ingredient_id{ $row->{'ingredient_id'} } : undef; push @$qparams, $row->{'amount'}; push @$qparams, $row->{'amount_offset'} || undef; - push @$qparams, $row->{'unit_id'}; + push @$qparams, ( $row->{'unit_id'} and $row->{'unit_id'} > 0 ) ? $map_unit_id{$row->{'unit_id'}} : undef; push @$qparams, $i; push @$qparams, ( ( $row->{'group_id'} and $row->{'group_id'} > 0 ) ? $row->{'group_id'} : undef ); @@ -1077,6 +1055,143 @@ END_SQL #------------------------------------------------------------- +=head2 create_units_table( ) + +=cut + +sub create_units_table { + + my $sql = <do($sql); + + return 1; + +} + +#------------------------------------------------------------- + +=head2 import_units_table( ) + +=cut + +sub import_units_table { + + my $sql = <prepare($sql) ) { + return undef; + } + + return undef unless $source_sth->execute(); + + $sql = <prepare($sql); + unless ($target_sth) { + $source_sth->finish(); + return undef; + } + + while ( $row = $source_sth->fetchrow_hashref() ) { + + $qparams = []; + push @$qparams, $row->{'name'} || ''; + push @$qparams, $row->{'name_abbrev'}; + push @$qparams, $row->{'plural'} || ''; + push @$qparams, $row->{'plural_abbrev'}; + push @$qparams, ( defined $row->{'type'} ? $row->{'type'} : 0 ) + 1; + + unless ( $target_dbh->do( $sql, {}, @$qparams ) ) { + $source_sth->finish(); + return undef; + } + + $map_unit_id{ $row->{'id'} } = $target_dbh->{'mysql_insertid'}; + + } + + return 1; + +} + +#------------------------------------------------------------- + +=head2 create_unit_types_table( ) + +=cut + +sub create_unit_types_table { + + my $sql = <do($sql); + + my @Units = qw( + Sonstiges + Masse + Volumen + ); + + my $i = 0; + my $qparams; + + $sql = <do( $sql, {}, @$qparams ) ) { + return undef; + } + + } + + return 1; + +} ## end sub create_yield_types_table + +#------------------------------------------------------------- + =head2 create_user_table( ) Erstellt die Tabelle 'users' ...