| 1 | package Geoffrey::Action::Constraint; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use 5.016; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | $Geoffrey::Action::Constraint::VERSION = '0.000206'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use parent 'Geoffrey::Role::Action'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 16 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 16 | my $class = shift; | |||||||||||||||||||||||||||
| 14 | 16 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||
| 15 | $self->{constraints} = [ | ||||||||||||||||||||||||||||
| 16 | 15 | qw/ | |||||||||||||||||||||||||||
| 17 | foreignkey | ||||||||||||||||||||||||||||
| 18 | primarykey | ||||||||||||||||||||||||||||
| 19 | check | ||||||||||||||||||||||||||||
| 20 | index | ||||||||||||||||||||||||||||
| 21 | unique | ||||||||||||||||||||||||||||
| 22 | / | ||||||||||||||||||||||||||||
| 23 | ]; | ||||||||||||||||||||||||||||
| 24 | 15 | return bless $self, $class; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 39 | ▶ | sub _obj_check { | ||||||||||||||||||||||||||
|
Subroutine
_obj_check: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 28 | 39 | my ($self) = @_; | |||||||||||||||||||||||||||
| 29 | 39 | require Geoffrey::Action::Constraint::Check; | |||||||||||||||||||||||||||
| 30 | 39 | ▶ | $self->{check} //= Geoffrey::Action::Constraint::Check->new( | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"check"} //= "Geoffrey::Action::Constraint::Check"->new("converter", $self->converter, "dbh", $self->dbh)
MC/DC0%0/2
$self->{"check"} //= "Geoffrey::Action::Constraint::Check"->new("converter", $self->converter, "dbh", $self->dbh)
$self->{"check"}
"Geoffrey::Action::Constraint::Check"->new("converter", $self->converter, "dbh", $self->dbh)
Truth table66%2/3
A //= B
A= $self->{"check"}B= "Geoffrey::Action::Constraint::Check"->new("converter", $self->converter, "dbh", $self->dbh)
| |||||||||||||||||||||||||||||
| 31 | converter => $self->converter, | ||||||||||||||||||||||||||||
| 32 | dbh => $self->dbh, | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | 39 | return $self->{check}; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 39 | ▶ | sub _obj_index { | ||||||||||||||||||||||||||
|
Subroutine
_obj_index: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 38 | 39 | my ($self) = @_; | |||||||||||||||||||||||||||
| 39 | 39 | require Geoffrey::Action::Constraint::Index; | |||||||||||||||||||||||||||
| 40 | 39 | ▶ | $self->{index} //= Geoffrey::Action::Constraint::Index->new( | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"index"} //= "Geoffrey::Action::Constraint::Index"->new("converter", $self->converter, "dbh", $self->dbh)
MC/DC0%0/2
$self->{"index"} //= "Geoffrey::Action::Constraint::Index"->new("converter", $self->converter, "dbh", $self->dbh)
$self->{"index"}
"Geoffrey::Action::Constraint::Index"->new("converter", $self->converter, "dbh", $self->dbh)
Truth table66%2/3
A //= B
A= $self->{"index"}B= "Geoffrey::Action::Constraint::Index"->new("converter", $self->converter, "dbh", $self->dbh)
| |||||||||||||||||||||||||||||
| 41 | converter => $self->converter, | ||||||||||||||||||||||||||||
| 42 | dbh => $self->dbh, | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | 39 | return $self->{index}; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 40 | ▶ | sub _obj_unique { | ||||||||||||||||||||||||||
|
Subroutine
_obj_unique: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 48 | 40 | my ($self) = @_; | |||||||||||||||||||||||||||
| 49 | 40 | require Geoffrey::Action::Constraint::Unique; | |||||||||||||||||||||||||||
| 50 | 40 | ▶ | $self->{unique} //= Geoffrey::Action::Constraint::Unique->new( | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"unique"} //= "Geoffrey::Action::Constraint::Unique"->new("converter", $self->converter, "dbh", $self->dbh)
MC/DC0%0/2
$self->{"unique"} //= "Geoffrey::Action::Constraint::Unique"->new("converter", $self->converter, "dbh", $self->dbh)
$self->{"unique"}
"Geoffrey::Action::Constraint::Unique"->new("converter", $self->converter, "dbh", $self->dbh)
Truth table66%2/3
A //= B
A= $self->{"unique"}B= "Geoffrey::Action::Constraint::Unique"->new("converter", $self->converter, "dbh", $self->dbh)
| |||||||||||||||||||||||||||||
| 51 | converter => $self->converter, | ||||||||||||||||||||||||||||
| 52 | dbh => $self->dbh, | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | 40 | return $self->{unique}; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 40 | ▶ | sub _obj_primarykey { | ||||||||||||||||||||||||||
|
Subroutine
_obj_primarykey: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 58 | 40 | my ($self) = @_; | |||||||||||||||||||||||||||
| 59 | 40 | require Geoffrey::Action::Constraint::PrimaryKey; | |||||||||||||||||||||||||||
| 60 | 40 | ▶ | $self->{primarykey} //= Geoffrey::Action::Constraint::PrimaryKey->new( | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"primarykey"} //= "Geoffrey::Action::Constraint::PrimaryKey"->new("converter", $self->converter, "dbh", $self->dbh)
MC/DC0%0/2
$self->{"primarykey"} //= "Geoffrey::Action::Constraint::PrimaryKey"->new("converter", $self->converter, "dbh", $self->dbh)
$self->{"primarykey"}
"Geoffrey::Action::Constraint::PrimaryKey"->new("converter", $self->converter, "dbh", $self->dbh)
Truth table66%2/3
A //= B
A= $self->{"primarykey"}B= "Geoffrey::Action::Constraint::PrimaryKey"->new("converter", $self->converter, "dbh", $self->dbh)
| |||||||||||||||||||||||||||||
| 61 | converter => $self->converter, | ||||||||||||||||||||||||||||
| 62 | dbh => $self->dbh, | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | 40 | return $self->{primarykey}; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 121 | ▶ | sub _obj_foreignkey { | ||||||||||||||||||||||||||
|
Subroutine
_obj_foreignkey: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 68 | 121 | my ($self) = @_; | |||||||||||||||||||||||||||
| 69 | 121 | require Geoffrey::Action::Constraint::ForeignKey; | |||||||||||||||||||||||||||
| 70 | 121 | ▶ | $self->{foreignkey} //= Geoffrey::Action::Constraint::ForeignKey->new( | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"foreignkey"} //= "Geoffrey::Action::Constraint::ForeignKey"->new("converter", $self->converter, "dbh", $self->dbh)
MC/DC0%0/2
$self->{"foreignkey"} //= "Geoffrey::Action::Constraint::ForeignKey"->new("converter", $self->converter, "dbh", $self->dbh)
$self->{"foreignkey"}
"Geoffrey::Action::Constraint::ForeignKey"->new("converter", $self->converter, "dbh", $self->dbh)
Truth table66%2/3
A //= B
A= $self->{"foreignkey"}B= "Geoffrey::Action::Constraint::ForeignKey"->new("converter", $self->converter, "dbh", $self->dbh)
| |||||||||||||||||||||||||||||
| 71 | converter => $self->converter, | ||||||||||||||||||||||||||||
| 72 | dbh => $self->dbh, | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | 121 | return $self->{foreignkey}; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 39 | ▶ | sub _set_collective_params { | ||||||||||||||||||||||||||
|
Subroutine
_set_collective_params: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 79 | 39 | my ($self, $s_sub_call, $b_param) = @_; | |||||||||||||||||||||||||||
| 80 | 39 | for (@{$self->{constraints}}) { | |||||||||||||||||||||||||||
| 81 | 195 | my $s_obj_sub = '_obj_' . $_; | |||||||||||||||||||||||||||
| 82 | 195 | $self->$s_obj_sub($self)->$s_sub_call($b_param); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 39 | return 1; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 3 | ▶ | sub _collective_do { | ||||||||||||||||||||||||||
|
Subroutine
_collective_do: called CC=5 SCAR=17.6
| |||||||||||||||||||||||||||||
| 88 | 3 | my ($self, $s_sub_call, $s_table_name, $constraints_to_alter) = @_; | |||||||||||||||||||||||||||
| 89 | 3 | my @a_result = (); | |||||||||||||||||||||||||||
| 90 | 3 | for (@{$constraints_to_alter}) { | |||||||||||||||||||||||||||
| 91 | 3 | my $s_obj_sub = '_obj_' . delete $_->{constraint}; | |||||||||||||||||||||||||||
| 92 | 3 | ▶ | if (!$self->can($s_obj_sub)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | require Geoffrey::Exception::General; | |||||||||||||||||||||||||||
| 94 | 0 | return Geoffrey::Exception::General::throw_unknown_action($s_obj_sub); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 3 | $_->{table} = $s_table_name; | |||||||||||||||||||||||||||
| 97 | 3 | push @a_result, $self->do($self->$s_obj_sub()->$s_sub_call($_)); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 1 | ▶ | return if scalar @a_result == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 1 | ▶ | return shift @a_result if scalar @a_result == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | return \@a_result; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 122 | ▶ | sub for_table { | ||||||||||||||||||||||||||
|
Subroutine
for_table: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 122 | my ($self, $b_for_table) = @_; | |||||||||||||||||||||||||||
| 106 | 122 | ▶ | return $self->{for_table} if (!defined $b_for_table); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 38 | $self->{for_table} = $b_for_table; | |||||||||||||||||||||||||||
| 108 | 38 | $self->_set_collective_params('for_table', $b_for_table); | |||||||||||||||||||||||||||
| 109 | 38 | return $self; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | sub dryrun { | ||||||||||||||||||||||||||
|
Subroutine
dryrun: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 2 | my ($self, $b_dryrun) = @_; | |||||||||||||||||||||||||||
| 114 | 2 | ▶ | return $self->{dryrun} if (!defined $b_dryrun); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1 | $self->{dryrun} = $b_dryrun; | |||||||||||||||||||||||||||
| 116 | 1 | $self->_set_collective_params('dryrun', $b_dryrun); | |||||||||||||||||||||||||||
| 117 | 1 | return $self; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 84 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 84 | my ($self, $s_table_name, $hr_params, $ar_constraint_params) = @_; | |||||||||||||||||||||||||||
| 122 | 84 | ▶ | return $self->create_table_column($s_table_name, $hr_params, $ar_constraint_params) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | if $self->for_table; | ||||||||||||||||||||||||||||
| 124 | 4 | require Geoffrey::Exception::General; | |||||||||||||||||||||||||||
| 125 | 4 | return Geoffrey::Exception::General::throw_unknown_action('add constraint'); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | ▶ | sub alter { | ||||||||||||||||||||||||||
|
Subroutine
alter: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 3 | my ($self, $hr_params) = @_; | |||||||||||||||||||||||||||
| 130 | 3 | require Ref::Util; | |||||||||||||||||||||||||||
| 131 | 3 | ▶ | if (!Ref::Util::is_hashref($hr_params)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 1 | require Geoffrey::Exception::General; | |||||||||||||||||||||||||||
| 133 | 1 | Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ . '::alter', 'hash'); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 2 | return $self->_collective_do('alter', delete $hr_params->{table}, $hr_params->{constraints}); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 2 | ▶ | sub drop { | ||||||||||||||||||||||||||
|
Subroutine
drop: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 2 | my ($self, $s_table_name, $hr_params) = @_; | |||||||||||||||||||||||||||
| 140 | 2 | ▶ | if (!$s_table_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 1 | require Geoffrey::Exception::RequiredValue; | |||||||||||||||||||||||||||
| 142 | 1 | Geoffrey::Exception::RequiredValue::throw_reftable_missing(); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 1 | return $self->_collective_do('drop', $s_table_name, $hr_params->{constraints}); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 80 | ▶ | sub create_table_column { | ||||||||||||||||||||||||||
|
Subroutine
create_table_column: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 80 | my ($self, $s_table_name, $hr_column_params, $ar_constraint_params) = @_; | |||||||||||||||||||||||||||
| 149 | 80 | ▶ | if ($hr_column_params->{primarykey} && $hr_column_params->{primarykey} != 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$hr_column_params->{"primarykey"} and $hr_column_params->{"primarykey"} != 1
MC/DC100%2/2
$hr_column_params->{"primarykey"} and $hr_column_params->{"primarykey"} != 1
$hr_column_params->{"primarykey"}
$hr_column_params->{"primarykey"} != 1
Truth table100%3/3
A and B
A= $hr_column_params->{"primarykey"}B= $hr_column_params->{"primarykey"} != 1
| |||||||||||||||||||||||||||||
| 150 | 1 | return push @{$ar_constraint_params}, | |||||||||||||||||||||||||||
| 151 | 1 | $self->_obj_primarykey->add($s_table_name, $hr_column_params->{primarykey}); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 79 | push @{$ar_constraint_params}, | |||||||||||||||||||||||||||
| 154 | $self->_uniques_to_add({ | ||||||||||||||||||||||||||||
| 155 | unique => $hr_column_params->{unique}, | ||||||||||||||||||||||||||||
| 156 | table => $s_table_name, | ||||||||||||||||||||||||||||
| 157 | column => $hr_column_params->{name}, | ||||||||||||||||||||||||||||
| 158 | 79 | }); | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 79 | my $o_foreign_key = $self->_obj_foreignkey; | |||||||||||||||||||||||||||
| 161 | 79 | ▶ | if ($hr_column_params->{foreignkey}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 17 | $hr_column_params->{foreignkey}->{column} = $hr_column_params->{name}; | |||||||||||||||||||||||||||
| 163 | 17 | $hr_column_params->{foreignkey}->{table} = $s_table_name; | |||||||||||||||||||||||||||
| 164 | $hr_column_params->{foreignkey}->{schema} = $hr_column_params->{schema} | ||||||||||||||||||||||||||||
| 165 | 17 | ▶ | if $hr_column_params->{schema}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 17 | push @{$ar_constraint_params}, | |||||||||||||||||||||||||||
| 167 | 17 | $o_foreign_key->for_table(1)->add($hr_column_params->{foreignkey}); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 79 | my $consts = $self->converter->constraints; | |||||||||||||||||||||||||||
| 171 | 79 | ▶ | my $not_null = ($hr_column_params->{notnull}) ? $consts->{not_null} : q~~; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 79 | ▶ | my $primarykey = (defined $hr_column_params->{primarykey}) ? $consts->{primary_key} : q~~; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 79 | $o_foreign_key->for_table(0); | |||||||||||||||||||||||||||
| 174 | 79 | return qq~$not_null $primarykey~; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 79 | ▶ | sub _uniques_to_add { | ||||||||||||||||||||||||||
|
Subroutine
_uniques_to_add: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 178 | 79 | my ($self, $hr_unique_params) = @_; | |||||||||||||||||||||||||||
| 179 | 79 | ▶ | return () if !$hr_unique_params->{unique}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 1 | require Ref::Util; | |||||||||||||||||||||||||||
| 181 | my $hr_to_add | ||||||||||||||||||||||||||||
| 182 | = Ref::Util::is_hashref($hr_unique_params->{unique}) | ||||||||||||||||||||||||||||
| 183 | ? $hr_unique_params->{unique} | ||||||||||||||||||||||||||||
| 184 | 1 | ▶ | : {columns => [$hr_unique_params->{column}]}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 1 | return $self->_obj_unique->add($hr_unique_params->{table}, $hr_to_add); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 6 | 1; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||