From: Arthur Axel 'fREW' Schmidt Date: Thu, 10 Mar 2011 01:38:11 +0000 (-0600) Subject: add enum_constraint X-Git-Tag: v0.11011~27^2~18 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=280d92babc4fcf48d0b634f50d011a655c3f0bd8;p=dbsrgits%2FSQL-Translator.git add enum_constraint --- diff --git a/lib/SQL/Translator/Generator/DDL/SQLServer.pm b/lib/SQL/Translator/Generator/DDL/SQLServer.pm index 0d9c5cc..15bab3a 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLServer.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLServer.pm @@ -126,5 +126,20 @@ sub foreign_key_constraint { ) . ';'; } +sub enum_constraint_name { + my ($self, $field_name) = @_; + $self->quote($field_name . '_chk' ) +} + +sub enum_constraint { + my ( $self, $field_name, $vals ) = @_; + + return ( + 'CONSTRAINT ' . $self->enum_constraint_name($field_name) . + ' CHECK (' . $self->quote($field_name) . + ' IN (' . join( ',', map qq('$_'), @$vals ) . '))' + ) +} + 1; diff --git a/lib/SQL/Translator/Producer/SQLServer.pm b/lib/SQL/Translator/Producer/SQLServer.pm index a125873..741f61e 100644 --- a/lib/SQL/Translator/Producer/SQLServer.pm +++ b/lib/SQL/Translator/Producer/SQLServer.pm @@ -144,15 +144,9 @@ sub produce { # my $data_type = lc $field->data_type; my %extra = $field->extra; - my $list = $extra{'list'} || []; - # \todo deal with embedded quotes - my $commalist = join( ', ', map { qq['$_'] } @$list ); if ( $data_type eq 'enum' ) { - my $check_name = mk_name( $field_name . '_chk' ); - push @constraint_defs, - "CONSTRAINT $check_name CHECK ($field_name IN ($commalist))"; - $data_type .= 'character varying'; + push @constraint_defs, $future->enum_constraint($field_name, $extra{'list'} || []) } push @field_defs, $future->field($field);