Add support for triggers in the MySQL producer (RT#63452) ilmari++
Rafael Kitover [Tue, 30 Nov 2010 10:42:27 +0000 (10:42 +0000)]
Changes
lib/SQL/Translator/Producer/MySQL.pm

diff --git a/Changes b/Changes
index 46ac5c3..1da568f 100644 (file)
--- a/Changes
+++ b/Changes
@@ -5,6 +5,7 @@
 * Some minor fixes to squash warnings on new perls
 * Support a custom_type_name hint when creating enum types in PostgreSQL
 * Fix sqlt options/pod mismatch (RT#58318)
+* Add support for triggers in the MySQL producer
 
 # ----------------------------------------------------------
 # 0.11006 2010-06-03
index db7f7c8..bfa1da6 100644 (file)
@@ -320,6 +320,20 @@ sub produce {
       }
     }
 
+    if ($mysql_version >= 5.000002) {
+      for my $trigger ( $schema->get_triggers ) {
+        push @table_defs, create_trigger($trigger,
+                                         { add_drop_trigger  => $add_drop_table,
+                                           show_warnings        => $show_warnings,
+                                           no_comments          => $no_comments,
+                                           quote_table_names    => $qt,
+                                           quote_field_names    => $qf,
+                                           max_id_length        => $max_id_length,
+                                           mysql_version        => $mysql_version
+                                           });
+      }
+    }
+
 
 #    print "@table_defs\n";
     push @table_defs, "SET foreign_key_checks=1";
@@ -327,6 +341,42 @@ sub produce {
     return wantarray ? ($create ? $create : (), @create, @table_defs) : ($create . join('', map { $_ ? "$_;\n\n" : () } (@create, @table_defs)));
 }
 
+sub create_trigger {
+    my ($trigger, $options) = @_;
+    my $qt = $options->{quote_table_names} || '';
+    my $qf = $options->{quote_field_names} || '';
+
+    my $trigger_name = $trigger->name;
+    debug("PKG: Looking at trigger '${trigger_name}'\n");
+
+    my @statements;
+
+    my $events = $trigger->database_events;
+    for my $event ( @$events ) {
+        my $name = $trigger_name;
+        if (@$events > 1) {
+            $name .= "_$event";
+
+            warn "Multiple database events supplied for trigger '${trigger_name}', ",
+                "creating trigger '${name}'  for the '${event}' event\n"
+                    if $options->{show_warnings};
+        }
+
+        my $action = $trigger->action;
+        $action .= ";" unless $action =~ /;\s*\z/;
+
+        push @statements, "DROP TRIGGER IF EXISTS ${qt}${name}${qt}" if $options->{add_drop_trigger};
+        push @statements, sprintf(
+            "CREATE TRIGGER ${qt}%s${qt} %s %s ON ${qt}%s${qt}\n  FOR EACH ROW BEGIN %s END",
+            $name, $trigger->perform_action_when, $event, $trigger->on_table, $action,
+        );
+
+    }
+    # Tack the comment onto the first statement
+    $statements[0] = "--\n-- Trigger ${qt}${trigger_name}${qt}\n--\n" . $statements[0] unless $options->{no_comments};
+    return @statements;
+}
+
 sub create_view {
     my ($view, $options) = @_;
     my $qt = $options->{quote_table_names} || '';