1 package SQL::Translator::Schema::Trigger;
3 # ----------------------------------------------------------------------
4 # $Id: Trigger.pm,v 1.2 2003-10-08 17:33:47 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
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.
52 use SQL::Translator::Utils 'parse_list_arg';
54 use base 'Class::Base';
55 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
57 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
59 # ----------------------------------------------------------------------
68 my $schema = SQL::Translator::Schema::Trigger->new;
72 my ( $self, $config ) = @_;
76 name perform_action_when database_event fields
77 on_table action schema
80 next unless $config->{ $arg };
81 $self->$arg( $config->{ $arg } );# or return;
87 # ----------------------------------------------------------------------
88 sub perform_action_when {
92 =head2 perform_action_when
94 Gets or sets whether the event happens "before" or "after" the
97 $trigger->perform_action_when('after');
103 if ( my $arg = shift ) {
106 if ( $arg =~ m/^(before|after)$/i ) {
107 $self->{'perform_action_when'} = $arg;
111 $self->error("Invalid argument '$arg' to perform_action_when");
115 return $self->{'perform_action_when'};
118 # ----------------------------------------------------------------------
123 =head2 database_event
125 Gets or sets the event that triggers the trigger.
127 my $ok = $trigger->database_event('insert');
133 if ( my $arg = shift ) {
136 if ( $arg =~ /^(insert|update|update_on|delete)$/ ) {
137 $self->{'database_event'} = $arg;
141 $self->error("Invalid argument '$arg' to database_event");
145 return $self->{'database_event'};
148 # ----------------------------------------------------------------------
155 Gets and set which fields to monitor for C<database_event>.
158 $view->fields('id', 'name');
159 $view->fields( 'id, name' );
160 $view->fields( [ 'id', 'name' ] );
161 $view->fields( qw[ id name ] );
163 my @fields = $view->fields;
168 my $fields = parse_list_arg( @_ );
171 my ( %unique, @unique );
172 for my $f ( @$fields ) {
173 next if $unique{ $f };
178 $self->{'fields'} = \@unique;
181 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
184 # ----------------------------------------------------------------------
191 Gets or set the table name on which the trigger works.
193 $trigger->table('foo');
198 my $arg = shift || '';
199 $self->{'on_table'} = $arg if $arg;
200 return $self->{'on_table'};
203 # ----------------------------------------------------------------------
210 Gets or set the actions of the trigger.
224 my $arg = shift || '';
225 $self->{'action'} = $arg if $arg;
226 return $self->{'action'};
229 # ----------------------------------------------------------------------
236 Determine whether the trigger is valid or not.
238 my $ok = $trigger->is_valid;
245 qw[ name perform_action_when database_event on_table action ]
247 return $self->error("No $attr") unless $self->$attr();
250 return $self->error("Missing fields for UPDATE ON") if
251 $self->database_event eq 'update_on' && !$self->fields;
256 # ----------------------------------------------------------------------
263 Get or set the trigger's name.
265 my $name = $trigger->name('foo');
270 $self->{'name'} = shift if @_;
271 return $self->{'name'} || '';
274 # ----------------------------------------------------------------------
281 Get or set the trigger's order.
283 my $order = $trigger->order(3);
287 my ( $self, $arg ) = @_;
289 if ( defined $arg && $arg =~ /^\d+$/ ) {
290 $self->{'order'} = $arg;
293 return $self->{'order'} || 0;
296 # ----------------------------------------------------------------------
303 Get or set the trigger's schema object.
305 $trigger->schema( $schema );
306 my $schema = $trigger->schema;
311 if ( my $arg = shift ) {
312 return $self->error('Not a schema object') unless
313 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
314 $self->{'schema'} = $arg;
317 return $self->{'schema'};
320 # ----------------------------------------------------------------------
323 undef $self->{'schema'}; # destroy cyclical reference
328 # ----------------------------------------------------------------------
334 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.