File Coverage

blib/lib/Geoffrey/Action/Trigger.pm
Criterion Covered Total %
statement 59 64 92.1
branch 30 40 75.0
condition 6 12 50.0
subroutine 13 13 100.0
pod 4 4 100.0
total 112 133 84.2


line stmt bran cond sub pod time code
1             package Geoffrey::Action::Trigger;
2              
3 5     5   20485 use utf8;
  5         12  
  5         50  
4 5     5   212 use 5.016;
  5         19  
5 5     5   25 use strict;
  5         10  
  5         96  
6 5     5   80 use warnings;
  5         53  
  5         149  
7 5     5   933 use Geoffrey::Utils;
  5         11  
  5         152  
8 5     5   1510 use Geoffrey::Exception::RequiredValue;
  5         28  
  5         174  
9 5     5   32 use Geoffrey::Exception::NotSupportedException;
  5         9  
  5         247  
10              
11             $Geoffrey::Action::Trigger::VERSION = '0.000204';
12              
13 5     5   32 use parent 'Geoffrey::Role::Action';
  5         12  
  5         33  
14              
15             sub add {
16 15     15 1 12853 my ($self, $params, $options) = @_;
17 15         78 my $trigger = $self->converter->trigger;
18              
19 15 50 33     76 Geoffrey::Exception::NotSupportedException::throw_action() if !$trigger || !$trigger->add;
20 15 100       53 Geoffrey::Exception::RequiredValue::throw_trigger_name('for add trigger') if !$params->{name};
21 11 100       29 Geoffrey::Exception::RequiredValue::throw_table_name('for add trigger') if !$params->{event_object_table};
22 9 100       35 Geoffrey::Exception::RequiredValue::throw_common('event_manipulation') if !$params->{event_manipulation};
23 2 50       6 Geoffrey::Exception::RequiredValue::throw_common('action_timing') if !$params->{action_timing};
24 2 50       5 Geoffrey::Exception::RequiredValue::throw_common('action_orientation') if !$params->{action_orientation};
25 2 50       4 Geoffrey::Exception::RequiredValue::throw_common('action_statement') if !$params->{action_statement};
26              
27 2         10 my $result = $self->_find_same_trigger($params->{event_object_table}, $params->{name}, $params->{schema});
28              
29 2 50       15 if (@{$result} > 0) {
  2         8  
30 0         0 $self->drop($params->{name}, $params->{event_object_table});
31 0         0 $params->{event_manipulation} .= ' OR ' . $result->[0]->{event_manipulation};
32             }
33              
34             my $sql = Geoffrey::Utils::replace_spare(
35             $trigger->add($options),
36             [
37             $params->{name}, $params->{action_timing}, $params->{event_manipulation},
38             $params->{event_object_table}, $params->{action_orientation}, $params->{action_statement},
39 2         7 ]);
40 2         12 return $self->do($sql);
41             }
42              
43             sub alter {
44 8     8 1 2168 my ($self, $params, $options) = @_;
45 8         25 my $trigger = $self->converter->trigger;
46 8 50 66     43 Geoffrey::Exception::NotSupportedException::throw_action() if !$trigger || !$trigger->alter;
47 6 100       60 Geoffrey::Exception::RequiredValue::throw_trigger_name('for drop trigger') if !$params->{name};
48 5 100       15 Geoffrey::Exception::RequiredValue::throw_table_name('for drop trigger') if !$params->{event_object_table};
49 4 100       13 Geoffrey::Exception::RequiredValue::throw_common('event_manipulation') if !$params->{event_manipulation};
50 3 100       10 Geoffrey::Exception::RequiredValue::throw_common('action_timing') if !$params->{action_timing};
51 2 100       8 Geoffrey::Exception::RequiredValue::throw_common('action_orientation') if !$params->{action_orientation};
52 1 50       7 Geoffrey::Exception::RequiredValue::throw_common('action_statement') if !$params->{action_statement};
53              
54 0         0 return [$self->drop($params, $options), $self->add($params, $options),];
55             }
56              
57             sub drop {
58 5     5 1 1848 my ($self, $name, $table) = @_;
59 5         16 my $trigger = $self->converter->trigger;
60 5 50 33     27 Geoffrey::Exception::NotSupportedException::throw_action() if !$trigger || !$trigger->drop;
61 5 100       17 Geoffrey::Exception::RequiredValue::throw_trigger_name('for drop trigger') if !$name;
62 3 100       14 Geoffrey::Exception::RequiredValue::throw_table_name('for drop trigger') if !$table;
63 1         4 return $self->do(Geoffrey::Utils::replace_spare($trigger->drop, [$name, $table]));
64             }
65              
66             sub list {
67 3     3 1 9 my ($self, $schema) = @_;
68 3         18 my $trigger = $self->converter->trigger;
69 3 50 66     40 if (!$trigger || !$trigger->list) {
70 0         0 Geoffrey::Exception::NotSupportedException::throw_action();
71             }
72 1         4 return $trigger->information($self->do_arrayref($trigger->list($schema)));
73             }
74              
75             sub _find_same_trigger {
76 2     2   7 my ($self, $table, $name, $schema) = @_;
77 2         5 my $trigger = $self->converter->trigger;
78 2 50       18 return [] if !$trigger->can('find_by_name_and_table');
79 0           return $self->do_arrayref($trigger->find_by_name_and_table, [$table, $name, $schema]);
80             }
81              
82             1;
83              
84             __END__