Allow skipped insert statements and trigger bodies to contain quoted semi-colons
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / MySQL.pm
index a812e74..20a75fa 100644 (file)
@@ -1,11 +1,9 @@
 package SQL::Translator::Parser::MySQL;
 
 # -------------------------------------------------------------------
-# $Id: MySQL.pm,v 1.23 2003-06-06 00:05:09 kycl4rk Exp $
+# $Id: MySQL.pm,v 1.54 2006-06-09 13:56:58 schiffbruechige Exp $
 # -------------------------------------------------------------------
-# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
-#                    darren chamberlain <darren@cpan.org>,
-#                    Chris Mungall <cjm@fruitfly.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
@@ -119,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.23 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.54 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -138,10 +149,10 @@ $::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.
 
-$GRAMMAR = q!
+$GRAMMAR = << 'END_OF_GRAMMAR';
 
 { 
-    our ( %tables, $table_order );
+    my ( $database_name, %tables, $table_order, @table_comments );
 }
 
 #
@@ -150,32 +161,81 @@ $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/
 
 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 = () }
+
+string :
+  # MySQL strings, unlike common SQL strings, can be double-quoted or 
+  # single-quoted, and you can escape the delmiters by doubling (but only the 
+  # delimiter) or by backslashing.
+
+   /'(\\.|''|[^\\\'])*'/ |
+   /"(\\.|""|[^\\\"])*"/
+  # For reference, std sql str: /(?:(?:\')(?:[^\']*(?:(?:\'\')[^\']*)*)(?:\'))//
+
+nonstring : /[^;\'"]+/
+
+statement_body : (string | nonstring)(s?)
+
+insert : /insert/i  statement_body ';'
+
+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;
         $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 };
@@ -191,26 +251,22 @@ create : CREATE TEMPORARY(?) TABLE opt_if_not_exists(?) table_name '(' create_de
                 }
             }
             elsif ( $definition->{'supertype'} eq 'constraint' ) {
-                # prob get rid of this?
-#                for my $field ( @{ $definition->{'fields'} } ) {
-#                    push @{ 
-#                        $tables{$table_name}{'fields'}{$field}{'constraints'}
-#                    },
-#                    $definition; 
-#                }
-
-                # this should be the only one needed
                 push @{ $tables{ $table_name }{'constraints'} }, $definition;
             }
             elsif ( $definition->{'supertype'} eq 'index' ) {
-                push @{ $tables{ $table_name }{'indices'} },
-                    $definition;
+                push @{ $tables{ $table_name }{'indices'} }, $definition;
             }
         }
 
-        for my $opt ( @{ $item{'table_option(s?)'} } ) {
-            if ( my ( $key, $val ) = each %$opt ) {
-                $tables{ $table_name }{'table_options'}{ $key } = $val;
+        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;
+                }
             }
         }
 
@@ -221,6 +277,7 @@ 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],
@@ -233,21 +290,56 @@ create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_n
 create_definition : constraint 
     | index
     | field
+    | comment
     | <error>
 
-comment : /^\s*(?:#|-{2}).*\n/
+comment : /^\s*(?:#|-{2}).*\n/ 
+    { 
+        my $comment =  $item[1];
+        $comment    =~ s/^\s*(#|--)\s*//;
+        $comment    =~ s/\s*$//;
+        $return     = $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;
+    }
+
+
+field_comment2 : /comment/i /'.*?'/
+    {
+        my $comment = $item[2];
+        $comment    =~ s/^'//;
+        $comment    =~ s/'$//;
+        $return     = $comment;
+    }
 
 blank : /\s*/
 
-field : field_name data_type field_qualifier(s?) reference_definition(?)
+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?)'} || [] };
-        my $null = defined $item{'not_null'} ? $item{'not_null'} : 1;
-        delete $qualifiers{'not_null'};
+        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'}, 
@@ -256,6 +348,7 @@ field : field_name data_type field_qualifier(s?) reference_definition(?)
             list        => $item{'data_type'}{'list'},
             null        => $null,
             constraints => $item{'reference_definition(?)'},
+            comments    => [ @comments ],
             %qualifiers,
         } 
     }
