Fey/SQL/Fragment/Join.pm

filter: Statementstmt 86.4% 89 / 103 Branchbran 53.8% 14 / 26 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 5 totaltotal 76.0% 124 / 163 CC 17 SCAR 12.2
CC
17
Cov
79%
_join_one_table
12.0
id
5.2
sql_with_alias
4.1
CRAP 3.4
« Fey/Table.pm ↑ Summary Fey/Role/SQL/Cloneable.pm »
1 package Fey::SQL::Fragment::Join;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 our $VERSION = '0.44';
8  
9 27 ▶use Fey::Exceptions qw( param_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use Fey::FakeDBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use Fey::Types qw( FK OuterJoinType Table WhereClause );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use List::AllUtils qw( pairwise );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 27 ▶use Moose 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 27 has '_table1' => (
17 is => 'ro',
18 does => 'Fey::Role::TableLike',
19 required => 1,
20 init_arg => 'table1',
21 );
22  
23 27 has '_table2' => (
24 is => 'ro',
25 does => 'Fey::Role::TableLike',
26 predicate => '_has_table2',
27 init_arg => 'table2',
28 );
29  
30 27 has '_fk' => (
31 is => 'ro',
32 isa => FK,
33 init_arg => 'fk',
34 predicate => '_has_fk',
35 );
36  
37 27 has '_outer_type' => (
38 is => 'ro',
39 isa => OuterJoinType,
40 predicate => '_has_outer_type',
41 init_arg => 'outer_type',
42 );
43  
44 27 has '_where' => (
45 is => 'ro',
46 isa => WhereClause,
47 predicate => '_has_where',
48 init_arg => 'where',
49 );
50  
51 57 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.4
Pod undocumented
52 57 my $self = shift;
53  
54 57 ▶ param_error 'You cannot join two tables without a foreign key'
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_table2 and not $self->_has_fk 0 57 57
Condition66%2/3
$self->_has_table2 and not $self->_has_fk
!ll&&!rl&&r
5610
MC/DC0%0/2
$self->_has_table2 and not $self->_has_fk
$self->_has_table2 not $self->_has_fk
Truth table66%2/3
A and not(B)
A= $self->_has_table2B= not $self->_has_fk
ABresult
0X0
100
111
55 if $self->_has_table2() && !$self->_has_fk();
56  
57 57 return;
58 }
59  
60 57 ▶sub id {
Subroutine id: called CC=5 SCAR=16.6
Pod undocumented
61 57 my $self = shift;
62  
63 # This is a rather special case, and handling it separately makes
64 # the rest of this method simpler.
65 57 ▶ return $self->_table1()->id()
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_table2 56 1 57
66 unless $self->_has_table2();
67  
68 1 my @tables = $self->tables();
69 0 ▶ @tables = sort { $a->name() cmp $b->name() } @tables
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_left_or_right_outer_join 0 1 1
70 unless $self->_is_left_or_right_outer_join();
71  
72 1 my @outer;
73 1 ▶ @outer = $self->_outer_type() if $self->_has_outer_type();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_outer_type 1 0 1
74  
75 1 my @where;
76 1 ▶ @where = $self->_where()->where_clause( 'Fey::FakeDBI', 'no WHERE' )
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_where 1 0 1
77 if $self->_has_where();
78  
79 return (
80 join "\0",
81 @outer,
82 1 ( map { $_->id() } @tables ),
83 $self->_fk()->id(),
84 @where,
85 );
86 }
87  
88 1 ▶sub _is_left_or_right_outer_join {
Subroutine _is_left_or_right_outer_join: called CC=2 SCAR=8.1
89 1 my $self = shift;
90  
91 1 ▶ return $self->_has_outer_type()
Condition33%1/3
$self->_has_outer_type && $self->_outer_type =~ /^(?:right|left)$/
!ll&&!rl&&r
001
MC/DC0%0/2
$self->_has_outer_type && $self->_outer_type =~ /^(?:right|left)$/
$self->_has_outer_type $self->_outer_type =~ /^(?:right|left)$/
Truth table33%1/3
A && B
A= $self->_has_outer_typeB= $self->_outer_type =~ /^(?:right|left)$/
ABresult
0X0
100
111
92 && $self->_outer_type() =~ /^(?:right|left)$/;
93 }
94  
95 141 ▶sub tables {
Subroutine tables: called CC=1 SCAR=0.0
Pod undocumented
96 141 my $self = shift;
97  
98 141 return grep {defined} ( $self->_table1(), $self->_table2() );
99 }
100  
101 70 ▶sub sql_with_alias {
Subroutine sql_with_alias: called CC=4 SCAR=14.1
Pod undocumented
102 70 my $self = shift;
103 70 my $dbh = shift;
104 70 my $joined_ids = shift;
105  
106 my @unseen_tables
107 70 = grep { !$joined_ids->{ $_->id() } } $self->tables();
108  
109 # This can happen in the case where we have just one table, and
110 # that table is participating in some other join.
111 70 ▶ return '' unless @unseen_tables;
Branch: 1 branch
BranchTrueFalseTotal
unless @unseen_tables 0 70 70
112  
113 70 ▶ return $self->_table1()->sql_with_alias($dbh)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_table2 69 1 70
114 unless $self->_has_table2();
115  
116 1 ▶ if ( @unseen_tables == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unseen_tables == 1) { } 0 1 1
117 0 return $self->_join_one_table( $dbh, @unseen_tables );
118 }
119 else {
120 1 return $self->_join_both_tables($dbh);
121 }
122 }
123  
124 # This could produce gibberish for an OUTER JOIN, but that would mean
125 # that the query is fundamentally wrong anyway (since you can't OUTER
126 # JOIN a table you've already joined with a normal join previously).
127 0 ▶sub _join_one_table {
Subroutine _join_one_table: not called CC=3 SCAR=24.8
128 0 my $self = shift;
129 0 my $dbh = shift;
130 0 my $unseen_table = shift;
131  
132 0 my $join = '';
133  
134 0 ▶ $join .= uc $self->_outer_type() . ' OUTER'
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_outer_type 0 0 0
135 if $self->_has_outer_type();
136  
137 0 ▶ $join .= q{ } if length $join;
Branch: 1 branch
BranchTrueFalseTotal
if length $join 0 0 0
138 0 $join .= 'JOIN ';
139 0 $join .= $unseen_table->sql_with_alias($dbh);
140  
141 0 $join .= $self->_on_clause($dbh);
142 0 $join .= $self->_where_clause($dbh);
143 0 $join .= ')';
144  
145 0 return $join;
146 }
147  
148 1 ▶sub _join_both_tables {
Subroutine _join_both_tables: called CC=2 SCAR=6.9
149 1 my $self = shift;
150 1 my $dbh = shift;
151  
152 1 my $join = $self->_table1()->sql_with_alias($dbh);
153  
154 1 ▶ $join .= q{ } . uc $self->_outer_type() . ' OUTER'
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_outer_type 1 0 1
155 if $self->_has_outer_type();
156  
157 1 $join .= ' JOIN ';
158 1 $join .= $self->_table2()->sql_with_alias($dbh);
159  
160 1 $join .= $self->_on_clause($dbh);
161 1 $join .= $self->_where_clause($dbh);
162 1 $join .= ')';
163  
164 1 return $join;
165 }
166  
167 1 ▶sub _on_clause {
Subroutine _on_clause: called CC=2 SCAR=6.9
168 1 my $self = shift;
169 1 my $dbh = shift;
170  
171 1 my $on .= ' ON (';
172  
173 1 my @s = @{ $self->_fk()->source_columns() };
174 1 my @t = @{ $self->_fk()->target_columns() };
175  
176 1 ▶ for my $p ( pairwise { [ $a, $b ] } @s, @t ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
177 1 $on .= $p->[0]->sql_or_alias($dbh);
178 1 $on .= ' = ';
179 1 $on .= $p->[1]->sql_or_alias($dbh);
180 }
181  
182 1 return $on;
183 }
184  
185 1 ▶sub _where_clause {
Subroutine _where_clause: called CC=2 SCAR=7.0
186 1 my $self = shift;
187 1 my $dbh = shift;
188  
189 1 ▶ return '' unless $self->_has_where();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_where 0 1 1
190  
191 1 return ' AND ' . $self->_where()->where_clause( $dbh, 'no WHERE' );
192 }
193  
194 16 ▶sub bind_params {
Subroutine bind_params: called CC=2 SCAR=6.9
Pod undocumented
195 16 my $self = shift;
196  
197 16 ▶ return unless $self->_has_where();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_where 15 1 16
198  
199 1 return $self->_where()->bind_params();
200 }
201  
202 27 __PACKAGE__->meta()->make_immutable();
203  
204 27 1;
205  
206 # ABSTRACT: Represents a single join in a FROM clause
207  
208 __END__
« Fey/Table.pm ↑ Summary Fey/Role/SQL/Cloneable.pm »