blib/lib/ObjectDB/Util.pm

filter: Statementstmt 74.7% 74 / 99 Branchbran 71.7% 33 / 46 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 6 totaltotal 71.7% 127 / 177 SCAR 38.7
CC
31
Cov
74%
force_arrayrefs
20.0
merge_rows
13.0
load_class
10.7
CRAP 47.8
« blib/lib/ObjectDB/With.pm ↑ Summary blib/lib/ObjectDB/Meta/Relationship/ManyToOne.pm »
1 package ObjectDB::Util;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 our $VERSION = '3.27';
9 7 our @EXPORT_OK = qw(load_class execute to_array merge_rows filter_columns);
10  
11 7 require Carp;
12 7 require Storable;
13  
14 sub load_class {
15 5 ▶ my ($class) = @_;
Subroutine load_class: called CC=8 SCAR=23.7
Pod undocumented
16  
17 5 ▶ Carp::croak('class name is required') unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 5 5
18  
19 5 ▶ Carp::croak("Invalid class name '$class'")
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /^[[:lower:]\d:]+$/imsx 0 5 5
20 unless $class =~ m/^[[:lower:]\d:]+$/smxi;
21  
22 5 my $path = $class;
23 5 $path =~ s{::}{/}smxg;
24 5 $path .= '.pm';
25  
26 5 ▶ return 1 if exists $INC{$path} && defined $INC{$path};
Branch: 1 branch
BranchTrueFalseTotal
if exists $INC{$path} and defined $INC{$path} 3 2 5
Condition66%2/3
exists $INC{$path} and defined $INC{$path}
!ll&&!rl&&r
203
MC/DC50%1/2
exists $INC{$path} and defined $INC{$path}
exists $INC{$path} defined $INC{$path}
Truth table66%2/3
A and B
A= exists $INC{$path}B= defined $INC{$path}
ABresult
0X0
100
111
27  
28 {
29 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 for (keys %{"$class\::"}) {
32 0 ▶ return 1 if defined &{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{$_;} 0 0 0
33 }
34 }
35  
36 eval {
37 2 require $path;
38  
39 2 1;
40 2 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $path; 1 } } or do { my $e = $@; delete $INC{$path}; { no strict 'refs'; %{"${class}::";} = (); } ; Carp::croak($e) } 0 2 2
41 0 my $e = $@;
42  
43 0 delete $INC{$path};
44  
45 {
46 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
47 0 %{"$class\::"} = ();
48 }
49  
50 0 Carp::croak($e);
51 };
52 }
53  
54 sub execute {
55 0 ▶ my ($dbh, $stmt) = @_;
Subroutine execute: not called CC=2 SCAR=17.9
Pod undocumented
56  
57 0 my $sql = $stmt->to_sql;
58 0 my @bind = $stmt->to_bind;
59  
60 0 my $sth = $dbh->prepare($sql);
61 0 my $rv = $sth->execute(@bind);
62  
63 0 ▶ return wantarray ? ($rv, $sth) : $rv;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
64 }
65  
66 sub force_arrayrefs {
67 0 ▶ my ($data, $defaults) = @_;
Subroutine force_arrayrefs: not called CC=4 SCAR=30.0
Pod undocumented
68  
69 0 my $clone = Storable::dclone($data);
70  
71 0 foreach my $key (keys %$defaults) {
72 0 ▶ if (!exists $clone->{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $clone->{$key}) { } 0 0 0
elsif (not ref $clone->{$key}) { } 0 0 0
73 0 $clone->{$key} = [ @{ $defaults->{$key} } ];
74 }
75 elsif (!ref $clone->{$key}) {
76 0 $clone->{$key} = [ $clone->{$key} ];
77 }
78  
79 0 push @{ $clone->{$key} }, @{ $defaults->{$key} };
80  
81 }
82  
83 0 return $clone;
84 }
85  
86 sub to_array {
87 7 ▶ my ($data) = @_;
Subroutine to_array: called CC=3 SCAR=11.0
Pod undocumented
88  
89 7 ▶ return () unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 6 7
90  
91 6 ▶ return @$data if ref $data eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "ARRAY" 5 1 6
92  
93 1 return ($data);
94 }
95  
96 sub merge_rows {
97 14 ▶ my $rows = shift;
Subroutine merge_rows: called CC=13 SCAR=25.7
Pod undocumented
98  
99 14 my $merged = [];
100  
101 14 my %order;
102 14 ▶ NEXT_MERGE: while (@$rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rows) 30 14 44
103 30 my $row = shift @$rows;
104  
105 30 my $row_sign = '';
106 30 foreach my $key (sort keys %$row) {
107 42 my $value = $row->{$key};
108 42 ▶ $value = \'join' if ref $value eq 'HASH' || ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" or ref $value eq "ARRAY" 12 30 42
Condition66%2/3
ref $value eq "HASH" or ref $value eq "ARRAY"
l!l&&r!l&&!r
12030
MC/DC50%1/2
ref $value eq "HASH" or ref $value eq "ARRAY"
ref $value eq "HASH" ref $value eq "ARRAY"
Truth table66%2/3
A or B
A= ref $value eq "HASH"B= ref $value eq "ARRAY"
ABresult
1X1
011
000
109  
110 42 ▶ $value = \undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 41 42
111 42 $row_sign .= "$key=$value";
112 }
113  
114 30 ▶ if (!exists $order{$row_sign}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $order{$row_sign}) 23 7 30
115 23 $order{$row_sign} = $row;
116  
117 23 push @$merged, $row;
118 23 next NEXT_MERGE;
119 }
120  
121 7 my $prev = $order{$row_sign};
122  
123 7 foreach my $key (keys %$row) {
124 next
125 unless ref $prev->{$key} eq 'HASH'
126 12 ▶ || ref $prev->{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $prev->{$key} eq "HASH" or ref $prev->{$key} eq "ARRAY" 7 5 12
Condition100%3/3
ref $prev->{$key} eq "HASH" or ref $prev->{$key} eq "ARRAY"
l!l&&r!l&&!r
417
MC/DC100%2/2
ref $prev->{$key} eq "HASH" or ref $prev->{$key} eq "ARRAY"
ref $prev->{$key} eq "HASH" ref $prev->{$key} eq "ARRAY"
Truth table100%3/3
A or B
A= ref $prev->{$key} eq "HASH"B= ref $prev->{$key} eq "ARRAY"
ABresult
1X1
011
000
127  
128 my $prev_row =
129 ref $prev->{$key} eq 'ARRAY'
130 ? $prev->{$key}->[-1]
131 5 ▶ : $prev->{$key};
Branch: 1 branch
BranchTrueFalseTotal
ref $prev->{$key} eq 'ARRAY' ? : 1 4 5
132  
133 5 my $merged_rows = merge_rows([ $prev_row, $row->{$key} ]);
134 5 ▶ if (@$merged_rows > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$merged_rows > 1) 4 1 5
135 my $prev_rows =
136 ref $prev->{$key} eq 'ARRAY'
137 ? $prev->{$key}
138 4 ▶ : [ $prev->{$key} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $prev->{$key} eq 'ARRAY' ? : 1 3 4
139 4 pop @$prev_rows;
140 4 $prev->{$key} = [ @$prev_rows, @$merged_rows ];
141 }
142 }
143 }
144  
145 14 return $merged;
146 }
147  
148 sub filter_columns {
149 4 ▶ my ($meta_columns, $params) = @_;
Subroutine filter_columns: called CC=6 SCAR=18.0
Pod undocumented
150  
151 4 my @columns;
152 4 ▶ if ($params->{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'columns'}) { } 1 3 4
153 1 ▶ push @columns, to_array($params->{columns}) if $params->{columns};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"columns"} 1 0 1
154 }
155 else {
156 3 push @columns, @$meta_columns;
157 }
158  
159 4 ▶ push @columns, to_array($params->{'+columns'}) if $params->{'+columns'};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"+columns"} 1 3 4
160 4 ▶ if ($params->{'-columns'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"-columns"}) 1 3 4
161 1 my $minus_columns = { map { $_ => 1 } to_array($params->{'-columns'}) };
162  
163 1 ▶ @columns = grep { !exists $minus_columns->{ ref($_) ? $_->{'-col'} : $_ } } @columns;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 2 2
164 }
165  
166 4 return \@columns;
167 }
168  
169 7 1;
« blib/lib/ObjectDB/With.pm ↑ Summary blib/lib/ObjectDB/Meta/Relationship/ManyToOne.pm »