Bumping version to 1.62
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
index 86d23c3..0430423 100644 (file)
@@ -82,7 +82,7 @@ Set the fields character set and collation order.
 use strict;
 use warnings;
 our ( $DEBUG, %used_names );
-our $VERSION = '1.59';
+our $VERSION = '1.62';
 $DEBUG   = 0 unless defined $DEBUG;
 
 # Maximum length for most identifiers is 64, according to:
@@ -356,11 +356,14 @@ sub create_trigger {
         }
 
         my $action = $trigger->action;
-        $action .= ";" unless $action =~ /;\s*\z/;
+        if($action !~ /^ \s* BEGIN [\s\;] .*? [\s\;] END [\s\;]* $/six) {
+            $action .= ";" unless $action =~ /;\s*\z/;
+            $action = "BEGIN $action END";
+        }
 
         push @statements, "DROP TRIGGER IF EXISTS " . $generator->quote($name) if $options->{add_drop_trigger};
         push @statements, sprintf(
-            "CREATE TRIGGER %s %s %s ON %s\n  FOR EACH ROW BEGIN %s END",
+            "CREATE TRIGGER %s %s %s ON %s\n  FOR EACH ROW %s",
             $generator->quote($name), $trigger->perform_action_when, $event,
             $generator->quote($trigger->on_table), $action,
         );
@@ -713,7 +716,7 @@ sub alter_drop_constraint
         push @out, $c->type;
     }
     else {
-        push @out, ($c->type eq FOREIGN_KEY ? $c->type : "INDEX"),
+        push @out, ($c->type eq FOREIGN_KEY ? $c->type : "CONSTRAINT"),
             $generator->quote($c->name);
     }
     return join(' ',@out);
@@ -740,12 +743,14 @@ sub create_constraint
 
     my $reference_table_name = $generator->quote($c->reference_table);
 
-    my @fields = $c->fields or return;
+    my @fields = $c->fields;
 
     if ( $c->type eq PRIMARY_KEY ) {
+        return unless @fields;
         return 'PRIMARY KEY (' . join(", ", map { $generator->quote($_) } @fields) . ')';
     }
     elsif ( $c->type eq UNIQUE ) {
+        return unless @fields;
         return sprintf 'UNIQUE %s(%s)',
           ((defined $c->name && $c->name)
             ? $generator->quote(
@@ -757,6 +762,7 @@ sub create_constraint
         ;
     }
     elsif ( $c->type eq FOREIGN_KEY ) {
+        return unless @fields;
         #
         # Make sure FK field is indexed or MySQL complains.
         #
@@ -810,6 +816,20 @@ sub create_constraint
         }
         return $def;
     }
+    elsif ( $c->type eq CHECK_C ) {
+        my $table = $c->table;
+        my $c_name = truncate_id_uniquely( $c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH );
+
+        my $def = join(' ',
+                         'CONSTRAINT',
+                         ($c_name ? $generator->quote($c_name) : () ),
+                         'CHECK'
+                      );
+
+
+        $def .= ' ('. $c->expression . ')';
+        return $def;
+    }
 
     return undef;
 }