Reduce $Id to its normal form
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Trigger.pm
CommitLineData
e2cf647c 1package SQL::Translator::Schema::Trigger;
2
3# ----------------------------------------------------------------------
782b5a43 4# $Id$
e2cf647c 5# ----------------------------------------------------------------------
478f608d 6# Copyright (C) 2002-2009 SQLFairy Authors
e2cf647c 7#
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.
11#
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.
16#
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
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=pod
24
25=head1 NAME
26
27SQL::Translator::Schema::Trigger - SQL::Translator trigger object
28
29=head1 SYNOPSIS
30
31 use SQL::Translator::Schema::Trigger;
32 my $trigger = SQL::Translator::Schema::Trigger->new(
33 name => 'foo',
34 perform_action_when => 'before', # or after
8742e408 35 database_event => 'insert', # Obsolete please use database_events!
36 database_events => [qw/update instert/], # or update, update_on, delete
e2cf647c 37 fields => [], # fields if event is "update"
38 on_table => 'foo', # table name
39 action => '...', # text of trigger
39ad1787 40 schema => $schema, # Schema object
e2cf647c 41 );
42
43=head1 DESCRIPTION
44
45C<SQL::Translator::Schema::Trigger> is the trigger object.
46
47=head1 METHODS
48
49=cut
50
51use strict;
e2cf647c 52use SQL::Translator::Utils 'parse_list_arg';
53
b6a880d1 54use base 'SQL::Translator::Schema::Object';
55
d0fcb05d 56use Carp;
57
da06ac74 58use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
59
60$VERSION = '1.99';
e2cf647c 61
62# ----------------------------------------------------------------------
9371be50 63
64__PACKAGE__->_attributes( qw/
8742e408 65 name schema perform_action_when database_event database_events fields table on_table action
9371be50 66 order
67/);
e2cf647c 68
69=pod
70
71=head2 new
72
73Object constructor.
74
75 my $schema = SQL::Translator::Schema::Trigger->new;
76
77=cut
78
e2cf647c 79# ----------------------------------------------------------------------
80sub perform_action_when {
81
82=pod
83
84=head2 perform_action_when
85
86Gets or sets whether the event happens "before" or "after" the
87C<database_event>.
88
89 $trigger->perform_action_when('after');
90
91=cut
92
93 my $self = shift;
94
95 if ( my $arg = shift ) {
96 $arg = lc $arg;
97 $arg =~ s/\s+/ /g;
98 if ( $arg =~ m/^(before|after)$/i ) {
99 $self->{'perform_action_when'} = $arg;
100 }
101 else {
102 return
103 $self->error("Invalid argument '$arg' to perform_action_when");
104 }
105 }
106
107 return $self->{'perform_action_when'};
108}
109
110# ----------------------------------------------------------------------
111sub database_event {
112
113=pod
114
115=head2 database_event
116
8742e408 117Obosolete please use database_events!
e2cf647c 118
119=cut
8742e408 120
121 my $self = shift;
122
123
124 if ( my $arg = shift ) {
125 $self->database_events( [$arg] );
126 }
127
d0fcb05d 128 return $self->error("Please use database_events the trigger has more then one events!")
129 if (scalar @{$self->{'database_events'}} > 1);
130
131 carp 'database_event is obsolete please use database_events';
8742e408 132 return (@{ $self->{'database_events'} })[0];
133}
134# ----------------------------------------------------------------------
135sub database_events {
e2cf647c 136
8742e408 137=pod
e2cf647c 138
8742e408 139=head2 database_events
e2cf647c 140
8742e408 141Gets or sets the events that triggers the trigger.
142
d0fcb05d 143 my $ok = $trigger->database_events('insert');
8742e408 144
145=cut
146
147 my $self = shift;
148
149 if ( my $arg = shift ) {
150 if (ref $arg eq "ARRAY"){
151 map {
152 $_ = lc;
153 $_ =~ s/\s+/ /g;
154 return $self->error("Invalid event '$_' in database_events") unless ( $_ =~ /^(insert|update|update_on|delete)$/ );
155 } @$arg ;
156 @{ $self->{'database_events'} } = @$arg;
157
158 }else{
159 return $self->error("Invalid argument to database_events");
160 };
161
162 }
163
164 return $self->{'database_events'};
e2cf647c 165}
166
167# ----------------------------------------------------------------------
168sub fields {
169
170=pod
171
172=head2 fields
173
174Gets and set which fields to monitor for C<database_event>.
175
176 $view->fields('id');
177 $view->fields('id', 'name');
178 $view->fields( 'id, name' );
179 $view->fields( [ 'id', 'name' ] );
180 $view->fields( qw[ id name ] );
181
182 my @fields = $view->fields;
183
184=cut
185
186 my $self = shift;
187 my $fields = parse_list_arg( @_ );
188
189 if ( @$fields ) {
190 my ( %unique, @unique );
191 for my $f ( @$fields ) {
192 next if $unique{ $f };
193 $unique{ $f } = 1;
194 push @unique, $f;
195 }
196
197 $self->{'fields'} = \@unique;
198 }
199
200 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
201}
202
203# ----------------------------------------------------------------------
8ce5d615 204sub table {
205
206=pod
207
208=head2 table
209
210Gets or set the table on which the trigger works, as a L<SQL::Translator::Schema::Table> object.
211 $trigger->table($triggered_table);
212
213=cut
214
215 my ($self, $arg) = @_;
216 if ( @_ == 2 ) {
217 $self->error("Table attribute of a ".__PACKAGE__.
218 " must be a SQL::Translator::Schema::Table")
219 unless ref $arg and $arg->isa('SQL::Translator::Schema::Table');
220 $self->{table} = $arg;
221 }
222 return $self->{table};
223}
224
225# ----------------------------------------------------------------------
e2cf647c 226sub on_table {
227
228=pod
229
230=head2 on_table
231
8ce5d615 232Gets or set the table name on which the trigger works, as a string.
233 $trigger->on_table('foo');
e2cf647c 234
235=cut
236
8ce5d615 237 my ($self, $arg) = @_;
238 if ( @_ == 2 ) {
239 my $table = $self->schema->get_table($arg);
240 die "Table named $arg doesn't exist"
241 if !$table;
242 $self->table($table);
243 }
244 return $self->table->name;
e2cf647c 245}
246
247# ----------------------------------------------------------------------
248sub action {
249
250=pod
251
252=head2 action
253
254Gets or set the actions of the trigger.
255
256 $trigger->actions(
257 q[
258 BEGIN
259 select ...;
260 update ...;
261 END
262 ]
263 );
264
265=cut
266
267 my $self = shift;
268 my $arg = shift || '';
269 $self->{'action'} = $arg if $arg;
270 return $self->{'action'};
271}
272
273# ----------------------------------------------------------------------
274sub is_valid {
275
276=pod
277
278=head2 is_valid
279
280Determine whether the trigger is valid or not.
281
282 my $ok = $trigger->is_valid;
283
284=cut
285
286 my $self = shift;
287
288 for my $attr (
289 qw[ name perform_action_when database_event on_table action ]
290 ) {
291 return $self->error("No $attr") unless $self->$attr();
292 }
293
294 return $self->error("Missing fields for UPDATE ON") if
295 $self->database_event eq 'update_on' && !$self->fields;
296
297 return 1;
298}
299
300# ----------------------------------------------------------------------
301sub name {
302
303=pod
304
305=head2 name
306
307Get or set the trigger's name.
308
309 my $name = $trigger->name('foo');
310
311=cut
312
313 my $self = shift;
314 $self->{'name'} = shift if @_;
315 return $self->{'name'} || '';
316}
317
318# ----------------------------------------------------------------------
319sub order {
320
321=pod
322
323=head2 order
324
325Get or set the trigger's order.
326
327 my $order = $trigger->order(3);
328
329=cut
330
331 my ( $self, $arg ) = @_;
332
333 if ( defined $arg && $arg =~ /^\d+$/ ) {
334 $self->{'order'} = $arg;
335 }
336
337 return $self->{'order'} || 0;
338}
339
39ad1787 340# ----------------------------------------------------------------------
341sub schema {
342
343=pod
344
345=head2 schema
346
347Get or set the trigger's schema object.
348
349 $trigger->schema( $schema );
350 my $schema = $trigger->schema;
351
352=cut
353
354 my $self = shift;
355 if ( my $arg = shift ) {
356 return $self->error('Not a schema object') unless
357 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
358 $self->{'schema'} = $arg;
359 }
360
361 return $self->{'schema'};
362}
363
364# ----------------------------------------------------------------------
8742e408 365sub compare_arrays {
366
367=pod
368
369=head2 compare_arrays
370
371Compare two arrays.
372
373=cut
374
375 my ($first, $second) = @_;
376 no warnings; # silence spurious -w undef complaints
377
378 return 0 unless (ref $first eq 'ARRAY' and ref $second eq 'ARRAY' ) ;
379 return 0 unless @$first == @$second;
380 my @first = sort @$first;
381 my @second = sort @$second;
382 for (my $i = 0; $i < scalar @first; $i++) {
383 return 0 if @first[$i] ne @second[$i];
384 }
385 return 1;
386}
387
388# ----------------------------------------------------------------------
abf315bb 389sub equals {
390
391=pod
392
393=head2 equals
394
395Determines if this trigger is the same as another
396
397 my $isIdentical = $trigger1->equals( $trigger2 );
398
399=cut
400
401 my $self = shift;
402 my $other = shift;
6be9534b 403 my $case_insensitive = shift;
abf315bb 404
405 return 0 unless $self->SUPER::equals($other);
6be9534b 406 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
4598b71c 407 #return 0 unless $self->is_valid eq $other->is_valid;
abf315bb 408 return 0 unless $self->perform_action_when eq $other->perform_action_when;
8742e408 409 return 0 unless compare_arrays($self->database_events,$other->database_events) ;
abf315bb 410 return 0 unless $self->on_table eq $other->on_table;
411 return 0 unless $self->action eq $other->action;
4598b71c 412 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
abf315bb 413 return 1;
414}
415
416# ----------------------------------------------------------------------
39ad1787 417sub DESTROY {
418 my $self = shift;
419 undef $self->{'schema'}; # destroy cyclical reference
420}
421
e2cf647c 4221;
423
424# ----------------------------------------------------------------------
425
426=pod
427
428=head1 AUTHOR
429
430Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
431
432=cut