Moar documentation, shape up license/copyright notices
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Generator / Role / DDL.pm
CommitLineData
1ea76bff 1package SQL::Translator::Generator::Role::DDL;
2
22c0c10f 3=head1 NAME
4
5SQL::Translator::Generator::Role::DDL - Role implementing common parts of
6DDL generation.
7
8=head1 DESCRIPTION
9
10I<documentation volunteers needed>
11
12=cut
d22073f1 13
1ea76bff 14use Moo::Role;
507693db 15use SQL::Translator::Utils qw(header_comment);
ff6dc6d4 16use Scalar::Util;
1ea76bff 17
1ea76bff 18requires '_build_type_map';
f699ffaa 19requires '_build_numeric_types';
20requires '_build_unquoted_defaults';
4b10273f 21requires '_build_sizeless_types';
39bfaa8b 22requires 'quote';
1ea76bff 23
24has type_map => (
28f1fad7 25 is => 'lazy',
1ea76bff 26);
27
f699ffaa 28has numeric_types => (
28f1fad7 29 is => 'lazy',
f699ffaa 30);
31
4b10273f 32has sizeless_types => (
33 is => 'lazy',
34);
35
f699ffaa 36has unquoted_defaults => (
28f1fad7 37 is => 'lazy',
f699ffaa 38);
39
f9356e0d 40has add_comments => (
41 is => 'ro',
42);
43
44has 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 50sub field_name { $_[0]->quote($_[1]->name) }
1ea76bff 51
f699ffaa 52sub field_comments {
53 ( $_[1]->comments ? ('-- ' . $_[1]->comments . "\n ") : () )
54}
55
f511a415 56sub 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 71sub field_nullable { ($_[1]->is_nullable ? $_[0]->nullable : 'NOT NULL' ) }
1ea76bff 72
73sub 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
89sub 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 96sub 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 105sub fields {
106 my ($self, $table) = @_;
107 ( map $self->field($_), $table->get_fields )
108}
109
3f9e80bf 110sub indices {
111 my ($self, $table) = @_;
112 (map $self->index($_), $table->get_indices)
113}
114
f699ffaa 115sub nullable { 'NULL' }
116
507693db 117sub header_comments { header_comment() . "\n" if $_[0]->add_comments }
118
1ea76bff 1191;
22c0c10f 120
121=head1 AUTHORS
122
123See the included AUTHORS file:
124L<http://search.cpan.org/dist/SQL-Translator/AUTHORS>
125
126=head1 COPYRIGHT
127
128Copyright (c) 2012 the SQL::Translator L</AUTHORS> as listed above.
129
130=head1 LICENSE
131
132This code is free software and may be distributed under the same terms as Perl
133itself.
134
135=cut