IRC/Store/SQLite.pm

filter: Statementstmt 44.2% 27 / 61 Branchbran 5.5% 1 / 18 Conditioncond 5.8% 1 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 72.7% 8 / 11 Podpod 50.0% 2 / 4 totaltotal 32.2% 39 / 121 CC 18 SCAR 34.5
CC
18
Cov
30%
_dbh
56.0
get
56.0
set
20.0
CRAP 31.4
« IRC/Infobot.pm ↑ Summary IRC/Join.pm »
1 package Bot::IRC::Store::SQLite;
2 # ABSTRACT: Bot::IRC persistent data storage with SQLite
3  
4 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use exact;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '1.49'; # VERSION
12  
13 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
14 1 my ($bot) = @_;
15 1 my $obj = __PACKAGE__->new($bot);
16  
17 1 ▶ $bot->subs( 'store' => sub { return $obj } );
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 $bot->register('Bot::IRC::Store');
19 }
20  
21 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
22 2 my ( $class, $bot ) = @_;
23  
24 2 ▶ my $conf = $bot->vars('store') || 'store.sqlite';
Condition50%1/2
$bot->vars('store') || 'store.sqlite'
l!l
20
MC/DC0%0/1
$bot->vars('store') || 'store.sqlite'
$bot->vars('store')
Truth table50%1/2
A
A= $bot->vars('store')
Aresult
11
00
25 2 ▶ $conf = { file => $conf } unless ( ref $conf eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conf eq "HASH" 0 2 2
26  
27 2 my $self = bless( $conf, $class );
28 2 $self->{json} = JSON::XS->new->ascii;
29  
30 2 return $self;
31 }
32  
33 0 ▶sub _dbh {
Subroutine _dbh: not called CC=7 SCAR=40.3
34 0 my ($self) = @_;
35 0 ▶ my $pre_exists = ( -f $self->{file} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-f $self->{'file'} ? : 0 0 0
36  
37 my $dbh = DBI->connect(
38 'dbi:SQLite:dbname=' . $self->{file},
39 undef,
40 undef,
41 {
42 PrintError => 0,
43 RaiseError => 1,
44 sqlite_see_if_its_a_number => 1,
45 sqlite_defensive => 1,
46 sqlite_extended_result_codes => 1,
47 sqlite_string_mode => 6,
48 # 4 = DBD_SQLITE_STRING_MODE_UNICODE_NAIVE
49 # 5 = DBD_SQLITE_STRING_MODE_UNICODE_FALLBACK
50 # 6 = DBD_SQLITE_STRING_MODE_UNICODE_STRICT
51 0 ▶ %{ $self->{attr} // {} },
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect('dbi:SQLite:dbname=' . $self->{"file"}, undef, undef, {"PrintError", 0, "RaiseError", 1, "sqlite_see_if_its_a_number", 1, "sqlite_defensive", 1, "sqlite_extended_result_codes", 1, "sqlite_string_mode", 6, %{$self->{"attr"} // {};}}) or die "$@\n" 0 0 0
Condition0%0/2
$self->{"attr"} // {}
l!l
00
MC/DC0%0/1
$self->{"attr"} // {}
$self->{"attr"}
Truth table0%0/2
A
A= $self->{"attr"}
Aresult
11
00
52 },
53 ) or die "$@\n";
54  
55 my $pragma = {
56 encoding => '"UTF-8"',
57 foreign_keys => 'ON',
58 journal_mode => 'DELETE',
59 recursive_triggers => 'ON',
60 synchronous => 'FULL',
61 temp_store => 'MEMORY',
62 0 ▶ %{ $self->{pragma} // {} },
Condition0%0/2
$self->{"pragma"} // {}
l!l
00
MC/DC0%0/1
$self->{"pragma"} // {}
$self->{"pragma"}
Truth table0%0/2
A
A= $self->{"pragma"}
Aresult
11
00
63 };
64 0 $dbh->do( 'PRAGMA ' . $_ . ' = ' . $pragma->{$_} ) for ( keys %$pragma );
65  
66 0 ▶ $dbh->do(q{
Branch: 1 branch
BranchTrueFalseTotal
unless $pre_exists 0 0 0
67 CREATE TABLE IF NOT EXISTS bot_store (
68 bot_store_id INTEGER PRIMARY KEY,
69 namespace TEXT,
70 key TEXT,
71 value TEXT,
72 created TEXT NOT NULL DEFAULT ( STRFTIME( '%Y-%m-%d %H:%M:%f', 'NOW', 'LOCALTIME' ) )
73 )
74 }) unless ($pre_exists);
75  
76 0 return $dbh;
77 }
78  
79 0 ▶sub get {
Subroutine get: not called CC=7 SCAR=40.3
Pod documented
80 0 my ( $self, $key ) = @_;
81 0 my $namespace = ( caller() )[0];
82 0 my $value;
83  
84 0 ▶ $self->{dbh} //= $self->_dbh;
Condition0%0/3
$self->{"dbh"} //= $self->_dbh
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"dbh"} //= $self->_dbh
$self->{"dbh"} $self->_dbh
Truth table0%0/3
A //= B
A= $self->{"dbh"}B= $self->_dbh
ABresult
1X1
011
000
85  
86 0 try {
87 0 my $sth = $self->{dbh}->prepare_cached(q{
88 SELECT value FROM bot_store WHERE namespace = ? AND key = ?
89 });
90 0 ▶ $sth->execute( $namespace, $key ) or die $self->{dbh}->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($namespace, $key) or die $self->{"dbh"}->errstr 0 0 0
91 0 $value = $sth->fetchrow_array;
92 0 $sth->finish;
93 }
94 catch ($e) {
95 0 warn "Store get error with $namespace (likely an IRC::Store::SQLite issue); key = $key; error = $e\n";
96 }
97  
98 0 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
99 0 ▶ $value = $self->{json}->decode($value) || undef;
Condition0%0/2
$self->{'json'}->decode($value) || undef
l!l
00
MC/DC0%0/1
$self->{'json'}->decode($value) || undef
$self->{'json'}->decode($value)
Truth table0%0/2
A
A= $self->{'json'}->decode($value)
Aresult
11
00
100 0 ▶ $value = $value->{value} if ( ref $value eq 'HASH' and exists $value->{value} );
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" and exists $value->{"value"} 0 0 0
Condition0%0/3
ref $value eq "HASH" and exists $value->{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value eq "HASH" and exists $value->{"value"}
ref $value eq "HASH" exists $value->{"value"}
Truth table0%0/3
A and B
A= ref $value eq "HASH"B= exists $value->{"value"}
ABresult
0X0
100
111
101 }
102  
103 0 return $value;
104 }
105  
106 0 ▶sub set {
Subroutine set: not called CC=4 SCAR=30.0
Pod documented
107 0 my ( $self, $key, $value ) = @_;
108 0 my $namespace = ( caller() )[0];
109  
110 0 ▶ $self->{dbh} //= $self->_dbh;
Condition0%0/3
$self->{"dbh"} //= $self->_dbh
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"dbh"} //= $self->_dbh
$self->{"dbh"} $self->_dbh
Truth table0%0/3
A //= B
A= $self->{"dbh"}B= $self->_dbh
ABresult
1X1
011
000
111  
112 0 try {
113 0 $self->{dbh}->begin_work;
114  
115 $self->{dbh}->prepare_cached(q{
116 DELETE FROM bot_store WHERE namespace = ? AND key = ?
117 0 ▶ })->execute( $namespace, $key ) or die $self->{dbh}->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"}->prepare_cached("\n DELETE FROM bot_store WHERE namespace = ? AND key = ?\n ")->execute($namespace, $key) or die $self->{"dbh"}->errstr 0 0 0
118  
119 $self->{dbh}->prepare_cached(q{
120 INSERT INTO bot_store ( namespace, key, value ) VALUES ( ?, ?, ? )
121 })->execute(
122 $namespace,
123 $key,
124 $self->{json}->encode( { value => $value } ),
125 0 ▶ ) or die $self->{dbh}->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"}->prepare_cached("\n INSERT INTO bot_store ( namespace, key, value ) VALUES ( ?, ?, ? )\n ")->execute($namespace, $key, $self->{"json"}->encode({"value", $value})) or die $self->{"dbh"}->errstr 0 0 0
126  
127 0 $self->{dbh}->commit;
128 }
129 catch ($e) {
130 0 $self->{dbh}->rollback;
131 0 warn "Store set error with $namespace (likely an IRC::Store::SQLite issue); key = $key; error = $e\n";
132 }
133  
134 0 return $self;
135 }
136  
137 2 1;
138  
139 __END__
« IRC/Infobot.pm ↑ Summary IRC/Join.pm »