Fey/SQL/Select.pm

filter: Statementstmt 83.1% 153 / 184 Branchbran 52.7% 39 / 74 Conditioncond 38.7% 31 / 80 MC/DCmcdc 9.5% 4 / 42 Subroutinesub 85.3% 35 / 41 Podpod 100% 13 / 13 totaltotal 63.3% 275 / 434 CC 68 SCAR 40.6
CC
68
Cov
66%
from
178.2
from_clause
12.2
__ANON__
12.0
CRAP 58.0
↑ Summary Fey/SQL/Fragment/Where/Comparison.pm »
1 package Fey::SQL::Select;
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::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use Fey::Role::ColumnLike;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use Fey::SQL::Fragment::Join;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use Fey::Types
14 27 ▶ qw( ArrayRef Bool HashRef CanQuote GroupByElement SelectElement );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 27 ▶use List::AllUtils qw( all );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 27 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 27 ▶use Moose 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 27 ▶use MooseX::Params::Validate 0.21 qw( pos_validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 27 ▶use MooseX::SemiAffordanceAccessor 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 27 ▶use MooseX::StrictConstructor 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 27 with 'Fey::Role::Comparable',
24 'Fey::Role::Selectable',
25 'Fey::Role::SQL::HasOrderByClause',
26 'Fey::Role::SQL::HasLimitClause',
27 'Fey::Role::SQL::ReturnsData';
28  
29 27 with 'Fey::Role::SQL::HasWhereClause' => {
30 -excludes => 'bind_params',
31 -alias => { bind_params => '_where_clause_bind_params' },
32 };
33  
34 27 with 'Fey::Role::SQL::HasBindParams' => { -excludes => 'bind_params' };
35  
36 27 with 'Fey::Role::HasAliasName' => {
37 generated_alias_prefix => 'SUBSELECT',
38 sql_needs_parens => 1,
39 };
40  
41 has '_select' => (
42 traits => ['Array'],
43 is => 'bare',
44 isa => ArrayRef,
45 159 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 27 handles => {
47 _add_select_element => 'push',
48 select_clause_elements => 'elements',
49 },
50 init_arg => undef,
51 );
52  
53 27 has 'is_distinct' => (
54 is => 'rw',
55 isa => Bool,
56 default => 0,
57 writer => '_set_is_distinct',
58 );
59  
60 27 has 'is_distinct_on' => (
61 is => 'rw',
62 does => 'Fey::Role::ColumnLike',
63 writer => '_set_is_distinct_on',
64 predicate => '_has_is_distinct_on',
65 );
66  
67 has '_from' => (
68 traits => ['Hash'],
69 is => 'bare',
70 isa => HashRef,
71 159 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 27 handles => {
73 _get_from => 'get',
74 _set_from => 'set',
75 _from_ids => 'keys',
76 },
77 init_arg => undef,
78 );
79  
80 has '_group_by' => (
81 traits => ['Array'],
82 is => 'bare',
83 isa => ArrayRef,
84 159 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 27 handles => {
86 _add_group_by_elements => 'push',
87 _has_group_by_elements => 'count',
88 _group_by => 'elements',
89 },
90 init_arg => undef,
91 );
92  
93 has '_having' => (
94 traits => ['Array'],
95 is => 'bare',
96 isa => ArrayRef,
97 159 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 27 handles => {
99 _add_having_element => 'push',
100 _has_having_elements => 'count',
101 _last_having_element => [ 'get', -1 ],
102 _having => 'elements',
103 },
104 init_arg => undef,
105 );
106  
107 27 with 'Fey::Role::SQL::Cloneable';
108  
109 88 ▶sub select {
Subroutine select: called CC=5 SCAR=16.1
Pod documented
110 88 my $self = shift;
111  
112 88 ▶ my $count = @_ ? @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 88 0 88
113 88 my (@select) = pos_validated_list(
114 \@_,
115 ( ( { isa => SelectElement } ) x $count ),
116 MX_PARAMS_VALIDATE_NO_CACHE => 1,
117 );
118  
119 88 for my $elt (
120 map {
121 $_->can('columns')
122 91 ▶ ? sort { $a->name() cmp $b->name() } $_->columns()
Branch: 1 branch
BranchTrueFalseTotal
$_->can('columns') ? : 22 69 91
123 : $_
124 }
125 91 ▶ map { blessed $_ ? $_ : Fey::Literal->new_from_scalar($_) } @select
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 51 40 91
126 ) {
127 135 $self->_add_select_element($elt);
128 }
129  
130 88 return $self;
131 }
132  
133 1 ▶sub distinct {
Subroutine distinct: called CC=2 SCAR=7.1
Pod documented
134 1 ▶ die 'Cannot call ->distinct and ->distinct_on'
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->_has_is_distinct_on 0 1 1
135 if $_[0]->_has_is_distinct_on();
136  
137 1 $_[0]->_set_is_distinct(1);
138  
139 1 return $_[0];
140 }
141  
142 0 ▶sub distinct_on {
Subroutine distinct_on: not called CC=2 SCAR=17.9
Pod documented
143 0 ▶ die 'Cannot call ->distinct and ->distinct_on'
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->is_distinct 0 0 0
144 if $_[0]->is_distinct();
145  
146 0 $_[0]->_set_is_distinct_on( $_[1] );
147  
148 0 return $_[0];
149 }
150  
151 # XXX - need to handle subselect as if it were a table rather than as
152 # a special case
153 62 ▶sub from {
Subroutine from: called CC=33 SCAR=51.8
Pod documented
154 62 my $self = shift;
155  
156 # $t1, 'LEFT', $t2, ...
157 62 ▶ my $is_outer_join = @_ >= 3 && !blessed $_[1];
Condition66%2/3
@_ >= 3 && !builtin::blessed($_[1])
!ll&&!rl&&r
6101
MC/DC50%1/2
@_ >= 3 && !builtin::blessed($_[1])
@_ >= 3 !builtin::blessed($_[1])
Truth table66%2/3
A && not(B)
A= @_ >= 3B= !builtin::blessed($_[1])
ABresult
0X0
100
111
158  
159 62 ▶ my $is_inner_join = @_ >= 2 && !$is_outer_join;
Condition66%2/3
@_ >= 2 && !$is_outer_join
!ll&&!rl&&r
6110
MC/DC0%0/2
@_ >= 2 && !$is_outer_join
@_ >= 2 !$is_outer_join
Truth table66%2/3
A && not(B)
A= @_ >= 2B= !$is_outer_join
ABresult
0X0
100
111
160  
161 # gee, wouldn't multimethods be nice here?
162  
163 #<<<
164 62 ▶ my $meth = (
Branch: 10 branches
BranchTrueFalseTotal
@_ == 1 && builtin::blessed($_[0]) && $_[0]->can('is_joinable') && $_[0]->is_joinable ? : 56 6 62
@_ == 1 && builtin::blessed($_[0]) && $_[0]->can('does') && $_[0]->does('Fey::Role::SQL::ReturnsData') ? : 5 1 6
$is_inner_join && @_ == 4 && $_[3]->isa('Fey::SQL::Where') ? : 0 1 1
$is_inner_join && @_ == 3 && $_[2]->isa('Fey::SQL::Where') ? : 0 1 1
$is_inner_join && @_ == 3 && $_[2]->isa('Fey::FK') ? : 0 1 1
$is_inner_join && @_ == 2 ? : 0 1 1
$is_outer_join && @_ == 5 ? : 0 1 1
$is_outer_join && @_ == 4 && $_[3]->isa('Fey::SQL::Where') ? : 1 0 1
$is_outer_join && @_ == 4 && $_[3]->isa('Fey::FK') ? : 0 0 0
$is_outer_join && @_ == 3 ? : 0 0 0
Condition40%23/57
@_ == 1 and builtin::blessed($_[0]) and $_[0]->can("is_joinable") and $_[0]->is_joinable
!ll&&!rl&&r
6056
@_ == 1 and builtin::blessed($_[0]) and $_[0]->can("is_joinable")
!ll&&!rl&&r
1556
@_ == 1 and builtin::blessed($_[0])
!ll&&!rl&&r
1061
@_ == 1 and builtin::blessed($_[0]) and $_[0]->can("does") and $_[0]->does("Fey::Role::SQL::ReturnsData")
!ll&&!rl&&r
105
@_ == 1 and builtin::blessed($_[0]) and $_[0]->can("does")
!ll&&!rl&&r
105
@_ == 1 and builtin::blessed($_[0])
!ll&&!rl&&r
105
$is_inner_join and @_ == 4 and $_[3]->isa("Fey::SQL::Where")
!ll&&!rl&&r
100
$is_inner_join and @_ == 4
!ll&&!rl&&r
100
$is_inner_join and @_ == 3 and $_[2]->isa("Fey::SQL::Where")
!ll&&!rl&&r
100
$is_inner_join and @_ == 3
!ll&&!rl&&r
100
$is_inner_join and @_ == 3 and $_[2]->isa("Fey::FK")
!ll&&!rl&&r
100
$is_inner_join and @_ == 3
!ll&&!rl&&r
100
$is_inner_join and @_ == 2
!ll&&!rl&&r
100
$is_outer_join and @_ == 5
!ll&&!rl&&r
010
$is_outer_join and @_ == 4 and $_[3]->isa("Fey::SQL::Where")
!ll&&!rl&&r
001
$is_outer_join and @_ == 4
!ll&&!rl&&r
001
$is_outer_join and @_ == 4 and $_[3]->isa("Fey::FK")
!ll&&!rl&&r
000
$is_outer_join and @_ == 4
!ll&&!rl&&r
000
$is_outer_join and @_ == 3
!ll&&!rl&&r
000
MC/DC50%2/4
@_ == 1 and builtin::blessed($_[0]) and $_[0]->can("is_joinable") and $_[0]->is_joinable
@_ == 1 builtin::blessed($_[0]) $_[0]->can("is_joinable") $_[0]->is_joinable
MC/DC25%1/4
@_ == 1 and builtin::blessed($_[0]) and $_[0]->can("does") and $_[0]->does("Fey::Role::SQL::ReturnsData")
@_ == 1 builtin::blessed($_[0]) $_[0]->can("does") $_[0]->does("Fey::Role::SQL::ReturnsData")
MC/DC0%0/3
$is_inner_join and @_ == 4 and $_[3]->isa("Fey::SQL::Where")
$is_inner_join @_ == 4 $_[3]->isa("Fey::SQL::Where")
MC/DC0%0/3
$is_inner_join and @_ == 3 and $_[2]->isa("Fey::SQL::Where")
$is_inner_join @_ == 3 $_[2]->isa("Fey::SQL::Where")
MC/DC0%0/3
$is_inner_join and @_ == 3 and $_[2]->isa("Fey::FK")
$is_inner_join @_ == 3 $_[2]->isa("Fey::FK")
MC/DC0%0/2
$is_inner_join and @_ == 2
$is_inner_join @_ == 2
MC/DC0%0/2
$is_outer_join and @_ == 5
$is_outer_join @_ == 5
MC/DC0%0/3
$is_outer_join and @_ == 4 and $_[3]->isa("Fey::SQL::Where")
$is_outer_join @_ == 4 $_[3]->isa("Fey::SQL::Where")
MC/DC0%0/3
$is_outer_join and @_ == 4 and $_[3]->isa("Fey::FK")
$is_outer_join @_ == 4 $_[3]->isa("Fey::FK")
MC/DC0%0/2
$is_outer_join and @_ == 3
$is_outer_join @_ == 3
Truth table60%3/5
A and B and C and D
A= @_ == 1B= builtin::blessed($_[0])C= $_[0]->can("is_joinable")D= $_[0]->is_joinable
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table40%2/5
A and B and C and D
A= @_ == 1B= builtin::blessed($_[0])C= $_[0]->can("does")D= $_[0]->does("Fey::Role::SQL::ReturnsData")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table25%1/4
A and B and C
A= $is_inner_joinB= @_ == 4C= $_[3]->isa("Fey::SQL::Where")
ABCresult
0XX0
10X0
1100
1111
Truth table25%1/4
A and B and C
A= $is_inner_joinB= @_ == 3C= $_[2]->isa("Fey::SQL::Where")
ABCresult
0XX0
10X0
1100
1111
Truth table25%1/4
A and B and C
A= $is_inner_joinB= @_ == 3C= $_[2]->isa("Fey::FK")
ABCresult
0XX0
10X0
1100
1111
Truth table33%1/3
A and B
A= $is_inner_joinB= @_ == 2
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $is_outer_joinB= @_ == 5
ABresult
0X0
100
111
Truth table25%1/4
A and B and C
A= $is_outer_joinB= @_ == 4C= $_[3]->isa("Fey::SQL::Where")
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $is_outer_joinB= @_ == 4C= $_[3]->isa("Fey::FK")
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A and B
A= $is_outer_joinB= @_ == 3
ABresult
0X0
100
111
165 @_ == 1
166 && blessed $_[0]
167 && $_[0]->can('is_joinable')
168 && $_[0]->is_joinable()
169 ? '_from_one_table'
170  
171 : @_ == 1
172 && blessed $_[0]
173 && $_[0]->can('does')
174 && $_[0]->does('Fey::Role::SQL::ReturnsData')
175 ? '_from_subselect'
176  
177 : $is_inner_join
178 && @_ == 4 && $_[3]->isa('Fey::SQL::Where')
179 ? '_join_with_where'
180  
181 : $is_inner_join
182 && @_ == 3 && $_[2]->isa('Fey::SQL::Where')
183 ? '_join_with_where'
184  
185 : $is_inner_join && @_ == 3 && $_[2]->isa('Fey::FK')
186 ? '_join'
187  
188 : $is_inner_join && @_ == 2
189 ? '_join'
190  
191 : $is_outer_join && @_ == 5
192 ? '_outer_join_with_where'
193  
194 : $is_outer_join
195 && @_ == 4
196 && $_[3]->isa('Fey::SQL::Where')
197 ? '_outer_join_with_where'
198  
199 : $is_outer_join && @_ == 4 && $_[3]->isa('Fey::FK')
200 ? '_outer_join'
201  
202 : $is_outer_join && @_ == 3
203 ? '_outer_join'
204  
205 : undef
206 );
207 #>>>
208  
209 62 ▶ param_error "from() called with invalid parameters (@_)."
Branch: 1 branch
BranchTrueFalseTotal
unless $meth 0 62 62
210 unless $meth;
211  
212 62 $self->$meth(@_);
213  
214 62 return $self;
215 }
216  
217 56 ▶sub _from_one_table {
Subroutine _from_one_table: called CC=1 SCAR=0.0
218 56 my $self = shift;
219  
220 56 my $join = Fey::SQL::Fragment::Join->new( table1 => $_[0] );
221 56 $self->_set_from( $join->id() => $join );
222 }
223  
224 5 ▶sub _from_subselect {
Subroutine _from_subselect: called CC=1 SCAR=0.0
225 5 my $self = shift;
226  
227 5 my $subsel = $_[0];
228 5 $self->_set_from( $subsel->id() => $subsel );
229 }
230  
231 0 ▶sub _join {
Subroutine _join: not called CC=2 SCAR=17.9
232 0 my $self = shift;
233  
234 0 _check_join_arguments(@_);
235  
236 0 ▶ my $fk = $_[2] || $self->_fk_for_join(@_);
Condition0%0/3
$_[2] || $self->_fk_for_join(@_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[2] || $self->_fk_for_join(@_)
$_[2] $self->_fk_for_join(@_)
Truth table0%0/3
A || B
A= $_[2]B= $self->_fk_for_join(@_)
ABresult
1X1
011
000
237  
238 0 my $join = Fey::SQL::Fragment::Join->new(
239 table1 => $_[0],
240 table2 => $_[1],
241 fk => $fk,
242 );
243 0 $self->_set_from( $join->id() => $join );
244 }
245  
246 0 ▶sub _join_with_where {
Subroutine _join_with_where: not called CC=2 SCAR=17.9
247 0 my $self = shift;
248  
249 0 _check_join_arguments(@_);
250  
251 0 my $where = pop;
252 0 ▶ my $fk = @_ == 3 ? pop : $self->_fk_for_join(@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 ? : 0 0 0
253  
254 0 my $join = Fey::SQL::Fragment::Join->new(
255 table1 => $_[0],
256 table2 => $_[1],
257 fk => $fk,
258 where => $where,
259 );
260 0 $self->_set_from( $join->id() => $join );
261 }
262  
263 0 ▶sub _check_join_arguments {
Subroutine _check_join_arguments: not called CC=2 SCAR=17.9
264 param_error
265 'the first two arguments to from() were not valid (not tables or something else joinable).'
266 unless
267 0 ▶ all { blessed $_ && $_->can('is_joinable') && $_->is_joinable() }
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) and $_->can("is_joinable") and $_->is_joinable 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->can("is_joinable")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->can("is_joinable")
builtin::blessed($_) $_->can("is_joinable")
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->can("is_joinable")
ABresult
0X0
100
111
268 0 ▶ @_[ 0, 1 ];
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { builtin::blessed($_) and $_->can("is_joinable") and $_->is_joinable; } , @_[0, 1]) 0 0 0
269 }
270  
271 1 ▶sub _fk_for_join {
Subroutine _fk_for_join: called CC=3 SCAR=14.2
272 1 my $self = shift;
273 1 my @tables = @_;
274  
275 1 my $s = $tables[0]->schema;
276 1 my @fk = $s->foreign_keys_between_tables(@tables);
277  
278 1 ▶ unless ( @fk == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@fk == 1) 0 1 1
279 0 my $names = join ' and ', sort map { $_->name() } @tables;
280  
281 0 ▶ param_error
Branch: 1 branch
BranchTrueFalseTotal
unless @fk 0 0 0
282 "You specified a join for two tables that do not share a foreign key ($names)."
283 unless @fk;
284  
285 0 param_error
286 "You specified a join for two tables with more than one foreign key ($names)"
287 . ', so you must specify which foreign key to use for the join.';
288 }
289  
290 1 return $fk[0];
291 }
292  
293 0 ▶sub _outer_join {
Subroutine _outer_join: not called CC=2 SCAR=17.9
294 0 my $self = shift;
295  
296 0 _check_outer_join_arguments(@_);
297  
298 # I used to have ...
299 #
300 # $_[3] || $self->_fk_for_join( @_[0, 2] )
301 #
302 # but this ends up reducing code coverage because it's not
303 # possible (I hope) to have a situation where both are false.
304 0 my $fk = $_[3];
305 0 ▶ $fk = $self->_fk_for_join( @_[ 0, 2 ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $fk 0 0 0
306 unless $fk;
307  
308 0 my $join = Fey::SQL::Fragment::Join->new(
309 table1 => $_[0],
310 table2 => $_[2],
311 fk => $fk,
312 outer_type => lc $_[1],
313 );
314 0 $self->_set_from( $join->id() => $join );
315 }
316  
317 1 ▶sub _outer_join_with_where {
Subroutine _outer_join_with_where: called CC=3 SCAR=11.2
318 1 my $self = shift;
319  
320 1 _check_outer_join_arguments(@_);
321  
322 1 my $fk;
323 1 ▶ $fk = $_[3]->isa('Fey::FK') ? $_[3] : $self->_fk_for_join( @_[ 0, 2 ] );
Branch: 1 branch
BranchTrueFalseTotal
$_[3]->isa('Fey::FK') ? : 0 1 1
324  
325 1 ▶ my $where = $_[4] ? $_[4] : $_[3];
Branch: 1 branch
BranchTrueFalseTotal
$_[4] ? : 0 1 1
326  
327 1 my $join = Fey::SQL::Fragment::Join->new(
328 table1 => $_[0],
329 table2 => $_[2],
330 fk => $fk,
331 outer_type => lc $_[1],
332 where => $where,
333 );
334 1 $self->_set_from( $join->id() => $join );
335 }
336  
337 1 ▶sub _check_outer_join_arguments {
Subroutine _check_outer_join_arguments: called CC=6 SCAR=24.4
338 1 ▶ param_error
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] =~ /^(?:left|right|full)$/i 0 1 1
339 'invalid outer join type, must be one of out left, right, or full.'
340 unless $_[1] =~ /^(?:left|right|full)$/i;
341  
342 1 ▶ param_error 'from() was called with invalid arguments'
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->can("is_joinable") and $_[0]->is_joinable and $_[2]->can("is_joinable") and $_[2]->is_joinable 0 1 1
Condition33%3/9
$_[0]->can("is_joinable") and $_[0]->is_joinable and $_[2]->can("is_joinable") and $_[2]->is_joinable
!ll&&!rl&&r
001
$_[0]->can("is_joinable") and $_[0]->is_joinable and $_[2]->can("is_joinable")
!ll&&!rl&&r
001
$_[0]->can("is_joinable") and $_[0]->is_joinable
!ll&&!rl&&r
001
MC/DC0%0/4
$_[0]->can("is_joinable") and $_[0]->is_joinable and $_[2]->can("is_joinable") and $_[2]->is_joinable
$_[0]->can("is_joinable") $_[0]->is_joinable $_[2]->can("is_joinable") $_[2]->is_joinable
Truth table20%1/5
A and B and C and D
A= $_[0]->can("is_joinable")B= $_[0]->is_joinableC= $_[2]->can("is_joinable")D= $_[2]->is_joinable
ABCDresult
0XXX0
10XX0
110X0
11100
11111
343 unless $_[0]->can('is_joinable')
344 && $_[0]->is_joinable()
345 && $_[2]->can('is_joinable')
346 && $_[2]->is_joinable();
347 }
348  
349 7 ▶sub group_by {
Subroutine group_by: called CC=2 SCAR=6.9
Pod documented
350 7 my $self = shift;
351  
352 7 ▶ my $count = @_ ? @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 1 7
353 7 my (@by) = pos_validated_list(
354 \@_,
355 ( ( { isa => GroupByElement } ) x $count ),
356 MX_PARAMS_VALIDATE_NO_CACHE => 1,
357 );
358  
359 5 $self->_add_group_by_elements(@by);
360  
361 5 return $self;
362 }
363  
364 27 ▶sub having {
Subroutine having: called CC=1 SCAR=0.0
Pod documented
365 27 my $self = shift;
366  
367 27 $self->_condition( 'having', @_ );
368  
369 23 return $self;
370 }
371  
372 2 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
Pod documented
373 2 return $_[0]->sql('Fey::FakeDBI');
374 }
375  
376 72 ▶sub sql {
Subroutine sql: called CC=1 SCAR=0.0
Pod documented
377 72 my $self = shift;
378 72 my ($dbh) = pos_validated_list( \@_, { isa => CanQuote } );
379  
380 return (
381 72 join q{ },
382 $self->select_clause($dbh),
383 $self->from_clause($dbh),
384 $self->where_clause($dbh),
385 $self->group_by_clause($dbh),
386 $self->having_clause($dbh),
387 $self->order_by_clause($dbh),
388 $self->limit_clause($dbh),
389 );
390 }
391  
392 82 ▶sub select_clause {
Subroutine select_clause: called CC=3 SCAR=11.1
Pod documented
393 82 my $self = shift;
394 82 my $dbh = shift;
395  
396 82 my $sql = 'SELECT ';
397  
398 82 ▶ if ( $self->is_distinct() ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_distinct) { } 1 81 82
elsif ($self->_has_is_distinct_on) { } 0 81 81
399 1 $sql .= 'DISTINCT ';
400 }
401 elsif ( $self->_has_is_distinct_on() ) {
402 0 $sql .= 'DISTINCT ON ('
403 . $self->is_distinct_on()->sql_or_alias($dbh) . ') ';
404 }
405  
406 $sql .= (
407 join ', ',
408 82 map { $_->sql_with_alias($dbh) } $self->select_clause_elements()
409 );
410  
411 82 return $sql;
412 }
413  
414 74 ▶sub from_clause {
Subroutine from_clause: called CC=10 SCAR=25.0
Pod documented
415 74 my $self = shift;
416 74 my $dbh = shift;
417  
418 74 my @from;
419  
420 my %seen;
421  
422 # We lower case the test for correctly skipping already-seen tables.
423 74 for my $frag (
424 0 $self->_get_from( sort { lc($a) cmp lc($b) } $self->_from_ids() ) ) {
425 74 my $join_sql = $frag->sql_with_alias( $dbh, \%seen );
426  
427 # the fragment could be a Fey::SQL::Select itself
428 74 ▶ my @tables = $frag->can('tables') ? $frag->tables() : ();
Branch: 1 branch
BranchTrueFalseTotal
$frag->can('tables') ? : 70 4 74
429  
430 74 $seen{ $_->id() } = 1 for @tables;
431  
432 74 ▶ next unless length $join_sql;
Branch: 1 branch
BranchTrueFalseTotal
unless length $join_sql 0 74 74
433  
434 74 push @from, $join_sql;
435 }
436  
437 74 my $sql = 'FROM ';
438  
439 # This is a sort of manual join() call special-cased to add a
440 # comma as needed.
441 74 ▶ for ( my $i = 0; $i < @from; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @from) 74 74 148
442 74 $sql .= $from[$i];
443  
444 74 ▶ if ( $sql =~ /\)^/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sql =~ /\)^/) { } 0 74 74
elsif (($from[$i + 1] || '') =~ /^[\w\s]*JOIN/) { } 0 74 74
elsif ($from[$i + 1]) { } 0 74 74
445 0 $sql .= q{ };
446 }
447 ▶ elsif ( ( $from[ $i + 1 ] || '' ) =~ /^[\w\s]*JOIN/ ) {
Condition50%1/2
$from[$i + 1] || ''
l!l
074
MC/DC0%0/1
$from[$i + 1] || ''
$from[$i + 1]
Truth table50%1/2
A
A= $from[$i + 1]
Aresult
11
00
448 0 $sql .= q{ };
449 }
450 elsif ( $from[ $i + 1 ] ) {
451 0 $sql .= q{, };
452 }
453 }
454  
455 74 return $sql;
456 }
457  
458 77 ▶sub group_by_clause {
Subroutine group_by_clause: called CC=2 SCAR=6.9
Pod documented
459 77 my $self = shift;
460 77 my $dbh = shift;
461  
462 77 ▶ return unless $self->_has_group_by_elements();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_group_by_elements 72 5 77
463  
464 return (
465 'GROUP BY '
466 . (
467 join ', ',
468 5 map { $_->sql_or_alias($dbh) } $self->_group_by()
469 )
470 );
471 }
472  
473 89 ▶sub having_clause {
Subroutine having_clause: called CC=2 SCAR=6.9
Pod documented
474 89 my $self = shift;
475 89 my $dbh = shift;
476  
477 89 ▶ return unless $self->_has_having_elements();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_having_elements 72 17 89
478  
479 return (
480 'HAVING '
481 . (
482 join ' ',
483 17 map { $_->sql($dbh) } $self->_having()
484 )
485 );
486 }
487  
488 29 ▶sub bind_params {
Subroutine bind_params: called CC=1 SCAR=0.0
Pod documented
489 29 my $self = shift;
490  
491 return (
492 (
493 1 map { $_->bind_params() }
494 20 grep { $_->can('bind_params') }
495 $self->select_clause_elements()
496 ),
497  
498 (
499 18 map { $_->bind_params() }
500 18 grep { $_->can('bind_params') }
501 18 map { $self->_get_from($_) }
502 sort $self->_from_ids()
503 ),
504  
505 $self->_where_clause_bind_params(),
506  
507 (
508 3 map { $_->bind_params() }
509 29 grep { $_->can('bind_params') } $self->_having()
510 ),
511 );
512 }
513  
514 27 __PACKAGE__->meta()->make_immutable();
515  
516 27 1;
517  
518 # ABSTRACT: Represents a SELECT query
519  
520 __END__
↑ Summary Fey/SQL/Fragment/Where/Comparison.pm »