Combined patches from RT#70734 and RT#44769
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLServer.pm
index 8672004..61c6eeb 100644 (file)
@@ -1,23 +1,5 @@
 package SQL::Translator::Producer::SQLServer;
 
-# -------------------------------------------------------------------
-# Copyright (C) 2002-2009 SQLFairy Authors
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License as
-# published by the Free Software Foundation; version 2.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-# 02111-1307  USA
-# -------------------------------------------------------------------
-
 =head1 NAME
 
 SQL::Translator::Producer::SQLServer - MS SQLServer producer for SQL::Translator
@@ -53,13 +35,17 @@ List of values for an enum field.
 =cut
 
 use strict;
-use vars qw[ $DEBUG $WARN $VERSION ];
-$VERSION = '1.59';
+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',
@@ -78,37 +64,17 @@ my %translate  = (
     #bit       => 'bit',
     #tinyint   => 'smallint',
     #float     => 'double precision',
-    #serial    => 'numeric', 
+    #serial    => 'numeric',
     #boolean   => 'varchar',
     #char      => 'char',
     #long      => 'varchar',
 );
 
-# TODO - This is still the Sybase list!
-my %reserved = map { $_, 1 } qw[
-    ALL ANALYSE ANALYZE AND ANY AS ASC 
-    BETWEEN BINARY BOTH
-    CASE CAST CHECK COLLATE COLUMN CONSTRAINT CROSS
-    CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER 
-    DEFAULT DEFERRABLE DESC DISTINCT DO
-    ELSE END EXCEPT
-    FALSE FOR FOREIGN FREEZE FROM FULL 
-    GROUP HAVING 
-    ILIKE IN INITIALLY INNER INTERSECT INTO IS ISNULL 
-    JOIN LEADING LEFT LIKE LIMIT 
-    NATURAL NEW NOT NOTNULL NULL
-    OFF OFFSET OLD ON ONLY OR ORDER OUTER OVERLAPS
-    PRIMARY PUBLIC REFERENCES RIGHT 
-    SELECT SESSION_USER SOME TABLE THEN TO TRAILING TRUE 
-    UNION UNIQUE USER USING VERBOSE WHEN WHERE
-];
-
 # If these datatypes have size appended the sql fails.
 my @no_size = qw/tinyint smallint int integer bigint text bit image datetime/;
 
 my $max_id_length    = 128;
 my %global_names;
-my %unreserve;
 
 =pod
 
@@ -118,7 +84,6 @@ TODO
 
 =cut
 
