File Coverage

blib/lib/SQL/Translator/Generator/Role/DDL.pm
Criterion Covered Total %
statement 34 35 97.1
branch 16 18 88.8
condition 15 15 100.0
subroutine 14 14 100.0
pod 0 11 0.0
total 79 93 84.9


line stmt bran cond sub pod time code
1             package SQL::Translator::Generator::Role::DDL;
2              
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
11              
12             =cut
13              
14 12     12   7621 use Moo::Role;
  12         29  
  12         107  
15 12     12   7316 use SQL::Translator::Utils qw(header_comment);
  12         28  
  12         791  
16 12     12   78 use Scalar::Util;
  12         42  
  12         9782  
17              
18             requires '_build_type_map';
19             requires '_build_numeric_types';
20             requires '_build_unquoted_defaults';
21             requires '_build_sizeless_types';
22             requires 'quote';
23             requires 'quote_string';
24              
25             has type_map => (is => 'lazy',);
26              
27             has numeric_types => (is => 'lazy',);
28              
29             has sizeless_types => (is => 'lazy',);
30              
31             has unquoted_defaults => (is => 'lazy',);
32              
33             has add_comments => (is => 'ro',);
34              
35             has add_drop_table => (is => 'ro',);
36              
37             # would also be handy to have a required size set if there is such a thing
38              
39 147     147 0 4710 sub field_name { $_[0]->quote($_[1]->name) }
40              
41             sub field_comments {
42 112 100   112 0 3150 ($_[1]->comments ? ('-- ' . $_[1]->comments . "\n ") : ())
43             }
44              
45             sub table_comments {
46 10     10 0 28 my ($self, $table) = @_;
47 10 50       60 if ($self->add_comments) {
48 0         0 return ("", "--", "-- Table: " . $self->quote($table->name) . "", "--", map "-- $_", $table->comments);
49             } else {
50 10         284 return ();
51             }
52             }
53              
54 147 100   147 0 6548 sub field_nullable { ($_[1]->is_nullable ? $_[0]->nullable : 'NOT NULL') }
55              
56             sub field_default {
57 147     147 0 2931 my ($self, $field, $exceptions) = @_;
58              
59 147         505 my $default = $field->default_value;
60 147 100       1292 return () if !defined $default;
61              
62             $default = \"$default"
63             if $exceptions
64             and !ref $default
65 59 100 100     414 and $exceptions->{$default};
      100        
66 59 100 100     1171 if (ref $default) {
    100          
67 8         20 $default = $$default;
68             } elsif (!($self->numeric_types->{ lc($field->data_type) } && Scalar::Util::looks_like_number($default))) {
69 32         444 $default = $self->quote_string($default);
70             }
71 59         941 return ("DEFAULT $default");
72             }
73              
74             sub field_type {
75 127     127 0 3273 my ($self, $field) = @_;
76              
77 127         506 my $field_type = $field->data_type;
78 127   100     2857 ($self->type_map->{$field_type} || $field_type) . $self->field_type_size($field);
79             }
80              
81             sub field_type_size {
82 127     127 0 1392 my ($self, $field) = @_;
83              
84             (
85 127 100 100     3238 $field->size && !$self->sizeless_types->{ $field->data_type }
86             ? '(' . $field->size . ')'
87             : ''
88             );
89             }
90              
91             sub fields {
92 10     10 0 29 my ($self, $table) = @_;
93 10         53 (map $self->field($_), $table->get_fields);
94             }
95              
96             sub indices {
97 10     10 0 153 my ($self, $table) = @_;
98 10         43 (map $self->index($_), $table->get_indices);
99             }
100              
101 23     23 0 1478 sub nullable {'NULL'}
102              
103 3 50   3 0 40 sub header_comments { header_comment() . "\n" if $_[0]->add_comments }
104              
105             1;
106              
107             =head1 AUTHORS
108              
109             See the included AUTHORS file:
110             L
111              
112             =head1 COPYRIGHT
113              
114             Copyright (c) 2012 the SQL::Translator L as listed above.
115              
116             =head1 LICENSE
117              
118             This code is free software and may be distributed under the same terms as Perl
119             itself.
120              
121             =cut