Commit | Line | Data |
1ea76bff |
1 | package SQL::Translator::Generator::Role::DDL; |
2 | |
d22073f1 |
3 | # AUTHOR: Arthur Axel fREW Schmidt |
4 | # Copyright: Same as Perl 5 |
5 | |
1ea76bff |
6 | use Moo::Role; |
507693db |
7 | use SQL::Translator::Utils qw(header_comment); |
1ea76bff |
8 | |
1ea76bff |
9 | requires '_build_type_map'; |
f699ffaa |
10 | requires '_build_numeric_types'; |
11 | requires '_build_unquoted_defaults'; |
4b10273f |
12 | requires '_build_sizeless_types'; |
39bfaa8b |
13 | requires 'quote'; |
1ea76bff |
14 | |
15 | has type_map => ( |
28f1fad7 |
16 | is => 'lazy', |
1ea76bff |
17 | ); |
18 | |
f699ffaa |
19 | has numeric_types => ( |
28f1fad7 |
20 | is => 'lazy', |
f699ffaa |
21 | ); |
22 | |
4b10273f |
23 | has sizeless_types => ( |
24 | is => 'lazy', |
25 | ); |
26 | |
f699ffaa |
27 | has unquoted_defaults => ( |
28f1fad7 |
28 | is => 'lazy', |
f699ffaa |
29 | ); |
30 | |
f9356e0d |
31 | has add_comments => ( |
32 | is => 'ro', |
33 | ); |
34 | |
35 | has add_drop_table => ( |
36 | is => 'ro', |
37 | ); |
38 | |
1ea76bff |
39 | # would also be handy to have a required size set if there is such a thing |
40 | |
38d0ddf0 |
41 | sub field_name { $_[0]->quote($_[1]->name) } |
1ea76bff |
42 | |
f699ffaa |
43 | sub field_comments { |
44 | ( $_[1]->comments ? ('-- ' . $_[1]->comments . "\n ") : () ) |
45 | } |
46 | |
f511a415 |
47 | sub table_comments { |
48 | my ($self, $table) = @_; |
49 | if ($self->add_comments) { |
50 | return ( |
9a6c1bf9 |
51 | "", |
52 | "--", |
53 | "-- Table: " . $self->quote($table->name) . "", |
54 | "--", |
55 | map "-- $_", $table->comments |
f511a415 |
56 | ) |
57 | } else { |
58 | return () |
59 | } |
60 | } |
61 | |
f699ffaa |
62 | sub field_nullable { ($_[1]->is_nullable ? $_[0]->nullable : 'NOT NULL' ) } |
1ea76bff |
63 | |
64 | sub field_default { |
f699ffaa |
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" ) |
1ea76bff |
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 | |
4b10273f |
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 | |
2ce8cf9b |
88 | sub fields { |
89 | my ($self, $table) = @_; |
90 | ( map $self->field($_), $table->get_fields ) |
91 | } |
92 | |
3f9e80bf |
93 | sub indices { |
94 | my ($self, $table) = @_; |
95 | (map $self->index($_), $table->get_indices) |
96 | } |
97 | |
f699ffaa |
98 | sub nullable { 'NULL' } |
99 | |
507693db |
100 | sub header_comments { header_comment() . "\n" if $_[0]->add_comments } |
101 | |
1ea76bff |
102 | 1; |