Downgrade global version - highest version in 9002 on cpan is 1.58 - thus go with...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Trigger.pm
index e3b0cd3..e377d50 100644 (file)
@@ -1,9 +1,7 @@
 package SQL::Translator::Schema::Trigger;
 
 # ----------------------------------------------------------------------
-# $Id: Trigger.pm,v 1.5 2004-11-05 13:19:31 grommit Exp $
-# ----------------------------------------------------------------------
-# Copyright (C) 2002-4 SQLFairy Authors
+# 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
@@ -32,7 +30,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
@@ -52,14 +51,16 @@ use SQL::Translator::Utils 'parse_list_arg';
 
 use base 'SQL::Translator::Schema::Object';
 
+use Carp;
+
 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
 
-$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/;
+$VERSION = '1.59';
 
 # ----------------------------------------------------------------------
 
 __PACKAGE__->_attributes( qw/
-    name perform_action_when database_event fields on_table action schema
+    name schema perform_action_when database_event database_events fields table on_table action
     order
 /);
 
@@ -111,27 +112,54 @@ 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);
+
+  carp '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
+
+Gets or sets the events that triggers the trigger.
 
-    return $self->{'database_event'};
+  my $ok = $trigger->database_events('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'};
 }
 
 # ----------------------------------------------------------------------
@@ -171,22 +199,47 @@ Gets and set which fields to monitor for C<database_event>.
 }
 
 # ----------------------------------------------------------------------
+sub table {
+
+=pod
+
+=head2 table
+
+Gets or set the table on which the trigger works, as a L<SQL::Translator::Schema::Table> object.
+  $trigger->table($triggered_table);
+
+=cut
+
+    my ($self, $arg) = @_;
+    if ( @_ == 2 ) {
+        $self->error("Table attribute of a ".__PACKAGE__.
+                     " must be a SQL::Translator::Schema::Table") 
+            unless ref $arg and $arg->isa('SQL::Translator::Schema::Table');
+        $self->{table} = $arg;
+    }
+    return $self->{table};
+}
+
+# ----------------------------------------------------------------------
 sub on_table {
 
 =pod
 
 =head2 on_table
 
-Gets or set the table name on which the trigger works.
-
-  $trigger->table('foo');
+Gets or set the table name on which the trigger works, as a string.
+  $trigger->on_table('foo');
 
 =cut
 
-    my $self = shift;
-    my $arg  = shift || '';
-    $self->{'on_table'} = $arg if $arg;
-    return $self->{'on_table'};
+    my ($self, $arg) = @_;
+    if ( @_ == 2 ) {
+        my $table = $self->schema->get_table($arg);
+        die "Table named $arg doesn't exist"
+            if !$table;
+        $self->table($table);
+    }
+    return $self->table->name;
 }
 
 # ----------------------------------------------------------------------
@@ -307,6 +360,58 @@ 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
+
+=head2 equals
+
+Determines if this trigger is the same as another
+
+  my $isIdentical = $trigger1->equals( $trigger2 );
+
+=cut
+
+    my $self = shift;
+    my $other = shift;
+    my $case_insensitive = shift;
+    
+    return 0 unless $self->SUPER::equals($other);
+    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 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);
+    return 1;
+}
+
+# ----------------------------------------------------------------------
 sub DESTROY {
     my $self = shift;
     undef $self->{'schema'}; # destroy cyclical reference