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