| 1 | package Fey::FK; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 23 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 23 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 23 | ▶ | use namespace::autoclean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 23 | our $VERSION = '0.44'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 23 | ▶ | use Fey::Column; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 23 | ▶ | use Fey::Exceptions qw(param_error); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 23 | ▶ | use Fey::Types qw( ArrayRef ArrayRefOfColumns Bool Column TableOrName ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 23 | ▶ | use List::AllUtils qw( max uniq all pairwise ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 23 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 23 | ▶ | use Moose 2.1200; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 23 | ▶ | use MooseX::Params::Validate 0.21 qw( pos_validated_list ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 23 | ▶ | use MooseX::SemiAffordanceAccessor 0.03; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 23 | ▶ | use MooseX::StrictConstructor 0.13; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 23 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 23 | has 'id' => ( | |||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||
| 23 | lazy_build => 1, | ||||||||||||||||||||
| 24 | init_arg => undef, | ||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 23 | has [qw( source_columns target_columns )] => ( | |||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||
| 29 | isa => ArrayRefOfColumns, | ||||||||||||||||||||
| 30 | required => 1, | ||||||||||||||||||||
| 31 | coerce => 1, | ||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 23 | has [qw( source_table target_table )] => ( | |||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||
| 36 | does => 'Fey::Role::TableLike', | ||||||||||||||||||||
| 37 | lazy_build => 1, | ||||||||||||||||||||
| 38 | init_arg => undef, | ||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 23 | has column_pairs => ( | |||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | # really, the inner array refs must always contain 2 columns, | ||||||||||||||||||||
| 45 | # but we don't have structured constraints quite yet. | ||||||||||||||||||||
| 46 | isa => ArrayRef [ ArrayRef [Column] ], | ||||||||||||||||||||
| 47 | lazy_build => 1, | ||||||||||||||||||||
| 48 | init_arg => undef, | ||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 23 | has is_self_referential => ( | |||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||
| 53 | isa => Bool, | ||||||||||||||||||||
| 54 | lazy_build => 1, | ||||||||||||||||||||
| 55 | init_arg => 1, | ||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 130 | ▶ | sub BUILD { | ||||||||||||||||||
|
Subroutine
BUILD: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 59 | 130 | my $self = shift; | |||||||||||||||||||
| 60 | 130 | my $p = shift; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 130 | my @source = @{ $self->source_columns() }; | |||||||||||||||||||
| 63 | 130 | my @target = @{ $self->target_columns() }; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 130 | ▶ | unless ( @source == @target ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 1 | param_error( | |||||||||||||||||||
| 67 | "The source and target arrays passed to add_foreign_key()" | ||||||||||||||||||||
| 68 | . " must contain the same number of columns." ); | ||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 129 | ▶ | if ( grep { !$_->table() } @source, @target ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 1 | param_error | |||||||||||||||||||
| 73 | "All columns passed to add_foreign_key() must have a table."; | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 128 | for my $p ( [ source => \@source ], [ target => \@target ] ) { | |||||||||||||||||||
| 77 | 256 | my ( $name, $array ) = @{$p}; | |||||||||||||||||||
| 78 | 256 | ▶ | if ( uniq( map { $_->table() } @{$array} ) > 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 1 | param_error( | |||||||||||||||||||
| 80 | "Each column in the $name argument to add_foreign_key()" | ||||||||||||||||||||
| 81 | . " must come from the same table." ); | ||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 127 | return; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 125 | ▶ | sub _build_id { | ||||||||||||||||||
|
Subroutine
_build_id: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 89 | 125 | my $self = shift; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | return join "\0", | ||||||||||||||||||||
| 92 | ( | ||||||||||||||||||||
| 93 | sort | ||||||||||||||||||||
| 94 | 250 | map { $_->table()->name() . q{.} . $_->name() } | |||||||||||||||||||
| 95 | 125 | @{ $self->source_columns() }, @{ $self->target_columns() } | |||||||||||||||||||
| 96 | ); | ||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 1 | ▶ | sub _build_column_pairs { | ||||||||||||||||||
|
Subroutine
_build_column_pairs: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 100 | 1 | my $self = shift; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 1 | my @s = @{ $self->source_columns() }; | |||||||||||||||||||
| 103 | 1 | my @t = @{ $self->target_columns() }; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 1 | ▶ | return [ pairwise { [ $a, $b ] } @s, @t ]; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 125 | ▶ | sub _build_source_table { | ||||||||||||||||||
|
Subroutine
_build_source_table: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 109 | 125 | my $self = shift; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 125 | return $self->source_columns()->[0]->table(); | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 125 | ▶ | sub _build_target_table { | ||||||||||||||||||
|
Subroutine
_build_target_table: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 115 | 125 | my $self = shift; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 125 | return $self->target_columns()->[0]->table(); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 15 | ▶ | sub has_tables { | ||||||||||||||||||
|
Subroutine
has_tables: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 121 | 15 | my $self = shift; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 15 | my ( $table1, $table2 ) = pos_validated_list( | |||||||||||||||||||
| 124 | \@_, | ||||||||||||||||||||
| 125 | { isa => TableOrName }, | ||||||||||||||||||||
| 126 | { isa => TableOrName }, | ||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 15 | ▶ | my $name1 = blessed $table1 ? $table1->name() : $table1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 15 | ▶ | my $name2 = blessed $table2 ? $table2->name() : $table2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 15 | my @looking_for = sort $name1, $name2; | |||||||||||||||||||
| 133 | my @have | ||||||||||||||||||||
| 134 | 15 | = sort map { $_->name() } $self->source_table(), | |||||||||||||||||||
| 135 | $self->target_table(); | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 15 | ▶ | return ( $looking_for[0] eq $have[0] && $looking_for[1] eq $have[1] ); | ||||||||||||||||||
|
Condition100%3/3
$looking_for[0] eq $have[0] && $looking_for[1] eq $have[1]
MC/DC100%2/2
$looking_for[0] eq $have[0] && $looking_for[1] eq $have[1]
$looking_for[0] eq $have[0]
$looking_for[1] eq $have[1]
Truth table100%3/3
A && B
A= $looking_for[0] eq $have[0]B= $looking_for[1] eq $have[1]
| |||||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 3 | ▶ | sub has_column { | ||||||||||||||||||
|
Subroutine
has_column: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 141 | 3 | my $self = shift; | |||||||||||||||||||
| 142 | 3 | my ($col) = pos_validated_list( \@_, { isa => Column } ); | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 3 | my $table_name = $col->table()->name(); | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 3 | my @cols; | |||||||||||||||||||
| 147 | 3 | for my $part (qw( source target )) { | |||||||||||||||||||
| 148 | 6 | my $table_meth = $part . '_table'; | |||||||||||||||||||
| 149 | 6 | ▶ | if ( $self->$table_meth()->name() eq $table_name ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | 2 | my $col_meth = $part . '_columns'; | |||||||||||||||||||
| 151 | 2 | @cols = @{ $self->$col_meth() }; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 3 | ▶ | return 0 unless @cols; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 2 | my $col_name = $col->name(); | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 2 | ▶ | return 1 if grep { $_->name() eq $col_name } @cols; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 1 | return 0; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 2 | ▶ | sub _build_is_self_referential { | ||||||||||||||||||
|
Subroutine
_build_is_self_referential: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 165 | 2 | my $self = shift; | |||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 2 | return $self->source_table()->name() eq $self->target_table()->name(); | |||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 0 | ▶ | sub pretty_print { | ||||||||||||||||||
|
Subroutine
pretty_print: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 171 | 0 | my $self = shift; | |||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 0 | my @source_columns = @{ $self->source_columns() }; | |||||||||||||||||||
| 174 | 0 | my @target_columns = @{ $self->target_columns() }; | |||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | my $longest = max | ||||||||||||||||||||
| 177 | 0 | map { length $_->name() } $self->source_table(), | |||||||||||||||||||
| 178 | $self->target_table(), | ||||||||||||||||||||
| 179 | @source_columns, @target_columns; | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 0 | $longest += 2; | |||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 0 | my $string = sprintf( | |||||||||||||||||||
| 184 | "\%-${longest}s \%-${longest}s\n", | ||||||||||||||||||||
| 185 | $self->source_table()->name(), | ||||||||||||||||||||
| 186 | $self->target_table()->name(), | ||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||
| 188 | 0 | $string .= ('-') x $longest; | |||||||||||||||||||
| 189 | 0 | $string .= q{ }; | |||||||||||||||||||
| 190 | 0 | $string .= ('-') x $longest; | |||||||||||||||||||
| 191 | 0 | $string .= "\n"; | |||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | $string .= ( | ||||||||||||||||||||
| 194 | join '', | ||||||||||||||||||||
| 195 | 0 | ▶ | pairwise { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 196 | 0 | sprintf( | |||||||||||||||||||
| 197 | "\%-${longest}s \%-${longest}s\n", | ||||||||||||||||||||
| 198 | $a->name(), $b->name() | ||||||||||||||||||||
| 199 | ); | ||||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | 0 | @source_columns, | |||||||||||||||||||
| 202 | @target_columns | ||||||||||||||||||||
| 203 | ); | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 0 | return $string; | |||||||||||||||||||
| 206 | } | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 23 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 23 | 1; | |||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | # ABSTRACT: Represents a foreign key | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | __END__ | ||||||||||||||||||||