blib/lib/SQL/Composer/Select.pm

filter: Statementstmt 99.3% 163 / 164 Branchbran 87.1% 68 / 78 Conditioncond 77.2% 17 / 22 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 5 totaltotal 90.6% 271 / 299 CC 54 SCAR 27.0
CC
54
Cov
94%
new
29.3
_collect_columns_from_joins
8.0
_populate_joins
7.1
CRAP 14.8
↑ Summary blib/lib/SQL/Composer/Update.pm »
1 package SQL::Composer::Select;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 require Carp;
7 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use SQL::Composer::Join;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use SQL::Composer::Expression;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use SQL::Composer::Quoter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 31 ▶ my $class = shift;
Subroutine new: called CC=29 SCAR=33.8
Pod undocumented
14 31 my (%params) = @_;
15  
16 31 my $self = { table => $params{from} };
17 31 bless $self, $class;
18  
19 31 $self->{from} = $params{from};
20 31 $self->{columns} = $params{columns};
21  
22 31 $self->{join} = $params{join};
23 $self->{join} = [$self->{join}]
24 31 ▶ if $self->{join} && ref $self->{join} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"join"} and ref $self->{"join"} ne "ARRAY" 3 28 31
Condition100%3/3
$self->{"join"} and ref $self->{"join"} ne "ARRAY"
!ll&&!rl&&r
2443
MC/DC100%2/2
$self->{"join"} and ref $self->{"join"} ne "ARRAY"
$self->{"join"} ref $self->{"join"} ne "ARRAY"
Truth table100%3/3
A and B
A= $self->{"join"}B= ref $self->{"join"} ne "ARRAY"
ABresult
0X0
100
111
25  
26 $self->{quoter} =
27 31 ▶ $params{quoter} || SQL::Composer::Quoter->new(driver => $params{driver});
Condition33%1/3
$params{'quoter'} || 'SQL::Composer::Quoter'->new('driver', $params{'driver'})
l!l&&r!l&&!r
0310
MC/DC0%0/2
$params{'quoter'} || 'SQL::Composer::Quoter'->new('driver', $params{'driver'})
$params{'quoter'} 'SQL::Composer::Quoter'->new('driver', $params{'driver'})
Truth table33%1/3
A || B
A= $params{'quoter'}B= 'SQL::Composer::Quoter'->new('driver', $params{'driver'})
ABresult
1X1
011
000
28  
29 31 my $sql = '';
30 31 my @bind;
31  
32 my @columns =
33 52 map { $self->_prepare_column($_, $self->{from}, \@bind) }
34 31 @{$self->{columns}};
35 31 push @columns, $self->_collect_columns_from_joins($self->{join});
36  
37 31 $sql .= 'SELECT ';
38  
39 31 ▶ if (@columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@columns) 31 0 31
40 31 $sql .= join ',', @columns;
41 }
42  
43 31 $sql .= ' FROM ';
44 31 $sql .= $self->_quote($params{from});
45  
46 31 ▶ if (my $joins = $self->{join}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $joins = $self->{"join"}) 7 24 31
47 7 my ($join_sql, $join_bind) = $self->_build_join($joins);
48 7 $sql .= $join_sql;
49 7 push @bind, @$join_bind;
50 }
51  
52 31 ▶ if (my $where = $params{where}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where = $params{"where"}) 1 30 31
53 1 ▶ if (!Scalar::Util::blessed($where)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($where)) 1 0 1
54 $where = SQL::Composer::Expression->new(
55 default_prefix => $self->{from},
56 quoter => $self->{quoter},
57 1 expr => $where
58 );
59 }
60  
61 1 ▶ if (my $where_sql = $where->to_sql) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where_sql = $where->to_sql) 1 0 1
62 1 $sql .= ' WHERE ' . $where_sql;
63 1 push @bind, $where->to_bind;
64 }
65 }
66  
67 31 ▶ if (my $group_bys = $params{group_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group_bys = $params{"group_by"}) 4 27 31
68 4 ▶ $group_bys = [$group_bys] unless ref $group_bys eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $group_bys eq "ARRAY" 3 1 4
69  
70 4 my @group_by;
71 4 foreach my $group_by (@$group_bys) {
72 push @group_by,
73 ref($group_by)
74 ? $$group_by
75 6 ▶ : $self->_quote($group_by, $self->{from});
Branch: 1 branch
BranchTrueFalseTotal
ref $group_by ? : 2 4 6
76 }
77  
78 4 $sql .= ' GROUP BY ' . join(', ', @group_by);
79 }
80  
81 31 ▶ if (my $having = $params{having}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $having = $params{"having"}) 1 30 31
82 1 ▶ if (!Scalar::Util::blessed($having)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($having)) 1 0 1
83 $having = SQL::Composer::Expression->new(
84 default_prefix => $self->{from},
85 quoter => $self->{quoter},
86 1 expr => $having
87 );
88 }
89  
90 1 ▶ if (my $having_sql = $having->to_sql) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $having_sql = $having->to_sql) 1 0 1
91 1 $sql .= ' HAVING ' . $having->to_sql;
92 1 push @bind, $having->to_bind;
93 }
94 }
95  
96 31 ▶ if (my $order_by = $params{order_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $order_by = $params{"order_by"}) 8 23 31
97 8 $sql .= ' ORDER BY ';
98 8 ▶ if (ref $order_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $order_by) { } 7 1 8
99 7 ▶ if (ref($order_by) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $order_by eq 'ARRAY') { } 6 1 7
elsif (ref $order_by eq 'SCALAR') { } 1 0 1
100 6 my @order;
101 6 ▶ while (my ($key, $value) = splice @$order_by, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$order_by, 0, 2)) 7 6 13
102 7 my $order_type;
103  
104 7 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 1 6 7
105 1 $order_type = ' ' . $$value;
106 }
107 else {
108 6 ▶ $order_type = uc($value // '');
Condition100%2/2
$value // ""
l!l
51
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
109 6 ▶ if ($order_type eq 'ASC' || $order_type eq 'DESC') {
Branch: 1 branch
BranchTrueFalseTotal
if ($order_type eq 'ASC' or $order_type eq 'DESC') { } 4 2 6
Condition100%3/3
$order_type eq "ASC" or $order_type eq "DESC"
l!l&&r!l&&!r
132
MC/DC100%2/2
$order_type eq "ASC" or $order_type eq "DESC"
$order_type eq "ASC" $order_type eq "DESC"
Truth table100%3/3
A or B
A= $order_type eq "ASC"B= $order_type eq "DESC"
ABresult
1X1
011
000
110 4 $order_type = " $order_type";
111 }
112 else {
113 2 $order_type = '';
114 }
115 }
116  
117 7 ▶ if (ref($key) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq 'SCALAR') { } 1 6 7
118 1 push @order, $$key . $order_type;
119 }
120 else {
121 push @order,
122 6 $self->_quote($key, $self->{from}) . $order_type;
123 }
124 }
125 6 $sql .= join ',', @order;
126 }
127 elsif (ref($order_by) eq 'SCALAR') {
128 1 $sql .= $$order_by;
129 }
130 else {
131 0 Carp::croak('unexpected reference');
132 }
133 }
134 else {
135 1 $sql .= $self->_quote($order_by);
136 }
137 }
138  
139 31 ▶ if (defined(my $limit = $params{limit})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $limit = $params{"limit"})) 4 27 31
140 4 $sql .= ' LIMIT ' . $limit;
141 }
142  
143 31 ▶ if (defined(my $offset = $params{offset})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $offset = $params{"offset"})) 2 29 31
144 2 $sql .= ' OFFSET ' . $offset;
145 }
146  
147 31 ▶ if ($params{for_update}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"for_update"}) 1 30 31
148 1 $sql .= ' FOR UPDATE';
149 }
150  
151 31 $self->{sql} = $sql;
152 31 $self->{bind} = \@bind;
153  
154 31 return $self;
155 }
156  
157 1 ▶sub table { shift->{table} }
Subroutine table: called CC=1 SCAR=0.0
Pod undocumented
158  
159 30 ▶sub to_sql { shift->{sql} }
Subroutine to_sql: called CC=1 SCAR=0.0
Pod undocumented
160 30 ▶sub to_bind { @{shift->{bind} || []} }
Subroutine to_bind: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"bind"} or [] 0 30 30
161  
162 sub from_rows {
163 10 ▶ my $self = shift;
Subroutine from_rows: called CC=2 SCAR=6.9
Pod undocumented
164 10 my ($rows) = @_;
165  
166 10 my $result = [];
167 10 foreach my $row (@$rows) {
168 10 my $set = {};
169  
170 10 $self->_populate($set, $row, $self->{columns});
171  
172 10 $self->_populate_joins($set, $row, $self->{join});
173  
174 10 push @$result, $set;
175 }
176  
177 10 return $result;
178 }
179  
180 sub _prepare_column {
181 61 ▶ my $self = shift;
Subroutine _prepare_column: called CC=5 SCAR=16.1
182 61 my ($column, $prefix, $bind) = @_;
183  
184 61 ▶ if (ref $column eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $column eq 'SCALAR') { } 1 60 61
elsif (ref $column eq 'HASH') { } 3 57 60
185 1 return $$column;
186 }
187 elsif (ref $column eq 'HASH') {
188 return (
189 ref($column->{-col})
190 ? (
191 do {
192 2 my $value = $column->{-col};
193 2 ▶ if (ref $$value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$value eq 'ARRAY') { } 1 1 2
194 1 my $sql = $$value->[0];
195 1 push @$bind, @$$value[1 .. $#{$$value}];
196 1 $sql;
197 }
198 else {
199 1 $$value;
200 }
201 }
202 )
203 : $self->_quote($column->{-col}, $prefix)
204 )
205 . ' AS '
206 3 ▶ . $self->_quote($column->{-as});
Branch: 1 branch
BranchTrueFalseTotal
ref $column->{'-col'} ? : 2 1 3
207 }
208 else {
209 57 return $self->_quote($column, $prefix);
210 }
211 }
212  
213 sub _populate {
214 19 ▶ my $self = shift;
Subroutine _populate: called CC=4 SCAR=13.9
215 19 my ($set, $row, $columns) = @_;
216  
217 19 my $name;
218 19 foreach my $column (@$columns) {
219 20 ▶ if (ref($column) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $column eq 'HASH') { } 2 18 20
elsif (ref $column eq 'SCALAR') { } 1 17 18
220 2 $name = $column->{-as};
221 }
222 elsif (ref($column) eq 'SCALAR') {
223 1 $name = $$column;
224 }
225 else {
226 17 $name = $column;
227 }
228  
229 20 $set->{$name} = shift @$row;
230 }
231 }
232  
233 sub _populate_joins {
234 13 ▶ my $self = shift;
Subroutine _populate_joins: called CC=7 SCAR=19.7
235 13 my ($set, $row, $joins) = @_;
236  
237 13 foreach my $join (@$joins) {
238 9 ▶ my $join_source = $join->{rel_name} || $join->{as} || $join->{source};
Condition83%5/6
$join->{'rel_name'} || $join->{'as'} || $join->{'source'}
l!l&&r!l&&!r
630
$join->{"rel_name"} or $join->{"as"}
l!l&&r!l&&!r
243
MC/DC0%0/3
$join->{'rel_name'} || $join->{'as'} || $join->{'source'}
$join->{"rel_name"} $join->{"as"} $join->{'source'}
Truth table75%3/4
A or B || C
A= $join->{"rel_name"}B= $join->{"as"}C= $join->{'source'}
ABCresult
1XX1
01X1
0011
0000
239  
240 9 ▶ $set->{$join_source} ||= {};
Condition50%1/2
$set->{$join_source} ||= {}
l!l
09
MC/DC0%0/1
$set->{$join_source} ||= {}
$set->{$join_source}
Truth table50%1/2
A
A= $set->{$join_source}
Aresult
11
00
241 9 $self->_populate($set->{$join_source}, $row, $join->{columns});
242  
243 9 ▶ if (my $subjoins = $join->{join}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subjoins = $join->{"join"}) 3 6 9
244 3 ▶ $subjoins = [$subjoins] unless ref $subjoins eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subjoins eq "ARRAY" 0 3 3
245  
246 3 $self->_populate_joins($set->{$join_source}, $row, $subjoins);
247 }
248 }
249 }
250  
251 sub _collect_columns_from_joins {
252 34 ▶ my $self = shift;
Subroutine _collect_columns_from_joins: called CC=8 SCAR=20.8
253 34 my ($joins) = @_;
254  
255 34 ▶ return () unless $joins && @$joins;
Branch: 1 branch
BranchTrueFalseTotal
unless $joins and @$joins 24 10 34
Condition66%2/3
$joins and @$joins
!ll&&!rl&&r
24010
MC/DC50%1/2
$joins and @$joins
$joins @$joins
Truth table66%2/3
A and B
A= $joinsB= @$joins
ABresult
0X0
100
111
256  
257 10 my @join_columns;
258 10 foreach my $join_params (@$joins) {
259 11 ▶ if (my $join_columns = $join_params->{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $join_columns = $join_params->{"columns"}) 9 2 11
260 push @join_columns, map {
261 9 $self->_prepare_column($_,
262 $join_params->{as}
263 ? $join_params->{as}
264 : $join_params->{source})
265 9 ▶ } @$join_columns;
Branch: 1 branch
BranchTrueFalseTotal
$join_params->{'as'} ? : 6 3 9
266 }
267  
268 11 ▶ if (my $subjoins = $join_params->{join}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subjoins = $join_params->{"join"}) 3 8 11
269 3 ▶ $subjoins = [$subjoins] unless ref $subjoins eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subjoins eq "ARRAY" 0 3 3
270  
271 3 push @join_columns, $self->_collect_columns_from_joins($subjoins);
272 }
273 }
274  
275 10 return @join_columns;
276 }
277  
278 sub _build_join {
279 10 ▶ my $self = shift;
Subroutine _build_join: called CC=4 SCAR=13.9
280 10 my ($joins) = @_;
281  
282 10 ▶ $joins = [$joins] unless ref $joins eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $joins eq "ARRAY" 0 10 10
283  
284 10 my $sql = '';
285 10 my @bind;
286 10 foreach my $join_params (@$joins) {
287 my $join =
288 11 SQL::Composer::Join->new(quoter => $self->{quoter}, %$join_params);
289  
290 11 $sql .= ' ' . $join->to_sql;
291 11 push @bind, $join->to_bind;
292  
293 11 ▶ if (my $subjoin = $join_params->{join}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subjoin = $join_params->{"join"}) 3 8 11
294 3 my ($subsql, $subbind) = $self->_build_join($subjoin);
295 3 $sql .= $subsql;
296 3 push @bind, @$subbind;
297 }
298 }
299  
300 10 return ($sql, \@bind);
301 }
302  
303 sub _quote {
304 103 ▶ my $self = shift;
Subroutine _quote: called CC=1 SCAR=0.0
305 103 my ($column, $prefix) = @_;
306  
307 103 return $self->{quoter}->quote($column, $prefix);
308 }
309  
310 3 1;
311 __END__
↑ Summary blib/lib/SQL/Composer/Update.pm »