Fey/Role/SetOperation.pm

filter: Statementstmt 88.5% 85 / 96 Branchbran 60.0% 6 / 10 Conditioncond 44.4% 4 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 86.3% 19 / 22 Podpod 25.0% 1 / 4 totaltotal 79.5% 117 / 147 SCAR 24.5
CC
11
Cov
83%
__ANON__
3.4
except
3.4
intersect
3.4
CRAP 11.6
« Fey/FK.pm ↑ Summary Fey/Role/SQL/Cloneable.pm »
1 package Fey::Role::SetOperation;
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 7 our $VERSION = '0.43';
8  
9 26 ▶use Fey::Types qw( ArrayRef Bool SetOperationArg Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 26 ▶use MooseX::Role::Parameterized 1.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use MooseX::Params::Validate 0.21 qw( pos_validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 parameter keyword => (
15 isa => Str,
16 required => 1,
17 );
18  
19 7 with 'Fey::Role::Comparable',
20 'Fey::Role::SQL::HasOrderByClause',
21 'Fey::Role::SQL::HasLimitClause',
22 'Fey::Role::SQL::ReturnsData';
23  
24 7 has 'is_all' => (
25 is => 'rw',
26 isa => Bool,
27 default => 0,
28 writer => '_set_is_all',
29 );
30  
31 has '_set_elements' => (
32 traits => ['Array'],
33 is => 'bare',
34 isa => ArrayRef [SetOperationArg],
35 25 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 7 handles => {
37 _add_set_elements => 'push',
38 _set_element_count => 'count',
39 _set_elements => 'elements',
40 },
41 init_arg => undef,
42 );
43  
44 sub id {
45 3 ▶ return $_[0]->sql('Fey::FakeDBI');
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
46 }
47  
48 sub all {
49 3 ▶ $_[0]->_set_is_all(1);
Subroutine all: called CC=1 SCAR=0.0
Pod documented
50 3 return $_[0];
51 }
52  
53 sub bind_params {
54 1 ▶ my $self = shift;
Subroutine bind_params: called CC=1 SCAR=0.0
Pod undocumented
55 1 return map { $_->bind_params } $self->_set_elements();
56 }
57  
58 sub select_clause_elements {
59 0 ▶ return ( $_[0]->_set_elements() )[0]->select_clause_elements();
Subroutine select_clause_elements: not called CC=1 SCAR=6.9
Pod undocumented
60 }
61  
62 role {
63 78 ▶ my $p = shift;
Subroutine __ANON__: called CC=1 SCAR=0.1
64 78 my %extra = @_;
65  
66 78 my $name = lc $p->keyword();
67  
68 method 'keyword_clause' => sub {
69 1 ▶ my $self = shift;
Subroutine keyword_clause: called CC=2 SCAR=6.9
Subroutine __ANON__: called CC=2 SCAR=6.9
70  
71 1 my $sql = uc($name);
72 1 ▶ $sql .= ' ALL' if $self->is_all();
Branch: 2 branches
BranchTrueFalseTotal
if $self->is_all 0 1 1
if $self->is_all 3 24 27
73 1 return $sql;
74 78 };
75  
76 78 my $clause_method = $name . '_clause';
77  
78 method 'sql' => sub {
79 1 ▶ my $self = shift;
Subroutine sql: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
80 1 my $dbh = shift;
81  
82 return (
83 1 join q{ },
84 $self->$clause_method($dbh),
85 $self->order_by_clause($dbh),
86 $self->limit_clause($dbh),
87 );
88 78 };
89  
90 method $name => sub {
91 0 ▶ my $self = shift;
Subroutine except: not called CC=3 SCAR=12.3
Subroutine intersect: called CC=3 SCAR=12.3
Subroutine __ANON__: called CC=3 SCAR=12.3
92  
93 0 my $count = @_;
94 0 ▶ $count = 2
Branch: 3 branches
BranchTrueFalseTotal
if $count < 2 and $self->_set_element_count < 2 0 0 0
if $count < 2 and $self->_set_element_count < 2 0 1 1
if $count < 2 and $self->_set_element_count < 2 9 27 36
Condition44%4/9
$count < 2 and $self->_set_element_count < 2
!ll&&!rl&&r
000
$count < 2 and $self->_set_element_count < 2
!ll&&!rl&&r
100
$count < 2 and $self->_set_element_count < 2
!ll&&!rl&&r
2169
MC/DC0%0/2
$count < 2 and $self->_set_element_count < 2
$count < 2 $self->_set_element_count < 2
MC/DC0%0/2
$count < 2 and $self->_set_element_count < 2
$count < 2 $self->_set_element_count < 2
MC/DC100%2/2
$count < 2 and $self->_set_element_count < 2
$count < 2 $self->_set_element_count < 2
Truth table0%0/3
A and B
A= $count < 2B= $self->_set_element_count < 2
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $count < 2B= $self->_set_element_count < 2
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $count < 2B= $self->_set_element_count < 2
ABresult
0X0
100
111
95 if $count < 2 && $self->_set_element_count() < 2;
96  
97 0 my (@set) = pos_validated_list(
98 \@_,
99 ( ( { isa => SetOperationArg } ) x $count ),
100 MX_PARAMS_VALIDATE_NO_CACHE => 1,
101 );
102  
103 0 $self->_add_set_elements(@set);
104  
105 0 return $self;
106 78 };
107  
108 method $clause_method => sub {
109 1 ▶ my $self = shift;
Subroutine union_clause: called CC=1 SCAR=0.2
Subroutine except_clause: not called CC=1 SCAR=0.2
Subroutine intersect_clause: called CC=1 SCAR=0.2
110 1 my $dbh = shift;
111  
112 return (
113 join q{ } . $self->keyword_clause($dbh) . q{ },
114 1 map { '(' . $_->sql($dbh) . ')' } $self->_set_elements()
115 );
116 78 };
117  
118 78 with 'Fey::Role::HasAliasName' => {
119 generated_alias_prefix => uc $name,
120 sql_needs_parens => 1,
121 };
122 7 };
123  
124 7 1;
125  
126 # ABSTRACT: A role for things that are a set operation
127  
128 __END__
129  
130 =pod
131  
132 =head1 NAME
133  
134 Fey::Role::SetOperation - A role for things that are a set operation
135  
136 =head1 VERSION
137  
138 version 0.43
139  
140 =head1 SYNOPSIS
141  
142 use Moose 2.1200;
143  
144 with 'Fey::Role::SetOperation' => { keyword => $keyword };
145  
146 =head1 DESCRIPTION
147  
148 Classes which do this role represent a query which can include
149 multiple C<SELECT> queries or set operations.
150  
151 =head1 PARAMETERS
152  
153 =head2 keyword
154  
155 The SQL keyword for this set operation (i.e. C<UNION>, C<INTERSECT>,
156 C<EXCEPT>).
157  
158 =head1 METHODS
159  
160 This role provides the following methods, where C<$keyword> is the
161 C<keyword> parameter, above:
162  
163 =head2 $query->$keyword()
164  
165 $union->union($select1, $select2, $select3);
166  
167 $union->union($select, $except->except($select2, $select3));
168  
169 Adds C<SELECT> queries or set operations to the list of queries that this set operation
170 includes.
171  
172 A set operation must include at least two queries, so the first time
173 this is called, at least two arguments must be provided; subsequent
174 calls do not suffer this constraint.
175  
176 =head2 $query->all()
177  
178 Sets whether or not C<ALL> is included in the SQL for this set
179 operation (e.g. C<UNION ALL>).
180  
181 =head2 $query->is_all()
182  
183 Returns true if C<< $query->all() >> has previously been called.
184  
185 =head2 $query->keyword_clause()
186  
187 Returns the SQL keyword and possible C<ALL> for this set operation.
188  
189 =head2 $query->${keyword}_clause()
190  
191 print $query->union_clause();
192  
193 Returns each of the selects for this set operation, joined by the
194 C<keyword_clause>.
195  
196 =head1 ROLES
197  
198 This class includes C<Fey::Role::SQL::HasOrderByClause>,
199 C<Fey::Role::SQL::HasLimitClause>, and C<Fey::Role::SQL::HasAliasName>.
200  
201 =head1 BUGS
202  
203 See L<Fey> for details on how to report bugs.
204  
205 =head1 AUTHOR
206  
207 Dave Rolsky <autarch@urth.org>
208  
209 =head1 COPYRIGHT AND LICENSE
210  
211 This software is Copyright (c) 2011 - 2015 by Dave Rolsky.
212  
213 This is free software, licensed under:
214  
215 The Artistic License 2.0 (GPL Compatible)
216  
217 =cut
« Fey/FK.pm ↑ Summary Fey/Role/SQL/Cloneable.pm »