mirror of
https://github.com/pragma-/pbot.git
synced 2025-01-11 12:32:37 +01:00
Factoids: DualIndexSQLiteObject->get() is now powerful enough to replace FactoidsSQLite
This commit is contained in:
parent
97b2394166
commit
b178b718f0
@ -343,29 +343,25 @@ sub get {
|
||||
my @where = ();
|
||||
|
||||
foreach my $key (keys %opts) {
|
||||
if (not defined $opts{$key}) {
|
||||
push @keys, $key;
|
||||
} else {
|
||||
push @where, $key;
|
||||
if (defined $opts{$key}) {
|
||||
if ($key =~ s/^!//) {
|
||||
push @where, "\"$key\" != ?";
|
||||
} else {
|
||||
push @where, "\"$key\" = ?";
|
||||
}
|
||||
}
|
||||
push @keys, $key;
|
||||
}
|
||||
|
||||
$sql .= join ', ', @keys;
|
||||
$sql .= ' FROM Stuff WHERE ';
|
||||
|
||||
foreach my $w (@where) {
|
||||
if ($w =~ s/^!//) {
|
||||
$sql .= "\"$w\" != ?";
|
||||
} else {
|
||||
$sql .= "\"$w\" = ?";
|
||||
}
|
||||
}
|
||||
$sql .= join ' AND ', @where;
|
||||
|
||||
my $sth = $self->{dbh}->prepare($sql);
|
||||
|
||||
my $param = 0;
|
||||
foreach my $w (@where) {
|
||||
$sth->bind_param(++$param, $opts{$w});
|
||||
foreach my $key (keys %opts) {
|
||||
$sth->bind_param(++$param, $opts{$key}) if defined $opts{$key};
|
||||
}
|
||||
|
||||
$sth->execute;
|
||||
|
@ -23,7 +23,6 @@ use POSIX qw(strftime);
|
||||
use Text::ParseWords;
|
||||
use JSON;
|
||||
|
||||
use PBot::FactoidsSQLite;
|
||||
use PBot::FactoidCommands;
|
||||
|
||||
use PBot::Utils::Indefinite;
|
||||
@ -71,7 +70,7 @@ our %factoid_metadata = (
|
||||
sub initialize {
|
||||
my ($self, %conf) = @_;
|
||||
my $filename = $conf{filename};
|
||||
$self->{factoids} = PBot::FactoidsSQLite->new(name => 'Factoids', filename => $filename, pbot => $self->{pbot});
|
||||
$self->{factoids} = PBot::DualIndexSQLiteObject->new(name => 'Factoids', filename => $filename, pbot => $self->{pbot});
|
||||
|
||||
$self->{pbot} = $self->{pbot};
|
||||
$self->{commands} = PBot::FactoidCommands->new(pbot => $self->{pbot});
|
||||
@ -367,21 +366,22 @@ sub find_factoid {
|
||||
}
|
||||
|
||||
CHECK_REGEX:
|
||||
if (not $opts{exact_trigger} and defined $trigger) {
|
||||
my $factoids;
|
||||
if (not $opts{exact_trigger}) {
|
||||
my @factoids;
|
||||
|
||||
if ($opts{exact_channel}) {
|
||||
$factoids = $self->{factoids}->get_regex_by_channel($channel);
|
||||
@factoids = $self->{factoids}->get(type => 'regex', index1 => $channel, index2 => undef, action => undef);
|
||||
push @factoids, $self->{factoids}->get(type => 'regex', index1 => '.*', index2 => undef, action => undef);
|
||||
} else {
|
||||
$factoids = $self->{factoids}->get_regex_by_channel(undef);
|
||||
@factoids = $self->{factoids}->get(type => 'regex', index1 => undef, index2 => undef, action => undef);
|
||||
}
|
||||
|
||||
my $regex = qr/$trigger/i;
|
||||
foreach my $factoid (@$factoids) {
|
||||
if ($string =~ /$regex/) {
|
||||
$channel = $factoid->{index1};
|
||||
$trigger = $factoid->{index2};
|
||||
$action = $factoid->{action};
|
||||
foreach my $factoid (@factoids) {
|
||||
$channel = $factoid->{index1};
|
||||
$trigger = $factoid->{index2};
|
||||
$action = $factoid->{action};
|
||||
|
||||
if ($string =~ /$trigger/) {
|
||||
|
||||
if ($opts{find_alias}) {
|
||||
my $command = $action;
|
||||
|
@ -1,60 +0,0 @@
|
||||
# File: FactoidsSQLite.pm
|
||||
# Author: pragma_
|
||||
#
|
||||
# Purpose: SQLite backend for Factoids; adds factoid-specific functionality
|
||||
# to DualIndexSQLiteObject parent class.
|
||||
|
||||
# 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/.
|
||||
|
||||
package PBot::FactoidsSQLite;
|
||||
|
||||
use parent 'PBot::DualIndexSQLiteObject';
|
||||
|
||||
use warnings; use strict;
|
||||
use feature 'unicode_strings';
|
||||
|
||||
sub new {
|
||||
my ($proto, %conf) = @_;
|
||||
my $class = ref($proto) || $proto;
|
||||
my $self = bless {}, $class;
|
||||
Carp::croak("Missing pbot reference to " . __FILE__) unless exists $conf{pbot};
|
||||
$self->{pbot} = $conf{pbot};
|
||||
$self->SUPER::initialize(%conf);
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub get_regex_by_channel {
|
||||
my ($self, $channel) = @_;
|
||||
|
||||
my $data = eval {
|
||||
my $d = [];
|
||||
my $sth;
|
||||
if (defined $channel) {
|
||||
$sth = $self->{dbh}->prepare('SELECT index1, index2, action FROM Stuff WHERE index1 = ? AND type = "regex"');
|
||||
$sth->execute($channel);
|
||||
push @$d, @{$sth->fetchall_arrayref({})};
|
||||
|
||||
if ($channel ne '.*') {
|
||||
$sth->execute('.*');
|
||||
push @$d, @{$sth->fetchall_arrayref({})};
|
||||
}
|
||||
} else {
|
||||
$sth = $self->{dbh}->prepare('SELECT index1, index2, action FROM Stuff WHERE type = "regex"');
|
||||
$sth->execute;
|
||||
push @$d, @{$sth->fetchall_arrayref({})};
|
||||
}
|
||||
|
||||
return $d;
|
||||
};
|
||||
|
||||
if ($@) {
|
||||
$self->{pbot}->{logger}->log("Error in get_regex_by_channel: $@\n");
|
||||
return undef;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
1;
|
Loading…
Reference in New Issue
Block a user