From: Arthur Axel 'fREW' Schmidt Date: Tue, 8 Mar 2011 02:18:36 +0000 (-0600) Subject: add comments, better default handling X-Git-Tag: v0.11011~27^2~24 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f699ffaa9c3e8ac63588c63146ec527df42958a1;p=dbsrgits%2FSQL-Translator.git add comments, better default handling --- diff --git a/lib/SQL/Translator/Generator/DDL/SQLServer.pm b/lib/SQL/Translator/Generator/DDL/SQLServer.pm index 8132bc4..c751e1d 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLServer.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLServer.pm @@ -7,6 +7,18 @@ with 'SQL::Translator::Generator::Role::DDL'; sub _build_shim { SQL::Translator::Generator::Utils->new( quote_chars => [qw( [ ] )] ) } +sub _build_numeric_types { + +{ + int => 1, + } +} + +sub _build_unquoted_defaults { + +{ + NULL => 1, + } +} + sub _build_type_map { +{ date => 'datetime', diff --git a/lib/SQL/Translator/Generator/Role/DDL.pm b/lib/SQL/Translator/Generator/Role/DDL.pm index 6510468..111a864 100644 --- a/lib/SQL/Translator/Generator/Role/DDL.pm +++ b/lib/SQL/Translator/Generator/Role/DDL.pm @@ -4,6 +4,8 @@ use Moo::Role; requires '_build_shim'; requires '_build_type_map'; +requires '_build_numeric_types'; +requires '_build_unquoted_defaults'; requires 'field_type_size'; has shim => ( @@ -16,14 +18,32 @@ has type_map => ( builder => '_build_type_map', ); +has numeric_types => ( + is => 'ro', + builder => '_build_numeric_types', +); + +has unquoted_defaults => ( + is => 'ro', + builder => '_build_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_nullable { ($_[1]->is_nullable ? 'NULL' : 'NOT NULL' ) } +sub field_comments { + ( $_[1]->comments ? ('-- ' . $_[1]->comments . "\n ") : () ) +} + +sub field_nullable { ($_[1]->is_nullable ? $_[0]->nullable : 'NOT NULL' ) } sub field_default { - (defined $_[1]->default_value ? 'DEFAULT ' . q(') . $_[1]->default_value . q(') : () ) + return () if !defined $_[1]->default_value; + + my $val = $_[1]->default_value; + $val = "'$val'" unless $_[0]->numeric_types->{$_[1]->data_type}; + return ( "DEFAULT $val" ) } sub field_type { @@ -33,4 +53,6 @@ sub field_type { ($self->type_map->{$field_type} || $field_type).$self->field_type_size($field) } +sub nullable { 'NULL' } + 1;