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