Use accessor for table options in MySQL producer
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
index 1ed2b1e..1f8c541 100644 (file)
@@ -1,23 +1,5 @@
 package SQL::Translator::Producer::MySQL;
 
-# -------------------------------------------------------------------
-# 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
-# published by the Free Software Foundation; version 2.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-# 02111-1307  USA
-# -------------------------------------------------------------------
-
 =head1 NAME
 
 SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
@@ -99,8 +81,8 @@ Set the fields charater set and collation order.
 
 use strict;
 use warnings;
-use vars qw[ $VERSION $DEBUG %used_names ];
-$VERSION = '1.59';
+our ( $DEBUG, %used_names );
+our $VERSION = '1.59';
 $DEBUG   = 0 unless defined $DEBUG;
 
 # Maximum length for most identifiers is 64, according to:
@@ -174,9 +156,7 @@ sub preprocess_schema {
       # TYPE is a synonym, but ENGINE is the preferred option name.
       #
 
-      # We have to use the hash directly here since otherwise there is no way
-      # to remove options.
-      my $options = ( $table->{options} ||= []);
+      my $options = $table->options;
 
       # If multiple option names, normalize to the first one
       if (ref $opt_name) {
@@ -630,7 +610,12 @@ sub create_field
     }
 
     # Null?
-    $field_def .= ' NOT NULL' unless $field->is_nullable;
+    if ( $field->is_nullable ) {
+        $field_def .= ' NULL';
+    }
+    else {
+        $field_def .= ' NOT NULL';
+    }
 
     # Default?
     SQL::Translator::Producer->_apply_default_value(
@@ -712,12 +697,15 @@ sub alter_drop_constraint
     my $qc      = $options->{quote_field_names} || '';
     my $table_name = quote_table_name($c->table->name, $qt);
 
-    my $out = sprintf('ALTER TABLE %s DROP %s %s',
-                      $table_name,
-                      $c->type eq FOREIGN_KEY ? $c->type : "INDEX",
-                      $qc . $c->name . $qc );
-
-    return $out;
+    my @out = ('ALTER','TABLE',$table_name,'DROP');
+    if($c->type eq PRIMARY_KEY) {
+        push @out, $c->type;
+    }
+    else {
+        push @out, ($c->type eq FOREIGN_KEY ? $c->type : "INDEX"),
+            $qc . $c->name . $qc;
+    }
+    return join(' ',@out);
 }
 
 sub alter_create_constraint
@@ -749,10 +737,18 @@ sub create_constraint
         return 'PRIMARY KEY (' . $qf . join("$qf, $qf", @fields). $qf . ')';
     }
     elsif ( $c->type eq UNIQUE ) {
-        return
-        'UNIQUE '.
-            (defined $c->name ? $qf.truncate_id_uniquely( $c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH ).$qf.' ' : '').
-            '(' . $qf . join("$qf, $qf", @fields). $qf . ')';
+        return sprintf 'UNIQUE %s(%s)',
+          ((defined $c->name && $c->name)
+            ? join ('',
+                $qf,
+                truncate_id_uniquely( $c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH ),
+                $qf,
+                ' '
+              )
+            : ''
+          ),
+          ( join ', ', map { "${qf}${_}${qf}" } @fields ),
+        ;
     }
     elsif ( $c->type eq FOREIGN_KEY ) {
         #