1 package SQL::Translator::Schema::Trigger;
7 SQL::Translator::Schema::Trigger - SQL::Translator trigger object
11 use SQL::Translator::Schema::Trigger;
12 my $trigger = SQL::Translator::Schema::Trigger->new(
14 perform_action_when => 'before', # or after
15 database_events => [qw/update insert/], # also update, update_on, delete
16 fields => [], # if event is "update"
17 on_table => 'foo', # table name
18 action => '...', # text of trigger
19 schema => $schema, # Schema object
20 scope => 'row', # or statement
25 C<SQL::Translator::Schema::Trigger> is the trigger object.
32 use SQL::Translator::Utils qw(parse_list_arg ex2err throw);
33 use SQL::Translator::Types qw(schema_obj enum);
34 use List::MoreUtils qw(uniq);
35 use Sub::Quote qw(quote_sub);
37 extends 'SQL::Translator::Schema::Object';
39 our $VERSION = '1.59';
45 my $schema = SQL::Translator::Schema::Trigger->new;
49 around BUILDARGS => sub {
50 my ($orig, $self, @args) = @_;
51 my $args = $self->$orig(@args);
52 if (exists $args->{on_table}) {
53 my $arg = delete $args->{on_table};
54 my $table = $args->{schema}->get_table($arg)
55 or die "Table named $arg doesn't exist";
56 $args->{table} = $table;
58 if (exists $args->{database_event}) {
59 $args->{database_events} = delete $args->{database_event};
64 =head2 perform_action_when
66 Gets or sets whether the event happens "before" or "after" the
69 $trigger->perform_action_when('after');
73 has perform_action_when => (
75 coerce => quote_sub(q{ defined $_[0] ? lc $_[0] : $_[0] }),
76 isa => enum([qw(before after)], {
77 msg => "Invalid argument '%s' to perform_action_when",
82 around perform_action_when => \&ex2err;
90 Obsolete please use database_events!
96 return $self->database_events( @_ );
99 =head2 database_events
101 Gets or sets the events that triggers the trigger.
103 my $ok = $trigger->database_events('insert');
107 has database_events => (
109 coerce => quote_sub(q{ [ map { lc } ref $_[0] eq 'ARRAY' ? @{$_[0]} : ($_[0]) ] }),
112 my %valid = map { $_, 1 } qw[ insert update update_on delete ];
113 my @invalid = grep { !defined $valid{ $_ } } @args;
117 sprintf("Invalid events '%s' in database_events",
125 around database_events => sub {
126 my ($orig,$self) = (shift, shift);
129 ex2err($orig, $self, ref $_[0] eq 'ARRAY' ? $_[0] : \@_)
134 ? @{ $self->$orig || [] }
140 Gets and set which fields to monitor for C<database_event>.
143 $view->fields('id', 'name');
144 $view->fields( 'id, name' );
145 $view->fields( [ 'id', 'name' ] );
146 $view->fields( qw[ id name ] );
148 my @fields = $view->fields;
155 my @fields = uniq @{parse_list_arg($_[0])};
156 @fields ? \@fields : undef;
160 around fields => sub {
163 my $fields = parse_list_arg( @_ );
164 $self->$orig($fields) if @$fields;
166 return wantarray ? @{ $self->$orig || [] } : $self->$orig;
171 Gets or set the table on which the trigger works, as a L<SQL::Translator::Schema::Table> object.
172 $trigger->table($triggered_table);
176 has table => ( is => 'rw', isa => schema_obj('Table'), weak_ref => 1 );
178 around table => \&ex2err;
186 Gets or set the table name on which the trigger works, as a string.
187 $trigger->on_table('foo');
191 my ($self, $arg) = @_;
193 my $table = $self->schema->get_table($arg);
194 die "Table named $arg doesn't exist"
196 $self->table($table);
198 return $self->table->name;
203 Gets or set the action of the trigger.
216 has action => ( is => 'rw', default => quote_sub(q{ '' }) );
224 Determine whether the trigger is valid or not.
226 my $ok = $trigger->is_valid;
233 qw[ name perform_action_when database_events on_table action ]
235 return $self->error("Invalid: missing '$attr'") unless $self->$attr();
238 return $self->error("Missing fields for UPDATE ON") if
239 $self->database_event eq 'update_on' && !$self->fields;
246 Get or set the trigger's name.
248 my $name = $trigger->name('foo');
252 has name => ( is => 'rw', default => quote_sub(q{ '' }) );
256 Get or set the trigger's order.
258 my $order = $trigger->order(3);
262 has order => ( is => 'rw', default => quote_sub(q{ 0 }) );
264 around order => sub {
265 my ( $orig, $self, $arg ) = @_;
267 if ( defined $arg && $arg =~ /^\d+$/ ) {
268 return $self->$orig($arg);
276 Get or set the trigger's scope (row or statement).
278 my $scope = $trigger->scope('statement');
284 isa => enum([qw(row statement)], {
285 msg => "Invalid scope '%s'", icase => 1, allow_undef => 1,
289 around scope => \&ex2err;
294 Get or set the trigger's schema object.
296 $trigger->schema( $schema );
297 my $schema = $trigger->schema;
301 has schema => (is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
303 around schema => \&ex2err;
309 =head2 compare_arrays
315 my ($first, $second) = @_;
316 no warnings; # silence spurious -w undef complaints
318 return 0 unless (ref $first eq 'ARRAY' and ref $second eq 'ARRAY' ) ;
320 return 0 unless @$first == @$second;
322 my @first = sort @$first;
324 my @second = sort @$second;
326 for (my $i = 0; $i < scalar @first; $i++) {
327 return 0 if @first[$i] ne @second[$i];
335 Determines if this trigger is the same as another
337 my $is_identical = $trigger1->equals( $trigger2 );
341 around equals => sub {
345 my $case_insensitive = shift;
347 return 0 unless $self->$orig($other);
350 for my $name ( $self->name, $other->name ) {
351 $name = lc $name if $case_insensitive;
355 if ( keys %names > 1 ) {
356 return $self->error('Names not equal');
359 if ( !$self->perform_action_when eq $other->perform_action_when ) {
360 return $self->error('perform_action_when differs');
364 !compare_arrays( [$self->database_events], [$other->database_events] )
366 return $self->error('database_events differ');
369 if ( $self->on_table ne $other->on_table ) {
370 return $self->error('on_table differs');
373 if ( $self->action ne $other->action ) {
374 return $self->error('action differs');
378 !$self->_compare_objects( scalar $self->extra, scalar $other->extra )
380 return $self->error('extras differ');
386 # Must come after all 'has' declarations
387 around new => \&ex2err;
396 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.