blib/lib/Bot/BasicBot/Pluggable/Store/DBI.pm

filter: Statementstmt 93.9% 108 / 115 Branchbran 55.5% 20 / 36 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 94.7% 18 / 19 Podpod 66.6% 6 / 9 totaltotal 82.9% 160 / 193 SCAR 32.8
CC
24
Cov
84%
keys
9.7
create_table
4.6
set
4.6
CRAP 26.5
« blib/lib/Bot/BasicBot/Pluggable/Module/Auth.pm ↑ Summary blib/lib/Bot/BasicBot/Pluggable/Terminal.pm »
1 package Bot::BasicBot::Pluggable::Store::DBI;
2 1 $Bot::BasicBot::Pluggable::Store::DBI::VERSION = '1.20';
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Storable qw( nfreeze thaw );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use base qw( Bot::BasicBot::Pluggable::Store );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub init {
14 1 ▶ my $self = shift;
Subroutine init: called CC=3 SCAR=11.4
Pod documented
15 1 ▶ $self->{dsn} ||= 'dbi:SQLite:bot-basicbot.sqlite';
Condition50%1/2
$self->{"dsn"} ||= "dbi:SQLite:bot-basicbot.sqlite"
l!l
10
MC/DC0%0/1
$self->{"dsn"} ||= "dbi:SQLite:bot-basicbot.sqlite"
$self->{"dsn"}
Truth table50%1/2
A
A= $self->{"dsn"}
Aresult
11
00
16 1 ▶ $self->{table} ||= 'basicbot';
Condition50%1/2
$self->{"table"} ||= "basicbot"
l!l
10
MC/DC0%0/1
$self->{"table"} ||= "basicbot"
$self->{"table"}
Truth table50%1/2
A
A= $self->{"table"}
Aresult
11
00
17 1 $self->create_table;
18 }
19  
20 sub dbh {
21 29 ▶ my $self = shift;
Subroutine dbh: called CC=2 SCAR=7.0
Pod undocumented
22 29 ▶ my $dsn = $self->{dsn} or die "I need a DSN";
Branch: 1 branch
BranchTrueFalseTotal
my $dsn = $self->{"dsn"} or die "I need a DSN" 0 29 29
23 29 my $user = $self->{user};
24 29 my $password = $self->{password};
25 29 return DBI->connect_cached( $dsn, $user, $password );
26 }
27  
28 sub create_table {
29 1 ▶ my $self = shift;
Subroutine create_table: called CC=4 SCAR=15.2
Pod undocumented
30 1 ▶ my $table = $self->{table} or die "Need DB table";
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->{"table"} or die "Need DB table" 0 1 1
31 1 my $sth = $self->dbh->table_info( '', '', $table, "TABLE" );
32  
33 1 $table = $self->dbh->quote_identifier($table);
34  
35 1 ▶ if ( !$sth->fetch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sth->fetch) 1 0 1
36 1 $self->dbh->do(
37 "CREATE TABLE $table (
38 id INT PRIMARY KEY,
39 namespace TEXT,
40 store_key TEXT,
41 store_value LONGBLOB )"
42 );
43 1 ▶ if ( $self->{create_index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"create_index"}) 0 1 1
44 try {
45 0 ▶ $self->dbh->do(
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 "CREATE INDEX lookup ON $table ( namespace(10), store_key(10) )"
47 );
48 0 };
49 }
50 }
51 }
52  
53 sub get {
54 4 ▶ my ( $self, $namespace, $key ) = @_;
Subroutine get: called CC=4 SCAR=13.9
Pod documented
55 4 ▶ my $table = $self->{table} or die "Need DB table";
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->{"table"} or die "Need DB table" 0 4 4
56  
57 4 $table = $self->dbh->quote_identifier($table);
58  
59 4 my $sth = $self->dbh->prepare_cached(
60 "SELECT store_value FROM $table WHERE namespace=? and store_key=?");
61 4 $sth->execute( $namespace, $key );
62 4 my $row = $sth->fetchrow_arrayref;
63 4 $sth->finish;
64 4 ▶ return unless $row and @$row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row and @$row 3 1 4
Condition66%2/3
$row and @$row
!ll&&!rl&&r
301
MC/DC50%1/2
$row and @$row
$row @$row
Truth table66%2/3
A and B
A= $rowB= @$row
ABresult
0X0
100
111
65 1 ▶ return try { thaw( $row->[0] ) } catch { $row->[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
66 }
67  
68 sub set {
69 2 ▶ my ( $self, $namespace, $key, $value ) = @_;
Subroutine set: called CC=4 SCAR=15.2
Pod documented
70 2 ▶ my $table = $self->{table} or die "Need DB table";
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->{"table"} or die "Need DB table" 0 2 2
71  
72 2 $table = $self->dbh->quote_identifier($table);
73  
74 2 ▶ $value = nfreeze($value) if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 2 2
75 2 ▶ if ( defined( $self->get( $namespace, $key ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get($namespace, $key)) { } 0 2 2
76 0 my $sth = $self->dbh->prepare_cached(
77 "UPDATE $table SET store_value=? WHERE namespace=? AND store_key=?"
78 );
79 0 $sth->execute( $value, $namespace, $key );
80 0 $sth->finish;
81 }
82 else {
83 2 my $sth = $self->dbh->prepare_cached(
84 "INSERT INTO $table (id, store_value, namespace, store_key) VALUES (?, ?, ?, ?)"
85 );
86 2 $sth->execute( $self->new_id($table), $value, $namespace, $key );
87 2 $sth->finish;
88 }
89 2 return $self;
90 }
91  
92 sub unset {
93 1 ▶ my ( $self, $namespace, $key ) = @_;
Subroutine unset: called CC=2 SCAR=7.0
Pod documented
94 1 ▶ my $table = $self->{table} or die "Need DB table";
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->{"table"} or die "Need DB table" 0 1 1
95  
96 1 $table = $self->dbh->quote_identifier($table);
97  
98 1 my $sth = $self->dbh->prepare_cached(
99 "DELETE FROM $table WHERE namespace=? and store_key=?");
100 1 $sth->execute( $namespace, $key );
101 1 $sth->finish;
102 }
103  
104 sub new_id {
105 2 ▶ my $self = shift;
Subroutine new_id: called CC=2 SCAR=6.9
Pod undocumented
106 2 my $table = shift;
107 2 my $sth = $self->dbh->prepare_cached("SELECT MAX(id) FROM $table");
108 2 $sth->execute();
109 2 ▶ my $id = $sth->fetchrow_arrayref->[0] || "0";
Condition100%2/2
$sth->fetchrow_arrayref->[0] || '0'
l!l
11
MC/DC100%1/1
$sth->fetchrow_arrayref->[0] || '0'
$sth->fetchrow_arrayref->[0]
Truth table100%2/2
A
A= $sth->fetchrow_arrayref->[0]
Aresult
11
00
110 2 $sth->finish();
111 2 return $id + 1;
112 }
113  
114 sub keys {
115 4 ▶ my ( $self, $namespace, %opts ) = @_;
Subroutine keys: called CC=9 SCAR=22.7
Pod documented
116 4 ▶ my $table = $self->{table} or die "Need DB table";
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->{"table"} or die "Need DB table" 0 4 4
117  
118 4 $table = $self->dbh->quote_identifier($table);
119  
120 4 ▶ my @res = ( exists $opts{res} ) ? @{ $opts{res} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'res'} ? : 1 3 4
121  
122 4 my $sql = "SELECT store_key FROM $table WHERE namespace=?";
123  
124 4 my @args = ($namespace);
125  
126 4 foreach my $re (@res) {
127 1 my $orig = $re;
128  
129 # h-h-h-hack .... convert to SQL and limit terms if too general
130 1 ▶ $re = "%$re" if $re !~ s!^\^!!;
Branch: 1 branch
BranchTrueFalseTotal
unless $re =~ s/^\^// 0 1 1
131 1 ▶ $re = "$re%" if $re !~ s!\$$!!;
Branch: 1 branch
BranchTrueFalseTotal
unless $re =~ s/\$$// 1 0 1
132 1 ▶ $re = "${namespace}_${re}" if $orig =~ m!^[^\^].*[^\$]$!;
Branch: 1 branch
BranchTrueFalseTotal
if $orig =~ /^[^\^].*[^\$]$/ 0 1 1
133  
134 1 $sql .= " AND store_key LIKE ?";
135 1 push @args, $re;
136 }
137 4 ▶ if ( exists $opts{limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"limit"}) 0 4 4
138 0 $sql .= " LIMIT ?";
139 0 push @args, $opts{limit};
140 }
141  
142 4 my $sth = $self->dbh->prepare_cached($sql);
143 4 $sth->execute(@args);
144  
145 4 ▶ return $sth->rows if $opts{_count_only};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"_count_only"} 0 4 4
146  
147 4 my @keys = map { $_->[0] } @{ $sth->fetchall_arrayref };
148 4 $sth->finish;
149 4 return @keys;
150 }
151  
152 sub namespaces {
153 1 ▶ my ($self) = @_;
Subroutine namespaces: called CC=2 SCAR=6.9
Pod documented
154 1 ▶ my $table = $self->{table} or die "Need DB table";
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->{"table"} or die "Need DB table" 0 1 1
155  
156 1 $table = $self->dbh->quote_identifier($table);
157  
158 1 my $sth =
159 $self->dbh->prepare_cached("SELECT DISTINCT namespace FROM $table");
160 1 $sth->execute();
161 1 my @keys = map { $_->[0] } @{ $sth->fetchall_arrayref };
162 1 $sth->finish;
163 1 return @keys;
164 }
165  
166 1 1;
167  
168 __END__
169  
170 =head1 NAME
171  
172 Bot::BasicBot::Pluggable::Store::DBI - use DBI to provide a storage backend
173  
174 =head1 VERSION
175  
176 version 1.20
177  
178 =head1 SYNOPSIS
179  
180 my $store = Bot::BasicBot::Pluggable::Store::DBI->new(
181 dsn => "dbi:mysql:bot",
182 user => "user",
183 password => "password",
184 table => "brane",
185  
186 # create indexes on key/values?
187 create_index => 1,
188 );
189  
190 $store->set( "namespace", "key", "value" );
191
192 =head1 DESCRIPTION
193  
194 This is a L<Bot::BasicBot::Pluggable::Store> that uses a database to store
195 the values set by modules. Complex values are stored using Storable.
196  
197 =head1 AUTHOR
198  
199 Mario Domgoergen <mdom@cpan.org>
200  
201 This program is free software; you can redistribute it
202 and/or modify it under the same terms as Perl itself.
« blib/lib/Bot/BasicBot/Pluggable/Module/Auth.pm ↑ Summary blib/lib/Bot/BasicBot/Pluggable/Terminal.pm »