1 package SQL::Translator::Parser::Access;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 SQL::Translator::Parser::Access - parser for Access as produced by mdbtools
28 use SQL::Translator::Parser::Access;
30 my $translator = SQL::Translator->new;
31 $translator->parser("SQL::Translator::Parser::Access");
35 The grammar derived from the MySQL grammar. The input is expected to be
36 something similar to the output of mdbtools (http://mdbtools.sourceforge.net/).
41 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
43 $DEBUG = 0 unless defined $DEBUG;
46 use Parse::RecDescent;
48 use base qw(Exporter);
50 @EXPORT_OK = qw(parse);
52 # Enable warnings within the Parse::RecDescent module.
53 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
54 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
55 $::RD_HINT = 1; # Give out hints to help fix problems.
60 my ( %tables, $table_order, @table_comments );
64 # The "eofile" rule makes the parser fail if any "statement" rule
65 # fails. Otherwise, the first successful match by a "statement"
66 # won't cause the failure needed to know that the parse, as a whole,
69 startrule : statement(s) eofile { \%tables }
81 { @table_comments = () }
83 set : /set/i /[^;]+/ ';'
84 { @table_comments = () }
86 drop : /drop/i TABLE /[^;]+/ ';'
88 drop : /drop/i WORD(s) ';'
89 { @table_comments = () }
91 create : CREATE /database/i WORD ';'
92 { @table_comments = () }
94 create : CREATE TABLE table_name '(' create_definition(s /,/) ')' ';'
96 my $table_name = $item{'table_name'};
97 $tables{ $table_name }{'order'} = ++$table_order;
98 $tables{ $table_name }{'table_name'} = $table_name;
100 if ( @table_comments ) {
101 $tables{ $table_name }{'comments'} = [ @table_comments ];
102 @table_comments = ();
106 for my $definition ( @{ $item[5] } ) {
107 if ( $definition->{'supertype'} eq 'field' ) {
108 my $field_name = $definition->{'name'};
109 $tables{ $table_name }{'fields'}{ $field_name } =
110 { %$definition, order => $i };
113 if ( $definition->{'is_primary_key'} ) {
114 push @{ $tables{ $table_name }{'constraints'} },
116 type => 'primary_key',
117 fields => [ $field_name ],
122 elsif ( $definition->{'supertype'} eq 'constraint' ) {
123 push @{ $tables{ $table_name }{'constraints'} }, $definition;
125 elsif ( $definition->{'supertype'} eq 'index' ) {
126 push @{ $tables{ $table_name }{'indices'} }, $definition;
133 create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';'
135 @table_comments = ();
136 push @{ $tables{ $item{'table_name'} }{'indices'} },
139 type => $item[2] ? 'unique' : 'normal',
145 create_definition : constraint
151 comment : /^\s*--(.*)\n/
155 push @table_comments, $comment;
158 field : field_name data_type field_qualifier(s?) reference_definition(?)
161 supertype => 'field',
162 name => $item{'field_name'},
163 data_type => $item{'data_type'}{'type'},
164 size => $item{'data_type'}{'size'},
165 constraints => $item{'reference_definition(?)'},
170 field_qualifier : not_null
173 null => $item{'not_null'},
177 field_qualifier : default_val
180 default => $item{'default_val'},
184 field_qualifier : auto_inc
187 is_auto_inc => $item{'auto_inc'},
191 field_qualifier : primary_key
194 is_primary_key => $item{'primary_key'},
198 field_qualifier : unsigned
201 is_unsigned => $item{'unsigned'},
205 field_qualifier : /character set/i WORD
208 character_set => $item[2],
212 reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete(?) on_update(?)
215 type => 'foreign_key',
216 reference_table => $item[2],
217 reference_fields => $item[3][0],
218 match_type => $item[4][0],
219 on_delete => $item[5][0],
220 on_update => $item[6][0],
224 match_type : /match full/i { 'full' }
226 /match partial/i { 'partial' }
228 on_delete : /on delete/i reference_option
231 on_update : /on update/i reference_option
234 reference_option: /restrict/i |
251 data_type : access_data_type parens_value_list(s?) type_qualifier(s?)
256 qualifiers => $item[3],
260 access_data_type : /long integer/i { $return = 'Long Integer' }
261 | /text/i { $return = 'Text' }
262 | /datetime (\(short\))?/i { $return = 'DateTime' }
263 | /boolean/i { $return = 'Boolean' }
266 parens_field_list : '(' field_name(s /,/) ')'
269 parens_value_list : '(' VALUE(s /,/) ')'
272 type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
277 create_index : /create/i /index/i
279 not_null : /not/i /null/i { $return = 0 }
281 unsigned : /unsigned/i { $return = 0 }
283 default_val : /default/i /'(?:.*?\\')*.*?'|(?:')?[\w\d:.-]*(?:')?/
285 $item[2] =~ s/^\s*'|'\s*$//g;
289 auto_inc : /auto_increment/i { 1 }
291 primary_key : /primary/i /key/i { 1 }
293 constraint : primary_key_def
298 foreign_key_def : foreign_key_def_begin parens_field_list reference_definition
301 supertype => 'constraint',
302 type => 'foreign_key',
305 %{ $item{'reference_definition'} },
309 foreign_key_def_begin : /constraint/i /foreign key/i
312 /constraint/i WORD /foreign key/i
313 { $return = $item[2] }
318 primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')'
321 supertype => 'constraint',
322 name => $item{'index_name(?)'}[0],
323 type => 'primary_key',
328 unique_key_def : UNIQUE KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
331 supertype => 'constraint',
332 name => $item{'index_name(?)'}[0],
338 normal_index : KEY index_name(?) '(' name_with_opt_paren(s /,/) ')'
341 supertype => 'index',
343 name => $item{'index_name(?)'}[0],
348 fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
351 supertype => 'index',
353 name => $item{'index_name(?)'}[0],
358 name_with_opt_paren : NAME parens_value_list(s?)
359 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
361 UNIQUE : /unique/i { 1 }
363 KEY : /key/i | /index/i
365 table_option : WORD /\s*=\s*/ WORD
367 $return = { $item[1] => $item[3] };
372 TEMPORARY : /temporary/i
387 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
391 # remove leading/trailing quotes
393 $val =~ s/^['"]|['"]$//g;
401 # -------------------------------------------------------------------
403 my ( $translator, $data ) = @_;
404 my $parser = Parse::RecDescent->new($GRAMMAR);
406 local $::RD_TRACE = $translator->trace ? 1 : undef;
407 local $DEBUG = $translator->debug;
409 unless (defined $parser) {
410 return $translator->error("Error instantiating Parse::RecDescent ".
411 "instance: Bad grammer");
414 my $result = $parser->startrule($data);
415 return $translator->error( "Parse failed." ) unless defined $result;
416 warn Dumper( $result ) if $DEBUG;
418 my $schema = $translator->schema;
420 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
423 for my $table_name ( @tables ) {
424 my $tdata = $result->{ $table_name };
425 my $table = $schema->add_table(
426 name => $tdata->{'table_name'},
427 ) or die $schema->error;
429 $table->comments( $tdata->{'comments'} );
432 $tdata->{'fields'}->{$a}->{'order'}
434 $tdata->{'fields'}->{$b}->{'order'}
435 } keys %{ $tdata->{'fields'} };
437 for my $fname ( @fields ) {
438 my $fdata = $tdata->{'fields'}{ $fname };
439 my $field = $table->add_field(
440 name => $fdata->{'name'},
441 data_type => $fdata->{'data_type'},
442 size => $fdata->{'size'},
443 default_value => $fdata->{'default'},
444 is_auto_increment => $fdata->{'is_auto_inc'},
445 is_nullable => $fdata->{'null'},
446 comments => $fdata->{'comments'},
447 ) or die $table->error;
449 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
452 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
453 my $index = $table->add_index(
454 name => $idata->{'name'},
455 type => uc $idata->{'type'},
456 fields => $idata->{'fields'},
457 ) or die $table->error;
466 # -------------------------------------------------------------------
467 # Where man is not nature is barren.
469 # -------------------------------------------------------------------
475 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
479 perl(1), Parse::RecDescent, SQL::Translator::Schema.