mirror of
https://github.com/pragma-/pbot.git
synced 2025-02-09 02:54:23 +01:00
DualIndexSQLiteObject: replace get() with get_all(), get_each() and get_next()
This commit is contained in:
parent
3ca3917891
commit
178c02de72
@ -88,6 +88,12 @@ sub end {
|
|||||||
$self->{pbot}->{timer}->unregister("DualIndexSQLiteObject $self->{name} Timer");
|
$self->{pbot}->{timer}->unregister("DualIndexSQLiteObject $self->{name} Timer");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub load {
|
||||||
|
my ($self) = @_;
|
||||||
|
$self->create_database;
|
||||||
|
$self->create_cache;
|
||||||
|
}
|
||||||
|
|
||||||
sub create_database {
|
sub create_database {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
|
|
||||||
@ -334,37 +340,79 @@ sub get_keys {
|
|||||||
return @keys;
|
return @keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get {
|
sub get_each {
|
||||||
my ($self, %opts) = @_;
|
my ($self, %opts) = @_;
|
||||||
|
|
||||||
my $data = eval {
|
my $sth = eval {
|
||||||
my $sql = 'SELECT ';
|
my $sql = 'SELECT ';
|
||||||
my @keys = ();
|
my @keys = ();
|
||||||
my @where = ();
|
my @where = ();
|
||||||
|
|
||||||
foreach my $key (keys %opts) {
|
foreach my $key (keys %opts) {
|
||||||
|
next if $key eq '_sort';
|
||||||
|
|
||||||
|
if ($key eq '_everything') {
|
||||||
|
push @keys, '*';
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
if (defined $opts{$key}) {
|
if (defined $opts{$key}) {
|
||||||
if ($key =~ s/^!//) {
|
if ($key =~ s/^!//) {
|
||||||
push @where, "\"$key\" != ?";
|
push @where, qq{"$key" != ?};
|
||||||
} else {
|
} else {
|
||||||
push @where, "\"$key\" = ?";
|
push @where, qq{"$key" = ?};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
push @keys, $key;
|
|
||||||
|
push @keys, $key unless $opts{_everything};
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql .= join ', ', @keys;
|
$sql .= join ', ', @keys;
|
||||||
$sql .= ' FROM Stuff WHERE ';
|
$sql .= ' FROM Stuff WHERE ';
|
||||||
$sql .= join ' AND ', @where;
|
$sql .= join ' AND ', @where;
|
||||||
|
$sql .= qq{ORDER BY "$opts{_sort}"} if defined $opts{_sort};
|
||||||
|
|
||||||
my $sth = $self->{dbh}->prepare($sql);
|
my $sth = $self->{dbh}->prepare($sql);
|
||||||
|
|
||||||
my $param = 0;
|
my $param = 0;
|
||||||
foreach my $key (keys %opts) {
|
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->bind_param(++$param, $opts{$key}) if defined $opts{$key};
|
||||||
}
|
}
|
||||||
|
|
||||||
$sth->execute;
|
$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({});
|
return $sth->fetchall_arrayref({});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -719,12 +767,6 @@ sub unset {
|
|||||||
return "[$name1] $name2.$key unset.";
|
return "[$name1] $name2.$key unset.";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub load {
|
|
||||||
my ($self) = @_;
|
|
||||||
$self->create_database;
|
|
||||||
$self->create_cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
# nothing to do here for SQLite
|
# nothing to do here for SQLite
|
||||||
# kept for compatibility with DualIndexHashObject
|
# kept for compatibility with DualIndexHashObject
|
||||||
sub save { }
|
sub save { }
|
||||||
|
@ -732,7 +732,6 @@ sub factalias {
|
|||||||
|
|
||||||
$self->{pbot}->{factoids}->add_factoid('text', $chan, "$nick!$user\@$host", $alias, "/call $command");
|
$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}->{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);
|
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) {
|
foreach my $chan (sort $factoids->get_keys) {
|
||||||
next if defined $channel and $chan !~ /^$channel$/i;
|
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;
|
my $match = 0;
|
||||||
|
|
||||||
if ($owner eq '.*') {
|
if ($owner eq '.*') {
|
||||||
|
Loading…
Reference in New Issue
Block a user