diff --git a/PBot/FactoidsSQLite.pm b/PBot/FactoidsSQLite.pm new file mode 100644 index 00000000..62c5a6c4 --- /dev/null +++ b/PBot/FactoidsSQLite.pm @@ -0,0 +1,77 @@ +# 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_all_by_trigger { + my ($self, $index2) = @_; + + my $data = eval { + my $sth = $self->{dbh}->prepare('SELECT index1, index2, action FROM Stuff WHERE index2 = ?'); + $sth->execute($index2); + return $sth->fetchall_arrayref({}); + }; + + if ($@) { + $self->{pbot}->{logger}->log("Error in get_all_by_trigger: $@\n"); + return undef; + } + + return $data; +} + +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;