assign copyright to my new files
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Generator / Role / DDL.pm
1 package SQL::Translator::Generator::Role::DDL;
2
3 # AUTHOR: Arthur Axel fREW Schmidt
4 # Copyright: Same as Perl 5
5
6 use Moo::Role;
7 use SQL::Translator::Utils qw(header_comment);
8
9 requires '_build_type_map';
10 requires '_build_numeric_types';
11 requires '_build_unquoted_defaults';
12 requires '_build_sizeless_types';
13 requires 'quote';
14
15 has type_map => (
16    is => 'lazy',
17 );
18
19 has numeric_types => (
20    is => 'lazy',
21 );
22
23 has sizeless_types => (
24    is => 'lazy',
25 );
26
27 has unquoted_defaults => (
28    is => 'lazy',
29 );
30
31 has add_comments => (
32    is => 'ro',
33 );
34
35 has add_drop_table => (
36    is => 'ro',
37 );
38
39 # would also be handy to have a required size set if there is such a thing
40
41 sub field_name { $_[0]->quote($_[1]->name) }
42
43 sub field_comments {
44    ( $_[1]->comments ? ('-- ' . $_[1]->comments . "\n ") : () )
45 }
46
47 sub table_comments {
48    my ($self, $table) = @_;
49    if ($self->add_comments) {
50       return (
51          "",
52          "--",
53          "-- Table: " . $self->quote($table->name) . "",
54          "--",
55          map "-- $_", $table->comments
56       )
57    } else {
58       return ()
59    }
60 }
61
62 sub field_nullable { ($_[1]->is_nullable ? $_[0]->nullable : 'NOT NULL' ) }
63
64 sub field_default {
65   return () if !defined $_[1]->default_value;
66
67   my $val = $_[1]->default_value;
68   $val = "'$val'" unless $_[0]->numeric_types->{$_[1]->data_type};
69   return ( "DEFAULT $val" )
70 }
71
72 sub field_type {
73    my ($self, $field) = @_;
74
75    my $field_type = $field->data_type;
76    ($self->type_map->{$field_type} || $field_type).$self->field_type_size($field)
77 }
78
79 sub field_type_size {
80    my ($self, $field) = @_;
81
82    ($field->size && !$self->sizeless_types->{$field->data_type}
83       ? '(' . $field->size . ')'
84       : ''
85    )
86 }
87
88 sub fields {
89   my ($self, $table) = @_;
90   ( map $self->field($_), $table->get_fields )
91 }
92
93 sub indices {
94   my ($self, $table) = @_;
95   (map $self->index($_), $table->get_indices)
96 }
97
98 sub nullable { 'NULL' }
99
100 sub header_comments { header_comment() . "\n" if $_[0]->add_comments }
101
102 1;