Fey/FK.pm

filter: Statementstmt 86.9% 107 / 123 Branchbran 100% 16 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 91.6% 22 / 24 Podpod 75.0% 3 / 4 totaltotal 88.9% 153 / 172 SCAR 25.0
CC
12
Cov
89%
BUILD
5.0
has_column
5.0
has_tables
4.0
CRAP 12.2
« Fey/SQL/Insert.pm ↑ Summary Fey/Role/SetOperation.pm »
1 package Fey::FK;
2  
3 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 22 our $VERSION = '0.43';
8  
9 22 ▶use Fey::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use Fey::Exceptions qw(param_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Fey::Types qw( ArrayRef ArrayRefOfColumns Bool Column TableOrName );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use List::AllUtils qw( max uniq all pairwise );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 22 ▶use Moose 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 22 ▶use MooseX::Params::Validate 0.21 qw( pos_validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 22 ▶use MooseX::SemiAffordanceAccessor 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 22 ▶use MooseX::StrictConstructor 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 22 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 22 has 'id' => (
22 is => 'ro',
23 lazy_build => 1,
24 init_arg => undef,
25 );
26  
27 22 has [qw( source_columns target_columns )] => (
28 is => 'ro',
29 isa => ArrayRefOfColumns,
30 required => 1,
31 coerce => 1,
32 );
33  
34 22 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 22 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 22 has is_self_referential => (
52 is => 'ro',
53 isa => Bool,
54 lazy_build => 1,
55 init_arg => 1,
56 );
57  
58 sub BUILD {
59 130 ▶ my $self = shift;
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
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
BranchTrueFalseTotal
unless (@source == @target) 1 129 130
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
BranchTrueFalseTotal
if (grep {not $_->table;} @source, @target) 1 128 129
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
BranchTrueFalseTotal
if (&uniq(map({$_->table;} @{$array;})) > 1) 1 255 256
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 sub _build_id {
89 125 ▶ my $self = shift;
Subroutine _build_id: called CC=1 SCAR=0.0
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 sub _build_column_pairs {
100 1 ▶ my $self = shift;
Subroutine _build_column_pairs: called CC=1 SCAR=0.0
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 sub _build_source_table {
109 125 ▶ my $self = shift;
Subroutine _build_source_table: called CC=1 SCAR=0.0
110  
111 125 return $self->source_columns()->[0]->table();
112 }
113  
114 sub _build_target_table {
115 125 ▶ my $self = shift;
Subroutine _build_target_table: called CC=1 SCAR=0.0
116  
117 125 return $self->target_columns()->[0]->table();
118 }
119  
120 sub has_tables {
121 15 ▶ my $self = shift;
Subroutine has_tables: called CC=4 SCAR=13.9
Pod documented
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
BranchTrueFalseTotal
builtin::blessed($table1) ? : 2 13 15
130 15 ▶ my $name2 = blessed $table2 ? $table2->name() : $table2;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($table2) ? : 2 13 15
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]
!ll&&!rl&&r
519
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]
ABresult
0X0
100
111
138 }
139  
140 sub has_column {
141 3 ▶ my $self = shift;
Subroutine has_column: called CC=5 SCAR=16.1
Pod documented
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
BranchTrueFalseTotal
if ($self->$table_meth->name eq $table_name) 2 4 6
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
BranchTrueFalseTotal
unless @cols 1 2 3
156  
157 2 my $col_name = $col->name();
158  
159 2 ▶ return 1 if grep { $_->name() eq $col_name } @cols;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->name eq $col_name;} @cols 1 1 2
160  
161 1 return 0;
162 }
163  
164 sub _build_is_self_referential {
165 2 ▶ my $self = shift;
Subroutine _build_is_self_referential: called CC=1 SCAR=0.0
166  
167 2 return $self->source_table()->name() eq $self->target_table()->name();
168 }
169  
170 sub pretty_print {
171 0 ▶ my $self = shift;
Subroutine pretty_print: not called CC=1 SCAR=6.9
Pod documented
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 pairwise {
196 0 ▶ sprintf(
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 22 __PACKAGE__->meta()->make_immutable();
209  
210 22 1;
211  
212 # ABSTRACT: Represents a foreign key
213  
214 __END__
215  
216 =pod
217  
218 =head1 NAME
219  
220 Fey::FK - Represents a foreign key
221  
222 =head1 VERSION
223  
224 version 0.43
225  
226 =head1 SYNOPSIS
227  
228 my $fk = Fey::FK->new( source => $user_id_from_user_table,
229 target => $user_id_from_department_table,
230 );
231  
232 =head1 DESCRIPTION
233  
234 This class represents a foreign key, connecting one or more columns in
235 one table to columns in another table.
236  
237 =head1 METHODS
238  
239 This class provides the following methods:
240  
241 =head2 Fey::FK->new()
242  
243 This method constructs a new C<Fey::FK> object. It takes the following
244 parameters:
245  
246 =over 4
247  
248 =item * source_columns - required
249  
250 =item * target_columns - required
251  
252 These parameters must be either a single C<Fey::Column> object or an
253 array reference containing one or more column objects.
254  
255 The number of columns for the source and target must be the same.
256  
257 =back
258  
259 =head2 $fk->source_table()
260  
261 =head2 $fk->target_table()
262  
263 Returns the appropriate C<Fey::Table> object.
264  
265 =head2 $fk->source_columns()
266  
267 =head2 $fk->target_columns()
268  
269 Returns the appropriate list of C<Fey::Column> objects as an array
270 reference.
271  
272 =head2 $fk->column_pairs()
273  
274 Returns an array reference. Each element of this reference is in turn
275 a two-element array reference of C<Fey::Column> objects, one from the
276 source table and one from the target.
277  
278 =head2 $fk->has_tables( $table1, $table2 )
279  
280 This method returns true if the foreign key includes both of the
281 specified tables. The tables can be specified by name or as
282 C<Fey::Table> objects.
283  
284 =head2 $fk->has_column($column)
285  
286 Given a C<Fey::Column> object, this method returns true if the foreign
287 key includes the specified column.
288  
289 =head2 $fk->is_self_referential()
290  
291 This returns true if the the source and target tables for the foreign
292 key are the same table.
293  
294 =head2 $fk->pretty_print()
295  
296 Returns a stringified representation of the foreign key in a pretty
297 layout something like this:
298  
299 User Message
300 ------- -------
301 user_id user_id
302  
303 =head1 BUGS
304  
305 See L<Fey> for details on how to report bugs.
306  
307 =head1 AUTHOR
308  
309 Dave Rolsky <autarch@urth.org>
310  
311 =head1 COPYRIGHT AND LICENSE
312  
313 This software is Copyright (c) 2011 - 2015 by Dave Rolsky.
314  
315 This is free software, licensed under:
316  
317 The Artistic License 2.0 (GPL Compatible)
318  
319 =cut
« Fey/SQL/Insert.pm ↑ Summary Fey/Role/SetOperation.pm »