-# -------------------------------------------------------------------
 sub produce {
     my $translator     = shift;
     $DEBUG             = $translator->debug;
@@ -128,30 +93,32 @@ sub produce {
     my $schema         = $translator->schema;
 
     %global_names = (); #reset
-    %unreserve = ();
 
     my $output;
     $output .= header_comment."\n" unless ($no_comments);
 
-    # Generate the DROP statements. We do this in one block here as if we
-    # have fkeys we need to drop in the correct order otherwise they will fail
-    # due to the dependancies the fkeys setup. (There is no way to turn off
-    # fkey checking while we sort the schema like MySQL's set
-    # foreign_key_checks=0)
-    # We assume the tables are in the correct order to set them up as you need
-    # to have created a table to fkey to it. So the reverse order should drop
-    # them properly, fingers crossed...
+    # 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 (
-            sort { $b->order <=> $a->order } $schema->get_tables
-        ) {
-            my $name = unreserve($table->name);
-            $output .= qq{IF EXISTS (SELECT name FROM sysobjects WHERE name = '$name' AND type = 'U') DROP TABLE $name;\n\n}
+        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) || '';
@@ -169,7 +136,7 @@ sub produce {
         my %field_name_scope;
         for my $field ( $table->get_fields ) {
             my $field_name    = $field->name;
-            my $field_name_ur = unreserve( $field_name, $table_name );
+            my $field_name_ur = unreserve( $field_name );
             my $field_def     = qq["$field_name_ur"];
             $field_def        =~ s/\"//g;
             if ( $field_def =~ /identity/ ){
@@ -195,6 +162,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,18 +217,15 @@ sub produce {
             #
             # Default value
             #
-            my $default = $field->default_value;
-            if ( defined $default ) {
-                SQL::Translator::Producer->_apply_default_value(
-                  \$field_def,
-                  $default, 
-                  [
-                    'NULL'       => \'NULL',
-                  ],
-                );
-            }
-
-            push @field_defs, $field_def;            
+            SQL::Translator::Producer->_apply_default_value(
+              $field,
+              \$field_def,
+              [
+                'NULL'       => \'NULL',
+              ],
+            );
+
+            push @field_defs, $field_def;
         }
 
         #
@@ -267,42 +234,68 @@ sub produce {
         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( $_, $table_name ) }
+            my @fields  = map { unreserve( $_ ) }
                 $constraint->fields;
-            my @rfields = map { unreserve( $_, $table_name ) }
+            my @rfields = map { unreserve( $_ ) }
                 $constraint->reference_fields;
             next unless @fields;
 
-                       my $c_def;
+            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 || '');
+
+                # 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'
+                }
+
+                $c_def =
+                    "ALTER TABLE $table_name_ur ADD CONSTRAINT $name_ur FOREIGN KEY".
+                    ' (' . join( ', ', @fields ) . ') REFERENCES '.
+                    unreserve($constraint->reference_table).
+                    ' (' . 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 = 
+                $name = ($name ? unreserve($name) : mk_name( $table_name . '_pk' ));
+                $c_def =
                     "CONSTRAINT $name PRIMARY KEY ".
                     '(' . join( ', ', @fields ) . ')';
             }
-            elsif ( $type eq FOREIGN_KEY ) {
-                $name ||= mk_name( $table_name . '_fk' );
-                $c_def = 
-                    "CONSTRAINT $name FOREIGN KEY".
-                    ' (' . join( ', ', @fields ) . ') REFERENCES '.
-                    $constraint->reference_table.
-                    ' (' . join( ', ', @rfields ) . ')';
-                 my $on_delete = $constraint->on_delete;
-                 if ( $on_delete && $on_delete ne "NO ACTION") {
-                       $c_def .= " ON DELETE $on_delete";
-                 }
-                 my $on_update = $constraint->on_update;
-                 if ( $on_update && $on_update ne "NO ACTION") {
-                       $c_def .= " ON UPDATE $on_update";
-                 }
-            }
             elsif ( $type eq UNIQUE ) {
-                $name ||= mk_name( $table_name . '_uc' );
-                $c_def = 
-                    "CONSTRAINT $name UNIQUE " .
-                    '(' . join( ', ', @fields ) . ')';
+                $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;
         }
@@ -312,14 +305,15 @@ sub produce {
         #
         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 ON $table_name (".
-                join( ', ', $index->fields ) . ");";
+                "CREATE INDEX $idx_name_ur ON $table_name_ur (".
+                join( ', ', map unreserve($_), $index->fields ) . ");";
         }
 
         my $create_statement = "";
         $create_statement .= qq[CREATE TABLE $table_name_ur (\n].
-            join( ",\n", 
+            join( ",\n",
                 map { "  $_" } @field_defs, @constraint_defs
             ).
             "\n);"
@@ -332,10 +326,13 @@ 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
 
-=begin
+=pod
 
     # Text of view is already a 'create view' statement so no need to
     # be fancy
@@ -357,7 +354,7 @@ sub produce {
         $output .= "\n\n";
         $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments;
         my $text = $_->sql();
-               $text =~ s/\r//g;
+      $text =~ s/\r//g;
         $output .= "$text\nGO\n";
     }
 =cut
@@ -365,7 +362,6 @@ sub produce {
     return $output;
 }
 
-# -------------------------------------------------------------------
 sub mk_name {
     my ($name, $scope, $critical) = @_;
 
@@ -373,7 +369,7 @@ sub mk_name {
     if ( my $prev = $scope->{ $name } ) {
         my $name_orig = $name;
         $name        .= sprintf( "%02d", ++$prev );
-        substr($name, $max_id_length - 3) = "00" 
+        substr($name, $max_id_length - 3) = "00"
             if length( $name ) > $max_id_length;
 
         warn "The name '$name_orig' has been changed to ",
@@ -381,36 +377,16 @@ sub mk_name {
 
         $scope->{ $name_orig }++;
     }
-    $name = substr( $name, 0, $max_id_length ) 
+    $name = substr( $name, 0, $max_id_length )
                         if ((length( $name ) > $max_id_length) && $critical);
     $scope->{ $name }++;
-    return $name;
+    return unreserve($name);
 }
 
-# -------------------------------------------------------------------
-sub unreserve {
-    my $name            = shift || '';
-    my $schema_obj_name = shift || '';
-    my ( $suffix ) = ( $name =~ s/(\W.*)$// ) ? $1 : '';
-
-    # also trap fields that don't begin with a letter
-    return $name if !$reserved{ uc $name } && $name =~ /^[a-z]/i; 
-
-    if ( $schema_obj_name ) {
-        ++$unreserve{"$schema_obj_name.$name"};
-    }
-    else {
-        ++$unreserve{"$name (table name)"};
-    }
-
-    my $unreserve = sprintf '%s_', $name;
-    return $unreserve.$suffix;
-}
+sub unreserve { $util->quote($_[0]) }
 
 1;
 
-# -------------------------------------------------------------------
-
 =pod
 
 =head1 SEE ALSO