e69933460c27912acba527563623e92f011bb4d8
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer.pm
1 package SQL::Translator::Producer;
2
3 use strict;
4 use vars qw($VERSION);
5 use Scalar::Util ();
6 $VERSION = '1.59';
7
8 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   my (undef, $field, $field_ref, $exceptions) = @_;
19   my $default = $field->default_value;
20   return if !defined $default;
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
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
38   if (ref $default) {
39       $$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       $$field_ref .= " DEFAULT $default";
43   } else {
44       $$field_ref .= " DEFAULT '$default'";
45   }
46
47 }
48
49 1;
50
51 # -------------------------------------------------------------------
52 # A burnt child loves the fire.
53 # Oscar Wilde
54 # -------------------------------------------------------------------
55
56 =pod
57
58 =head1 NAME
59
60 SQL::Translator::Producer - describes how to write a producer
61
62 =head1 DESCRIPTION
63
64 Producer modules designed to be used with SQL::Translator need to
65 implement a single function, called B<produce>.  B<produce> will be
66 called with the SQL::Translator object from which it is expected to
67 retrieve the SQL::Translator::Schema object which has been populated
68 by the parser.  It is expected to return a string.
69
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
94 =back
95
96 =head1 AUTHORS
97
98 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
99 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
100
101 =head1 SEE ALSO
102
103 perl(1), SQL::Translator, SQL::Translator::Schema.
104
105 =cut