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