package SQL::Translator::Parser::MySQL;
# -------------------------------------------------------------------
-# $Id: MySQL.pm,v 1.44 2004-03-01 17:39:22 kycl4rk Exp $
+# $Id: MySQL.pm,v 1.45 2004-11-25 22:32:48 grommit Exp $
# -------------------------------------------------------------------
# Copyright (C) 2002-4 SQLFairy Authors
#
or DATA DIRECTORY="absolute path to directory"
or INDEX DIRECTORY="absolute path to directory"
+A subset of the ALTER TABLE syntax that allows addition of foreign keys:
+
+ ALTER [IGNORE] TABLE tbl_name alter_specification [, alter_specification] ...
+
+ alter_specification:
+ ADD [CONSTRAINT [symbol]]
+ FOREIGN KEY [index_name] (index_col_name,...)
+ [reference_definition]
+
+A subset of INSERT that we ignore:
+
+ INSERT anything
+
=cut
use strict;
use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.44 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.45 $ =~ /(\d+)\.(\d+)/;
$DEBUG = 0 unless defined $DEBUG;
use Data::Dumper;
$GRAMMAR = q!
{
- my ( %tables, $table_order, @table_comments );
+ my ( $database_name, %tables, $table_order, @table_comments );
}
#
# won't cause the failure needed to know that the parse, as a whole,
# failed. -ky
#
-startrule : statement(s) eofile { \%tables }
+startrule : statement(s) eofile {
+ { tables => \%tables, database_name => $database_name }
+}
eofile : /^\Z/
| set
| drop
| create
+ | alter
+ | insert
| <error>
use : /use/i WORD ';'
- { @table_comments = () }
+ {
+ $database_name = $item[2];
+ @table_comments = ();
+ }
set : /set/i /[^;]+/ ';'
{ @table_comments = () }
drop : /drop/i WORD(s) ';'
{ @table_comments = () }
+insert : /insert/i /[^;]+/ ';'
+
+alter : ALTER TABLE table_name alter_specification(s /,/) ';'
+ {
+ my $table_name = $item{'table_name'};
+ die "Cannot ALTER table '$table_name'; it does not exist"
+ unless $tables{ $table_name };
+ for my $definition ( @{ $item[4] } ) {
+ $definition->{'extra'}->{'alter'} = 1;
+ push @{ $tables{ $table_name }{'constraints'} }, $definition;
+ }
+ }
+
+alter_specification : ADD foreign_key_def
+ { $return = $item[2] }
+
create : CREATE /database/i WORD ';'
{ @table_comments = () }
-create : CREATE TEMPORARY(?) TABLE opt_if_not_exists(?) table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
+create : CREATE TEMPORARY(?) TABLE opt_if_not_exists(?) table_name '(' create_definition(s /,/) /(,\s*)?\)/ table_option(s?) ';'
{
my $table_name = $item{'table_name'};
$tables{ $table_name }{'order'} = ++$table_order;
$return = { $item[1] => $item[3] };
}
+ADD : /add/i
+
+ALTER : /alter/i
+
CREATE : /create/i
TEMPORARY : /temporary/i
my $result = $parser->startrule($data);
return $translator->error( "Parse failed." ) unless defined $result;
- warn Dumper( $result ) if $DEBUG;
+ warn "Parse result:".Dumper( $result ) if $DEBUG;
my $schema = $translator->schema;
+ $schema->name($result->{'database_name'}) if $result->{'database_name'};
+
my @tables = sort {
- $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
- } keys %{ $result };
+ $result->{'tables'}{ $a }{'order'}
+ <=>
+ $result->{'tables'}{ $b }{'order'}
+ } keys %{ $result->{'tables'} };
for my $table_name ( @tables ) {
- my $tdata = $result->{ $table_name };
+ my $tdata = $result->{tables}{ $table_name };
my $table = $schema->add_table(
name => $tdata->{'table_name'},
) or die $schema->error;