Bot-BasicBot-Pluggable
view release on metacpan or search on metacpan
lib/Bot/BasicBot/Pluggable/Store/DBI.pm view on Meta::CPAN
package Bot::BasicBot::Pluggable::Store::DBI;
$Bot::BasicBot::Pluggable::Store::DBI::VERSION = '1.30';
use warnings;
use strict;
use Carp qw( croak );
use Data::Dumper;
use DBI;
use Storable qw( nfreeze thaw );
use Try::Tiny;
use base qw( Bot::BasicBot::Pluggable::Store );
sub init {
my $self = shift;
$self->{dsn} ||= 'dbi:SQLite:bot-basicbot.sqlite';
$self->{table} ||= 'basicbot';
$self->create_table;
}
sub dbh {
my $self = shift;
my $dsn = $self->{dsn} or die "I need a DSN";
my $user = $self->{user};
my $password = $self->{password};
return DBI->connect_cached( $dsn, $user, $password );
}
sub create_table {
my $self = shift;
my $table = $self->{table} or die "Need DB table";
my $sth = $self->dbh->table_info( '%', '%', $table, "TABLE" );
$table = $self->dbh->quote_identifier($table);
if ( !$sth->fetch ) {
$self->dbh->do(
"CREATE TABLE $table (
id INT PRIMARY KEY,
namespace TEXT,
store_key TEXT,
store_value LONGBLOB )"
);
if ( $self->{create_index} ) {
try {
$self->dbh->do(
"CREATE INDEX lookup ON $table ( namespace(10), store_key(10) )"
);
};
}
}
}
sub get {
my ( $self, $namespace, $key ) = @_;
my $table = $self->{table} or die "Need DB table";
$table = $self->dbh->quote_identifier($table);
my $sth = $self->dbh->prepare_cached(
"SELECT store_value FROM $table WHERE namespace=? and store_key=?");
$sth->execute( $namespace, $key );
my $row = $sth->fetchrow_arrayref;
$sth->finish;
return unless $row and @$row;
return try { thaw( $row->[0] ) } catch { $row->[0] };
}
sub set {
my ( $self, $namespace, $key, $value ) = @_;
( run in 1.777 second using v1.01-cache-2.11-cpan-97f6503c9c8 )