From: Arthur Axel 'fREW' Schmidt Date: Tue, 8 Mar 2011 04:57:01 +0000 (-0600) Subject: factor out unique constraints X-Git-Tag: v0.11011~27^2~20 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2363a6297557d792253fbdd20956cc791b3a1ea9;p=dbsrgits%2FSQL-Translator.git factor out unique constraints --- diff --git a/lib/SQL/Translator/Generator/DDL/SQLServer.pm b/lib/SQL/Translator/Generator/DDL/SQLServer.pm index 85be5ab..b14d692 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLServer.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLServer.pm @@ -71,5 +71,30 @@ sub index { ' (' . join( ', ', map $_[0]->shim->quote($_), $_[1]->fields ) . ');' } +sub unique_constraint_single { + my ($self, $constraint) = @_; + + 'CONSTRAINT ' . + $self->unique_constraint_name($constraint) . + ' UNIQUE (' . join( ', ', $constraint->fields ) . ')' +} + +sub unique_constraint_name { + my ($self, $constraint) = @_; + $self->shim->quote($constraint->name || $constraint->table->name . '_uc' ) +} + +sub unique_constraint_multiple { + my ($self, $constraint) = @_; + + 'CREATE UNIQUE NONCLUSTERED INDEX ' . + $self->unique_constraint_name($constraint) . + ' ON ' . $self->shim->quote($constraint->table->name) . ' (' . + join( ', ', $constraint->fields ) . ')' . + ' WHERE ' . join( ' AND ', + map $self->shim->quote($_->name) . ' IS NOT NULL', + grep { $_->is_nullable } $constraint->fields ) . ';' +} + 1; diff --git a/lib/SQL/Translator/Producer/SQLServer.pm b/lib/SQL/Translator/Producer/SQLServer.pm index cb93c64..8e89429 100644 --- a/lib/SQL/Translator/Producer/SQLServer.pm +++ b/lib/SQL/Translator/Producer/SQLServer.pm @@ -210,17 +210,10 @@ sub produce { $c_def = $future->primary_key_constraint($constraint) } elsif ( $type eq UNIQUE ) { - $name = $name_ur || mk_name( $table_name . '_uc' ); - my @nullable = grep { $_->is_nullable } $constraint->fields; - if (!@nullable) { - $c_def = - "CONSTRAINT $name UNIQUE " . - '(' . join( ', ', @fields ) . ')'; + if (!grep { $_->is_nullable } $constraint->fields) { + $c_def = $future->unique_constraint_single($constraint) } 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 ) . ';'; + push @index_defs, $future->unique_constraint_multiple($constraint); next; } }