#!/usr/bin/perl -w
-# vim:filetype=perl
-
#
# Note that the bulk of the testing for the mysql producer is in
# 08postgres-to-mysql.t. This test is for additional stuff that can't be tested
use strict;
use Test::More;
use Test::Exception;
-#use Test::SQL::Translator qw(maybe_plan);
use Data::Dumper;
use FindBin qw/$Bin/;
-# Testing 1,2,3,4...
-#=============================================================================
-
-#BEGIN {
-# maybe_plan(40,
-# 'YAML',
-# 'SQL::Translator::Producer::MySQL',
-# 'Test::Differences',
-# )
-#}
use Test::Differences;
use SQL::Translator;
-# Main test.
+my $sqlt;
+
{
my $yaml_in = <<EOSCHEMA;
---
my $mysql_out = join(";\n\n", @stmts_no_drop) . ";\n\n";
- my $sqlt;
$sqlt = SQL::Translator->new(
show_warnings => 1,
no_comments => 1,
eq_or_diff $out, $mysql_out, "Scalar output looks right with quoting";
my @out = $sqlt->translate(data => $yaml_in)
- or die "Translat eerror:".$sqlt->error;
+ or die "Translate error:".$sqlt->error;
is_deeply \@out, \@stmts_no_drop, "Array output looks right with quoting";
-
@{$sqlt}{qw/quote_table_names quote_field_names/} = (0,0);
$out = $sqlt->translate(data => $yaml_in)
or die "Translate error:".$sqlt->error;
@{$sqlt}{qw/add_drop_table quote_field_names quote_table_names/} = (1,1,1);
@out = $sqlt->translate(data => $yaml_in)
- or die "Translat eerror:".$sqlt->error;
+ or die "Translate error:".$sqlt->error;
$out = $sqlt->translate(data => $yaml_in)
- or die "Translat eerror:".$sqlt->error;
+ or die "Translate error:".$sqlt->error;
eq_or_diff $out, join(";\n\n", @stmts) . ";\n\n", "Output looks right with DROP TABLEs";
is_deeply \@out, \@stmts, "Array output looks right with DROP TABLEs";
}
+my $producer = $sqlt->_producer;
+my $parser = $sqlt->_parser;
+
###############################################################################
# New alter/add subs
-my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
+my $table = SQL::Translator::Object::Table->new( name => 'mytable');
-my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
+my $field1 = SQL::Translator::Object::Column->new( name => 'myfield',
table => $table,
data_type => 'VARCHAR',
size => 10,
is_foreign_key => 0,
is_unique => 0 );
-my $field1_sql = SQL::Translator::Producer::MySQL::create_field($field1);
+my $field1_sql = $producer->create_field($field1);
is($field1_sql, 'myfield VARCHAR(10)', 'Create field works');
-my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
+my $field2 = SQL::Translator::Object::Column->new( name => 'myfield',
table => $table,
data_type => 'VARCHAR',
size => 25,
is_foreign_key => 0,
is_unique => 0 );
-my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1,
+my $alter_field = $producer->alter_field($field1,
$field2);
is($alter_field, 'ALTER TABLE mytable CHANGE COLUMN myfield myfield VARCHAR(25) NOT NULL', 'Alter field works');
-my $add_field = SQL::Translator::Producer::MySQL::add_field($field1);
+my $add_field = $producer->add_field($field1);
is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10)', 'Add field works');
-my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2);
+my $drop_field = $producer->drop_field($field2);
is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
-my $field3 = SQL::Translator::Schema::Field->new( name => 'myfield',
+my $field3 = SQL::Translator::Object::Column->new( name => 'myfield',
table => $table,
data_type => 'boolean',
is_nullable => 0,
is_foreign_key => 0,
is_unique => 0 );
-my $field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 4.1 });
+my $field3_sql = $producer->create_field($field3, { mysql_version => 4.1 });
is($field3_sql, 'myfield boolean NOT NULL', 'For Mysql >= 4, use boolean type');
-$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3, { mysql_version => 3.22 });
+$field3_sql = $producer->create_field($field3, { mysql_version => 3.22 });
is($field3_sql, "myfield enum('0','1') NOT NULL", 'For Mysql < 4, use enum for boolean type');
-$field3_sql = SQL::Translator::Producer::MySQL::create_field($field3,);
+$field3_sql = $producer->create_field($field3,);
is($field3_sql, "myfield enum('0','1') NOT NULL", 'When no version specified, use enum for boolean type');
my $number_sizes = {
};
for my $size (keys %$number_sizes) {
my $expected = $number_sizes->{$size};
- my $number_field = SQL::Translator::Schema::Field->new(
+ my $number_field = SQL::Translator::Object::Column->new(
name => "numberfield_$expected",
table => $table,
data_type => 'number',
);
is(
- SQL::Translator::Producer::MySQL::create_field($number_field),
+ $producer->create_field($number_field),
"numberfield_$expected $expected($size)",
"Use $expected for NUMBER types of size $size"
);
my $varchars;
for my $size (qw/255 256 65535 65536/) {
- $varchars->{$size} = SQL::Translator::Schema::Field->new(
+ $varchars->{$size} = SQL::Translator::Object::Column->new(
name => "vch_$size",
table => $table,
data_type => 'varchar',
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.000003 }),
+ $producer->create_field($varchars->{255}, { mysql_version => 5.000003 }),
'vch_255 varchar(255)',
'VARCHAR(255) is not substituted with TEXT for Mysql >= 5.0.3'
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{255}, { mysql_version => 5.0 }),
+ $producer->create_field($varchars->{255}, { mysql_version => 5.0 }),
'vch_255 varchar(255)',
'VARCHAR(255) is not substituted with TEXT for Mysql < 5.0.3'
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{255}),
+ $producer->create_field($varchars->{255}),
'vch_255 varchar(255)',
'VARCHAR(255) is not substituted with TEXT when no version specified',
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.000003 }),
+ $producer->create_field($varchars->{256}, { mysql_version => 5.000003 }),
'vch_256 varchar(256)',
'VARCHAR(256) is not substituted with TEXT for Mysql >= 5.0.3'
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{256}, { mysql_version => 5.0 }),
+ $producer->create_field($varchars->{256}, { mysql_version => 5.0 }),
'vch_256 text',
'VARCHAR(256) is substituted with TEXT for Mysql < 5.0.3'
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{256}),
+ $producer->create_field($varchars->{256}),
'vch_256 text',
'VARCHAR(256) is substituted with TEXT when no version specified',
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.000003 }),
+ $producer->create_field($varchars->{65535}, { mysql_version => 5.000003 }),
'vch_65535 varchar(65535)',
'VARCHAR(65535) is not substituted with TEXT for Mysql >= 5.0.3'
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{65535}, { mysql_version => 5.0 }),
+ $producer->create_field($varchars->{65535}, { mysql_version => 5.0 }),
'vch_65535 text',
'VARCHAR(65535) is substituted with TEXT for Mysql < 5.0.3'
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{65535}),
+ $producer->create_field($varchars->{65535}),
'vch_65535 text',
'VARCHAR(65535) is substituted with TEXT when no version specified',
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.000003 }),
+ $producer->create_field($varchars->{65536}, { mysql_version => 5.000003 }),
'vch_65536 text',
'VARCHAR(65536) is substituted with TEXT for Mysql >= 5.0.3'
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{65536}, { mysql_version => 5.0 }),
+ $producer->create_field($varchars->{65536}, { mysql_version => 5.0 }),
'vch_65536 text',
'VARCHAR(65536) is substituted with TEXT for Mysql < 5.0.3'
);
is (
- SQL::Translator::Producer::MySQL::create_field($varchars->{65536}),
+ $producer->create_field($varchars->{65536}),
'vch_65536 text',
'VARCHAR(65536) is substituted with TEXT when no version specified',
);
{
- my $view1 = SQL::Translator::Schema::View->new( name => 'view_foo',
+ my $view1 = SQL::Translator::Object::View->new( name => 'view_foo',
fields => [qw/id name/],
sql => 'SELECT id, name FROM thing',
extra => {
mysql_security => 'DEFINER',
});
my $create_opts = { add_replace_view => 1, no_comments => 1 };
- my $view1_sql1 = SQL::Translator::Producer::MySQL::create_view($view1, $create_opts);
+ my $view1_sql1 = $producer->create_view($view1, $create_opts);
my $view_sql_replace = "CREATE OR REPLACE
ALGORITHM = MERGE
is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
- my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo',
+ my $view2 = SQL::Translator::Object::View->new( name => 'view_foo',
fields => [qw/id name/],
sql => 'SELECT id, name FROM thing',);
my $create2_opts = { add_replace_view => 0, no_comments => 1 };
- my $view1_sql2 = SQL::Translator::Producer::MySQL::create_view($view2, $create2_opts);
+ my $view1_sql2 = $producer->create_view($view2, $create2_opts);
my $view_sql_noreplace = "CREATE
VIEW view_foo ( id, name ) AS (
SELECT id, name FROM thing
# certain types do not support a size, see also:
# http://dev.mysql.com/doc/refman/5.1/de/create-table.html
for my $type (qw/date time timestamp datetime year/) {
- my $field = SQL::Translator::Schema::Field->new(
+ my $field = SQL::Translator::Object::Column->new(
name => "my$type",
table => $table,
data_type => $type,
is_foreign_key => 0,
is_unique => 0
);
- my $sql = SQL::Translator::Producer::MySQL::create_field($field);
+ my $sql = $producer->create_field($field);
is($sql, "my$type $type", "Skip length param for type $type");
}
}