1 package SQL::Translator::Parser::Access;
5 SQL::Translator::Parser::Access - parser for Access as produced by mdbtools
10 use SQL::Translator::Parser::Access;
12 my $translator = SQL::Translator->new;
13 $translator->parser("SQL::Translator::Parser::Access");
17 The grammar derived from the MySQL grammar. The input is expected to be
18 something similar to the output of mdbtools (http://mdbtools.sourceforge.net/).
23 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
25 $DEBUG = 0 unless defined $DEBUG;
28 use Parse::RecDescent;
30 use base qw(Exporter);
32 @EXPORT_OK = qw(parse);
34 # Enable warnings within the Parse::RecDescent module.
35 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
36 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
37 $::RD_HINT = 1; # Give out hints to help fix problems.
42 my ( %tables, $table_order, @table_comments );
46 # The "eofile" rule makes the parser fail if any "statement" rule
47 # fails. Otherwise, the first successful match by a "statement"
48 # won't cause the failure needed to know that the parse, as a whole,
51 startrule : statement(s) eofile { \%tables }
63 { @table_comments = () }
65 set : /set/i /[^;]+/ ';'
66 { @table_comments = () }
68 drop : /drop/i TABLE /[^;]+/ ';'
70 drop : /drop/i WORD(s) ';'
71 { @table_comments = () }
73 create : CREATE /database/i WORD ';'
74 { @table_comments = () }
76 create : CREATE TABLE table_name '(' create_definition(s /,/) ')' ';'
78 my $table_name = $item{'table_name'};
79 $tables{ $table_name }{'order'} = ++$table_order;
80 $tables{ $table_name }{'table_name'} = $table_name;
82 if ( @table_comments ) {
83 $tables{ $table_name }{'comments'} = [ @table_comments ];
88 for my $definition ( @{ $item[5] } ) {
89 if ( $definition->{'supertype'} eq 'field' ) {
90 my $field_name = $definition->{'name'};
91 $tables{ $table_name }{'fields'}{ $field_name } =
92 { %$definition, order => $i };
95 if ( $definition->{'is_primary_key'} ) {
96 push @{ $tables{ $table_name }{'constraints'} },
98 type => 'primary_key',
99 fields => [ $field_name ],
104 elsif ( $definition->{'supertype'} eq 'constraint' ) {
105 push @{ $tables{ $table_name }{'constraints'} }, $definition;
107 elsif ( $definition->{'supertype'} eq 'index' ) {
108 push @{ $tables{ $table_name }{'indices'} }, $definition;
115 create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';'
117 @table_comments = ();
118 push @{ $tables{ $item{'table_name'} }{'indices'} },
121 type => $item[2] ? 'unique' : 'normal',
127 create_definition : constraint
133 comment : /^\s*--(.*)\n/
137 push @table_comments, $comment;
140 field : field_name data_type field_qualifier(s?) reference_definition(?)
143 supertype => 'field',
144 name => $item{'field_name'},
145 data_type => $item{'data_type'}{'type'},
146 size => $item{'data_type'}{'size'},
147 constraints => $item{'reference_definition(?)'},
152 field_qualifier : not_null
155 null => $item{'not_null'},
159 field_qualifier : default_val
162 default => $item{'default_val'},
166 field_qualifier : auto_inc
169 is_auto_inc => $item{'auto_inc'},
173 field_qualifier : primary_key
176 is_primary_key => $item{'primary_key'},
180 field_qualifier : unsigned
183 is_unsigned => $item{'unsigned'},
187 field_qualifier : /character set/i WORD
190 character_set => $item[2],
194 reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete(?) on_update(?)
197 type => 'foreign_key',
198 reference_table => $item[2],
199 reference_fields => $item[3][0],
200 match_type => $item[4][0],
201 on_delete => $item[5][0],
202 on_update => $item[6][0],
206 match_type : /match full/i { 'full' }
208 /match partial/i { 'partial' }
210 on_delete : /on delete/i reference_option
213 on_update : /on update/i reference_option
216 reference_option: /restrict/i |
233 data_type : access_data_type parens_value_list(s?) type_qualifier(s?)
238 qualifiers => $item[3],
242 access_data_type : /long integer/i { $return = 'Long Integer' }
243 | /text/i { $return = 'Text' }
244 | /datetime (\(short\))?/i { $return = 'DateTime' }
245 | /boolean/i { $return = 'Boolean' }
248 parens_field_list : '(' field_name(s /,/) ')'
251 parens_value_list : '(' VALUE(s /,/) ')'
254 type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
259 create_index : /create/i /index/i
261 not_null : /not/i /null/i { $return = 0 }
263 unsigned : /unsigned/i { $return = 0 }
265 default_val : /default/i /'(?:.*?\\')*.*?'|(?:')?[\w\d:.-]*(?:')?/
267 $item[2] =~ s/^\s*'|'\s*$//g;
271 auto_inc : /auto_increment/i { 1 }
273 primary_key : /primary/i /key/i { 1 }
275 constraint : primary_key_def
280 foreign_key_def : foreign_key_def_begin parens_field_list reference_definition
283 supertype => 'constraint',
284 type => 'foreign_key',
287 %{ $item{'reference_definition'} },
291 foreign_key_def_begin : /constraint/i /foreign key/i
294 /constraint/i WORD /foreign key/i
295 { $return = $item[2] }
300 primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')'
303 supertype => 'constraint',
304 name => $item{'index_name(?)'}[0],
305 type => 'primary_key',
310 unique_key_def : UNIQUE KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
313 supertype => 'constraint',
314 name => $item{'index_name(?)'}[0],
320 normal_index : KEY index_name(?) '(' name_with_opt_paren(s /,/) ')'
323 supertype => 'index',
325 name => $item{'index_name(?)'}[0],
330 fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
333 supertype => 'index',
335 name => $item{'index_name(?)'}[0],
340 name_with_opt_paren : NAME parens_value_list(s?)
341 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
343 UNIQUE : /unique/i { 1 }
345 KEY : /key/i | /index/i
347 table_option : WORD /\s*=\s*/ WORD
349 $return = { $item[1] => $item[3] };
354 TEMPORARY : /temporary/i
369 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
373 # remove leading/trailing quotes
375 $val =~ s/^['"]|['"]$//g;
383 # -------------------------------------------------------------------
385 my ( $translator, $data ) = @_;
386 my $parser = Parse::RecDescent->new($GRAMMAR);
388 local $::RD_TRACE = $translator->trace ? 1 : undef;
389 local $DEBUG = $translator->debug;
391 unless (defined $parser) {
392 return $translator->error("Error instantiating Parse::RecDescent ".
393 "instance: Bad grammer");
396 my $result = $parser->startrule($data);
397 return $translator->error( "Parse failed." ) unless defined $result;
398 warn Dumper( $result ) if $DEBUG;
400 my $schema = $translator->schema;
402 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
405 for my $table_name ( @tables ) {
406 my $tdata = $result->{ $table_name };
407 my $table = $schema->add_table(
408 name => $tdata->{'table_name'},
409 ) or die $schema->error;
411 $table->comments( $tdata->{'comments'} );
414 $tdata->{'fields'}->{$a}->{'order'}
416 $tdata->{'fields'}->{$b}->{'order'}
417 } keys %{ $tdata->{'fields'} };
419 for my $fname ( @fields ) {
420 my $fdata = $tdata->{'fields'}{ $fname };
421 my $field = $table->add_field(
422 name => $fdata->{'name'},
423 data_type => $fdata->{'data_type'},
424 size => $fdata->{'size'},
425 default_value => $fdata->{'default'},
426 is_auto_increment => $fdata->{'is_auto_inc'},
427 is_nullable => $fdata->{'null'},
428 comments => $fdata->{'comments'},
429 ) or die $table->error;
431 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
434 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
435 my $index = $table->add_index(
436 name => $idata->{'name'},
437 type => uc $idata->{'type'},
438 fields => $idata->{'fields'},
439 ) or die $table->error;
448 # -------------------------------------------------------------------
449 # Where man is not nature is barren.
451 # -------------------------------------------------------------------
457 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
461 perl(1), Parse::RecDescent, SQL::Translator::Schema.