package SQL::Translator::Generator::Role::DDL;
+=head1 NAME
+
+SQL::Translator::Generator::Role::DDL - Role implementing common parts of
+DDL generation.
+
+=head1 DESCRIPTION
+
+I<documentation volunteers needed>
+
+=cut
+
use Moo::Role;
use SQL::Translator::Utils qw(header_comment);
+use Scalar::Util;
requires '_build_type_map';
requires '_build_numeric_types';
requires '_build_unquoted_defaults';
requires '_build_sizeless_types';
requires 'quote';
+requires 'quote_string';
has type_map => (
is => 'lazy',
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 = $self->quote_string($default);
+ }
+ return ( "DEFAULT $default" )
}
sub field_type {
sub header_comments { header_comment() . "\n" if $_[0]->add_comments }
1;
+
+=head1 AUTHORS
+
+See the included AUTHORS file:
+L<http://search.cpan.org/dist/SQL-Translator/AUTHORS>
+
+=head1 COPYRIGHT
+
+Copyright (c) 2012 the SQL::Translator L</AUTHORS> as listed above.
+
+=head1 LICENSE
+
+This code is free software and may be distributed under the same terms as Perl
+itself.
+
+=cut