--- /dev/null
+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__
+
session_log
authors
yield_types
+ unit_types
+ units
ingredients
ingredient_groups
recipes
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',
my %map_recipe_id;
my %map_author_id;
+my %map_unit_id;
my %map_ingredient_id;
my $port;
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) {
#-------------------------------------------------------------
+=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' ...
#-------------------------------------------------------------
-=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
#-------------------------------------------------------------
-=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`,
return 1;
-} ## end sub import_receipes_table
+} ## end sub import_recipes_table
#-------------------------------------------------------------
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 );
#-------------------------------------------------------------
+=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' ...