Allow skipped insert statements and trigger bodies to contain quoted semi-colons
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / MySQL.pm
index 5d4a4d1..20a75fa 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Parser::MySQL;
 
 # -------------------------------------------------------------------
-# $Id: MySQL.pm,v 1.50 2005-06-28 16:39:41 mwz444 Exp $
+# $Id: MySQL.pm,v 1.54 2006-06-09 13:56:58 schiffbruechige Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2002-4 SQLFairy Authors
 #
@@ -134,7 +134,7 @@ A subset of INSERT that we ignore:
 
 use strict;
 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.50 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.54 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -149,7 +149,7 @@ $::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';
 
 { 
     my ( $database_name, %tables, $table_order, @table_comments );
@@ -190,7 +190,20 @@ drop : /drop/i TABLE /[^;]+/ ';'
 drop : /drop/i WORD(s) ';'
     { @table_comments = () }
 
-insert : /insert/i  /[^;]+/ ';'
+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 /,/) ';'
     {
@@ -286,7 +299,6 @@ comment : /^\s*(?:#|-{2}).*\n/
         $comment    =~ s/^\s*(#|--)\s*//;
         $comment    =~ s/\s*$//;
         $return     = $comment;
-        push @table_comments, $comment;
     }
 
 comment : /\/\*/ /[^\*]+/ /\*\// ';'
@@ -377,7 +389,7 @@ field_qualifier : unsigned
         } 
     }
 
-field_qualifier : /character set/i WORD
+field_qualifier : /character set/i WORD 
     {
         $return = {
             'CHARACTER SET' => $item[2],
@@ -398,6 +410,20 @@ field_qualifier : /on update/i CURRENT_TIMESTAMP
         }
     }
 
+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 = {
@@ -622,11 +648,7 @@ UNIQUE : /unique/i { 1 }
 
 KEY : /key/i | /index/i
 
-table_option : WORD /\s*=\s*/ WORD
-    { 
-        $return = { $item[1] => $item[3] };
-    }
-    | /comment/i /=/ /'.*?'/
+table_option : /comment/i /=/ /'.*?'/
     {
         my $comment = $item[3];
         $comment    =~ s/^'//;
@@ -637,6 +659,10 @@ table_option : WORD /\s*=\s*/ WORD
     { 
         $return = { 'CHARACTER SET' => $item[3] };
     }
+    | WORD /\s*=\s*/ WORD
+    { 
+        $return = { $item[1] => $item[3] };
+    }
     
 default : /default/i
 
@@ -677,7 +703,7 @@ CURRENT_TIMESTAMP : /current_timestamp(\(\))?/i
        | /now\(\)/i
        { 'CURRENT_TIMESTAMP' }
        
-!;
+END_OF_GRAMMAR
 
 # -------------------------------------------------------------------
 sub parse {
@@ -741,6 +767,22 @@ sub parse {
                 }
             }
 
+            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 = 0;
@@ -797,11 +839,11 @@ sub parse {
 
 =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