Use accessor for table options in MySQL producer
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
index 7e10d55..1f8c541 100644 (file)
@@ -156,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) {
@@ -612,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(
@@ -694,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
@@ -731,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 ) {
         #