All Schema objects now have an extra attribute. Added parsing support (and
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / MySQL.pm
index c65424a..c0a2144 100644 (file)
@@ -1,11 +1,9 @@
 package SQL::Translator::Parser::MySQL;
 
 # -------------------------------------------------------------------
-# $Id: MySQL.pm,v 1.31 2003-08-16 20:10:39 rossta Exp $
+# $Id: MySQL.pm,v 1.44 2004-03-01 17:39:22 kycl4rk 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
@@ -123,7 +121,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.31 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.44 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -141,7 +139,7 @@ $::RD_HINT   = 1; # Give out hints to help fix problems.
 $GRAMMAR = q!
 
 { 
-    our ( %tables, $table_order );
+    my ( %tables, $table_order, @table_comments );
 }
 
 #
@@ -162,12 +160,18 @@ statement : comment
     | <error>
 
 use : /use/i WORD ';'
+    { @table_comments = () }
 
 set : /set/i /[^;]+/ ';'
+    { @table_comments = () }
+
+drop : /drop/i TABLE /[^;]+/ ';'
 
 drop : /drop/i WORD(s) ';'
+    { @table_comments = () }
 
 create : CREATE /database/i WORD ';'
+    { @table_comments = () }
 
 create : CREATE TEMPORARY(?) TABLE opt_if_not_exists(?) table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
     { 
@@ -175,10 +179,14 @@ create : CREATE TEMPORARY(?) TABLE opt_if_not_exists(?) table_name '(' create_de
         $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 };
@@ -194,27 +202,15 @@ 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?)'} } ) {
+            $tables{ $table_name }{'table_options'} = \@options;
         }
 
         1;
@@ -224,6 +220,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],
@@ -236,26 +233,39 @@ 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/ { 
-    my $comment =  $item[1];
-    $comment    =~ s/^\s*(#|-{2})\s*//;
-    $comment    =~ s/\s*$//;
-    $return     = $comment;
-}
+comment : /^\s*(?:#|-{2}).*\n/ 
+    { 
+        my $comment =  $item[1];
+        $comment    =~ s/^\s*(#|--)\s*//;
+        $comment    =~ s/\s*$//;
+        $return     = $comment;
+        push @table_comments, $comment;
+    }
+
+field_comment : /^\s*(?:#|-{2}).*\n/ 
+    { 
+        my $comment =  $item[1];
+        $comment    =~ s/^\s*(#|--)\s*//;
+        $comment    =~ s/\s*$//;
+        $return     = $comment;
+    }
 
 blank : /\s*/
 
-field : comment(s?) field_name data_type field_qualifier(s?) reference_definition(?) comment(s?)
+field : field_comment(s?) field_name data_type field_qualifier(s?) reference_definition(?) 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[6] } );
 
         $return = { 
@@ -326,9 +336,9 @@ reference_definition : /references/i table_name parens_field_list(?) match_type(
         }
     }
 
-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
     { $item[2] }
@@ -351,7 +361,7 @@ table_name   : NAME
 
 field_name   : NAME
 
-index_name   : WORD
+index_name   : NAME
 
 data_type    : WORD parens_value_list(s?) type_qualifier(s?)
     { 
@@ -370,26 +380,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)?$/ ) {
                 $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,
@@ -415,9 +440,15 @@ not_null     : /not/i /null/i { $return = 0 }
 
 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 /'(?:.*?\\')*.*?'|(?:')?[\w\d:.-]*(?:')?/
+    {
+        $item[2] =~ s/^\s*'|'\s*$//g;
         $return  =  $item[2];
     }
 
@@ -430,18 +461,25 @@ 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 
+    { $return = '' }
+    |
+    /constraint/i WORD /foreign key/i
+    { $return = $item[2] }
+    |
+    /foreign key/i
+    { $return = '' }
 
 primary_key_def : primary_key index_name(?) '(' name_with_opt_paren(s /,/) ')'
     { 
@@ -490,9 +528,9 @@ UNIQUE : /unique/i { 1 }
 
 KEY : /key/i | /index/i
 
-table_option : /[^\s;]*/ 
+table_option : WORD /\s*=\s*/ WORD
     { 
-        $return = { split /=/, $item[1] }
+        $return = { $item[1] => $item[3] };
     }
 
 CREATE : /create/i
@@ -554,11 +592,7 @@ sub parse {
             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'} 
@@ -611,6 +645,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'},