2014-05-06 07:15:27 +02:00
|
|
|
# File: Quotegrabs_SQLite.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: SQLite back-end for storing and retreiving quotegrabs
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2015-09-09 14:24:16 +02:00
|
|
|
package PBot::Plugins::Quotegrabs::Quotegrabs_SQLite;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use DBI;
|
|
|
|
use Carp qw(shortmess);
|
|
|
|
|
|
|
|
sub new {
|
2019-05-28 18:19:42 +02:00
|
|
|
if (ref($_[1]) eq 'HASH') {
|
2014-05-06 07:15:27 +02:00
|
|
|
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
|
|
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference in " . __FILE__);
|
|
|
|
$self->{filename} = delete $conf{filename};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub begin {
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Opening quotegrabs SQLite database: $self->{filename}\n");
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2019-07-01 00:08:18 +02:00
|
|
|
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0, sqlite_unicode => 1 }) or die $DBI::errstr;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
|
|
|
eval {
|
|
|
|
$self->{dbh}->do(<< 'SQL');
|
|
|
|
CREATE TABLE IF NOT EXISTS Quotegrabs (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
nick TEXT,
|
|
|
|
channel TEXT,
|
|
|
|
grabbed_by TEXT,
|
|
|
|
text TEXT,
|
|
|
|
timestamp NUMERIC
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
};
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log($@) if $@;
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub end {
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Closing quotegrabs SQLite database\n");
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (exists $self->{dbh} and defined $self->{dbh}) {
|
2014-05-06 07:15:27 +02:00
|
|
|
$self->{dbh}->disconnect();
|
2014-05-13 12:15:52 +02:00
|
|
|
delete $self->{dbh};
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add_quotegrab {
|
|
|
|
my ($self, $quotegrab) = @_;
|
|
|
|
|
|
|
|
my $id = eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('INSERT INTO Quotegrabs VALUES (?, ?, ?, ?, ?, ?)');
|
|
|
|
$sth->bind_param(1, undef);
|
|
|
|
$sth->bind_param(2, $quotegrab->{nick});
|
|
|
|
$sth->bind_param(3, $quotegrab->{channel});
|
|
|
|
$sth->bind_param(4, $quotegrab->{grabbed_by});
|
|
|
|
$sth->bind_param(5, $quotegrab->{text});
|
|
|
|
$sth->bind_param(6, $quotegrab->{timestamp});
|
|
|
|
$sth->execute();
|
|
|
|
|
|
|
|
return $self->{dbh}->sqlite_last_insert_rowid();
|
|
|
|
};
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log($@) if $@;
|
2014-05-06 07:15:27 +02:00
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_quotegrab {
|
|
|
|
my ($self, $id) = @_;
|
|
|
|
|
|
|
|
my $quotegrab = eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('SELECT * FROM Quotegrabs WHERE id == ?');
|
|
|
|
$sth->bind_param(1, $id);
|
|
|
|
$sth->execute();
|
|
|
|
return $sth->fetchrow_hashref();
|
|
|
|
};
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log($@) if $@;
|
2014-05-06 07:15:27 +02:00
|
|
|
return $quotegrab;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_random_quotegrab {
|
|
|
|
my ($self, $nick, $channel, $text) = @_;
|
|
|
|
|
2014-05-31 03:08:01 +02:00
|
|
|
$nick =~ s/\.?\*\??/%/g if defined $nick;
|
|
|
|
$channel =~ s/\.?\*\??/%/g if defined $channel;
|
|
|
|
$text =~ s/\.?\*\??/%/g if defined $text;
|
|
|
|
|
|
|
|
$nick =~ s/\./_/g if defined $nick;
|
|
|
|
$channel =~ s/\./_/g if defined $channel;
|
|
|
|
$text =~ s/\./_/g if defined $text;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
|
|
|
my $quotegrab = eval {
|
|
|
|
my $sql = 'SELECT * FROM Quotegrabs ';
|
|
|
|
my @params;
|
|
|
|
my $where = 'WHERE ';
|
|
|
|
my $and = '';
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (defined $nick) {
|
2014-05-06 07:15:27 +02:00
|
|
|
$sql .= $where . 'nick LIKE ? ';
|
2018-03-26 01:04:17 +02:00
|
|
|
push @params, "$nick";
|
2014-05-06 07:15:27 +02:00
|
|
|
$where = '';
|
|
|
|
$and = 'AND ';
|
2019-06-26 18:34:19 +02:00
|
|
|
}
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (defined $channel) {
|
2014-05-06 07:15:27 +02:00
|
|
|
$sql .= $where . $and . 'channel LIKE ? ';
|
|
|
|
push @params, $channel;
|
|
|
|
$where = '';
|
|
|
|
$and = 'AND ';
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (defined $text) {
|
2014-05-06 07:15:27 +02:00
|
|
|
$sql .= $where . $and . 'text LIKE ? ';
|
|
|
|
push @params, "%$text%";
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= 'ORDER BY RANDOM() LIMIT 1';
|
|
|
|
|
|
|
|
my $sth = $self->{dbh}->prepare($sql);
|
|
|
|
$sth->execute(@params);
|
|
|
|
return $sth->fetchrow_hashref();
|
|
|
|
};
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log($@) if $@;
|
2014-05-06 07:15:27 +02:00
|
|
|
return $quotegrab;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_all_quotegrabs {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $quotegrabs = eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('SELECT * from Quotegrabs');
|
|
|
|
$sth->execute();
|
|
|
|
return $sth->fetchall_arrayref({});
|
|
|
|
};
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log($@) if $@;
|
2014-05-06 07:15:27 +02:00
|
|
|
return $quotegrabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub delete_quotegrab {
|
|
|
|
my ($self, $id) = @_;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('DELETE FROM Quotegrabs WHERE id == ?');
|
|
|
|
$sth->bind_param(1, $id);
|
|
|
|
$sth->execute();
|
|
|
|
};
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log($@) if $@;
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|