From: Arthur Axel 'fREW' Schmidt Date: Sat, 26 Feb 2011 11:04:24 +0000 (-0600) Subject: start of hardcore refactoring X-Git-Tag: v0.11011~27^2~26 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1ea76bff59c57c205b8fb8012803dd37699ae450;p=dbsrgits%2FSQL-Translator.git start of hardcore refactoring --- diff --git a/lib/SQL/Translator/Generator/DDL/SQLServer.pm b/lib/SQL/Translator/Generator/DDL/SQLServer.pm new file mode 100644 index 0000000..8132bc4 --- /dev/null +++ b/lib/SQL/Translator/Generator/DDL/SQLServer.pm @@ -0,0 +1,48 @@ +package SQL::Translator::Generator::DDL::SQLServer; + +use Moo; +use SQL::Translator::Generator::Utils; + +with 'SQL::Translator::Generator::Role::DDL'; + +sub _build_shim { SQL::Translator::Generator::Utils->new( quote_chars => [qw( [ ] )] ) } + +sub _build_type_map { + +{ + date => 'datetime', + 'time' => 'datetime', + } +} + +has sizeless_types => ( + is => 'ro', + builder => '_build_sizeless_types', +); + +sub _build_sizeless_types { + +{ map { $_ => 1 } + qw( tinyint smallint int integer bigint text bit image datetime ) } +} + +sub field { + my ($self, $field) = @_; + + return join ' ', $self->field_name($field), ($self->field_type($field)||die 'type is required'), + $self->field_autoinc($field), + $self->field_nullable($field), + $self->field_default($field), +} + +sub field_type_size { + my ($self, $field) = @_; + + ($field->size && !$self->sizeless_types->{$field->data_type} + ? '(' . $field->size . ')' + : '' + ) +} + +sub field_autoinc { ( $_[1]->is_auto_increment ? 'IDENTITY' : () ) } + +1; + diff --git a/lib/SQL/Translator/Generator/Role/DDL.pm b/lib/SQL/Translator/Generator/Role/DDL.pm new file mode 100644 index 0000000..6510468 --- /dev/null +++ b/lib/SQL/Translator/Generator/Role/DDL.pm @@ -0,0 +1,36 @@ +package SQL::Translator::Generator::Role::DDL; + +use Moo::Role; + +requires '_build_shim'; +requires '_build_type_map'; +requires 'field_type_size'; + +has shim => ( + is => 'ro', + builder => '_build_shim', +); + +has type_map => ( + is => 'ro', + builder => '_build_type_map', +); + +# 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_default { + (defined $_[1]->default_value ? 'DEFAULT ' . q(') . $_[1]->default_value . q(') : () ) +} + +sub field_type { + my ($self, $field) = @_; + + my $field_type = $field->data_type; + ($self->type_map->{$field_type} || $field_type).$self->field_type_size($field) +} + +1; diff --git a/t/71-generator-sql_server.t b/t/71-generator-sql_server.t new file mode 100644 index 0000000..17b0da3 --- /dev/null +++ b/t/71-generator-sql_server.t @@ -0,0 +1,23 @@ +use strict; +use warnings; + +use Test::More; + +use SQL::Translator::Generator::DDL::SQLServer; +use SQL::Translator::Schema::Field; + +my $shim = SQL::Translator::Generator::DDL::SQLServer->new(); + +is $shim->field(SQL::Translator::Schema::Field->new( + name => 'lol', + data_type => 'int', +)), '[lol] int NULL', 'simple field is generated correctly'; + +is $shim->field(SQL::Translator::Schema::Field->new( + name => 'nice', + data_type => 'varchar', + size => 10, +)), '[nice] varchar(10) NULL', 'sized field is generated correctly'; + +done_testing; +