Commit | Line | Data |
16dc9970 |
1 | package SQL::Translator::Parser::MySQL; |
2 | |
49e1eb70 |
3 | # ------------------------------------------------------------------- |
371f5f88 |
4 | # $Id: MySQL.pm,v 1.11 2003-02-15 02:30:59 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> |
077ebf34 |
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 | # ------------------------------------------------------------------- |
16dc9970 |
24 | |
d529894e |
25 | =head1 NAME |
26 | |
27 | SQL::Translator::Parser::MySQL - parser for MySQL |
28 | |
29 | =head1 SYNOPSIS |
30 | |
31 | use SQL::Translator; |
32 | use SQL::Translator::Parser::MySQL; |
33 | |
34 | my $translator = SQL::Translator->new; |
35 | $translator->parser("SQL::Translator::Parser::MySQL"); |
36 | |
37 | =head1 DESCRIPTION |
38 | |
39 | The grammar is influenced heavily by Tim Bunce's "mysql2ora" grammar. |
40 | |
41 | =cut |
42 | |
16dc9970 |
43 | use strict; |
d529894e |
44 | use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ]; |
371f5f88 |
45 | $VERSION = sprintf "%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/; |
8d0f3086 |
46 | $DEBUG = 0 unless defined $DEBUG; |
077ebf34 |
47 | |
d529894e |
48 | use Data::Dumper; |
077ebf34 |
49 | use Parse::RecDescent; |
50 | use Exporter; |
51 | use base qw(Exporter); |
52 | |
53 | @EXPORT_OK = qw(parse); |
54 | |
d529894e |
55 | # Enable warnings within the Parse::RecDescent module. |
56 | $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error |
57 | $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c. |
58 | $::RD_HINT = 1; # Give out hints to help fix problems. |
59 | |
077ebf34 |
60 | my $parser; # should we do this? There's no programmic way to |
61 | # change the grammar, so I think this is safe. |
16dc9970 |
62 | |
d529894e |
63 | $GRAMMAR = q! |
64 | |
65 | { our ( %tables, $table_order ) } |
66 | |
67 | startrule : statement(s) { \%tables } |
68 | |
69 | statement : comment |
61745327 |
70 | | drop |
d529894e |
71 | | create |
72 | | <error> |
73 | |
61745327 |
74 | drop : /drop/i WORD(s) ';' |
75 | |
d529894e |
76 | create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';' |
77 | { |
78 | my $table_name = $item{'table_name'}; |
79 | $tables{ $table_name }{'order'} = ++$table_order; |
80 | $tables{ $table_name }{'table_name'} = $table_name; |
81 | |
61745327 |
82 | my $i = 1; |
d529894e |
83 | for my $definition ( @{ $item[4] } ) { |
84 | if ( $definition->{'type'} eq 'field' ) { |
85 | my $field_name = $definition->{'name'}; |
86 | $tables{ $table_name }{'fields'}{ $field_name } = |
87 | { %$definition, order => $i }; |
88 | $i++; |
89 | |
90 | if ( $definition->{'is_primary_key'} ) { |
91 | push @{ $tables{ $table_name }{'indices'} }, |
92 | { |
93 | type => 'primary_key', |
94 | fields => [ $field_name ], |
16dc9970 |
95 | } |
d529894e |
96 | ; |
97 | } |
dd2ef5ae |
98 | } |
d529894e |
99 | else { |
100 | push @{ $tables{ $table_name }{'indices'} }, |
101 | $definition; |
dd2ef5ae |
102 | } |
d529894e |
103 | } |
dd2ef5ae |
104 | |
d529894e |
105 | for my $opt ( @{ $item{'table_option'} } ) { |
106 | if ( my ( $key, $val ) = each %$opt ) { |
107 | $tables{ $table_name }{'table_options'}{ $key } = $val; |
dd2ef5ae |
108 | } |
d529894e |
109 | } |
110 | } |
dd2ef5ae |
111 | |
d529894e |
112 | create : /CREATE/i unique(?) /(INDEX|KEY)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';' |
113 | { |
114 | push @{ $tables{ $item{'table_name'} }{'indices'} }, |
115 | { |
116 | name => $item[4], |
117 | type => $item[2] ? 'unique' : 'normal', |
118 | fields => $item[8], |
dd2ef5ae |
119 | } |
d529894e |
120 | ; |
121 | } |
dd2ef5ae |
122 | |
d529894e |
123 | create_definition : index |
124 | | field |
125 | | <error> |
126 | |
127 | comment : /^\s*(?:#|-{2}).*\n/ |
128 | |
129 | blank : /\s*/ |
130 | |
131 | field : field_name data_type field_qualifier(s?) |
132 | { |
133 | my %qualifiers = map { %$_ } @{ $item{'field_qualifier'} || [] }; |
134 | my $null = defined $item{'not_null'} ? $item{'not_null'} : 1; |
135 | delete $qualifiers{'not_null'}; |
136 | if ( my @type_quals = @{ $item{'data_type'}{'qualifiers'} || [] } ) { |
137 | $qualifiers{ $_ } = 1 for @type_quals; |
138 | } |
139 | |
140 | $return = { |
141 | type => 'field', |
142 | name => $item{'field_name'}, |
143 | data_type => $item{'data_type'}{'type'}, |
144 | size => $item{'data_type'}{'size'}, |
145 | list => $item{'data_type'}{'list'}, |
146 | null => $null, |
147 | %qualifiers, |
148 | } |
149 | } |
150 | | <error> |
dd2ef5ae |
151 | |
d529894e |
152 | field_qualifier : not_null |
153 | { |
154 | $return = { |
155 | null => $item{'not_null'}, |
156 | } |
157 | } |
16dc9970 |
158 | |
d529894e |
159 | field_qualifier : default_val |
160 | { |
161 | $return = { |
162 | default => $item{'default_val'}, |
163 | } |
164 | } |
16dc9970 |
165 | |
d529894e |
166 | field_qualifier : auto_inc |
167 | { |
168 | $return = { |
169 | is_auto_inc => $item{'auto_inc'}, |
170 | } |
171 | } |
16dc9970 |
172 | |
d529894e |
173 | field_qualifier : primary_key |
174 | { |
175 | $return = { |
176 | is_primary_key => $item{'primary_key'}, |
177 | } |
178 | } |
16dc9970 |
179 | |
d529894e |
180 | field_qualifier : unsigned |
181 | { |
182 | $return = { |
183 | is_unsigned => $item{'unsigned'}, |
184 | } |
185 | } |
16dc9970 |
186 | |
d529894e |
187 | index : primary_key_index |
188 | | unique_index |
371f5f88 |
189 | | fulltext_index |
d529894e |
190 | | normal_index |
191 | |
192 | table_name : WORD |
193 | |
194 | field_name : WORD |
195 | |
196 | index_name : WORD |
197 | |
198 | data_type : WORD parens_value_list(s?) type_qualifier(s?) |
199 | { |
200 | my $type = $item[1]; |
201 | my $size; # field size, applicable only to non-set fields |
202 | my $list; # set list, applicable only to sets (duh) |
203 | |
44fcd0b5 |
204 | if ( uc($type) =~ /^(SET|ENUM)$/ ) { |
d529894e |
205 | $size = undef; |
206 | $list = $item[2][0]; |
207 | } |
208 | else { |
209 | $size = $item[2][0]; |
210 | $list = []; |
211 | } |
212 | |
213 | $return = { |
214 | type => $type, |
215 | size => $size, |
216 | list => $list, |
217 | qualifiers => $item[3], |
218 | } |
219 | } |
16dc9970 |
220 | |
d529894e |
221 | parens_value_list : '(' VALUE(s /,/) ')' |
222 | { $item[2] } |
16dc9970 |
223 | |
d529894e |
224 | type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i |
225 | { lc $item[1] } |
16dc9970 |
226 | |
d529894e |
227 | field_type : WORD |
16dc9970 |
228 | |
d529894e |
229 | field_size : '(' num_range ')' { $item{'num_range'} } |
16dc9970 |
230 | |
d529894e |
231 | num_range : DIGITS ',' DIGITS |
232 | { $return = $item[1].','.$item[3] } |
233 | | DIGITS |
234 | { $return = $item[1] } |
dd2ef5ae |
235 | |
d529894e |
236 | create_table : /create/i /table/i |
16dc9970 |
237 | |
d529894e |
238 | create_index : /create/i /index/i |
dd2ef5ae |
239 | |
d529894e |
240 | not_null : /not/i /null/i { $return = 0 } |
16dc9970 |
241 | |
d529894e |
242 | unsigned : /unsigned/i { $return = 0 } |
16dc9970 |
243 | |
d529894e |
244 | default_val : /default/i /(?:')?[\w\d.-]*(?:')?/ |
245 | { |
246 | $item[2] =~ s/'//g; |
247 | $return = $item[2]; |
248 | } |
16dc9970 |
249 | |
d529894e |
250 | auto_inc : /auto_increment/i { 1 } |
16dc9970 |
251 | |
d529894e |
252 | primary_key : /primary/i /key/i { 1 } |
16dc9970 |
253 | |
d529894e |
254 | primary_key_index : primary_key index_name(?) '(' field_name(s /,/) ')' |
255 | { |
256 | $return = { |
257 | name => $item{'index_name'}[0], |
258 | type => 'primary_key', |
259 | fields => $item[4], |
260 | } |
261 | } |
16dc9970 |
262 | |
d529894e |
263 | normal_index : key index_name(?) '(' name_with_opt_paren(s /,/) ')' |
264 | { |
265 | $return = { |
266 | name => $item{'index_name'}[0], |
267 | type => 'normal', |
268 | fields => $item[4], |
269 | } |
270 | } |
16dc9970 |
271 | |
d529894e |
272 | unique_index : unique key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')' |
273 | { |
274 | $return = { |
275 | name => $item{'index_name'}[0], |
276 | type => 'unique', |
277 | fields => $item[5], |
278 | } |
279 | } |
16dc9970 |
280 | |
371f5f88 |
281 | fulltext_index : fulltext key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')' |
282 | { |
283 | $return = { |
284 | name => $item{'index_name'}[0], |
285 | type => 'fulltext', |
286 | fields => $item[5], |
287 | } |
288 | } |
289 | |
d529894e |
290 | name_with_opt_paren : NAME parens_value_list(s?) |
291 | { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] } |
16dc9970 |
292 | |
371f5f88 |
293 | fulltext : /fulltext/i { 1 } |
294 | |
d529894e |
295 | unique : /unique/i { 1 } |
16dc9970 |
296 | |
d529894e |
297 | key : /key/i | /index/i |
16dc9970 |
298 | |
44fcd0b5 |
299 | table_option : /[^\s;]*/ |
d529894e |
300 | { |
301 | $return = { split /=/, $item[1] } |
302 | } |
16dc9970 |
303 | |
d529894e |
304 | WORD : /\w+/ |
16dc9970 |
305 | |
d529894e |
306 | DIGITS : /\d+/ |
16dc9970 |
307 | |
d529894e |
308 | COMMA : ',' |
16dc9970 |
309 | |
d529894e |
310 | NAME : "`" /\w+/ "`" |
311 | { $item[2] } |
312 | | /\w+/ |
313 | { $item[1] } |
16dc9970 |
314 | |
d529894e |
315 | VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ |
316 | { $item[1] } |
317 | | /'.*?'/ # XXX doesn't handle embedded quotes |
318 | { $item[1] } |
319 | | /NULL/ |
320 | { 'NULL' } |
16dc9970 |
321 | |
d529894e |
322 | !; |
16dc9970 |
323 | |
d529894e |
324 | # ------------------------------------------------------------------- |
325 | sub parse { |
326 | my ( $translator, $data ) = @_; |
327 | $parser ||= Parse::RecDescent->new($GRAMMAR); |
077ebf34 |
328 | |
d529894e |
329 | $::RD_TRACE = $translator->trace ? 1 : undef; |
330 | $DEBUG = $translator->debug; |
331 | |
332 | unless (defined $parser) { |
333 | return $translator->error("Error instantiating Parse::RecDescent ". |
334 | "instance: Bad grammer"); |
335 | } |
336 | |
337 | my $result = $parser->startrule($data); |
338 | die "Parse failed.\n" unless defined $result; |
339 | warn Dumper($result) if $DEBUG; |
340 | return $result; |
341 | } |
342 | |
343 | 1; |
344 | |
345 | #----------------------------------------------------- |
346 | # Where man is not nature is barren. |
347 | # William Blake |
348 | #----------------------------------------------------- |
16dc9970 |
349 | |
d529894e |
350 | =pod |
16dc9970 |
351 | |
352 | =head1 AUTHOR |
353 | |
d529894e |
354 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, |
355 | Chris Mungall |
16dc9970 |
356 | |
357 | =head1 SEE ALSO |
358 | |
d529894e |
359 | perl(1), Parse::RecDescent. |
16dc9970 |
360 | |
361 | =cut |