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

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