From: Arthur Axel 'fREW' Schmidt Date: Tue, 8 Mar 2011 05:15:48 +0000 (-0600) Subject: add foreign_key_constraint X-Git-Tag: v0.11011~27^2~19 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=38d0ddf0677af37270825aa67edde30aa8ba13bd;p=dbsrgits%2FSQL-Translator.git add foreign_key_constraint --- diff --git a/lib/SQL/Translator/Generator/DDL/SQLServer.pm b/lib/SQL/Translator/Generator/DDL/SQLServer.pm index b14d692..0d9c5cc 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLServer.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLServer.pm @@ -58,17 +58,17 @@ sub field_autoinc { ( $_[1]->is_auto_increment ? 'IDENTITY' : () ) } sub primary_key_constraint { 'CONSTRAINT ' . - $_[0]->shim->quote($_[1]->name || $_[1]->table->name . '_pk') . + $_[0]->quote($_[1]->name || $_[1]->table->name . '_pk') . ' PRIMARY KEY (' . - join( ', ', map $_[0]->shim->quote($_), $_[1]->fields ) . + join( ', ', map $_[0]->quote($_), $_[1]->fields ) . ')' } sub index { 'CREATE INDEX ' . - $_[0]->shim->quote($_[1]->name || $_[1]->table->name . '_idx') . - ' ON ' . $_[0]->shim->quote($_[1]->table->name) . - ' (' . join( ', ', map $_[0]->shim->quote($_), $_[1]->fields ) . ');' + $_[0]->quote($_[1]->name || $_[1]->table->name . '_idx') . + ' ON ' . $_[0]->quote($_[1]->table->name) . + ' (' . join( ', ', map $_[0]->quote($_), $_[1]->fields ) . ');' } sub unique_constraint_single { @@ -76,12 +76,12 @@ sub unique_constraint_single { 'CONSTRAINT ' . $self->unique_constraint_name($constraint) . - ' UNIQUE (' . join( ', ', $constraint->fields ) . ')' + ' UNIQUE (' . join( ', ', map $self->quote($_), $constraint->fields ) . ')' } sub unique_constraint_name { my ($self, $constraint) = @_; - $self->shim->quote($constraint->name || $constraint->table->name . '_uc' ) + $self->quote($constraint->name || $constraint->table->name . '_uc' ) } sub unique_constraint_multiple { @@ -89,12 +89,42 @@ sub unique_constraint_multiple { 'CREATE UNIQUE NONCLUSTERED INDEX ' . $self->unique_constraint_name($constraint) . - ' ON ' . $self->shim->quote($constraint->table->name) . ' (' . + ' ON ' . $self->quote($constraint->table->name) . ' (' . join( ', ', $constraint->fields ) . ')' . ' WHERE ' . join( ' AND ', - map $self->shim->quote($_->name) . ' IS NOT NULL', + 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 + : '' + ) . ';'; +} + 1; diff --git a/lib/SQL/Translator/Generator/Role/DDL.pm b/lib/SQL/Translator/Generator/Role/DDL.pm index 111a864..7b6726f 100644 --- a/lib/SQL/Translator/Generator/Role/DDL.pm +++ b/lib/SQL/Translator/Generator/Role/DDL.pm @@ -10,6 +10,7 @@ requires 'field_type_size'; has shim => ( is => 'ro', + handles => [ 'quote' ], builder => '_build_shim', ); @@ -30,7 +31,7 @@ has unquoted_defaults => ( # would also be handy to have a required size set if there is such a thing -sub field_name { $_[0]->shim->quote($_[1]->name) } +sub field_name { $_[0]->quote($_[1]->name) } sub field_comments { ( $_[1]->comments ? ('-- ' . $_[1]->comments . "\n ") : () ) diff --git a/lib/SQL/Translator/Producer/SQLServer.pm b/lib/SQL/Translator/Producer/SQLServer.pm index 8e89429..a125873 100644 --- a/lib/SQL/Translator/Producer/SQLServer.pm +++ b/lib/SQL/Translator/Producer/SQLServer.pm @@ -163,45 +163,12 @@ 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( $_ ) } - $constraint->fields; - my @rfields = map { unreserve( $_ ) } - $constraint->reference_fields; - next unless @fields; + next unless $constraint->fields; 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; + push @foreign_constraints, $future->foreign_key_constraint($constraint); next; }