1 package SQL::Translator::Producer::MySQL;
3 # -------------------------------------------------------------------
4 # $Id: MySQL.pm,v 1.9 2003-03-12 14:17:11 dlc 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.9 $ =~ /(\d+)\.(\d+)/;
28 $DEBUG = 1 unless defined $DEBUG;
31 use SQL::Translator::Utils qw(debug);
34 warn "loading " . __PACKAGE__ . "...\n";
38 my ($translator, $data) = @_;
39 $DEBUG = $translator->debug;
40 my $no_comments = $translator->no_comments;
41 my $add_drop_table = $translator->add_drop_table;
43 debug("PKG: Beginning production\n");
46 unless ( $no_comments ) {
47 $create .= sprintf "--\n-- Created by %s\n-- Created on %s\n--\n\n",
48 __PACKAGE__, scalar localtime;
51 for my $table ( keys %{ $data } ) {
52 debug("PKG: Looking at table '$table'\n");
53 my $table_data = $data->{$table};
55 $table_data->{'fields'}->{$a}->{'order'}
57 $table_data->{'fields'}->{$b}->{'order'}
58 } keys %{$table_data->{'fields'}};
61 # Header. Should this look like what mysqldump produces?
63 $create .= "--\n-- Table: $table\n--\n" unless $no_comments;
64 $create .= qq[DROP TABLE $table;\n] if $add_drop_table;
65 $create .= "CREATE TABLE $table (";
70 for (my $i = 0; $i <= $#fields; $i++) {
71 my $field = $fields[$i];
72 debug("PKG: Looking at field '$field'\n");
73 my $field_data = $table_data->{'fields'}->{$field};
74 my @fdata = ("", $field);
78 my $attr = uc $field_data->{'data_type'} eq 'SET' ? 'list' : 'size';
79 my @values = @{ $field_data->{ $attr } || [] };
80 push @fdata, sprintf "%s%s",
81 $field_data->{'data_type'},
83 ? '('.join(', ', @values).')'
87 for my $qual ( qw[ binary unsigned zerofill ] ) {
89 if $field_data->{ $qual } ||
90 $field_data->{ uc $qual };
94 push @fdata, "NOT NULL" unless $field_data->{'null'};
96 # Default? XXX Need better quoting!
97 my $default = $field_data->{'default'};
98 if ( defined $default ) {
99 if ( uc $default eq 'NULL') {
100 push @fdata, "DEFAULT NULL";
102 push @fdata, "DEFAULT '$default'";
107 push @fdata, "auto_increment" if $field_data->{'is_auto_inc'};
110 # This is taken care of in the indices, could be duplicated here
111 # push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'};
114 $create .= (join " ", '', @fdata);
115 $create .= "," unless ($i == $#fields);
122 my @indices = @{ $table_data->{'indices'} || [] };
123 for (my $i = 0; $i <= $#indices; $i++) {
124 my $key = $indices[$i];
125 my ($name, $type, $fields) = @{ $key }{ qw[ name type fields ] };
128 $type eq 'primary_key' ? 'PRIMARY KEY' :
129 $type eq 'unique' ? 'UNIQUE KEY' : 'KEY';
131 " $index_type $name (" . join( ', ', @$fields ) . ')';
134 if ( @index_creates ) {
135 $create .= join(",\n", '', @index_creates);
139 # Constraints -- need to handle more than just FK. -ky
142 for my $constraint ( @{ $table_data->{'constraints'} } ) {
143 my $name = $constraint->{'name'} || '';
144 my $type = $constraint->{'type'};
145 my $fields = $constraint->{'fields'};
146 my $ref_table = $constraint->{'reference_table'};
147 my $ref_fields = $constraint->{'reference_fields'};
148 my $match_type = $constraint->{'match_type'} || '';
149 my $on_delete = $constraint->{'on_delete_do'};
150 my $on_update = $constraint->{'on_update_do'};
152 if ( $type eq 'foreign_key' ) {
153 my $def = join(' ', map { $_ || () } ' FOREIGN KEY', $name );
155 $def .= ' (' . join( ', ', @$fields ) . ')';
157 $def .= " REFERENCES $ref_table";
159 if ( @$ref_fields ) {
160 $def .= ' (' . join( ', ', @$ref_fields ) . ')';
165 ( $match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
168 if ( @{ $on_delete || [] } ) {
169 $def .= ' ON DELETE '.join(' ', @$on_delete);
172 if ( @{ $on_update || [] } ) {
173 $def .= ' ON UPDATE '.join(' ', @$on_update);
176 push @constraints, $def;
180 $create .= join(",\n", '', @constraints) if @constraints;
186 while ( my ( $key, $val ) = each %{ $table_data->{'table_options'} } ) {
187 $create .= " $key=$val"
200 SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
204 darren chamberlain E<lt>darren@cpan.orgE<gt>,
205 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>