1 package SQL::Translator::Schema::Trigger;
3 # ----------------------------------------------------------------------
4 # $Id: Trigger.pm,v 1.6 2005-06-27 21:59:20 duality72 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.6 $ =~ /(\d+)\.(\d+)/;
59 # ----------------------------------------------------------------------
61 __PACKAGE__->_attributes( qw/
62 name perform_action_when database_event fields on_table action schema
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 name on which the trigger works.
182 $trigger->table('foo');
187 my $arg = shift || '';
188 $self->{'on_table'} = $arg if $arg;
189 return $self->{'on_table'};
192 # ----------------------------------------------------------------------
199 Gets or set the actions of the trigger.
213 my $arg = shift || '';
214 $self->{'action'} = $arg if $arg;
215 return $self->{'action'};
218 # ----------------------------------------------------------------------
225 Determine whether the trigger is valid or not.
227 my $ok = $trigger->is_valid;
234 qw[ name perform_action_when database_event on_table action ]
236 return $self->error("No $attr") unless $self->$attr();
239 return $self->error("Missing fields for UPDATE ON") if
240 $self->database_event eq 'update_on' && !$self->fields;
245 # ----------------------------------------------------------------------
252 Get or set the trigger's name.
254 my $name = $trigger->name('foo');
259 $self->{'name'} = shift if @_;
260 return $self->{'name'} || '';
263 # ----------------------------------------------------------------------
270 Get or set the trigger's order.
272 my $order = $trigger->order(3);
276 my ( $self, $arg ) = @_;
278 if ( defined $arg && $arg =~ /^\d+$/ ) {
279 $self->{'order'} = $arg;
282 return $self->{'order'} || 0;
285 # ----------------------------------------------------------------------
292 Get or set the trigger's schema object.
294 $trigger->schema( $schema );
295 my $schema = $trigger->schema;
300 if ( my $arg = shift ) {
301 return $self->error('Not a schema object') unless
302 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
303 $self->{'schema'} = $arg;
306 return $self->{'schema'};
309 # ----------------------------------------------------------------------
316 Determines if this trigger is the same as another
318 my $isIdentical = $trigger1->equals( $trigger2 );
325 return 0 unless $self->SUPER::equals($other);
326 return 0 unless $self->name eq $other->name;
327 return 0 unless $self->is_valid eq $other->is_valid;
328 return 0 unless $self->perform_action_when eq $other->perform_action_when;
329 return 0 unless $self->database_event eq $other->database_event;
330 return 0 unless $self->on_table eq $other->on_table;
331 return 0 unless $self->action eq $other->action;
332 return 0 unless $self->_compare_objects($self->extra, $other->extra);
336 # ----------------------------------------------------------------------
339 undef $self->{'schema'}; # destroy cyclical reference
344 # ----------------------------------------------------------------------
350 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.