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