Added some documentation to PG and MySQL; the "eofile" rule to MySQL.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
CommitLineData
9398955f 1package SQL::Translator::Producer::MySQL;
2
49e1eb70 3# -------------------------------------------------------------------
5e56da9a 4# $Id: MySQL.pm,v 1.6 2003-02-25 05:02:06 kycl4rk Exp $
49e1eb70 5# -------------------------------------------------------------------
abfa405a 6# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7# darren chamberlain <darren@cpan.org>,
8# Chris Mungall <cjm@fruitfly.org>
9398955f 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
25use strict;
d529894e 26use vars qw[ $VERSION $DEBUG ];
5e56da9a 27$VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/;
d529894e 28$DEBUG = 1 unless defined $DEBUG;
9398955f 29
30use Data::Dumper;
31
32sub import {
33 warn "loading " . __PACKAGE__ . "...\n";
34}
35
36sub produce {
37 my ($translator, $data) = @_;
d529894e 38 $DEBUG = $translator->debug;
39 my $no_comments = $translator->no_comments;
40
49e1eb70 41 debug("Beginning production\n");
d529894e 42
43 my $create;
44 unless ( $no_comments ) {
45 $create .= sprintf "--\n-- Created by %s\n-- Created on %s\n--\n\n",
46 __PACKAGE__, scalar localtime;
47 }
9398955f 48
49 for my $table (keys %{$data}) {
49e1eb70 50 debug("Looking at table '$table'\n");
9398955f 51 my $table_data = $data->{$table};
d529894e 52 my @fields = sort {
53 $table_data->{'fields'}->{$a}->{'order'}
54 <=>
55 $table_data->{'fields'}->{$b}->{'order'}
56 } keys %{$table_data->{'fields'}};
9398955f 57
d529894e 58 #
9398955f 59 # Header. Should this look like what mysqldump produces?
d529894e 60 #
61 $create .= "--\n-- Table: $table\n--\n" unless $no_comments;
c45c546e 62 $create .= "CREATE TABLE $table (";
9398955f 63
d529894e 64 #
9398955f 65 # Fields
d529894e 66 #
9398955f 67 for (my $i = 0; $i <= $#fields; $i++) {
68 my $field = $fields[$i];
49e1eb70 69 debug("Looking at field '$field'\n");
9398955f 70 my $field_data = $table_data->{'fields'}->{$field};
71 my @fdata = ("", $field);
c45c546e 72 $create .= "\n";
9398955f 73
74 # data type and size
d529894e 75 my $attr = uc $field_data->{'data_type'} eq 'SET' ? 'list' : 'size';
76 my @values = @{ $field_data->{ $attr } || [] };
77 push @fdata, sprintf "%s%s",
78 $field_data->{'data_type'},
79 ( @values )
80 ? '('.join(', ', @values).')'
81 : '';
82
83 # MySQL qualifiers
84 for my $qual ( qw[ binary unsigned zerofill ] ) {
85 push @fdata, $qual
86 if $field_data->{ $qual } ||
87 $field_data->{ uc $qual };
88 }
9398955f 89
90 # Null?
91 push @fdata, "NOT NULL" unless $field_data->{'null'};
92
93 # Default? XXX Need better quoting!
d529894e 94 my $default = $field_data->{'default'};
95 if ( defined $default ) {
96 if ( uc $default eq 'NULL') {
97 push @fdata, "DEFAULT NULL";
9398955f 98 } else {
99 push @fdata, "DEFAULT '$default'";
100 }
101 }
102
103 # auto_increment?
104 push @fdata, "auto_increment" if $field_data->{'is_auto_inc'};
105
106 # primary key?
d529894e 107 # This is taken care of in the indices, could be duplicated here
108 # push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'};
9398955f 109
110
d529894e 111 $create .= (join " ", '', @fdata);
9398955f 112 $create .= "," unless ($i == $#fields);
9398955f 113 }
114
d529894e 115 #
116 # Indices
117 #
118 my @index_creates;
119 my @indices = @{ $table_data->{'indices'} || [] };
49e1eb70 120 for (my $i = 0; $i <= $#indices; $i++) {
d529894e 121 my $key = $indices[$i];
122 my ($name, $type, $fields) = @{ $key }{ qw[ name type fields ] };
123 $name ||= '';
124 my $index_type =
125 $type eq 'primary_key' ? 'PRIMARY KEY' :
126 $type eq 'unique' ? 'UNIQUE KEY' : 'KEY';
127 push @index_creates,
128 " $index_type $name (" . join( ', ', @$fields ) . ')';
c45c546e 129 }
9398955f 130
d529894e 131 if ( @index_creates ) {
132 $create .= join(",\n", '', @index_creates);
133 }
134
135 #
5e56da9a 136 # Constraints -- need to handle more than just FK. -ky
137 #
138 my @constraints;
139 for my $constraint ( @{ $table_data->{'constraints'} } ) {
140 my $name = $constraint->{'name'} || '';
141 my $type = $constraint->{'type'};
142 my $fields = $constraint->{'fields'};
143 my $ref_table = $constraint->{'reference_table'};
144 my $ref_fields = $constraint->{'reference_fields'};
145 my $match_type = $constraint->{'match_type'} || '';
146 my $on_delete = $constraint->{'on_delete_do'};
147 my $on_update = $constraint->{'on_update_do'};
148
149 if ( $type eq 'foreign_key' ) {
150 my $def = join(' ', map { $_ || () } ' FOREIGN KEY', $name );
151 if ( @$fields ) {
152 $def .= ' (' . join( ', ', @$fields ) . ')';
153 }
154 $def .= " REFERENCES $ref_table";
155
156 if ( @$ref_fields ) {
157 $def .= ' (' . join( ', ', @$ref_fields ) . ')';
158 }
159
160 if ( $match_type ) {
161 $def .= ' MATCH ' .
162 ( $match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
163 }
164
165 $def .= " ON DELETE $on_delete" if $on_delete;
166 $def .= " ON UPDATE $on_update" if $on_update;
167
168 push @constraints, $def;
169 }
170 }
171
172 $create .= join(",\n", '', @constraints) if @constraints;
173
174 #
9398955f 175 # Footer
d529894e 176 #
c45c546e 177 $create .= "\n)";
d529894e 178 while ( my ( $key, $val ) = each %{ $table_data->{'table_options'} } ) {
179 $create .= " $key=$val"
180 }
9398955f 181 $create .= ";\n\n";
182 }
183
9398955f 184 return $create;
185}
186
9398955f 187sub debug {
188 if ($DEBUG) {
d529894e 189 map { warn "[" . __PACKAGE__ . "] $_" } @_;
9398955f 190 }
191}
192
1931;
194__END__
195
196=head1 NAME
197
198SQL::Translator::Producer::MySQL - mysql-specific producer for SQL::Translator
199
9398955f 200=head1 AUTHOR
201
202darren chamberlain E<lt>darren@cpan.orgE<gt>