Commit | Line | Data |
16dc9970 |
1 | package SQL::Translator::Producer; |
2 | |
077ebf34 |
3 | use strict; |
f27f9229 |
4 | use warnings; |
06baeb21 |
5 | use Scalar::Util (); |
0c04c5a2 |
6 | our $VERSION = '1.59'; |
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 { |
06baeb21 |
18 | my (undef, $field, $field_ref, $exceptions) = @_; |
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 { |
0f4c1a5b |
44 | $$field_ref .= " DEFAULT '$default'"; |
bc8e2aa1 |
45 | } |
46 | |
47 | } |
48 | |
16dc9970 |
49 | 1; |
50 | |
49e1eb70 |
51 | # ------------------------------------------------------------------- |
16dc9970 |
52 | # A burnt child loves the fire. |
53 | # Oscar Wilde |
49e1eb70 |
54 | # ------------------------------------------------------------------- |
55 | |
56 | =pod |
16dc9970 |
57 | |
58 | =head1 NAME |
59 | |
56435d6f |
60 | SQL::Translator::Producer - describes how to write a producer |
16dc9970 |
61 | |
16dc9970 |
62 | =head1 DESCRIPTION |
63 | |
077ebf34 |
64 | Producer modules designed to be used with SQL::Translator need to |
65 | implement a single function, called B<produce>. B<produce> will be |
ea93df61 |
66 | called with the SQL::Translator object from which it is expected to |
67 | retrieve the SQL::Translator::Schema object which has been populated |
56435d6f |
68 | by the parser. It is expected to return a string. |
16dc9970 |
69 | |
0e1ec14f |
70 | =head1 METHODS |
71 | |
72 | =over 4 |
73 | |
74 | =item produce |
75 | |
76 | =item create_table($table) |
77 | |
78 | =item create_field($field) |
79 | |
80 | =item create_view($view) |
81 | |
82 | =item create_index($index) |
83 | |
84 | =item create_constraint($constraint) |
85 | |
86 | =item create_trigger($trigger) |
87 | |
88 | =item alter_field($from_field, $to_field) |
89 | |
90 | =item add_field($table, $new_field) |
91 | |
92 | =item drop_field($table, $old_field) |
93 | |
0e42fda6 |
94 | =back |
95 | |
56435d6f |
96 | =head1 AUTHORS |
16dc9970 |
97 | |
56435d6f |
98 | Darren Chamberlain E<lt>darren@cpan.orgE<gt>, |
11ad2df9 |
99 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. |
16dc9970 |
100 | |
101 | =head1 SEE ALSO |
102 | |
56435d6f |
103 | perl(1), SQL::Translator, SQL::Translator::Schema. |
16dc9970 |
104 | |
105 | =cut |