=head1 DESCRIPTION
-The grammar derived from the MySQL grammar. The input is expected to be
+The grammar derived from the MySQL grammar. The input is expected to be
something similar to the output of mdbtools (http://mdbtools.sourceforge.net/).
=cut
use strict;
-use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = '1.59';
+use warnings;
+our ( $DEBUG, $GRAMMAR, @EXPORT_OK );
+our $VERSION = '1.59';
$DEBUG = 0 unless defined $DEBUG;
use Data::Dumper;
$GRAMMAR = q!
-{
+{
my ( %tables, $table_order, @table_comments );
}
#
# The "eofile" rule makes the parser fail if any "statement" rule
-# fails. Otherwise, the first successful match by a "statement"
+# fails. Otherwise, the first successful match by a "statement"
# won't cause the failure needed to know that the parse, as a whole,
# failed. -ky
#
{ @table_comments = () }
create : CREATE TABLE table_name '(' create_definition(s /,/) ')' ';'
- {
+ {
my $table_name = $item{'table_name'};
$tables{ $table_name }{'order'} = ++$table_order;
$tables{ $table_name }{'table_name'} = $table_name;
for my $definition ( @{ $item[5] } ) {
if ( $definition->{'supertype'} eq 'field' ) {
my $field_name = $definition->{'name'};
- $tables{ $table_name }{'fields'}{ $field_name } =
+ $tables{ $table_name }{'fields'}{ $field_name } =
{ %$definition, order => $i };
$i++;
-
+
if ( $definition->{'is_primary_key'} ) {
push @{ $tables{ $table_name }{'constraints'} },
{
;
}
-create_definition : constraint
+create_definition : constraint
| index
| field
| comment
| <error>
-comment : /^\s*--(.*)\n/
- {
+comment : /^\s*--(.*)\n/
+ {
my $comment = $1;
$return = $comment;
push @table_comments, $comment;
}
field : field_name data_type field_qualifier(s?) reference_definition(?)
- {
- $return = {
+ {
+ $return = {
supertype => 'field',
- name => $item{'field_name'},
+ name => $item{'field_name'},
data_type => $item{'data_type'}{'type'},
size => $item{'data_type'}{'size'},
constraints => $item{'reference_definition(?)'},
- }
+ }
}
| <error>
field_qualifier : not_null
- {
- $return = {
+ {
+ $return = {
null => $item{'not_null'},
- }
+ }
}
field_qualifier : default_val
- {
- $return = {
+ {
+ $return = {
default => $item{'default_val'},
- }
+ }
}
field_qualifier : auto_inc
- {
- $return = {
+ {
+ $return = {
is_auto_inc => $item{'auto_inc'},
- }
+ }
}
field_qualifier : primary_key
- {
- $return = {
+ {
+ $return = {
is_primary_key => $item{'primary_key'},
- }
+ }
}
field_qualifier : unsigned
- {
- $return = {
+ {
+ $return = {
is_unsigned => $item{'unsigned'},
- }
+ }
}
field_qualifier : /character set/i WORD
on_update : /on update/i reference_option
{ $item[2] }
-reference_option: /restrict/i |
- /cascade/i |
- /set null/i |
- /no action/i |
+reference_option: /restrict/i |
+ /cascade/i |
+ /set null/i |
+ /no action/i |
/set default/i
- { $item[1] }
+ { $item[1] }
index : normal_index
| fulltext_index
index_name : NAME
data_type : access_data_type parens_value_list(s?) type_qualifier(s?)
- {
- $return = {
+ {
+ $return = {
type => $item[1],
size => $item[2][0],
qualifiers => $item[3],
- }
+ }
}
access_data_type : /long integer/i { $return = 'Long Integer' }
}
}
-foreign_key_def_begin : /constraint/i /foreign key/i
+foreign_key_def_begin : /constraint/i /foreign key/i
{ $return = '' }
|
/constraint/i WORD /foreign key/i
{ $return = '' }
primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')'
- {
- $return = {
+ {
+ $return = {
supertype => 'constraint',
name => $item{'index_name(?)'}[0],
type => 'primary_key',
}
unique_key_def : UNIQUE KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
- {
- $return = {
+ {
+ $return = {
supertype => 'constraint',
name => $item{'index_name(?)'}[0],
type => 'unique',
fields => $item[5],
- }
+ }
}
normal_index : KEY index_name(?) '(' name_with_opt_paren(s /,/) ')'
- {
- $return = {
+ {
+ $return = {
supertype => 'index',
type => 'normal',
name => $item{'index_name(?)'}[0],
fields => $item[4],
- }
+ }
}
fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
- {
- $return = {
+ {
+ $return = {
supertype => 'index',
type => 'fulltext',
name => $item{'index_name(?)'}[0],
fields => $item[5],
- }
+ }
}
name_with_opt_paren : NAME parens_value_list(s?)
KEY : /key/i | /index/i
table_option : WORD /\s*=\s*/ WORD
- {
+ {
$return = { $item[1] => $item[3] };
}
VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
{ $item[1] }
- | /'.*?'/
- {
- # remove leading/trailing quotes
+ | /'.*?'/
+ {
+ # remove leading/trailing quotes
my $val = $item[1];
$val =~ s/^['"]|['"]$//g;
$return = $val;
!;
-# -------------------------------------------------------------------
sub parse {
my ( $translator, $data ) = @_;
my $parser = Parse::RecDescent->new($GRAMMAR);
warn Dumper( $result ) if $DEBUG;
my $schema = $translator->schema;
- my @tables = sort {
+ my @tables = sort {
$result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
} keys %{ $result };
for my $table_name ( @tables ) {
my $tdata = $result->{ $table_name };
- my $table = $schema->add_table(
+ my $table = $schema->add_table(
name => $tdata->{'table_name'},
) or die $schema->error;
$table->comments( $tdata->{'comments'} );
- my @fields = sort {
- $tdata->{'fields'}->{$a}->{'order'}
+ my @fields = sort {
+ $tdata->{'fields'}->{$a}->{'order'}
<=>
$tdata->{'fields'}->{$b}->{'order'}
} keys %{ $tdata->{'fields'} };