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
#
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;
];
# 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;
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(
$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 ) {
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;
}
#
$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;
}
#
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
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.