1 package SQL::Translator::Parser::MySQL;
3 # -------------------------------------------------------------------
4 # $Id: MySQL.pm,v 1.8 2002-11-28 04:21:06 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002 Ken Y. Clark <kclark@cpan.org>,
7 # darren chamberlain <darren@cpan.org>
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; version 2.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # -------------------------------------------------------------------
26 SQL::Translator::Parser::MySQL - parser for MySQL
31 use SQL::Translator::Parser::MySQL;
33 my $translator = SQL::Translator->new;
34 $translator->parser("SQL::Translator::Parser::MySQL");
38 The grammar is influenced heavily by Tim Bunce's "mysql2ora" grammar.
43 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
44 $VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
45 $DEBUG = 1 unless defined $DEBUG;
48 use Parse::RecDescent;
50 use base qw(Exporter);
52 @EXPORT_OK = qw(parse);
54 # Enable warnings within the Parse::RecDescent module.
55 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
56 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
57 $::RD_HINT = 1; # Give out hints to help fix problems.
59 my $parser; # should we do this? There's no programmic way to
60 # change the grammar, so I think this is safe.
64 { our ( %tables, $table_order ) }
66 startrule : statement(s) { \%tables }
73 drop : /drop/i WORD(s) ';'
75 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
77 my $table_name = $item{'table_name'};
78 $tables{ $table_name }{'order'} = ++$table_order;
79 $tables{ $table_name }{'table_name'} = $table_name;
82 for my $definition ( @{ $item[4] } ) {
83 if ( $definition->{'type'} eq 'field' ) {
84 my $field_name = $definition->{'name'};
85 $tables{ $table_name }{'fields'}{ $field_name } =
86 { %$definition, order => $i };
89 if ( $definition->{'is_primary_key'} ) {
90 push @{ $tables{ $table_name }{'indices'} },
92 type => 'primary_key',
93 fields => [ $field_name ],
99 push @{ $tables{ $table_name }{'indices'} },
104 for my $opt ( @{ $item{'table_option'} } ) {
105 if ( my ( $key, $val ) = each %$opt ) {
106 $tables{ $table_name }{'table_options'}{ $key } = $val;
111 create : /CREATE/i unique(?) /(INDEX|KEY)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';'
113 push @{ $tables{ $item{'table_name'} }{'indices'} },
116 type => $item[2] ? 'unique' : 'normal',
122 create_definition : index
126 comment : /^\s*(?:#|-{2}).*\n/
130 field : field_name data_type field_qualifier(s?)
132 my %qualifiers = map { %$_ } @{ $item{'field_qualifier'} || [] };
133 my $null = defined $item{'not_null'} ? $item{'not_null'} : 1;
134 delete $qualifiers{'not_null'};
135 if ( my @type_quals = @{ $item{'data_type'}{'qualifiers'} || [] } ) {
136 $qualifiers{ $_ } = 1 for @type_quals;
141 name => $item{'field_name'},
142 data_type => $item{'data_type'}{'type'},
143 size => $item{'data_type'}{'size'},
144 list => $item{'data_type'}{'list'},
151 field_qualifier : not_null
154 null => $item{'not_null'},
158 field_qualifier : default_val
161 default => $item{'default_val'},
165 field_qualifier : auto_inc
168 is_auto_inc => $item{'auto_inc'},
172 field_qualifier : primary_key
175 is_primary_key => $item{'primary_key'},
179 field_qualifier : unsigned
182 is_unsigned => $item{'unsigned'},
186 index : primary_key_index
196 data_type : WORD parens_value_list(s?) type_qualifier(s?)
199 my $size; # field size, applicable only to non-set fields
200 my $list; # set list, applicable only to sets (duh)
202 if ( uc($type) =~ /^(SET|ENUM)$/ ) {
215 qualifiers => $item[3],
219 parens_value_list : '(' VALUE(s /,/) ')'
222 type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
227 field_size : '(' num_range ')' { $item{'num_range'} }
229 num_range : DIGITS ',' DIGITS
230 { $return = $item[1].','.$item[3] }
232 { $return = $item[1] }
234 create_table : /create/i /table/i
236 create_index : /create/i /index/i
238 not_null : /not/i /null/i { $return = 0 }
240 unsigned : /unsigned/i { $return = 0 }
242 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
248 auto_inc : /auto_increment/i { 1 }
250 primary_key : /primary/i /key/i { 1 }
252 primary_key_index : primary_key index_name(?) '(' field_name(s /,/) ')'
255 name => $item{'index_name'}[0],
256 type => 'primary_key',
261 normal_index : key index_name(?) '(' name_with_opt_paren(s /,/) ')'
264 name => $item{'index_name'}[0],
270 unique_index : unique key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
273 name => $item{'index_name'}[0],
279 name_with_opt_paren : NAME parens_value_list(s?)
280 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
282 unique : /unique/i { 1 }
284 key : /key/i | /index/i
286 table_option : /[^\s;]*/
288 $return = { split /=/, $item[1] }
302 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
304 | /'.*?'/ # XXX doesn't handle embedded quotes
311 # -------------------------------------------------------------------
313 my ( $translator, $data ) = @_;
314 $parser ||= Parse::RecDescent->new($GRAMMAR);
316 $::RD_TRACE = $translator->trace ? 1 : undef;
317 $DEBUG = $translator->debug;
319 unless (defined $parser) {
320 return $translator->error("Error instantiating Parse::RecDescent ".
321 "instance: Bad grammer");
324 my $result = $parser->startrule($data);
325 die "Parse failed.\n" unless defined $result;
326 warn Dumper($result) if $DEBUG;
332 #-----------------------------------------------------
333 # Where man is not nature is barren.
335 #-----------------------------------------------------
341 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
346 perl(1), Parse::RecDescent.