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