@@ -296,26 +389,65 @@ field_qualifier : unsigned
         } 
     }
 
-reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete_do(?) on_update_do(?)
+field_qualifier : /character set/i WORD 
+    {
+        $return = {
+            'CHARACTER SET' => $item[2],
+        }
+    }
+
+field_qualifier : /collate/i WORD
+    {
+        $return = {
+            COLLATE => $item[2],
+        }
+    }
+
+field_qualifier : /on update/i CURRENT_TIMESTAMP
+    {
+        $return = {
+            'ON UPDATE' => $item[2],
+        }
+    }
+
+field_qualifier : /unique/i KEY(?)
+    {
+        $return = {
+            is_unique => 1,
+        }
+    }
+
+field_qualifier : KEY
+    {
+        $return = {
+            has_index => 1,
+        }
+    }
+
+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_do     => $item[5][0],
-            on_update_do     => $item[6][0],
+            on_delete        => $item[5][0],
+            on_update        => $item[6][0],
         }
     }
 
-match_type : /match full/i { 'match_full' }
+match_type : /match full/i { 'full' }
     |
-    /match partial/i { 'match_partial' }
+    /match partial/i { 'partial' }
 
-on_delete_do : /on delete/i reference_option
+on_delete : /on delete/i reference_option
     { $item[2] }
 
-on_update_do : /on update/i reference_option
+on_update : 
+    /on update/i 'CURRENT_TIMESTAMP'
+    { $item[2] }
+    |
+    /on update/i reference_option
     { $item[2] }
 
 reference_option: /restrict/i | 
@@ -329,11 +461,11 @@ index : normal_index
     | fulltext_index
     | <error>
 
-table_name   : WORD
+table_name   : NAME
 
-field_name   : WORD
+field_name   : NAME
 
