blib/lib/SQL/Translator/Schema/Trigger.pm

filter: Statementstmt 92.1% 106 / 115 Branchbran 65.0% 39 / 60 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 80.9% 170 / 210 SCAR 38.0
CC
36
Cov
81%
__ANON__
13.1
compare_arrays
6.2
__ANON__
6.0
CRAP 44.9
« blib/lib/SQL/Translator/Parser/XML/SQLFairy.pm ↑ Summary blib/lib/Test/SQL/Translator.pm »
1 package SQL::Translator::Schema::Trigger;
2  
3 =pod
4  
5 =head1 NAME
6  
7 SQL::Translator::Schema::Trigger - SQL::Translator trigger object
8  
9 =head1 SYNOPSIS
10  
11 use SQL::Translator::Schema::Trigger;
12 my $trigger = SQL::Translator::Schema::Trigger->new(
13 name => 'foo',
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
21 );
22  
23 =head1 DESCRIPTION
24  
25 C<SQL::Translator::Schema::Trigger> is the trigger object.
26  
27 =head1 METHODS
28  
29 =cut
30  
31 72 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 72 ▶use SQL::Translator::Utils qw(parse_list_arg ex2err throw uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 72 ▶use SQL::Translator::Types qw(schema_obj enum);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 72 ▶use Sub::Quote qw(quote_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 72 extends 'SQL::Translator::Schema::Object';
37  
38 72 our $VERSION = '1.63';
39  
40 =head2 new
41  
42 Object constructor.
43  
44 my $trigger = SQL::Translator::Schema::Trigger->new;
45  
46 =cut
47  
48 around BUILDARGS => sub {
49 77 ▶ my ($orig, $self, @args) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
50 77 my $args = $self->$orig(@args);
51 77 ▶ if (exists $args->{on_table}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"on_table"}) 65 12 77
52 65 my $arg = delete $args->{on_table};
53 65 ▶ my $table = $args->{schema}->get_table($arg)
Branch: 1 branch
BranchTrueFalseTotal
my $table = $args->{"schema"}->get_table($arg) or die "Table named $arg doesn't exist" 0 65 65
54 or die "Table named $arg doesn't exist";
55 65 $args->{table} = $table;
56 }
57 77 ▶ if (exists $args->{database_event}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"database_event"}) 13 64 77
58 13 $args->{database_events} = delete $args->{database_event};
59 }
60 77 return $args;
61 72 };
62  
63 =head2 perform_action_when
64  
65 Gets or sets whether the event happens "before" or "after" the
66 C<database_event>.
67  
68 $trigger->perform_action_when('after');
69  
70 =cut
71  
72 72 has perform_action_when => (
73 is => 'rw',
74 coerce => quote_sub(q{ defined $_[0] ? lc $_[0] : $_[0] }),
75 isa => enum([qw(before after)], {
76 msg => "Invalid argument '%s' to perform_action_when",
77 allow_undef => 1,
78 }),
79 );
80  
81 72 around perform_action_when => \&ex2err;
82  
83 sub database_event {
84  
85 =pod
86  
87 =head2 database_event
88  
89 Obsolete please use database_events!
90  
91 =cut
92  
93 2 ▶ my $self = shift;
Subroutine database_event: called CC=1 SCAR=0.0
Pod documented
94  
95 2 return $self->database_events( @_ );
96 }
97  
98 =head2 database_events
99  
100 Gets or sets the events that triggers the trigger.
101  
102 my $ok = $trigger->database_events('insert');
103  
104 =cut
105  
106 has database_events => (
107 is => 'rw',
108 coerce => quote_sub(q{ [ map { lc } ref $_[0] eq 'ARRAY' ? @{$_[0]} : ($_[0]) ] }),
109 isa => sub {
110 71 ▶ my @args = @{$_[0]};
Subroutine __ANON__: called CC=2 SCAR=6.9
111 71 my %valid = map { $_, 1 } qw[ insert update update_on delete ];
112 71 my @invalid = grep { !defined $valid{ $_ } } @args;
113  
114 71 ▶ if ( @invalid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@invalid) 1 70 71
115 1 throw(
116 sprintf("Invalid events '%s' in database_events",
117 join(', ', @invalid)
118 )
119 );
120 }
121 },
122 72 );
123  
124 around database_events => sub {
125 95 ▶ my ($orig,$self) = (shift, shift);
Subroutine __ANON__: called CC=6 SCAR=17.9
126  
127 95 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 90 95
128 5 ▶ ex2err($orig, $self, ref $_[0] eq 'ARRAY' ? $_[0] : \@_)
Branch: 2 branches
BranchTrueFalseTotal
ex2err($orig, $self, ref $_[0] eq "ARRAY" ? $_[0] : \@_) or return 1 4 5
ref $_[0] eq 'ARRAY' ? : 2 3 5
129 or return;
130 }
131  
132 return wantarray
133 94 ▶ ? @{ $self->$orig || [] }
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 17 77 94
$self->$orig or [] 0 17 17
134 : $self->$orig;
135 72 };
136  
137 =head2 fields
138  
139 Gets and set which fields to monitor for C<database_event>.
140  
141 $view->fields('id');
142 $view->fields('id', 'name');
143 $view->fields( 'id, name' );
144 $view->fields( [ 'id', 'name' ] );
145 $view->fields( qw[ id name ] );
146  
147 my @fields = $view->fields;
148  
149 =cut
150  
151 has fields => (
152 is => 'rw',
153 coerce => sub {
154 4 ▶ my @fields = uniq @{parse_list_arg($_[0])};
Subroutine __ANON__: called CC=2 SCAR=7.1
155 4 ▶ @fields ? \@fields : undef;
Branch: 1 branch
BranchTrueFalseTotal
@fields ? : 4 0 4
156 },
157 72 );
158  
159 around fields => sub {
160 21 ▶ my $orig = shift;
Subroutine __ANON__: called CC=4 SCAR=16.4
161 21 my $self = shift;
162 21 my $fields = parse_list_arg( @_ );
163 21 ▶ $self->$orig($fields) if @$fields;
Branch: 1 branch
BranchTrueFalseTotal
if @$fields 0 21 21
164  
165 21 ▶ return wantarray ? @{ $self->$orig || [] } : $self->$orig;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 21 21
$self->$orig or [] 0 0 0
166 72 };
167  
168 =head2 table
169  
170 Gets or set the table on which the trigger works, as a L<SQL::Translator::Schema::Table> object.
171 $trigger->table($triggered_table);
172  
173 =cut
174  
175 72 has table => ( is => 'rw', isa => schema_obj('Table'), weak_ref => 1 );
176  
177 72 around table => \&ex2err;
178  
179 sub on_table {
180  
181 =pod
182  
183 =head2 on_table
184  
185 Gets or set the table name on which the trigger works, as a string.
186 $trigger->on_table('foo');
187  
188 =cut
189  
190 81 ▶ my ($self, $arg) = @_;
Subroutine on_table: called CC=3 SCAR=16.0
Pod documented
191 81 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 81 81
192 0 my $table = $self->schema->get_table($arg);
193 0 ▶ die "Table named $arg doesn't exist"
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 0 0
194 if !$table;
195 0 $self->table($table);
196 }
197 81 return $self->table->name;
198 }
199  
200 =head2 action
201  
202 Gets or set the action of the trigger.
203  
204 $trigger->action(
205 q[
206 BEGIN
207 select ...;
208 update ...;
209 END
210 ]
211 );
212  
213 =cut
214  
215 72 has action => ( is => 'rw', default => quote_sub(q{ '' }) );
216  
217 sub is_valid {
218  
219 =pod
220  
221 =head2 is_valid
222  
223 Determine whether the trigger is valid or not.
224  
225 my $ok = $trigger->is_valid;
226  
227 =cut
228  
229 2 ▶ my $self = shift;
Subroutine is_valid: called CC=5 SCAR=17.8
Pod documented
230  
231 2 for my $attr (
232 qw[ name perform_action_when database_events on_table action ]
233 ) {
234 10 ▶ return $self->error("Invalid: missing '$attr'") unless $self->$attr();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$attr 0 10 10
235 }
236  
237 2 ▶ return $self->error("Missing fields for UPDATE ON") if
Branch: 1 branch
BranchTrueFalseTotal
if $self->database_event eq "update_on" and not $self->fields 0 2 2
Condition33%1/3
$self->database_event eq "update_on" and not $self->fields
!ll&&!rl&&r
200
MC/DC0%0/2
$self->database_event eq "update_on" and not $self->fields
$self->database_event eq "update_on" not $self->fields
Truth table33%1/3
A and not(B)
A= $self->database_event eq "update_on"B= not $self->fields
ABresult
0X0
100
111
238 $self->database_event eq 'update_on' && !$self->fields;
239  
240 2 return 1;
241 }
242  
243 =head2 name
244  
245 Get or set the trigger's name.
246  
247 my $name = $trigger->name('foo');
248  
249 =cut
250  
251 72 has name => ( is => 'rw', default => quote_sub(q{ '' }) );
252  
253 =head2 order
254  
255 Get or set the trigger's order.
256  
257 my $order = $trigger->order(3);
258  
259 =cut
260  
261 72 has order => ( is => 'rw', default => quote_sub(q{ 0 }) );
262  
263 around order => sub {
264 174 ▶ my ( $orig, $self, $arg ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
265  
266 174 ▶ if ( defined $arg && $arg =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg and $arg =~ /^\d+$/) 79 95 174
Condition66%2/3
defined $arg and $arg =~ /^\d+$/
!ll&&!rl&&r
95079
MC/DC50%1/2
defined $arg and $arg =~ /^\d+$/
defined $arg $arg =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $argB= $arg =~ /^\d+$/
ABresult
0X0
100
111
267 79 return $self->$orig($arg);
268 }
269  
270 95 return $self->$orig;
271 72 };
272  
273 =head2 scope
274  
275 Get or set the trigger's scope (row or statement).
276  
277 my $scope = $trigger->scope('statement');
278  
279 =cut
280  
281 72 has scope => (
282 is => 'rw',
283 isa => enum([qw(row statement)], {
284 msg => "Invalid scope '%s'", icase => 1, allow_undef => 1,
285 }),
286 );
287  
288 72 around scope => \&ex2err;
289  
290  
291 =head2 schema
292  
293 Get or set the trigger's schema object.
294  
295 $trigger->schema( $schema );
296 my $schema = $trigger->schema;
297  
298 =cut
299  
300 72 has schema => (is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
301  
302 72 around schema => \&ex2err;
303  
304 sub compare_arrays {
305  
306 =pod
307  
308 =head2 compare_arrays
309  
310 Compare two arrays.
311  
312 =cut
313  
314 3 ▶ my ($first, $second) = @_;
Subroutine compare_arrays: called CC=6 SCAR=18.3
Pod documented
315 72 ▶ no warnings; # silence spurious -w undef complaints
Subroutine BEGIN: called CC=1 SCAR=0.0
316  
317 3 ▶ return 0 unless (ref $first eq 'ARRAY' and ref $second eq 'ARRAY' ) ;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $first eq "ARRAY" and ref $second eq "ARRAY" 0 3 3
Condition33%1/3
ref $first eq "ARRAY" and ref $second eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/2
ref $first eq "ARRAY" and ref $second eq "ARRAY"
ref $first eq "ARRAY" ref $second eq "ARRAY"
Truth table33%1/3
A and B
A= ref $first eq "ARRAY"B= ref $second eq "ARRAY"
ABresult
0X0
100
111
318  
319 3 ▶ return 0 unless @$first == @$second;
Branch: 1 branch
BranchTrueFalseTotal
unless @$first == @$second 1 2 3
320  
321 2 my @first = sort @$first;
322  
323 2 my @second = sort @$second;
324  
325 2 ▶ for (my $i = 0; $i < scalar @first; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @first) 3 2 5
326 3 ▶ return 0 if @first[$i] ne @second[$i];
Branch: 1 branch
BranchTrueFalseTotal
if @first[$i] ne @second[$i] 0 3 3
327 }
328  
329 2 return 1;
330 }
331  
332 =head2 equals
333  
334 Determines if this trigger is the same as another
335  
336 my $is_identical = $trigger1->equals( $trigger2 );
337  
338 =cut
339  
340 around equals => sub {
341 3 ▶ my $orig = shift;
Subroutine __ANON__: called CC=10 SCAR=25.8
342 3 my $self = shift;
343 3 my $other = shift;
344 3 my $case_insensitive = shift;
345  
346 3 ▶ return 0 unless $self->$orig($other);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$orig($other) 0 3 3
347  
348 3 my %names;
349 3 for my $name ( $self->name, $other->name ) {
350 6 ▶ $name = lc $name if $case_insensitive;
Branch: 1 branch
BranchTrueFalseTotal
if $case_insensitive 0 6 6
351 6 $names{ $name }++;
352 }
353  
354 3 ▶ if ( keys %names > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %names > 1) 0 3 3
355 0 return $self->error('Names not equal');
356 }
357  
358 3 ▶ if ( !$self->perform_action_when eq $other->perform_action_when ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!$self->perform_action_when eq $other->perform_action_when) 0 3 3
359 0 return $self->error('perform_action_when differs');
360 }
361  
362 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (compare_arrays([$self->database_events], [$other->database_events])) 1 2 3
363 !compare_arrays( [$self->database_events], [$other->database_events] )
364 ) {
365 1 return $self->error('database_events differ');
366 }
367  
368 2 ▶ if ( $self->on_table ne $other->on_table ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_table ne $other->on_table) 0 2 2
369 0 return $self->error('on_table differs');
370 }
371  
372 2 ▶ if ( $self->action ne $other->action ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action ne $other->action) 0 2 2
373 0 return $self->error('action differs');
374 }
375  
376 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_compare_objects(scalar $self->extra, scalar $other->extra)) 0 2 2
377 !$self->_compare_objects( scalar $self->extra, scalar $other->extra )
378 ) {
379 0 return $self->error('extras differ');
380 }
381  
382 2 return 1;
383 72 };
384  
385 # Must come after all 'has' declarations
386 72 around new => \&ex2err;
387  
388 72 1;
389  
390 =pod
391  
392 =head1 AUTHORS
393  
394 Anonymous,
395 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
396  
397 =cut
« blib/lib/SQL/Translator/Parser/XML/SQLFairy.pm ↑ Summary blib/lib/Test/SQL/Translator.pm »