Commit | Line | Data |
16dc9970 |
1 | package SQL::Translator::Producer; |
2 | |
077ebf34 |
3 | use strict; |
f27f9229 |
4 | use warnings; |
06baeb21 |
5 | use Scalar::Util (); |
e0583e2c |
6 | our $VERSION = '1.59_01'; |
16dc9970 |
7 | |
077ebf34 |
8 | sub produce { "" } |
16dc9970 |
9 | |
bc8e2aa1 |
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 { |
1868ddbe |
18 | my ($self, $field, $field_ref, $exceptions) = @_; |
06baeb21 |
19 | my $default = $field->default_value; |
20 | return if !defined $default; |
bc8e2aa1 |
21 | |
22 | if ($exceptions and ! ref $default) { |
23 | for (my $i = 0; $i < @$exceptions; $i += 2) { |
24 | my ($pat, $val) = @$exceptions[ $i, $i + 1 ]; |
25 | if (ref $pat and $default =~ $pat) { |
26 | $default = $val; |
27 | last; |
28 | } elsif (lc $default eq lc $pat) { |
29 | $default = $val; |
30 | last |
31 | } |
32 | } |
33 | } |
34 | |
06baeb21 |
35 | my $type = lc $field->data_type; |
36 | my $is_numeric_datatype = ($type =~ /^(?:(?:big|medium|small|tiny)?int(?:eger)?|decimal|double|float|num(?:ber|eric)?|real)$/); |
37 | |
bc8e2aa1 |
38 | if (ref $default) { |
39 | $$field_ref .= " DEFAULT $$default"; |
06baeb21 |
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 | $$field_ref .= " DEFAULT $default"; |
bc8e2aa1 |
43 | } else { |
1868ddbe |
44 | $default = $self->_quote_string($default); |
45 | $$field_ref .= " DEFAULT $default"; |
bc8e2aa1 |
46 | } |
47 | |
48 | } |
49 | |
1868ddbe |
50 | sub _quote_string { |
51 | my ($self, $string) = @_; |
52 | $string =~ s/'/''/g; |
53 | return qq{'$string'}; |
54 | } |
55 | |
16dc9970 |
56 | 1; |
57 | |
49e1eb70 |
58 | # ------------------------------------------------------------------- |
16dc9970 |
59 | # A burnt child loves the fire. |
60 | # Oscar Wilde |
49e1eb70 |
61 | # ------------------------------------------------------------------- |
62 | |
63 | =pod |
16dc9970 |
64 | |
65 | =head1 NAME |
66 | |
56435d6f |
67 | SQL::Translator::Producer - describes how to write a producer |
16dc9970 |
68 | |
16dc9970 |
69 | =head1 DESCRIPTION |
70 | |
077ebf34 |
71 | Producer modules designed to be used with SQL::Translator need to |
72 | implement a single function, called B<produce>. B<produce> will be |
ea93df61 |
73 | called with the SQL::Translator object from which it is expected to |
74 | retrieve the SQL::Translator::Schema object which has been populated |
56435d6f |
75 | by the parser. It is expected to return a string. |
16dc9970 |
76 | |
0e1ec14f |
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 | |
0e42fda6 |
101 | =back |
102 | |
56435d6f |
103 | =head1 AUTHORS |
16dc9970 |
104 | |
56435d6f |
105 | Darren Chamberlain E<lt>darren@cpan.orgE<gt>, |
11ad2df9 |
106 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. |
16dc9970 |
107 | |
108 | =head1 SEE ALSO |
109 | |
56435d6f |
110 | perl(1), SQL::Translator, SQL::Translator::Schema. |
16dc9970 |
111 | |
112 | =cut |