From: Justin Hunter Date: Wed, 10 Feb 2010 23:32:05 +0000 (-0800) Subject: add (and use) _default_value method, relatively similar to old _apply_default_value X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b62122e666f4975b52ffad3386fdcc83554c847b;p=dbsrgits%2FSQL-Translator-2.0-ish.git add (and use) _default_value method, relatively similar to old _apply_default_value --- diff --git a/lib/SQL/Translator/Producer.pm b/lib/SQL/Translator/Producer.pm index f9ca01a..7363ee9 100644 --- a/lib/SQL/Translator/Producer.pm +++ b/lib/SQL/Translator/Producer.pm @@ -1,7 +1,7 @@ use MooseX::Declare; class SQL::Translator::Producer { use SQL::Translator::Constants qw(:sqlt_types); - use MooseX::Types::Moose qw(Bool HashRef Str); + use MooseX::Types::Moose qw(ArrayRef Bool HashRef ScalarRef Str); use SQL::Translator::Types qw(Column Table Translator); has 'data_type_mapping' => ( @@ -61,4 +61,21 @@ class SQL::Translator::Producer { $column_def .= ' NOT NULL' unless $column->is_nullable; $column_def; } + + method _default_value(ScalarRef|Str $default, ArrayRef $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 + } + } + } + + return ref($default) ? " DEFAULT $$default" : " DEFAULT '$default'"; + } } diff --git a/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm b/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm index 69b0d4d..6bef033 100644 --- a/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/SQL/PostgreSQL.pm @@ -1,5 +1,5 @@ use MooseX::Declare; -role SQL::Translator::Producer::SQL::PostgreSQL { +role SQL::Translator::Producer::SQL::PostgreSQL { use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants); use SQL::Translator::Types qw(Column Constraint Index Table View); my ( %index_name ); @@ -332,7 +332,6 @@ method create_view(View $view, $options?) { my $add_drop_view = $options->{add_drop_view}; my $view_name = $view->name; -# debug("PKG: Looking at view '${view_name}'\n"); my $create = ''; $create .= "--\n-- View: ${qt}${view_name}${qt}\n--\n" @@ -406,18 +405,7 @@ method create_view(View $view, $options?) { # Default value # my $default = $field->default_value; - -# if ( defined $default ) { -# SQL::Translator::Producer->_apply_default_value( -# \$field_def, -# $default, -# [ -# 'NULL' => \'NULL', -# 'now()' => 'now()', -# 'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP', -# ], -# ); -# } + $field_def .= $self->_default_value($default, [ 'NULL'=> \'NULL', 'now()' => 'now()', 'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP', ] ) if defined $default; # # Not null constraint