X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer%2FSQLServer.pm;h=8affe1ae942ca0c767881da7cb5fa5cf8e91ee12;hb=44659089c28216f1984873bc4aa8641e2e0e3410;hp=5e91cc81a0443c171b5f92d6769a1014ae154ca8;hpb=5bac76bc7960f05f858628266ab1c057491845b2;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Producer/SQLServer.pm b/lib/SQL/Translator/Producer/SQLServer.pm index 5e91cc8..8affe1a 100644 --- a/lib/SQL/Translator/Producer/SQLServer.pm +++ b/lib/SQL/Translator/Producer/SQLServer.pm @@ -152,6 +152,9 @@ sub produce { } # 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) || ''; @@ -195,6 +198,9 @@ sub produce { 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 }; @@ -247,16 +253,13 @@ sub produce { # # Default value # - my $default = $field->default_value; - if ( defined $default ) { - SQL::Translator::Producer->_apply_default_value( - \$field_def, - $default, - [ - 'NULL' => \'NULL', - ], - ); - } + SQL::Translator::Producer->_apply_default_value( + $field, + \$field_def, + [ + 'NULL' => \'NULL', + ], + ); push @field_defs, $field_def; } @@ -276,13 +279,7 @@ sub produce { next unless @fields; my $c_def; - if ( $type eq PRIMARY_KEY ) { - $name ||= mk_name( $table_name . '_pk' ); - $c_def = - "CONSTRAINT $name PRIMARY KEY ". - '(' . join( ', ', @fields ) . ')'; - } - elsif ( $type eq FOREIGN_KEY ) { + 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 || ''); @@ -294,16 +291,31 @@ sub produce { } $c_def = - "CONSTRAINT $name FOREIGN KEY". + "ALTER TABLE $table_name ADD CONSTRAINT $name FOREIGN KEY". ' (' . join( ', ', @fields ) . ') REFERENCES '. $constraint->reference_table. - ' (' . join( ', ', @rfields ) . ')'; + ' (' . join( ', ', @rfields ) . ')' + ; + 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"; } + + $c_def .= ";"; + + push @foreign_constraints, $c_def; + next; + } + + + if ( $type eq PRIMARY_KEY ) { + $name ||= mk_name( $table_name . '_pk' ); + $c_def = + "CONSTRAINT $name PRIMARY KEY ". + '(' . join( ', ', @fields ) . ')'; } elsif ( $type eq UNIQUE ) { $name ||= mk_name( $table_name . '_uc' ); @@ -339,6 +351,9 @@ sub produce { ); } +# Add FK constraints + $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints; + # create view/procedure are NOT prepended to the input $sql, needs # to be filled in with the proper syntax