Applied Dave Howorth's MySQL parser patches
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / MySQL.pm
index c0a2144..69ad29f 100644 (file)
@@ -1,7 +1,7 @@
 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
 #
@@ -117,11 +117,24 @@ Here's the word from the MySQL site
   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;
@@ -139,7 +152,7 @@ $::RD_HINT   = 1; # Give out hints to help fix problems.
 $GRAMMAR = q!
 
 { 
-    my ( %tables, $table_order, @table_comments );
+    my ( $database_name, %tables, $table_order, @table_comments );
 }
 
 #
@@ -148,7 +161,9 @@ $GRAMMAR = q!
 # 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/
 
@@ -157,10 +172,15 @@ statement : comment
     | set
     | drop
     | create
+    | alter
+    | insert
     | <error>
 
 use : /use/i WORD ';'
-    { @table_comments = () }
+    {
+        $database_name = $item[2];
+        @table_comments = ();
+    }
 
 set : /set/i /[^;]+/ ';'
     { @table_comments = () }
@@ -170,10 +190,26 @@ drop : /drop/i TABLE /[^;]+/ ';'
 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;
@@ -533,6 +569,10 @@ table_option : WORD /\s*=\s*/ WORD
         $return = { $item[1] => $item[3] };
     }
 
+ADD : /add/i
+
+ALTER : /alter/i
+
 CREATE : /create/i
 
 TEMPORARY : /temporary/i
@@ -579,15 +619,19 @@ sub parse {
 
     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;