Doc tweaks
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
CommitLineData
9398955f 1package SQL::Translator::Producer::MySQL;
2
49e1eb70 3# -------------------------------------------------------------------
2b695517 4# $Id: MySQL.pm,v 1.37 2004-08-11 21:55:34 kycl4rk Exp $
49e1eb70 5# -------------------------------------------------------------------
977651a5 6# Copyright (C) 2002-4 SQLFairy Authors
9398955f 7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License as
10# published by the Free Software Foundation; version 2.
11#
12# This program is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
c855a748 23=head1 NAME
24
25SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
26
27=head1 SYNOPSIS
28
29Use via SQL::Translator:
30
31 use SQL::Translator;
32
33 my $t = SQL::Translator->new( parser => '...', producer => 'MySQL', '...' );
34 $t->translate;
35
36=head1 DESCRIPTION
37
38This module will produce text output of the schema suitable for MySQL.
39There are still some issues to be worked out with syntax differences
40between MySQL versions 3 and 4 ("SET foreign_key_checks," character sets
41for fields, etc.).
42
43=cut
44
9398955f 45use strict;
d529894e 46use vars qw[ $VERSION $DEBUG ];
2b695517 47$VERSION = sprintf "%d.%02d", q$Revision: 1.37 $ =~ /(\d+)\.(\d+)/;
5636ed00 48$DEBUG = 0 unless defined $DEBUG;
9398955f 49
50use Data::Dumper;
1c14e9f1 51use SQL::Translator::Schema::Constants;
5ee19df8 52use SQL::Translator::Utils qw(debug header_comment);
9398955f 53
d2344c83 54#
55# Use only lowercase for the keys (e.g. "long" and not "LONG")
56#
2620fc1c 57my %translate = (
58 #
59 # Oracle types
60 #
61 varchar2 => 'varchar',
62 long => 'text',
d2344c83 63 clob => 'longtext',
2620fc1c 64
65 #
66 # Sybase types
67 #
68 int => 'integer',
69 money => 'float',
70 real => 'double',
71 comment => 'text',
72 bit => 'tinyint',
87779799 73
74 #
75 # Access types
76 #
77 'long integer' => 'integer',
78 'text' => 'text',
79 'datetime' => 'datetime',
2620fc1c 80);
81
9398955f 82sub produce {
a1d94525 83 my $translator = shift;
84 local $DEBUG = $translator->debug;
85 my $no_comments = $translator->no_comments;
86 my $add_drop_table = $translator->add_drop_table;
87 my $schema = $translator->schema;
d529894e 88
1a24938d 89 debug("PKG: Beginning production\n");
d529894e 90
91 my $create;
5ee19df8 92 $create .= header_comment unless ($no_comments);
0823773d 93 # \todo Don't set if MySQL 3.x is set on command line
da147d03 94 $create .= "SET foreign_key_checks=0;\n\n";
9398955f 95
1c14e9f1 96 for my $table ( $schema->get_tables ) {
97 my $table_name = $table->name;
98 debug("PKG: Looking at table '$table_name'\n");
9398955f 99
d529894e 100 #
9398955f 101 # Header. Should this look like what mysqldump produces?
d529894e 102 #
1c14e9f1 103 $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments;
104 $create .= qq[DROP TABLE IF EXISTS $table_name;\n] if $add_drop_table;
105 $create .= "CREATE TABLE $table_name (\n";
9398955f 106
d529894e 107 #
9398955f 108 # Fields
d529894e 109 #
1c14e9f1 110 my @field_defs;
111 for my $field ( $table->get_fields ) {
112 my $field_name = $field->name;
113 debug("PKG: Looking at field '$field_name'\n");
114 my $field_def = $field_name;
9398955f 115
116 # data type and size
1c14e9f1 117 my $data_type = $field->data_type;
118 my @size = $field->size;
35ed60b5 119 my %extra = $field->extra;
120 my $list = $extra{'list'} || [];
0823773d 121 # \todo deal with embedded quotes
4524cf01 122 my $commalist = join( ', ', map { qq['$_'] } @$list );
2620fc1c 123
1c14e9f1 124 #
125 # Oracle "number" type -- figure best MySQL type
126 #
127 if ( lc $data_type eq 'number' ) {
2620fc1c 128 # not an integer
1c14e9f1 129 if ( scalar @size > 1 ) {
2620fc1c 130 $data_type = 'double';
131 }
1c14e9f1 132 elsif ( $size[0] >= 12 ) {
2620fc1c 133 $data_type = 'bigint';
134 }
1c14e9f1 135 elsif ( $size[0] <= 1 ) {
2620fc1c 136 $data_type = 'tinyint';
137 }
138 else {
139 $data_type = 'int';
140 }
141 }
49758c5d 142 #
143 # Convert a large Oracle varchar to "text"
144 #
2b695517 145 elsif ( $data_type =~ /char/i && $size[0] > 255 ) {
49758c5d 146 $data_type = 'text';
147 @size = ();
148 }
4167a803 149 elsif ( $data_type =~ /char/i && ! $size[0] ) {
150 @size = (255);
151 }
2b695517 152 elsif ( $data_type =~ /boolean/i ) {
153 $data_type = 'enum';
154 $commalist = "'0','1'";
155 }
d2344c83 156 elsif ( exists $translate{ lc $data_type } ) {
157 $data_type = $translate{ lc $data_type };
2620fc1c 158 }
159
472b787e 160 @size = () if $data_type =~ /(text|blob)/i;
6d3f6379 161
2b695517 162 if ( $data_type =~ /(double|float)/ && scalar @size == 1 ) {
163 push @size, '0';
164 }
165
1c14e9f1 166 $field_def .= " $data_type";
35ed60b5 167
168 if ( lc $data_type eq 'enum' ) {
169 $field_def .= '(' . $commalist . ')';
6d3f6379 170 }
171 elsif ( defined $size[0] && $size[0] > 0 ) {
1c14e9f1 172 $field_def .= '(' . join( ', ', @size ) . ')';
173 }
d529894e 174
175 # MySQL qualifiers
176 for my $qual ( qw[ binary unsigned zerofill ] ) {
1c14e9f1 177 my $val = $extra{ $qual || uc $qual } or next;
0823773d 178 $field_def .= " $qual";
d529894e 179 }
9398955f 180
181 # Null?
1c14e9f1 182 $field_def .= ' NOT NULL' unless $field->is_nullable;
9398955f 183
184 # Default? XXX Need better quoting!
1c14e9f1 185 my $default = $field->default_value;
d529894e 186 if ( defined $default ) {
187 if ( uc $default eq 'NULL') {
1c14e9f1 188 $field_def .= ' DEFAULT NULL';
9398955f 189 } else {
1c14e9f1 190 $field_def .= " DEFAULT '$default'";
9398955f 191 }
192 }
193
194 # auto_increment?
1c14e9f1 195 $field_def .= " auto_increment" if $field->is_auto_increment;
196 push @field_defs, $field_def;
56120730 197 }
9398955f 198
d529894e 199 #
200 # Indices
201 #
1c14e9f1 202 my @index_defs;
bc60004d 203 my %indexed_fields;
1c14e9f1 204 for my $index ( $table->get_indices ) {
205 push @index_defs, join( ' ',
35ed60b5 206 lc $index->type eq 'normal' ? 'INDEX' : $index->type,
1c14e9f1 207 $index->name,
208 '(' . join( ', ', $index->fields ) . ')'
209 );
bc60004d 210 $indexed_fields{ $_ } = 1 for $index->fields;
d529894e 211 }
212
213 #
5e56da9a 214 # Constraints -- need to handle more than just FK. -ky
215 #
2620fc1c 216 my @constraint_defs;
d2d944cc 217 my $has_fk;
1c14e9f1 218 for my $c ( $table->get_constraints ) {
219 my @fields = $c->fields or next;
5e56da9a 220
1c14e9f1 221 if ( $c->type eq PRIMARY_KEY ) {
222 push @constraint_defs,
223 'PRIMARY KEY (' . join(', ', @fields). ')';
224 }
225 elsif ( $c->type eq UNIQUE ) {
226 push @constraint_defs,
227 'UNIQUE (' . join(', ', @fields). ')';
228 }
229 elsif ( $c->type eq FOREIGN_KEY ) {
d2d944cc 230 $has_fk = 1;
bc60004d 231
232 #
233 # Make sure FK field is indexed or MySQL complains.
234 #
235 unless ( $indexed_fields{ $fields[0] } ) {
236 push @index_defs, "INDEX ($fields[0])";
237 }
238
1c14e9f1 239 my $def = join(' ',
240 map { $_ || () } 'FOREIGN KEY', $c->name
241 );
242
243 $def .= ' (' . join( ', ', @fields ) . ')';
244
245 $def .= ' REFERENCES ' . $c->reference_table;
246
247 if ( my @rfields = $c->reference_fields ) {
248 $def .= ' (' . join( ', ', @rfields ) . ')';
5e56da9a 249 }
250
1c14e9f1 251 if ( $c->match_type ) {
5e56da9a 252 $def .= ' MATCH ' .
1c14e9f1 253 ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
5e56da9a 254 }
255
1c14e9f1 256 if ( $c->on_delete ) {
257 $def .= ' ON DELETE '.join( ' ', $c->on_delete );
586809da 258 }
259
1c14e9f1 260 if ( $c->on_update ) {
261 $def .= ' ON UPDATE '.join( ' ', $c->on_update );
586809da 262 }
5e56da9a 263
2620fc1c 264 push @constraint_defs, $def;
5e56da9a 265 }
266 }
267
1c14e9f1 268 $create .= join(",\n", map { " $_" }
269 @field_defs, @index_defs, @constraint_defs
270 );
5e56da9a 271
272 #
9398955f 273 # Footer
d529894e 274 #
c45c546e 275 $create .= "\n)";
d2d944cc 276 if ( $has_fk ) {
277 $create .= " Type=InnoDB";
278 }
9398955f 279 $create .= ";\n\n";
280 }
281
9398955f 282 return $create;
283}
284
9398955f 2851;
9398955f 286
c855a748 287# -------------------------------------------------------------------
9398955f 288
c855a748 289=pod
290
291=head1 SEE ALSO
292
293SQL::Translator, http://www.mysql.com/.
9398955f 294
2d6979da 295=head1 AUTHORS
9398955f 296
758ab1cd 297darren chamberlain E<lt>darren@cpan.orgE<gt>,
c855a748 298Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
299
300=cut