Sims/Source.pm

filter: Statementstmt 100% 96 / 96 Branchbran 100% 8 / 8 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 23 / 23 Podpod 55.5% 10 / 18 totaltotal 94.5% 140 / 148 CC 15 SCAR 12.2
CC
15
Cov
100%
initialize
9.0
columns_not_in_parent_relationships
3.0
find_inverse_relationships
3.0
CRAP 3.4
« Sims/Util.pm ↑ Summary Sims/Random.pm »
1 # This class exists to encapsulate the DBIx::Class::Source object and provide
2 # Sims-specific functionality to navigate sources and the attributes of
3 # sources.
4  
5 package DBIx::Class::Sims::Source;
6  
7 54 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 54 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 54 ▶use DDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 54 ▶use DBIx::Class::Sims::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 54 ▶use DBIx::Class::Sims::Relationship;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Requires the following attributes:
17 # * name
18 # * runner
19 814 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
20 814 my $class = shift;
21 814 my $self = bless {@_}, $class;
22 814 $self->initialize;
23 814 return $self;
24 }
25  
26 814 ▶sub initialize {
Subroutine initialize: called CC=9 SCAR=22.0
Pod undocumented
27 814 my $self = shift;
28  
29 # Do this first so all the other methods work properly.
30 814 $self->{source} = $self->schema->source($self->name);
31  
32 814 $self->{columns} = {};
33 814 foreach my $col_name ( $self->source->columns ) {
34 1873 my $c = DBIx::Class::Sims::Column->new(
35 source => $self,
36 name => $col_name,
37 info => $self->source->column_info($col_name),
38 );
39 1873 $self->{columns}{$col_name} = $c;
40 }
41  
42 814 foreach my $col ( $self->source->primary_columns ) {
43 814 $self->{columns}{$col}->in_pk;
44 }
45  
46 814 $self->{uniques} = {};
47 814 foreach my $uk ( $self->source->unique_constraint_names ) {
48 866 $self->{uniques}{$uk} = [];
49 866 foreach my $col ( $self->source->unique_constraint_columns($uk) ) {
50 891 push @{$self->{uniques}{$uk}}, $self->{columns}{$col};
51 891 $self->{columns}{$col}->in_uk($uk);
52 }
53 }
54  
55 814 $self->{relationships} = {};
56 814 ▶ my $constraints = delete($self->{constraints}) // {};
Condition100%2/2
delete $self->{"constraints"} // {}
l!l
9805
MC/DC100%1/1
delete $self->{"constraints"} // {}
delete $self->{"constraints"}
Truth table100%2/2
A
A= delete $self->{"constraints"}
Aresult
11
00
57 814 foreach my $rel_name ( $self->source->relationships ) {
58 my $r = DBIx::Class::Sims::Relationship->new(
59 source => $self,
60 name => $rel_name,
61 info => $self->source->relationship_info($rel_name),
62 303 constraints => $constraints->{$rel_name},
63 );
64 303 $self->{relationships}{$rel_name} = $r;
65  
66 303 ▶ if ($r->is_fk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->is_fk) 152 151 303
67 152 $self->{columns}{$_}->in_fk($r) for $r->self_fk_cols;
68 }
69 }
70  
71 814 return;
72 }
73  
74 6236 ▶sub name { $_[0]{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
75 6047 ▶sub runner { $_[0]{runner} }
Subroutine runner: called CC=1 SCAR=0.0
Pod undocumented
76 6492 ▶sub source { $_[0]{source} }
Subroutine source: called CC=1 SCAR=0.0
Pod documented
77  
78 955 ▶sub columns {
Subroutine columns: called CC=1 SCAR=0.0
Pod documented
79 955 my $self = shift;
80 955 return sort { $a->name cmp $b->name } values %{$self->{columns}};
81 }
82 1197 ▶sub column {
Subroutine column: called CC=2 SCAR=6.9
Pod documented
83 1197 my $self = shift;
84 1197 ▶ return unless exists $self->{columns}{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"columns"}{$_[0]} 6 1191 1197
85 1191 return $self->{columns}{$_[0]};
86 }
87  
88 808 ▶sub columns_not_in_parent_relationships {
Subroutine columns_not_in_parent_relationships: called CC=3 SCAR=11.0
Pod documented
89 808 my $self = shift;
90  
91 808 my %c = map { $_->name => $_ } $self->columns;
92 808 foreach my $r ( $self->parent_relationships ) {
93 146 delete $c{$_} for $r->self_fk_cols;
94 }
95  
96 808 return sort { $a->name cmp $b->name } values %c;
97 }
98  
99 3238 ▶sub relationships {
Subroutine relationships: called CC=1 SCAR=0.0
Pod documented
100 3238 my $self = shift;
101 3238 return values %{$self->{relationships}};
102 }
103 874 ▶sub relationship {
Subroutine relationship: called CC=2 SCAR=6.9
Pod documented
104 874 my $self = shift;
105 874 ▶ return unless exists $self->{relationships}{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"relationships"}{$_[0]} 801 73 874
106 73 return $self->{relationships}{$_[0]};
107 }
108  
109 2344 ▶sub uniques {
Subroutine uniques: called CC=1 SCAR=0.0
Pod undocumented
110 2344 my $self = shift;
111 2344 return values %{$self->{uniques}};
112 }
113  
114 2420 ▶sub parent_relationships {
Subroutine parent_relationships: called CC=1 SCAR=0.0
Pod documented
115 2420 my $self = shift;
116  
117 2420 return sort { $a->name cmp $b->name } grep { $_->is_fk } $self->relationships;
118 }
119  
120 762 ▶sub child_relationships {
Subroutine child_relationships: called CC=1 SCAR=0.0
Pod documented
121 762 my $self = shift;
122  
123 762 return sort { $a->name cmp $b->name } grep { !$_->is_fk } $self->relationships;
124 }
125  
126 4000 ▶sub schema {
Subroutine schema: called CC=1 SCAR=0.0
Pod undocumented
127 4000 my $self = shift;
128 4000 return $self->runner->schema;
129 }
130 3184 ▶sub resultset {
Subroutine resultset: called CC=1 SCAR=0.0
Pod documented
131 3184 my $self = shift;
132 3184 return $self->schema->resultset($self->name);
133 }
134  
135 93 ▶sub unique_columns {
Subroutine unique_columns: called CC=1 SCAR=0.0
Pod undocumented
136 93 my $self = shift;
137 return map {
138 93 [ $self->source->unique_constraint_columns($_) ]
139 } $self->source->unique_constraint_names();
140 }
141  
142 ################################################################################
143 # There are no tests for the methods below here to the end of the file.
144  
145 # This is used to determine if additional constraints need to be added when
146 # looking for a parent row that already exists. The use of this method needs to
147 # be upgraded to optionally throw an error if the parent found would not meet
148 # the other requirements on the parent.
149 93 ▶sub unique_constraints_containing {
Subroutine unique_constraints_containing: called CC=1 SCAR=0.0
Pod undocumented
150 93 my $self = shift;
151 93 my ($column) = @_;
152  
153 # Only return true if the unique constraint is solely built from the column.
154 # When we handle multi-column relationships, then we will need to handle the
155 # situation where the relationship's columns are the UK.
156 #
157 # The situation where the UK has multiple columns, one of which is the the FK,
158 # is potentially undecideable.
159 return grep {
160 93 my $col_def = $_;
161 101 ! grep { $column ne $_ } @$col_def
162 } $self->unique_columns;
163 }
164  
165 # TODO: This should probably be cached. Maybe even pre-generated.
166 54 ▶sub find_inverse_relationships {
Subroutine find_inverse_relationships: called CC=3 SCAR=11.0
Pod undocumented
167 54 my $self = shift;
168 54 my ($fksource, $fkcol) = @_;
169  
170 54 my @inverses;
171 54 foreach my $r ( $fksource->relationships ) {
172  
173 # Skip relationships that aren't back towards the table we're coming from.
174 # TODO: ::Relationship should connect to both ::Source's (source / target)
175 68 ▶ next unless $r->short_fk_source eq $self->name;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->short_fk_source eq $self->name 14 54 68
176  
177 # Assumption: We don't need to verify the $fkcol because there shouldn't be
178 # multiple relationships on different columns between the same tables. This
179 # is likely to be violated, but only by badly-designed schemas.
180  
181 54 push @inverses, {
182 rel => $r->name,
183 col => $r->foreign_fk_col,
184 };
185 }
186  
187 54 return @inverses;
188 }
189  
190 54 1;
191 __END__
« Sims/Util.pm ↑ Summary Sims/Random.pm »