1 package SQL::Translator::Schema::Trigger;
3 # ----------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
25 SQL::Translator::Schema::Trigger - SQL::Translator trigger object
29 use SQL::Translator::Schema::Trigger;
30 my $trigger = SQL::Translator::Schema::Trigger->new(
32 perform_action_when => 'before', # or after
33 database_event => 'insert', # Obsolete please use database_events!
34 database_events => [qw/update instert/], # or update, update_on, delete
35 fields => [], # fields if event is "update"
36 on_table => 'foo', # table name
37 action => '...', # text of trigger
38 schema => $schema, # Schema object
43 C<SQL::Translator::Schema::Trigger> is the trigger object.
50 use SQL::Translator::Utils 'parse_list_arg';
52 use base 'SQL::Translator::Schema::Object';
56 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
60 # ----------------------------------------------------------------------
62 __PACKAGE__->_attributes( qw/
63 name schema perform_action_when database_event database_events fields table on_table action
73 my $schema = SQL::Translator::Schema::Trigger->new;
77 # ----------------------------------------------------------------------
78 sub perform_action_when {
82 =head2 perform_action_when
84 Gets or sets whether the event happens "before" or "after" the
87 $trigger->perform_action_when('after');
93 if ( my $arg = shift ) {
96 if ( $arg =~ m/^(before|after)$/i ) {
97 $self->{'perform_action_when'} = $arg;
101 $self->error("Invalid argument '$arg' to perform_action_when");
105 return $self->{'perform_action_when'};
108 # ----------------------------------------------------------------------
113 =head2 database_event
115 Obosolete please use database_events!
122 if ( my $arg = shift ) {
123 $self->database_events( [$arg] );
126 return $self->error("Please use database_events the trigger has more then one events!")
127 if (scalar @{$self->{'database_events'}} > 1);
129 carp 'database_event is obsolete please use database_events';
130 return (@{ $self->{'database_events'} })[0];
132 # ----------------------------------------------------------------------
133 sub database_events {
137 =head2 database_events
139 Gets or sets the events that triggers the trigger.
141 my $ok = $trigger->database_events('insert');
147 if ( my $arg = shift ) {
148 if (ref $arg eq "ARRAY"){
152 return $self->error("Invalid event '$_' in database_events") unless ( $_ =~ /^(insert|update|update_on|delete)$/ );
154 @{ $self->{'database_events'} } = @$arg;
157 return $self->error("Invalid argument to database_events");
162 return $self->{'database_events'};
165 # ----------------------------------------------------------------------
172 Gets and set which fields to monitor for C<database_event>.
175 $view->fields('id', 'name');
176 $view->fields( 'id, name' );
177 $view->fields( [ 'id', 'name' ] );
178 $view->fields( qw[ id name ] );
180 my @fields = $view->fields;
185 my $fields = parse_list_arg( @_ );
188 my ( %unique, @unique );
189 for my $f ( @$fields ) {
190 next if $unique{ $f };
195 $self->{'fields'} = \@unique;
198 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
201 # ----------------------------------------------------------------------
208 Gets or set the table on which the trigger works, as a L<SQL::Translator::Schema::Table> object.
209 $trigger->table($triggered_table);
213 my ($self, $arg) = @_;
215 $self->error("Table attribute of a ".__PACKAGE__.
216 " must be a SQL::Translator::Schema::Table")
217 unless ref $arg and $arg->isa('SQL::Translator::Schema::Table');
218 $self->{table} = $arg;
220 return $self->{table};
223 # ----------------------------------------------------------------------
230 Gets or set the table name on which the trigger works, as a string.
231 $trigger->on_table('foo');
235 my ($self, $arg) = @_;
237 my $table = $self->schema->get_table($arg);
238 die "Table named $arg doesn't exist"
240 $self->table($table);
242 return $self->table->name;
245 # ----------------------------------------------------------------------
252 Gets or set the actions of the trigger.
266 my $arg = shift || '';
267 $self->{'action'} = $arg if $arg;
268 return $self->{'action'};
271 # ----------------------------------------------------------------------
278 Determine whether the trigger is valid or not.
280 my $ok = $trigger->is_valid;
287 qw[ name perform_action_when database_event on_table action ]
289 return $self->error("No $attr") unless $self->$attr();
292 return $self->error("Missing fields for UPDATE ON") if
293 $self->database_event eq 'update_on' && !$self->fields;
298 # ----------------------------------------------------------------------
305 Get or set the trigger's name.
307 my $name = $trigger->name('foo');
312 $self->{'name'} = shift if @_;
313 return $self->{'name'} || '';
316 # ----------------------------------------------------------------------
323 Get or set the trigger's order.
325 my $order = $trigger->order(3);
329 my ( $self, $arg ) = @_;
331 if ( defined $arg && $arg =~ /^\d+$/ ) {
332 $self->{'order'} = $arg;
335 return $self->{'order'} || 0;
338 # ----------------------------------------------------------------------
345 Get or set the trigger's schema object.
347 $trigger->schema( $schema );
348 my $schema = $trigger->schema;
353 if ( my $arg = shift ) {
354 return $self->error('Not a schema object') unless
355 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
356 $self->{'schema'} = $arg;
359 return $self->{'schema'};
362 # ----------------------------------------------------------------------
367 =head2 compare_arrays
373 my ($first, $second) = @_;
374 no warnings; # silence spurious -w undef complaints
376 return 0 unless (ref $first eq 'ARRAY' and ref $second eq 'ARRAY' ) ;
377 return 0 unless @$first == @$second;
378 my @first = sort @$first;
379 my @second = sort @$second;
380 for (my $i = 0; $i < scalar @first; $i++) {
381 return 0 if @first[$i] ne @second[$i];
386 # ----------------------------------------------------------------------
393 Determines if this trigger is the same as another
395 my $isIdentical = $trigger1->equals( $trigger2 );
401 my $case_insensitive = shift;
403 return 0 unless $self->SUPER::equals($other);
404 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
405 #return 0 unless $self->is_valid eq $other->is_valid;
406 return 0 unless $self->perform_action_when eq $other->perform_action_when;
407 return 0 unless compare_arrays($self->database_events,$other->database_events) ;
408 return 0 unless $self->on_table eq $other->on_table;
409 return 0 unless $self->action eq $other->action;
410 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
414 # ----------------------------------------------------------------------
417 undef $self->{'schema'}; # destroy cyclical reference
422 # ----------------------------------------------------------------------
428 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.