1 package SQL::Translator::Parser::Access;
3 # -------------------------------------------------------------------
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::Access - parser for Access as produced by mdbtools
30 use SQL::Translator::Parser::Access;
32 my $translator = SQL::Translator->new;
33 $translator->parser("SQL::Translator::Parser::Access");
37 The grammar derived from the MySQL grammar. The input is expected to be
38 something similar to the output of mdbtools (http://mdbtools.sourceforge.net/).
43 use vars qw[ $DEBUG $GRAMMAR @EXPORT_OK ];
44 $DEBUG = 0 unless defined $DEBUG;
47 use Parse::RecDescent;
49 use base qw(Exporter);
51 @EXPORT_OK = qw(parse);
53 # Enable warnings within the Parse::RecDescent module.
54 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
55 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
56 $::RD_HINT = 1; # Give out hints to help fix problems.
61 my ( %tables, $table_order, @table_comments );
65 # The "eofile" rule makes the parser fail if any "statement" rule
66 # fails. Otherwise, the first successful match by a "statement"
67 # won't cause the failure needed to know that the parse, as a whole,
70 startrule : statement(s) eofile { \%tables }
82 { @table_comments = () }
84 set : /set/i /[^;]+/ ';'
85 { @table_comments = () }
87 drop : /drop/i TABLE /[^;]+/ ';'
89 drop : /drop/i WORD(s) ';'
90 { @table_comments = () }
92 create : CREATE /database/i WORD ';'
93 { @table_comments = () }
95 create : CREATE TABLE table_name '(' create_definition(s /,/) ')' ';'
97 my $table_name = $item{'table_name'};
98 $tables{ $table_name }{'order'} = ++$table_order;
99 $tables{ $table_name }{'table_name'} = $table_name;
101 if ( @table_comments ) {
102 $tables{ $table_name }{'comments'} = [ @table_comments ];
103 @table_comments = ();
107 for my $definition ( @{ $item[5] } ) {
108 if ( $definition->{'supertype'} eq 'field' ) {
109 my $field_name = $definition->{'name'};
110 $tables{ $table_name }{'fields'}{ $field_name } =
111 { %$definition, order => $i };
114 if ( $definition->{'is_primary_key'} ) {
115 push @{ $tables{ $table_name }{'constraints'} },
117 type => 'primary_key',
118 fields => [ $field_name ],
123 elsif ( $definition->{'supertype'} eq 'constraint' ) {
124 push @{ $tables{ $table_name }{'constraints'} }, $definition;
126 elsif ( $definition->{'supertype'} eq 'index' ) {
127 push @{ $tables{ $table_name }{'indices'} }, $definition;
134 create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';'
136 @table_comments = ();
137 push @{ $tables{ $item{'table_name'} }{'indices'} },
140 type => $item[2] ? 'unique' : 'normal',
146 create_definition : constraint
152 comment : /^\s*--(.*)\n/
156 push @table_comments, $comment;
159 field : field_name data_type field_qualifier(s?) reference_definition(?)
162 supertype => 'field',
163 name => $item{'field_name'},
164 data_type => $item{'data_type'}{'type'},
165 size => $item{'data_type'}{'size'},
166 constraints => $item{'reference_definition(?)'},
171 field_qualifier : not_null
174 null => $item{'not_null'},
178 field_qualifier : default_val
181 default => $item{'default_val'},
185 field_qualifier : auto_inc
188 is_auto_inc => $item{'auto_inc'},
192 field_qualifier : primary_key
195 is_primary_key => $item{'primary_key'},
199 field_qualifier : unsigned
202 is_unsigned => $item{'unsigned'},
206 field_qualifier : /character set/i WORD
209 character_set => $item[2],
213 reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete(?) on_update(?)
216 type => 'foreign_key',
217 reference_table => $item[2],
218 reference_fields => $item[3][0],
219 match_type => $item[4][0],
220 on_delete => $item[5][0],
221 on_update => $item[6][0],
225 match_type : /match full/i { 'full' }
227 /match partial/i { 'partial' }
229 on_delete : /on delete/i reference_option
232 on_update : /on update/i reference_option
235 reference_option: /restrict/i |
252 data_type : access_data_type parens_value_list(s?) type_qualifier(s?)
257 qualifiers => $item[3],
261 access_data_type : /long integer/i { $return = 'Long Integer' }
262 | /text/i { $return = 'Text' }
263 | /datetime (\(short\))?/i { $return = 'DateTime' }
264 | /boolean/i { $return = 'Boolean' }
267 parens_field_list : '(' field_name(s /,/) ')'
270 parens_value_list : '(' VALUE(s /,/) ')'
273 type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
278 create_index : /create/i /index/i
280 not_null : /not/i /null/i { $return = 0 }
282 unsigned : /unsigned/i { $return = 0 }
284 default_val : /default/i /'(?:.*?\\')*.*?'|(?:')?[\w\d:.-]*(?:')?/
286 $item[2] =~ s/^\s*'|'\s*$//g;
290 auto_inc : /auto_increment/i { 1 }
292 primary_key : /primary/i /key/i { 1 }
294 constraint : primary_key_def
299 foreign_key_def : foreign_key_def_begin parens_field_list reference_definition
302 supertype => 'constraint',
303 type => 'foreign_key',
306 %{ $item{'reference_definition'} },
310 foreign_key_def_begin : /constraint/i /foreign key/i
313 /constraint/i WORD /foreign key/i
314 { $return = $item[2] }
319 primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')'
322 supertype => 'constraint',
323 name => $item{'index_name(?)'}[0],
324 type => 'primary_key',
329 unique_key_def : UNIQUE KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
332 supertype => 'constraint',
333 name => $item{'index_name(?)'}[0],
339 normal_index : KEY index_name(?) '(' name_with_opt_paren(s /,/) ')'
342 supertype => 'index',
344 name => $item{'index_name(?)'}[0],
349 fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
352 supertype => 'index',
354 name => $item{'index_name(?)'}[0],
359 name_with_opt_paren : NAME parens_value_list(s?)
360 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
362 UNIQUE : /unique/i { 1 }
364 KEY : /key/i | /index/i
366 table_option : WORD /\s*=\s*/ WORD
368 $return = { $item[1] => $item[3] };
373 TEMPORARY : /temporary/i
388 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
392 # remove leading/trailing quotes
394 $val =~ s/^['"]|['"]$//g;
402 # -------------------------------------------------------------------
404 my ( $translator, $data ) = @_;
405 my $parser = Parse::RecDescent->new($GRAMMAR);
407 local $::RD_TRACE = $translator->trace ? 1 : undef;
408 local $DEBUG = $translator->debug;
410 unless (defined $parser) {
411 return $translator->error("Error instantiating Parse::RecDescent ".
412 "instance: Bad grammer");
415 my $result = $parser->startrule($data);
416 return $translator->error( "Parse failed." ) unless defined $result;
417 warn Dumper( $result ) if $DEBUG;
419 my $schema = $translator->schema;
421 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
424 for my $table_name ( @tables ) {
425 my $tdata = $result->{ $table_name };
426 my $table = $schema->add_table(
427 name => $tdata->{'table_name'},
428 ) or die $schema->error;
430 $table->comments( $tdata->{'comments'} );
433 $tdata->{'fields'}->{$a}->{'order'}
435 $tdata->{'fields'}->{$b}->{'order'}
436 } keys %{ $tdata->{'fields'} };
438 for my $fname ( @fields ) {
439 my $fdata = $tdata->{'fields'}{ $fname };
440 my $field = $table->add_field(
441 name => $fdata->{'name'},
442 data_type => $fdata->{'data_type'},
443 size => $fdata->{'size'},
444 default_value => $fdata->{'default'},
445 is_auto_increment => $fdata->{'is_auto_inc'},
446 is_nullable => $fdata->{'null'},
447 comments => $fdata->{'comments'},
448 ) or die $table->error;
450 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
453 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
454 my $index = $table->add_index(
455 name => $idata->{'name'},
456 type => uc $idata->{'type'},
457 fields => $idata->{'fields'},
458 ) or die $table->error;
467 # -------------------------------------------------------------------
468 # Where man is not nature is barren.
470 # -------------------------------------------------------------------
476 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
480 perl(1), Parse::RecDescent, SQL::Translator::Schema.