Reduce $Id to its normal form
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / MySQL.pm
index 86027e1..a25de2d 100644 (file)
@@ -1,9 +1,9 @@
 package SQL::Translator::Parser::MySQL;
 
 # -------------------------------------------------------------------
-# $Id: MySQL.pm,v 1.58 2007-03-19 17:15:24 duality72 Exp $
+# $Id$
 # -------------------------------------------------------------------
-# Copyright (C) 2002-4 SQLFairy Authors
+# Copyright (C) 2002-2009 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
@@ -101,9 +101,12 @@ Here's the word from the MySQL site
   
   table_options:
           TYPE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM }
+  or      ENGINE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM }
   or      AUTO_INCREMENT = #
   or      AVG_ROW_LENGTH = #
+  or      [ DEFAULT ] CHARACTER SET charset_name
   or      CHECKSUM = {0 | 1}
+  or      COLLATE collation_name
   or      COMMENT = "string"
   or      MAX_ROWS = #
   or      MIN_ROWS = #
@@ -117,6 +120,7 @@ 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] ...
@@ -130,19 +134,36 @@ A subset of INSERT that we ignore:
 
   INSERT anything
 
+=head1 ARGUMENTS
+
+This parser takes a single optional parser_arg C<mysql_parser_version>, which
+provides the desired version for the target database. Any statement in the processed
+dump file, that is commented with a version higher than the one supplied, will be stripped.
+
+Valid version specifiers for C<mysql_parser_version> are listed L<here|SQL::Translator::Utils/parse_mysql_version>
+
+More information about the MySQL comment-syntax: L<http://dev.mysql.com/doc/refman/5.0/en/comments.html>
+
+
 =cut
 
 use strict;
 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.58 $ =~ /(\d+)\.(\d+)/;
+$VERSION = '1.99';
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
 use Parse::RecDescent;
 use Exporter;
 use Storable qw(dclone);
+use DBI qw(:sql_types);
 use base qw(Exporter);
 
+use SQL::Translator::Utils qw/parse_mysql_version/;
+
+our %type_mapping = (
+);
+
 @EXPORT_OK = qw(parse);
 
 # Enable warnings within the Parse::RecDescent module.
@@ -292,7 +313,7 @@ create : CREATE UNIQUE(?) /(index|key)/i index_name /on/i table_name '(' field_n
         push @{ $tables{ $item{'table_name'} }{'indices'} },
             {
                 name   => $item[4],
-                type   => $item[2] ? 'unique' : 'normal',
+                type   => $item[2][0] ? 'unique' : 'normal',
                 fields => $item[8],
             }
         ;
@@ -513,6 +534,7 @@ reference_option: /restrict/i |
 
 index : normal_index
     | fulltext_index
+    | spatial_index
     | <error>
 
 table_name   : NAME
@@ -575,7 +597,7 @@ unsigned     : /unsigned/i { $return = 0 }
 default_val : 
     /default/i 'CURRENT_TIMESTAMP'
     {
-        $return =  $item[2];
+        $return =  \$item[2];
     }
     |
     /default/i /'(?:.*?\\')*.*?'|(?:')?[\w\d:.-]*(?:')?/
@@ -659,10 +681,20 @@ fulltext_index : /fulltext/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/)
         } 
     }
 
