DualIndexSQLiteObject: replace get() with get_all(), get_each() and get_next()

This commit is contained in:
Pragmatic Software 2020-02-29 01:45:17 -08:00
parent 3ca3917891
commit 178c02de72
2 changed files with 54 additions and 13 deletions

View File

@ -88,6 +88,12 @@ sub end {
$self->{pbot}->{timer}->unregister("DualIndexSQLiteObject $self->{name} Timer");
}
sub load {
my ($self) = @_;
$self->create_database;
$self->create_cache;
}
sub create_database {
my ($self) = @_;
@ -334,37 +340,79 @@ sub get_keys {
return @keys;
}
sub get {
sub get_each {
my ($self, %opts) = @_;
my $data = eval {
my $sth = eval {
my $sql = 'SELECT ';
my @keys = ();
my @where = ();
foreach my $key (keys %opts) {
next if $key eq '_sort';
if ($key eq '_everything') {
push @keys, '*';
next;
}
if (defined $opts{$key}) {
if ($key =~ s/^!//) {
push @where, "\"$key\" != ?";
push @where, qq{"$key" != ?};
} else {
push @where, "\"$key\" = ?";
push @where, qq{"$key" = ?};
}
}
push @keys, $key;
push @keys, $key unless $opts{_everything};
}
$sql .= join ', ', @keys;
$sql .= ' FROM Stuff WHERE ';
$sql .= join ' AND ', @where;
$sql .= qq{ORDER BY "$opts{_sort}"} if defined $opts{_sort};
my $sth = $self->{dbh}->prepare($sql);
my $param = 0;
foreach my $key (keys %opts) {
next if $key eq '_everything' or $key eq '_sort';
$sth->bind_param(++$param, $opts{$key}) if defined $opts{$key};
}
$sth->execute;
return $sth;
};
if ($@) {
$self->{pbot}->{logger}->log("Error getting data: $@\n");
return undef;
}
return $sth;
}
sub get_next {
my ($self, $sth) = @_;
my $data = eval {
return $sth->fetchrow_hashref;
};
if ($@) {
$self->{pbot}->{logger}->log("Error getting next: $@\n");
return undef;
}
return $data;
}
sub get_all {
my ($self, %opts) = @_;
my $sth = $self->get_each(%opts);
my $data = eval {
return $sth->fetchall_arrayref({});
};
@ -719,12 +767,6 @@ sub unset {
return "[$name1] $name2.$key unset.";
}
sub load {
my ($self) = @_;
$self->create_database;
$self->create_cache;
}
# nothing to do here for SQLite
# kept for compatibility with DualIndexHashObject
sub save { }

View File

@ -732,7 +732,6 @@ sub factalias {
$self->{pbot}->{factoids}->add_factoid('text', $chan, "$nick!$user\@$host", $alias, "/call $command");
$self->{pbot}->{logger}->log("$nick!$user\@$host [$chan] aliased $alias => $command\n");
$self->{pbot}->{factoids}->save_factoids();
return "/say $alias aliases `$command` for " . ($chan eq '.*' ? 'the global channel' : $chan);
}
@ -1322,7 +1321,7 @@ sub factfind {
foreach my $chan (sort $factoids->get_keys) {
next if defined $channel and $chan !~ /^$channel$/i;
foreach my $factoid ($factoids->get(index1 => $chan, index2 => undef, owner => undef, ref_user => undef, edited_by => undef, action => undef)) {
foreach my $factoid ($factoids->get_all(index1 => $chan, index2 => undef, owner => undef, ref_user => undef, edited_by => undef, action => undef)) {
my $match = 0;
if ($owner eq '.*') {