--- /dev/null
+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__
+
--- /dev/null
+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__
+
yield_types
unit_types
units
+ categories
ingredients
ingredient_groups
recipes
recipe_authors
+ recipe_categories
recipe_ingredients
);
'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,
#-------------------------------------------------------------
+=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
#-------------------------------------------------------------
+=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