CrossTab/Util.pm

filter: Statementstmt 100% 57 / 57 Branchbran 100% 12 / 12 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 4 totaltotal 83.5% 81 / 97 CC 11 SCAR 16.1
CC
11
Cov
92%
parse_summary_function
9.0
summary_function_to_pivot_field_func
2.0
summary_function_to_pivot_field_name
2.0
CRAP 5.0
↑ Summary CrossTab.pm »
1 package DBIx::Class::Helper::ResultSet::CrossTab::Util;
2  
3 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 our @EXPORT = qw(summary_function_to_pivot_field_def summary_function_to_pivot_field_func summary_function_to_pivot_field_name); # symbols to export on request
5  
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Text::Balanced qw (extract_bracketed) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # use Data::Dump qw/dump/;
10  
11 7 ▶use String::SQLColumnName qw/fix_name/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 44 ▶my $quote = sub { sprintf "'%s'", shift };
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 34 ▶sub summary_function_to_pivot_field_def {
Subroutine summary_function_to_pivot_field_def: called CC=1 SCAR=0.0
Pod undocumented
16 34 my $func = summary_function_to_pivot_field_func(@_);
17 34 my $name = summary_function_to_pivot_field_name(@_);
18  
19 34 return "$func as $name";
20 }
21  
22 44 ▶sub summary_function_to_pivot_field_func {
Subroutine summary_function_to_pivot_field_func: called CC=2 SCAR=7.1
Pod undocumented
23 44 my $pivot = shift;
24 44 my $field = shift;
25 44 my $value = shift;
26 44 ▶ my $quote = shift || $quote;
Condition33%1/3
shift() || $quote
l!l&&r!l&&!r
0440
MC/DC0%0/2
shift() || $quote
shift() $quote
Truth table33%1/3
A || B
A= shift()B= $quote
ABresult
1X1
011
000
27  
28 44 my ($aggs, $clause, $extracted, $rems) = parse_summary_function($pivot, $field);
29  
30 44 $extracted = sprintf '(%s CASE WHEN %s=%s then %s ELSE NULL END)', $clause, $field, $quote->($value), $extracted;
31 44 my $res = join '', (join '(', @$aggs), $extracted, (join ')', @$rems)
32 }
33  
34 122 ▶sub parse_summary_function {
Subroutine parse_summary_function: called CC=9 SCAR=22.0
Pod undocumented
35 122 my $pivot = shift;
36 122 my $field = shift;
37 122 my $value = shift;
38 122 ▶ my $quote = shift || $quote;
Condition33%1/3
shift() || $quote
l!l&&r!l&&!r
01220
MC/DC0%0/2
shift() || $quote
shift() $quote
Truth table33%1/3
A || B
A= shift()B= $quote
ABresult
1X1
011
000
39  
40 122 my (@aggs, @rems, $extracted);
41  
42 122 ▶ if (ref $pivot eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pivot eq "HASH") 62 60 122
43 62 my ($sum_func, $sum_field) = (%$pivot);
44 62 return [$sum_func], '', $sum_field, [];
45 }
46  
47 60 ▶ $pivot = $$pivot if (ref $pivot eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if ref $pivot eq "SCALAR" 42 18 60
48  
49 60 my (@aggs, @rems, $extracted);
50 60 ▶ while ($pivot =~ s/\s*(\w+)\s*\(//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pivot =~ s/\s*(\w+)\s*\(//) 76 60 136
51 76 push @aggs, $1;
52 76 my $remainder;
53 76 ($extracted, $remainder) = extract_bracketed('(' . $pivot, '()');
54 76 ▶ unshift @rems, $remainder if $remainder;
Branch: 1 branch
BranchTrueFalseTotal
if $remainder 16 60 76
55 }
56  
57 60 my $clause = '';
58 60 for ($extracted) {
59 60 ▶ s/\(\s*distinct\s/(/ && do {
Branch: 1 branch
BranchTrueFalseTotal
s/\(\s*distinct\s/(/ and do { $clause = "distinct"; next } 2 58 60
60 2 $clause = 'distinct';
61 2 next;
62 };
63 58 ▶ s/\(\s*all\s/(/ && do {
Branch: 1 branch
BranchTrueFalseTotal
s/\(\s*all\s/(/ and do { $clause = "all"; next } 2 56 58
64 2 $clause = 'all';
65 2 next;
66 };
67 }
68 60 return \@aggs, $clause, $extracted, \@rems;
69 }
70  
71 78 ▶sub summary_function_to_pivot_field_name {
Subroutine summary_function_to_pivot_field_name: called CC=2 SCAR=7.1
Pod undocumented
72 78 my $pivot = shift;
73 78 my $field = shift;
74 78 my $value = shift;
75 78 ▶ my $quote = shift || $quote;
Condition33%1/3
shift() || $quote
l!l&&r!l&&!r
0780
MC/DC0%0/2
shift() || $quote
shift() $quote
Truth table33%1/3
A || B
A= shift()B= $quote
ABresult
1X1
011
000
76  
77 78 my ($aggs, $clause, $extracted, $rems) = parse_summary_function($pivot, $field);
78  
79 78 my $res = join '_', (join '_', @$aggs), $extracted, $field, $value;
80 78 return fix_name($res);
81 }
82  
83 7 1
↑ Summary CrossTab.pm »