Browse files

Merge branch 'people/frew/mega-refactor'

  • Loading branch information...
2 parents 0a2833d + 4b10273 commit 4df76cbb4c65cf3b393ed86b3eafd527d4cab122 @ribasushi ribasushi committed Feb 4, 2012
View
3 MANIFEST.SKIP
@@ -59,3 +59,6 @@ Build$
# junk
\.log$
\.tmp$
+
+# temporary
+\.sqlt_refactor_placeholder$
View
1 lib/SQL/Translator/Generator/DDL/.sqlt_refactor_placeholder
@@ -0,0 +1 @@
+This directory will hold the new style SQLT DDL producers
View
233 lib/SQL/Translator/Generator/DDL/SQLServer.pm
@@ -0,0 +1,233 @@
+package SQL::Translator::Generator::DDL::SQLServer;
+
+use Moo;
+use SQL::Translator::Generator::Utils;
+use SQL::Translator::Schema::Constants;
+
+with 'SQL::Translator::Generator::Role::Quote';
+with 'SQL::Translator::Generator::Role::DDL';
+
+sub quote_chars { [qw([ ])] }
+sub name_sep { q(.) }
+
+sub _build_numeric_types {
+ +{
+ int => 1,
+ }
+}
+
+sub _build_unquoted_defaults {
+ +{
+ NULL => 1,
+ }
+}
+
+sub _build_type_map {
+ +{
+ date => 'datetime',
+ 'time' => 'datetime',
+ }
+}
+
+sub _build_sizeless_types {
+ +{ map { $_ => 1 }
+ qw( tinyint smallint int integer bigint text bit image datetime ) }
+}
+
+sub field {
+ my ($self, $field) = @_;
+
+ return join ' ', $self->field_name($field), ($self->field_type($field)||die 'type is required'),
+ $self->field_autoinc($field),
+ $self->field_nullable($field),
+ $self->field_default($field),
+}
+
+sub field_autoinc { ( $_[1]->is_auto_increment ? 'IDENTITY' : () ) }
+
+sub primary_key_constraint {
+ 'CONSTRAINT ' .
+ $_[0]->quote($_[1]->name || $_[1]->table->name . '_pk') .
+ ' PRIMARY KEY (' .
+ join( ', ', map $_[0]->quote($_), $_[1]->fields ) .
+ ')'
+}
+
+sub index {
+ 'CREATE INDEX ' .
+ $_[0]->quote($_[1]->name || $_[1]->table->name . '_idx') .
+ ' ON ' . $_[0]->quote($_[1]->table->name) .
+ ' (' . join( ', ', map $_[0]->quote($_), $_[1]->fields ) . ');'
+}
+
+sub unique_constraint_single {
+ my ($self, $constraint) = @_;
+
+ 'CONSTRAINT ' .
+ $self->unique_constraint_name($constraint) .
+ ' UNIQUE (' . join( ', ', map $self->quote($_), $constraint->fields ) . ')'
+}
+
+sub unique_constraint_name {
+ my ($self, $constraint) = @_;
+ $self->quote($constraint->name || $constraint->table->name . '_uc' )
+}
+
+sub unique_constraint_multiple {
+ my ($self, $constraint) = @_;
+
+ 'CREATE UNIQUE NONCLUSTERED INDEX ' .
+ $self->unique_constraint_name($constraint) .
+ ' ON ' . $self->quote($constraint->table->name) . ' (' .
+ join( ', ', $constraint->fields ) . ')' .
+ ' WHERE ' . join( ' AND ',
+ map $self->quote($_->name) . ' IS NOT NULL',
+ grep { $_->is_nullable } $constraint->fields ) . ';'
+}
+
+sub foreign_key_constraint {
+ my ($self, $constraint) = @_;
+
+ my $on_delete = uc ($constraint->on_delete || '');
+ my $on_update = uc ($constraint->on_update || '');
+
+ # The default implicit constraint action in MSSQL is RESTRICT
+ # but you can not specify it explicitly. Go figure :)
+ for (map uc $_ || '', $on_delete, $on_update) {
+ undef $_ if $_ eq 'RESTRICT'
+ }
+
+ 'ALTER TABLE ' . $self->quote($constraint->table->name) .
+ ' ADD CONSTRAINT ' .
+ $self->quote($constraint->name || $constraint->table->name . '_fk') .
+ ' FOREIGN KEY' .
+ ' (' . join( ', ', map $self->quote($_), $constraint->fields ) . ') REFERENCES '.
+ $self->quote($constraint->reference_table) .
+ ' (' . join( ', ', map $self->quote($_), $constraint->reference_fields ) . ')'
+ . (
+ $on_delete && $on_delete ne "NO ACTION"
+ ? ' ON DELETE ' . $on_delete
+ : ''
+ ) . (
+ $on_update && $on_update ne "NO ACTION"
+ ? ' ON UPDATE ' . $on_update
+ : ''
+ ) . ';';
+}
+
+sub enum_constraint_name {
+ my ($self, $field_name) = @_;
+ $self->quote($field_name . '_chk' )
+}
+
+sub enum_constraint {
+ my ( $self, $field_name, $vals ) = @_;
+
+ return (
+ 'CONSTRAINT ' . $self->enum_constraint_name($field_name) .
+ ' CHECK (' . $self->quote($field_name) .
+ ' IN (' . join( ',', map qq('$_'), @$vals ) . '))'
+ )
+}
+
+sub constraints {
+ my ($self, $table) = @_;
+
+ (map $self->enum_constraint($_->name, { $_->extra }->{list} || []),
+ grep { 'enum' eq lc $_->data_type } $table->get_fields),
+
+ (map $self->primary_key_constraint($_),
+ grep { $_->type eq PRIMARY_KEY } $table->get_constraints),
+
+ (map $self->unique_constraint_single($_),
+ grep {
+ $_->type eq UNIQUE &&
+ !grep { $_->is_nullable } $_->fields
+ } $table->get_constraints),
+}
+
+sub table {
+ my ($self, $table) = @_;
+ join ( "\n", $self->table_comments($table), '' ) .
+ join ( "\n\n",
+ 'CREATE TABLE ' . $self->quote($table->name) . " (\n".
+ join( ",\n",
+ map { " $_" }
+ $self->fields($table),
+ $self->constraints($table),
+ ) .
+ "\n);",
+ $self->unique_constraints_multiple($table),
+ $self->indices($table),
+ )
+}
+
+sub unique_constraints_multiple {
+ my ($self, $table) = @_;
+ (map $self->unique_constraint_multiple($_),
+ grep {
+ $_->type eq UNIQUE &&
+ grep { $_->is_nullable } $_->fields
+ } $table->get_constraints)
+}
+
+sub drop_table {
+ my ($self, $table) = @_;
+ my $name = $table->name;
+ my $q_name = $self->quote($name);
+ "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U')" .
+ " DROP TABLE $q_name;"
+}
+
+sub remove_table_constraints {
+ my ($self, $table) = @_;
+ my $name = $table->name;
+ my $q_name = $self->quote($name);
+ "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U')" .
+ " ALTER TABLE $q_name NOCHECK CONSTRAINT all;"
+}
+
+sub drop_tables {
+ my ($self, $schema) = shift;
+
+ if ($self->add_drop_table) {
+ my @tables = sort { $b->order <=> $a->order } $schema->get_tables;
+ return join "\n", (
+ ( $self->add_comments ? (
+ '--',
+ '-- Turn off constraints',
+ '--',
+ '',
+ ) : () ),
+ (map $self->remove_table_constraints($_), @tables),
+ ( $self->add_comments ? (
+ '--',
+ '-- Drop tables',
+ '--',
+ '',
+ ) : () ),
+ (map $self->drop_table($_), @tables),
+ )
+ }
+ return '';
+}
+
+sub foreign_key_constraints {
+ my ($self, $schema) = @_;
+ ( map $self->foreign_key_constraint($_),
+ grep { $_->type eq FOREIGN_KEY }
+ map $_->get_constraints,
+ $schema->get_tables )
+}
+
+sub schema {
+ my ($self, $schema) = @_;
+
+ $self->header_comments .
+ $self->drop_tables($schema) .
+ join("\n\n", map $self->table($_), grep { $_->name } $schema->get_tables) .
+ "\n" . join "\n", $self->foreign_key_constraints($schema)
+}
+
+1;
+
View
60 lib/SQL/Translator/Generator/DDL/SQLite.pm
@@ -0,0 +1,60 @@
+package SQL::Translator::Generator::DDL::SQLite;
+
+use Moo;
+use SQL::Translator::Generator::Utils;
+
+with 'SQL::Translator::Generator::Role::Quote';
+with 'SQL::Translator::Generator::Role::DDL';
+
+sub quote_chars { [qw(" ")] }
+sub name_sep { q(.) }
+
+sub _build_type_map {
+ +{
+ date => 'datetime',
+ 'time' => 'datetime',
+ }
+}
+
+sub _build_sizeless_types { +{ text => 1 } }
+sub _build_numeric_types { +{ int => 1, tinyint => 1 } }
+
+sub _build_unquoted_defaults {
+ +{
+ NULL => 1,
+ 'now()' => 1,
+ CURRENT_TIMESTAMP => 1,
+ }
+}
+
+sub nullable { () }
+
+sub _ipk {
+ my ($self, $field) = @_;
+
+ my $pk = $field->table->primary_key;
+ my @pk_fields = $pk ? $pk->fields : ();
+
+ $field->is_primary_key && scalar @pk_fields == 1 &&
+ ( $field->data_type =~ /int(eger)?$/i
+ ||
+ ( $field->data_type =~ /^number?$/i && $field->size !~ /,/ ) )
+}
+
+sub field {
+ my ($self, $field) = @_;
+
+
+ return join ' ',
+ $self->field_comments($field),
+ $self->field_name($field),
+ ( $self->_ipk($field)
+ ? ( 'INTEGER PRIMARY KEY' )
+ : ( $self->field_type($field) )
+ ),
+ $self->field_nullable($field),
+ $self->field_default($field),
+}
+
+1;
+
View
1 lib/SQL/Translator/Generator/Role/.sqlt_refactor_placeholder
@@ -0,0 +1 @@
+This directory will hold the Roles for new style SQLT producers
View
99 lib/SQL/Translator/Generator/Role/DDL.pm
@@ -0,0 +1,99 @@
+package SQL::Translator::Generator::Role::DDL;
+
+use Moo::Role;
+use SQL::Translator::Utils qw(header_comment);
+
+requires '_build_type_map';
+requires '_build_numeric_types';
+requires '_build_unquoted_defaults';
+requires '_build_sizeless_types';
+requires 'quote';
+
+has type_map => (
+ is => 'lazy',
+);
+
+has numeric_types => (
+ is => 'lazy',
+);
+
+has sizeless_types => (
+ is => 'lazy',
+);
+
+has unquoted_defaults => (
+ is => 'lazy',
+);
+
+has add_comments => (
+ is => 'ro',
+);
+
+has add_drop_table => (
+ is => 'ro',
+);
+
+# would also be handy to have a required size set if there is such a thing
+
+sub field_name { $_[0]->quote($_[1]->name) }
+
+sub field_comments {
+ ( $_[1]->comments ? ('-- ' . $_[1]->comments . "\n ") : () )
+}
+
+sub table_comments {
+ my ($self, $table) = @_;
+ if ($self->add_comments) {
+ return (
+ "",
+ "--",
+ "-- Table: " . $self->quote($table->name) . "",
+ "--",
+ map "-- $_", $table->comments
+ )
+ } else {
+ return ()
+ }
+}
+
+sub field_nullable { ($_[1]->is_nullable ? $_[0]->nullable : 'NOT NULL' ) }
+
+sub field_default {
+ return () if !defined $_[1]->default_value;
+
+ my $val = $_[1]->default_value;
+ $val = "'$val'" unless $_[0]->numeric_types->{$_[1]->data_type};
+ return ( "DEFAULT $val" )
+}
+
+sub field_type {
+ my ($self, $field) = @_;
+
+ my $field_type = $field->data_type;
+ ($self->type_map->{$field_type} || $field_type).$self->field_type_size($field)
+}
+
+sub field_type_size {
+ my ($self, $field) = @_;
+
+ ($field->size && !$self->sizeless_types->{$field->data_type}
+ ? '(' . $field->size . ')'
+ : ''
+ )
+}
+
+sub fields {
+ my ($self, $table) = @_;
+ ( map $self->field($_), $table->get_fields )
+}
+
+sub indices {
+ my ($self, $table) = @_;
+ (map $self->index($_), $table->get_indices)
+}
+
+sub nullable { 'NULL' }
+
+sub header_comments { header_comment() . "\n" if $_[0]->add_comments }
+
+1;
View
27 lib/SQL/Translator/ProducerUtils.pm → lib/SQL/Translator/Generator/Role/Quote.pm
@@ -1,30 +1,9 @@
package # hide from pause
- SQL::Translator::ProducerUtils;
+ SQL::Translator::Generator::Role::Quote;
-use Moo;
-use Sub::Quote 'quote_sub';
+use Moo::Role;
-# this should be ro, but I have to modify it in BUILD so bleh
-has quote_chars => ( is => 'rw' );
-
-has name_sep => (
- is => 'ro',
- default => quote_sub q{ '.' },
-);
-
-sub BUILD {
- my $self = shift;
-
- unless (ref($self->quote_chars)) {
- if ($self->quote_chars) {
- $self->quote_chars([$self->quote_chars])
- } else {
- $self->quote_chars([])
- }
- }
-
- $self
-}
+requires qw(quote_chars name_sep);
sub quote {
my ($self, $label) = @_;
View
31 lib/SQL/Translator/Generator/Utils.pm
@@ -0,0 +1,31 @@
+package # hide from pause
+ SQL::Translator::Generator::Utils;
+
+use Moo;
+use Sub::Quote 'quote_sub';
+
+# this should be ro, but I have to modify it in BUILD so bleh
+has quote_chars => ( is => 'rw' );
+
+has name_sep => (
+ is => 'ro',
+ default => quote_sub q{ '.' },
+);
+
+with 'SQL::Translator::Generator::Role::Quote';
+
+sub BUILD {
+ my $self = shift;
+
+ unless (ref($self->quote_chars)) {
+ if ($self->quote_chars) {
+ $self->quote_chars([$self->quote_chars])
+ } else {
+ $self->quote_chars([])
+ }
+ }
+
+ $self
+}
+
+1;
View
1 lib/SQL/Translator/Loader/DBI/.sqlt_refactor_placeholder
@@ -0,0 +1 @@
+This directory will hold the new style SQLT DBI parsers
View
1 lib/SQL/Translator/Loader/DDL/.sqlt_refactor_placeholder
@@ -0,0 +1 @@
+This directory will hold the new style SQLT DDL parsers
View
1 lib/SQL/Translator/Loader/Role/.sqlt_refactor_placeholder
@@ -0,0 +1 @@
+This directory will hold the Roles for new style SQLT parsers
View
359 lib/SQL/Translator/Producer/SQLServer.pm
@@ -1,338 +1,64 @@
package SQL::Translator::Producer::SQLServer;
-=head1 NAME
-
-SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator
-
-=head1 SYNOPSIS
-
- use SQL::Translator;
-
- my $t = SQL::Translator->new( parser => '...', producer => 'SQLServer' );
- $t->translate;
-
-=head1 DESCRIPTION
-
-B<WARNING>B This is still fairly early code, basically a hacked version of the
-Sybase Producer (thanks Sam, Paul and Ken for doing the real work ;-)
-
-=head1 Extra Attributes
-
-=over 4
-
-=item field.list
-
-List of values for an enum field.
-
-=back
-
-=head1 TODO
-
- * !! Write some tests !!
- * Reserved words list needs updating to SQLServer.
- * Triggers, Procedures and Views DO NOT WORK
-
-=cut
-
use strict;
use warnings;
our ( $DEBUG, $WARN );
our $VERSION = '1.59';
$DEBUG = 1 unless defined $DEBUG;
-use Data::Dumper;
use SQL::Translator::Schema::Constants;
use SQL::Translator::Utils qw(debug header_comment);
-use SQL::Translator::ProducerUtils;
-
-my $util = SQL::Translator::ProducerUtils->new( quote_chars => ['[', ']'] );
-
-my %translate = (
- date => 'datetime',
- 'time' => 'datetime',
- # Sybase types
- #integer => 'numeric',
- #int => 'numeric',
- #number => 'numeric',
- #money => 'money',
- #varchar => 'varchar',
- #varchar2 => 'varchar',
- #timestamp => 'datetime',
- #text => 'varchar',
- #real => 'double precision',
- #comment => 'text',
- #bit => 'bit',
- #tinyint => 'smallint',
- #float => 'double precision',
- #serial => 'numeric',
- #boolean => 'varchar',
- #char => 'char',
- #long => 'varchar',
-);
+use SQL::Translator::Generator::DDL::SQLServer;
-# If these datatypes have size appended the sql fails.
-my @no_size = qw/tinyint smallint int integer bigint text bit image datetime/;
+sub produce {
+ my $translator = shift;
+ SQL::Translator::Generator::DDL::SQLServer->new(
+ add_comments => !$translator->no_comments,
+ add_drop_tables => $translator->add_drop_table,
+ )->schema($translator->schema)
+}
-my $max_id_length = 128;
-my %global_names;
+1;
=pod
=head1 SQLServer Create Table Syntax
TODO
-=cut
-
-sub produce {
- my $translator = shift;
- $DEBUG = $translator->debug;
- $WARN = $translator->show_warnings;
- my $no_comments = $translator->no_comments;
- my $add_drop_table = $translator->add_drop_table;
- my $schema = $translator->schema;
-
- %global_names = (); #reset
-
- my $output;
- $output .= header_comment."\n" unless ($no_comments);
-
- # Generate the DROP statements.
- if ($add_drop_table) {
- my @tables = sort { $b->order <=> $a->order } $schema->get_tables;
- $output .= "--\n-- Turn off constraints\n--\n\n" unless $no_comments;
- foreach my $table (@tables) {
- my $name = $table->name;
- my $q_name = unreserve($name);
- $output .= "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') ALTER TABLE $q_name NOCHECK CONSTRAINT all;\n"
- }
- $output .= "\n";
- $output .= "--\n-- Drop tables\n--\n\n" unless $no_comments;
- foreach my $table (@tables) {
- my $name = $table->name;
- my $q_name = unreserve($name);
- $output .= "IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') DROP TABLE $q_name;\n"
- }
- }
-
- # Generate the CREATE sql
-
- my @foreign_constraints = (); # these need to be added separately, as tables may not exist yet
- for my $table ( $schema->get_tables ) {
- my $table_name = $table->name or next;
- my $table_name_ur = unreserve($table_name) || '';
-
- my ( @comments, @field_defs, @index_defs, @constraint_defs );
-
- push @comments, "\n\n--\n-- Table: $table_name_ur\n--"
- unless $no_comments;
-
- push @comments, map { "-- $_" } $table->comments;
-
- #
- # Fields
- #
- my %field_name_scope;
- for my $field ( $table->get_fields ) {
- my $field_name = $field->name;
- my $field_name_ur = unreserve( $field_name );
- my $field_def = qq["$field_name_ur"];
- $field_def =~ s/\"//g;
- if ( $field_def =~ /identity/ ){
- $field_def =~ s/identity/pidentity/;
- }
-
- #
- # Datatype
- #
- my $data_type = lc $field->data_type;
- my $orig_data_type = $data_type;
- my %extra = $field->extra;
- my $list = $extra{'list'} || [];
- # \todo deal with embedded quotes
- my $commalist = join( ', ', map { qq['$_'] } @$list );
-
- if ( $data_type eq 'enum' ) {
- my $check_name = mk_name( $field_name . '_chk' );
- push @constraint_defs,
- "CONSTRAINT $check_name CHECK ($field_name IN ($commalist))";
- $data_type .= 'character varying';
- }
- elsif ( $data_type eq 'set' ) {
- $data_type .= 'character varying';
- }
- elsif ( grep { $data_type eq $_ } qw/bytea blob clob/ ) {
- $data_type = 'varbinary';
- }
- else {
- if ( defined $translate{ $data_type } ) {
- $data_type = $translate{ $data_type };
- }
- else {
- warn "Unknown datatype: $data_type ",
- "($table_name.$field_name)\n" if $WARN;
- }
- }
-
- my $size = $field->size;
- if ( grep $_ eq $data_type, @no_size) {
- # SQLServer doesn't seem to like sizes on some datatypes
- $size = undef;
- }
- elsif ( !$size ) {
- if ( $data_type =~ /numeric/ ) {
- $size = '9,0';
- }
- elsif ( $orig_data_type eq 'text' ) {
- #interpret text fields as long varchars
- $size = '255';
- }
- elsif (
- $data_type eq 'varchar' &&
- $orig_data_type eq 'boolean'
- ) {
- $size = '6';
- }
- elsif ( $data_type eq 'varchar' ) {
- $size = '255';
- }
- }
-
- $field_def .= " $data_type";
- $field_def .= "($size)" if $size;
-
- $field_def .= ' IDENTITY' if $field->is_auto_increment;
-
- #
- # Not null constraint
- #
- unless ( $field->is_nullable ) {
- $field_def .= ' NOT NULL';
- }
- else {
- $field_def .= ' NULL' if $data_type ne 'bit';
- }
-
- #
- # Default value
- #
- SQL::Translator::Producer->_apply_default_value(
- $field,
- \$field_def,
- [
- 'NULL' => \'NULL',
- ],
- );
-
- push @field_defs, $field_def;
- }
-
- #
- # Constraint Declarations
- #
- my @constraint_decs = ();
- for my $constraint ( $table->get_constraints ) {
- my $name = $constraint->name || '';
- my $name_ur = unreserve($name);
- # Make sure we get a unique name
- my $type = $constraint->type || NORMAL;
- my @fields = map { unreserve( $_ ) }
- $constraint->fields;
- my @rfields = map { unreserve( $_ ) }
- $constraint->reference_fields;
- next unless @fields;
+=head1 NAME
- my $c_def;
- if ( $type eq FOREIGN_KEY ) {
- $name ||= mk_name( $table_name . '_fk' );
- my $on_delete = uc ($constraint->on_delete || '');
- my $on_update = uc ($constraint->on_update || '');
+SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator
- # The default implicit constraint action in MSSQL is RESTRICT
- # but you can not specify it explicitly. Go figure :)
- for ($on_delete, $on_update) {
- undef $_ if $_ eq 'RESTRICT'
- }
+=head1 SYNOPSIS
- $c_def =
- "ALTER TABLE $table_name_ur ADD CONSTRAINT $name_ur FOREIGN KEY".
- ' (' . join( ', ', @fields ) . ') REFERENCES '.
- unreserve($constraint->reference_table).
- ' (' . join( ', ', @rfields ) . ')'
- ;
+ use SQL::Translator;
- if ( $on_delete && $on_delete ne "NO ACTION") {
- $c_def .= " ON DELETE $on_delete";
- }
- if ( $on_update && $on_update ne "NO ACTION") {
- $c_def .= " ON UPDATE $on_update";
- }
+ my $t = SQL::Translator->new( parser => '...', producer => 'SQLServer' );
+ $t->translate;
- $c_def .= ";";
+=head1 DESCRIPTION
- push @foreign_constraints, $c_def;
- next;
- }
+B<WARNING>B This is still fairly early code, basically a hacked version of the
+Sybase Producer (thanks Sam, Paul and Ken for doing the real work ;-)
+=head1 Extra Attributes
- if ( $type eq PRIMARY_KEY ) {
- $name = ($name ? unreserve($name) : mk_name( $table_name . '_pk' ));
- $c_def =
- "CONSTRAINT $name PRIMARY KEY ".
- '(' . join( ', ', @fields ) . ')';
- }
- elsif ( $type eq UNIQUE ) {
- $name = $name_ur || mk_name( $table_name . '_uc' );
- my @nullable = grep { $_->is_nullable } $constraint->fields;
- if (!@nullable) {
- $c_def =
- "CONSTRAINT $name UNIQUE " .
- '(' . join( ', ', @fields ) . ')';
- } else {
- push @index_defs,
- "CREATE UNIQUE NONCLUSTERED INDEX $name_ur ON $table_name_ur (" .
- join( ', ', @fields ) . ')' .
- ' WHERE ' . join( ' AND ', map unreserve($_->name) . ' IS NOT NULL', @nullable ) . ';';
- next;
- }
- }
- push @constraint_defs, $c_def;
- }
+=over 4
- #
- # Indices
- #
- for my $index ( $table->get_indices ) {
- my $idx_name = $index->name || mk_name($table_name . '_idx');
- my $idx_name_ur = unreserve($idx_name);
- push @index_defs,
- "CREATE INDEX $idx_name_ur ON $table_name_ur (".
- join( ', ', map unreserve($_), $index->fields ) . ");";
- }
+=item field.list
- my $create_statement = "";
- $create_statement .= qq[CREATE TABLE $table_name_ur (\n].
- join( ",\n",
- map { " $_" } @field_defs, @constraint_defs
- ).
- "\n);"
- ;
+List of values for an enum field.
- $output .= join( "\n\n",
- @comments,
- $create_statement,
- @index_defs,
- );
- }
+=back
-# Add FK constraints
- $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
+=head1 TODO
-# create view/procedure are NOT prepended to the input $sql, needs
-# to be filled in with the proper syntax
+ * !! Write some tests !!
+ * Reserved words list needs updating to SQLServer.
+ * Triggers, Procedures and Views DO NOT WORK
-=pod
# Text of view is already a 'create view' statement so no need to
# be fancy
@@ -357,37 +83,6 @@ sub produce {
$text =~ s/\r//g;
$output .= "$text\nGO\n";
}
-=cut
-
- return $output;
-}
-
-sub mk_name {
- my ($name, $scope, $critical) = @_;
-
- $scope ||= \%global_names;
- if ( my $prev = $scope->{ $name } ) {
- my $name_orig = $name;
- $name .= sprintf( "%02d", ++$prev );
- substr($name, $max_id_length - 3) = "00"
- if length( $name ) > $max_id_length;
-
- warn "The name '$name_orig' has been changed to ",
- "'$name' to make it unique.\n" if $WARN;
-
- $scope->{ $name_orig }++;
- }
- $name = substr( $name, 0, $max_id_length )
- if ((length( $name ) > $max_id_length) && $critical);
- $scope->{ $name }++;
- return unreserve($name);
-}
-
-sub unreserve { $util->quote($_[0]) }
-
-1;
-
-=pod
=head1 SEE ALSO
View
83 lib/SQL/Translator/Producer/SQLite.pm
@@ -22,8 +22,9 @@ use warnings;
use Data::Dumper;
use SQL::Translator::Schema::Constants;
use SQL::Translator::Utils qw(debug header_comment parse_dbms_version);
-use SQL::Translator::ProducerUtils;
-my $util = SQL::Translator::ProducerUtils->new( quote_chars => q(") );
+use SQL::Translator::Generator::Utils;
+my $util = SQL::Translator::Generator::Utils->new( quote_chars => q(") );
+use SQL::Translator::Generator::DDL::SQLite;
our ( $DEBUG, $WARN );
our $VERSION = '1.59';
@@ -32,6 +33,7 @@ $WARN = 0 unless defined $WARN;
our $max_id_length = 30;
my %global_names;
+my $future = SQL::Translator::Generator::DDL::SQLite->new();
sub produce {
my $translator = shift;
@@ -262,82 +264,7 @@ sub create_foreignkey {
return $fk_sql;
}
-sub create_field
-{
- my ($field, $options) = @_;
-
- my $field_name = $util->quote($field->name);
- debug("PKG: Looking at field '$field_name'\n");
- my $field_comments = $field->comments
- ? "-- " . $field->comments . "\n "
- : '';
-
- my $field_def = $field_comments.$field_name;
-
- # data type and size
- my $size = $field->size;
- my $data_type = $field->data_type;
- $data_type = 'varchar' if lc $data_type eq 'set';
- $data_type = 'blob' if lc $data_type eq 'bytea';
-
- if ( lc $data_type =~ /(text|blob)/i ) {
- $size = undef;
- }
-
-# if ( $data_type =~ /timestamp/i ) {
-# push @trigger_defs,
-# "CREATE TRIGGER ts_${table_name} ".
-# "after insert on $table_name\n".
-# "begin\n".
-# " update $table_name set $field_name=timestamp() ".
-# "where id=new.id;\n".
-# "end;\n"
-# ;
-#
-# }
-
- #
- # SQLite is generally typeless, but newer versions will
- # make a field autoincrement if it is declared as (and
- # *only* as) INTEGER PRIMARY KEY
- #
- my $pk = $field->table->primary_key;
- my @pk_fields = $pk ? $pk->fields : ();
-
- if (
- $field->is_primary_key &&
- scalar @pk_fields == 1 &&
- (
- $data_type =~ /int(eger)?$/i
- ||
- ( $data_type =~ /^number?$/i && $size !~ /,/ )
- )
- ) {
- $data_type = 'INTEGER PRIMARY KEY';
- $size = undef;
-# $pk_set = 1;
- }
-
- $field_def .= sprintf " %s%s", $data_type,
- ( !$field->is_auto_increment && $size ) ? "($size)" : '';
-
- # Null?
- $field_def .= ' NOT NULL' unless $field->is_nullable;
-
- # Default?
- SQL::Translator::Producer->_apply_default_value(
- $field,
- \$field_def,
- [
- 'NULL' => \'NULL',
- 'now()' => 'now()',
- 'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP',
- ],
- );
-
- return $field_def;
-
-}
+sub create_field { return $future->field($_[0]) }
sub create_index
{
View
8 t/65-producerutils.t → t/65-generator-utils.t
@@ -3,23 +3,23 @@ use warnings;
use Test::More;
-use SQL::Translator::ProducerUtils;
+use SQL::Translator::Generator::Utils;
-my $util = SQL::Translator::ProducerUtils->new(
+my $util = SQL::Translator::Generator::Utils->new(
quote_chars => ['[', ']'],
);
is $util->quote('frew'), '[frew]', 'simple quote works';
is $util->quote('people.frew'), '[people].[frew]', 'namespaced quote works';
-my $single_util = SQL::Translator::ProducerUtils->new(
+my $single_util = SQL::Translator::Generator::Utils->new(
quote_chars => q(|),
);
is $single_util->quote('frew'), '|frew|', 'simple single quote works';
is $single_util->quote('people.frew'), '|people|.|frew|', 'namespaced single quote works';
-my $no_util = SQL::Translator::ProducerUtils->new();
+my $no_util = SQL::Translator::Generator::Utils->new();
is $no_util->quote('frew'), 'frew', 'simple no quote works';
is $no_util->quote('people.frew'), 'people.frew', 'namespaced no quote works';
View
23 t/71-generator-sql_server.t
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+use SQL::Translator::Generator::DDL::SQLServer;
+use SQL::Translator::Schema::Field;
+
+my $shim = SQL::Translator::Generator::DDL::SQLServer->new();
+
+is $shim->field(SQL::Translator::Schema::Field->new(
+ name => 'lol',
+ data_type => 'int',
+)), '[lol] int NULL', 'simple field is generated correctly';
+
+is $shim->field(SQL::Translator::Schema::Field->new(
+ name => 'nice',
+ data_type => 'varchar',
+ size => 10,
+)), '[nice] varchar(10) NULL', 'sized field is generated correctly';
+
+done_testing;
+

0 comments on commit 4df76cb

Please sign in to comment.