Commit | Line | Data |
9398955f |
1 | package SQL::Translator::Producer::MySQL; |
2 | |
49e1eb70 |
3 | # ------------------------------------------------------------------- |
4524cf01 |
4 | # $Id: MySQL.pm,v 1.26 2003-08-18 15:43:14 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 ]; |
4524cf01 |
27 | $VERSION = sprintf "%d.%02d", q$Revision: 1.26 $ =~ /(\d+)\.(\d+)/; |
5636ed00 |
28 | $DEBUG = 0 unless defined $DEBUG; |
9398955f |
29 | |
30 | use Data::Dumper; |
1c14e9f1 |
31 | use SQL::Translator::Schema::Constants; |
5ee19df8 |
32 | use SQL::Translator::Utils qw(debug header_comment); |
9398955f |
33 | |
2620fc1c |
34 | my %translate = ( |
35 | # |
36 | # Oracle types |
37 | # |
38 | varchar2 => 'varchar', |
39 | long => 'text', |
40 | CLOB => 'longtext', |
41 | |
42 | # |
43 | # Sybase types |
44 | # |
45 | int => 'integer', |
46 | money => 'float', |
47 | real => 'double', |
48 | comment => 'text', |
49 | bit => 'tinyint', |
50 | ); |
51 | |
9398955f |
52 | sub produce { |
a1d94525 |
53 | my $translator = shift; |
54 | local $DEBUG = $translator->debug; |
55 | my $no_comments = $translator->no_comments; |
56 | my $add_drop_table = $translator->add_drop_table; |
57 | my $schema = $translator->schema; |
d529894e |
58 | |
1a24938d |
59 | debug("PKG: Beginning production\n"); |
d529894e |
60 | |
61 | my $create; |
5ee19df8 |
62 | $create .= header_comment unless ($no_comments); |
0823773d |
63 | # \todo Don't set if MySQL 3.x is set on command line |
da147d03 |
64 | $create .= "SET foreign_key_checks=0;\n\n"; |
9398955f |
65 | |
1c14e9f1 |
66 | for my $table ( $schema->get_tables ) { |
67 | my $table_name = $table->name; |
68 | debug("PKG: Looking at table '$table_name'\n"); |
9398955f |
69 | |
d529894e |
70 | # |
9398955f |
71 | # Header. Should this look like what mysqldump produces? |
d529894e |
72 | # |
1c14e9f1 |
73 | $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments; |
74 | $create .= qq[DROP TABLE IF EXISTS $table_name;\n] if $add_drop_table; |
75 | $create .= "CREATE TABLE $table_name (\n"; |
9398955f |
76 | |
d529894e |
77 | # |
9398955f |
78 | # Fields |
d529894e |
79 | # |
1c14e9f1 |
80 | my @field_defs; |
81 | for my $field ( $table->get_fields ) { |
82 | my $field_name = $field->name; |
83 | debug("PKG: Looking at field '$field_name'\n"); |
84 | my $field_def = $field_name; |
9398955f |
85 | |
86 | # data type and size |
1c14e9f1 |
87 | my $data_type = $field->data_type; |
88 | my @size = $field->size; |
35ed60b5 |
89 | my %extra = $field->extra; |
90 | my $list = $extra{'list'} || []; |
0823773d |
91 | # \todo deal with embedded quotes |
4524cf01 |
92 | my $commalist = join( ', ', map { qq['$_'] } @$list ); |
2620fc1c |
93 | |
1c14e9f1 |
94 | # |
95 | # Oracle "number" type -- figure best MySQL type |
96 | # |
97 | if ( lc $data_type eq 'number' ) { |
2620fc1c |
98 | # not an integer |
1c14e9f1 |
99 | if ( scalar @size > 1 ) { |
2620fc1c |
100 | $data_type = 'double'; |
101 | } |
1c14e9f1 |
102 | elsif ( $size[0] >= 12 ) { |
2620fc1c |
103 | $data_type = 'bigint'; |
104 | } |
1c14e9f1 |
105 | elsif ( $size[0] <= 1 ) { |
2620fc1c |
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 | |
1c14e9f1 |
116 | $field_def .= " $data_type"; |
35ed60b5 |
117 | |
118 | if ( lc $data_type eq 'enum' ) { |
119 | $field_def .= '(' . $commalist . ')'; |
120 | } elsif ( defined $size[0] && $size[0] > 0 ) { |
1c14e9f1 |
121 | $field_def .= '(' . join( ', ', @size ) . ')'; |
122 | } |
d529894e |
123 | |
124 | # MySQL qualifiers |
125 | for my $qual ( qw[ binary unsigned zerofill ] ) { |
1c14e9f1 |
126 | my $val = $extra{ $qual || uc $qual } or next; |
0823773d |
127 | $field_def .= " $qual"; |
d529894e |
128 | } |
9398955f |
129 | |
130 | # Null? |
1c14e9f1 |
131 | $field_def .= ' NOT NULL' unless $field->is_nullable; |
9398955f |
132 | |
133 | # Default? XXX Need better quoting! |
1c14e9f1 |
134 | my $default = $field->default_value; |
d529894e |
135 | if ( defined $default ) { |
136 | if ( uc $default eq 'NULL') { |
1c14e9f1 |
137 | $field_def .= ' DEFAULT NULL'; |
9398955f |
138 | } else { |
1c14e9f1 |
139 | $field_def .= " DEFAULT '$default'"; |
9398955f |
140 | } |
141 | } |
142 | |
143 | # auto_increment? |
1c14e9f1 |
144 | $field_def .= " auto_increment" if $field->is_auto_increment; |
145 | push @field_defs, $field_def; |
56120730 |
146 | } |
9398955f |
147 | |
d529894e |
148 | # |
149 | # Indices |
150 | # |
1c14e9f1 |
151 | my @index_defs; |
152 | for my $index ( $table->get_indices ) { |
153 | push @index_defs, join( ' ', |
35ed60b5 |
154 | lc $index->type eq 'normal' ? 'INDEX' : $index->type, |
1c14e9f1 |
155 | $index->name, |
156 | '(' . join( ', ', $index->fields ) . ')' |
157 | ); |
d529894e |
158 | } |
159 | |
160 | # |
5e56da9a |
161 | # Constraints -- need to handle more than just FK. -ky |
162 | # |
2620fc1c |
163 | my @constraint_defs; |
1c14e9f1 |
164 | for my $c ( $table->get_constraints ) { |
165 | my @fields = $c->fields or next; |
5e56da9a |
166 | |
1c14e9f1 |
167 | if ( $c->type eq PRIMARY_KEY ) { |
168 | push @constraint_defs, |
169 | 'PRIMARY KEY (' . join(', ', @fields). ')'; |
170 | } |
171 | elsif ( $c->type eq UNIQUE ) { |
172 | push @constraint_defs, |
173 | 'UNIQUE (' . join(', ', @fields). ')'; |
174 | } |
175 | elsif ( $c->type eq FOREIGN_KEY ) { |
176 | my $def = join(' ', |
177 | map { $_ || () } 'FOREIGN KEY', $c->name |
178 | ); |
179 | |
180 | $def .= ' (' . join( ', ', @fields ) . ')'; |
181 | |
182 | $def .= ' REFERENCES ' . $c->reference_table; |
183 | |
184 | if ( my @rfields = $c->reference_fields ) { |
185 | $def .= ' (' . join( ', ', @rfields ) . ')'; |
5e56da9a |
186 | } |
187 | |
1c14e9f1 |
188 | if ( $c->match_type ) { |
5e56da9a |
189 | $def .= ' MATCH ' . |
1c14e9f1 |
190 | ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL'; |
5e56da9a |
191 | } |
192 | |
1c14e9f1 |
193 | if ( $c->on_delete ) { |
194 | $def .= ' ON DELETE '.join( ' ', $c->on_delete ); |
586809da |
195 | } |
196 | |
1c14e9f1 |
197 | if ( $c->on_update ) { |
198 | $def .= ' ON UPDATE '.join( ' ', $c->on_update ); |
586809da |
199 | } |
5e56da9a |
200 | |
2620fc1c |
201 | push @constraint_defs, $def; |
5e56da9a |
202 | } |
203 | } |
204 | |
1c14e9f1 |
205 | $create .= join(",\n", map { " $_" } |
206 | @field_defs, @index_defs, @constraint_defs |
207 | ); |
5e56da9a |
208 | |
209 | # |
9398955f |
210 | # Footer |
d529894e |
211 | # |
c45c546e |
212 | $create .= "\n)"; |
1c14e9f1 |
213 | # while ( |
214 | # my ( $key, $val ) = each %{ $table->options } |
215 | # ) { |
216 | # $create .= " $key=$val" |
217 | # } |
9398955f |
218 | $create .= ";\n\n"; |
219 | } |
220 | |
9398955f |
221 | return $create; |
222 | } |
223 | |
9398955f |
224 | 1; |
225 | __END__ |
226 | |
227 | =head1 NAME |
228 | |
758ab1cd |
229 | SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator |
9398955f |
230 | |
2d6979da |
231 | =head1 AUTHORS |
9398955f |
232 | |
758ab1cd |
233 | darren chamberlain E<lt>darren@cpan.orgE<gt>, |
234 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt> |