1 package SQL::Translator::Producer::MySQL;
3 # -------------------------------------------------------------------
4 # $Id: MySQL.pm,v 1.19 2003-05-09 17:04:39 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 # -------------------------------------------------------------------
26 use vars qw[ $VERSION $DEBUG ];
27 $VERSION = sprintf "%d.%02d", q$Revision: 1.19 $ =~ /(\d+)\.(\d+)/;
28 $DEBUG = 0 unless defined $DEBUG;
31 use SQL::Translator::Utils qw(debug header_comment);
37 varchar2 => 'varchar',
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;
57 debug("PKG: Beginning production\n");
60 $create .= header_comment unless ($no_comments);
62 for my $table ( keys %{ $data } ) {
63 debug("PKG: Looking at table '$table'\n");
64 my $table_data = $data->{$table};
66 $table_data->{'fields'}->{$a}->{'order'}
68 $table_data->{'fields'}->{$b}->{'order'}
69 } keys %{$table_data->{'fields'}};
72 # Header. Should this look like what mysqldump produces?
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 (";
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);
89 my $attr = uc $field_data->{'data_type'} eq 'SET'
91 my @values = @{ $field_data->{ $attr } || [] };
92 my $data_type = $field_data->{'data_type'};
94 if ( $data_type eq 'number' ) {
96 if ( scalar @values > 1 ) {
97 $data_type = 'double';
99 elsif ( $values[0] >= 12 ) {
100 $data_type = 'bigint';
102 elsif ( $values[0] <= 1 ) {
103 $data_type = 'tinyint';
109 elsif ( exists $translate{ $data_type } ) {
110 $data_type = $translate{ $data_type };
113 push @fdata, sprintf "%s%s",
115 defined( $values[0] )
116 ? '(' . join( ', ', @values ) . ')'
120 for my $qual ( qw[ binary unsigned zerofill ] ) {
122 if $field_data->{ $qual } ||
123 $field_data->{ uc $qual };
127 push @fdata, "NOT NULL" unless $field_data->{'null'};
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";
135 push @fdata, "DEFAULT '$default'";
140 push @fdata, "auto_increment" if $field_data->{'is_auto_inc'};
143 # This is taken care of in the indices, could be duplicated here
144 # push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'};
147 $create .= (join " ", '', @fdata);
148 $create .= "," unless ($i == $#fields);
155 my @indices = @{ $table_data->{'indices'} || [] };
156 my @constraints = @{ $table_data->{'constraints'} || [] };
158 for my $key ( @indices, @constraints ) {
159 my ($name, $type, $fields) = @{ $key }{ qw[ name type fields ] };
163 $type eq 'primary_key' ? 'PRIMARY KEY' :
164 $type eq 'unique' ? 'UNIQUE KEY' :
165 $type eq 'key' ? 'KEY' :
166 $type eq 'normal' ? 'KEY' : '';
168 next unless $index_type;
170 " $index_type $name (" . join( ', ', @$fields ) . ')';
173 if ( @index_creates ) {
174 $create .= join(",\n", '', @index_creates);
178 # Constraints -- need to handle more than just FK. -ky
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'};
191 if ( $type eq 'foreign_key' ) {
192 my $def = join(' ', map { $_ || () } ' FOREIGN KEY', $name );
194 $def .= ' (' . join( ', ', @$fields ) . ')';
196 $def .= " REFERENCES $ref_table";
198 if ( @{ $ref_fields || [] } ) {
199 $def .= ' (' . join( ', ', @$ref_fields ) . ')';
204 ( $match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
207 if ( @{ $on_delete || [] } ) {
208 $def .= ' ON DELETE '.join(' ', @$on_delete);
211 if ( @{ $on_update || [] } ) {
212 $def .= ' ON UPDATE '.join(' ', @$on_update);
215 push @constraint_defs, $def;
219 $create .= join(",\n", '', @constraint_defs) if @constraint_defs;
226 my ( $key, $val ) = each %{ $table_data->{'table_options'} ||= {} }
228 $create .= " $key=$val"
241 SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
245 darren chamberlain E<lt>darren@cpan.orgE<gt>,
246 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>