1 package SQL::Translator::Producer::MySQL;
3 # -------------------------------------------------------------------
4 # $Id: MySQL.pm,v 1.4 2002-11-22 03:03:40 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002 Ken Y. Clark <kclark@cpan.org>,
7 # darren chamberlain <darren@cpan.org>
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; version 2.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # -------------------------------------------------------------------
25 use vars qw[ $VERSION $DEBUG ];
26 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
27 $DEBUG = 1 unless defined $DEBUG;
32 warn "loading " . __PACKAGE__ . "...\n";
36 my ($translator, $data) = @_;
37 $DEBUG = $translator->debug;
38 my $no_comments = $translator->no_comments;
40 debug("Beginning production\n");
43 unless ( $no_comments ) {
44 $create .= sprintf "--\n-- Created by %s\n-- Created on %s\n--\n\n",
45 __PACKAGE__, scalar localtime;
48 for my $table (keys %{$data}) {
49 debug("Looking at table '$table'\n");
50 my $table_data = $data->{$table};
52 $table_data->{'fields'}->{$a}->{'order'}
54 $table_data->{'fields'}->{$b}->{'order'}
55 } keys %{$table_data->{'fields'}};
58 # Header. Should this look like what mysqldump produces?
60 $create .= "--\n-- Table: $table\n--\n" unless $no_comments;
61 $create .= "CREATE TABLE $table (";
66 for (my $i = 0; $i <= $#fields; $i++) {
67 my $field = $fields[$i];
68 debug("Looking at field '$field'\n");
69 my $field_data = $table_data->{'fields'}->{$field};
70 my @fdata = ("", $field);
74 my $attr = uc $field_data->{'data_type'} eq 'SET' ? 'list' : 'size';
75 my @values = @{ $field_data->{ $attr } || [] };
76 push @fdata, sprintf "%s%s",
77 $field_data->{'data_type'},
79 ? '('.join(', ', @values).')'
83 for my $qual ( qw[ binary unsigned zerofill ] ) {
85 if $field_data->{ $qual } ||
86 $field_data->{ uc $qual };
90 push @fdata, "NOT NULL" unless $field_data->{'null'};
92 # Default? XXX Need better quoting!
93 my $default = $field_data->{'default'};
94 if ( defined $default ) {
95 if ( uc $default eq 'NULL') {
96 push @fdata, "DEFAULT NULL";
98 push @fdata, "DEFAULT '$default'";
103 push @fdata, "auto_increment" if $field_data->{'is_auto_inc'};
106 # This is taken care of in the indices, could be duplicated here
107 # push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'};
110 $create .= (join " ", '', @fdata);
111 $create .= "," unless ($i == $#fields);
118 my @indices = @{ $table_data->{'indices'} || [] };
119 for (my $i = 0; $i <= $#indices; $i++) {
120 my $key = $indices[$i];
121 my ($name, $type, $fields) = @{ $key }{ qw[ name type fields ] };
124 $type eq 'primary_key' ? 'PRIMARY KEY' :
125 $type eq 'unique' ? 'UNIQUE KEY' : 'KEY';
127 " $index_type $name (" . join( ', ', @$fields ) . ')';
130 if ( @index_creates ) {
131 $create .= join(",\n", '', @index_creates);
138 while ( my ( $key, $val ) = each %{ $table_data->{'table_options'} } ) {
139 $create .= " $key=$val"
149 map { warn "[" . __PACKAGE__ . "] $_" } @_;
158 SQL::Translator::Producer::MySQL - mysql-specific producer for SQL::Translator
162 darren chamberlain E<lt>darren@cpan.orgE<gt>