Null out size if a blob field.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
CommitLineData
9398955f 1package SQL::Translator::Producer::MySQL;
2
49e1eb70 3# -------------------------------------------------------------------
472b787e 4# $Id: MySQL.pm,v 1.29 2004-01-25 18:11:42 kycl4rk 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
c855a748 25=head1 NAME
26
27SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
28
29=head1 SYNOPSIS
30
31Use via SQL::Translator:
32
33 use SQL::Translator;
34
35 my $t = SQL::Translator->new( parser => '...', producer => 'MySQL', '...' );
36 $t->translate;
37
38=head1 DESCRIPTION
39
40This module will produce text output of the schema suitable for MySQL.
41There are still some issues to be worked out with syntax differences
42between MySQL versions 3 and 4 ("SET foreign_key_checks," character sets
43for fields, etc.).
44
45=cut
46
9398955f 47use strict;
d529894e 48use vars qw[ $VERSION $DEBUG ];
472b787e 49$VERSION = sprintf "%d.%02d", q$Revision: 1.29 $ =~ /(\d+)\.(\d+)/;
5636ed00 50$DEBUG = 0 unless defined $DEBUG;
9398955f 51
52use Data::Dumper;
1c14e9f1 53use SQL::Translator::Schema::Constants;
5ee19df8 54use SQL::Translator::Utils qw(debug header_comment);
9398955f 55
2620fc1c 56my %translate = (
57 #
58 # Oracle types
59 #
60 varchar2 => 'varchar',
61 long => 'text',
62 CLOB => 'longtext',
63
64 #
65 # Sybase types
66 #
67 int => 'integer',
68 money => 'float',
69 real => 'double',
70 comment => 'text',
71 bit => 'tinyint',
72);
73
9398955f 74sub produce {
a1d94525 75 my $translator = shift;
76 local $DEBUG = $translator->debug;
77 my $no_comments = $translator->no_comments;
78 my $add_drop_table = $translator->add_drop_table;
79 my $schema = $translator->schema;
d529894e 80
1a24938d 81 debug("PKG: Beginning production\n");
d529894e 82
83 my $create;
5ee19df8 84 $create .= header_comment unless ($no_comments);
0823773d 85 # \todo Don't set if MySQL 3.x is set on command line
da147d03 86 $create .= "SET foreign_key_checks=0;\n\n";
9398955f 87
1c14e9f1 88 for my $table ( $schema->get_tables ) {
89 my $table_name = $table->name;
90 debug("PKG: Looking at table '$table_name'\n");
9398955f 91
d529894e 92 #
9398955f 93 # Header. Should this look like what mysqldump produces?
d529894e 94 #
1c14e9f1 95 $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments;
96 $create .= qq[DROP TABLE IF EXISTS $table_name;\n] if $add_drop_table;
97 $create .= "CREATE TABLE $table_name (\n";
9398955f 98
d529894e 99 #
9398955f 100 # Fields
d529894e 101 #
1c14e9f1 102 my @field_defs;
103 for my $field ( $table->get_fields ) {
104 my $field_name = $field->name;
105 debug("PKG: Looking at field '$field_name'\n");
106 my $field_def = $field_name;
9398955f 107
108 # data type and size
1c14e9f1 109 my $data_type = $field->data_type;
110 my @size = $field->size;
35ed60b5 111 my %extra = $field->extra;
112 my $list = $extra{'list'} || [];
0823773d 113 # \todo deal with embedded quotes
4524cf01 114 my $commalist = join( ', ', map { qq['$_'] } @$list );
2620fc1c 115
1c14e9f1 116 #
117 # Oracle "number" type -- figure best MySQL type
118 #
119 if ( lc $data_type eq 'number' ) {
2620fc1c 120 # not an integer
1c14e9f1 121 if ( scalar @size > 1 ) {
2620fc1c 122 $data_type = 'double';
123 }
1c14e9f1 124 elsif ( $size[0] >= 12 ) {
2620fc1c 125 $data_type = 'bigint';
126 }
1c14e9f1 127 elsif ( $size[0] <= 1 ) {
2620fc1c 128 $data_type = 'tinyint';
129 }
130 else {
131 $data_type = 'int';
132 }
133 }
134 elsif ( exists $translate{ $data_type } ) {
135 $data_type = $translate{ $data_type };
136 }
137
472b787e 138 @size = () if $data_type =~ /(text|blob)/i;
6d3f6379 139
1c14e9f1 140 $field_def .= " $data_type";
35ed60b5 141
142 if ( lc $data_type eq 'enum' ) {
143 $field_def .= '(' . $commalist . ')';
6d3f6379 144 }
145 elsif ( defined $size[0] && $size[0] > 0 ) {
1c14e9f1 146 $field_def .= '(' . join( ', ', @size ) . ')';
147 }
d529894e 148
149 # MySQL qualifiers
150 for my $qual ( qw[ binary unsigned zerofill ] ) {
1c14e9f1 151 my $val = $extra{ $qual || uc $qual } or next;
0823773d 152 $field_def .= " $qual";
d529894e 153 }
9398955f 154
155 # Null?
1c14e9f1 156 $field_def .= ' NOT NULL' unless $field->is_nullable;
9398955f 157
158 # Default? XXX Need better quoting!
1c14e9f1 159 my $default = $field->default_value;
d529894e 160 if ( defined $default ) {
161 if ( uc $default eq 'NULL') {
1c14e9f1 162 $field_def .= ' DEFAULT NULL';
9398955f 163 } else {
1c14e9f1 164 $field_def .= " DEFAULT '$default'";
9398955f 165 }
166 }
167
168 # auto_increment?
1c14e9f1 169 $field_def .= " auto_increment" if $field->is_auto_increment;
170 push @field_defs, $field_def;
56120730 171 }
9398955f 172
d529894e 173 #
174 # Indices
175 #
1c14e9f1 176 my @index_defs;
177 for my $index ( $table->get_indices ) {
178 push @index_defs, join( ' ',
35ed60b5 179 lc $index->type eq 'normal' ? 'INDEX' : $index->type,
1c14e9f1 180 $index->name,
181 '(' . join( ', ', $index->fields ) . ')'
182 );
d529894e 183 }
184
185 #
5e56da9a 186 # Constraints -- need to handle more than just FK. -ky
187 #
2620fc1c 188 my @constraint_defs;
1c14e9f1 189 for my $c ( $table->get_constraints ) {
190 my @fields = $c->fields or next;
5e56da9a 191
1c14e9f1 192 if ( $c->type eq PRIMARY_KEY ) {
193 push @constraint_defs,
194 'PRIMARY KEY (' . join(', ', @fields). ')';
195 }
196 elsif ( $c->type eq UNIQUE ) {
197 push @constraint_defs,
198 'UNIQUE (' . join(', ', @fields). ')';
199 }
200 elsif ( $c->type eq FOREIGN_KEY ) {
201 my $def = join(' ',
202 map { $_ || () } 'FOREIGN KEY', $c->name
203 );
204
205 $def .= ' (' . join( ', ', @fields ) . ')';
206
207 $def .= ' REFERENCES ' . $c->reference_table;
208
209 if ( my @rfields = $c->reference_fields ) {
210 $def .= ' (' . join( ', ', @rfields ) . ')';
5e56da9a 211 }
212
1c14e9f1 213 if ( $c->match_type ) {
5e56da9a 214 $def .= ' MATCH ' .
1c14e9f1 215 ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
5e56da9a 216 }
217
1c14e9f1 218 if ( $c->on_delete ) {
219 $def .= ' ON DELETE '.join( ' ', $c->on_delete );
586809da 220 }
221
1c14e9f1 222 if ( $c->on_update ) {
223 $def .= ' ON UPDATE '.join( ' ', $c->on_update );
586809da 224 }
5e56da9a 225
2620fc1c 226 push @constraint_defs, $def;
5e56da9a 227 }
228 }
229
1c14e9f1 230 $create .= join(",\n", map { " $_" }
231 @field_defs, @index_defs, @constraint_defs
232 );
5e56da9a 233
234 #
9398955f 235 # Footer
d529894e 236 #
c45c546e 237 $create .= "\n)";
1c14e9f1 238# while (
239# my ( $key, $val ) = each %{ $table->options }
240# ) {
241# $create .= " $key=$val"
242# }
9398955f 243 $create .= ";\n\n";
244 }
245
9398955f 246 return $create;
247}
248
9398955f 2491;
9398955f 250
c855a748 251# -------------------------------------------------------------------
9398955f 252
c855a748 253=pod
254
255=head1 SEE ALSO
256
257SQL::Translator, http://www.mysql.com/.
9398955f 258
2d6979da 259=head1 AUTHORS
9398955f 260
758ab1cd 261darren chamberlain E<lt>darren@cpan.orgE<gt>,
c855a748 262Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
263
264=cut