Fixed ORA-02329 and ORA-00907 errors.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / MySQL.pm
index c6fcd77..01c95da 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Parser::MySQL;
 
 # -------------------------------------------------------------------
-# $Id: MySQL.pm,v 1.24 2003-06-06 22:37:25 kycl4rk Exp $
+# $Id: MySQL.pm,v 1.32 2003-08-17 07:44:06 rossta Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
 #                    darren chamberlain <darren@cpan.org>,
@@ -123,7 +123,7 @@ Here's the word from the MySQL site
 
 use strict;
 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.32 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -156,12 +156,15 @@ eofile : /^\Z/
 
 statement : comment
     | use
+    | set
     | drop
     | create
     | <error>
 
 use : /use/i WORD ';'
 
+set : /set/i /[^;]+/ ';'
+
 drop : /drop/i WORD(s) ';'
 
 create : CREATE /database/i WORD ';'
@@ -235,11 +238,16 @@ create_definition : constraint
     | field
     | <error>
 
-comment : /^\s*(?:#|-{2}).*\n/
+comment : /^\s*(?:#|-{2}).*\n/ { 
+    my $comment =  $item[1];
+    $comment    =~ s/^\s*(#|-{2})\s*//;
+    $comment    =~ s/\s*$//;
+    $return     = $comment;
+}
 
 blank : /\s*/
 
-field : field_name data_type field_qualifier(s?) reference_definition(?)
+field : comment(s?) field_name data_type field_qualifier(s?) reference_definition(?) comment(s?)
     { 
         my %qualifiers = map { %$_ } @{ $item{'field_qualifier(s?)'} || [] };
         my $null = defined $item{'not_null'} ? $item{'not_null'} : 1;
@@ -248,6 +256,8 @@ field : field_name data_type field_qualifier(s?) reference_definition(?)
             $qualifiers{ $_ } = 1 for @type_quals;
         }
 
+        my @comments = ( @{ $item[1] }, @{ $item[6] } );
+
         $return = { 
             supertype   => 'field',
             name        => $item{'field_name'}, 
@@ -256,6 +266,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,6 +307,13 @@ field_qualifier : unsigned
         } 
     }
 
+field_qualifier : /character set/i WORD
+    {
+        $return = {
+            character_set => $item[2],
+        }
+    }
+
 reference_definition : /references/i table_name parens_field_list(?) match_type(?) on_delete_do(?) on_update_do(?)
     {
         $return = {
@@ -329,9 +347,9 @@ index : normal_index
     | fulltext_index
     | <error>
 
-table_name   : WORD
+table_name   : NAME
 
-field_name   : WORD
+field_name   : NAME
 
 index_name   : WORD
 
@@ -367,7 +385,7 @@ data_type    : WORD parens_value_list(s?) type_qualifier(s?)
             elsif ( lc $type eq 'bigint' ) {
                 $size = [20];
             }
-            elsif ( lc $type =~ /(float|double|decimal|numeric|real)/ ) {
+            elsif ( lc $type =~ /(float|double|decimal|numeric|real|fixed|dec)/ ) {
                 $size = [8,2];
             }
         }
@@ -425,7 +443,7 @@ foreign_key_def : opt_constraint(?) /foreign key/i WORD(?) parens_field_list ref
 
 opt_constraint : /constraint/i WORD
 
-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',
@@ -557,6 +575,7 @@ 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'};
@@ -570,7 +589,7 @@ sub parse {
 
             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;
                 }
@@ -606,7 +625,7 @@ sub parse {
         }
     }
 
-    return $result;
+    return 1;
 }
 
 1;