Stops Use of uninitialized value in numeric gt (>) at /usr/local/share/perl/5.6.1...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
CommitLineData
9398955f 1package SQL::Translator::Producer::MySQL;
2
49e1eb70 3# -------------------------------------------------------------------
35ed60b5 4# $Id: MySQL.pm,v 1.24 2003-08-16 15:56:58 rossta Exp $
49e1eb70 5# -------------------------------------------------------------------
abfa405a 6# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7# darren chamberlain <darren@cpan.org>,
8# Chris Mungall <cjm@fruitfly.org>
9398955f 9#
10# This program is free software; you can redistribute it and/or
11# modify it under the terms of the GNU General Public License as
12# published by the Free Software Foundation; version 2.
13#
14# This program is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17# General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22# 02111-1307 USA
23# -------------------------------------------------------------------
24
25use strict;
d529894e 26use vars qw[ $VERSION $DEBUG ];
35ed60b5 27$VERSION = sprintf "%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/;
5636ed00 28$DEBUG = 0 unless defined $DEBUG;
9398955f 29
30use Data::Dumper;
1c14e9f1 31use SQL::Translator::Schema::Constants;
5ee19df8 32use SQL::Translator::Utils qw(debug header_comment);
9398955f 33
2620fc1c 34my %translate = (
35 #
36 # Oracle types
37 #
38 varchar2 => 'varchar',
39 long => 'text',
40 CLOB => 'longtext',
41
42 #
43 # Sybase types
44 #
45 int => 'integer',
46 money => 'float',
47 real => 'double',
48 comment => 'text',
49 bit => 'tinyint',
50);
51
9398955f 52sub produce {
a1d94525 53 my $translator = shift;
54 local $DEBUG = $translator->debug;
55 my $no_comments = $translator->no_comments;
56 my $add_drop_table = $translator->add_drop_table;
57 my $schema = $translator->schema;
d529894e 58
1a24938d 59 debug("PKG: Beginning production\n");
d529894e 60
61 my $create;
5ee19df8 62 $create .= header_comment unless ($no_comments);
da147d03 63 $create .= "SET foreign_key_checks=0;\n\n";
9398955f 64
1c14e9f1 65 for my $table ( $schema->get_tables ) {
66 my $table_name = $table->name;
67 debug("PKG: Looking at table '$table_name'\n");
9398955f 68
d529894e 69 #
9398955f 70 # Header. Should this look like what mysqldump produces?
d529894e 71 #
1c14e9f1 72 $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments;
73 $create .= qq[DROP TABLE IF EXISTS $table_name;\n] if $add_drop_table;
74 $create .= "CREATE TABLE $table_name (\n";
9398955f 75
d529894e 76 #
9398955f 77 # Fields
d529894e 78 #
1c14e9f1 79 my @field_defs;
80 for my $field ( $table->get_fields ) {
81 my $field_name = $field->name;
82 debug("PKG: Looking at field '$field_name'\n");
83 my $field_def = $field_name;
9398955f 84
85 # data type and size
1c14e9f1 86 my $data_type = $field->data_type;
87 my @size = $field->size;
35ed60b5 88 my %extra = $field->extra;
89 my $list = $extra{'list'} || [];
90 my $commalist = "'" . join("','", @$list) . "'"; # \todo deal with embedded quotes
2620fc1c 91
1c14e9f1 92 #
93 # Oracle "number" type -- figure best MySQL type
94 #
95 if ( lc $data_type eq 'number' ) {
2620fc1c 96 # not an integer
1c14e9f1 97 if ( scalar @size > 1 ) {
2620fc1c 98 $data_type = 'double';
99 }
1c14e9f1 100 elsif ( $size[0] >= 12 ) {
2620fc1c 101 $data_type = 'bigint';
102 }
1c14e9f1 103 elsif ( $size[0] <= 1 ) {
2620fc1c 104 $data_type = 'tinyint';
105 }
106 else {
107 $data_type = 'int';
108 }
109 }
110 elsif ( exists $translate{ $data_type } ) {
111 $data_type = $translate{ $data_type };
112 }
113
1c14e9f1 114 $field_def .= " $data_type";
35ed60b5 115
116 if ( lc $data_type eq 'enum' ) {
117 $field_def .= '(' . $commalist . ')';
118 } elsif ( defined $size[0] && $size[0] > 0 ) {
1c14e9f1 119 $field_def .= '(' . join( ', ', @size ) . ')';
120 }
d529894e 121
122 # MySQL qualifiers
123 for my $qual ( qw[ binary unsigned zerofill ] ) {
1c14e9f1 124 my $val = $extra{ $qual || uc $qual } or next;
125 $field_def .= " $val";
d529894e 126 }
9398955f 127
128 # Null?
1c14e9f1 129 $field_def .= ' NOT NULL' unless $field->is_nullable;
9398955f 130
131 # Default? XXX Need better quoting!
1c14e9f1 132 my $default = $field->default_value;
d529894e 133 if ( defined $default ) {
134 if ( uc $default eq 'NULL') {
1c14e9f1 135 $field_def .= ' DEFAULT NULL';
9398955f 136 } else {
1c14e9f1 137 $field_def .= " DEFAULT '$default'";
9398955f 138 }
139 }
140
141 # auto_increment?
1c14e9f1 142 $field_def .= " auto_increment" if $field->is_auto_increment;
143 push @field_defs, $field_def;
56120730 144 }
9398955f 145
d529894e 146 #
147 # Indices
148 #
1c14e9f1 149 my @index_defs;
150 for my $index ( $table->get_indices ) {
151 push @index_defs, join( ' ',
35ed60b5 152 lc $index->type eq 'normal' ? 'INDEX' : $index->type,
1c14e9f1 153 $index->name,
154 '(' . join( ', ', $index->fields ) . ')'
155 );
d529894e 156 }
157
158 #
5e56da9a 159 # Constraints -- need to handle more than just FK. -ky
160 #
2620fc1c 161 my @constraint_defs;
1c14e9f1 162 for my $c ( $table->get_constraints ) {
163 my @fields = $c->fields or next;
5e56da9a 164
1c14e9f1 165 if ( $c->type eq PRIMARY_KEY ) {
166 push @constraint_defs,
167 'PRIMARY KEY (' . join(', ', @fields). ')';
168 }
169 elsif ( $c->type eq UNIQUE ) {
170 push @constraint_defs,
171 'UNIQUE (' . join(', ', @fields). ')';
172 }
173 elsif ( $c->type eq FOREIGN_KEY ) {
174 my $def = join(' ',
175 map { $_ || () } 'FOREIGN KEY', $c->name
176 );
177
178 $def .= ' (' . join( ', ', @fields ) . ')';
179
180 $def .= ' REFERENCES ' . $c->reference_table;
181
182 if ( my @rfields = $c->reference_fields ) {
183 $def .= ' (' . join( ', ', @rfields ) . ')';
5e56da9a 184 }
185
1c14e9f1 186 if ( $c->match_type ) {
5e56da9a 187 $def .= ' MATCH ' .
1c14e9f1 188 ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
5e56da9a 189 }
190
1c14e9f1 191 if ( $c->on_delete ) {
192 $def .= ' ON DELETE '.join( ' ', $c->on_delete );
586809da 193 }
194
1c14e9f1 195 if ( $c->on_update ) {
196 $def .= ' ON UPDATE '.join( ' ', $c->on_update );
586809da 197 }
5e56da9a 198
2620fc1c 199 push @constraint_defs, $def;
5e56da9a 200 }
201 }
202
1c14e9f1 203 $create .= join(",\n", map { " $_" }
204 @field_defs, @index_defs, @constraint_defs
205 );
5e56da9a 206
207 #
9398955f 208 # Footer
d529894e 209 #
c45c546e 210 $create .= "\n)";
1c14e9f1 211# while (
212# my ( $key, $val ) = each %{ $table->options }
213# ) {
214# $create .= " $key=$val"
215# }
9398955f 216 $create .= ";\n\n";
217 }
218
9398955f 219 return $create;
220}
221
9398955f 2221;
223__END__
224
225=head1 NAME
226
758ab1cd 227SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
9398955f 228
2d6979da 229=head1 AUTHORS
9398955f 230
758ab1cd 231darren chamberlain E<lt>darren@cpan.orgE<gt>,
232Ken Y. Clark E<lt>kclark@cpan.orgE<gt>