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