# ----------------------------------------------------------
#
# ----------------------------------------------------------
+* Applied patch from rjbs with minor changes, now we support scalar refs in default values!
* Fixed SQLite producer to end index statements in newlines, in scalar context
* Decreed that all list context statements shall not end in ; or ;\n
* Fixed SQLite, Diff and MySQL producers to agree with Decree.
sub produce { "" }
+# Do not rely on this if you are not bundled with SQL::Translator.
+# -- rjbs, 2008-09-30
+## $exceptions contains an arrayref of paired values
+## Each pair contains a pattern match or string, and a value to be used as
+## the default if matched.
+## They are special per Producer, and provide support for the old 'now()'
+## default value exceptions
+sub _apply_default_value {
+ my (undef, $field_ref, $default, $exceptions) = @_;
+
+ if ($exceptions and ! ref $default) {
+ for (my $i = 0; $i < @$exceptions; $i += 2) {
+ my ($pat, $val) = @$exceptions[ $i, $i + 1 ];
+ if (ref $pat and $default =~ $pat) {
+ $default = $val;
+ last;
+ } elsif (lc $default eq lc $pat) {
+ $default = $val;
+ last
+ }
+ }
+ }
+
+ if (ref $default) {
+ $$field_ref .= " DEFAULT $$default";
+ } else {
+ $$field_ref .= " DEFAULT '$default'";
+ }
+
+}
+
1;
# -------------------------------------------------------------------
# Default? XXX Need better quoting!
my $default = $field->default_value;
if ( defined $default ) {
- if ( uc $default eq 'NULL') {
- $field_def .= ' DEFAULT NULL';
- } else {
- $field_def .= " DEFAULT '$default'";
- }
+ SQL::Translator::Producer->_apply_default_value(
+ \$field_def,
+ $default,
+ [
+ 'NULL' => \'NULL',
+ ],
+ );
}
if ( my $comments = $field->comments ) {
# then sub "1/0," otherwise just test the truthity of the
# argument and use that (naive?).
#
- if (
+ if (ref $default and defined $$default) {
+ $default = $$default;
+ } elsif (ref $default) {
+ $default = 'NULL';
+ } elsif (
$data_type =~ /^number$/i &&
$default !~ /^-?\d+$/ &&
$default !~ m/null/i
my $list = $extra{'list'} || [];
# todo deal with embedded quotes
my $commalist = join( ', ', map { qq['$_'] } @$list );
- my $seq_name;
if ($postgres_version >= 8.3 && $field->data_type eq 'enum') {
my $type_name = $field->table->name . '_' . $field->name . '_type';
}
#
- # Default value -- disallow for timestamps
+ # Default value
#
-# my $default = $data_type =~ /(timestamp|date)/i
-# ? undef : $field->default_value;
my $default = $field->default_value;
if ( defined $default ) {
- my $qd = "'";
- $qd = '' if ($default eq 'now()' ||
- $default eq 'CURRENT_TIMESTAMP');
- $field_def .= sprintf( ' DEFAULT %s',
- ( $field->is_auto_increment && $seq_name )
- ? qq[nextval('"$seq_name"'::text)] :
- ( $default =~ m/null/i ) ? 'NULL' : "$qd$default$qd"
- );
+ SQL::Translator::Producer->_apply_default_value(
+ \$field_def,
+ $default,
+ [
+ 'NULL' => \'NULL',
+ 'now()' => 'now()',
+ 'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP',
+ ],
+ );
}
#
my $list = $extra{'list'} || [];
# \todo deal with embedded quotes
my $commalist = join( ', ', map { qq['$_'] } @$list );
- my $seq_name;
if ( $data_type eq 'enum' ) {
my $check_name = mk_name(
#
my $default = $field->default_value;
if ( defined $default ) {
- $field_def .= sprintf( ' DEFAULT %s',
- ( $field->is_auto_increment && $seq_name )
- ? qq[nextval('"$seq_name"'::text)] :
- ( $default =~ m/null/i ) ? 'NULL' : "'$default'"
+ SQL::Translator::Producer->_apply_default_value(
+ \$field_def,
+ $default,
+ [
+ 'NULL' => \'NULL',
+ ],
);
}
-
+
push @field_defs, $field_def;
}
# Default? XXX Need better quoting!
my $default = $field->default_value;
- if ( defined $default ) {
- if ( uc $default eq 'NULL') {
- $field_def .= ' DEFAULT NULL';
- } elsif ( $default eq 'now()' ||
- $default eq 'CURRENT_TIMESTAMP' ) {
- $field_def .= ' DEFAULT CURRENT_TIMESTAMP';
- } elsif ( $default =~ /val\(/ ) {
- next;
- } else {
- $field_def .= " DEFAULT '$default'";
- }
+ if (defined $default) {
+ SQL::Translator::Producer->_apply_default_value(
+ \$field_def,
+ $default,
+ [
+ 'NULL' => \'NULL',
+ 'now()' => 'now()',
+ 'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP',
+ ],
+ );
}
return $field_def;
#=============================================================================
BEGIN {
- maybe_plan(9,
+ maybe_plan(13,
'SQL::Translator::Producer::PostgreSQL',
'Test::Differences',
)
use Test::Differences;
use SQL::Translator;
-
+my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field;
my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
+{
+ # let's test default values! -- rjbs, 2008-09-30
+ my %field = (
+ table => $table,
+ data_type => 'VARCHAR',
+ size => 10,
+ is_auto_increment => 0,
+ is_nullable => 1,
+ is_foreign_key => 0,
+ is_unique => 0,
+ );
+
+ {
+ my $simple_default = SQL::Translator::Schema::Field->new(
+ %field,
+ name => 'str_default',
+ default_value => 'foo',
+ );
+
+ is(
+ $PRODUCER->($simple_default),
+ q{str_default character varying(10) DEFAULT 'foo'},
+ 'default str',
+ );
+ }
+
+ {
+ my $null_default = SQL::Translator::Schema::Field->new(
+ %field,
+ name => 'null_default',
+ default_value => \'NULL',
+ );
+
+ is(
+ $PRODUCER->($null_default),
+ q{null_default character varying(10) DEFAULT NULL},
+ 'default null',
+ );
+ }
+
+ {
+ my $null_default = SQL::Translator::Schema::Field->new(
+ %field,
+ name => 'null_default_2',
+ default_value => 'NULL', # XXX: this should go away
+ );
+
+ is(
+ $PRODUCER->($null_default),
+ q{null_default_2 character varying(10) DEFAULT NULL},
+ 'default null from special cased string',
+ );
+ }
+
+ {
+ my $func_default = SQL::Translator::Schema::Field->new(
+ %field,
+ name => 'func_default',
+ default_value => \'func(funky)',
+ );
+
+ is(
+ $PRODUCER->($func_default),
+ q{func_default character varying(10) DEFAULT func(funky)},
+ 'unquoted default from scalar ref',
+ );
+ }
+}
+
+
my $view1 = SQL::Translator::Schema::View->new(
name => 'view_foo',
fields => [qw/id name/],