Cosmetic change in POD.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
1 package SQL::Translator::Producer::MySQL;
2
3 # -------------------------------------------------------------------
4 # $Id: MySQL.pm,v 1.20 2003-05-12 14:59:15 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.20 $ =~ /(\d+)\.(\d+)/;
28 $DEBUG   = 0 unless defined $DEBUG;
29
30 use Data::Dumper;
31 use SQL::Translator::Utils qw(debug header_comment);
32
33 my %translate  = (
34     #
35     # Oracle types
36     #
37     varchar2   => 'varchar',
38     long       => 'text',
39     CLOB       => 'longtext',
40
41     #
42     # Sybase types
43     #
44     int        => 'integer',
45     money      => 'float',
46     real       => 'double',
47     comment    => 'text',
48     bit        => 'tinyint',
49 );
50
51 sub produce {
52     my ($translator, $data) = @_;
53     local $DEBUG            = $translator->debug;
54     my $no_comments         = $translator->no_comments;
55     my $add_drop_table      = $translator->add_drop_table;
56
57     debug("PKG: Beginning production\n");
58
59     my $create; 
60     $create .= header_comment unless ($no_comments);
61
62     for my $table ( keys %{ $data } ) {
63         debug("PKG: Looking at table '$table'\n");
64         my $table_data = $data->{$table};
65         my @fields = sort { 
66             $table_data->{'fields'}->{$a}->{'order'} 
67             <=>
68             $table_data->{'fields'}->{$b}->{'order'}
69         } keys %{$table_data->{'fields'}};
70
71         #
72         # Header.  Should this look like what mysqldump produces?
73         #
74         $create .= "--\n-- Table: $table\n--\n" unless $no_comments;
75         $create .= qq[DROP TABLE IF EXISTS $table;\n] if $add_drop_table;
76         $create .= "CREATE TABLE $table (";
77
78         #
79         # Fields
80         #
81         for (my $i = 0; $i <= $#fields; $i++) {
82             my $field = $fields[$i];
83             debug("PKG: Looking at field '$field'\n");
84             my $field_data = $table_data->{'fields'}->{$field};
85             my @fdata = ("", $field);
86             $create .= "\n";
87
88             # data type and size
89             my $attr      = uc $field_data->{'data_type'} eq 'SET' 
90                             ? 'list' : 'size';
91             my @values    = @{ $field_data->{ $attr } || [] };
92             my $data_type = $field_data->{'data_type'};
93
94             if ( $data_type eq 'number' ) {
95                 # not an integer
96                 if ( scalar @values > 1 ) {
97                     $data_type = 'double';
98                 }
99                 elsif ( $values[0] >= 12 ) {
100                     $data_type = 'bigint';
101                 }
102                 elsif ( $values[0] <= 1 ) {
103                     $data_type = 'tinyint';
104                 }
105                 else {
106                     $data_type = 'int';
107                 }
108             }
109             elsif ( exists $translate{ $data_type } ) {
110                 $data_type = $translate{ $data_type };
111             }
112
113             push @fdata, sprintf "%s%s",
114                 $data_type,
115                 defined( $values[0] )
116                     ? '(' . join( ', ', @values ) . ')'
117                     : '';
118
119             # MySQL qualifiers
120             for my $qual ( qw[ binary unsigned zerofill ] ) {
121                 push @fdata, $qual 
122                     if $field_data->{ $qual } ||
123                        $field_data->{ uc $qual };
124             }
125
126             # Null?
127             push @fdata, "NOT NULL" unless $field_data->{'null'};
128
129             # Default?  XXX Need better quoting!
130             my $default = $field_data->{'default'};
131             if ( defined $default ) {
132                 if ( uc $default eq 'NULL') {
133                     push @fdata, "DEFAULT NULL";
134                 } else {
135                     push @fdata, "DEFAULT '$default'";
136                 }
137             }
138
139             # auto_increment?
140             push @fdata, "auto_increment" if $field_data->{'is_auto_inc'};
141
142             # primary key?
143             # This is taken care of in the indices, could be duplicated here
144             # push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'};
145
146
147             $create .= (join " ", '', @fdata);
148             $create .= "," unless ($i == $#fields);
149                 }
150
151         #
152         # Indices
153         #
154         my @index_creates;
155         my @indices     = @{ $table_data->{'indices'}     || [] };
156         my @constraints = @{ $table_data->{'constraints'} || [] };
157
158         for my $key ( @indices, @constraints ) {
159             my ($name, $type, $fields) = @{ $key }{ qw[ name type fields ] };
160             $name ||= '';
161             $type ||= '';
162             my $index_type = 
163                 $type eq 'primary_key' ? 'PRIMARY KEY' :
164                 $type eq 'unique'      ? 'UNIQUE KEY'  :
165                 $type eq 'key'         ? 'KEY'         :
166                                 $type eq 'normal'      ? 'KEY'         : '';
167
168             next unless $index_type;
169             push @index_creates,
170                 "  $index_type $name (" . join( ', ', @$fields ) . ')';
171         }
172
173         if ( @index_creates ) {
174             $create .= join(",\n", '', @index_creates);
175         }
176
177         #
178         # Constraints -- need to handle more than just FK. -ky
179         #
180         my @constraint_defs;
181         for my $constraint ( @constraints ) {
182             my $name       = $constraint->{'name'} || '';
183             my $type       = $constraint->{'type'};
184             my $fields     = $constraint->{'fields'};
185             my $ref_table  = $constraint->{'reference_table'};
186             my $ref_fields = $constraint->{'reference_fields'};
187             my $match_type = $constraint->{'match_type'} || '';
188             my $on_delete  = $constraint->{'on_delete_do'};
189             my $on_update  = $constraint->{'on_update_do'};
190
191             if ( $type eq 'foreign_key' ) {
192                 my $def = join(' ', map { $_ || () } '  FOREIGN KEY', $name );
193                 if ( @$fields ) {
194                     $def .= ' (' . join( ', ', @$fields ) . ')';
195                 }
196                 $def .= " REFERENCES $ref_table";
197
198                 if ( @{ $ref_fields || [] } ) {
199                     $def .= ' (' . join( ', ', @$ref_fields ) . ')';
200                 }
201
202                 if ( $match_type ) {
203                     $def .= ' MATCH ' . 
204                         ( $match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
205                 }
206
207                 if ( @{ $on_delete || [] } ) {
208                     $def .= ' ON DELETE '.join(' ', @$on_delete);
209                 }
210
211                 if ( @{ $on_update || [] } ) {
212                     $def .= ' ON UPDATE '.join(' ', @$on_update);
213                 }
214
215                 push @constraint_defs, $def;
216             }
217         }
218
219         $create .= join(",\n", '', @constraint_defs) if @constraint_defs;
220
221         #
222         # Footer
223         #
224         $create .= "\n)";
225         while ( 
226             my ( $key, $val ) = each %{ $table_data->{'table_options'} ||= {} }
227         ) {
228             $create .= " $key=$val" 
229         }
230         $create .= ";\n\n";
231     }
232
233     return $create;
234 }
235
236 1;
237 __END__
238
239 =head1 NAME
240
241 SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
242
243 =head1 AUTHORS
244
245 darren chamberlain E<lt>darren@cpan.orgE<gt>,
246 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>