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