3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-19 10:29:30 +01:00

Plugin/Quotegrabs: polish Storage/SQLite.pm a bit

This commit is contained in:
Pragmatic Software 2023-04-13 11:21:30 -07:00
parent cdbbf9c183
commit 3180d11303
2 changed files with 51 additions and 71 deletions

View File

@ -12,22 +12,18 @@ use PBot::Imports;
use DBI; use DBI;
use Carp; use Carp;
sub new { sub new($class, %conf) {
my ($class, %conf) = @_;
my $self = bless {}, $class; my $self = bless {}, $class;
$self->initialize(%conf); $self->initialize(%conf);
return $self; return $self;
} }
sub initialize { sub initialize($self, %conf) {
my ($self, %conf) = @_;
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference in " . __FILE__); $self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference in " . __FILE__);
$self->{filename} = delete $conf{filename}; $self->{filename} = delete $conf{filename};
} }
sub begin { sub begin($self) {
my $self = shift;
$self->{pbot}->{logger}->log("Opening quotegrabs SQLite database: $self->{filename}\n"); $self->{pbot}->{logger}->log("Opening quotegrabs SQLite database: $self->{filename}\n");
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", {RaiseError => 1, PrintError => 0, sqlite_unicode => 1}) or die $DBI::errstr; $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", {RaiseError => 1, PrintError => 0, sqlite_unicode => 1}) or die $DBI::errstr;
@ -43,39 +39,31 @@ CREATE TABLE IF NOT EXISTS Quotegrabs (
) )
SQL SQL
$self->{dbh}->do(<< 'SQL'); $self->{dbh}->do('CREATE TABLE IF NOT EXISTS Seen (id INTEGER UNIQUE)');
CREATE TABLE IF NOT EXISTS Seen (
id INTEGER UNIQUE
)
SQL
}; };
$self->{pbot}->{logger}->log($@) if $@; $self->{pbot}->{logger}->log($@) if $@;
} }
sub end { sub end($self) {
my $self = shift;
$self->{pbot}->{logger}->log("Closing quotegrabs SQLite database\n");
if (exists $self->{dbh} and defined $self->{dbh}) { if (exists $self->{dbh} and defined $self->{dbh}) {
$self->{pbot}->{logger}->log("Closing quotegrabs SQLite database\n");
$self->{dbh}->disconnect(); $self->{dbh}->disconnect();
delete $self->{dbh}; delete $self->{dbh};
} }
} }
sub add_quotegrab { sub add_quotegrab($self, $quotegrab) {
my ($self, $quotegrab) = @_;
my $id = eval { my $id = eval {
my $sth = $self->{dbh}->prepare('INSERT INTO Quotegrabs VALUES (?, ?, ?, ?, ?, ?)'); my $sth = $self->{dbh}->prepare('INSERT INTO Quotegrabs VALUES (?, ?, ?, ?, ?, ?)');
$sth->bind_param(1, undef); $sth->execute(
$sth->bind_param(2, $quotegrab->{nick}); undef,
$sth->bind_param(3, $quotegrab->{channel}); $quotegrab->{nick},
$sth->bind_param(4, $quotegrab->{grabbed_by}); $quotegrab->{channel},
$sth->bind_param(5, $quotegrab->{text}); $quotegrab->{grabbed_by},
$sth->bind_param(6, $quotegrab->{timestamp}); $quotegrab->{text},
$sth->execute(); $quotegrab->{timestamp},
);
return $self->{dbh}->sqlite_last_insert_rowid(); return $self->{dbh}->sqlite_last_insert_rowid();
}; };
@ -83,9 +71,7 @@ sub add_quotegrab {
return $id; return $id;
} }
sub get_quotegrab { sub get_quotegrab($self, $id) {
my ($self, $id) = @_;
my $quotegrab = eval { my $quotegrab = eval {
my $sth = $self->{dbh}->prepare('SELECT * FROM Quotegrabs WHERE id == ?'); my $sth = $self->{dbh}->prepare('SELECT * FROM Quotegrabs WHERE id == ?');
$sth->bind_param(1, $id); $sth->bind_param(1, $id);
@ -97,51 +83,53 @@ sub get_quotegrab {
return $quotegrab; return $quotegrab;
} }
sub get_random_quotegrab { sub get_random_quotegrab($self, $nick, $channel, $text) {
my ($self, $nick, $channel, $text) = @_; # convert from regex metachars to SQL LIKE metachars
if (defined $nick) {
$nick =~ s/\.?\*\??/%/g;
$nick =~ s/\./_/g;
}
$nick =~ s/\.?\*\??/%/g if defined $nick; if (defined $channel) {
$channel =~ s/\.?\*\??/%/g if defined $channel; $channel =~ s/\.?\*\??/%/g;
$text =~ s/\.?\*\??/%/g if defined $text; $channel =~ s/\./_/g;
}
$nick =~ s/\./_/g if defined $nick; if (defined $text) {
$channel =~ s/\./_/g if defined $channel; $text =~ s/\.?\*\??/%/g;
$text =~ s/\./_/g if defined $text; $text =~ s/\./_/g;
}
my $quotegrab = eval { my $quotegrab = eval {
my $sql = 'SELECT * FROM Quotegrabs '; my $sql = 'SELECT * FROM Quotegrabs';
my @params; my @params;
my $where = 'WHERE '; my $joiner = ' WHERE';
my $and = '';
# multi-grabs have the nick separated by +'s so we must test for # multi-grabs have the nick separated by +'s so we must test for
# nick, nick+*, *+nick, and *+nick+* to match each of these cases # nick, nick+*, *+nick, and *+nick+* to match each of these cases
if (defined $nick) { if (defined $nick) {
$sql .= $where . '(nick LIKE ? OR nick LIKE ? OR nick LIKE ? OR nick LIKE ?) '; $sql .= "$joiner (nick LIKE ? OR nick LIKE ? OR nick LIKE ? OR nick LIKE ?)";
push @params, "$nick"; push @params, $nick;
push @params, "$nick+%"; push @params, $nick . '+%';
push @params, "%+$nick"; push @params, '%+' . $nick;
push @params, "%+$nick+%"; push @params, '%+' . $nick . '+%';
$where = ''; $joiner = ' AND';
$and = 'AND ';
} }
if (defined $channel) { if (defined $channel) {
$sql .= $where . $and . 'channel LIKE ? '; $sql .= "$joiner channel LIKE ?";
push @params, $channel; push @params, $channel;
$where = ''; $joiner = ' AND';
$and = 'AND ';
} }
if (defined $text) { if (defined $text) {
$sql .= $where . $and . 'text LIKE ? '; $sql .= "$joiner text LIKE ?";
push @params, "%$text%"; push @params, '%' . $text . '%';
$where = ''; $joiner = ' AND';
$and = 'AND ';
} }
$self->{pbot}->{logger}->log("$sql $joiner id NOT IN Seen ORDER BY RANDOM() LIMIT 1\n");
# search for a random unseen quotegrab # search for a random unseen quotegrab
my $sth = $self->{dbh}->prepare($sql . $where . $and . 'id NOT IN Seen ORDER BY RANDOM() LIMIT 1'); my $sth = $self->{dbh}->prepare("$sql $joiner id NOT IN Seen ORDER BY RANDOM() LIMIT 1");
$sth->execute(@params); $sth->execute(@params);
my $quotegrab = $sth->fetchrow_hashref(); my $quotegrab = $sth->fetchrow_hashref();
@ -170,9 +158,7 @@ sub get_random_quotegrab {
return $quotegrab; return $quotegrab;
} }
sub remove_seen { sub remove_seen($self, $sql, $params) {
my ($self, $sql, $params) = @_;
$sql =~ s/^SELECT \*/SELECT id/; $sql =~ s/^SELECT \*/SELECT id/;
my $count = eval { my $count = eval {
@ -185,9 +171,7 @@ sub remove_seen {
return $count; return $count;
} }
sub add_seen { sub add_seen($self, $id) {
my ($self, $id) = @_;
eval { eval {
my $sth = $self->{dbh}->prepare('INSERT INTO Seen VALUES (?)'); my $sth = $self->{dbh}->prepare('INSERT INTO Seen VALUES (?)');
$sth->execute($id); $sth->execute($id);
@ -196,9 +180,7 @@ sub add_seen {
$self->{pbot}->{logger}->log($@) if $@; $self->{pbot}->{logger}->log($@) if $@;
} }
sub get_all_quotegrabs { sub get_all_quotegrabs($self) {
my $self = shift;
my $quotegrabs = eval { my $quotegrabs = eval {
my $sth = $self->{dbh}->prepare('SELECT * from Quotegrabs'); my $sth = $self->{dbh}->prepare('SELECT * from Quotegrabs');
$sth->execute(); $sth->execute();
@ -209,9 +191,7 @@ sub get_all_quotegrabs {
return $quotegrabs; return $quotegrabs;
} }
sub delete_quotegrab { sub delete_quotegrab($self, $id) {
my ($self, $id) = @_;
eval { eval {
my $sth = $self->{dbh}->prepare('DELETE FROM Quotegrabs WHERE id == ?'); my $sth = $self->{dbh}->prepare('DELETE FROM Quotegrabs WHERE id == ?');
$sth->execute($id); $sth->execute($id);

View File

@ -25,8 +25,8 @@ use PBot::Imports;
# These are set by the /misc/update_version script # These are set by the /misc/update_version script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 4643, BUILD_REVISION => 4644,
BUILD_DATE => "2023-04-10", BUILD_DATE => "2023-04-13",
}; };
sub initialize {} sub initialize {}