JPList/DB/Result.pm

filter: Statementstmt 8.3% 9 / 108 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 22.2% 2 / 9 Podpod 100% 1 / 1 totaltotal 6.0% 12 / 198 CC 36 SCAR 42.1
CC
36
Cov
5%
select_hashref
132.0
_build_where_clause
72.0
_build_sort_order
56.0
CRAP 67.4
↑ Summary JPList/Request.pm »
1 # ========================================================================== #
2 # lib/JPList/Result.pm - JPList DB Result
3 # Copyright (C) 2017 Exceleron Software, LLC
4 # ========================================================================== #
5  
6 package JPList::DB::Result;
7  
8 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ========================================================================== #
12  
13 =head1 NAME
14  
15 JPList::DB::Result - JPList DB Result
16  
17 =head1 VERSION
18  
19 Version 0.05
20  
21 =cut
22  
23 2 our $VERSION = '0.05';
24  
25 =head1 SYNOPSIS
26  
27 with 'JPList::DB::Result'
28  
29 =head1 DESCRIPTION
30  
31 The JPList::DB::Result module allows you get the resultset from table/view
32  
33 =head2 ATTRIBUTES
34  
35 =over 4
36  
37 =cut
38  
39 # ========================================================================== #
40  
41 has 'sql_abs' => (
42 is => 'rw',
43 isa => 'SQL::Abstract',
44 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 SQL::Abstract->new(quote_char => '"');
46 }
47 2 );
48  
49 # ========================================================================== #
50  
51 =back
52  
53 =head2 METHODS
54  
55 =over 4
56  
57 =cut
58  
59 # ========================================================================== #
60  
61 =item C<_get_resultset>
62  
63 Params : $request_data
64  
65 Returns: Resultset data from table/view based on the request params
66  
67 Desc : Resultset data from table/view based on the request params
68  
69 =cut
70  
71 sub _get_resultset
72 0 ▶{
Subroutine _get_resultset: not called CC=6 SCAR=37.4
73 0 my $self = shift;
74 0 my $request_data = shift;
75 0 ▶ my $is_download = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
76  
77 0 my $source = $self->db_table_name;
78  
79 0 my $fields;
80 0 ▶ if ($self->fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fields) { } 0 0 0
81 0 $fields = $self->fields;
82 }
83 else {
84 0 $fields = ['*'];
85 }
86  
87 0 my $where = $self->_build_where_clause($request_data);
88  
89 0 my $order = $self->_build_sort_order($request_data);
90  
91 0 my ($sql_query, @bind_vals) = $self->sql_abs->select($source, $fields, $where);
92  
93 0 ▶ if ($self->group_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->group_fields) 0 0 0
94 0 $sql_query .= " GROUP BY " . $self->group_fields;
95 }
96  
97 0 ▶ if ($order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order) 0 0 0
98 0 $sql_query .= " $order";
99 }
100  
101 0 my $bind_vals = \@bind_vals;
102  
103 0 my $params;
104 0 my $count = 0;
105 0 ▶ if (!$is_download) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_download) { } 0 0 0
106 ## Add Limit Query for jplist data
107 0 ($sql_query, $bind_vals) = $self->_build_limit_query($request_data, $sql_query, \@bind_vals);
108  
109 #Get total count for paging
110 0 $count = $self->_get_paging_count($source, $where);
111 }
112 else {
113 ## Return array of array for csv download
114 0 $params->{return_aoa} = 1;
115 }
116  
117 0 print STDERR "[jplist info] " . $sql_query . "\n";
118 0 print STDERR "[jplist info] bind_vals @bind_vals \n";
119  
120 0 $params->{sql} = $sql_query;
121 0 $params->{bind_vals} = $bind_vals;
122  
123 0 my $data = $self->select_hashref($params);
124  
125 #Return data
126 0 my $return_data = {
127 count => $count,
128 data => $data
129 };
130  
131 0 return $return_data;
132 }
133  
134 # ========================================================================== #
135  
136 =item C<_build_where_clause>
137  
138 Params : $request_data
139  
140 Returns: $where_clause
141  
142 Desc : Builds where_clause based on JPList request "filter_data" and also custom "where_fields"
143  
144 =cut
145  
146 sub _build_where_clause
147 0 ▶{
Subroutine _build_where_clause: not called CC=8 SCAR=42.8
148 0 my $self = shift;
149 0 my $request_data = shift;
150  
151 0 ▶ return $self->{where_clause} if ($self->{where_clause});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"where_clause"} 0 0 0
152  
153 0 my %where;
154  
155 #Custom Where Clause like UtilityId/AccountId/MeterId filter based on auth
156 0 foreach my $column (keys %{$self->where_fields}) {
157 0 $where{$column} = $self->where_fields->{$column};
158 }
159  
160 #Where clause applied based on filter data
161 0 foreach my $data (@{$request_data->filter_data}) {
162 0 ▶ if (exists($data->{'type'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'type'}) { } 0 0 0
163 0 ▶ if ($data->{'type'} eq 'like') {
Branch: 3 branches
BranchTrueFalseTotal
if ($data->{'type'} eq 'like') { } 0 0 0
elsif ($data->{'type'} eq 'between') { } 0 0 0
elsif ($data->{'type'} eq 'in') { } 0 0 0
164 0 $where{$data->{'column'}} = {-like => '%' . $data->{'value'} . '%'};
165 }
166 elsif ($data->{'type'} eq 'between') {
167 0 $where{$data->{'column'}} = {-between => [$data->{'from_date'}, $data->{'to_date'}]};
168 }
169 elsif ($data->{'type'} eq 'in') {
170 0 $where{$data->{'column'}} = {-in => $data->{'values'}};
171 }
172 }
173 else {
174 0 $where{$data->{'column'}} = $data->{'value'};
175 }
176 }
177  
178 0 $self->{where_clause} = \%where;
179  
180 0 return $self->{where_clause};
181 }
182  
183 # ========================================================================== #
184  
185 =item C<_build_sort_order>
186  
187 Params : $request_data
188  
189 Returns: $order
190  
191 Desc : Builds order data structure based on JPList request "sort_data"
192  
193 =cut
194  
195 sub _build_sort_order
196 0 ▶{
Subroutine _build_sort_order: not called CC=7 SCAR=40.3
197 0 my $self = shift;
198 0 my $request_data = shift;
199  
200 0 ▶ my $sort_one = $request_data->sort_data->[0] if (ref($request_data->sort_data) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $request_data->sort_data eq "ARRAY" 0 0 0
201  
202 0 my $order;
203 0 ▶ if ($sort_one && $sort_one->{column} ne 'default') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_one and $sort_one->{"column"} ne "default") 0 0 0
Condition0%0/3
$sort_one and $sort_one->{"column"} ne "default"
!ll&&!rl&&r
000
MC/DC0%0/2
$sort_one and $sort_one->{"column"} ne "default"
$sort_one $sort_one->{"column"} ne "default"
Truth table0%0/3
A and B
A= $sort_oneB= $sort_one->{"column"} ne "default"
ABresult
0X0
100
111
204  
205 0 ▶ if ($self->order_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->order_index) { } 0 0 0
206 0 my $order_index = $self->order_index->{$sort_one->{column}};
207 0 $order = qq/ORDER BY $order_index/;
208 0 ▶ if ($sort_one->{order} eq "desc") {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_one->{"order"} eq "desc") 0 0 0
209  
210 0 $order .= ' DESC';
211  
212 }
213 }
214 else {
215 0 $order = qq/ORDER BY "$sort_one->{column}"/;
216 0 ▶ if ($sort_one->{order} eq "desc") {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_one->{"order"} eq "desc") 0 0 0
217  
218 0 $order .= ' DESC';
219  
220 }
221 }
222  
223 }
224  
225 0 return $order;
226 }
227  
228 # ========================================================================== #
229  
230 =item C<_build_limit_query>
231  
232 Params : $request_data, $sql_query, $bind_vals
233  
234 Returns: $sql_query, $bind_vals
235  
236 Desc : Builds SQL updated with LIMIT and OFFEST based on JPList request "pagination_data"
237  
238 =cut
239  
240 sub _build_limit_query
241 0 ▶{
Subroutine _build_limit_query: not called CC=6 SCAR=37.4
242 0 my $self = shift;
243 0 my $request_data = shift;
244 0 my $sql_query = shift;
245 0 my $bind_vals = shift;
246  
247 0 ▶ my $number = $request_data->pagination_data->{'number'} || 10;
Condition0%0/2
$request_data->pagination_data->{'number'} || 10
l!l
00
MC/DC0%0/1
$request_data->pagination_data->{'number'} || 10
$request_data->pagination_data->{'number'}
Truth table0%0/2
A
A= $request_data->pagination_data->{'number'}
Aresult
11
00
248 0 ▶ my $current_page = $request_data->pagination_data->{'currentPage'} || 0;
Condition0%0/2
$request_data->pagination_data->{'currentPage'} || 0
l!l
00
MC/DC0%0/1
$request_data->pagination_data->{'currentPage'} || 0
$request_data->pagination_data->{'currentPage'}
Truth table0%0/2
A
A= $request_data->pagination_data->{'currentPage'}
Aresult
11
00
249  
250 0 my $limit = $number;
251 0 ▶ my $offset = ($limit * $current_page) || 0;
Condition0%0/2
$limit * $current_page || 0
l!l
00
MC/DC0%0/1
$limit * $current_page || 0
$limit * $current_page
Truth table0%0/2
A
A= $limit * $current_page
Aresult
11
00
252  
253 0 ▶ if ($limit and $limit ne 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit and $limit ne "all") 0 0 0
Condition0%0/3
$limit and $limit ne "all"
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and $limit ne "all"
$limit $limit ne "all"
Truth table0%0/3
A and B
A= $limitB= $limit ne "all"
ABresult
0X0
100
111
254 0 $sql_query .= " LIMIT ? OFFSET ?";
255 0 push(@$bind_vals, ($limit, $offset));
256 }
257  
258 0 return ($sql_query, $bind_vals);
259 }
260  
261 # ========================================================================== #
262  
263 =item C<_get_paging_count>
264  
265 Params : $table_source, $wheres
266  
267 Returns: total count based on filter
268  
269 Desc : total count based on filter
270  
271 =cut
272  
273 sub _get_paging_count
274 0 ▶{
Subroutine _get_paging_count: not called CC=3 SCAR=24.8
275 0 my $self = shift;
276 0 my $source = shift;
277 0 my $where = shift;
278  
279 0 my ($sql_count_query, @bind_count_vals);
280  
281 0 ▶ if ($self->group_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->group_fields) { } 0 0 0
282 0 my $group_fields = $self->group_fields;
283  
284 0 ($sql_count_query, @bind_count_vals) = $self->sql_abs->select($source, $group_fields, $where);
285 0 $sql_count_query = "SELECT COUNT(*) FROM ($sql_count_query GROUP BY $group_fields) AS SQ";
286  
287 }
288 else {
289  
290 0 ($sql_count_query, @bind_count_vals) = $self->sql_abs->select($source, 'count(*)', $where);
291 }
292  
293 0 my $data = $self->select_hashref(
294 {
295 sql => $sql_count_query,
296 bind_vals => \@bind_count_vals,
297 return_aoa => 1
298 }
299 );
300  
301 0 ▶ my $count = $data->[0][0] || 0;
Condition0%0/2
$data->[0][0] || 0
l!l
00
MC/DC0%0/1
$data->[0][0] || 0
$data->[0][0]
Truth table0%0/2
A
A= $data->[0][0]
Aresult
11
00
302  
303 0 return $count;
304 }
305  
306 # ========================================================================== #
307  
308 =item C<select_hashref>
309  
310 Params :
311  
312 {
313 sql => 'SQL Statement',
314 bind_vals => ARRAYREF[optional],
315 return_key => Column [optional],
316 return_aoa => 1 [optional]
317 }
318  
319 Returns: ARRAYREF/HASHREF of data
320  
321 - If return_key is undef : Returns rows as Array of Hash
322 - If return_key has a valid column: Returns rows as Hash of Hash with Column value as hash key
323 - If return_aoa is 1 : Returns rows as Array of Array
324  
325 Desc : Using SQL get hashref of data
326  
327 =cut
328  
329 sub select_hashref
330 0 ▶{
Subroutine select_hashref: not called CC=11 SCAR=48.8
Pod documented
331 0 my $self = shift;
332 0 my $params = shift;
333 0 my $sql = $params->{sql};
334 0 ▶ my $bind_vals = (defined($params->{'bind_vals'}) && exists($params->{'bind_vals'})) ? $params->{'bind_vals'} : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{'bind_vals'} && exists $params->{'bind_vals'} ? : 0 0 0
Condition0%0/3
defined $params->{"bind_vals"} and exists $params->{"bind_vals"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params->{"bind_vals"} and exists $params->{"bind_vals"}
defined $params->{"bind_vals"} exists $params->{"bind_vals"}
Truth table0%0/3
A and B
A= defined $params->{"bind_vals"}B= exists $params->{"bind_vals"}
ABresult
0X0
100
111
335  
336 0 my $dbh = $self->dbh;
337  
338 0 my $data;
339 0 ▶ my $sth = $dbh->prepare_cached($sql) or die("$!");
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare_cached($sql) or die "$!" 0 0 0
340  
341 0 ▶ for (my $i = 0; $i < scalar(@$bind_vals); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$bind_vals) 0 0 0
342 0 ▶ $sth->bind_param($i + 1, $bind_vals->[$i]) or die("$!");
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param($i + 1, $bind_vals->[$i]) or die "$!" 0 0 0
343 }
344  
345 0 ▶ if (defined $params->{return_key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $params->{'return_key'}) { } 0 0 0
elsif (defined $params->{'return_aoa'}) { } 0 0 0
346 0 ▶ $data = $dbh->selectall_hashref($sth, $params->{return_key}) or die("$!");
Branch: 1 branch
BranchTrueFalseTotal
$data = $dbh->selectall_hashref($sth, $params->{"return_key"}) or die "$!" 0 0 0
347 }
348 elsif (defined $params->{return_aoa}) {
349 0 ▶ $data = $dbh->selectall_arrayref($sth) or die("$!");
Branch: 1 branch
BranchTrueFalseTotal
$data = $dbh->selectall_arrayref($sth) or die "$!" 0 0 0
350 }
351 else {
352 0 ▶ $data = $dbh->selectall_arrayref($sth, {Slice => {}}) or die("$!");
Branch: 1 branch
BranchTrueFalseTotal
$data = $dbh->selectall_arrayref($sth, {"Slice", {}}) or die "$!" 0 0 0
353 }
354
355 0 return $data;
356 }
357  
358 # ========================================================================== #
359  
360  
361 2 1;
362  
363 __END__
↑ Summary JPList/Request.pm »