1 package SQL::Translator::Parser::Access;
3 # -------------------------------------------------------------------
4 # $Id: Access.pm,v 1.1 2004-04-19 16:38:17 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.1 $ =~ /(\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(?)
162 # my %qualifiers = map { %$_ } @{ $item{'field_qualifier(s?)'} || [] };
163 # if ( my @type_quals = @{ $item{'data_type'}{'qualifiers'} || [] } ) {
164 # $qualifiers{ $_ } = 1 for @type_quals;
167 # my $null = defined $qualifiers{'not_null'}
168 # ? $qualifiers{'not_null'} : 1;
169 # delete $qualifiers{'not_null'};
172 supertype => 'field',
173 name => $item{'field_name'},
174 data_type => $item{'data_type'}{'type'},
175 size => $item{'data_type'}{'size'},
177 constraints => $item{'reference_definition(?)'},
183 field_qualifier : not_null
186 null => $item{'not_null'},
190 field_qualifier : default_val
193 default => $item{'default_val'},
197 field_qualifier : auto_inc
200 is_auto_inc => $item{'auto_inc'},
204 field_qualifier : primary_key
207 is_primary_key => $item{'primary_key'},
211 field_qualifier : unsigned
214 is_unsigned => $item{'unsigned'},
218 field_qualifier : /character set/i WORD
221 character_set => $item[2],
225 reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete_do(?) on_update_do(?)
228 type => 'foreign_key',
229 reference_table => $item[2],
230 reference_fields => $item[3][0],
231 match_type => $item[4][0],
232 on_delete_do => $item[5][0],
233 on_update_do => $item[6][0],
237 match_type : /match full/i { 'full' }
239 /match partial/i { 'partial' }
241 on_delete_do : /on delete/i reference_option
244 on_update_do : /on update/i reference_option
247 reference_option: /restrict/i |
264 data_type : access_data_type parens_value_list(s?) type_qualifier(s?)
269 qualifiers => $item[3],
273 access_data_type : /long integer/i { $return = 'Long Integer' }
274 | /text/i { $return = 'Text' }
275 | /datetime (\(short\))?/i { $return = 'DateTime' }
276 | /boolean/i { $return = 'Boolean' }
279 parens_field_list : '(' field_name(s /,/) ')'
282 parens_value_list : '(' VALUE(s /,/) ')'
285 type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
290 create_index : /create/i /index/i
292 not_null : /not/i /null/i { $return = 0 }
294 unsigned : /unsigned/i { $return = 0 }
296 default_val : /default/i /'(?:.*?\\')*.*?'|(?:')?[\w\d:.-]*(?:')?/
298 $item[2] =~ s/^\s*'|'\s*$//g;
302 auto_inc : /auto_increment/i { 1 }
304 primary_key : /primary/i /key/i { 1 }
306 constraint : primary_key_def
311 foreign_key_def : foreign_key_def_begin parens_field_list reference_definition
314 supertype => 'constraint',
315 type => 'foreign_key',
318 %{ $item{'reference_definition'} },
322 foreign_key_def_begin : /constraint/i /foreign key/i
325 /constraint/i WORD /foreign key/i
326 { $return = $item[2] }
331 primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')'
334 supertype => 'constraint',
335 name => $item{'index_name(?)'}[0],
336 type => 'primary_key',
341 unique_key_def : UNIQUE KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
344 supertype => 'constraint',
345 name => $item{'index_name(?)'}[0],
351 normal_index : KEY index_name(?) '(' name_with_opt_paren(s /,/) ')'
354 supertype => 'index',
356 name => $item{'index_name(?)'}[0],
361 fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
364 supertype => 'index',
366 name => $item{'index_name(?)'}[0],
371 name_with_opt_paren : NAME parens_value_list(s?)
372 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
374 UNIQUE : /unique/i { 1 }
376 KEY : /key/i | /index/i
378 table_option : WORD /\s*=\s*/ WORD
380 $return = { $item[1] => $item[3] };
385 TEMPORARY : /temporary/i
400 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
404 # remove leading/trailing quotes
406 $val =~ s/^['"]|['"]$//g;
414 # -------------------------------------------------------------------
416 my ( $translator, $data ) = @_;
417 my $parser = Parse::RecDescent->new($GRAMMAR);
419 local $::RD_TRACE = $translator->trace ? 1 : undef;
420 local $DEBUG = $translator->debug;
422 unless (defined $parser) {
423 return $translator->error("Error instantiating Parse::RecDescent ".
424 "instance: Bad grammer");
427 my $result = $parser->startrule($data);
428 return $translator->error( "Parse failed." ) unless defined $result;
429 warn Dumper( $result ) if $DEBUG;
431 my $schema = $translator->schema;
433 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
436 for my $table_name ( @tables ) {
437 my $tdata = $result->{ $table_name };
438 my $table = $schema->add_table(
439 name => $tdata->{'table_name'},
440 ) or die $schema->error;
442 $table->comments( $tdata->{'comments'} );
445 $tdata->{'fields'}->{$a}->{'order'}
447 $tdata->{'fields'}->{$b}->{'order'}
448 } keys %{ $tdata->{'fields'} };
450 for my $fname ( @fields ) {
451 my $fdata = $tdata->{'fields'}{ $fname };
452 my $field = $table->add_field(
453 name => $fdata->{'name'},
454 data_type => $fdata->{'data_type'},
455 size => $fdata->{'size'},
456 default_value => $fdata->{'default'},
457 is_auto_increment => $fdata->{'is_auto_inc'},
458 is_nullable => $fdata->{'null'},
459 comments => $fdata->{'comments'},
460 ) or die $table->error;
462 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
464 # for my $qual ( qw[ binary unsigned zerofill list ] ) {
465 # if ( my $val = $fdata->{ $qual } || $fdata->{ uc $qual } ) {
466 # next if ref $val eq 'ARRAY' && !@$val;
467 # $field->extra( $qual, $val );
471 # for my $cdata ( @{ $fdata->{'constraints'} } ) {
472 # next unless $cdata->{'type'} eq 'foreign_key';
473 # $cdata->{'fields'} ||= [ $field->name ];
474 # push @{ $tdata->{'constraints'} }, $cdata;
478 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
479 my $index = $table->add_index(
480 name => $idata->{'name'},
481 type => uc $idata->{'type'},
482 fields => $idata->{'fields'},
483 ) or die $table->error;
486 # for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
487 # my $constraint = $table->add_constraint(
488 # name => $cdata->{'name'},
489 # type => $cdata->{'type'},
490 # fields => $cdata->{'fields'},
491 # reference_table => $cdata->{'reference_table'},
492 # reference_fields => $cdata->{'reference_fields'},
493 # match_type => $cdata->{'match_type'} || '',
494 # on_delete => $cdata->{'on_delete_do'},
495 # on_update => $cdata->{'on_update_do'},
496 # ) or die $table->error;
505 # -------------------------------------------------------------------
506 # Where man is not nature is barren.
508 # -------------------------------------------------------------------
514 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
518 perl(1), Parse::RecDescent, SQL::Translator::Schema.