1 package SQL::Translator::Producer::MySQL;
3 # -------------------------------------------------------------------
4 # $Id: MySQL.pm,v 1.30 2004-02-04 21:09:54 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 # darren chamberlain <darren@cpan.org>,
8 # Chris Mungall <cjm@fruitfly.org>
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.
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.
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
23 # -------------------------------------------------------------------
27 SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
31 Use via SQL::Translator:
35 my $t = SQL::Translator->new( parser => '...', producer => 'MySQL', '...' );
40 This module will produce text output of the schema suitable for MySQL.
41 There are still some issues to be worked out with syntax differences
42 between MySQL versions 3 and 4 ("SET foreign_key_checks," character sets
48 use vars qw[ $VERSION $DEBUG ];
49 $VERSION = sprintf "%d.%02d", q$Revision: 1.30 $ =~ /(\d+)\.(\d+)/;
50 $DEBUG = 0 unless defined $DEBUG;
53 use SQL::Translator::Schema::Constants;
54 use SQL::Translator::Utils qw(debug header_comment);
57 # Use only lowercase for the keys (e.g. "long" and not "LONG")
63 varchar2 => 'varchar',
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;
84 debug("PKG: Beginning production\n");
87 $create .= header_comment unless ($no_comments);
88 # \todo Don't set if MySQL 3.x is set on command line
89 $create .= "SET foreign_key_checks=0;\n\n";
91 for my $table ( $schema->get_tables ) {
92 my $table_name = $table->name;
93 debug("PKG: Looking at table '$table_name'\n");
96 # Header. Should this look like what mysqldump produces?
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";
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;
112 my $data_type = $field->data_type;
113 my @size = $field->size;
114 my %extra = $field->extra;
115 my $list = $extra{'list'} || [];
116 # \todo deal with embedded quotes
117 my $commalist = join( ', ', map { qq['$_'] } @$list );
120 # Oracle "number" type -- figure best MySQL type
122 if ( lc $data_type eq 'number' ) {
124 if ( scalar @size > 1 ) {
125 $data_type = 'double';
127 elsif ( $size[0] >= 12 ) {
128 $data_type = 'bigint';
130 elsif ( $size[0] <= 1 ) {
131 $data_type = 'tinyint';
137 elsif ( exists $translate{ lc $data_type } ) {
138 $data_type = $translate{ lc $data_type };
141 @size = () if $data_type =~ /(text|blob)/i;
143 $field_def .= " $data_type";
145 if ( lc $data_type eq 'enum' ) {
146 $field_def .= '(' . $commalist . ')';
148 elsif ( defined $size[0] && $size[0] > 0 ) {
149 $field_def .= '(' . join( ', ', @size ) . ')';
153 for my $qual ( qw[ binary unsigned zerofill ] ) {
154 my $val = $extra{ $qual || uc $qual } or next;
155 $field_def .= " $qual";
159 $field_def .= ' NOT NULL' unless $field->is_nullable;
161 # Default? XXX Need better quoting!
162 my $default = $field->default_value;
163 if ( defined $default ) {
164 if ( uc $default eq 'NULL') {
165 $field_def .= ' DEFAULT NULL';
167 $field_def .= " DEFAULT '$default'";
172 $field_def .= " auto_increment" if $field->is_auto_increment;
173 push @field_defs, $field_def;
180 for my $index ( $table->get_indices ) {
181 push @index_defs, join( ' ',
182 lc $index->type eq 'normal' ? 'INDEX' : $index->type,
184 '(' . join( ', ', $index->fields ) . ')'
189 # Constraints -- need to handle more than just FK. -ky
192 for my $c ( $table->get_constraints ) {
193 my @fields = $c->fields or next;
195 if ( $c->type eq PRIMARY_KEY ) {
196 push @constraint_defs,
197 'PRIMARY KEY (' . join(', ', @fields). ')';
199 elsif ( $c->type eq UNIQUE ) {
200 push @constraint_defs,
201 'UNIQUE (' . join(', ', @fields). ')';
203 elsif ( $c->type eq FOREIGN_KEY ) {
205 map { $_ || () } 'FOREIGN KEY', $c->name
208 $def .= ' (' . join( ', ', @fields ) . ')';
210 $def .= ' REFERENCES ' . $c->reference_table;
212 if ( my @rfields = $c->reference_fields ) {
213 $def .= ' (' . join( ', ', @rfields ) . ')';
216 if ( $c->match_type ) {
218 ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
221 if ( $c->on_delete ) {
222 $def .= ' ON DELETE '.join( ' ', $c->on_delete );
225 if ( $c->on_update ) {
226 $def .= ' ON UPDATE '.join( ' ', $c->on_update );
229 push @constraint_defs, $def;
233 $create .= join(",\n", map { " $_" }
234 @field_defs, @index_defs, @constraint_defs
242 # my ( $key, $val ) = each %{ $table->options }
244 # $create .= " $key=$val"
254 # -------------------------------------------------------------------
260 SQL::Translator, http://www.mysql.com/.
264 darren chamberlain E<lt>darren@cpan.orgE<gt>,
265 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.