| 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
| |||||||||||||
| 11 | 16 | ▶ | return $self->{ad}->key_col if $key eq '__key'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 12 | 16 | my $ismultiple = ref $key; | |||||||||||
| 13 | 16 | $self->{ad}->seek_first_record; | |||||||||||
| 14 | 16 | ▶ | while ($row = $self->{ad}->fetchrow_hashref) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 15 | 34 | ▶ | if ( $self->{ad}->match($row,$key) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 16 | 16 | $found++; | |||||||||||
| 17 | 16 | ▶ | last unless $ismultiple; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | 0 | push @rows, $row; | |||||||||||
| 19 | } | ||||||||||||
| 20 | } | ||||||||||||
| 21 | 16 | ▶ | return \@rows if $ismultiple; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 16 | ▶ | return $found ? $row : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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'
MC/DC0%0/1
shift() || 'r'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 29 | 10 | ▶ | my $records = shift || {}; | ||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 50 | 36 | for (keys %$val) { | |||||||||||
| 51 | 66 | $errstr =~ s/XXX/$_/; | |||||||||||
| 52 | 66 | ▶ | die $errstr if !$is_col{$_}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | } | ||||||||||||
| 54 | } | ||||||||||||
| 55 | else { | ||||||||||||
| 56 | 0 | $errstr =~ s/XXX/$val/; | |||||||||||
| 57 | 0 | ▶ | $is_col{$val} | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 68 | 36 | ▶ | return undef if $self->{errstr}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 78 | 30 | ▶ | $newrow[$i] = $key if $colnames[$i] eq $self->{ad}->key_col; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 30 | ▶ | $newrow[$i] = undef unless $newrow[$i]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 88 | 6 | my $row; | |||||||||||
| 89 | my $count; | ||||||||||||
| 90 | 6 | ▶ | return $self->{ad}->delete_multiple_rows($key) if ref $key; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | 6 | ▶ | if ($row = $self->FETCH($key) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 111 | 8 | ▶ | $row = $self->{ad}->fetchrow_hashref() or last; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 8 | $found++; | |||||||||||
| 113 | 8 | last; | |||||||||||
| 114 | } | ||||||||||||
| 115 | 8 | ▶ | return $found ? $row : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 124 | 30 | ▶ | $row = $self->{ad}->fetchrow_hashref() or last; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 23 | $found++; | |||||||||||
| 126 | 23 | last; | |||||||||||
| 127 | } | ||||||||||||
| 128 | 30 | ▶ | return $found ? $row : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 137 | 0 | ▶ | while (my $row = $self->{ad}->fetchrow_hashref) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 138 | 0 | $count++; | |||||||||||
| 139 | } | ||||||||||||
| 140 | } | ||||||||||||
| 141 | else { | ||||||||||||
| 142 | 10 | ▶ | while (my $row = $self->{ad}->fetchrow_hashref) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 43 | ▶ | $count++ if $self->{ad}->match($row,$key); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 ||= ""
MC/DC100%1/1
$flags ||= ""
$flags
Truth table100%2/2
A
A= $flags
| |||||||||||||
| 152 | 12 | my @results=(); | |||||||||||
| 153 | 12 | $self->{ad}->seek_first_record; | |||||||||||
| 154 | 12 | ▶ | while (my $row = $self->{ad}->fetchrow_hashref) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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; | |||||||||||