1 package SQL::Translator::Schema::Trigger;
3 # ----------------------------------------------------------------------
4 # $Id: Trigger.pm,v 1.9 2006-06-07 16:37:33 schiffbruechige Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
27 SQL::Translator::Schema::Trigger - SQL::Translator trigger object
31 use SQL::Translator::Schema::Trigger;
32 my $trigger = SQL::Translator::Schema::Trigger->new(
34 perform_action_when => 'before', # or after
35 database_event => 'insert', # or update, update_on, delete
36 fields => [], # fields if event is "update"
37 on_table => 'foo', # table name
38 action => '...', # text of trigger
39 schema => $schema, # Schema object
44 C<SQL::Translator::Schema::Trigger> is the trigger object.
51 use SQL::Translator::Utils 'parse_list_arg';
53 use base 'SQL::Translator::Schema::Object';
55 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
57 $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
59 # ----------------------------------------------------------------------
61 __PACKAGE__->_attributes( qw/
62 name schema perform_action_when database_event fields table on_table action
72 my $schema = SQL::Translator::Schema::Trigger->new;
76 # ----------------------------------------------------------------------
77 sub perform_action_when {
81 =head2 perform_action_when
83 Gets or sets whether the event happens "before" or "after" the
86 $trigger->perform_action_when('after');
92 if ( my $arg = shift ) {
95 if ( $arg =~ m/^(before|after)$/i ) {
96 $self->{'perform_action_when'} = $arg;
100 $self->error("Invalid argument '$arg' to perform_action_when");
104 return $self->{'perform_action_when'};
107 # ----------------------------------------------------------------------
112 =head2 database_event
114 Gets or sets the event that triggers the trigger.
116 my $ok = $trigger->database_event('insert');
122 if ( my $arg = shift ) {
125 if ( $arg =~ /^(insert|update|update_on|delete)$/ ) {
126 $self->{'database_event'} = $arg;
130 $self->error("Invalid argument '$arg' to database_event");
134 return $self->{'database_event'};
137 # ----------------------------------------------------------------------
144 Gets and set which fields to monitor for C<database_event>.
147 $view->fields('id', 'name');
148 $view->fields( 'id, name' );
149 $view->fields( [ 'id', 'name' ] );
150 $view->fields( qw[ id name ] );
152 my @fields = $view->fields;
157 my $fields = parse_list_arg( @_ );
160 my ( %unique, @unique );
161 for my $f ( @$fields ) {
162 next if $unique{ $f };
167 $self->{'fields'} = \@unique;
170 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
173 # ----------------------------------------------------------------------
180 Gets or set the table on which the trigger works, as a L<SQL::Translator::Schema::Table> object.
181 $trigger->table($triggered_table);
185 my ($self, $arg) = @_;
187 $self->error("Table attribute of a ".__PACKAGE__.
188 " must be a SQL::Translator::Schema::Table")
189 unless ref $arg and $arg->isa('SQL::Translator::Schema::Table');
190 $self->{table} = $arg;
192 return $self->{table};
195 # ----------------------------------------------------------------------
202 Gets or set the table name on which the trigger works, as a string.
203 $trigger->on_table('foo');
207 my ($self, $arg) = @_;
209 my $table = $self->schema->get_table($arg);
210 die "Table named $arg doesn't exist"
212 $self->table($table);
214 return $self->table->name;
217 # ----------------------------------------------------------------------
224 Gets or set the actions of the trigger.
238 my $arg = shift || '';
239 $self->{'action'} = $arg if $arg;
240 return $self->{'action'};
243 # ----------------------------------------------------------------------
250 Determine whether the trigger is valid or not.
252 my $ok = $trigger->is_valid;
259 qw[ name perform_action_when database_event on_table action ]
261 return $self->error("No $attr") unless $self->$attr();
264 return $self->error("Missing fields for UPDATE ON") if
265 $self->database_event eq 'update_on' && !$self->fields;
270 # ----------------------------------------------------------------------
277 Get or set the trigger's name.
279 my $name = $trigger->name('foo');
284 $self->{'name'} = shift if @_;
285 return $self->{'name'} || '';
288 # ----------------------------------------------------------------------
295 Get or set the trigger's order.
297 my $order = $trigger->order(3);
301 my ( $self, $arg ) = @_;
303 if ( defined $arg && $arg =~ /^\d+$/ ) {
304 $self->{'order'} = $arg;
307 return $self->{'order'} || 0;
310 # ----------------------------------------------------------------------
317 Get or set the trigger's schema object.
319 $trigger->schema( $schema );
320 my $schema = $trigger->schema;
325 if ( my $arg = shift ) {
326 return $self->error('Not a schema object') unless
327 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
328 $self->{'schema'} = $arg;
331 return $self->{'schema'};
334 # ----------------------------------------------------------------------
341 Determines if this trigger is the same as another
343 my $isIdentical = $trigger1->equals( $trigger2 );
349 my $case_insensitive = shift;
351 return 0 unless $self->SUPER::equals($other);
352 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
353 #return 0 unless $self->is_valid eq $other->is_valid;
354 return 0 unless $self->perform_action_when eq $other->perform_action_when;
355 return 0 unless $self->database_event eq $other->database_event;
356 return 0 unless $self->on_table eq $other->on_table;
357 return 0 unless $self->action eq $other->action;
358 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
362 # ----------------------------------------------------------------------
365 undef $self->{'schema'}; # destroy cyclical reference
370 # ----------------------------------------------------------------------
376 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.