AnyData/Storage/TiedHash.pm

filter: Statementstmt 93.1% 95 / 102 Branchbran 60.9% 39 / 64 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 3 totaltotal 78.5% 150 / 191 CC 38 SCAR 17.6
CC
38
Cov
80%
FETCH
8.7
STORE
8.2
verify_columns
7.5
CRAP 5.8
« AnyData/Format/Ini.pm ↑ Summary AnyData/Format/Fixed.pm »
1 ######################################
2 package AnyData::Storage::TiedHash;
3 ######################################
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 22 ▶sub FETCH {
Subroutine FETCH: called CC=8 SCAR=21.6
8 22 my($self,$key) = @_;
9 22 my(@rows,$row,$found);
10 22 ▶ return $self->{ad}->col_names if($key eq '__colnames');
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "__colnames" 6 16 22
11 16 ▶ return $self->{ad}->key_col if $key eq '__key';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "__key" 0 16 16
12 16 my $ismultiple = ref $key;
13 16 $self->{ad}->seek_first_record;
14 16 ▶ while ($row = $self->{ad}->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $self->{"ad"}->fetchrow_hashref) 34 0 34
15 34 ▶ if ( $self->{ad}->match($row,$key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ad"}->match($row, $key)) 16 18 34
16 16 $found++;
17 16 ▶ last unless $ismultiple;
Branch: 1 branch
BranchTrueFalseTotal
unless $ismultiple 16 0 16
18 0 push @rows, $row;
19 }
20 }
21 16 ▶ return \@rows if $ismultiple;
Branch: 1 branch
BranchTrueFalseTotal
if $ismultiple 0 16 16
22 16 ▶ return $found ? $row : undef;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 16 0 16
23 }
24  
25 10 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=3 SCAR=11.2
26 10 my $class = shift;
27 10 my $ad = shift;
28 10 ▶ my $perms = shift || 'r';
Condition50%1/2
shift() || 'r'
l!l
100
MC/DC0%0/1
shift() || 'r'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
29 10 ▶ my $records = shift || {};
Condition50%1/2
shift() || {}
l!l
010
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
30 10 my $self = {
31 INDEX => 0,
32 RECORDS => $records,
33 ad => $ad,
34 del_marker => "\0",
35 needs_packing => 0,
36 PERMS => $perms,
37 };
38 10 return bless $self, $class;
39 }
40  
41 36 ▶sub verify_columns {
Subroutine verify_columns: called CC=6 SCAR=20.2
Pod undocumented
42 36 my $col_names = shift;
43 36 my $val = shift;
44 36 my %is_col = map {$_ => 1} @$col_names;
45 36 my $errstr = "ERROR: XXX is not a column in the table!\n";
46 36 ▶ $errstr .= scalar @$col_names
Branch: 1 branch
BranchTrueFalseTotal
scalar @$col_names ? : 36 0 36
47 ? " columns are: " . join "~",@$col_names,"\n"
48 : " couldn't find any column names\n";
49 36 ▶ if (ref $val eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'HASH') { } 36 0 36
50 36 for (keys %$val) {
51 66 $errstr =~ s/XXX/$_/;
52 66 ▶ die $errstr if !$is_col{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $is_col{$_} 0 66 66
53 }
54 }
55 else {
56 0 $errstr =~ s/XXX/$val/;
57 0 ▶ $is_col{$val}
Branch: 1 branch
BranchTrueFalseTotal
$is_col{$val} ? : 0 0 0
58 ? return 1
59 : die $errstr;
60 }
61 }
62  
63 36 ▶sub STORE {
Subroutine STORE: called CC=8 SCAR=21.1
64 36 my($self,$key,$value) = @_;
65 #my @c = caller 1;
66 $self->{errstr} = "Can't store: file is opened in 'r' read-only mode!"
67 36 ▶ if $self->{PERMS} eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"PERMS"} eq "r" 0 36 36
68 36 ▶ return undef if $self->{errstr};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"errstr"} 0 36 36
69 36 my @colnames = @{ $self->{ad}->col_names };
70 36 verify_columns(\@colnames,$value);
71 36 ▶ return $self->{ad}->update_multiple_rows($key,$value)
Branch: 1 branch
BranchTrueFalseTotal
if ref $key eq "HASH" 6 30 36
72 if ref $key eq 'HASH';
73 30 $self->{ad}->seek(0,2);
74 30 my @newrow;
75 30 for my $i(0..$#colnames) {
76 90 $newrow[$i] = $value->{$colnames[$i]};
77 90 ▶ next if defined $newrow[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $newrow[$i] 60 30 90
78 30 ▶ $newrow[$i] = $key if $colnames[$i] eq $self->{ad}->key_col;
Branch: 1 branch
BranchTrueFalseTotal
if $colnames[$i] eq $self->{"ad"}->key_col 30 0 30
79 30 ▶ $newrow[$i] = undef unless $newrow[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $newrow[$i] 0 30 30
80 }
81 30 return $self->{ad}->push_row(@newrow);
82 }
83  
84 6 ▶sub DELETE {
Subroutine DELETE: called CC=4 SCAR=14.2
85 6 my($self,$key)=@_;
86 die "Can't delete: file is opened in 'r' read-only mode!"
87 6 ▶ if $self->{PERMS} eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"PERMS"} eq "r" 0 6 6
88 6 my $row;
89 my $count;
90 6 ▶ return $self->{ad}->delete_multiple_rows($key) if ref $key;
Branch: 1 branch
BranchTrueFalseTotal
if ref $key 0 6 6
91 6 ▶ if ($row = $self->FETCH($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $self->FETCH($key)) 6 0 6
92 6 $self->{ad}->delete_single_row;
93 6 $self->{needs_packing}++;
94 6 $count++;
95 }
96 #return $row;
97 6 return $count;
98 }
99  
100 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
101 0 my($self,$key)=@_;
102 0 return $self->FETCH($key);
103 }
104  
105 8 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=4 SCAR=14.2
106 8 my $self = shift;
107 8 $self->{ad}->seek_first_record();
108 8 my $found =0;
109 8 my $row;
110 8 ▶ while (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 8 0 8
111 8 ▶ $row = $self->{ad}->fetchrow_hashref() or last;
Branch: 1 branch
BranchTrueFalseTotal
$row = $self->{"ad"}->fetchrow_hashref or last 8 0 8
112 8 $found++;
113 8 last;
114 }
115 8 ▶ return $found ? $row : undef;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 8 0 8
116 }
117  
118 30 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=4 SCAR=13.9
119 30 my $self = shift;
120 30 my $row;
121 30 my $lastcol=0;
122 30 my $found=0;
123 30 ▶ while (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 30 0 30
124 30 ▶ $row = $self->{ad}->fetchrow_hashref() or last;
Branch: 1 branch
BranchTrueFalseTotal
$row = $self->{"ad"}->fetchrow_hashref or last 23 7 30
125 23 $found++;
126 23 last;
127 }
128 30 ▶ return $found ? $row : undef;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 23 7 30
129 }
130  
131 10 ▶sub adRows {
Subroutine adRows: called CC=5 SCAR=17.8
Pod undocumented
132 10 my $self = shift;
133 10 my $key = shift;
134 10 my $count=0;
135 10 $self->{ad}->seek_first_record;
136 10 ▶ if (!$key) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $key) { } 0 10 10
137 0 ▶ while (my $row = $self->{ad}->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->{"ad"}->fetchrow_hashref) 0 0 0
138 0 $count++;
139 }
140 }
141 else {
142 10 ▶ while (my $row = $self->{ad}->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->{"ad"}->fetchrow_hashref) 43 10 53
143 43 ▶ $count++ if $self->{ad}->match($row,$key);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ad"}->match($row, $key) 43 0 43
144 }
145 }
146 10 return $count;
147 }
148  
149 12 ▶sub adColumn {
Subroutine adColumn: called CC=4 SCAR=13.9
Pod undocumented
150 12 my($self,$column,$flags)=@_;
151 12 ▶ $flags ||= '';
Condition100%2/2
$flags ||= ""
l!l
66
MC/DC100%1/1
$flags ||= ""
$flags
Truth table100%2/2
A
A= $flags
Aresult
11
00
152 12 my @results=();
153 12 $self->{ad}->seek_first_record;
154 12 ▶ while (my $row = $self->{ad}->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->{"ad"}->fetchrow_hashref) 48 12 60
155 48 push @results, $row->{$column}
156 }
157 12 my %is_member;
158 12 ▶ @results = grep(!$is_member{$_}++, @results) if $flags; $flags =~ /u/i;
Branch: 1 branch
BranchTrueFalseTotal
if $flags 6 6 12
159 # @results = sort @results if $flags =~ /a/i;
160 # @results = reverse sort @results if $flags =~ /d/i;
161 12 return @results;
162 }
163  
164 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
165 #my $self=shift;
166 #undef $self->{ad};
167 #print "HASH DESTROYED";
168 }
169 ##############################
170 # END OF AnyData::Tiedhash
171 ##############################
172 6 1;
« AnyData/Format/Ini.pm ↑ Summary AnyData/Format/Fixed.pm »