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   20680 use utf8;
  5         11  
  5         36  
4 5     5   210 use 5.016;
  5         18  
5 5     5   24 use strict;
  5         9  
  5         96  
6 5     5   22 use warnings;
  5         47  
  5         147  
7 5     5   869 use Geoffrey::Utils;
  5         10  
  5         153  
8 5     5   1485 use Geoffrey::Exception::RequiredValue;
  5         12  
  5         160  
9 5     5   31 use Geoffrey::Exception::NotSupportedException;
  5         11  
  5         245  
10              
11             $Geoffrey::Action::Trigger::VERSION = '0.000203';
12              
13 5     5   32 use parent 'Geoffrey::Role::Action';
  5         11  
  5         33  
14              
15             sub add {
16 15     15 1 12881 my ($self, $params, $options) = @_;
17 15         49 my $trigger = $self->converter->trigger;
18              
19 15 50 33     60 Geoffrey::Exception::NotSupportedException::throw_action() if !$trigger || !$trigger->add;
20 15 100       47 Geoffrey::Exception::RequiredValue::throw_trigger_name('for add trigger') if !$params->{name};
21 11 100       28 Geoffrey::Exception::RequiredValue::throw_table_name('for add trigger') if !$params->{event_object_table};
22 9 100       36 Geoffrey::Exception::RequiredValue::throw_common('event_manipulation') if !$params->{event_manipulation};
23 2 50       5 Geoffrey::Exception::RequiredValue::throw_common('action_timing') if !$params->{action_timing};
24 2 50       4 Geoffrey::Exception::RequiredValue::throw_common('action_orientation') if !$params->{action_orientation};
25 2 50       7 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         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         6 ]);
40 2         14 return $self->do($sql);
41             }
42              
43             sub alter {
44 8     8 1 2246 my ($self, $params, $options) = @_;
45 8         28 my $trigger = $self->converter->trigger;
46 8 50 66     49 Geoffrey::Exception::NotSupportedException::throw_action() if !$trigger || !$trigger->alter;
47 6 100       57 Geoffrey::Exception::RequiredValue::throw_trigger_name('for drop trigger') if !$params->{name};
48 5 100       16 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       12 Geoffrey::Exception::RequiredValue::throw_common('action_timing') if !$params->{action_timing};
51 2 100       9 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 1835 my ($self, $name, $table) = @_;
59 5         17 my $trigger = $self->converter->trigger;
60 5 50 33     29 Geoffrey::Exception::NotSupportedException::throw_action() if !$trigger || !$trigger->drop;
61 5 100       20 Geoffrey::Exception::RequiredValue::throw_trigger_name('for drop trigger') if !$name;
62 3 100       17 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         14 my $trigger = $self->converter->trigger;
69 3 50 66     31 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   9 my ($self, $table, $name, $schema) = @_;
77 2         6 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__