Changed references to on_delete_do to on_delete and on_update_do to on_update. I...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / MySQL.pm
index 547934b..5d4a4d1 100644 (file)
@@ -1,10 +1,9 @@
 package SQL::Translator::Parser::MySQL;
 
 # -------------------------------------------------------------------
-# $Id: MySQL.pm,v 1.5 2002-11-20 04:03:04 kycl4rk Exp $
+# $Id: MySQL.pm,v 1.50 2005-06-28 16:39:41 mwz444 Exp $
 # -------------------------------------------------------------------
-# Copyright (C) 2002 Ken Y. Clark <kycl4rk@users.sourceforge.net>,
-#                    darren chamberlain <darren@cpan.org>
+# Copyright (C) 2002-4 SQLFairy Authors
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as
@@ -21,256 +20,788 @@ package SQL::Translator::Parser::MySQL;
 # 02111-1307  USA
 # -------------------------------------------------------------------
 
+=head1 NAME
+
+SQL::Translator::Parser::MySQL - parser for MySQL
+
+=head1 SYNOPSIS
+
+  use SQL::Translator;
+  use SQL::Translator::Parser::MySQL;
+
+  my $translator = SQL::Translator->new;
+  $translator->parser("SQL::Translator::Parser::MySQL");
+
+=head1 DESCRIPTION
+
+The grammar is influenced heavily by Tim Bunce's "mysql2ora" grammar.
+
+Here's the word from the MySQL site
+(http://www.mysql.com/doc/en/CREATE_TABLE.html):
+
+  CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
+  [table_options] [select_statement]
+  
+  or
+  
+  CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name LIKE old_table_name;
+  
+  create_definition:
+    col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
+              [PRIMARY KEY] [reference_definition]
+    or    PRIMARY KEY (index_col_name,...)
+    or    KEY [index_name] (index_col_name,...)
+    or    INDEX [index_name] (index_col_name,...)
+    or    UNIQUE [INDEX] [index_name] (index_col_name,...)
+    or    FULLTEXT [INDEX] [index_name] (index_col_name,...)
+    or    [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
+              [reference_definition]
+    or    CHECK (expr)
+  
+  type:
+          TINYINT[(length)] [UNSIGNED] [ZEROFILL]
+    or    SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
+    or    MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
+    or    INT[(length)] [UNSIGNED] [ZEROFILL]
+    or    INTEGER[(length)] [UNSIGNED] [ZEROFILL]
+    or    BIGINT[(length)] [UNSIGNED] [ZEROFILL]
+    or    REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
+    or    DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
+    or    FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
+    or    DECIMAL(length,decimals) [UNSIGNED] [ZEROFILL]
+    or    NUMERIC(length,decimals) [UNSIGNED] [ZEROFILL]
+    or    CHAR(length) [BINARY]
+    or    VARCHAR(length) [BINARY]
+    or    DATE
+    or    TIME
+    or    TIMESTAMP
+    or    DATETIME
+    or    TINYBLOB
+    or    BLOB
+    or    MEDIUMBLOB
+    or    LONGBLOB
+    or    TINYTEXT
+    or    TEXT
+    or    MEDIUMTEXT
+    or    LONGTEXT
+    or    ENUM(value1,value2,value3,...)
+    or    SET(value1,value2,value3,...)
+  
+  index_col_name:
+          col_name [(length)]
+  
+  reference_definition:
+          REFERENCES tbl_name [(index_col_name,...)]
+                     [MATCH FULL | MATCH PARTIAL]
+                     [ON DELETE reference_option]
+                     [ON UPDATE reference_option]
+  
+  reference_option:
+          RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
+  
+  table_options:
+          TYPE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM }
+  or      AUTO_INCREMENT = #
+  or      AVG_ROW_LENGTH = #
+  or      CHECKSUM = {0 | 1}
+  or      COMMENT = "string"
+  or      MAX_ROWS = #
+  or      MIN_ROWS = #
+  or      PACK_KEYS = {0 | 1 | DEFAULT}
+  or      PASSWORD = "string"
+  or      DELAY_KEY_WRITE = {0 | 1}
+  or      ROW_FORMAT= { default | dynamic | fixed | compressed }
+  or      RAID_TYPE= {1 | STRIPED | RAID0 } RAID_CHUNKS=#  RAID_CHUNKSIZE=#
+  or      UNION = (table_name,[table_name...])
+  or      INSERT_METHOD= {NO | FIRST | LAST }
+  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($VERSION $GRAMMAR @EXPORT_OK);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/;
+use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
+$VERSION = sprintf "%d.%02d", q$Revision: 1.50 $ =~ /(\d+)\.(\d+)/;
+$DEBUG   = 0 unless defined $DEBUG;
 
-#use SQL::Translator::Parser;  # This is not necessary!
+use Data::Dumper;
 use Parse::RecDescent;
 use Exporter;
 use base qw(Exporter);
 
 @EXPORT_OK = qw(parse);
 
-my $parser; # should we do this?  There's no programmic way to 
-            # change the grammar, so I think this is safe.
-sub parse {
-    my ( $translator, $data ) = @_;
-    $parser ||= Parse::RecDescent->new($GRAMMAR);
+# Enable warnings within the Parse::RecDescent module.
+$::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
+$::RD_WARN   = 1; # Enable warnings. This will warn on unused rules &c.
+$::RD_HINT   = 1; # Give out hints to help fix problems.
 
-    unless (defined $parser) {
-        return $translator->error("Error instantiating Parse::RecDescent ".
-            "instance: Bad grammer");
-    }
+$GRAMMAR = q!
+
+{ 
+    my ( $database_name, %tables, $table_order, @table_comments );
+}
 
-    # Is this right?  It was $parser->parse before, but that didn't
-    # work; Parse::RecDescent appears to need the name of a rule
-    # with which to begin, so I chose the first rule in the grammar.
-    return $parser->file($data);
+#
+# The "eofile" rule makes the parser fail if any "statement" rule
+# 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
+#
+startrule : statement(s) eofile { 
+    { tables => \%tables, database_name => $database_name } 
 }
 
-$GRAMMAR =
-    q!
-        { our ( %tables ) }
-
-        file         : statement(s) { \%tables }
-
-        statement    : comment
-                       | create
-                       | <error>
-
-        create       : create_table table_name '(' line(s /,/) ')' table_type(?) ';'
-                    { 
-                        my $i = 0;
-                        for my $line ( @{ $item[4] } ) {
-                            if ( $line->{'type'} eq 'field' ) {
-                                my $field_name = $line->{'name'};
-                                $tables{ $item{'table_name'} }
-                                    {'fields'}{$field_name} = 
-                                    { %$line, order => $i };
-                                $i++;
-                        
-                                if ( $line->{'is_primary_key'} ) {
-                                    push
-                                    @{ $tables{ $item{'table_name'} }{'indices'} },
-                                    {
-                                        type   => 'primary_key',
-                                        fields => [ $field_name ],
-                                    };
-                                }
-                            }
-                            else {
-                                push @{ $tables{ $item{'table_name'} }{'indices'} },
-                                    $line;
-                            }
-                            $tables{ $item{'table_name'} }{'type'} = 
-                                $item{'table_type'}[0];
+eofile : /^\Z/
+
+statement : comment
+    | use
+    | set
+    | drop
+    | create
+    | alter
+    | insert
+    | <error>
+
+use : /use/i WORD ';'
+    {
+        $database_name = $item[2];
+        @table_comments = ();
+    }
+
+set : /set/i /[^;]+/ ';'
+    { @table_comments = () }
+
+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 /,/) /(,\s*)?\)/ table_option(s?) ';'
+    { 
+        my $table_name                       = $item{'table_name'};
+        $tables{ $table_name }{'order'}      = ++$table_order;
+        $tables{ $table_name }{'table_name'} = $table_name;
+
+        if ( @table_comments ) {
+            $tables{ $table_name }{'comments'} = [ @table_comments ];
+            @table_comments = ();
+        }
+
+        my $i = 1;
+        for my $definition ( @{ $item[7] } ) {
+            if ( $definition->{'supertype'} eq 'field' ) {
+                my $field_name = $definition->{'name'};
+                $tables{ $table_name }{'fields'}{ $field_name } = 
+                    { %$definition, order => $i };
+                $i++;
+        
+                if ( $definition->{'is_primary_key'} ) {
+                    push @{ $tables{ $table_name }{'constraints'} },
+                        {
+                            type   => 'primary_key',
+                            fields => [ $field_name ],
                         }
-                    }
-                       | <error>
-
-        create       : create_index index_name /on/i table_name '(' field_name(s /,/) ')' ';'
-#        create       : create_index index_name keyword_on table_name '(' field_name ')' ';'
-                       {
-                         # do nothing just now
-                         my $dummy = 0;
-                       }
-                        | <error>
-
-        keyword_on   : /on/i
-
-        line         : index
-                       | field
-                       | <error>
-
-        comment      : /^\s*[#-]+.*\n/
-
-        blank        : /\s*/
-
-
-        field        : field_name data_type field_qualifier(s?)
-                       { 
-                          my %qualifier_h =  
-                            map {%$_} @{$item{'field_qualifier'} || []};
-                          my $null = defined $item{'not_null'}
-                            ? $item{'not_null'} : 1 ;
-                          delete $qualifier_h{'not_null'};
-                          $return = { 
-                                type           => 'field',
-                                name           => $item{'field_name'}, 
-                                data_type      => $item{'data_type'}{'type'},
-                               null           => $null,
-                               %qualifier_h,
-                           } 
-                       }
-                    | <error>
-
-        field_qualifier : not_null
-            { 
-                $return = { 
-                     null => $item{'not_null'},
-                } 
+                    ;
+                }
             }
-
-        field_qualifier : default_val
-            { 
-                $return = { 
-                     default => $item{default_val},
-                } 
+            elsif ( $definition->{'supertype'} eq 'constraint' ) {
+                push @{ $tables{ $table_name }{'constraints'} }, $definition;
             }
-
-        field_qualifier : auto_inc
-            { 
-                $return = { 
-                     is_auto_inc => $item{auto_inc},
-                } 
+            elsif ( $definition->{'supertype'} eq 'index' ) {
+                push @{ $tables{ $table_name }{'indices'} }, $definition;
             }
-
-        field_qualifier : primary_key
-            { 
-                $return = { 
-                     is_primary_key => $item{primary_key},
-                } 
+        }
+
+        if ( my @options = @{ $item{'table_option(s?)'} } ) {
+            for my $option ( @options ) {
+                my ( $key, $value ) = each %$option;
+                if ( $key eq 'comment' ) {
+                    push @{ $tables{ $table_name }{'comments'} }, $value;
+                }
+                else {
+                    push @{ $tables{ $table_name }{'table_options'} }, $option;
+                }
             }
+        }
+
+        1;
+    }
 
-        field_qualifier : unsigned
-            { 
-                $return = { 
-                     is_unsigned => $item{unsigned},
-                } 
+opt_if_not_exists : /if not exists/i
+
+create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';'
+    {
+        @table_comments = ();
+        push @{ $tables{ $item{'table_name'} }{'indices'} },
+            {
+                name   => $item[4],
+                type   => $item[2] ? 'unique' : 'normal',
+                fields => $item[8],
             }
+        ;
+    }
+
+create_definition : constraint 
+    | index
+    | field
+    | comment
+    | <error>
+
+comment : /^\s*(?:#|-{2}).*\n/ 
+    { 
+        my $comment =  $item[1];
+        $comment    =~ s/^\s*(#|--)\s*//;
+        $comment    =~ s/\s*$//;
+        $return     = $comment;
+        push @table_comments, $comment;
+    }
+
+comment : /\/\*/ /[^\*]+/ /\*\// ';'
+    {
+        my $comment = $item[2];
+        $comment    =~ s/^\s*|\s*$//g;
+        $return = $comment;
+    }
+
+field_comment : /^\s*(?:#|-{2}).*\n/ 
+    { 
+        my $comment =  $item[1];
+        $comment    =~ s/^\s*(#|--)\s*//;
+        $comment    =~ s/\s*$//;
+        $return     = $comment;
+    }
 
-        index        : primary_key_index
-                       | unique_index
-                       | normal_index
 
-        table_name   : WORD
+field_comment2 : /comment/i /'.*?'/
+    {
+        my $comment = $item[2];
+        $comment    =~ s/^'//;
+        $comment    =~ s/'$//;
+        $return     = $comment;
+    }
+
+blank : /\s*/
+
+field : field_comment(s?) field_name data_type field_qualifier(s?) field_comment2(?) reference_definition(?) on_update(?) field_comment(s?)
+    { 
+        my %qualifiers  = map { %$_ } @{ $item{'field_qualifier(s?)'} || [] };
+        if ( my @type_quals = @{ $item{'data_type'}{'qualifiers'} || [] } ) {
+            $qualifiers{ $_ } = 1 for @type_quals;
+        }
+
+        my $null = defined $qualifiers{'not_null'} 
+                   ? $qualifiers{'not_null'} : 1;
+        delete $qualifiers{'not_null'};
+
+        my @comments = ( @{ $item[1] }, @{ $item[5] }, @{ $item[8] } );
+
+        $return = { 
+            supertype   => 'field',
+            name        => $item{'field_name'}, 
+            data_type   => $item{'data_type'}{'type'},
+            size        => $item{'data_type'}{'size'},
+            list        => $item{'data_type'}{'list'},
+            null        => $null,
+            constraints => $item{'reference_definition(?)'},
+            comments    => [ @comments ],
+            %qualifiers,
+        } 
+    }
+    | <error>
+
+field_qualifier : not_null
+    { 
+        $return = { 
+             null => $item{'not_null'},
+        } 
+    }
+
+field_qualifier : default_val
+    { 
+        $return = { 
+             default => $item{'default_val'},
+        } 
+    }
+
+field_qualifier : auto_inc
+    { 
+        $return = { 
+             is_auto_inc => $item{'auto_inc'},
+        } 
+    }
+
+field_qualifier : primary_key
+    { 
+        $return = { 
+             is_primary_key => $item{'primary_key'},
+        } 
+    }
+
+field_qualifier : unsigned
+    { 
+        $return = { 
+             is_unsigned => $item{'unsigned'},
+        } 
+    }
 
-        field_name   : WORD
+field_qualifier : /character set/i WORD
+    {
+        $return = {
+            'CHARACTER SET' => $item[2],
+        }
+    }
 
-        index_name   : WORD
+field_qualifier : /collate/i WORD
+    {
+        $return = {
+            COLLATE => $item[2],
+        }
+    }
 
-        data_type    : WORD field_size(?) 
-            { 
-                $return = { 
-                    type => $item[1], 
-                    size => $item[2][0]
-                } 
+field_qualifier : /on update/i CURRENT_TIMESTAMP
+    {
+        $return = {
+            'ON UPDATE' => $item[2],
+        }
+    }
+
+reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete(?) on_update(?)
+    {
+        $return = {
+            type             => 'foreign_key',
+            reference_table  => $item[2],
+            reference_fields => $item[3][0],
+            match_type       => $item[4][0],
+            on_delete        => $item[5][0],
+            on_update        => $item[6][0],
+        }
+    }
+
+match_type : /match full/i { 'full' }
+    |
+    /match partial/i { 'partial' }
+
+on_delete : /on delete/i reference_option
+    { $item[2] }
+
+on_update : 
+    /on update/i 'CURRENT_TIMESTAMP'
+    { $item[2] }
+    |
+    /on update/i reference_option
+    { $item[2] }
+
+reference_option: /restrict/i | 
+    /cascade/i   | 
+    /set null/i  | 
+    /no action/i | 
+    /set default/i
+    { $item[1] }  
+
+index : normal_index
+    | fulltext_index
+    | <error>
+
+table_name   : NAME
+
+field_name   : NAME
+
+index_name   : NAME
+
+data_type    : WORD parens_value_list(s?) type_qualifier(s?)
+    { 
+        my $type = $item[1];
+        my $size; # field size, applicable only to non-set fields
+        my $list; # set list, applicable only to sets (duh)
+
+        if ( uc($type) =~ /^(SET|ENUM)$/ ) {
+            $size = undef;
+            $list = $item[2][0];
+        }
+        else {
+            $size = $item[2][0];
+            $list = [];
+        }
+
+        unless ( @{ $size || [] } ) {
+            if ( lc $type eq 'tinyint' ) {
+                $size = 4;
+            }
+            elsif ( lc $type eq 'smallint' ) {
+                $size = 6;
+            }
+            elsif ( lc $type eq 'mediumint' ) {
+                $size = 9;
+            }
+            elsif ( $type =~ /^int(eger)?$/i ) {
+                $type = 'int';
+                $size = 11;
+            }
+            elsif ( lc $type eq 'bigint' ) {
+                $size = 20;
+            }
+            elsif ( 
+                lc $type =~ /(float|double|decimal|numeric|real|fixed|dec)/ 
+            ) {
+                $size = [8,2];
             }
+        }
+
+        if ( $type =~ /^tiny(text|blob)$/i ) {
+            $size = 255;
+        }
+        elsif ( $type =~ /^(blob|text)$/i ) {
+            $size = 65_535;
+        }
+        elsif ( $type =~ /^medium(blob|text)$/i ) {
+            $size = 16_777_215;
+        }
+        elsif ( $type =~ /^long(blob|text)$/i ) {
+            $size = 4_294_967_295;
+        }
+
+        $return        = { 
+            type       => $type,
+            size       => $size,
+            list       => $list,
+            qualifiers => $item[3],
+        } 
+    }
 
-        field_type   : WORD
+parens_field_list : '(' field_name(s /,/) ')'
+    { $item[2] }
 
-        field_size   : '(' num_range ')' { $item{'num_range'} }
+parens_value_list : '(' VALUE(s /,/) ')'
+    { $item[2] }
 
-        num_range    : DIGITS ',' DIGITS
-            { $return = $item[1].','.$item[3] }
-                       | DIGITS
-            { $return = $item[1] }
+type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
+    { lc $item[1] }
 
+field_type   : WORD
 
-        create_table : /create/i /table/i
+create_index : /create/i /index/i
 
-        create_index : /create/i /index/i
+not_null     : /not/i /null/i 
+    { $return = 0 }
+    |
+    /null/i
+    { $return = 1 }
 
-        not_null     : /not/i /null/i { $return = 0 }
+unsigned     : /unsigned/i { $return = 0 }
 
-        unsigned     : /unsigned/i { $return = 0 }
+#default_val  : /default/i /(?:')?[\s\w\d:.-]*(?:')?/ 
+#    { 
+#        $item[2] =~ s/'//g; 
+#        $return  =  $item[2];
+#    }
 
-        default_val  : /default/i /(?:')?[\w\d.-]*(?:')?/ { $item[2]=~s/'//g; $return=$item[2] }
+default_val : 
+    /default/i 'CURRENT_TIMESTAMP'
+    {
+        $return =  $item[2];
+    }
+    |
+    /default/i /'(?:.*?\\')*.*?'|(?:')?[\w\d:.-]*(?:')?/
+    {
+        $item[2] =~ s/^\s*'|'\s*$//g;
+        $return  =  $item[2];
+    }
 
-        auto_inc     : /auto_increment/i { 1 }
+auto_inc : /auto_increment/i { 1 }
 
-        primary_key  : /primary/i /key/i { 1 }
+primary_key : /primary/i /key/i { 1 }
 
-        primary_key_index : primary_key index_name(?) '(' field_name(s /,/) ')'
-            { 
-                $return = { 
-                    name   => $item{'index_name'}[0],
-                    type   => 'primary_key',
-                    fields => $item[4],
-                } 
-            }
+constraint : primary_key_def
+    | unique_key_def
+    | foreign_key_def
+    | <error>
 
-        normal_index      : key index_name(?) '(' field_name(s /,/) ')'
-            { 
-                $return = { 
-                    name   => $item{'index_name'}[0],
-                    type   => 'normal',
-                    fields => $item[4],
-                } 
-            }
+foreign_key_def : foreign_key_def_begin parens_field_list reference_definition
+    {
+        $return              =  {
+            supertype        => 'constraint',
+            type             => 'foreign_key',
+            name             => $item[1],
+            fields           => $item[2],
+            %{ $item{'reference_definition'} },
+        }
+    }
 
-        unique_index      : /unique/i key(?) index_name(?) '(' field_name(s /,/) ')'
-            { 
-                $return = { 
-                    name   => $item{'index_name'}[0],
-                    type   => 'unique',
-                    fields => $item[5],
-                } 
-            }
+foreign_key_def_begin : /constraint/i /foreign key/i WORD
+    { $return = $item[3] }
+    |
+    /constraint/i NAME /foreign key/i
+    { $return = $item[2] }
+    |
+    /constraint/i /foreign key/i
+    { $return = '' }
+    |
+    /foreign key/i WORD
+    { $return = $item[2] }
+    |
+    /foreign key/i
+    { $return = '' }
+
+primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')'
+    { 
+        $return       = { 
+            supertype => 'constraint',
+            name      => $item{'index_name(?)'}[0],
+            type      => 'primary_key',
+            fields    => $item[4],
+        };
+    }
 
-        key          : /key/i 
-                       | /index/i
+unique_key_def : UNIQUE KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
+    { 
+        $return       = { 
+            supertype => 'constraint',
+            name      => $item{'index_name(?)'}[0],
+            type      => 'unique',
+            fields    => $item[5],
+        } 
+    }
 
-        table_type   : /TYPE=/i /\w+/ { $item[2] }
+normal_index : KEY index_name(?) '(' name_with_opt_paren(s /,/) ')'
+    { 
+        $return       = { 
+            supertype => 'index',
+            type      => 'normal',
+            name      => $item{'index_name(?)'}[0],
+            fields    => $item[4],
+        } 
+    }
 
-        WORD         : /\w+/
+fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
+    { 
+        $return       = { 
+            supertype => 'index',
+            type      => 'fulltext',
+            name      => $item{'index_name(?)'}[0],
+            fields    => $item[5],
+        } 
+    }
 
-        DIGITS       : /\d+/
+name_with_opt_paren : NAME parens_value_list(s?)
+    { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
 
-        COMMA        : ','
+UNIQUE : /unique/i { 1 }
 
-    !;
+KEY : /key/i | /index/i
 
-1;
+table_option : WORD /\s*=\s*/ WORD
+    { 
+        $return = { $item[1] => $item[3] };
+    }
+    | /comment/i /=/ /'.*?'/
+    {
+        my $comment = $item[3];
+        $comment    =~ s/^'//;
+        $comment    =~ s/'$//;
+        $return     = { comment => $comment };
+    }
+    | /(default )?(charset|character set)/i /\s*=\s*/ WORD
+    { 
+        $return = { 'CHARACTER SET' => $item[3] };
+    }
+    
+default : /default/i
 
-#-----------------------------------------------------
-# Where man is not nature is barren.
-# William Blake
-#-----------------------------------------------------
+ADD : /add/i
 
-=head1 NAME
+ALTER : /alter/i
 
-SQL::Translator::Parser::MySQL - parser for MySQL
+CREATE : /create/i
 
-=head1 SYNOPSIS
+TEMPORARY : /temporary/i
 
-  use SQL::Translator;
-  use SQL::Translator::Parser::MySQL;
+TABLE : /table/i
 
-  my $translator = SQL::Translator->new;
-  $translator->parser("SQL::Translator::Parser::MySQL");
+WORD : /\w+/
 
-=head1 DESCRIPTION
+DIGITS : /\d+/
+
+COMMA : ','
+
+NAME    : "`" /\w+/ "`"
+    { $item[2] }
+    | /\w+/
+    { $item[1] }
+
+VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
+    { $item[1] }
+    | /'.*?'/   
+    { 
+        # remove leading/trailing quotes 
+        my $val = $item[1];
+        $val    =~ s/^['"]|['"]$//g;
+        $return = $val;
+    }
+    | /NULL/
+    { 'NULL' }
+
+CURRENT_TIMESTAMP : /current_timestamp(\(\))?/i
+       | /now\(\)/i
+       { 'CURRENT_TIMESTAMP' }
+       
+!;
+
+# -------------------------------------------------------------------
+sub parse {
+    my ( $translator, $data ) = @_;
+    my $parser = Parse::RecDescent->new($GRAMMAR);
+
+    local $::RD_TRACE  = $translator->trace ? 1 : undef;
+    local $DEBUG       = $translator->debug;
+
+    unless (defined $parser) {
+        return $translator->error("Error instantiating Parse::RecDescent ".
+            "instance: Bad grammer");
+    }
+
+    my $result = $parser->startrule($data);
+    return $translator->error( "Parse failed." ) unless defined $result;
+    warn "Parse result:".Dumper( $result ) if $DEBUG;
+
+    my $schema = $translator->schema;
+    $schema->name($result->{'database_name'}) if $result->{'database_name'};
+
+    my @tables = sort { 
+        $result->{'tables'}{ $a }{'order'} 
+        <=> 
+        $result->{'tables'}{ $b }{'order'}
+    } keys %{ $result->{'tables'} };
+
+    for my $table_name ( @tables ) {
+        my $tdata =  $result->{tables}{ $table_name };
+        my $table =  $schema->add_table( 
+            name  => $tdata->{'table_name'},
+        ) or die $schema->error;
+
+        $table->comments( $tdata->{'comments'} );
+
+        my @fields = sort { 
+            $tdata->{'fields'}->{$a}->{'order'} 
+            <=>
+            $tdata->{'fields'}->{$b}->{'order'}
+        } keys %{ $tdata->{'fields'} };
+
+        for my $fname ( @fields ) {
+            my $fdata = $tdata->{'fields'}{ $fname };
+            my $field = $table->add_field(
+                name              => $fdata->{'name'},
+                data_type         => $fdata->{'data_type'},
+                size              => $fdata->{'size'},
+                default_value     => $fdata->{'default'},
+                is_auto_increment => $fdata->{'is_auto_inc'},
+                is_nullable       => $fdata->{'null'},
+                comments          => $fdata->{'comments'},
+            ) or die $table->error;
+
+            $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
+
+            for my $qual ( qw[ binary unsigned zerofill list collate ],
+                       'character set', 'on update' ) {
+                if ( my $val = $fdata->{ $qual } || $fdata->{ uc $qual } ) {
+                    next if ref $val eq 'ARRAY' && !@$val;
+                    $field->extra( $qual, $val );
+                }
+            }
+
+            if ( $field->data_type =~ /(set|enum)/i && !$field->size ) {
+                my %extra = $field->extra;
+                my $longest = 0;
+                for my $len ( map { length } @{ $extra{'list'} || [] } ) {
+                    $longest = $len if $len > $longest;
+                }
+                $field->size( $longest ) if $longest;
+            }
+
+            for my $cdata ( @{ $fdata->{'constraints'} } ) {
+                next unless $cdata->{'type'} eq 'foreign_key';
+                $cdata->{'fields'} ||= [ $field->name ];
+                push @{ $tdata->{'constraints'} }, $cdata;
+            }
+        }
+
+        for my $idata ( @{ $tdata->{'indices'} || [] } ) {
+            my $index  =  $table->add_index(
+                name   => $idata->{'name'},
+                type   => uc $idata->{'type'},
+                fields => $idata->{'fields'},
+            ) or die $table->error;
+        }
+
+        if ( my @options = @{ $tdata->{'table_options'} || [] } ) {
+            $table->options( \@options ) or die $table->error;
+        }
+
+        for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
+            my $constraint       =  $table->add_constraint(
+                name             => $cdata->{'name'},
+                type             => $cdata->{'type'},
+                fields           => $cdata->{'fields'},
+                reference_table  => $cdata->{'reference_table'},
+                reference_fields => $cdata->{'reference_fields'},
+                match_type       => $cdata->{'match_type'} || '',
+                on_delete        => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
+                on_update        => $cdata->{'on_update'} || $cdata->{'on_update_do'},
+            ) or die $table->error;
+        }
+    }
+
+    return 1;
+}
+
+1;
+
+# -------------------------------------------------------------------
+# Where man is not nature is barren.
+# William Blake
+# -------------------------------------------------------------------
 
-Blah blah blah.
+=pod
 
 =head1 AUTHOR
 
-Ken Y. Clark, kclark@logsoft.com
+Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
+Chris Mungall E<lt>cjm@fruitfly.orgE<gt>.
 
 =head1 SEE ALSO
 
-perl(1).
+perl(1), Parse::RecDescent, SQL::Translator::Schema.
 
 =cut