| 1 | package Alzabo::Runtime::ForeignKey; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use vars qw( $VERSION %DELETED ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Alzabo::Runtime; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Alzabo::Exceptions ( abbr => 'params_exception' ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Params::Validate qw( validate ARRAYREF OBJECT ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | Params::Validate::validation_options | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | ( on_fail => sub { params_exception join '', @_ } ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use base qw(Alzabo::ForeignKey); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 11 | $VERSION = 2.0; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # FIXME - needs docs | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | sub new | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | my $proto = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | validate( @_, { columns_from => { type => ARRAYREF | OBJECT }, | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | columns_to => { type => ARRAYREF | OBJECT }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | } ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my %p = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # XXX - needs a little more validation, like that both "sides" | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # have the same number of columns | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $self->{columns_from} = $p{columns_from}; | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | $self->{columns_to} = $p{columns_to}; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | sub register_insert | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register_insert: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | shift->_insert_or_update( 'insert', @_ ); | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | sub register_update | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register_update: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | shift->_insert_or_update( 'update', @_ ); | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | sub _insert_or_update | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_insert_or_update: not called CC=14 SCAR=53.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $type = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | my %vals = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $driver = $self->table_from->schema->driver; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my @one_to_one_where; | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | my @one_to_one_vals; | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $has_nulls = grep { ! defined } values %vals; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | foreach my $pair ( $self->column_pairs ) | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # if we're inserting into a table we don't check if its primary | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # key exists elsewhere, no matter what the cardinality of the | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # relation. Otherwise, we end up in cycles where it is impossible | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # to insert things into the table. | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | next if $type eq 'insert' && $pair->[0]->is_primary_key; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$type eq "insert" and $pair->[0]->is_primary_key
MC/DC0%0/2
$type eq "insert" and $pair->[0]->is_primary_key
$type eq "insert"
$pair->[0]->is_primary_key
Truth table0%0/3
A and B
A= $type eq "insert"B= $pair->[0]->is_primary_key
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # A table is always allowed to make updates to its own primary | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # key columns ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( ( $type eq 'update' || $pair->[1]->is_primary_key ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$type eq "update" || $pair->[1]->is_primary_key and not $pair->[0]->is_primary_key
$type eq "update" or $pair->[1]->is_primary_key
MC/DC0%0/3
$type eq "update" || $pair->[1]->is_primary_key and not $pair->[0]->is_primary_key
$type eq "update"
$pair->[1]->is_primary_key
not $pair->[0]->is_primary_key
Truth table0%0/5
A or B and not(C)
A= $type eq "update"B= $pair->[1]->is_primary_keyC= not $pair->[0]->is_primary_key
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | && ! $pair->[0]->is_primary_key ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | $self->_check_existence( $pair->[1] => $vals{ $pair->[0]->name } ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if defined $vals{ $pair->[0]->name }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # Except when the PK has a one-to-one relationship to some | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # other table, and the update would cause a duplication in the | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # other table. | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( $self->is_one_to_one && ! $has_nulls ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->is_one_to_one and not $has_nulls
MC/DC0%0/2
$self->is_one_to_one and not $has_nulls
$self->is_one_to_one
not $has_nulls
Truth table0%0/3
A and not(B)
A= $self->is_one_to_oneB= not $has_nulls
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | push @one_to_one_where, [ $pair->[0], '=', $vals{ $pair->[0]->name } ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | push @one_to_one_vals, $pair->[0]->name . ' = ' . $vals{ $pair->[0]->name }; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ( $self->is_one_to_one && ! $has_nulls ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->is_one_to_one and not $has_nulls
MC/DC0%0/2
$self->is_one_to_one and not $has_nulls
$self->is_one_to_one
not $has_nulls
Truth table0%0/3
A and not(B)
A= $self->is_one_to_oneB= not $has_nulls
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( @one_to_one_where && | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@one_to_one_where and $self->table_from->row_count("where", \@one_to_one_where)
MC/DC0%0/2
@one_to_one_where and $self->table_from->row_count("where", \@one_to_one_where)
@one_to_one_where
$self->table_from->row_count("where", \@one_to_one_where)
Truth table0%0/3
A and B
A= @one_to_one_whereB= $self->table_from->row_count("where", \@one_to_one_where)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | $self->table_from->row_count( where => \@one_to_one_where ) ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $err = '(' . (join ', ', @one_to_one_vals) . ') already exists in the ' . $self->table_from->name . ' table'; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | Alzabo::Exception::ReferentialIntegrity->throw( error => $err ); | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | sub _check_existence | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_existence: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my ($col, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | unless ( $self->table_to->row_count( where => [ $col, '=', $val ] ) ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | Alzabo::Exception::ReferentialIntegrity->throw( error => 'Foreign key must exist in foreign table. No rows in ' . $self->table_to->name . ' where ' . $col->name . " = $val" ); | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | sub register_delete | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register_delete: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $row = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my @update = grep { $_->nullable } $self->columns_to; | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return unless $self->to_is_dependent || @update; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->to_is_dependent or @update
MC/DC0%0/2
$self->to_is_dependent or @update
$self->to_is_dependent
@update
Truth table0%0/3
A or B
A= $self->to_is_dependentB= @update
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # Find the rows in the other table that are related to the row | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # being deleted. | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | my @where = map { [ $_->[1], '=', $row->select( $_->[0]->name ) ] } $self->column_pairs; | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $cursor = $self->table_to->rows_where( where => \@where ); | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | while ( my $related_row = $cursor->next ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # This is a class variable so that multiple foreign key | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # objects don't try to delete the same rows | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | next if $DELETED{ $related_row->id_as_string }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ($self->to_is_dependent) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | local %DELETED = %DELETED; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $DELETED{ $related_row->id_as_string } = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | # dependent relationship so delete other row (may begin a | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | # chain reaction!) | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $related_row->delete; | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | elsif (@update) | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # not dependent so set the column(s) to null | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | $related_row->update( map { $_->name => undef } @update ); | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||