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

sbin/initial_import.pl

index f4f00b01b97b54999c9a13677e622c001bd747fe..5655e7bbbe0a69eb723d0e4b172a1ca0f1dd1b4c 100755 (executable)
@@ -36,6 +36,7 @@ my @target_tables = qw(
     yield_types
     unit_types
     units
+    unit_conversations
     properties
     prep_methods
     categories
@@ -70,12 +71,14 @@ my %create_method = (
     'prep_methods'            => \&create_prep_methods_table,
     'ingredient_prep_methods' => \&create_ingredient_prep_methods_table,
     'ingredient_weights'      => \&create_ingredient_weights_table,
+    'unit_conversations'      => \&create_unit_conversations_table,
 );
 
 my @import_tables = qw(
     authors
     yield_types
     units
+    unit_conversations
     categories
     properties
     prep_methods
@@ -96,6 +99,7 @@ my %import_method = (
     'recipe_categories'       => \&import_recipe_categories_table,
     'yield_types'             => \&import_yield_types_table,
     'units'                   => \&import_units_table,
+    'unit_conversations'      => \&import_unit_conversations_table,
     'ingredients'             => \&import_ingredients_table,
     'ingredient_groups'       => \&import_ingredient_groups_table,
     'ingredient_properties'   => \&import_ingredient_properties_table,
@@ -581,6 +585,84 @@ sub import_author_table {
 
 #-------------------------------------------------------------
 
+=head2 create_unit_conversations_table( )
+
+=cut
+
+sub create_unit_conversations_table {
+
+    my $sql = <<END_SQL;
+CREATE TABLE `unit_conversations` (
+  `unit_conversation_id` int(10) unsigned NOT NULL auto_increment,
+  `unit_id_from`         int(10) unsigned NOT NULL,
+  `unit_id_to`           int(10) unsigned NOT NULL,
+  `ratio`                float   unsigned          default NULL COMMENT 'Umwandlungsfaktor',
+  `date_created`         datetime         NOT NULL,
+  `date_changed`         datetime         NOT NULL,
+  PRIMARY KEY             (`unit_conversation_id`),
+  KEY          `unit_ids` (`unit_id_from`,`unit_id_to`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Umrechnungen zwischen den Einheiten'
+END_SQL
+
+    return undef unless $target_dbh->do($sql);
+    return 1;
+
+} ## end sub create_categories_table
+
+#-------------------------------------------------------------
+
+=head2 import_unit_conversations_table( )
+
+=cut
+
+sub import_unit_conversations_table {
+
+    my $sql = <<END_SQL;
+SELECT `unit1_id`, `unit2_id`, `ratio`
+  FROM `units_conversion`
+ ORDER BY `unit1_id`
+END_SQL
+
+    my ( $source_sth, $target_sth, $conv, $qparams );
+
+    unless ( $source_sth = $source_dbh->prepare($sql) ) {
+        return undef;
+    }
+
+    return undef unless $source_sth->execute();
+
+    $sql = <<END_SQL;
+INSERT INTO `unit_conversations` ( `unit_id_from`, `unit_id_to`, `ratio`, `date_created`, `date_changed` )
+    VALUES ( ?, ?, ?, now(), now() )
+END_SQL
+    $target_sth = $target_dbh->prepare($sql);
+    unless ($target_sth) {
+        $source_sth->finish();
+        return undef;
+    }
+
+    while ( $conv = $source_sth->fetchrow_hashref() ) {
+
+        next unless $map_unit_id{ $conv->{'unit1_id'} } and $map_unit_id{ $conv->{'unit2_id'} };
+
+        $qparams = [];
+        push @$qparams, $map_unit_id{ $conv->{'unit1_id'} };
+        push @$qparams, $map_unit_id{ $conv->{'unit2_id'} };
+        push @$qparams, $conv->{'ratio'};
+
+        unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+            $source_sth->finish();
+            return undef;
+        }
+
+    } ## end while ( $cat = $source_sth->fetchrow_hashref(...
+
+    return 1;
+
+} ## end sub import_categories_table
+
+#-------------------------------------------------------------
+
 =head2 create_categories_table( )
 
 =cut