3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-01-25 19:44:26 +01:00

MessageHistory_SQLite: replace REGEXP with LIKE

This commit is contained in:
Pragmatic Software 2020-04-19 12:42:44 -07:00
parent 700e7b3945
commit 87461f38e7

View File

@ -128,7 +128,7 @@ SQL
CREATE TABLE IF NOT EXISTS Messages ( CREATE TABLE IF NOT EXISTS Messages (
id INTEGER, id INTEGER,
channel TEXT COLLATE NOCASE, channel TEXT COLLATE NOCASE,
msg TEXT, msg TEXT COLLATE NOCASE,
timestamp NUMERIC, timestamp NUMERIC,
mode INTEGER mode INTEGER
) )
@ -909,27 +909,25 @@ sub get_message_context {
my ($messages_before, $messages_after, $messages_count); my ($messages_before, $messages_after, $messages_count);
if (defined $count and $count > 1) { if (defined $count and $count > 1) {
my $regex = '(?i)'; my $search = "%$text%";
$regex .= ($text =~ m/^\w/) ? '\b' : '\B'; $search =~ s/\*/%/g;
$regex .= quotemeta $text; $search =~ s/\?/_/g;
$regex .= ($text =~ m/\w$/) ? '\b' : '\B';
$regex =~ s/\\\*/.*?/g;
$messages_count = eval { $messages_count = eval {
my $sth; my $sth;
if (defined $context_id) { if (defined $context_id) {
$sth = $self->{dbh}->prepare( $sth = $self->{dbh}->prepare(
'SELECT id, msg, mode, timestamp, channel FROM Messages WHERE id = ? AND channel = ? AND msg REGEXP ? AND timestamp < ? AND mode = 0 ORDER BY timestamp DESC LIMIT ?'); 'SELECT id, msg, mode, timestamp, channel FROM Messages WHERE id = ? AND channel = ? AND msg LIKE ? ESCAPE "\" AND timestamp < ? AND mode = 0 ORDER BY timestamp DESC LIMIT ?');
$sth->bind_param(1, $context_id); $sth->bind_param(1, $context_id);
$sth->bind_param(2, $message->{channel}); $sth->bind_param(2, $message->{channel});
$sth->bind_param(3, $regex); $sth->bind_param(3, $search);
$sth->bind_param(4, $message->{timestamp}); $sth->bind_param(4, $message->{timestamp});
$sth->bind_param(5, $count - 1); $sth->bind_param(5, $count - 1);
} else { } else {
$sth = $self->{dbh} $sth = $self->{dbh}
->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE channel = ? AND msg REGEXP ? AND timestamp < ? AND mode = 0 ORDER BY timestamp DESC LIMIT ?'); ->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE channel = ? AND msg LIKE ? ESCAPE "\" AND timestamp < ? AND mode = 0 ORDER BY timestamp DESC LIMIT ?');
$sth->bind_param(1, $message->{channel}); $sth->bind_param(1, $message->{channel});
$sth->bind_param(2, $regex); $sth->bind_param(2, $search);
$sth->bind_param(3, $message->{timestamp}); $sth->bind_param(3, $message->{timestamp});
$sth->bind_param(4, $count - 1); $sth->bind_param(4, $count - 1);
} }
@ -1076,24 +1074,22 @@ sub recall_message_by_count {
sub recall_message_by_text { sub recall_message_by_text {
my ($self, $id, $channel, $text, $ignore_command) = @_; my ($self, $id, $channel, $text, $ignore_command) = @_;
my $regex = '(?i)'; my $search = "%$text%";
$regex .= ($text =~ m/^\w/) ? '\b' : '\B'; $search =~ s/\*/%/g;
$regex .= quotemeta $text; $search =~ s/\?/_/g;
$regex .= ($text =~ m/\w$/) ? '\b' : '\B';
$regex =~ s/\\\*/.*?/g;
my $messages; my $messages;
if (defined $id) { if (defined $id) {
$messages = eval { $messages = eval {
my $sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE id = ? AND channel = ? AND msg REGEXP ? ORDER BY timestamp DESC LIMIT 10'); my $sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE id = ? AND channel = ? AND msg LIKE ? ESCAPE "\" ORDER BY timestamp DESC LIMIT 10');
$sth->execute($id, $channel, $regex); $sth->execute($id, $channel, $search);
return $sth->fetchall_arrayref({}); return $sth->fetchall_arrayref({});
}; };
} else { } else {
$messages = eval { $messages = eval {
my $sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE channel = ? AND msg REGEXP ? ORDER BY timestamp DESC LIMIT 10'); my $sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE channel = ? AND msg LIKE ? ESCAPE "\" ORDER BY timestamp DESC LIMIT 10');
$sth->execute($channel, $regex); $sth->execute($channel, $search);
return $sth->fetchall_arrayref({}); return $sth->fetchall_arrayref({});
}; };
} }