-index_name   : WORD
+index_name   : NAME
 
 data_type    : WORD parens_value_list(s?) type_qualifier(s?)
     { 
@@ -352,26 +484,41 @@ data_type    : WORD parens_value_list(s?) type_qualifier(s?)
 
         unless ( @{ $size || [] } ) {
             if ( lc $type eq 'tinyint' ) {
-                $size = [4];
+                $size = 4;
             }
             elsif ( lc $type eq 'smallint' ) {
-                $size = [6];
+                $size = 6;
             }
             elsif ( lc $type eq 'mediumint' ) {
-                $size = [9];
+                $size = 9;
             }
-            elsif ( $type =~ /^int(eger)?$/ ) {
+            elsif ( $type =~ /^int(eger)?$/i ) {
                 $type = 'int';
-                $size = [11];
+                $size = 11;
             }
             elsif ( lc $type eq 'bigint' ) {
-                $size = [20];
+                $size = 20;
             }
-            elsif ( lc $type =~ /(float|double|decimal|numeric|real)/ ) {
+            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,
@@ -393,13 +540,29 @@ field_type   : WORD
 
 create_index : /create/i /index/i
 
-not_null     : /not/i /null/i { $return = 0 }
+not_null     : /not/i /null/i 
+    { $return = 0 }
+    |
+    /null/i
+    { $return = 1 }
 
 unsigned     : /unsigned/i { $return = 0 }
 
-default_val  : /default/i /(?:')?[\w\d:.-]*(?:')?/ 
-    { 
-        $item[2] =~ s/'//g; 
+#default_val  : /default/i /(?:')?[\s\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];
     }
 
@@ -412,20 +575,33 @@ constraint : primary_key_def
     | foreign_key_def
     | <error>
 
-foreign_key_def : opt_constraint(?) /foreign key/i WORD(?) parens_field_list reference_definition
+foreign_key_def : foreign_key_def_begin parens_field_list reference_definition
     {
         $return              =  {
             supertype        => 'constraint',
             type             => 'foreign_key',
-            name             => $item[3][0],
-            fields           => $item[4],
+            name             => $item[1],
+            fields           => $item[2],
             %{ $item{'reference_definition'} },
         }
     }
 
-opt_constraint : /constraint/i WORD
+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(?) '(' field_name(s /,/) ')'
+primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')'
     { 
         $return       = { 
             supertype => 'constraint',
@@ -472,10 +648,27 @@ UNIQUE : /unique/i { 1 }
 
 KEY : /key/i | /index/i
 
-table_option : /[^\s;]*/ 
+table_option : /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] };
+    }
+    | WORD /\s*=\s*/ WORD
     { 
-        $return = { split /=/, $item[1] }
+        $return = { $item[1] => $item[3] };
     }
+    
+default : /default/i
+
+ADD : /add/i
+
+ALTER : /alter/i
 
 CREATE : /create/i
 
@@ -506,7 +699,11 @@ VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
     | /NULL/
     { 'NULL' }
 
-!;
+CURRENT_TIMESTAMP : /current_timestamp(\(\))?/i
+       | /now\(\)/i
+       { 'CURRENT_TIMESTAMP' }
+       
+END_OF_GRAMMAR
 
 # -------------------------------------------------------------------
 sub parse {
@@ -523,20 +720,24 @@ 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;
-    for my $table_name ( keys %{ $result } ) {
-        my $tdata =  $result->{ $table_name };
+    $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;
 
-#        for my $opt ( @{ $tdata->{'table_options'} } ) {
-#            if ( my ( $key, $val ) = each %$opt ) {
-#                $tables->options( 
-#            }
-#        }
+        $table->comments( $tdata->{'comments'} );
 
         my @fields = sort { 
             $tdata->{'fields'}->{$a}->{'order'} 
@@ -553,20 +754,38 @@ sub parse {
                 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 ] ) {
+            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 ( $fdata->{'has_index'} ) {
+                $table->add_index(
+                    name   => '',
+                    type   => 'NORMAL',
+                    fields => $fdata->{'name'},
+                ) or die $table->error;
+            }
+
+            if ( $fdata->{'is_unique'} ) {
+                $table->add_constraint(
+                    name   => '',
+                    type   => 'UNIQUE',
+                    fields => $fdata->{'name'},
+                ) or die $table->error;
+            }
+
             if ( $field->data_type =~ /(set|enum)/i && !$field->size ) {
                 my %extra = $field->extra;
-                my $longest;
+                my $longest = 0;
                 for my $len ( map { length } @{ $extra{'list'} || [] } ) {
                     $longest = $len if $len > $longest;
                 }
@@ -588,6 +807,10 @@ sub parse {
             ) 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'},
@@ -596,31 +819,31 @@ sub parse {
                 reference_table  => $cdata->{'reference_table'},
                 reference_fields => $cdata->{'reference_fields'},
                 match_type       => $cdata->{'match_type'} || '',
-                on_delete        => $cdata->{'on_delete_do'},
-                on_update        => $cdata->{'on_update_do'},
+                on_delete        => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
+                on_update        => $cdata->{'on_update'} || $cdata->{'on_update_do'},
             ) or die $table->error;
         }
     }
 
-    return $result;
+    return 1;
 }
 
 1;
 
-# ----------------------------------------------------
+# -------------------------------------------------------------------
 # Where man is not nature is barren.
 # William Blake
-# ----------------------------------------------------
+# -------------------------------------------------------------------
 
 =pod
 
 =head1 AUTHOR
 
-Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
+Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
 Chris Mungall E<lt>cjm@fruitfly.orgE<gt>.
 
 =head1 SEE ALSO
 
-perl(1), Parse::RecDescent, SQL::Translator::Schema.
+Parse::RecDescent, SQL::Translator::Schema.
 
 =cut