]> Frank Brehm's Git Trees - cookbook.git/commitdiff
Bis zu den Zutaten des Rezepts gekommen
authorFrank Brehm <frank@brehm-online.com>
Fri, 10 Aug 2007 07:57:46 +0000 (07:57 +0000)
committerFrank Brehm <frank@brehm-online.com>
Fri, 10 Aug 2007 07:57:46 +0000 (07:57 +0000)
git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@17 191103c4-1d37-0410-b3e5-d8c2315c0aac

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

index fb77c841c9bef8703953c6ec9cd6de5170de53e7..f2440dd11a482aa337b41220ecd270644a04033c 100644 (file)
@@ -25,8 +25,11 @@ use base qw/DBIx::Class::Schema/;
 __PACKAGE__->load_classes(
     qw/
         Authors
-        RecipeAuthors
+        Ingredients
+        IngredientGroups
         Recipes
+        RecipeAuthors
+        RecipeIngredients
         Session
         SessionLog
         Users
diff --git a/lib/CookBook/Db/IngredientGroups.pm b/lib/CookBook/Db/IngredientGroups.pm
new file mode 100644 (file)
index 0000000..b2170ba
--- /dev/null
@@ -0,0 +1,59 @@
+package CookBook::Db::IngredientGroups;
+
+# $Id$
+# $URL$
+
+=head1 NAME
+
+CookBook::Db::IngredientGroups
+
+=head1 DESCRIPTION
+
+Module for abstract database access to the table 'ingredient_groups'
+
+=cut
+
+#---------------------------------------------------------------------------
+
+use strict;
+use warnings;
+
+use CookBook::Common;
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(
+    qw/
+        PK::Auto
+        Core
+        /
+);
+
+__PACKAGE__->table('ingredient_groups');
+
+__PACKAGE__->add_columns(
+    "ingredient_group_id" => {
+        'data_type'         => "INT",
+        'default_value'     => undef,
+        'is_nullable'       => 0,
+        'size'              => 10,
+        'is_auto_increment' => 1,
+        'extras'            => { 'unsigned' => 1 },
+    },
+    "ingredient_group_name" => { 'data_type' => "VARCHAR",  'default_value' => "", 'is_nullable' => 0, 'size' => 100 },
+    "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_group_id");
+__PACKAGE__->add_unique_constraint( "ingredient_group_name", ["ingredient_group_name"] );
+
+# __PACKAGE__->might_have( 'user' => 'CookBook::Db::Users', { 'foreign.user_id' => 'self.user_id' } );
+
+#----------------------------------------------------------------------------------------
+
+1;
+
+#----------------------------------------------------------------------------------------
+
+__END__
+
diff --git a/lib/CookBook/Db/Ingredients.pm b/lib/CookBook/Db/Ingredients.pm
new file mode 100644 (file)
index 0000000..176ac81
--- /dev/null
@@ -0,0 +1,59 @@
+package CookBook::Db::Ingredients;
+
+# $Id$
+# $URL$
+
+=head1 NAME
+
+CookBook::Db::Ingredients
+
+=head1 DESCRIPTION
+
+Module for abstract database access to the table 'ingredients'
+
+=cut
+
+#---------------------------------------------------------------------------
+
+use strict;
+use warnings;
+
+use CookBook::Common;
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(
+    qw/
+        PK::Auto
+        Core
+        /
+);
+
+__PACKAGE__->table('ingredients');
+
+__PACKAGE__->add_columns(
+    "ingredient_id" => {
+        'data_type'         => "INT",
+        'default_value'     => undef,
+        'is_nullable'       => 0,
+        'size'              => 10,
+        'is_auto_increment' => 1,
+        'extras'            => { 'unsigned' => 1 },
+    },
+    "ingredient_name" => { 'data_type' => "VARCHAR",  'default_value' => "", 'is_nullable' => 0, 'size' => 100 },
+    "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_id");
+__PACKAGE__->add_unique_constraint( "ingredient_name", ["ingredient_name"] );
+
+# __PACKAGE__->might_have( 'user' => 'CookBook::Db::Users', { 'foreign.user_id' => 'self.user_id' } );
+
+#----------------------------------------------------------------------------------------
+
+1;
+
+#----------------------------------------------------------------------------------------
+
+__END__
+
diff --git a/lib/CookBook/Db/RecipeIngredients.pm b/lib/CookBook/Db/RecipeIngredients.pm
new file mode 100644 (file)
index 0000000..abc025f
--- /dev/null
@@ -0,0 +1,70 @@
+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__
+
index 1e8f81f271be0e429070b47ce6315872583f9ed5..24240b6543a669168476897bfd08ee672675272c 100755 (executable)
@@ -34,8 +34,11 @@ my @target_tables = qw(
     session_log
     authors
     yield_types
+    ingredients
+    ingredient_groups
     recipes
     recipe_authors
+    recipe_ingredients
 );
 
 my $admin_data = {
@@ -92,6 +95,7 @@ my ( $target_dbh, $source_dbh );
 
 my %map_recipe_id;
 my %map_author_id;
+my %map_ingredient_id;
 
 my $port;
 
@@ -364,13 +368,16 @@ 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,
+        '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";
@@ -463,6 +470,24 @@ sub import_data {
     }
     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 ($@) {
@@ -480,6 +505,17 @@ sub import_data {
         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;
 
@@ -543,6 +579,150 @@ sub import_author_table {
 
 #-------------------------------------------------------------
 
+=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
@@ -777,6 +957,7 @@ END_SQL
 
         if ( $old_rid != $row->{'recipe_id'} ) {
             $i = 1;
+            $old_rid = $row->{'recipe_id'};
         }
         else {
             $i++;
@@ -796,6 +977,106 @@ END_SQL
 
 #-------------------------------------------------------------
 
+=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' ...