Composer/Select.pm

filter: Statementstmt 99.4% 166 / 167 Branchbran 89.0% 73 / 82 Conditioncond 72.7% 16 / 22 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 5 totaltotal 90.5% 277 / 306 CC 56 SCAR 27.8
CC
56
Cov
94%
new
31.3
_collect_columns_from_joins
8.0
_populate_joins
7.1
CRAP 16.1
↑ Summary 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 32 ▶sub new {
Subroutine new: called CC=31 SCAR=34.4
Pod undocumented
13 32 my $class = shift;
14 32 my (%params) = @_;
15  
16 32 my $self = { table => $params{from} };
17 32 bless $self, $class;
18  
19 32 $self->{from} = $params{from};
20 32 $self->{columns} = $params{columns};
21  
22 32 $self->{join} = $params{join};
23 $self->{join} = [$self->{join}]
24 32 ▶ if $self->{join} && ref $self->{join} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"join"} and ref $self->{"join"} ne "ARRAY" 3 29 32
Condition100%3/3
$self->{"join"} and ref $self->{"join"} ne "ARRAY"
!ll&&!rl&&r
2543
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 32 ▶ $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
0320
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 32 my $sql = '';
30 32 my @bind;
31  
32 my @columns =
33 54 map { $self->_prepare_column($_, $self->{from}, \@bind) }
34 32 @{$self->{columns}};
35 32 push @columns, $self->_collect_columns_from_joins($self->{join});
36  
37 32 $sql .= 'SELECT ';
38  
39 32 ▶ if (@columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@columns) 32 0 32
40 32 $sql .= join ',', @columns;
41 }
42  
43 32 $sql .= ' FROM ';
44 32 $sql .= $self->_quote($params{from});
45  
46 32 ▶ if (my $joins = $self->{join}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $joins = $self->{"join"}) 7 25 32
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 32 ▶ if (my $where = $params{where}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where = $params{"where"}) 1 31 32
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 32 ▶ if (my $group_bys = $params{group_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group_bys = $params{"group_by"}) 4 28 32
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 32 ▶ if (my $having = $params{having}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $having = $params{"having"}) 1 31 32
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 32 ▶ if (my $order_by = $params{order_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $order_by = $params{"order_by"}) 8 24 32
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 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 5 6
109 6 $order_type = uc($value);
110 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
111 4 $order_type = " $order_type";
112 }
113 else {
114 2 $order_type = '';
115 }
116 }
117  
118 7 ▶ if (ref($key) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq 'SCALAR') { } 1 6 7
119 1 push @order, $$key . $order_type;
120 }
121 else {
122 push @order,
123 6 $self->_quote($key, $self->{from}) . $order_type;
124 }
125 }
126 6 $sql .= join ',', @order;
127 }
128 elsif (ref($order_by) eq 'SCALAR') {
129 1 $sql .= $$order_by;
130 }
131 else {
132 0 Carp::croak('unexpected reference');
133 }
134 }
135 else {
136 1 $sql .= $self->_quote($order_by);
137 }
138 }
139  
140 32 ▶ if (defined(my $limit = $params{limit})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $limit = $params{"limit"})) 5 27 32
141 5 ▶ $limit = 0 unless $limit =~ m/^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit =~ /^[0-9]+$/ 1 4 5
142 5 $sql .= ' LIMIT ' . $limit;
143 }
144  
145 32 ▶ if (defined(my $offset = $params{offset})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $offset = $params{"offset"})) 3 29 32
146 3 ▶ $offset = 0 unless $offset =~ m/^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $offset =~ /^[0-9]+$/ 1 2 3
147 3 $sql .= ' OFFSET ' . $offset;
148 }
149  
150 32 ▶ if ($params{for_update}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"for_update"}) 1 31 32
151 1 $sql .= ' FOR UPDATE';
152 }
153  
154 32 $self->{sql} = $sql;
155 32 $self->{bind} = \@bind;
156  
157 32 return $self;
158 }
159  
160 1 ▶sub table { shift->{table} }
Subroutine table: called CC=1 SCAR=0.0
Pod undocumented
161  
162 31 ▶sub to_sql { shift->{sql} }
Subroutine to_sql: called CC=1 SCAR=0.0
Pod undocumented
163 31 ▶sub to_bind { @{shift->{bind} || []} }
Subroutine to_bind: called CC=2 SCAR=7.2
Pod undocumented
Condition50%1/2
+(shift())->{'bind'} || []
l!l
310
MC/DC0%0/1
+(shift())->{'bind'} || []
+(shift())->{'bind'}
Truth table50%1/2
A
A= +(shift())->{'bind'}
Aresult
11
00
164  
165 10 ▶sub from_rows {
Subroutine from_rows: called CC=2 SCAR=6.9
Pod undocumented
166 10 my $self = shift;
167 10 my ($rows) = @_;
168  
169 10 my $result = [];
170 10 foreach my $row (@$rows) {
171 10 my $set = {};
172  
173 10 $self->_populate($set, $row, $self->{columns});
174  
175 10 $self->_populate_joins($set, $row, $self->{join});
176  
177 10 push @$result, $set;
178 }
179  
180 10 return $result;
181 }
182  
183 63 ▶sub _prepare_column {
Subroutine _prepare_column: called CC=5 SCAR=16.1
184 63 my $self = shift;
185 63 my ($column, $prefix, $bind) = @_;
186  
187 63 ▶ if (ref $column eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $column eq 'SCALAR') { } 1 62 63
elsif (ref $column eq 'HASH') { } 3 59 62
188 1 return $$column;
189 }
190 elsif (ref $column eq 'HASH') {
191 return (
192 ref($column->{-col})
193 ? (
194 do {
195 2 my $value = $column->{-col};
196 2 ▶ if (ref $$value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$value eq 'ARRAY') { } 1 1 2
197 1 my $sql = $$value->[0];
198 1 push @$bind, @$$value[1 .. $#{$$value}];
199 1 $sql;
200 }
201 else {
202 1 $$value;
203 }
204 }
205 )
206 : $self->_quote($column->{-col}, $prefix)
207 )
208 . ' AS '
209 3 ▶ . $self->_quote($column->{-as});
Branch: 1 branch
BranchTrueFalseTotal
ref $column->{'-col'} ? : 2 1 3
210 }
211 else {
212 59 return $self->_quote($column, $prefix);
213 }
214 }
215  
216 19 ▶sub _populate {
Subroutine _populate: called CC=4 SCAR=13.9
217 19 my $self = shift;
218 19 my ($set, $row, $columns) = @_;
219  
220 19 my $name;
221 19 foreach my $column (@$columns) {
222 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
223 2 $name = $column->{-as};
224 }
225 elsif (ref($column) eq 'SCALAR') {
226 1 $name = $$column;
227 }
228 else {
229 17 $name = $column;
230 }
231  
232 20 $set->{$name} = shift @$row;
233 }
234 }
235  
236 13 ▶sub _populate_joins {
Subroutine _populate_joins: called CC=7 SCAR=19.7
237 13 my $self = shift;
238 13 my ($set, $row, $joins) = @_;
239  
240 13 foreach my $join (@$joins) {
241 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
242  
243 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
244 9 $self->_populate($set->{$join_source}, $row, $join->{columns});
245  
246 9 ▶ if (my $subjoins = $join->{join}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subjoins = $join->{"join"}) 3 6 9
247 3 ▶ $subjoins = [$subjoins] unless ref $subjoins eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subjoins eq "ARRAY" 0 3 3
248  
249 3 $self->_populate_joins($set->{$join_source}, $row, $subjoins);
250 }
251 }
252 }
253  
254 35 ▶sub _collect_columns_from_joins {
Subroutine _collect_columns_from_joins: called CC=8 SCAR=20.8
255 35 my $self = shift;
256 35 my ($joins) = @_;
257  
258 35 ▶ return () unless $joins && @$joins;
Branch: 1 branch
BranchTrueFalseTotal
unless $joins and @$joins 25 10 35
Condition66%2/3
$joins and @$joins
!ll&&!rl&&r
25010
MC/DC50%1/2
$joins and @$joins
$joins @$joins
Truth table66%2/3
A and B
A= $joinsB= @$joins
ABresult
0X0
100
111
259  
260 10 my @join_columns;
261 10 foreach my $join_params (@$joins) {
262 11 ▶ if (my $join_columns = $join_params->{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $join_columns = $join_params->{"columns"}) 9 2 11
263 push @join_columns, map {
264 9 $self->_prepare_column($_,
265 $join_params->{as}
266 ? $join_params->{as}
267 : $join_params->{source})
268 9 ▶ } @$join_columns;
Branch: 1 branch
BranchTrueFalseTotal
$join_params->{'as'} ? : 6 3 9
269 }
270  
271 11 ▶ if (my $subjoins = $join_params->{join}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subjoins = $join_params->{"join"}) 3 8 11
272 3 ▶ $subjoins = [$subjoins] unless ref $subjoins eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subjoins eq "ARRAY" 0 3 3
273  
274 3 push @join_columns, $self->_collect_columns_from_joins($subjoins);
275 }
276 }
277  
278 10 return @join_columns;
279 }
280  
281 10 ▶sub _build_join {
Subroutine _build_join: called CC=4 SCAR=13.9
282 10 my $self = shift;
283 10 my ($joins) = @_;
284  
285 10 ▶ $joins = [$joins] unless ref $joins eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $joins eq "ARRAY" 0 10 10
286  
287 10 my $sql = '';
288 10 my @bind;
289 10 foreach my $join_params (@$joins) {
290 my $join =
291 11 SQL::Composer::Join->new(quoter => $self->{quoter}, %$join_params);
292  
293 11 $sql .= ' ' . $join->to_sql;
294 11 push @bind, $join->to_bind;
295  
296 11 ▶ if (my $subjoin = $join_params->{join}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subjoin = $join_params->{"join"}) 3 8 11
297 3 my ($subsql, $subbind) = $self->_build_join($subjoin);
298 3 $sql .= $subsql;
299 3 push @bind, @$subbind;
300 }
301 }
302  
303 10 return ($sql, \@bind);
304 }
305  
306 106 ▶sub _quote {
Subroutine _quote: called CC=1 SCAR=0.0
307 106 my $self = shift;
308 106 my ($column, $prefix) = @_;
309  
310 106 return $self->{quoter}->quote($column, $prefix);
311 }
312  
313 3 1;
314 __END__
↑ Summary Composer/Update.pm »