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

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

index 0a12758bab6f2acff5d9136604d716b33cda9a1f..e5f5eebb21bca41c2f4302de9f4ccb206759bfdc 100644 (file)
@@ -25,9 +25,11 @@ use base qw/DBIx::Class::Schema/;
 __PACKAGE__->load_classes(
     qw/
         Authors
+        Categories
         Ingredients
         IngredientGroups
         RecipeAuthors
+        RecipeCategories
         RecipeIngredients
         Recipes
         SessionLog
diff --git a/lib/CookBook/Db/Categories.pm b/lib/CookBook/Db/Categories.pm
new file mode 100644 (file)
index 0000000..8791d1f
--- /dev/null
@@ -0,0 +1,58 @@
+package CookBook::Db::Categories;
+
+# $Id$
+# $URL$
+
+=head1 NAME
+
+CookBook::Db::Categories
+
+=head1 DESCRIPTION
+
+Module for abstract database access to the table 'categories'
+
+=cut
+
+#---------------------------------------------------------------------------
+
+use strict;
+use warnings;
+
+use CookBook::Common;
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(
+    qw/
+        PK::Auto
+        Core
+        /
+);
+
+__PACKAGE__->table('categories');
+
+__PACKAGE__->add_columns(
+    "category_id" => {
+        'data_type'         => "INT",
+        'default_value'     => undef,
+        'is_nullable'       => 0,
+        'size'              => 10,
+        'is_auto_increment' => 1,
+        'extras'            => { 'unsigned' => 1 },
+    },
+    "category_name" => { 'data_type' => "VARCHAR",  'default_value' => "",    'is_nullable' => 0, 'size' => 100 },
+    "parent_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("category_id");
+__PACKAGE__->add_unique_constraint( "category_name", ["category_name"] );
+
+#----------------------------------------------------------------------------------------
+
+1;
+
+#----------------------------------------------------------------------------------------
+
+__END__
+
diff --git a/lib/CookBook/Db/RecipeCategories.pm b/lib/CookBook/Db/RecipeCategories.pm
new file mode 100644 (file)
index 0000000..939be71
--- /dev/null
@@ -0,0 +1,59 @@
+package CookBook::Db::RecipeCategories;
+
+# $Id$
+# $URL$
+
+=head1 NAME
+
+CookBook::Db::RecipeCategories
+
+=head1 DESCRIPTION
+
+Module for abstract database access to the table 'recipe_categories'
+
+=cut
+
+#---------------------------------------------------------------------------
+
+use strict;
+use warnings;
+
+use CookBook::Common;
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(
+    qw/
+        PK::Auto
+        Core
+        /
+);
+
+__PACKAGE__->table('recipe_categories');
+
+__PACKAGE__->add_columns(
+    "recipe_category_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 } },
+    "category_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 },
+);
+
+__PACKAGE__->set_primary_key("recipe_category_id");
+
+__PACKAGE__->belongs_to( 'recipe'   => 'CookBook::Db::Recipes',    'recipe_id', );
+__PACKAGE__->belongs_to( 'category' => 'CookBook::Db::Categories', 'category_id', );
+
+#----------------------------------------------------------------------------------------
+
+1;
+
+#----------------------------------------------------------------------------------------
+
+__END__
+
index 6b1e653e7ceaa9b251220efe0142d941974a0f1f..921e6e0bdaf7f157fd0eff58d0c44117c991b3e9 100644 (file)
@@ -9,7 +9,7 @@ CookBook::Db::UnitTypes
 
 =head1 DESCRIPTION
 
-Module for abstract database access to the table 'yield_types'
+Module for abstract database access to the table 'unit_types'
 
 =cut
 
index 92a83f31611edca79374502afcf20d31bb020777..06db69b0c70b7e735a8f7474df0f030c2cdef896 100755 (executable)
@@ -36,10 +36,12 @@ my @target_tables = qw(
     yield_types
     unit_types
     units
+    categories
     ingredients
     ingredient_groups
     recipes
     recipe_authors
+    recipe_categories
     recipe_ingredients
 );
 
@@ -56,21 +58,27 @@ my %create_method = (
     'recipe_ingredients' => \&create_recipe_ingredients_table,
     'unit_types'         => \&create_unit_types_table,
     'units'              => \&create_units_table,
+    'categories'         => \&create_categories_table,
+    'recipe_categories'  => \&create_recipe_categories_table,
 );
 
 my @import_tables = qw(
     authors
     yield_types
     units
+    categories
     ingredients
     ingredient_groups
     recipes
     recipe_authors
+    recipe_categories
     recipe_ingredients
 );
 
 my %import_method = (
     'authors'            => \&import_author_table,
+    'categories'         => \&import_categories_table,
+    'recipe_categories'  => \&import_recipe_categories_table,
     'yield_types'        => \&import_yield_types_table,
     'units'              => \&import_units_table,
     'ingredients'        => \&import_ingredients_table,
@@ -557,6 +565,82 @@ sub import_author_table {
 
 #-------------------------------------------------------------
 
+=head2 create_categories_table( )
+
+=cut
+
+sub create_categories_table {
+
+    my $sql = <<END_SQL;
+CREATE TABLE `categories` (
+  `category_id`    int(10) unsigned NOT NULL auto_increment,
+  `category_name`  varchar(100)     NOT NULL COMMENT 'Name der Kategorie',
+  `parent_id`      int(10) unsigned          COMMENT 'Id der übergeordneten Kategorie',
+  `date_created`   datetime         NOT NULL,
+  `date_changed`   datetime         NOT NULL,
+  PRIMARY KEY                 (`category_id`),
+  UNIQUE KEY  `category_name` (`category_name`),
+  KEY         `parent_id`     (`parent_id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Kategorien'
+END_SQL
+
+    return undef unless $target_dbh->do($sql);
+    return 1;
+
+} ## end sub create_ingredients_table
+
+#-------------------------------------------------------------
+
+=head2 import_categories_table( )
+
+=cut
+
+sub import_categories_table {
+
+    my $sql = <<END_SQL;
+SELECT `id`, `name`, `parent_id`
+  FROM `categories`
+ ORDER BY `name`
+END_SQL
+
+    my ( $source_sth, $target_sth, $cat, $qparams );
+
+    unless ( $source_sth = $source_dbh->prepare($sql) ) {
+        return undef;
+    }
+
+    return undef unless $source_sth->execute();
+
+    $sql = <<END_SQL;
+INSERT INTO `categories` ( `category_id`, `category_name`, `parent_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 ( $cat = $source_sth->fetchrow_hashref() ) {
+
+        $qparams = [];
+        push @$qparams, $cat->{'id'};
+        push @$qparams, $cat->{'name'};
+        push @$qparams, ( $cat->{'parent_id'} and $cat->{'parent_id'} > 0 ) ? $cat->{'parent_id'} : undef;
+
+        unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+            $source_sth->finish();
+            return undef;
+        }
+
+    } ## end while ( $ingr = $source_sth->fetchrow_hashref...
+
+    return 1;
+
+} ## end sub import_ingredients_table
+
+#-------------------------------------------------------------
+
 =head2 create_ingredients_table( )
 
 =cut
@@ -955,6 +1039,79 @@ END_SQL
 
 #-------------------------------------------------------------
 
+=head2 create_recipe_categories_table( )
+
+=cut
+
+sub create_recipe_categories_table {
+
+    my $sql = <<END_SQL;
+CREATE TABLE `recipe_categories` (
+  `recipe_category_id` int(10) unsigned NOT NULL auto_increment,
+  `recipe_id`          int(10) unsigned NOT NULL COMMENT 'Verknüpfung zum Rezept',
+  `category_id`        int(10) unsigned NOT NULL COMMENT 'Verknüpfung zur Kategorie',
+  `date_created`     datetime         NOT NULL,
+  PRIMARY KEY               (`recipe_category_id`),
+  KEY         `recipe_id`   (`recipe_id`),
+  KEY         `category_id` (`category_id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Kategorien eines Rezepts'
+END_SQL
+
+    return undef unless $target_dbh->do($sql);
+    return 1;
+
+} ## end sub create_recipe_authors_table
+
+#-------------------------------------------------------------
+
+=head2 import_recipe_categories_table( )
+
+=cut
+
+sub import_recipe_categories_table {
+
+    my $sql = <<END_SQL;
+SELECT `recipe_id`, `category_id`
+  FROM `category_list`
+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 `recipe_categories` ( `recipe_id`, `category_id`, `date_created` )
+    VALUES ( ?, ?, now() )
+END_SQL
+    $target_sth = $target_dbh->prepare($sql);
+    unless ($target_sth) {
+        $source_sth->finish();
+        return undef;
+    }
+
+    while ( $row = $source_sth->fetchrow_hashref() ) {
+
+        next unless $row->{'recipe_id'} and $row->{'recipe_id'} > 0;
+        next unless $row->{'category_id'} and $row->{'category_id'} > 0;
+
+        $qparams = [ $map_recipe_id{ $row->{'recipe_id'} }, $row->{'category_id'}, ];
+
+        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_recipe_ingredients_table( )
 
 =cut