3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-22 20:09:43 +01:00

DualIndexSQLiteObject: get_each(): put parentheses around related OR clauses

This commit is contained in:
Pragmatic Software 2020-03-03 10:10:20 -08:00
parent b7240d3bdc
commit 82b4e51f09

View File

@ -371,7 +371,7 @@ sub get_each {
} }
if (defined $op) { if (defined $op) {
my $prefix = 'AND '; my $prefix = 'AND';
if ($op eq '=' or $op eq '==') { if ($op eq '=' or $op eq '==') {
$op = '='; $op = '=';
@ -379,12 +379,12 @@ sub get_each {
$op = '!='; $op = '!=';
} }
if ($key =~ s/^(OR\s+|AND\s+)//) { if ($key =~ s/^(OR|AND)\s+//) {
$prefix = $1; $prefix = $1;
} }
$prefix = '' if not @where; $prefix = '' if not @where;
push @where, qq{$prefix"$key" $op ?}; push @where, [ $prefix, $key, $op ];
push @values, $value; push @values, $value;
} }
@ -392,8 +392,34 @@ sub get_each {
} }
$sql .= join ', ', @keys; $sql .= join ', ', @keys;
$sql .= ' FROM Stuff WHERE '; $sql .= ' FROM Stuff WHERE';
$sql .= join ' ', @where;
my $in_or = 0;
for (my $i = 0; $i < @where; $i++) {
my ($prefix, $key, $op) = @{$where[$i]};
my ($next_prefix, $next_key) = ('', '');
if ($i < @where - 1) {
($next_prefix, $next_key) = @{$where[$i + 1]};
}
if ($next_prefix eq 'OR' and $next_key eq $key) {
$sql .= "$prefix ";
$sql .= '(' if not $in_or;
$sql .= "\"$key\" $op ? ";
$in_or = 1;
} else {
$sql .= "$prefix \"$key\" $op ? ";
if ($in_or) {
$sql .= ') ';
$in_or = 0;
}
}
}
$sql .= ')' if $in_or;
$sql .= ' ORDER BY ' . join(', ', @sort) if @sort; $sql .= ' ORDER BY ' . join(', ', @sort) if @sort;
my $sth = $self->{dbh}->prepare($sql); my $sth = $self->{dbh}->prepare($sql);