+spatial_index : /spatial/i KEY(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
+    { 
+        $return       = { 
+            supertype => 'index',
+            type      => 'spatial',
+            name      => $item{'index_name(?)'}[0],
+            fields    => $item[5],
+        } 
+    }
+
 name_with_opt_paren : NAME parens_value_list(s?)
     { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
 
-UNIQUE : /unique/i { 1 }
+UNIQUE : /unique/i
 
 KEY : /key/i | /index/i
 
@@ -673,10 +705,18 @@ table_option : /comment/i /=/ /'.*?'/
         $comment    =~ s/'$//;
         $return     = { comment => $comment };
     }
-    | /(default )?(charset|character set)/i /\s*=\s*/ WORD
+    | /(default )?(charset|character set)/i /\s*=?\s*/ WORD
     { 
         $return = { 'CHARACTER SET' => $item[3] };
     }
+    | /collate/i WORD
+    {
+        $return = { 'COLLATE' => $item[2] }
+    }
+    | /union/i /\s*=\s*/ '(' table_name(s /,/) ')'
+    { 
+        $return = { $item[1] => $item[4] };
+    }
     | WORD /\s*=\s*/ WORD
     { 
         $return = { $item[1] => $item[3] };
@@ -700,7 +740,9 @@ DIGITS : /\d+/
 
 COMMA : ','
 
-NAME    : "`" /\w+/ "`"
+BACKTICK : '`'
+
+NAME    : BACKTICK /[^`]+/ BACKTICK
     { $item[2] }
     | /\w+/
     { $item[1] }
@@ -727,7 +769,6 @@ END_OF_GRAMMAR
 sub parse {
     my ( $translator, $data ) = @_;
     my $parser = Parse::RecDescent->new($GRAMMAR);
-
     local $::RD_TRACE  = $translator->trace ? 1 : undef;
     local $DEBUG       = $translator->debug;
 
@@ -737,7 +778,9 @@ sub parse {
     }
     
     # Preprocess for MySQL-specific and not-before-version comments from mysqldump
-    my $parser_version = $translator->parser_args->{mysql_parser_version} || DEFAULT_PARSER_VERSION;
+    my $parser_version = 
+        parse_mysql_version ($translator->parser_args->{mysql_parser_version}, 'mysql') 
+        || DEFAULT_PARSER_VERSION;
     while ( $data =~ s#/\*!(\d{5})?(.*?)\*/#($1 && $1 > $parser_version ? '' : $2)#es ) {}
 
     my $result = $parser->startrule($data);
@@ -805,15 +848,6 @@ sub parse {
                 ) or die $table->error;
             }
 
-            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 ];
@@ -831,7 +865,19 @@ sub parse {
         }
 
         if ( my @options = @{ $tdata->{'table_options'} || [] } ) {
-            $table->options( \@options ) or die $table->error;
+            my @cleaned_options;
+            my @ignore_opts = $translator->parser_args->{ignore_opts}?split(/,/,$translator->parser_args->{ignore_opts}):();
+            if (@ignore_opts) {
+                my $ignores = { map { $_ => 1 } @ignore_opts };
+                foreach my $option (@options) {
+                    # make sure the option isn't in ignore list
+                    my ($option_key) = keys %$option;
+                    push(@cleaned_options, $option) unless (exists $ignores->{$option_key});
+                }
+            } else {
+                @cleaned_options = @options;
+            }
+            $table->options( \@cleaned_options ) or die $table->error;
         }
 
         for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
@@ -908,7 +954,7 @@ sub normalize_field {
         }
         elsif ( lc $type =~ /(float|double|decimal|numeric|real|fixed|dec)/ ) {
             my $old_size = (ref $size || '') eq 'ARRAY' ? $size : [];
-            $changed = @$old_size != 2 && $old_size->[0] != 8 && $old_size->[1] != 2;
+            $changed = @$old_size != 2 || $old_size->[0] != 8 || $old_size->[1] != 2;
             $size = [8,2];
         }
     }
@@ -929,7 +975,17 @@ sub normalize_field {
         $changed = $size != 4_294_967_295;
         $size = 4_294_967_295;
     }
-    $DB::single = 1 if $field->name eq 'employee_id';
+    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;
+        }
+        $changed = 1;
+        $size = $longest if $longest;
+    }
+
+
     if ($changed) {
       # We only want to clone the field, not *everything*
       { local $field->{table} = undef;
@@ -938,10 +994,12 @@ sub normalize_field {
       }
       $field->size($size);
       $field->data_type($type);
+      $field->sql_data_type( $type_mapping{lc $type} ) if exists $type_mapping{lc $type};
       $field->extra->{list} = $list if @$list;
     }
 }
 
+
 1;
 
 # -------------------------------------------------------------------