Kill size attribute if field data type is "text."
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
1 package SQL::Translator::Producer::MySQL;
2
3 # -------------------------------------------------------------------
4 # $Id: MySQL.pm,v 1.27 2003-09-29 14:55:26 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>
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
25 use strict;
26 use vars qw[ $VERSION $DEBUG ];
27 $VERSION = sprintf "%d.%02d", q$Revision: 1.27 $ =~ /(\d+)\.(\d+)/;
28 $DEBUG   = 0 unless defined $DEBUG;
29
30 use Data::Dumper;
31 use SQL::Translator::Schema::Constants;
32 use SQL::Translator::Utils qw(debug header_comment);
33
34 my %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
52 sub produce {
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;
58
59     debug("PKG: Beginning production\n");
60
61     my $create; 
62     $create .= header_comment unless ($no_comments);
63     # \todo Don't set if MySQL 3.x is set on command line
64     $create .= "SET foreign_key_checks=0;\n\n";
65
66     for my $table ( $schema->get_tables ) {
67         my $table_name = $table->name;
68         debug("PKG: Looking at table '$table_name'\n");
69
70         #
71         # Header.  Should this look like what mysqldump produces?
72         #
73         $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments;
74         $create .= qq[DROP TABLE IF EXISTS $table_name;\n] if $add_drop_table;
75         $create .= "CREATE TABLE $table_name (\n";
76
77         #
78         # Fields
79         #
80         my @field_defs;
81         for my $field ( $table->get_fields ) {
82             my $field_name = $field->name;
83             debug("PKG: Looking at field '$field_name'\n");
84             my $field_def = $field_name;
85
86             # data type and size
87             my $data_type = $field->data_type;
88             my @size      = $field->size;
89             my %extra     = $field->extra;
90             my $list      = $extra{'list'} || [];
91             # \todo deal with embedded quotes
92             my $commalist = join( ', ', map { qq['$_'] } @$list );
93
94             #
95             # Oracle "number" type -- figure best MySQL type
96             #
97             if ( lc $data_type eq 'number' ) {
98                 # not an integer
99                 if ( scalar @size > 1 ) {
100                     $data_type = 'double';
101                 }
102                 elsif ( $size[0] >= 12 ) {
103                     $data_type = 'bigint';
104                 }
105                 elsif ( $size[0] <= 1 ) {
106                     $data_type = 'tinyint';
107                 }
108                 else {
109                     $data_type = 'int';
110                 }
111             }
112             elsif ( exists $translate{ $data_type } ) {
113                 $data_type = $translate{ $data_type };
114             }
115
116             @size = () if $data_type eq 'text';
117
118             $field_def .= " $data_type";
119             
120             if ( lc $data_type eq 'enum' ) {
121                 $field_def .= '(' . $commalist . ')';
122                         } 
123             elsif ( defined $size[0] && $size[0] > 0 ) {
124                 $field_def .= '(' . join( ', ', @size ) . ')';
125             }
126
127             # MySQL qualifiers
128             for my $qual ( qw[ binary unsigned zerofill ] ) {
129                 my $val = $extra{ $qual || uc $qual } or next;
130                 $field_def .= " $qual";
131             }
132
133             # Null?
134             $field_def .= ' NOT NULL' unless $field->is_nullable;
135
136             # Default?  XXX Need better quoting!
137             my $default = $field->default_value;
138             if ( defined $default ) {
139                 if ( uc $default eq 'NULL') {
140                     $field_def .= ' DEFAULT NULL';
141                 } else {
142                     $field_def .= " DEFAULT '$default'";
143                 }
144             }
145
146             # auto_increment?
147             $field_def .= " auto_increment" if $field->is_auto_increment;
148             push @field_defs, $field_def;
149                 }
150
151         #
152         # Indices
153         #
154         my @index_defs;
155         for my $index ( $table->get_indices ) {
156             push @index_defs, join( ' ', 
157                 lc $index->type eq 'normal' ? 'INDEX' : $index->type,
158                 $index->name,
159                 '(' . join( ', ', $index->fields ) . ')'
160             );
161         }
162
163         #
164         # Constraints -- need to handle more than just FK. -ky
165         #
166         my @constraint_defs;
167         for my $c ( $table->get_constraints ) {
168             my @fields = $c->fields or next;
169
170             if ( $c->type eq PRIMARY_KEY ) {
171                 push @constraint_defs,
172                     'PRIMARY KEY (' . join(', ', @fields). ')';
173             }
174             elsif ( $c->type eq UNIQUE ) {
175                 push @constraint_defs,
176                     'UNIQUE (' . join(', ', @fields). ')';
177             }
178             elsif ( $c->type eq FOREIGN_KEY ) {
179                 my $def = join(' ', 
180                     map { $_ || () } 'FOREIGN KEY', $c->name 
181                 );
182
183                 $def .= ' (' . join( ', ', @fields ) . ')';
184
185                 $def .= ' REFERENCES ' . $c->reference_table;
186
187                 if ( my @rfields = $c->reference_fields ) {
188                     $def .= ' (' . join( ', ', @rfields ) . ')';
189                 }
190
191                 if ( $c->match_type ) {
192                     $def .= ' MATCH ' . 
193                         ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
194                 }
195
196                 if ( $c->on_delete ) {
197                     $def .= ' ON DELETE '.join( ' ', $c->on_delete );
198                 }
199
200                 if ( $c->on_update ) {
201                     $def .= ' ON UPDATE '.join( ' ', $c->on_update );
202                 }
203
204                 push @constraint_defs, $def;
205             }
206         }
207
208         $create .= join(",\n", map { "  $_" } 
209             @field_defs, @index_defs, @constraint_defs
210         );
211
212         #
213         # Footer
214         #
215         $create .= "\n)";
216 #        while ( 
217 #            my ( $key, $val ) = each %{ $table->options }
218 #        ) {
219 #            $create .= " $key=$val" 
220 #        }
221         $create .= ";\n\n";
222     }
223
224     return $create;
225 }
226
227 1;
228 __END__
229
230 =head1 NAME
231
232 SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
233
234 =head1 AUTHORS
235
236 darren chamberlain E<lt>darren@cpan.orgE<gt>,
237 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>