File Coverage

blib/lib/SQL/Translator/Producer.pm
Criterion Covered Total %
statement 28 31 90.3
branch 11 12 91.6
condition 5 9 55.5
subroutine 5 6 83.3
pod 1 1 100.0
total 50 59 84.7


line stmt bran cond sub pod time code
1             package SQL::Translator::Producer;
2              
3 69     69   523 use strict;
  69         256  
  69         2238  
4 69     69   422 use warnings;
  69         148  
  69         1778  
5 69     69   369 use Scalar::Util ();
  69         169  
  69         29277  
6             our $VERSION = '1.62';
7              
8 0     0 1 0 sub produce { "" }
9              
10             # Do not rely on this if you are not bundled with SQL::Translator.
11             # -- rjbs, 2008-09-30
12             ## $exceptions contains an arrayref of paired values
13             ## Each pair contains a pattern match or string, and a value to be used as
14             ## the default if matched.
15             ## They are special per Producer, and provide support for the old 'now()'
16             ## default value exceptions
17             sub _apply_default_value {
18 385     385   1142 my ($self, $field, $field_ref, $exceptions) = @_;
19 385         1049 my $default = $field->default_value;
20 385 100       1222 return if !defined $default;
21              
22 107 100 66     527 if ($exceptions and ! ref $default) {
23 99         330 for (my $i = 0; $i < @$exceptions; $i += 2) {
24 149         392 my ($pat, $val) = @$exceptions[ $i, $i + 1 ];
25 149 50 33     763 if (ref $pat and $default =~ $pat) {
    100          
26 0         0 $default = $val;
27 0         0 last;
28             } elsif (lc $default eq lc $pat) {
29 1         3 $default = $val;
30             last
31 1         3 }
32             }
33             }
34              
35 107         333 my $type = lc $field->data_type;
36 107         599 my $is_numeric_datatype = ($type =~ /^(?:(?:big|medium|small|tiny)?int(?:eger)?|decimal|double|float|num(?:ber|eric)?|real)$/);
37              
38 107 100 66     585 if (ref $default) {
    100          
39 9         36 $$field_ref .= " DEFAULT $$default";
40             } elsif ($is_numeric_datatype && Scalar::Util::looks_like_number ($default) ) {
41             # we need to check the data itself in addition to the datatype, for basic safety
42 38         175 $$field_ref .= " DEFAULT $default";
43             } else {
44 60         223 $default = $self->_quote_string($default);
45 60         231 $$field_ref .= " DEFAULT $default";
46             }
47              
48             }
49              
50             sub _quote_string {
51 122     122   371 my ($self, $string) = @_;
52 122         323 $string =~ s/'/''/g;
53 122         644 return qq{'$string'};
54             }
55              
56             1;
57              
58             # -------------------------------------------------------------------
59             # A burnt child loves the fire.
60             # Oscar Wilde
61             # -------------------------------------------------------------------
62              
63             =pod
64              
65             =head1 NAME
66              
67             SQL::Translator::Producer - describes how to write a producer
68              
69             =head1 DESCRIPTION
70              
71             Producer modules designed to be used with SQL::Translator need to
72             implement a single function, called B. B will be
73             called with the SQL::Translator object from which it is expected to
74             retrieve the SQL::Translator::Schema object which has been populated
75             by the parser. It is expected to return a string.
76              
77             =head1 METHODS
78              
79             =over 4
80              
81             =item produce
82              
83             =item create_table($table)
84              
85             =item create_field($field)
86              
87             =item create_view($view)
88              
89             =item create_index($index)
90              
91             =item create_constraint($constraint)
92              
93             =item create_trigger($trigger)
94              
95             =item alter_field($from_field, $to_field)
96              
97             =item add_field($table, $new_field)
98              
99             =item drop_field($table, $old_field)
100              
101             =back
102              
103             =head1 AUTHORS
104              
105             Darren Chamberlain Edarren@cpan.orgE,
106             Ken Y. Clark Ekclark@cpan.orgE.
107              
108             =head1 SEE ALSO
109              
110             perl(1), SQL::Translator, SQL::Translator::Schema.
111              
112             =cut