| 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
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $source = $self->db_table_name; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | my $fields; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($self->fields) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 94 | 0 | $sql_query .= " GROUP BY " . $self->group_fields; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($order) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if ($data->{'type'} eq 'like') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | my $order; | |||||||||||||||||||||||||||
| 203 | 0 | ▶ | if ($sort_one && $sort_one->{column} ne 'default') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$sort_one and $sort_one->{"column"} ne "default"
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"
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if ($self->order_index) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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'}
| |||||||||||||||||||||||||||||
| 248 | 0 | ▶ | my $current_page = $request_data->pagination_data->{'currentPage'} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$request_data->pagination_data->{'currentPage'} || 0
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'}
| |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 0 | my $limit = $number; | |||||||||||||||||||||||||||
| 251 | 0 | ▶ | my $offset = ($limit * $current_page) || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$limit * $current_page || 0
MC/DC0%0/1
$limit * $current_page || 0
$limit * $current_page
Truth table0%0/2
A
A= $limit * $current_page
| |||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 0 | ▶ | if ($limit and $limit ne 'all') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$limit and $limit ne "all"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$data->[0][0] || 0
$data->[0][0]
Truth table0%0/2
A
A= $data->[0][0]
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $params->{"bind_vals"} and exists $params->{"bind_vals"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | ▶ | for (my $i = 0; $i < scalar(@$bind_vals); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | 0 | ▶ | $sth->bind_param($i + 1, $bind_vals->[$i]) or die("$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 0 | ▶ | if (defined $params->{return_key}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 346 | 0 | ▶ | $data = $dbh->selectall_hashref($sth, $params->{return_key}) or die("$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | elsif (defined $params->{return_aoa}) { | ||||||||||||||||||||||||||||
| 349 | 0 | ▶ | $data = $dbh->selectall_arrayref($sth) or die("$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | else { | ||||||||||||||||||||||||||||
| 352 | 0 | ▶ | $data = $dbh->selectall_arrayref($sth, {Slice => {}}) or die("$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 0 | return $data; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | # ========================================================================== # | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 2 | 1; | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | __END__ | ||||||||||||||||||||||||||||