]> Frank Brehm's Git Trees - cookbook.git/commitdiff
Maßeinheiten dazu
authorFrank Brehm <frank@brehm-online.com>
Fri, 10 Aug 2007 09:09:55 +0000 (09:09 +0000)
committerFrank Brehm <frank@brehm-online.com>
Fri, 10 Aug 2007 09:09:55 +0000 (09:09 +0000)
git-svn-id: http://svn.brehm-online.com/svn/cookbook/trunk@18 191103c4-1d37-0410-b3e5-d8c2315c0aac

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

index f2440dd11a482aa337b41220ecd270644a04033c..0a12758bab6f2acff5d9136604d716b33cda9a1f 100644 (file)
@@ -27,11 +27,13 @@ __PACKAGE__->load_classes(
         Authors
         Ingredients
         IngredientGroups
-        Recipes
         RecipeAuthors
         RecipeIngredients
-        Session
+        Recipes
         SessionLog
+        Session
+        Units
+        UnitTypes
         Users
         YieldTypes
         /
index abc025f95e5c46685630b5a0b58fef423de33122..4a26dfc5d390aa799eaace70a4db03f16c491da0 100644 (file)
@@ -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 (file)
index 0000000..fff80a4
--- /dev/null
@@ -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 (file)
index 0000000..aba7696
--- /dev/null
@@ -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__
+
index 24240b6543a669168476897bfd08ee672675272c..4c8cc65d3e232013966c2038337f3a1b12126d76 100755 (executable)
@@ -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 = <<END_SQL;
 SELECT `id`, `title`, `yield_amount`, `yield_amount_offset`, `yield_type_id`, `instructions`,
@@ -891,7 +869,7 @@ END_SQL
 
     return 1;
 
-} ## end sub import_receipes_table
+} ## end sub import_recipes_table
 
 #-------------------------------------------------------------
 
@@ -1058,7 +1036,7 @@ END_SQL
         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, ( $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 = <<END_SQL;
+CREATE TABLE IF NOT EXISTS `units` (
+    `unit_id`            int(10) unsigned NOT NULL auto_increment,
+    `unit_name`          varchar(50)      NOT NULL             COMMENT 'Name der Maßeinheit',
+    `unit_name_abbrev`   varchar(20)                           COMMENT 'Abkürzung der Maßeinheit',
+    `unit_plural`        varchar(50)      NOT NULL             COMMENT 'Name der Maßeinheit in der Mehrzahl',
+    `unit_plural_abbrev` varchar(20)                           COMMENT 'Abkürzung der Maßeinheit in der Mehrzahl',
+    `unit_type_id`       int(10) unsigned NOT NULL default '0' COMMENT 'Verknüpfung zu den Maßeinheittypen',
+    `date_created`       datetime         NOT NULL,
+    `date_changed`       datetime         NOT NULL,
+    PRIMARY KEY                (`unit_id`),
+    UNIQUE KEY  `unit_name`    (`unit_name`),
+    KEY         `unit_type_id` (`unit_type_id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Maßeinheiten'
+END_SQL
+
+    return undef unless $target_dbh->do($sql);
+
+    return 1;
+
+}
+
+#-------------------------------------------------------------
+
+=head2 import_units_table( )
+
+=cut
+
+sub import_units_table {
+
+    my $sql = <<END_SQL;
+SELECT `id`, `name`, `name_abbrev`, `plural`, `plural_abbrev`, `type`
+  FROM `units`
+ ORDER BY `name`
+END_SQL
+
+    my ( $source_sth, $target_sth, $row, $qparams );
+
+    unless ( $source_sth = $source_dbh->prepare($sql) ) {
+        return undef;
+    }
+
+    return undef unless $source_sth->execute();
+
+    $sql = <<END_SQL;
+INSERT INTO `units` (
+        `unit_name`, `unit_name_abbrev`, `unit_plural`, `unit_plural_abbrev`, `unit_type_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 ( $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 = <<END_SQL;
+CREATE TABLE IF NOT EXISTS `unit_types` (
+  `unit_type_id`   int(10) unsigned NOT NULL auto_increment,
+  `unit_type_name` varchar(30)      NOT NULL COMMENT 'Name des Maßeinheittyps',
+  `date_created`    datetime         NOT NULL,
+  PRIMARY KEY                   (`unit_type_id`),
+  UNIQUE KEY   `unit_type_name` (`unit_type_name`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Maßeinheittypen'
+END_SQL
+
+    return undef unless $target_dbh->do($sql);
+
+    my @Units = qw(
+        Sonstiges
+        Masse
+        Volumen
+    );
+
+    my $i = 0;
+    my $qparams;
+
+    $sql = <<END_SQL;
+INSERT INTO `unit_types` (
+    `unit_type_id`, `unit_type_name`, `date_created`
+  ) VALUES (
+    ?, ?, now()
+  )
+END_SQL
+
+    for my $unit ( @Units ) {
+
+        $i++;
+        $qparams = [ $i, $unit ];
+
+        unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+            return undef;
+        }
+
+    }
+
+    return 1;
+
+} ## end sub create_yield_types_table
+
+#-------------------------------------------------------------
+
 =head2 create_user_table( )
 
 Erstellt die Tabelle 'users' ...