--- /dev/null
+package CookBook::Db::RecipeIngredients;
+
+# $Id$
+# $URL$
+
+=head1 NAME
+
+CookBook::Db::RecipeIngredients
+
+=head1 DESCRIPTION
+
+Module for abstract database access to the table 'recipe_ingredients'
+
+=cut
+
+#---------------------------------------------------------------------------
+
+use strict;
+use warnings;
+
+use CookBook::Common;
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(
+ qw/
+ PK::Auto
+ Core
+ /
+);
+
+__PACKAGE__->table('recipe_ingredients');
+
+__PACKAGE__->add_columns(
+ "recipe_ingredient_id" => {
+ 'data_type' => "INT",
+ 'default_value' => undef,
+ 'is_nullable' => 0,
+ 'size' => 10,
+ 'is_auto_increment' => 1,
+ 'extras' => { 'unsigned' => 1 },
+ },
+ "recipe_id" => { 'data_type' => "INT", 'default_value' => 0, 'is_nullable' => 0, 'size' => 10, 'extras' => { 'unsigned' => 1 } },
+ "ingredient_id" =>
+ { 'data_type' => "INT", 'default_value' => undef, 'is_nullable' => 1, 'size' => 10, 'extras' => { 'unsigned' => 1 } },
+ "amount" => { 'data_type' => "FLOAT", 'default_value' => undef, 'is_nullable' => 1, 'size' => 32, 'extras' => { 'unsigned' => 1 }, },
+ "amount_offset" =>
+ { 'data_type' => "FLOAT", 'default_value' => undef, 'is_nullable' => 1, 'size' => 32, 'extras' => { 'unsigned' => 1 }, },
+ "unit_id" => { 'data_type' => "INT", 'default_value' => undef, 'is_nullable' => 1, 'size' => 10, 'extras' => { 'unsigned' => 1 } },
+ "order_index" => { 'data_type' => "INT", 'default_value' => 0, 'is_nullable' => 0, 'size' => 10, 'extras' => { 'unsigned' => 1 } },
+ "ingredient_group_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("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', );
+
+#----------------------------------------------------------------------------------------
+
+1;
+
+#----------------------------------------------------------------------------------------
+
+__END__
+
session_log
authors
yield_types
+ ingredients
+ ingredient_groups
recipes
recipe_authors
+ recipe_ingredients
);
my $admin_data = {
my %map_recipe_id;
my %map_author_id;
+my %map_ingredient_id;
my $port;
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,
+ '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";
}
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 ($@) {
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;
#-------------------------------------------------------------
+=head2 create_ingredients_table( )
+
+=cut
+
+sub create_ingredients_table {
+
+ my $sql = <<END_SQL;
+CREATE TABLE `ingredients` (
+ `ingredient_id` int(10) unsigned NOT NULL auto_increment,
+ `ingredient_name` varchar(100) NOT NULL COMMENT 'Name der Zutat',
+ `date_created` datetime NOT NULL,
+ `date_changed` datetime NOT NULL,
+ PRIMARY KEY (`ingredient_id`),
+ UNIQUE KEY `ingredient_name` (`ingredient_name`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Zutaten'
+END_SQL
+
+ return undef unless $target_dbh->do($sql);
+ return 1;
+
+} ## end sub create_recipe_authors_table
+
+#-------------------------------------------------------------
+
+=head2 import_ingredients_table( )
+
+=cut
+
+sub import_ingredients_table {
+
+ my $sql = <<END_SQL;
+SELECT `id`, `name`
+ FROM `ingredients`
+ ORDER BY `name`
+END_SQL
+
+ my ( $source_sth, $target_sth, $ingr, $qparams );
+
+ unless ( $source_sth = $source_dbh->prepare($sql) ) {
+ return undef;
+ }
+
+ return undef unless $source_sth->execute();
+
+ $sql = <<END_SQL;
+INSERT INTO `ingredients` ( `ingredient_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 ( $ingr = $source_sth->fetchrow_hashref() ) {
+
+ $qparams = [ $ingr->{'name'} ];
+
+ unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+ $source_sth->finish();
+ return undef;
+ }
+
+ $map_ingredient_id{ $ingr->{'id'} } = $target_dbh->{'mysql_insertid'};
+
+ }
+
+ return 1;
+
+} ## end sub import_receipes_table
+
+#-------------------------------------------------------------
+
+=head2 create_ingredient_groups_table( )
+
+=cut
+
+sub create_ingredient_groups_table {
+
+ my $sql = <<END_SQL;
+CREATE TABLE `ingredient_groups` (
+ `ingredient_group_id` int(10) unsigned NOT NULL auto_increment,
+ `ingredient_group_name` varchar(100) NOT NULL COMMENT 'Name der Gruppe',
+ `date_created` datetime NOT NULL,
+ `date_changed` datetime NOT NULL,
+ PRIMARY KEY (`ingredient_group_id`),
+ UNIQUE KEY `ingredient_group_name` (`ingredient_group_name`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Zutaten-Gruppierungstexte'
+END_SQL
+
+ return undef unless $target_dbh->do($sql);
+ return 1;
+
+} ## end sub create_recipe_authors_table
+
+#-------------------------------------------------------------
+
+=head2 import_ingredient_groups_table( )
+
+=cut
+
+sub import_ingredient_groups_table {
+
+ my $sql = <<END_SQL;
+SELECT `id`, `name`
+ FROM `ingredient_groups`
+ ORDER BY `name`
+END_SQL
+
+ my ( $source_sth, $target_sth, $ingr, $qparams );
+
+ unless ( $source_sth = $source_dbh->prepare($sql) ) {
+ return undef;
+ }
+
+ return undef unless $source_sth->execute();
+
+ $sql = <<END_SQL;
+INSERT INTO `ingredient_groups` ( `ingredient_group_id`, `ingredient_group_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 ( $ingr = $source_sth->fetchrow_hashref() ) {
+
+ $qparams = [ $ingr->{'id'}, $ingr->{'name'} ];
+
+ unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+ $source_sth->finish();
+ return undef;
+ }
+
+ }
+
+ return 1;
+
+} ## end sub import_receipes_table
+
+#-------------------------------------------------------------
+
=head2 create_session_table( )
=cut
if ( $old_rid != $row->{'recipe_id'} ) {
$i = 1;
+ $old_rid = $row->{'recipe_id'};
}
else {
$i++;
#-------------------------------------------------------------
+=head2 create_recipe_ingredients_table( )
+
+=cut
+
+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',
+ `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',
+ `date_created` datetime NOT NULL,
+ `date_changed` datetime NOT NULL,
+ PRIMARY KEY ( `recipe_ingredient_id`),
+ KEY `recipe_ingredient_id` ( `recipe_id`, `ingredient_id` ),
+ KEY `unit_id` ( `unit_id` ),
+ KEY `ingredient_group_id` ( `ingredient_group_id` )
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Zutaten zu einem Kochrezept'
+END_SQL
+
+ return undef unless $target_dbh->do($sql);
+ return 1;
+
+} ## end sub create_recipe_authors_table
+
+#-------------------------------------------------------------
+
+=head2 import_recipe_ingredients_table( )
+
+=cut
+
+sub import_recipe_ingredients_table {
+
+ my $sql = <<END_SQL;
+SELECT `id`, `recipe_id`, `ingredient_id`, `amount`, `amount_offset`, `unit_id`, `order_index`, `group_id`
+ FROM `ingredient_list`
+ ORDER BY `recipe_id`, `order_index`
+END_SQL
+
+ my ( $source_sth, $target_sth, $row, $qparams, $i, $old_rid );
+
+ unless ( $source_sth = $source_dbh->prepare($sql) ) {
+ return undef;
+ }
+
+ return undef unless $source_sth->execute();
+
+ $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() )
+END_SQL
+ $target_sth = $target_dbh->prepare($sql);
+ unless ($target_sth) {
+ $source_sth->finish();
+ return undef;
+ }
+
+ $old_rid = 0;
+ $i = 0;
+
+ while ( $row = $source_sth->fetchrow_hashref() ) {
+
+ if ( $old_rid != $row->{'recipe_id'} ) {
+ $i = 1;
+ $old_rid = $row->{'recipe_id'};
+ }
+ else {
+ $i++;
+ }
+
+ $qparams = [];
+ 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'};
+ push @$qparams, $row->{'amount_offset'} || undef;
+ push @$qparams, $row->{'unit_id'};
+ push @$qparams, $i;
+ push @$qparams, ( ( $row->{'group_id'} and $row->{'group_id'} > 0 ) ? $row->{'group_id'} : undef );
+
+ #$qparams = [ $map_recipe_id{ $row->{'recipe_id'} }, $map_author_id{ $row->{'author_id'} }, $i, ];
+
+ unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+ $source_sth->finish();
+ return undef;
+ }
+
+ } ## end while ( $row = $source_sth->fetchrow_hashref(...
+
+ return 1;
+
+} ## end sub import_recipe_authors_table
+
+#-------------------------------------------------------------
+
=head2 create_user_table( )
Erstellt die Tabelle 'users' ...