use Moo::Role;
use SQL::Translator::Utils qw(header_comment);
+use Scalar::Util;
requires '_build_type_map';
requires '_build_numeric_types';
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 {
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<CREATE TABLE "foo_table" (\n "id" integer DEFAULT gunshow\n)>];
+ 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<CREATE TABLE "foo_table" (\n "id" integer DEFAULT 'frew'\n)>];
+ 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<CREATE TABLE "foo" (
+ "id" integer DEFAULT NULL,
+ "when" DEFAULT now(),
+ "at" DEFAULT CURRENT_TIMESTAMP
+)>];
+ my $result = [SQL::Translator::Producer::SQLite::create_table($table, { no_comments => 1 })];
+ is_deeply($result, $expected, 'correctly unquoted excempted DEFAULTs');
+}
+
done_testing;