Commit | Line | Data |
1ea76bff |
1 | package SQL::Translator::Generator::Role::DDL; |
2 | |
22c0c10f |
3 | =head1 NAME |
4 | |
5 | SQL::Translator::Generator::Role::DDL - Role implementing common parts of |
6 | DDL generation. |
7 | |
8 | =head1 DESCRIPTION |
9 | |
10 | I<documentation volunteers needed> |
11 | |
12 | =cut |
d22073f1 |
13 | |
1ea76bff |
14 | use Moo::Role; |
507693db |
15 | use SQL::Translator::Utils qw(header_comment); |
ff6dc6d4 |
16 | use Scalar::Util; |
1ea76bff |
17 | |
1ea76bff |
18 | requires '_build_type_map'; |
f699ffaa |
19 | requires '_build_numeric_types'; |
20 | requires '_build_unquoted_defaults'; |
4b10273f |
21 | requires '_build_sizeless_types'; |
39bfaa8b |
22 | requires 'quote'; |
1ea76bff |
23 | |
24 | has type_map => ( |
28f1fad7 |
25 | is => 'lazy', |
1ea76bff |
26 | ); |
27 | |
f699ffaa |
28 | has numeric_types => ( |
28f1fad7 |
29 | is => 'lazy', |
f699ffaa |
30 | ); |
31 | |
4b10273f |
32 | has sizeless_types => ( |
33 | is => 'lazy', |
34 | ); |
35 | |
f699ffaa |
36 | has unquoted_defaults => ( |
28f1fad7 |
37 | is => 'lazy', |
f699ffaa |
38 | ); |
39 | |
f9356e0d |
40 | has add_comments => ( |
41 | is => 'ro', |
42 | ); |
43 | |
44 | has add_drop_table => ( |
45 | is => 'ro', |
46 | ); |
47 | |
1ea76bff |
48 | # would also be handy to have a required size set if there is such a thing |
49 | |
38d0ddf0 |
50 | sub field_name { $_[0]->quote($_[1]->name) } |
1ea76bff |
51 | |
f699ffaa |
52 | sub field_comments { |
53 | ( $_[1]->comments ? ('-- ' . $_[1]->comments . "\n ") : () ) |
54 | } |
55 | |
f511a415 |
56 | sub table_comments { |
57 | my ($self, $table) = @_; |
58 | if ($self->add_comments) { |
59 | return ( |
9a6c1bf9 |
60 | "", |
61 | "--", |
62 | "-- Table: " . $self->quote($table->name) . "", |
63 | "--", |
64 | map "-- $_", $table->comments |
f511a415 |
65 | ) |
66 | } else { |
67 | return () |
68 | } |
69 | } |
70 | |
f699ffaa |
71 | sub field_nullable { ($_[1]->is_nullable ? $_[0]->nullable : 'NOT NULL' ) } |
1ea76bff |
72 | |
73 | sub field_default { |
ff6dc6d4 |
74 | my ($self, $field, $exceptions) = @_; |
75 | |
76 | my $default = $field->default_value; |
77 | return () if !defined $default; |
78 | |
79 | $default = \"$default" |
80 | if $exceptions and !ref $default and $exceptions->{$default}; |
81 | if (ref $default) { |
82 | $default = $$default; |
83 | } elsif (!($self->numeric_types->{lc($field->data_type)} && Scalar::Util::looks_like_number ($default))) { |
84 | $default = "'$default'"; |
85 | } |
86 | return ( "DEFAULT $default" ) |
1ea76bff |
87 | } |
88 | |
89 | sub field_type { |
90 | my ($self, $field) = @_; |
91 | |
92 | my $field_type = $field->data_type; |
93 | ($self->type_map->{$field_type} || $field_type).$self->field_type_size($field) |
94 | } |
95 | |
4b10273f |
96 | sub field_type_size { |
97 | my ($self, $field) = @_; |
98 | |
99 | ($field->size && !$self->sizeless_types->{$field->data_type} |
100 | ? '(' . $field->size . ')' |
101 | : '' |
102 | ) |
103 | } |
104 | |
2ce8cf9b |
105 | sub fields { |
106 | my ($self, $table) = @_; |
107 | ( map $self->field($_), $table->get_fields ) |
108 | } |
109 | |
3f9e80bf |
110 | sub indices { |
111 | my ($self, $table) = @_; |
112 | (map $self->index($_), $table->get_indices) |
113 | } |
114 | |
f699ffaa |
115 | sub nullable { 'NULL' } |
116 | |
507693db |
117 | sub header_comments { header_comment() . "\n" if $_[0]->add_comments } |
118 | |
1ea76bff |
119 | 1; |
22c0c10f |
120 | |
121 | =head1 AUTHORS |
122 | |
123 | See the included AUTHORS file: |
124 | L<http://search.cpan.org/dist/SQL-Translator/AUTHORS> |
125 | |
126 | =head1 COPYRIGHT |
127 | |
128 | Copyright (c) 2012 the SQL::Translator L</AUTHORS> as listed above. |
129 | |
130 | =head1 LICENSE |
131 | |
132 | This code is free software and may be distributed under the same terms as Perl |
133 | itself. |
134 | |
135 | =cut |