File: Producer.pm

package info (click to toggle)
libsql-translator-perl 0.11024-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,572 kB
  • sloc: perl: 67,471; sql: 3,809; xml: 258; makefile: 2
file content (112 lines) | stat: -rw-r--r-- 2,745 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package SQL::Translator::Producer;

use strict;
use warnings;
use Scalar::Util ();
our $VERSION = '1.59';

sub produce { "" }

# Do not rely on this if you are not bundled with SQL::Translator.
# -- rjbs, 2008-09-30
## $exceptions contains an arrayref of paired values
## Each pair contains a pattern match or string, and a value to be used as
## the default if matched.
## They are special per Producer, and provide support for the old 'now()'
## default value exceptions
sub _apply_default_value {
  my ($self, $field, $field_ref, $exceptions) = @_;
  my $default = $field->default_value;
  return if !defined $default;

  if ($exceptions and ! ref $default) {
    for (my $i = 0; $i < @$exceptions; $i += 2) {
      my ($pat, $val) = @$exceptions[ $i, $i + 1 ];
      if (ref $pat and $default =~ $pat) {
          $default = $val;
          last;
      } elsif (lc $default eq lc $pat) {
          $default = $val;
          last
      }
    }
  }

  my $type = lc $field->data_type;
  my $is_numeric_datatype = ($type =~ /^(?:(?:big|medium|small|tiny)?int(?:eger)?|decimal|double|float|num(?:ber|eric)?|real)$/);

  if (ref $default) {
      $$field_ref .= " DEFAULT $$default";
  } elsif ($is_numeric_datatype && Scalar::Util::looks_like_number ($default) ) {
    # we need to check the data itself in addition to the datatype, for basic safety
      $$field_ref .= " DEFAULT $default";
  } else {
      $default = $self->_quote_string($default);
      $$field_ref .= " DEFAULT $default";
  }

}

sub _quote_string {
    my ($self, $string) = @_;
    $string =~ s/'/''/g;
    return qq{'$string'};
}

1;

# -------------------------------------------------------------------
# A burnt child loves the fire.
# Oscar Wilde
# -------------------------------------------------------------------

=pod

=head1 NAME

SQL::Translator::Producer - describes how to write a producer

=head1 DESCRIPTION

Producer modules designed to be used with SQL::Translator need to
implement a single function, called B<produce>.  B<produce> will be
called with the SQL::Translator object from which it is expected to
retrieve the SQL::Translator::Schema object which has been populated
by the parser.  It is expected to return a string.

=head1 METHODS

=over 4

=item produce

=item create_table($table)

=item create_field($field)

=item create_view($view)

=item create_index($index)

=item create_constraint($constraint)

=item create_trigger($trigger)

=item alter_field($from_field, $to_field)

=item add_field($table, $new_field)

=item drop_field($table, $old_field)

=back

=head1 AUTHORS

Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.

=head1 SEE ALSO

perl(1), SQL::Translator, SQL::Translator::Schema.

=cut