Brackup/Dict/SQLite.pm

filter: Statementstmt 91.6% 55 / 60 Branchbran 63.6% 14 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 8 totaltotal 76.1% 83 / 109 CC 13 SCAR 12.2
CC
13
Cov
84%
each
6.1
count
6.0
new
4.2
CRAP 3.4
« Brackup/Metafile.pm ↑ Summary Brackup/Target/Filebased.pm »
1 package Brackup::Dict::SQLite;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod undocumented
8 9 my ($class, %opts) = @_;
9 my $self = bless {
10 table => $opts{table},
11 file => $opts{file},
12 9 data => {},
13 }, $class;
14  
15 9 ▶ my $dbh = $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$opts{file}","","", { RaiseError => 1, PrintError => 0 }) or
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->{"dbh"} = "DBI"->connect(qq[dbi:SQLite:dbname=$opts{"file"}], "", "", {"RaiseError", 1, "PrintError", 0}) or die qq[Failed to connect to SQLite filesystem digest cache database at $opts{"file"}: ] . "DBI"->errstr 9 0 9
16 die "Failed to connect to SQLite filesystem digest cache database at $opts{file}: " . DBI->errstr;
17  
18 9 eval {
19 9 $dbh->do("CREATE TABLE $opts{table} (key TEXT PRIMARY KEY, value TEXT)");
20 };
21 9 ▶ die "Error: $@" if $@ && $@ !~ /table \w+ already exists/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $@ =~ /table \w+ already exists/ 0 9 9
Condition66%2/3
$@ and not $@ =~ /table \w+ already exists/
!ll&&!rl&&r
810
MC/DC0%0/2
$@ and not $@ =~ /table \w+ already exists/
$@ not $@ =~ /table \w+ already exists/
Truth table66%2/3
A and not(B)
A= $@B= not $@ =~ /table \w+ already exists/
ABresult
0X0
100
111
22  
23 9 return $self;
24 }
25  
26 9 ▶sub _reset {
Subroutine _reset: called CC=1 SCAR=0.0
27 9 my $self = shift;
28 9 $self->{data} = {};
29 9 $self->{keys} = [];
30 9 $self->{_loaded_keys} = 0;
31 }
32  
33 sub _load_all
34 9 ▶{
Subroutine _load_all: called CC=3 SCAR=11.0
35 9 my $self = shift;
36 9 ▶ unless ($self->{_loaded_all}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_loaded_all"}++) 9 0 9
37 # SQLite sucks at doing anything quickly (likes hundred thousand
38 # selects back-to-back), so we just suck the whole damn thing into
39 # a perl hash. cute, huh? then it doesn't have to
40 # open/read/seek/seek/seek/read/close for each select later.
41 9 $self->_reset;
42 9 my $sth = $self->{dbh}->prepare("SELECT key, value FROM $self->{table}");
43 9 $sth->execute;
44 9 ▶ while (my ($k, $v) = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 19 9 28
45 19 $self->{data}{$k} = $v;
46 }
47 }
48 }
49  
50 195 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod undocumented
51 195 my ($self, $key) = @_;
52 195 ▶ $self->_load_all unless $self->{_loaded_all};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_loaded_all"} 9 186 195
53 195 return $self->{data}{$key};
54 }
55  
56 126 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
57 126 my ($self, $key, $val) = @_;
58 126 $self->{dbh}->do("REPLACE INTO $self->{table} VALUES (?,?)", undef, $key, $val);
59 126 $self->{data}{$key} = $val;
60 126 return 1;
61 }
62  
63 # Iterator interface, returning ($key, $value), and () on eod
64 109 ▶sub each {
Subroutine each: called CC=6 SCAR=18.1
Pod undocumented
65 109 my $self = shift;
66 109 ▶ $self->_load_all unless $self->{_loaded_all};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_loaded_all"} 0 109 109
67 109 ▶ $self->{keys} = [ keys %{$self->{data}} ] unless $self->{_loaded_keys}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_loaded_keys"}++ 8 101 109
68 109 ▶ if (! @{$self->{keys}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"keys"};}) 8 101 109
69 8 $self->{_loaded_keys} = 0;
70 8 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 0 8
71 }
72 101 my $next = shift @{$self->{keys}};
73 101 ▶ return wantarray ? ($next, $self->{data}{$next}) : $next;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 101 0 101
74 }
75  
76 3 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
77 3 my ($self, $key) = @_;
78 3 $self->{dbh}->do("DELETE FROM $self->{table} WHERE key = ?", undef, $key);
79 3 delete $self->{data}{$key};
80 3 return 1;
81 }
82  
83 0 ▶sub count {
Subroutine count: not called CC=2 SCAR=17.9
Pod undocumented
84 0 my $self = shift;
85 0 ▶ $self->_load_all unless $self->{_loaded_all};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_loaded_all"} 0 0 0
86 0 return scalar keys %{$self->{data}};
87 }
88  
89 17 ▶sub backing_file {
Subroutine backing_file: called CC=1 SCAR=0.0
Pod undocumented
90 17 my $self = shift;
91 17 return $self->{file};
92 }
93  
94 0 ▶sub wipe {
Subroutine wipe: not called CC=1 SCAR=6.9
Pod undocumented
95 0 die "not implemented";
96 }
97  
98 13 1;
99  
100 __END__
« Brackup/Metafile.pm ↑ Summary Brackup/Target/Filebased.pm »