1 package SQL::Translator::Parser::Access;
3 # -------------------------------------------------------------------
4 # $Id: Access.pm,v 1.2 2004-07-30 21:56:18 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 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 $VERSION $GRAMMAR @EXPORT_OK ];
44 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
45 $DEBUG = 0 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.
62 my ( %tables, $table_order, @table_comments );
66 # The "eofile" rule makes the parser fail if any "statement" rule
67 # fails. Otherwise, the first successful match by a "statement"
68 # won't cause the failure needed to know that the parse, as a whole,
71 startrule : statement(s) eofile { \%tables }
83 { @table_comments = () }
85 set : /set/i /[^;]+/ ';'
86 { @table_comments = () }
88 drop : /drop/i TABLE /[^;]+/ ';'
90 drop : /drop/i WORD(s) ';'
91 { @table_comments = () }
93 create : CREATE /database/i WORD ';'
94 { @table_comments = () }
96 create : CREATE TABLE table_name '(' create_definition(s /,/) ')' ';'
98 my $table_name = $item{'table_name'};
99 $tables{ $table_name }{'order'} = ++$table_order;
100 $tables{ $table_name }{'table_name'} = $table_name;
102 if ( @table_comments ) {
103 $tables{ $table_name }{'comments'} = [ @table_comments ];
104 @table_comments = ();
108 for my $definition ( @{ $item[5] } ) {
109 if ( $definition->{'supertype'} eq 'field' ) {
110 my $field_name = $definition->{'name'};
111 $tables{ $table_name }{'fields'}{ $field_name } =
112 { %$definition, order => $i };
115 if ( $definition->{'is_primary_key'} ) {
116 push @{ $tables{ $table_name }{'constraints'} },
118 type => 'primary_key',
119 fields => [ $field_name ],
124 elsif ( $definition->{'supertype'} eq 'constraint' ) {
125 push @{ $tables{ $table_name }{'constraints'} }, $definition;
127 elsif ( $definition->{'supertype'} eq 'index' ) {
128 push @{ $tables{ $table_name }{'indices'} }, $definition;
135 create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';'
137 @table_comments = ();
138 push @{ $tables{ $item{'table_name'} }{'indices'} },
141 type => $item[2] ? 'unique' : 'normal',
147 create_definition : constraint
153 comment : /^\s*--(.*)\n/
157 push @table_comments, $comment;
160 field : field_name data_type field_qualifier(s?) reference_definition(?)
163 supertype => 'field',
164 name => $item{'field_name'},
165 data_type => $item{'data_type'}{'type'},
166 size => $item{'data_type'}{'size'},
167 constraints => $item{'reference_definition(?)'},
172 field_qualifier : not_null
175 null => $item{'not_null'},
179 field_qualifier : default_val
182 default => $item{'default_val'},
186 field_qualifier : auto_inc
189 is_auto_inc => $item{'auto_inc'},
193 field_qualifier : primary_key
196 is_primary_key => $item{'primary_key'},
200 field_qualifier : unsigned
203 is_unsigned => $item{'unsigned'},
207 field_qualifier : /character set/i WORD
210 character_set => $item[2],
214 reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete_do(?) on_update_do(?)
217 type => 'foreign_key',
218 reference_table => $item[2],
219 reference_fields => $item[3][0],
220 match_type => $item[4][0],
221 on_delete_do => $item[5][0],
222 on_update_do => $item[6][0],
226 match_type : /match full/i { 'full' }
228 /match partial/i { 'partial' }
230 on_delete_do : /on delete/i reference_option
233 on_update_do : /on update/i reference_option
236 reference_option: /restrict/i |
253 data_type : access_data_type parens_value_list(s?) type_qualifier(s?)
258 qualifiers => $item[3],
262 access_data_type : /long integer/i { $return = 'Long Integer' }
263 | /text/i { $return = 'Text' }
264 | /datetime (\(short\))?/i { $return = 'DateTime' }
265 | /boolean/i { $return = 'Boolean' }
268 parens_field_list : '(' field_name(s /,/) ')'
271 parens_value_list : '(' VALUE(s /,/) ')'
274 type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
279 create_index : /create/i /index/i
281 not_null : /not/i /null/i { $return = 0 }
283 unsigned : /unsigned/i { $return = 0 }
285 default_val : /default/i /'(?:.*?\\')*.*?'|(?:')?[\w\d:.-]*(?:')?/
287 $item[2] =~ s/^\s*'|'\s*$//g;
291 auto_inc : /auto_increment/i { 1 }
293 primary_key : /primary/i /key/i { 1 }
295 constraint : primary_key_def
300 foreign_key_def : foreign_key_def_begin parens_field_list reference_definition
303 supertype => 'constraint',
304 type => 'foreign_key',
307 %{ $item{'reference_definition'} },
311 foreign_key_def_begin : /constraint/i /foreign key/i
314 /constraint/i WORD /foreign key/i
315 { $return = $item[2] }
320 primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')'
323 supertype => 'constraint',
324 name => $item{'index_name(?)'}[0],
325 type => 'primary_key',
330 unique_key_def : UNIQUE KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
333 supertype => 'constraint',
334 name => $item{'index_name(?)'}[0],
340 normal_index : KEY index_name(?) '(' name_with_opt_paren(s /,/) ')'
343 supertype => 'index',
345 name => $item{'index_name(?)'}[0],
350 fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
353 supertype => 'index',
355 name => $item{'index_name(?)'}[0],
360 name_with_opt_paren : NAME parens_value_list(s?)
361 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
363 UNIQUE : /unique/i { 1 }
365 KEY : /key/i | /index/i
367 table_option : WORD /\s*=\s*/ WORD
369 $return = { $item[1] => $item[3] };
374 TEMPORARY : /temporary/i
389 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
393 # remove leading/trailing quotes
395 $val =~ s/^['"]|['"]$//g;
403 # -------------------------------------------------------------------
405 my ( $translator, $data ) = @_;
406 my $parser = Parse::RecDescent->new($GRAMMAR);
408 local $::RD_TRACE = $translator->trace ? 1 : undef;
409 local $DEBUG = $translator->debug;
411 unless (defined $parser) {
412 return $translator->error("Error instantiating Parse::RecDescent ".
413 "instance: Bad grammer");
416 my $result = $parser->startrule($data);
417 return $translator->error( "Parse failed." ) unless defined $result;
418 warn Dumper( $result ) if $DEBUG;
420 my $schema = $translator->schema;
422 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
425 for my $table_name ( @tables ) {
426 my $tdata = $result->{ $table_name };
427 my $table = $schema->add_table(
428 name => $tdata->{'table_name'},
429 ) or die $schema->error;
431 $table->comments( $tdata->{'comments'} );
434 $tdata->{'fields'}->{$a}->{'order'}
436 $tdata->{'fields'}->{$b}->{'order'}
437 } keys %{ $tdata->{'fields'} };
439 for my $fname ( @fields ) {
440 my $fdata = $tdata->{'fields'}{ $fname };
441 my $field = $table->add_field(
442 name => $fdata->{'name'},
443 data_type => $fdata->{'data_type'},
444 size => $fdata->{'size'},
445 default_value => $fdata->{'default'},
446 is_auto_increment => $fdata->{'is_auto_inc'},
447 is_nullable => $fdata->{'null'},
448 comments => $fdata->{'comments'},
449 ) or die $table->error;
451 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
454 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
455 my $index = $table->add_index(
456 name => $idata->{'name'},
457 type => uc $idata->{'type'},
458 fields => $idata->{'fields'},
459 ) or die $table->error;
468 # -------------------------------------------------------------------
469 # Where man is not nature is barren.
471 # -------------------------------------------------------------------
477 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
481 perl(1), Parse::RecDescent, SQL::Translator::Schema.