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