Patch from rbo to support multiple database events per trigger
Jess Robinson [Tue, 21 Oct 2008 13:09:31 +0000 (13:09 +0000)]
Changes
lib/SQL/Translator/Schema/Trigger.pm
t/13schema.t

diff --git a/Changes b/Changes
index 188832a..ecade0b 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,8 @@
 # ----------------------------------------------------------
 # 
 # ----------------------------------------------------------
+* Applied patch from rbo to add support of multiple database events on a trigger
+* Applied patch from lukes to allow drop if exists in sqlite producer, with version >= 3.3
 * Applied patch from rjbs with minor changes, now we support scalar refs in default values!
 * Fixed SQLite producer to end index statements in newlines, in scalar context
 * Decreed that all list context statements shall not end in ; or ;\n
index 8aa4171..8df2c8e 100644 (file)
@@ -32,7 +32,8 @@ SQL::Translator::Schema::Trigger - SQL::Translator trigger object
   my $trigger = SQL::Translator::Schema::Trigger->new(
       name                => 'foo',
       perform_action_when => 'before', # or after
-      database_event      => 'insert', # or update, update_on, delete
+      database_event      => 'insert', # Obsolete please use database_events!
+         database_events     => [qw/update instert/], # or update, update_on, delete
       fields              => [],       # fields if event is "update"
       on_table            => 'foo',    # table name
       action              => '...',    # text of trigger
@@ -59,7 +60,7 @@ $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
 # ----------------------------------------------------------------------
 
 __PACKAGE__->_attributes( qw/
-    name schema perform_action_when database_event fields table on_table action
+    name schema perform_action_when database_event database_events fields table on_table action
     order
 /);
 
@@ -111,27 +112,53 @@ sub database_event {
 
 =head2 database_event
 
-Gets or sets the event that triggers the trigger.
-
-  my $ok = $trigger->database_event('insert');
+Obosolete please use database_events!
 
 =cut
+    
+       my $self = shift;
+       
+       
+       if ( my $arg = shift ) {
+               $self->database_events( [$arg] );
+       }
+
+       return $self->error("Please use database_events the trigger has more then one events!") if (scalar @{$self->{'database_events'}} > 1);
+       
+    warn 'database_event is obsolete please use database_events';
+       return (@{ $self->{'database_events'} })[0];
+}
+# ----------------------------------------------------------------------
+sub database_events {
 
-    my $self = shift;
+=pod
 
-    if ( my $arg = shift ) {
-        $arg =  lc $arg;
-        $arg =~ s/\s+/ /g;
-        if ( $arg =~ /^(insert|update|update_on|delete)$/ ) {
-            $self->{'database_event'} = $arg;
-        }
-        else {
-            return 
-                $self->error("Invalid argument '$arg' to database_event");
-        }
-    }
+=head2 database_events
 
-    return $self->{'database_event'};
+Gets or sets the events that triggers the trigger.
+
+  my $ok = $trigger->database_event('insert');
+
+=cut
+
+       my $self = shift;
+       
+       if ( my $arg = shift ) {
+               if (ref $arg eq "ARRAY"){
+                       map  {
+                               $_ = lc; 
+                               $_ =~ s/\s+/ /g;  
+                               return $self->error("Invalid event '$_' in database_events") unless ( $_ =~ /^(insert|update|update_on|delete)$/ );
+                       } @$arg ;
+                       @{ $self->{'database_events'} } = @$arg;
+                       
+               }else{
+                       return $self->error("Invalid argument to database_events");
+               };
+               
+       }
+
+       return $self->{'database_events'};
 }
 
 # ----------------------------------------------------------------------
@@ -332,6 +359,30 @@ Get or set the trigger's schema object.
 }
 
 # ----------------------------------------------------------------------
+sub compare_arrays {
+
+=pod
+
+=head2 compare_arrays
+
+Compare two arrays.
+
+=cut
+       
+       my ($first, $second) = @_;
+       no warnings;  # silence spurious -w undef complaints
+
+       return 0 unless (ref $first eq 'ARRAY' and ref $second eq 'ARRAY' ) ;
+       return 0 unless @$first == @$second;
+       my @first = sort @$first;
+       my @second = sort @$second;
+       for (my $i = 0; $i < scalar @first; $i++) {
+               return 0 if @first[$i] ne @second[$i];
+               }
+       return 1;
+}
+
+# ----------------------------------------------------------------------
 sub equals {
 
 =pod
@@ -352,7 +403,7 @@ Determines if this trigger is the same as another
     return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
     #return 0 unless $self->is_valid eq $other->is_valid;
     return 0 unless $self->perform_action_when eq $other->perform_action_when;
-    return 0 unless $self->database_event eq $other->database_event;
+    return 0 unless compare_arrays($self->database_events,$other->database_events) ;
     return 0 unless $self->on_table eq $other->on_table;
     return 0 unless $self->action eq $other->action;
     return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
index e72da02..8a23b44 100644 (file)
@@ -4,7 +4,7 @@
 $| = 1;
 
 use strict;
-use Test::More tests => 229;
+use Test::More tests => 238;
 use SQL::Translator::Schema::Constants;
 
 require_ok( 'SQL::Translator' );
@@ -613,6 +613,38 @@ require_ok( 'SQL::Translator::Schema' );
     isa_ok( $t1, 'SQL::Translator::Schema::Trigger', 'Trigger' );
     is( $t1->name, $name, qq[Name is "$name"] );
 
+
+
+       my $s2                   = SQL::Translator::Schema->new(name => 'TrigTest2');
+    $s2->add_table(name=>'foo') or die "Couldn't create table: ", $s2->error;
+    my $t2                   = $s2->add_trigger(
+        name                => 'foo_trigger',
+        perform_action_when => 'after',
+        database_events     => [qw/insert update/],
+        on_table            => 'foo',
+        action              => 'update modified=timestamp();',
+    ) or die $s2->error;
+       isa_ok( $t2, 'SQL::Translator::Schema::Trigger', 'Trigger' );
+    isa_ok( $t2->schema, 'SQL::Translator::Schema', 'Schema' );
+    is( $t2->schema->name, 'TrigTest2', qq[Schema name is "'TrigTest2'"] );
+    is( $t2->name, 'foo_trigger', qq[Name is "foo_trigger"] );
+       is_deeply($t2->database_events,[qw/insert update/],"Database events are [qw/insert update/] ");
+       isa_ok($t2->database_events,'ARRAY','Database events');
+       
+       #
+       # Trigger equal tests
+       #
+       isnt($t1->equals($t2),1,'Compare two Triggers with database_event and database_events');
+       
+       $t2->database_events([]);
+       $t2->database_event($database_event);
+       is($t1->equals($t2),1,'Compare two Triggers with database_event');
+
+       $t2->database_event('');
+       $t1->database_events([qw/update insert/]);
+       $t2->database_events([qw/insert update/]);
+       is($t1->equals($t2),1,'Compare two Triggers with database_events');
+       
     #
     # $schema-> drop_trigger
     #