| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Bot::BasicBot::Pluggable::Store::DBI; |
|
2
|
|
|
|
|
|
|
$Bot::BasicBot::Pluggable::Store::DBI::VERSION = '1.11'; |
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
34
|
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
21
|
|
|
5
|
1
|
|
|
1
|
|
3
|
use Carp qw( croak ); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
63
|
|
|
6
|
1
|
|
|
1
|
|
504
|
use Data::Dumper; |
|
|
1
|
|
|
|
|
4585
|
|
|
|
1
|
|
|
|
|
69
|
|
|
7
|
1
|
|
|
1
|
|
1301
|
use DBI; |
|
|
1
|
|
|
|
|
12124
|
|
|
|
1
|
|
|
|
|
68
|
|
|
8
|
1
|
|
|
1
|
|
676
|
use Storable qw( nfreeze thaw ); |
|
|
1
|
|
|
|
|
2407
|
|
|
|
1
|
|
|
|
|
63
|
|
|
9
|
1
|
|
|
1
|
|
401
|
use Try::Tiny; |
|
|
1
|
|
|
|
|
1508
|
|
|
|
1
|
|
|
|
|
50
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
6
|
use base qw( Bot::BasicBot::Pluggable::Store ); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
429
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub init { |
|
14
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
|
15
|
1
|
|
50
|
|
|
5
|
$self->{dsn} ||= 'dbi:SQLite:bot-basicbot.sqlite'; |
|
16
|
1
|
|
50
|
|
|
3
|
$self->{table} ||= 'basicbot'; |
|
17
|
1
|
|
|
|
|
2
|
$self->create_table; |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub dbh { |
|
21
|
29
|
|
|
29
|
0
|
33
|
my $self = shift; |
|
22
|
29
|
50
|
|
|
|
69
|
my $dsn = $self->{dsn} or die "I need a DSN"; |
|
23
|
29
|
|
|
|
|
35
|
my $user = $self->{user}; |
|
24
|
29
|
|
|
|
|
26
|
my $password = $self->{password}; |
|
25
|
29
|
|
|
|
|
97
|
return DBI->connect_cached( $dsn, $user, $password ); |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub create_table { |
|
29
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
|
30
|
1
|
50
|
|
|
|
9
|
my $table = $self->{table} or die "Need DB table"; |
|
31
|
1
|
|
|
|
|
2
|
my $sth = $self->dbh->table_info( '', '', $table, "TABLE" ); |
|
32
|
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
9884
|
$table = $self->dbh->quote_identifier($table); |
|
34
|
|
|
|
|
|
|
|
|
35
|
1
|
50
|
|
|
|
253
|
if ( !$sth->fetch ) { |
|
36
|
1
|
|
|
|
|
4
|
$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
|
50
|
|
|
|
19333
|
if ( $self->{create_index} ) { |
|
44
|
|
|
|
|
|
|
try { |
|
45
|
0
|
|
|
0
|
|
0
|
$self->dbh->do( |
|
46
|
|
|
|
|
|
|
"CREATE INDEX lookup ON $table ( namespace(10), store_key(10) )" |
|
47
|
|
|
|
|
|
|
); |
|
48
|
0
|
|
|
|
|
0
|
}; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub get { |
|
54
|
4
|
|
|
4
|
1
|
8
|
my ( $self, $namespace, $key ) = @_; |
|
55
|
4
|
50
|
|
|
|
13
|
my $table = $self->{table} or die "Need DB table"; |
|
56
|
|
|
|
|
|
|
|
|
57
|
4
|
|
|
|
|
13
|
$table = $self->dbh->quote_identifier($table); |
|
58
|
|
|
|
|
|
|
|
|
59
|
4
|
|
|
|
|
781
|
my $sth = $self->dbh->prepare_cached( |
|
60
|
|
|
|
|
|
|
"SELECT store_value FROM $table WHERE namespace=? and store_key=?"); |
|
61
|
4
|
|
|
|
|
947
|
$sth->execute( $namespace, $key ); |
|
62
|
4
|
|
|
|
|
33
|
my $row = $sth->fetchrow_arrayref; |
|
63
|
4
|
|
|
|
|
10
|
$sth->finish; |
|
64
|
4
|
100
|
66
|
|
|
32
|
return unless $row and @$row; |
|
65
|
1
|
|
|
1
|
|
10
|
return try { thaw( $row->[0] ) } catch { $row->[0] }; |
|
|
1
|
|
|
|
|
57
|
|
|
|
1
|
|
|
|
|
258
|
|
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub set { |
|
69
|
2
|
|
|
2
|
1
|
7
|
my ( $self, $namespace, $key, $value ) = @_; |
|
70
|
2
|
50
|
|
|
|
9
|
my $table = $self->{table} or die "Need DB table"; |
|
71
|
|
|
|
|
|
|
|
|
72
|
2
|
|
|
|
|
5
|
$table = $self->dbh->quote_identifier($table); |
|
73
|
|
|
|
|
|
|
|
|
74
|
2
|
50
|
|
|
|
413
|
$value = nfreeze($value) if ref($value); |
|
75
|
2
|
50
|
|
|
|
7
|
if ( defined( $self->get( $namespace, $key ) ) ) { |
|
76
|
0
|
|
|
|
|
0
|
my $sth = $self->dbh->prepare_cached( |
|
77
|
|
|
|
|
|
|
"UPDATE $table SET store_value=? WHERE namespace=? AND store_key=?" |
|
78
|
|
|
|
|
|
|
); |
|
79
|
0
|
|
|
|
|
0
|
$sth->execute( $value, $namespace, $key ); |
|
80
|
0
|
|
|
|
|
0
|
$sth->finish; |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
else { |
|
83
|
2
|
|
|
|
|
6
|
my $sth = $self->dbh->prepare_cached( |
|
84
|
|
|
|
|
|
|
"INSERT INTO $table (id, store_value, namespace, store_key) VALUES (?, ?, ?, ?)" |
|
85
|
|
|
|
|
|
|
); |
|
86
|
2
|
|
|
|
|
484
|
$sth->execute( $self->new_id($table), $value, $namespace, $key ); |
|
87
|
2
|
|
|
|
|
41
|
$sth->finish; |
|
88
|
|
|
|
|
|
|
} |
|
89
|
2
|
|
|
|
|
35
|
return $self; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub unset { |
|
93
|
1
|
|
|
1
|
1
|
2
|
my ( $self, $namespace, $key ) = @_; |
|
94
|
1
|
50
|
|
|
|
8
|
my $table = $self->{table} or die "Need DB table"; |
|
95
|
|
|
|
|
|
|
|
|
96
|
1
|
|
|
|
|
3
|
$table = $self->dbh->quote_identifier($table); |
|
97
|
|
|
|
|
|
|
|
|
98
|
1
|
|
|
|
|
197
|
my $sth = $self->dbh->prepare_cached( |
|
99
|
|
|
|
|
|
|
"DELETE FROM $table WHERE namespace=? and store_key=?"); |
|
100
|
1
|
|
|
|
|
7575
|
$sth->execute( $namespace, $key ); |
|
101
|
1
|
|
|
|
|
25
|
$sth->finish; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub new_id { |
|
105
|
2
|
|
|
2
|
0
|
5
|
my $self = shift; |
|
106
|
2
|
|
|
|
|
4
|
my $table = shift; |
|
107
|
2
|
|
|
|
|
7
|
my $sth = $self->dbh->prepare_cached("SELECT MAX(id) FROM $table"); |
|
108
|
2
|
|
|
|
|
571
|
$sth->execute(); |
|
109
|
2
|
|
100
|
|
|
40
|
my $id = $sth->fetchrow_arrayref->[0] || "0"; |
|
110
|
2
|
|
|
|
|
10
|
$sth->finish(); |
|
111
|
2
|
|
|
|
|
14631
|
return $id + 1; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub keys { |
|
115
|
4
|
|
|
4
|
1
|
16
|
my ( $self, $namespace, %opts ) = @_; |
|
116
|
4
|
50
|
|
|
|
20
|
my $table = $self->{table} or die "Need DB table"; |
|
117
|
|
|
|
|
|
|
|
|
118
|
4
|
|
|
|
|
16
|
$table = $self->dbh->quote_identifier($table); |
|
119
|
|
|
|
|
|
|
|
|
120
|
4
|
100
|
|
|
|
1113
|
my @res = ( exists $opts{res} ) ? @{ $opts{res} } : (); |
|
|
1
|
|
|
|
|
2
|
|
|
121
|
|
|
|
|
|
|
|
|
122
|
4
|
|
|
|
|
12
|
my $sql = "SELECT store_key FROM $table WHERE namespace=?"; |
|
123
|
|
|
|
|
|
|
|
|
124
|
4
|
|
|
|
|
9
|
my @args = ($namespace); |
|
125
|
|
|
|
|
|
|
|
|
126
|
4
|
|
|
|
|
42
|
foreach my $re (@res) { |
|
127
|
1
|
|
|
|
|
2
|
my $orig = $re; |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# h-h-h-hack .... convert to SQL and limit terms if too general |
|
130
|
1
|
50
|
|
|
|
8
|
$re = "%$re" if $re !~ s!^\^!!; |
|
131
|
1
|
50
|
|
|
|
4
|
$re = "$re%" if $re !~ s!\$$!!; |
|
132
|
1
|
50
|
|
|
|
6
|
$re = "${namespace}_${re}" if $orig =~ m!^[^\^].*[^\$]$!; |
|
133
|
|
|
|
|
|
|
|
|
134
|
1
|
|
|
|
|
3
|
$sql .= " AND store_key LIKE ?"; |
|
135
|
1
|
|
|
|
|
2
|
push @args, $re; |
|
136
|
|
|
|
|
|
|
} |
|
137
|
4
|
50
|
|
|
|
15
|
if ( exists $opts{limit} ) { |
|
138
|
0
|
|
|
|
|
0
|
$sql .= " LIMIT ?"; |
|
139
|
0
|
|
|
|
|
0
|
push @args, $opts{limit}; |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
|
|
142
|
4
|
|
|
|
|
12
|
my $sth = $self->dbh->prepare_cached($sql); |
|
143
|
4
|
|
|
|
|
1483
|
$sth->execute(@args); |
|
144
|
|
|
|
|
|
|
|
|
145
|
4
|
50
|
|
|
|
18
|
return $sth->rows if $opts{_count_only}; |
|
146
|
|
|
|
|
|
|
|
|
147
|
4
|
|
|
|
|
4
|
my @keys = map { $_->[0] } @{ $sth->fetchall_arrayref }; |
|
|
4
|
|
|
|
|
18
|
|
|
|
4
|
|
|
|
|
132
|
|
|
148
|
4
|
|
|
|
|
20
|
$sth->finish; |
|
149
|
4
|
|
|
|
|
31
|
return @keys; |
|
150
|
|
|
|
|
|
|
} |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub namespaces { |
|
153
|
1
|
|
|
1
|
1
|
3
|
my ($self) = @_; |
|
154
|
1
|
50
|
|
|
|
4
|
my $table = $self->{table} or die "Need DB table"; |
|
155
|
|
|
|
|
|
|
|
|
156
|
1
|
|
|
|
|
3
|
$table = $self->dbh->quote_identifier($table); |
|
157
|
|
|
|
|
|
|
|
|
158
|
1
|
|
|
|
|
150
|
my $sth = |
|
159
|
|
|
|
|
|
|
$self->dbh->prepare_cached("SELECT DISTINCT namespace FROM $table"); |
|
160
|
1
|
|
|
|
|
496
|
$sth->execute(); |
|
161
|
1
|
|
|
|
|
2
|
my @keys = map { $_->[0] } @{ $sth->fetchall_arrayref }; |
|
|
1
|
|
|
|
|
6
|
|
|
|
1
|
|
|
|
|
59
|
|
|
162
|
1
|
|
|
|
|
6
|
$sth->finish; |
|
163
|
1
|
|
|
|
|
7
|
return @keys; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
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.11 |
|
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. |