1 package SQL::Translator::Schema::Trigger;
3 # ----------------------------------------------------------------------
4 # $Id: Trigger.pm,v 1.1 2003-10-03 23:56:41 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
43 C<SQL::Translator::Schema::Trigger> is the trigger object.
51 use SQL::Translator::Utils 'parse_list_arg';
53 use base 'Class::Base';
54 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
56 $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
58 # ----------------------------------------------------------------------
67 my $schema = SQL::Translator::Schema::Trigger->new;
71 my ( $self, $config ) = @_;
74 qw[ name perform_action_when database_event fields on_table action ]
76 next unless $config->{ $arg };
77 $self->$arg( $config->{ $arg } );# or return;
83 # ----------------------------------------------------------------------
84 sub perform_action_when {
88 =head2 perform_action_when
90 Gets or sets whether the event happens "before" or "after" the
93 $trigger->perform_action_when('after');
99 if ( my $arg = shift ) {
102 if ( $arg =~ m/^(before|after)$/i ) {
103 $self->{'perform_action_when'} = $arg;
107 $self->error("Invalid argument '$arg' to perform_action_when");
111 return $self->{'perform_action_when'};
114 # ----------------------------------------------------------------------
119 =head2 database_event
121 Gets or sets the event that triggers the trigger.
123 my $ok = $trigger->database_event('insert');
129 if ( my $arg = shift ) {
132 if ( $arg =~ /^(insert|update|update_on|delete)$/ ) {
133 $self->{'database_event'} = $arg;
137 $self->error("Invalid argument '$arg' to database_event");
141 return $self->{'database_event'};
144 # ----------------------------------------------------------------------
151 Gets and set which fields to monitor for C<database_event>.
154 $view->fields('id', 'name');
155 $view->fields( 'id, name' );
156 $view->fields( [ 'id', 'name' ] );
157 $view->fields( qw[ id name ] );
159 my @fields = $view->fields;
164 my $fields = parse_list_arg( @_ );
167 my ( %unique, @unique );
168 for my $f ( @$fields ) {
169 next if $unique{ $f };
174 $self->{'fields'} = \@unique;
177 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
180 # ----------------------------------------------------------------------
187 Gets or set the table name on which the trigger works.
189 $trigger->table('foo');
194 my $arg = shift || '';
195 $self->{'on_table'} = $arg if $arg;
196 return $self->{'on_table'};
199 # ----------------------------------------------------------------------
206 Gets or set the actions of the trigger.
220 my $arg = shift || '';
221 $self->{'action'} = $arg if $arg;
222 return $self->{'action'};
225 # ----------------------------------------------------------------------
232 Determine whether the trigger is valid or not.
234 my $ok = $trigger->is_valid;
241 qw[ name perform_action_when database_event on_table action ]
243 return $self->error("No $attr") unless $self->$attr();
246 return $self->error("Missing fields for UPDATE ON") if
247 $self->database_event eq 'update_on' && !$self->fields;
252 # ----------------------------------------------------------------------
259 Get or set the trigger's name.
261 my $name = $trigger->name('foo');
266 $self->{'name'} = shift if @_;
267 return $self->{'name'} || '';
270 # ----------------------------------------------------------------------
277 Get or set the trigger's order.
279 my $order = $trigger->order(3);
283 my ( $self, $arg ) = @_;
285 if ( defined $arg && $arg =~ /^\d+$/ ) {
286 $self->{'order'} = $arg;
289 return $self->{'order'} || 0;
294 # ----------------------------------------------------------------------
300 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.