1 package SQL::Translator::Parser::MySQL;
3 # -------------------------------------------------------------------
4 # $Id: MySQL.pm,v 1.11 2003-02-15 02:30:59 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 # darren chamberlain <darren@cpan.org>,
8 # Chris Mungall <cjm@fruitfly.org>
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.
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.
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
23 # -------------------------------------------------------------------
27 SQL::Translator::Parser::MySQL - parser for MySQL
32 use SQL::Translator::Parser::MySQL;
34 my $translator = SQL::Translator->new;
35 $translator->parser("SQL::Translator::Parser::MySQL");
39 The grammar is influenced heavily by Tim Bunce's "mysql2ora" grammar.
44 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
45 $VERSION = sprintf "%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/;
46 $DEBUG = 0 unless defined $DEBUG;
49 use Parse::RecDescent;
51 use base qw(Exporter);
53 @EXPORT_OK = qw(parse);
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.
60 my $parser; # should we do this? There's no programmic way to
61 # change the grammar, so I think this is safe.
65 { our ( %tables, $table_order ) }
67 startrule : statement(s) { \%tables }
74 drop : /drop/i WORD(s) ';'
76 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
78 my $table_name = $item{'table_name'};
79 $tables{ $table_name }{'order'} = ++$table_order;
80 $tables{ $table_name }{'table_name'} = $table_name;
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 };
90 if ( $definition->{'is_primary_key'} ) {
91 push @{ $tables{ $table_name }{'indices'} },
93 type => 'primary_key',
94 fields => [ $field_name ],
100 push @{ $tables{ $table_name }{'indices'} },
105 for my $opt ( @{ $item{'table_option'} } ) {
106 if ( my ( $key, $val ) = each %$opt ) {
107 $tables{ $table_name }{'table_options'}{ $key } = $val;
112 create : /CREATE/i unique(?) /(INDEX|KEY)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';'
114 push @{ $tables{ $item{'table_name'} }{'indices'} },
117 type => $item[2] ? 'unique' : 'normal',
123 create_definition : index
127 comment : /^\s*(?:#|-{2}).*\n/
131 field : field_name data_type field_qualifier(s?)
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;
142 name => $item{'field_name'},
143 data_type => $item{'data_type'}{'type'},
144 size => $item{'data_type'}{'size'},
145 list => $item{'data_type'}{'list'},
152 field_qualifier : not_null
155 null => $item{'not_null'},
159 field_qualifier : default_val
162 default => $item{'default_val'},
166 field_qualifier : auto_inc
169 is_auto_inc => $item{'auto_inc'},
173 field_qualifier : primary_key
176 is_primary_key => $item{'primary_key'},
180 field_qualifier : unsigned
183 is_unsigned => $item{'unsigned'},
187 index : primary_key_index
198 data_type : WORD parens_value_list(s?) type_qualifier(s?)
201 my $size; # field size, applicable only to non-set fields
202 my $list; # set list, applicable only to sets (duh)
204 if ( uc($type) =~ /^(SET|ENUM)$/ ) {
217 qualifiers => $item[3],
221 parens_value_list : '(' VALUE(s /,/) ')'
224 type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
229 field_size : '(' num_range ')' { $item{'num_range'} }
231 num_range : DIGITS ',' DIGITS
232 { $return = $item[1].','.$item[3] }
234 { $return = $item[1] }
236 create_table : /create/i /table/i
238 create_index : /create/i /index/i
240 not_null : /not/i /null/i { $return = 0 }
242 unsigned : /unsigned/i { $return = 0 }
244 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
250 auto_inc : /auto_increment/i { 1 }
252 primary_key : /primary/i /key/i { 1 }
254 primary_key_index : primary_key index_name(?) '(' field_name(s /,/) ')'
257 name => $item{'index_name'}[0],
258 type => 'primary_key',
263 normal_index : key index_name(?) '(' name_with_opt_paren(s /,/) ')'
266 name => $item{'index_name'}[0],
272 unique_index : unique key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
275 name => $item{'index_name'}[0],
281 fulltext_index : fulltext key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
284 name => $item{'index_name'}[0],
290 name_with_opt_paren : NAME parens_value_list(s?)
291 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
293 fulltext : /fulltext/i { 1 }
295 unique : /unique/i { 1 }
297 key : /key/i | /index/i
299 table_option : /[^\s;]*/
301 $return = { split /=/, $item[1] }
315 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
317 | /'.*?'/ # XXX doesn't handle embedded quotes
324 # -------------------------------------------------------------------
326 my ( $translator, $data ) = @_;
327 $parser ||= Parse::RecDescent->new($GRAMMAR);
329 $::RD_TRACE = $translator->trace ? 1 : undef;
330 $DEBUG = $translator->debug;
332 unless (defined $parser) {
333 return $translator->error("Error instantiating Parse::RecDescent ".
334 "instance: Bad grammer");
337 my $result = $parser->startrule($data);
338 die "Parse failed.\n" unless defined $result;
339 warn Dumper($result) if $DEBUG;
345 #-----------------------------------------------------
346 # Where man is not nature is barren.
348 #-----------------------------------------------------
354 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
359 perl(1), Parse::RecDescent.