Fey/SQL/Select.pm

filter: Statementstmt 82.8% 150 / 181 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.1% 272 / 431 SCAR 55.4
CC
68
Cov
66%
from
178.2
from_clause
12.2
__ANON__
12.0
CRAP 255.1
↑ Summary Fey/Types/Internal.pm »
1 package Fey::SQL::Select;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 our $VERSION = '0.43';
8  
9 26 ▶use Fey::Exceptions qw( param_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use Fey::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use Fey::Role::ColumnLike;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use Fey::SQL::Fragment::Join;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use Fey::Types
14 26 ▶ qw( ArrayRef Bool HashRef CanQuote GroupByElement SelectElement );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 26 ▶use List::AllUtils qw( all );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 26 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 26 ▶use Moose 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 26 ▶use MooseX::Params::Validate 0.21 qw( pos_validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 26 ▶use MooseX::SemiAffordanceAccessor 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 26 ▶use MooseX::StrictConstructor 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 26 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 26 with 'Fey::Role::SQL::HasWhereClause' => {
30 -excludes => 'bind_params',
31 -alias => { bind_params => '_where_clause_bind_params' },
32 };
33  
34 26 with 'Fey::Role::SQL::HasBindParams' => { -excludes => 'bind_params' };
35  
36 26 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 26 handles => {
47 _add_select_element => 'push',
48 select_clause_elements => 'elements',
49 },
50 init_arg => undef,
51 );
52  
53 26 has 'is_distinct' => (
54 is => 'rw',
55 isa => Bool,
56 default => 0,
57 writer => '_set_is_distinct',
58 );
59  
60 26 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 26 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 26 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 26 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 26 with 'Fey::Role::SQL::Cloneable';
108  
109 sub select {
110 88 ▶ my $self = shift;
Subroutine select: called CC=4 SCAR=13.9
Pod documented
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 (@select) {
120 91 ▶ $self->_add_select_element(
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($elt) ? : 51 40 91
121 blessed $elt ? $elt : Fey::Literal->new_from_scalar($elt) );
122 }
123  
124 88 return $self;
125 }
126  
127 sub distinct {
128 1 ▶ die 'Cannot call ->distinct and ->distinct_on'
Subroutine distinct: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->_has_is_distinct_on 0 1 1
129 if $_[0]->_has_is_distinct_on();
130  
131 1 $_[0]->_set_is_distinct(1);
132  
133 1 return $_[0];
134 }
135  
136 sub distinct_on {
137 0 ▶ die 'Cannot call ->distinct and ->distinct_on'
Subroutine distinct_on: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->is_distinct 0 0 0
138 if $_[0]->is_distinct();
139  
140 0 $_[0]->_set_is_distinct_on( $_[1] );
141  
142 0 return $_[0];
143 }
144  
145 # XXX - need to handle subselect as if it were a table rather than as
146 # a special case
147 sub from {
148 62 ▶ my $self = shift;
Subroutine from: called CC=33 SCAR=51.8
Pod documented
149  
150 # $t1, 'LEFT', $t2, ...
151 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
152  
153 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
154  
155 # gee, wouldn't multimethods be nice here?
156  
157 #<<<
158 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
159 @_ == 1
160 && blessed $_[0]
161 && $_[0]->can('is_joinable')
162 && $_[0]->is_joinable()
163 ? '_from_one_table'
164  
165 : @_ == 1
166 && blessed $_[0]
167 && $_[0]->can('does')
168 && $_[0]->does('Fey::Role::SQL::ReturnsData')
169 ? '_from_subselect'
170  
171 : $is_inner_join
172 && @_ == 4 && $_[3]->isa('Fey::SQL::Where')
173 ? '_join_with_where'
174  
175 : $is_inner_join
176 && @_ == 3 && $_[2]->isa('Fey::SQL::Where')
177 ? '_join_with_where'
178  
179 : $is_inner_join && @_ == 3 && $_[2]->isa('Fey::FK')
180 ? '_join'
181  
182 : $is_inner_join && @_ == 2
183 ? '_join'
184  
185 : $is_outer_join && @_ == 5
186 ? '_outer_join_with_where'
187  
188 : $is_outer_join
189 && @_ == 4
190 && $_[3]->isa('Fey::SQL::Where')
191 ? '_outer_join_with_where'
192  
193 : $is_outer_join && @_ == 4 && $_[3]->isa('Fey::FK')
194 ? '_outer_join'
195  
196 : $is_outer_join && @_ == 3
197 ? '_outer_join'
198  
199 : undef
200 );
201 #>>>
202  
203 62 ▶ param_error "from() called with invalid parameters (@_)."
Branch: 1 branch
BranchTrueFalseTotal
unless $meth 0 62 62
204 unless $meth;
205  
206 62 $self->$meth(@_);
207  
208 62 return $self;
209 }
210  
211 sub _from_one_table {
212 56 ▶ my $self = shift;
Subroutine _from_one_table: called CC=1 SCAR=0.0
213  
214 56 my $join = Fey::SQL::Fragment::Join->new( table1 => $_[0] );
215 56 $self->_set_from( $join->id() => $join );
216 }
217  
218 sub _from_subselect {
219 5 ▶ my $self = shift;
Subroutine _from_subselect: called CC=1 SCAR=0.0
220  
221 5 my $subsel = $_[0];
222 5 $self->_set_from( $subsel->id() => $subsel );
223 }
224  
225 sub _join {
226 0 ▶ my $self = shift;
Subroutine _join: not called CC=2 SCAR=17.9
227  
228 0 _check_join_arguments(@_);
229  
230 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
231  
232 0 my $join = Fey::SQL::Fragment::Join->new(
233 table1 => $_[0],
234 table2 => $_[1],
235 fk => $fk,
236 );
237 0 $self->_set_from( $join->id() => $join );
238 }
239  
240 sub _join_with_where {
241 0 ▶ my $self = shift;
Subroutine _join_with_where: not called CC=2 SCAR=17.9
242  
243 0 _check_join_arguments(@_);
244  
245 0 my $where = pop;
246 0 ▶ my $fk = @_ == 3 ? pop : $self->_fk_for_join(@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 ? : 0 0 0
247  
248 0 my $join = Fey::SQL::Fragment::Join->new(
249 table1 => $_[0],
250 table2 => $_[1],
251 fk => $fk,
252 where => $where,
253 );
254 0 $self->_set_from( $join->id() => $join );
255 }
256  
257 sub _check_join_arguments {
258 param_error
259 'the first two arguments to from() were not valid (not tables or something else joinable).'
260 unless
261 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
262 0 ▶ @_[ 0, 1 ];
Subroutine _check_join_arguments: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { builtin::blessed($_) and $_->can("is_joinable") and $_->is_joinable; } , @_[0, 1]) 0 0 0
263 }
264  
265 sub _fk_for_join {
266 1 ▶ my $self = shift;
Subroutine _fk_for_join: called CC=3 SCAR=14.2
267 1 my @tables = @_;
268  
269 1 my $s = $tables[0]->schema;
270 1 my @fk = $s->foreign_keys_between_tables(@tables);
271  
272 1 ▶ unless ( @fk == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@fk == 1) 0 1 1
273 0 my $names = join ' and ', sort map { $_->name() } @tables;
274  
275 0 ▶ param_error
Branch: 1 branch
BranchTrueFalseTotal
unless @fk 0 0 0
276 "You specified a join for two tables that do not share a foreign key ($names)."
277 unless @fk;
278  
279 0 param_error
280 "You specified a join for two tables with more than one foreign key ($names)"
281 . ', so you must specify which foreign key to use for the join.';
282 }
283  
284 1 return $fk[0];
285 }
286  
287 sub _outer_join {
288 0 ▶ my $self = shift;
Subroutine _outer_join: not called CC=2 SCAR=17.9
289  
290 0 _check_outer_join_arguments(@_);
291  
292 # I used to have ...
293 #
294 # $_[3] || $self->_fk_for_join( @_[0, 2] )
295 #
296 # but this ends up reducing code coverage because it's not
297 # possible (I hope) to have a situation where both are false.
298 0 my $fk = $_[3];
299 0 ▶ $fk = $self->_fk_for_join( @_[ 0, 2 ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $fk 0 0 0
300 unless $fk;
301  
302 0 my $join = Fey::SQL::Fragment::Join->new(
303 table1 => $_[0],
304 table2 => $_[2],
305 fk => $fk,
306 outer_type => lc $_[1],
307 );
308 0 $self->_set_from( $join->id() => $join );
309 }
310  
311 sub _outer_join_with_where {
312 1 ▶ my $self = shift;
Subroutine _outer_join_with_where: called CC=3 SCAR=11.2
313  
314 1 _check_outer_join_arguments(@_);
315  
316 1 my $fk;
317 1 ▶ $fk = $_[3]->isa('Fey::FK') ? $_[3] : $self->_fk_for_join( @_[ 0, 2 ] );
Branch: 1 branch
BranchTrueFalseTotal
$_[3]->isa('Fey::FK') ? : 0 1 1
318  
319 1 ▶ my $where = $_[4] ? $_[4] : $_[3];
Branch: 1 branch
BranchTrueFalseTotal
$_[4] ? : 0 1 1
320  
321 1 my $join = Fey::SQL::Fragment::Join->new(
322 table1 => $_[0],
323 table2 => $_[2],
324 fk => $fk,
325 outer_type => lc $_[1],
326 where => $where,
327 );
328 1 $self->_set_from( $join->id() => $join );
329 }
330  
331 sub _check_outer_join_arguments {
332 1 ▶ param_error
Subroutine _check_outer_join_arguments: called CC=6 SCAR=24.4
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] =~ /^(?:left|right|full)$/i 0 1 1
333 'invalid outer join type, must be one of out left, right, or full.'
334 unless $_[1] =~ /^(?:left|right|full)$/i;
335  
336 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
337 unless $_[0]->can('is_joinable')
338 && $_[0]->is_joinable()
339 && $_[2]->can('is_joinable')
340 && $_[2]->is_joinable();
341 }
342  
343 sub group_by {
344 7 ▶ my $self = shift;
Subroutine group_by: called CC=2 SCAR=6.9
Pod documented
345  
346 7 ▶ my $count = @_ ? @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 1 7
347 7 my (@by) = pos_validated_list(
348 \@_,
349 ( ( { isa => GroupByElement } ) x $count ),
350 MX_PARAMS_VALIDATE_NO_CACHE => 1,
351 );
352  
353 5 $self->_add_group_by_elements(@by);
354  
355 5 return $self;
356 }
357  
358 sub having {
359 27 ▶ my $self = shift;
Subroutine having: called CC=1 SCAR=0.0
Pod documented
360  
361 27 $self->_condition( 'having', @_ );
362  
363 23 return $self;
364 }
365  
366 sub id {
367 2 ▶ return $_[0]->sql('Fey::FakeDBI');
Subroutine id: called CC=1 SCAR=0.0
Pod documented
368 }
369  
370 sub sql {
371 72 ▶ my $self = shift;
Subroutine sql: called CC=1 SCAR=0.0
Pod documented
372 72 my ($dbh) = pos_validated_list( \@_, { isa => CanQuote } );
373  
374 return (
375 72 join q{ },
376 $self->select_clause($dbh),
377 $self->from_clause($dbh),
378 $self->where_clause($dbh),
379 $self->group_by_clause($dbh),
380 $self->having_clause($dbh),
381 $self->order_by_clause($dbh),
382 $self->limit_clause($dbh),
383 );
384 }
385  
386 sub select_clause {
387 82 ▶ my $self = shift;
Subroutine select_clause: called CC=4 SCAR=14.0
Pod documented
388 82 my $dbh = shift;
389  
390 82 my $sql = 'SELECT ';
391  
392 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
393 1 $sql .= 'DISTINCT ';
394 }
395 elsif ( $self->_has_is_distinct_on() ) {
396 0 $sql .= 'DISTINCT ON ('
397 . $self->is_distinct_on()->sql_or_alias($dbh) . ') ';
398 }
399  
400 $sql .= (
401 join ', ',
402 map {
403 82 ▶ $_->can('sql_for_select_clause')
Branch: 1 branch
BranchTrueFalseTotal
$_->can('sql_for_select_clause') ? : 4 83 87
404 ? $_->sql_for_select_clause($dbh)
405 : $_->sql_with_alias($dbh)
406 } $self->select_clause_elements()
407 );
408  
409 82 return $sql;
410 }
411  
412 sub from_clause {
413 74 ▶ my $self = shift;
Subroutine from_clause: called CC=10 SCAR=25.0
Pod documented
414 74 my $dbh = shift;
415  
416 74 my @from;
417  
418 my %seen;
419  
420 # We lower case the test for correctly skipping already-seen tables.
421 74 for my $frag (
422 0 $self->_get_from( sort { lc($a) cmp lc($b) } $self->_from_ids() ) ) {
423 74 my $join_sql = $frag->sql_with_alias( $dbh, \%seen );
424  
425 # the fragment could be a Fey::SQL::Select itself
426 74 ▶ my @tables = $frag->can('tables') ? $frag->tables() : ();
Branch: 1 branch
BranchTrueFalseTotal
$frag->can('tables') ? : 70 4 74
427  
428 74 $seen{ $_->id() } = 1 for @tables;
429  
430 74 ▶ next unless length $join_sql;
Branch: 1 branch
BranchTrueFalseTotal
unless length $join_sql 0 74 74
431  
432 74 push @from, $join_sql;
433 }
434  
435 74 my $sql = 'FROM ';
436  
437 # This is a sort of manual join() call special-cased to add a
438 # comma as needed.
439 74 ▶ for ( my $i = 0; $i < @from; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @from) 74 74 148
440 74 $sql .= $from[$i];
441  
442 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
443 0 $sql .= q{ };
444 }
445 ▶ 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
446 0 $sql .= q{ };
447 }
448 elsif ( $from[ $i + 1 ] ) {
449 0 $sql .= q{, };
450 }
451 }
452  
453 74 return $sql;
454 }
455  
456 sub group_by_clause {
457 77 ▶ my $self = shift;
Subroutine group_by_clause: called CC=2 SCAR=6.9
Pod documented
458 77 my $dbh = shift;
459  
460 77 ▶ return unless $self->_has_group_by_elements();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_group_by_elements 72 5 77
461  
462 return (
463 'GROUP BY '
464 . (
465 join ', ',
466 5 map { $_->sql_or_alias($dbh) } $self->_group_by()
467 )
468 );
469 }
470  
471 sub having_clause {
472 89 ▶ my $self = shift;
Subroutine having_clause: called CC=2 SCAR=6.9
Pod documented
473 89 my $dbh = shift;
474  
475 89 ▶ return unless $self->_has_having_elements();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_having_elements 72 17 89
476  
477 return (
478 'HAVING '
479 . (
480 join ' ',
481 17 map { $_->sql($dbh) } $self->_having()
482 )
483 );
484 }
485  
486 sub bind_params {
487 29 ▶ my $self = shift;
Subroutine bind_params: called CC=1 SCAR=0.0
Pod documented
488  
489 return (
490 (
491 1 map { $_->bind_params() }
492 16 grep { $_->can('bind_params') }
493 $self->select_clause_elements()
494 ),
495  
496 (
497 18 map { $_->bind_params() }
498 18 grep { $_->can('bind_params') }
499 18 map { $self->_get_from($_) }
500 sort $self->_from_ids()
501 ),
502  
503 $self->_where_clause_bind_params(),
504  
505 (
506 3 map { $_->bind_params() }
507 29 grep { $_->can('bind_params') } $self->_having()
508 ),
509 );
510 }
511  
512 26 __PACKAGE__->meta()->make_immutable();
513  
514 26 1;
515  
516 # ABSTRACT: Represents a SELECT query
517  
518 __END__
519  
520 =pod
521  
522 =head1 NAME
523  
524 Fey::SQL::Select - Represents a SELECT query
525  
526 =head1 VERSION
527  
528 version 0.43
529  
530 =head1 SYNOPSIS
531  
532 my $sql = Fey::SQL->new_select();
533  
534 # SELECT Part.part_id, Part.part_name
535 # FROM Part JOIN MachinePart
536 # ON Part.part_id = MachinePart.part_id
537 # WHERE MachinePart.machine_id = $value
538 # ORDER BY Part.name DESC
539 # LIMIT 10
540 $sql->select( $part_id, $part_name );
541 $sql->from( $Part, $MachinePart );
542 $sql->where( $machine_id, '=', $value );
543 $sql->order_by( $part_Name, 'DESC' );
544 $sql->limit(10);
545  
546 print $sql->sql($dbh);
547  
548 =head1 DESCRIPTION
549  
550 This class represents a C<SELECT> query.
551  
552 =head1 METHODS
553  
554 This class provides the following methods:
555  
556 =head2 Constructor
557  
558 To construct an object of this class, call C<< $query->select() >> on
559 a C<Fey::SQL> object.
560  
561 =head2 $select->select(...)
562  
563 This method accepts a list of parameters, which are the things being
564 selected.
565  
566 The list can include the following types of elements:
567  
568 =over 4
569  
570 =item * plain scalars, including C<undef>
571  
572 These will be passed to C<< Fey::Literal->new_from_scalar() >>.
573  
574 =item * C<Fey::Table> objects
575  
576 If a table is passed, then all of its columns will be included in the
577 C<SELECT> clause.
578  
579 =item * C<Fey::Column> objects, and aliases
580  
581 This specifies an individual column (possibly aliased) to include in
582 the select.
583  
584 The C<< $column->is_selectable() >> method must return true for these
585 objects.
586  
587 This method can be called multiple times with different elements each
588 time.
589  
590 =item * C<Fey::Literal> objects
591  
592 Any type of literal can be included in a C<SELECT> clause.
593  
594 =back
595  
596 =head2 $select->distinct()
597  
598 If this is called, the generated SQL will start with C<SELECT DISTINCT>. You
599 cannot call both C<< $select->distinct() >> and C<< $select->distinct_on() >>
600 for the same query.
601  
602 =head2 $select->distinct_on($column)
603  
604 If this is called, the generated SQL will start with C<SELECT DISTINCT ON
605 (Table.column)>. You cannot call both C<< $select->distinct() >> and C<<
606 $select->distinct_on() >> for the same query.
607  
608 =head2 $select->from(...)
609  
610 This method specifies the C<FROM> clause of the query. It can accept a
611 variety of argument lists.
612  
613 =over 4
614  
615 =item * ($table_or_alias)
616  
617 If called with a single C<Fey::Table> or table alias object, that
618 table is included in the C<FROM> clause.
619  
620 FROM Part
621  
622 FROM Part as Part0
623  
624 =item * ($select_query)
625  
626 If called with a single C<Fey::SQL::Select> object, that object's SQL
627 will be included in the C<FROM> clause as a subselect.
628  
629 FROM (SELECT part_id FROM Part) AS SUBSELECT0
630  
631 =item * ($table1, $table2)
632  
633 If two tables (or aliases) are passed to this method, these two tables
634 are included and joined together. The foreign key between these two
635 tables will be looked up in the C<Fey::Schema> object for the
636 tables. However, if the tables do not have a foreign key between them,
637 or have more than one foreign key, an exception is thrown.
638  
639 FROM Part, MachinePart
640 ON Part.part_id = MachinePart.part_id
641  
642 =item * ($table1, $table2, $fk)
643  
644 When joining two tables, you can manually specify the foreign key
645 which should be used to join them. This is necessary when there are
646 multiple foreign keys between two tables.
647  
648 You can also use this to "fake" a foreign key between two tables which
649 don't really have one, but where it makes sense to join them
650 anyway. If this paragraph doesn't make sense, don't worry about it ;)
651  
652 =item * ($table1, $table2, $where_clause)
653  
654 =item * ($table1, $table2, $fk, $where_clause)
655  
656 If you want to specify a C<WHERE> clause as part of an inner join, include this
657 as the last argument when calling C<< $select->from() >>.
658  
659 To create a standalone C<WHERE> clause suitable for passing to this
660 method, use the C<Fey::SQL::Where> class.
661  
662 =item * ($table1, 'left', $table2)
663  
664 If you want to do an outer join between two tables, pass the two
665 tables, separated by one of the following string:
666  
667 =over 8
668  
669 =item * left
670  
671 =item * right
672  
673 =item * full
674  
675 =back
676  
677 This will generate the appropriate outer join SQL in the C<FROM>
678 clause.
679  
680 FROM Part
681 LEFT OUTER JOIN MachinePart
682 ON Part.part_id = MachinePart.part_id
683  
684 Just as with a normal join, the C<<$select->from() >> will attempt to
685 automatically find a foreign key between the two tables.
686  
687 =item * ($table1, 'left', $table2, $fk)
688  
689 Just as with a normal join, you can manually specify the foreign key
690 to use for an outer join as well.
691  
692 =item * ($table1, 'left', $table2, $where_clause)
693  
694 If you want to specify a C<WHERE> clause as part of an outer join,
695 include this as the fourth argument when calling C<< $select->from()
696 >>.
697  
698 FROM Part
699 LEFT OUTER JOIN MachinePart
700 ON Part.part_id = MachinePart.part_id
701 AND MachinePart.machine_id = ?
702  
703 To create a standalone C<WHERE> clause suitable for passing to this
704 method, use the C<Fey::SQL::Where> class.
705  
706 =item * ($table1, 'left', $table2, $fk, $where_clause)
707  
708 You can manually specify a foreign key I<and> include a where clause
709 in an outer join.
710  
711 =back
712  
713 The C<< $select->from() >> method can be called multiple times with
714 different join options. If you call the method with arguments that it
715 has already seen, then it will effectively ignore that call.
716  
717 =head2 $select->where(...)
718  
719 See the L<Fey::SQL section on WHERE Clauses|Fey::SQL/WHERE Clauses>
720 for more details.
721  
722 =head2 $select->group_by(...)
723  
724 This method accepts a list of elements. Each element can be a
725 C<Fey::Column> object, a column alias, or a literal function or term.
726  
727 =head2 $select->having(...)
728  
729 The C<< $select->having() >> method accepts exactly the same arguments
730 as the C<< $select->where() >> method.
731  
732 =head2 $select->order_by(...)
733  
734 See the L<Fey::SQL section on ORDER BY Clauses|Fey::SQL/ORDER BY
735 Clauses> for more details.
736  
737 =head2 $select->limit(...)
738  
739 See the L<Fey::SQL section on LIMIT Clauses|Fey::SQL/LIMIT Clauses>
740 for more details.
741  
742 =head2 $select->sql($dbh)
743  
744 Returns the full SQL statement which this object represents. A DBI
745 handle must be passed so that identifiers can be properly quoted.
746  
747 =head2 $select->bind_params()
748  
749 See the L<Fey::SQL section on Bind Parameters|Fey::SQL/Bind
750 Parameters> for more details.
751  
752 =head2 $select->select_clause_elements
753  
754 Returns a list of objects, one for each element in the C<SELECT>
755 clause. These can be C<Fey::Column>s, C<Fey::Column::Alias>es, or any
756 type of C<Fey::Literal>.
757  
758 These items are returned in the order in which they will be included
759 in the C<SELECT> clause.
760  
761 =head2 $select->select_clause()
762  
763 Returns the C<SELECT> clause portion of the SQL statement as a string.
764  
765 =head2 $select->from_clause()
766  
767 Returns the C<FROM> clause portion of the SQL statement as a string.
768  
769 =head2 $select->where_clause()
770  
771 Returns the C<WHERE> clause portion of the SQL statement as a string.
772  
773 =head2 $select->order_by_clause()
774  
775 Returns the C<ORDER BY> clause portion of the SQL statement as a
776 string.
777  
778 =head2 $select->group_by_clause()
779  
780 Returns the C<GROUP BY> clause portion of the SQL statement as a
781 string.
782  
783 =head2 $select->having_clause()
784  
785 Returns the C<HAVING> clause portion of the SQL statement as a string.
786  
787 =head2 $select->limit_clause()
788  
789 Returns the C<LIMIT> clause portion of the SQL statement as a string.
790  
791 =head2 $select->sql_or_alias()
792  
793 =head2 $select->sql_with_alias()
794  
795 Returns the appropriate SQL snippet. See L<Fey::Role::HasAliasName>.
796  
797 =head2 $select->id()
798  
799 Returns a unique identifier for the select.
800  
801 =head1 ROLES
802  
803 =over 4
804  
805 =item * L<Fey::Role::SQL::HasBindParams>
806  
807 =item * L<Fey::Role::SQL::HasWhereClause>
808  
809 =item * L<Fey::Role::SQL::HasOrderByClause>
810  
811 =item * L<Fey::Role::SQL::HasLimitClause>
812  
813 =item * L<Fey::Role::SQL::HasAliasName>
814  
815 =item * L<Fey::Role::SQL::Comparable>
816  
817 This role allows a C<Fey::SQL::Select> object to be used as a
818 subselect in C<WHERE> clauses.
819  
820 =item * L<Fey::Role::SQL::Cloneable>
821  
822 =back
823  
824 =head1 BUGS
825  
826 See L<Fey> for details on how to report bugs.
827  
828 =head1 AUTHOR
829  
830 Dave Rolsky <autarch@urth.org>
831  
832 =head1 COPYRIGHT AND LICENSE
833  
834 This software is Copyright (c) 2011 - 2015 by Dave Rolsky.
835  
836 This is free software, licensed under:
837  
838 The Artistic License 2.0 (GPL Compatible)
839  
840 =cut
↑ Summary Fey/Types/Internal.pm »