X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer%2FSQLServer.pm;h=d85ff96eecdb2b36e59c600c834793f340e0ef10;hb=821a0fde221f5accf93e3f65efa77b5a6733cb5e;hp=bbbaa28a8d01ffa063f1f4c1a2c2a29a572c31a9;hpb=7a0ceaa1f8db050650ec94065943174b087b06df;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Producer/SQLServer.pm b/lib/SQL/Translator/Producer/SQLServer.pm index bbbaa28..d85ff96 100644 --- a/lib/SQL/Translator/Producer/SQLServer.pm +++ b/lib/SQL/Translator/Producer/SQLServer.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::SQLServer; # ------------------------------------------------------------------- -# $Id: SQLServer.pm,v 1.1 2005-01-13 21:30:04 grommit Exp $ +# $Id$ # ------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -56,7 +56,7 @@ List of values for an enum field. use strict; use vars qw[ $DEBUG $WARN $VERSION ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/; $DEBUG = 1 unless defined $DEBUG; use Data::Dumper; @@ -106,9 +106,9 @@ my %reserved = map { $_, 1 } qw[ ]; # If these datatypes have size appended the sql fails. -my @no_size = qw/int integer bigint text bit/; +my @no_size = qw/tinyint smallint int integer bigint text bit image datetime/; -my $max_id_length = 30; +my $max_id_length = 128; my %used_identifiers = (); my %global_names; my %unreserve; @@ -189,7 +189,6 @@ sub produce { my $list = $extra{'list'} || []; # \todo deal with embedded quotes my $commalist = join( ', ', map { qq['$_'] } @$list ); - my $seq_name; if ( $data_type eq 'enum' ) { my $check_name = mk_name( @@ -242,18 +241,6 @@ sub produce { $field_def .= ' IDENTITY' if $field->is_auto_increment; # - # Default value - # - my $default = $field->default_value; - if ( defined $default ) { - $field_def .= sprintf( ' DEFAULT %s', - ( $field->is_auto_increment && $seq_name ) - ? qq[nextval('"$seq_name"'::text)] : - ( $default =~ m/null/i ) ? 'NULL' : "'$default'" - ); - } - - # # Not null constraint # unless ( $field->is_nullable ) { @@ -262,7 +249,22 @@ sub produce { else { $field_def .= ' NULL' if $data_type ne 'bit'; } - push @field_defs, $field_def; + + # + # 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; } # @@ -281,30 +283,40 @@ sub produce { $constraint->reference_fields; next unless @fields; + my $c_def; if ( $type eq PRIMARY_KEY ) { $name ||= mk_name( $table_name, 'pk', undef,1 ); - push @constraint_defs, + $c_def = "CONSTRAINT $name PRIMARY KEY ". '(' . join( ', ', @fields ) . ')'; } elsif ( $type eq FOREIGN_KEY ) { $name ||= mk_name( $table_name, 'fk', undef,1 ); #$name = mk_name( ($name || $table_name), 'fk', undef,1 ); - push @constraint_defs, + $c_def = "CONSTRAINT $name FOREIGN KEY". ' (' . join( ', ', @fields ) . ') REFERENCES '. $constraint->reference_table. ' (' . join( ', ', @rfields ) . ')'; + my $on_delete = $constraint->on_delete; + if ( defined $on_delete && $on_delete ne "NO ACTION") { + $c_def .= " ON DELETE $on_delete"; + } + my $on_update = $constraint->on_update; + if ( defined $on_update && $on_update ne "NO ACTION") { + $c_def .= " ON UPDATE $on_update"; + } } elsif ( $type eq UNIQUE ) { $name ||= mk_name( $table_name, $name || ++$c_name_default,undef, 1 ); - push @constraint_defs, + $c_def = "CONSTRAINT $name UNIQUE " . '(' . join( ', ', @fields ) . ')'; } + push @constraint_defs, $c_def; } # @@ -338,8 +350,10 @@ sub produce { foreach ( $schema->get_views ) { my $name = $_->name(); $output .= "\n\n"; - $output .= "--\n-- View: $name\n--" unless $no_comments; - $output .= $_->sql(); + $output .= "--\n-- View: $name\n--\n\n" unless $no_comments; + my $text = $_->sql(); + $text =~ s/\r//g; + $output .= "$text\nGO\n"; } # Text of procedure already has the 'create procedure' stuff @@ -349,8 +363,10 @@ sub produce { foreach ( $schema->get_procedures ) { my $name = $_->name(); $output .= "\n\n"; - $output .= "--\n-- Procedure: $name\n--" unless $no_comments; - $output .= $_->sql(); + $output .= "--\n-- Procedure: $name\n--\n\n" unless $no_comments; + my $text = $_->sql(); + $text =~ s/\r//g; + $output .= "$text\nGO\n"; } # Warn out how we messed with the names.