From: Arthur Axel 'fREW' Schmidt Date: Mon, 30 Apr 2012 16:03:31 +0000 (-0500) Subject: fix excepted and scalarref quoting for DEFAULTS in SQLite (and SQL Server) X-Git-Tag: v0.11011~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ff6dc6d436d02a95aee6ecb876eede74423b3352;p=dbsrgits%2FSQL-Translator.git fix excepted and scalarref quoting for DEFAULTS in SQLite (and SQL Server) --- diff --git a/lib/SQL/Translator/Generator/DDL/SQLite.pm b/lib/SQL/Translator/Generator/DDL/SQLite.pm index ec2febe..eaf147a 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLite.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLite.pm @@ -81,7 +81,11 @@ sub field { : ( $self->field_type($field) ) ), $self->field_nullable($field), - $self->field_default($field), + $self->field_default($field, { + NULL => 1, + 'now()' => 1, + 'CURRENT_TIMESTAMP' => 1, + }), } 1; diff --git a/lib/SQL/Translator/Generator/Role/DDL.pm b/lib/SQL/Translator/Generator/Role/DDL.pm index d1fd536..1691467 100644 --- a/lib/SQL/Translator/Generator/Role/DDL.pm +++ b/lib/SQL/Translator/Generator/Role/DDL.pm @@ -5,6 +5,7 @@ package SQL::Translator::Generator::Role::DDL; use Moo::Role; use SQL::Translator::Utils qw(header_comment); +use Scalar::Util; requires '_build_type_map'; requires '_build_numeric_types'; @@ -62,11 +63,19 @@ sub table_comments { sub field_nullable { ($_[1]->is_nullable ? $_[0]->nullable : 'NOT NULL' ) } sub field_default { - return () if !defined $_[1]->default_value; - - my $val = $_[1]->default_value; - $val = "'$val'" unless $_[0]->numeric_types->{$_[1]->data_type}; - return ( "DEFAULT $val" ) + my ($self, $field, $exceptions) = @_; + + my $default = $field->default_value; + return () if !defined $default; + + $default = \"$default" + if $exceptions and !ref $default and $exceptions->{$default}; + if (ref $default) { + $default = $$default; + } elsif (!($self->numeric_types->{lc($field->data_type)} && Scalar::Util::looks_like_number ($default))) { + $default = "'$default'"; + } + return ( "DEFAULT $default" ) } sub field_type { diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t index 79e7880..5d56adf 100644 --- a/t/56-sqlite-producer.t +++ b/t/56-sqlite-producer.t @@ -110,4 +110,58 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0; is_deeply($result, $expected, 'correctly translated bytea to blob'); } +{ + my $table = SQL::Translator::Schema::Table->new( + name => 'foo_table', + ); + $table->add_field( + name => 'id', + data_type => 'integer', + default_value => \'gunshow', + ); + my $expected = [ qq]; + my $result = [SQL::Translator::Producer::SQLite::create_table($table, { no_comments => 1 })]; + is_deeply($result, $expected, 'correctly unquoted DEFAULT'); +} + +{ + my $table = SQL::Translator::Schema::Table->new( + name => 'foo_table', + ); + $table->add_field( + name => 'id', + data_type => 'integer', + default_value => 'frew', + ); + my $expected = [ qq]; + my $result = [SQL::Translator::Producer::SQLite::create_table($table, { no_comments => 1 })]; + is_deeply($result, $expected, 'correctly quoted DEFAULT'); +} + +{ + my $table = SQL::Translator::Schema::Table->new( + name => 'foo', + ); + $table->add_field( + name => 'id', + data_type => 'integer', + default_value => 'NULL', + ); + $table->add_field( + name => 'when', + default_value => 'now()', + ); + $table->add_field( + name => 'at', + default_value => 'CURRENT_TIMESTAMP', + ); + my $expected = [ qq]; + my $result = [SQL::Translator::Producer::SQLite::create_table($table, { no_comments => 1 })]; + is_deeply($result, $expected, 'correctly unquoted excempted DEFAULTs'); +} + done_testing;