Commit | Line | Data |
9398955f |
1 | package SQL::Translator::Producer::MySQL; |
2 | |
49e1eb70 |
3 | # ------------------------------------------------------------------- |
3c32785b |
4 | # $Id: MySQL.pm,v 1.18 2003-05-03 15:17:23 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 | |
25 | use strict; |
d529894e |
26 | use vars qw[ $VERSION $DEBUG ]; |
3c32785b |
27 | $VERSION = sprintf "%d.%02d", q$Revision: 1.18 $ =~ /(\d+)\.(\d+)/; |
5636ed00 |
28 | $DEBUG = 0 unless defined $DEBUG; |
9398955f |
29 | |
30 | use Data::Dumper; |
5ee19df8 |
31 | use SQL::Translator::Utils qw(debug header_comment); |
9398955f |
32 | |
2620fc1c |
33 | my %translate = ( |
34 | # |
35 | # Oracle types |
36 | # |
37 | varchar2 => 'varchar', |
38 | long => 'text', |
39 | CLOB => 'longtext', |
40 | |
41 | # |
42 | # Sybase types |
43 | # |
44 | int => 'integer', |
45 | money => 'float', |
46 | real => 'double', |
47 | comment => 'text', |
48 | bit => 'tinyint', |
49 | ); |
50 | |
9398955f |
51 | sub produce { |
52 | my ($translator, $data) = @_; |
5636ed00 |
53 | local $DEBUG = $translator->debug; |
d529894e |
54 | my $no_comments = $translator->no_comments; |
758ab1cd |
55 | my $add_drop_table = $translator->add_drop_table; |
d529894e |
56 | |
1a24938d |
57 | debug("PKG: Beginning production\n"); |
d529894e |
58 | |
59 | my $create; |
5ee19df8 |
60 | $create .= header_comment unless ($no_comments); |
9398955f |
61 | |
758ab1cd |
62 | for my $table ( keys %{ $data } ) { |
56120730 |
63 | |
64 | |
1a24938d |
65 | debug("PKG: Looking at table '$table'\n"); |
9398955f |
66 | my $table_data = $data->{$table}; |
56120730 |
67 | #warn Dumper($table_data); |
d529894e |
68 | my @fields = sort { |
69 | $table_data->{'fields'}->{$a}->{'order'} |
70 | <=> |
71 | $table_data->{'fields'}->{$b}->{'order'} |
72 | } keys %{$table_data->{'fields'}}; |
9398955f |
73 | |
d529894e |
74 | # |
9398955f |
75 | # Header. Should this look like what mysqldump produces? |
d529894e |
76 | # |
77 | $create .= "--\n-- Table: $table\n--\n" unless $no_comments; |
2620fc1c |
78 | $create .= qq[DROP TABLE IF EXISTS $table;\n] if $add_drop_table; |
c45c546e |
79 | $create .= "CREATE TABLE $table ("; |
9398955f |
80 | |
d529894e |
81 | # |
9398955f |
82 | # Fields |
d529894e |
83 | # |
9398955f |
84 | for (my $i = 0; $i <= $#fields; $i++) { |
85 | my $field = $fields[$i]; |
1a24938d |
86 | debug("PKG: Looking at field '$field'\n"); |
9398955f |
87 | my $field_data = $table_data->{'fields'}->{$field}; |
88 | my @fdata = ("", $field); |
c45c546e |
89 | $create .= "\n"; |
9398955f |
90 | |
91 | # data type and size |
2620fc1c |
92 | my $attr = uc $field_data->{'data_type'} eq 'SET' |
93 | ? 'list' : 'size'; |
94 | my @values = @{ $field_data->{ $attr } || [] }; |
95 | my $data_type = $field_data->{'data_type'}; |
96 | |
97 | if ( $data_type eq 'number' ) { |
98 | # not an integer |
99 | if ( scalar @values > 1 ) { |
100 | $data_type = 'double'; |
101 | } |
102 | elsif ( $values[0] >= 12 ) { |
103 | $data_type = 'bigint'; |
104 | } |
105 | elsif ( $values[0] <= 1 ) { |
106 | $data_type = 'tinyint'; |
107 | } |
108 | else { |
109 | $data_type = 'int'; |
110 | } |
111 | } |
112 | elsif ( exists $translate{ $data_type } ) { |
113 | $data_type = $translate{ $data_type }; |
114 | } |
115 | |
0a7fc605 |
116 | push @fdata, sprintf "%s%s", |
2620fc1c |
117 | $data_type, |
0a7fc605 |
118 | defined( $values[0] ) |
2620fc1c |
119 | ? '(' . join( ', ', @values ) . ')' |
d529894e |
120 | : ''; |
121 | |
122 | # MySQL qualifiers |
123 | for my $qual ( qw[ binary unsigned zerofill ] ) { |
124 | push @fdata, $qual |
125 | if $field_data->{ $qual } || |
126 | $field_data->{ uc $qual }; |
127 | } |
9398955f |
128 | |
129 | # Null? |
130 | push @fdata, "NOT NULL" unless $field_data->{'null'}; |
131 | |
132 | # Default? XXX Need better quoting! |
d529894e |
133 | my $default = $field_data->{'default'}; |
134 | if ( defined $default ) { |
135 | if ( uc $default eq 'NULL') { |
136 | push @fdata, "DEFAULT NULL"; |
9398955f |
137 | } else { |
138 | push @fdata, "DEFAULT '$default'"; |
139 | } |
140 | } |
141 | |
142 | # auto_increment? |
143 | push @fdata, "auto_increment" if $field_data->{'is_auto_inc'}; |
144 | |
145 | # primary key? |
d529894e |
146 | # This is taken care of in the indices, could be duplicated here |
147 | # push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'}; |
9398955f |
148 | |
149 | |
d529894e |
150 | $create .= (join " ", '', @fdata); |
9398955f |
151 | $create .= "," unless ($i == $#fields); |
56120730 |
152 | } |
9398955f |
153 | |
d529894e |
154 | # |
155 | # Indices |
156 | # |
157 | my @index_creates; |
2620fc1c |
158 | my @indices = @{ $table_data->{'indices'} || [] }; |
159 | my @constraints = @{ $table_data->{'constraints'} || [] }; |
56120730 |
160 | |
2620fc1c |
161 | for my $key ( @indices, @constraints ) { |
d529894e |
162 | my ($name, $type, $fields) = @{ $key }{ qw[ name type fields ] }; |
163 | $name ||= ''; |
3c32785b |
164 | $type ||= ''; |
d529894e |
165 | my $index_type = |
166 | $type eq 'primary_key' ? 'PRIMARY KEY' : |
2620fc1c |
167 | $type eq 'unique' ? 'UNIQUE KEY' : |
56120730 |
168 | $type eq 'key' ? 'KEY' : |
169 | $type eq 'normal' ? 'KEY' : ''; |
170 | |
2620fc1c |
171 | next unless $index_type; |
56120730 |
172 | push @index_creates, |
d529894e |
173 | " $index_type $name (" . join( ', ', @$fields ) . ')'; |
c45c546e |
174 | } |
9398955f |
175 | |
d529894e |
176 | if ( @index_creates ) { |
177 | $create .= join(",\n", '', @index_creates); |
178 | } |
179 | |
180 | # |
5e56da9a |
181 | # Constraints -- need to handle more than just FK. -ky |
182 | # |
2620fc1c |
183 | my @constraint_defs; |
184 | for my $constraint ( @constraints ) { |
5e56da9a |
185 | my $name = $constraint->{'name'} || ''; |
186 | my $type = $constraint->{'type'}; |
187 | my $fields = $constraint->{'fields'}; |
188 | my $ref_table = $constraint->{'reference_table'}; |
189 | my $ref_fields = $constraint->{'reference_fields'}; |
190 | my $match_type = $constraint->{'match_type'} || ''; |
191 | my $on_delete = $constraint->{'on_delete_do'}; |
192 | my $on_update = $constraint->{'on_update_do'}; |
193 | |
194 | if ( $type eq 'foreign_key' ) { |
195 | my $def = join(' ', map { $_ || () } ' FOREIGN KEY', $name ); |
196 | if ( @$fields ) { |
197 | $def .= ' (' . join( ', ', @$fields ) . ')'; |
198 | } |
199 | $def .= " REFERENCES $ref_table"; |
200 | |
023c4026 |
201 | if ( @{ $ref_fields || [] } ) { |
5e56da9a |
202 | $def .= ' (' . join( ', ', @$ref_fields ) . ')'; |
203 | } |
204 | |
205 | if ( $match_type ) { |
206 | $def .= ' MATCH ' . |
207 | ( $match_type =~ /full/i ) ? 'FULL' : 'PARTIAL'; |
208 | } |
209 | |
586809da |
210 | if ( @{ $on_delete || [] } ) { |
211 | $def .= ' ON DELETE '.join(' ', @$on_delete); |
212 | } |
213 | |
214 | if ( @{ $on_update || [] } ) { |
215 | $def .= ' ON UPDATE '.join(' ', @$on_update); |
216 | } |
5e56da9a |
217 | |
2620fc1c |
218 | push @constraint_defs, $def; |
5e56da9a |
219 | } |
220 | } |
221 | |
2620fc1c |
222 | $create .= join(",\n", '', @constraint_defs) if @constraint_defs; |
5e56da9a |
223 | |
224 | # |
9398955f |
225 | # Footer |
d529894e |
226 | # |
c45c546e |
227 | $create .= "\n)"; |
95f99bd3 |
228 | while ( |
229 | my ( $key, $val ) = each %{ $table_data->{'table_options'} ||= {} } |
230 | ) { |
d529894e |
231 | $create .= " $key=$val" |
232 | } |
9398955f |
233 | $create .= ";\n\n"; |
234 | } |
235 | |
9398955f |
236 | return $create; |
237 | } |
238 | |
9398955f |
239 | 1; |
240 | __END__ |
241 | |
242 | =head1 NAME |
243 | |
758ab1cd |
244 | SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator |
9398955f |
245 | |
9398955f |
246 | =head1 AUTHOR |
247 | |
758ab1cd |
248 | darren chamberlain E<lt>darren@cpan.orgE<gt>, |
249 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt> |