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/).
24 our ( $DEBUG, $GRAMMAR, @EXPORT_OK );
25 our $VERSION = '1.59';
26 $DEBUG = 0 unless defined $DEBUG;
29 use Parse::RecDescent;
31 use base qw(Exporter);
33 @EXPORT_OK = qw(parse);
35 # Enable warnings within the Parse::RecDescent module.
36 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
37 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
38 $::RD_HINT = 1; # Give out hints to help fix problems.
43 my ( %tables, $table_order, @table_comments );
47 # The "eofile" rule makes the parser fail if any "statement" rule
48 # fails. Otherwise, the first successful match by a "statement"
49 # won't cause the failure needed to know that the parse, as a whole,
52 startrule : statement(s) eofile { \%tables }
64 { @table_comments = () }
66 set : /set/i /[^;]+/ ';'
67 { @table_comments = () }
69 drop : /drop/i TABLE /[^;]+/ ';'
71 drop : /drop/i WORD(s) ';'
72 { @table_comments = () }
74 create : CREATE /database/i WORD ';'
75 { @table_comments = () }
77 create : CREATE TABLE table_name '(' create_definition(s /,/) ')' ';'
79 my $table_name = $item{'table_name'};
80 $tables{ $table_name }{'order'} = ++$table_order;
81 $tables{ $table_name }{'table_name'} = $table_name;
83 if ( @table_comments ) {
84 $tables{ $table_name }{'comments'} = [ @table_comments ];
89 for my $definition ( @{ $item[5] } ) {
90 if ( $definition->{'supertype'} eq 'field' ) {
91 my $field_name = $definition->{'name'};
92 $tables{ $table_name }{'fields'}{ $field_name } =
93 { %$definition, order => $i };
96 if ( $definition->{'is_primary_key'} ) {
97 push @{ $tables{ $table_name }{'constraints'} },
99 type => 'primary_key',
100 fields => [ $field_name ],
105 elsif ( $definition->{'supertype'} eq 'constraint' ) {
106 push @{ $tables{ $table_name }{'constraints'} }, $definition;
108 elsif ( $definition->{'supertype'} eq 'index' ) {
109 push @{ $tables{ $table_name }{'indices'} }, $definition;
116 create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';'
118 @table_comments = ();
119 push @{ $tables{ $item{'table_name'} }{'indices'} },
122 type => $item[2] ? 'unique' : 'normal',
128 create_definition : constraint
134 comment : /^\s*--(.*)\n/
138 push @table_comments, $comment;
141 field : field_name data_type field_qualifier(s?) reference_definition(?)
144 supertype => 'field',
145 name => $item{'field_name'},
146 data_type => $item{'data_type'}{'type'},
147 size => $item{'data_type'}{'size'},
148 constraints => $item{'reference_definition(?)'},
153 field_qualifier : not_null
156 null => $item{'not_null'},
160 field_qualifier : default_val
163 default => $item{'default_val'},
167 field_qualifier : auto_inc
170 is_auto_inc => $item{'auto_inc'},
174 field_qualifier : primary_key
177 is_primary_key => $item{'primary_key'},
181 field_qualifier : unsigned
184 is_unsigned => $item{'unsigned'},
188 field_qualifier : /character set/i WORD
191 character_set => $item[2],
195 reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete(?) on_update(?)
198 type => 'foreign_key',
199 reference_table => $item[2],
200 reference_fields => $item[3][0],
201 match_type => $item[4][0],
202 on_delete => $item[5][0],
203 on_update => $item[6][0],
207 match_type : /match full/i { 'full' }
209 /match partial/i { 'partial' }
211 on_delete : /on delete/i reference_option
214 on_update : /on update/i reference_option
217 reference_option: /restrict/i |
234 data_type : access_data_type parens_value_list(s?) type_qualifier(s?)
239 qualifiers => $item[3],
243 access_data_type : /long integer/i { $return = 'Long Integer' }
244 | /text/i { $return = 'Text' }
245 | /datetime (\(short\))?/i { $return = 'DateTime' }
246 | /boolean/i { $return = 'Boolean' }
249 parens_field_list : '(' field_name(s /,/) ')'
252 parens_value_list : '(' VALUE(s /,/) ')'
255 type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
260 create_index : /create/i /index/i
262 not_null : /not/i /null/i { $return = 0 }
264 unsigned : /unsigned/i { $return = 0 }
266 default_val : /default/i /'(?:.*?\\')*.*?'|(?:')?[\w\d:.-]*(?:')?/
268 $item[2] =~ s/^\s*'|'\s*$//g;
272 auto_inc : /auto_increment/i { 1 }
274 primary_key : /primary/i /key/i { 1 }
276 constraint : primary_key_def
281 foreign_key_def : foreign_key_def_begin parens_field_list reference_definition
284 supertype => 'constraint',
285 type => 'foreign_key',
288 %{ $item{'reference_definition'} },
292 foreign_key_def_begin : /constraint/i /foreign key/i
295 /constraint/i WORD /foreign key/i
296 { $return = $item[2] }
301 primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')'
304 supertype => 'constraint',
305 name => $item{'index_name(?)'}[0],
306 type => 'primary_key',
311 unique_key_def : UNIQUE KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
314 supertype => 'constraint',
315 name => $item{'index_name(?)'}[0],
321 normal_index : KEY index_name(?) '(' name_with_opt_paren(s /,/) ')'
324 supertype => 'index',
326 name => $item{'index_name(?)'}[0],
331 fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
334 supertype => 'index',
336 name => $item{'index_name(?)'}[0],
341 name_with_opt_paren : NAME parens_value_list(s?)
342 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
344 UNIQUE : /unique/i { 1 }
346 KEY : /key/i | /index/i
348 table_option : WORD /\s*=\s*/ WORD
350 $return = { $item[1] => $item[3] };
355 TEMPORARY : /temporary/i
370 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
374 # remove leading/trailing quotes
376 $val =~ s/^['"]|['"]$//g;
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.