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