session
session_log
authors
+ yield_types
recipes
+ recipe_authors
);
my $admin_data = {
my ( $target_dsn, $target_user, $target_passwd, $source_dsn, $source_user, $source_passwd );
my ( $target_dbh, $source_dbh );
+my %map_recipe_id;
+my %map_author_id;
+
my $port;
$target_dsn = 'dbi:mysql:host=%s;database=%s';
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,
+ '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,
);
print green_star() . " Erstelle Ziel-Tabellen ...\n";
}
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 " - recipes ... ";
eval { die "Tabelle 'recipes' wurde nicht importiert.\n" unless import_receipes_table(); };
if ($@) {
}
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 "\n";
return 1;
sub import_author_table {
my $sql = 'SELECT `id`, `name` FROM `authors` order by `name`';
- my ( $source_sth, $target_sth, $author, $row, $count, $qparams );
+ my ( $source_sth, $target_sth, $author, $row, $count, $qparams, $do_insert );
unless ( $source_sth = $source_dbh->prepare($sql) ) {
return undef;
$count = $row->{'count'};
$target_sth->finish();
$qparams = [ $author->{'id'}, $author->{'name'} ];
+ $do_insert = 0;
if ( $count ) {
$sql = 'UPDATE `authors` SET `old_author_id` = ? WHERE `author_name` = ?';
}
else {
$sql = 'INSERT INTO `authors` ( `old_author_id`, `author_name`, `date_created` ) VALUES ( ?, ?, now() )';
+ $do_insert = 1;
}
unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
$source_sth->finish();
return undef;
}
+ if ( $do_insert ) {
+ $map_author_id{$author->{'id'}} = $target_dbh->{'mysql_insertid'};
+ }
+ else {
+ $map_author_id{$author->{'id'}} = 1;
+ }
}
return 1;
sub import_receipes_table {
my $sql = <<END_SQL;
-SELECT `id`,
- `title`,
- `yield_amount`,
- `yield_amount_offset`,
- `yield_type_id`,
- `instructions`,
- `prep_time`,
- `ctime`,
- `mtime`
+SELECT `id`, `title`, `yield_amount`, `yield_amount_offset`, `yield_type_id`, `instructions`,
+ `prep_time`, `ctime`, `mtime`
FROM `recipes`
ORDER BY `ctime`
END_SQL
$source_sth->finish();
return undef;
}
+
+ $map_recipe_id{$recipe->{'id'}} = $target_dbh->{'mysql_insertid'};
+
}
return 1;
+}
+
+#-------------------------------------------------------------
+
+=head2 create_recipe_authors_table( )
+
+=cut
+
+sub create_recipe_authors_table {
+
+ my $sql = <<END_SQL;
+CREATE TABLE `recipe_authors` (
+ `recipe_author_id` int(10) unsigned NOT NULL auto_increment,
+ `recipe_id` int(10) unsigned NOT NULL COMMENT 'Verknüpfung zum Rezept',
+ `author_id` int(10) unsigned NOT NULL COMMENT 'Verknüpfung zum Autor',
+ `order_index` int(10) unsigned NOT NULL COMMENT 'Sortierreihenfolge',
+ `date_created` datetime NOT NULL,
+ PRIMARY KEY (`recipe_author_id`),
+ KEY `recipe_id` (`recipe_id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Autoren eines Rezepts'
+END_SQL
+ return undef unless $target_dbh->do($sql);
+ return 1;
}
+
+#-------------------------------------------------------------
+
+=head2 import_recipe_authors_table( )
+
+=cut
+
+sub import_recipe_authors_table {
+
+ my $sql = <<END_SQL;
+SELECT `recipe_id`, `author_id`
+ FROM `author_list`
+ ORDER BY `recipe_id`, `author_id`
+END_SQL
+
+ my ( $source_sth, $target_sth, $row, $qparams, $i, $old_rid );
+
+ unless ( $source_sth = $source_dbh->prepare($sql) ) {
+ return undef;
+ }
+
+ return undef unless $source_sth->execute();
+
+ $sql = <<END_SQL;
+INSERT INTO `recipe_authors` ( `recipe_id`, `author_id`, `order_index`, `date_created` )
+ VALUES ( ?, ?, ?, now() )
+END_SQL
+ $target_sth = $target_dbh->prepare($sql);
+ unless ( $target_sth ) {
+ $source_sth->finish();
+ return undef;
+ }
+
+ $old_rid = 0;
+ $i = 0;
+
+ while ( $row = $source_sth->fetchrow_hashref() ) {
+
+ if ( $old_rid != $row->{'recipe_id'} ) {
+ $i = 1;
+ }
+ else {
+ $i++;
+ }
+
+ $qparams = [
+ $map_recipe_id{$row->{'recipe_id'}},
+ $map_author_id{$row->{'author_id'}},
+ $i,
+ ];
+
+ unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+ $source_sth->finish();
+ return undef;
+ }
+ }
+
+ return 1;
+
+}
+
#-------------------------------------------------------------
=head2 create_user_table( )
#-------------------------------------------------------------
+=head2 create_yield_types_table( )
+
+=cut
+
+sub create_yield_types_table {
+
+ my $sql = <<END_SQL;
+CREATE TABLE IF NOT EXISTS `yield_types` (
+ `yield_type_id` int(10) unsigned NOT NULL auto_increment,
+ `yield_type_name` varchar(30) NOT NULL COMMENT 'Name des Ergebnismengentyps',
+ `date_created` datetime NOT NULL,
+ PRIMARY KEY (`yield_type_id`),
+ UNIQUE KEY `yield_type_name` (`yield_type_name`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Ergebnismengentypen'
+END_SQL
+
+ return undef unless $target_dbh->do($sql);
+ return 1;
+
+}
+
+#-------------------------------------------------------------
+
+=head2 import_yield_types_table( )
+
+=cut
+
+sub import_yield_types_table {
+
+ my $sql = <<END_SQL;
+SELECT `id`, `name`
+ FROM `yield_types`
+ ORDER BY `name`
+END_SQL
+
+ my ( $source_sth, $target_sth, $type, $qparams );
+
+ unless ( $source_sth = $source_dbh->prepare($sql) ) {
+ return undef;
+ }
+
+ return undef unless $source_sth->execute();
+
+ $sql = <<END_SQL;
+INSERT INTO `yield_types` ( `yield_type_id`, `yield_type_name`, `date_created` )
+ VALUES ( ?, ?, now() )
+END_SQL
+ $target_sth = $target_dbh->prepare($sql);
+ unless ( $target_sth ) {
+ $source_sth->finish();
+ return undef;
+ }
+
+ while ( $type = $source_sth->fetchrow_hashref() ) {
+
+ $qparams = [
+ $type->{'id'},
+ $type->{'name'},
+ ];
+
+ unless ( $target_dbh->do( $sql, {}, @$qparams ) ) {
+ $source_sth->finish();
+ return undef;
+ }
+ }
+
+ return 1;
+
+}
+
+#-------------------------------------------------------------
+
__END__