]> Frank Brehm's Git Trees - cookbook.git/commitdiff
Tabelle 'ingredient_weights' importiert
authorFrank Brehm <frank@brehm-online.com>
Sun, 7 Oct 2007 12:58:40 +0000 (12:58 +0000)
committerFrank Brehm <frank@brehm-online.com>
Sun, 7 Oct 2007 12:58:40 +0000 (12:58 +0000)
git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@25 191103c4-1d37-0410-b3e5-d8c2315c0aac

sbin/initial_import.pl

index 7c94a87259ec52ee2bd503209bb4bdea29e61bfd..f4f00b01b97b54999c9a13677e622c001bd747fe 100755 (executable)
@@ -47,6 +47,7 @@ my @target_tables = qw(
     recipe_categories
     recipe_ingredients
     ingredient_prep_methods
+    ingredient_weights
 );
 
 my %create_method = (
@@ -68,6 +69,7 @@ my %create_method = (
     'ingredient_properties'   => \&create_ingredient_properties_table,
     'prep_methods'            => \&create_prep_methods_table,
     'ingredient_prep_methods' => \&create_ingredient_prep_methods_table,
+    'ingredient_weights'      => \&create_ingredient_weights_table,
 );
 
 my @import_tables = qw(
@@ -85,6 +87,7 @@ my @import_tables = qw(
     recipe_categories
     recipe_ingredients
     ingredient_prep_methods
+    ingredient_weights
 );
 
 my %import_method = (
@@ -102,6 +105,7 @@ my %import_method = (
     'properties'              => \&import_properties_table,
     'prep_methods'            => \&import_prep_methods_table,
     'ingredient_prep_methods' => \&import_ingredient_prep_methods_table,
+    'ingredient_weights'      => \&import_ingredient_weights_table,
 );
 
 my $admin_data = {
@@ -161,6 +165,8 @@ my %map_author_id;
 my %map_unit_id;
 my %map_ingredient_id;
 
+my %valid_prep_methods;
+
 my $port;
 
 $target_dsn = 'dbi:mysql:host=%s;database=%s';
@@ -1037,6 +1043,7 @@ END_SQL
     while ( $type = $source_sth->fetchrow_hashref() ) {
 
         $qparams = [ $type->{'id'}, $type->{'name'}, ];
+        $valid_prep_methods{ $type->{'id'} } = 1;
 
         unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
             $source_sth->finish();
@@ -1757,6 +1764,92 @@ END_SQL
 
 #-------------------------------------------------------------
 
+=head2 create_ingredient_weights_table( )
+
+=cut
+
+sub create_ingredient_weights_table {
+
+    my $sql = <<END_SQL;
+CREATE TABLE IF NOT EXISTS `ingredient_weights` (
+  `ingredient_weight_id` int(10)  unsigned NOT NULL auto_increment,
+  `ingredient_id`        int(10)  unsigned NOT NULL,
+  `amount`               float    unsigned default NULL,
+  `unit_id`              int(10)  unsigned default NULL,
+  `weight`               float    unsigned default NULL,
+  `weight_unit_id`       int(10)  unsigned default NULL,
+  `prep_method_id`       int(10)  unsigned default NULL,
+  `date_created`         datetime          NOT NULL,
+  `date_changed`         datetime          NOT NULL,
+  PRIMARY KEY                  (`ingredient_weight_id`),
+  KEY         `ingredient_id`  (`ingredient_id`),
+  KEY         `unit_id`        (`unit_id`),
+  KEY         `weight_unit_id` (`weight_unit_id`),
+  KEY         `prep_method_id` (`prep_method_id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Spezielle Gewichtsumrechnungen einzelner Zutaten';
+END_SQL
+
+    return undef unless $target_dbh->do($sql);
+    return 1;
+
+} ## end sub create_yield_types_table
+
+#-------------------------------------------------------------
+
+=head2 import_ingredient_weights_table( )
+
+=cut
+
+sub import_ingredient_weights_table {
+
+    my $sql = <<END_SQL;
+SELECT `ingredient_id`, `amount`, `unit_id`, `weight`, `weight_unit_id`, `prep_method_id`
+  FROM `ingredient_weights`
+ ORDER BY `ingredient_id`
+END_SQL
+
+    my ( $source_sth, $target_sth, $weight, $qparams );
+
+    unless ( $source_sth = $source_dbh->prepare($sql) ) {
+        return undef;
+    }
+
+    return undef unless $source_sth->execute();
+
+    $sql = <<END_SQL;
+INSERT INTO `ingredient_weights` (
+   `ingredient_id`, `amount`, `unit_id`, `weight`, `weight_unit_id`, `prep_method_id`, `date_created`, `date_changed`
+ ) VALUES (
+   ?,               ?,        ?,         ?,        ?,                ?,                now(),          now() )
+END_SQL
+    $target_sth = $target_dbh->prepare($sql);
+    unless ($target_sth) {
+        $source_sth->finish();
+        return undef;
+    }
+
+    while ( $weight = $source_sth->fetchrow_hashref() ) {
+
+        $qparams = [];
+        push @$qparams, $map_ingredient_id{ $weight->{'ingredient_id'} };
+        push @$qparams, $weight->{'amount'};
+        push @$qparams, $map_unit_id{ $weight->{'unit_id'} };
+        push @$qparams, $weight->{'weight'};
+        push @$qparams, $map_unit_id{ $weight->{'weight_unit_id'} };
+        push @$qparams, ( $valid_prep_methods{ $weight->{'prep_method_id'} } ? $weight->{'prep_method_id'} : undef );
+
+        unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+            $source_sth->finish();
+            return undef;
+        }
+    }
+
+    return 1;
+
+} ## end sub import_yield_types_table
+
+#-------------------------------------------------------------
+
 =head2 create_yield_types_table( )
 
 =cut