add table_comments
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Generator / Role / DDL.pm
CommitLineData
1ea76bff 1package SQL::Translator::Generator::Role::DDL;
2
3use Moo::Role;
507693db 4use SQL::Translator::Utils qw(header_comment);
1ea76bff 5
6requires '_build_shim';
7requires '_build_type_map';
f699ffaa 8requires '_build_numeric_types';
9requires '_build_unquoted_defaults';
1ea76bff 10requires 'field_type_size';
11
12has shim => (
13 is => 'ro',
38d0ddf0 14 handles => [ 'quote' ],
1ea76bff 15 builder => '_build_shim',
16);
17
18has type_map => (
19 is => 'ro',
20 builder => '_build_type_map',
21);
22
f699ffaa 23has numeric_types => (
24 is => 'ro',
25 builder => '_build_numeric_types',
26);
27
28has unquoted_defaults => (
29 is => 'ro',
30 builder => '_build_unquoted_defaults',
31);
32
f9356e0d 33has add_comments => (
34 is => 'ro',
35);
36
37has add_drop_table => (
38 is => 'ro',
39);
40
1ea76bff 41# would also be handy to have a required size set if there is such a thing
42
38d0ddf0 43sub field_name { $_[0]->quote($_[1]->name) }
1ea76bff 44
f699ffaa 45sub field_comments {
46 ( $_[1]->comments ? ('-- ' . $_[1]->comments . "\n ") : () )
47}
48
f511a415 49sub table_comments {
50 my ($self, $table) = @_;
51 if ($self->add_comments) {
52 return (
53 "\n",
54 "--\n",
55 "-- Table: " . $self->quote($table->name) . "\n",
56 "--\n",
57 map "-- $_\n", $table->comments
58 )
59 } else {
60 return ()
61 }
62}
63
f699ffaa 64sub field_nullable { ($_[1]->is_nullable ? $_[0]->nullable : 'NOT NULL' ) }
1ea76bff 65
66sub field_default {
f699ffaa 67 return () if !defined $_[1]->default_value;
68
69 my $val = $_[1]->default_value;
70 $val = "'$val'" unless $_[0]->numeric_types->{$_[1]->data_type};
71 return ( "DEFAULT $val" )
1ea76bff 72}
73
74sub field_type {
75 my ($self, $field) = @_;
76
77 my $field_type = $field->data_type;
78 ($self->type_map->{$field_type} || $field_type).$self->field_type_size($field)
79}
80
2ce8cf9b 81sub fields {
82 my ($self, $table) = @_;
83 ( map $self->field($_), $table->get_fields )
84}
85
f699ffaa 86sub nullable { 'NULL' }
87
507693db 88sub header_comments { header_comment() . "\n" if $_[0]->add_comments }
89
1ea76bff 901;