Commit | Line | Data |
1ea76bff |
1 | package SQL::Translator::Generator::Role::DDL; |
2 | |
3 | use Moo::Role; |
507693db |
4 | use SQL::Translator::Utils qw(header_comment); |
1ea76bff |
5 | |
6 | requires '_build_shim'; |
7 | requires '_build_type_map'; |
f699ffaa |
8 | requires '_build_numeric_types'; |
9 | requires '_build_unquoted_defaults'; |
1ea76bff |
10 | requires 'field_type_size'; |
11 | |
12 | has shim => ( |
13 | is => 'ro', |
38d0ddf0 |
14 | handles => [ 'quote' ], |
1ea76bff |
15 | builder => '_build_shim', |
16 | ); |
17 | |
18 | has type_map => ( |
19 | is => 'ro', |
20 | builder => '_build_type_map', |
21 | ); |
22 | |
f699ffaa |
23 | has numeric_types => ( |
24 | is => 'ro', |
25 | builder => '_build_numeric_types', |
26 | ); |
27 | |
28 | has unquoted_defaults => ( |
29 | is => 'ro', |
30 | builder => '_build_unquoted_defaults', |
31 | ); |
32 | |
f9356e0d |
33 | has add_comments => ( |
34 | is => 'ro', |
35 | ); |
36 | |
37 | has 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 |
43 | sub field_name { $_[0]->quote($_[1]->name) } |
1ea76bff |
44 | |
f699ffaa |
45 | sub field_comments { |
46 | ( $_[1]->comments ? ('-- ' . $_[1]->comments . "\n ") : () ) |
47 | } |
48 | |
f511a415 |
49 | sub 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 |
64 | sub field_nullable { ($_[1]->is_nullable ? $_[0]->nullable : 'NOT NULL' ) } |
1ea76bff |
65 | |
66 | sub 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 | |
74 | sub 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 |
81 | sub fields { |
82 | my ($self, $table) = @_; |
83 | ( map $self->field($_), $table->get_fields ) |
84 | } |
85 | |
f699ffaa |
86 | sub nullable { 'NULL' } |
87 | |
507693db |
88 | sub header_comments { header_comment() . "\n" if $_[0]->add_comments } |
89 | |
1ea76bff |
90 | 1; |