line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bot::BasicBot::Pluggable::Store::DBI; |
2
|
|
|
|
|
|
|
$Bot::BasicBot::Pluggable::Store::DBI::VERSION = '1.10'; |
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
5
|
1
|
|
|
1
|
|
5
|
use Carp qw( croak ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
73
|
|
6
|
1
|
|
|
1
|
|
683
|
use Data::Dumper; |
|
1
|
|
|
|
|
4829
|
|
|
1
|
|
|
|
|
50
|
|
7
|
1
|
|
|
1
|
|
1203
|
use DBI; |
|
1
|
|
|
|
|
12174
|
|
|
1
|
|
|
|
|
124
|
|
8
|
1
|
|
|
1
|
|
579
|
use Storable qw( nfreeze thaw ); |
|
1
|
|
|
|
|
2364
|
|
|
1
|
|
|
|
|
78
|
|
9
|
1
|
|
|
1
|
|
393
|
use Try::Tiny; |
|
1
|
|
|
|
|
1502
|
|
|
1
|
|
|
|
|
49
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
5
|
use base qw( Bot::BasicBot::Pluggable::Store ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
458
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub init { |
14
|
1
|
|
|
1
|
1
|
1
|
my $self = shift; |
15
|
1
|
|
50
|
|
|
6
|
$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
|
35
|
my $self = shift; |
22
|
29
|
50
|
|
|
|
51
|
my $dsn = $self->{dsn} or die "I need a DSN"; |
23
|
29
|
|
|
|
|
30
|
my $user = $self->{user}; |
24
|
29
|
|
|
|
|
21
|
my $password = $self->{password}; |
25
|
29
|
|
|
|
|
86
|
return DBI->connect_cached( $dsn, $user, $password ); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub create_table { |
29
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
30
|
1
|
50
|
|
|
|
8
|
my $table = $self->{table} or die "Need DB table"; |
31
|
1
|
|
|
|
|
2
|
my $sth = $self->dbh->table_info( '', '', $table, "TABLE" ); |
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
9928
|
$table = $self->dbh->quote_identifier($table); |
34
|
|
|
|
|
|
|
|
35
|
1
|
50
|
|
|
|
276
|
if ( !$sth->fetch ) { |
36
|
1
|
|
|
|
|
3
|
$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
|
|
|
|
23590
|
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
|
|
|
|
|
10
|
$table = $self->dbh->quote_identifier($table); |
58
|
|
|
|
|
|
|
|
59
|
4
|
|
|
|
|
669
|
my $sth = $self->dbh->prepare_cached( |
60
|
|
|
|
|
|
|
"SELECT store_value FROM $table WHERE namespace=? and store_key=?"); |
61
|
4
|
|
|
|
|
815
|
$sth->execute( $namespace, $key ); |
62
|
4
|
|
|
|
|
33
|
my $row = $sth->fetchrow_arrayref; |
63
|
4
|
|
|
|
|
14
|
$sth->finish; |
64
|
4
|
100
|
66
|
|
|
28
|
return unless $row and @$row; |
65
|
1
|
|
|
1
|
|
13
|
return try { thaw( $row->[0] ) } catch { $row->[0] }; |
|
1
|
|
|
|
|
53
|
|
|
1
|
|
|
|
|
263
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub set { |
69
|
2
|
|
|
2
|
1
|
7
|
my ( $self, $namespace, $key, $value ) = @_; |
70
|
2
|
50
|
|
|
|
8
|
my $table = $self->{table} or die "Need DB table"; |
71
|
|
|
|
|
|
|
|
72
|
2
|
|
|
|
|
5
|
$table = $self->dbh->quote_identifier($table); |
73
|
|
|
|
|
|
|
|
74
|
2
|
50
|
|
|
|
313
|
$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
|
|
|
|
|
5
|
my $sth = $self->dbh->prepare_cached( |
84
|
|
|
|
|
|
|
"INSERT INTO $table (id, store_value, namespace, store_key) VALUES (?, ?, ?, ?)" |
85
|
|
|
|
|
|
|
); |
86
|
2
|
|
|
|
|
329
|
$sth->execute( $self->new_id($table), $value, $namespace, $key ); |
87
|
2
|
|
|
|
|
28
|
$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
|
|
|
|
7
|
my $table = $self->{table} or die "Need DB table"; |
95
|
|
|
|
|
|
|
|
96
|
1
|
|
|
|
|
3
|
$table = $self->dbh->quote_identifier($table); |
97
|
|
|
|
|
|
|
|
98
|
1
|
|
|
|
|
153
|
my $sth = $self->dbh->prepare_cached( |
99
|
|
|
|
|
|
|
"DELETE FROM $table WHERE namespace=? and store_key=?"); |
100
|
1
|
|
|
|
|
5853
|
$sth->execute( $namespace, $key ); |
101
|
1
|
|
|
|
|
22
|
$sth->finish; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub new_id { |
105
|
2
|
|
|
2
|
0
|
4
|
my $self = shift; |
106
|
2
|
|
|
|
|
3
|
my $table = shift; |
107
|
2
|
|
|
|
|
3
|
my $sth = $self->dbh->prepare_cached("SELECT MAX(id) FROM $table"); |
108
|
2
|
|
|
|
|
387
|
$sth->execute(); |
109
|
2
|
|
100
|
|
|
24
|
my $id = $sth->fetchrow_arrayref->[0] || "0"; |
110
|
2
|
|
|
|
|
6
|
$sth->finish(); |
111
|
2
|
|
|
|
|
17715
|
return $id + 1; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub keys { |
115
|
4
|
|
|
4
|
1
|
12
|
my ( $self, $namespace, %opts ) = @_; |
116
|
4
|
50
|
|
|
|
14
|
my $table = $self->{table} or die "Need DB table"; |
117
|
|
|
|
|
|
|
|
118
|
4
|
|
|
|
|
12
|
$table = $self->dbh->quote_identifier($table); |
119
|
|
|
|
|
|
|
|
120
|
4
|
100
|
|
|
|
885
|
my @res = ( exists $opts{res} ) ? @{ $opts{res} } : (); |
|
1
|
|
|
|
|
3
|
|
121
|
|
|
|
|
|
|
|
122
|
4
|
|
|
|
|
9
|
my $sql = "SELECT store_key FROM $table WHERE namespace=?"; |
123
|
|
|
|
|
|
|
|
124
|
4
|
|
|
|
|
7
|
my @args = ($namespace); |
125
|
|
|
|
|
|
|
|
126
|
4
|
|
|
|
|
7
|
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
|
|
|
|
5
|
$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
|
|
|
|
|
2
|
$sql .= " AND store_key LIKE ?"; |
135
|
1
|
|
|
|
|
3
|
push @args, $re; |
136
|
|
|
|
|
|
|
} |
137
|
4
|
50
|
|
|
|
10
|
if ( exists $opts{limit} ) { |
138
|
0
|
|
|
|
|
0
|
$sql .= " LIMIT ?"; |
139
|
0
|
|
|
|
|
0
|
push @args, $opts{limit}; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
4
|
|
|
|
|
8
|
my $sth = $self->dbh->prepare_cached($sql); |
143
|
4
|
|
|
|
|
1068
|
$sth->execute(@args); |
144
|
|
|
|
|
|
|
|
145
|
4
|
50
|
|
|
|
15
|
return $sth->rows if $opts{_count_only}; |
146
|
|
|
|
|
|
|
|
147
|
4
|
|
|
|
|
7
|
my @keys = map { $_->[0] } @{ $sth->fetchall_arrayref }; |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
57
|
|
148
|
4
|
|
|
|
|
16
|
$sth->finish; |
149
|
4
|
|
|
|
|
27
|
return @keys; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub namespaces { |
153
|
1
|
|
|
1
|
1
|
2
|
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
|
|
|
|
|
193
|
my $sth = |
159
|
|
|
|
|
|
|
$self->dbh->prepare_cached("SELECT DISTINCT namespace FROM $table"); |
160
|
1
|
|
|
|
|
524
|
$sth->execute(); |
161
|
1
|
|
|
|
|
2
|
my @keys = map { $_->[0] } @{ $sth->fetchall_arrayref }; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
128
|
|
162
|
1
|
|
|
|
|
5
|
$sth->finish; |
163
|
1
|
|
|
|
|
6
|
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.10 |
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. |