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   20968 use utf8;
  5         9  
  5         33  
4 5     5   215 use 5.016;
  5         19  
5 5     5   23 use strict;
  5         9  
  5         95  
6 5     5   21 use warnings;
  5         52  
  5         142  
7 5     5   999 use Geoffrey::Utils;
  5         11  
  5         138  
8 5     5   1522 use Geoffrey::Exception::RequiredValue;
  5         23  
  5         186  
9 5     5   34 use Geoffrey::Exception::NotSupportedException;
  5         11  
  5         203  
10              
11             $Geoffrey::Action::Trigger::VERSION = '0.000205';
12              
13 5     5   29 use parent 'Geoffrey::Role::Action';
  5         17  
  5         37  
14              
15             sub add {
16 15     15 1 12527 my ($self, $params, $options) = @_;
17 15         55 my $trigger = $self->converter->trigger;
18              
19 15 50 33     61 Geoffrey::Exception::NotSupportedException::throw_action() if !$trigger || !$trigger->add;
20 15 100       49 Geoffrey::Exception::RequiredValue::throw_trigger_name('for add trigger') if !$params->{name};
21 11 100       30 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       6 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       5 if (@{$result} > 0) {
  2         7  
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         8 ]);
40 2         12 return $self->do($sql);
41             }
42              
43             sub alter {
44 8     8 1 2146 my ($self, $params, $options) = @_;
45 8         25 my $trigger = $self->converter->trigger;
46 8 50 66     39 Geoffrey::Exception::NotSupportedException::throw_action() if !$trigger || !$trigger->alter;
47 6 100       49 Geoffrey::Exception::RequiredValue::throw_trigger_name('for drop trigger') if !$params->{name};
48 5 100       17 Geoffrey::Exception::RequiredValue::throw_table_name('for drop trigger') if !$params->{event_object_table};
49 4 100       17 Geoffrey::Exception::RequiredValue::throw_common('event_manipulation') if !$params->{event_manipulation};
50 3 100       14 Geoffrey::Exception::RequiredValue::throw_common('action_timing') if !$params->{action_timing};
51 2 100       15 Geoffrey::Exception::RequiredValue::throw_common('action_orientation') if !$params->{action_orientation};
52 1 50       6 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 1854 my ($self, $name, $table) = @_;
59 5         17 my $trigger = $self->converter->trigger;
60 5 50 33     26 Geoffrey::Exception::NotSupportedException::throw_action() if !$trigger || !$trigger->drop;
61 5 100       18 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 8 my ($self, $schema) = @_;
68 3         16 my $trigger = $self->converter->trigger;
69 3 50 66     24 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         6 my $trigger = $self->converter->trigger;
78 2 50       26 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__