From d44c15eea0a1764381f9c24bba0e80c5d27b8a58 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 20 Aug 2013 22:39:31 +0200 Subject: [PATCH] Filter handling cleanup and small improvements: * a pipe (|) is now accepted as OR * improved readability * prepared for splitting logic - most of this could be backend-agnostic * greater / less then is now possible --- .../Backend/Ido/Query/AbstractQuery.php | 83 ++++++++++++------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/AbstractQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/AbstractQuery.php index 95ffe60c8..9bb8b4786 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/AbstractQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/AbstractQuery.php @@ -288,8 +288,11 @@ abstract class AbstractQuery extends Query $or = array(); $and = array(); - if (! is_array($value) && strpos($value, ',') !== false) { - $value = preg_split('~,~', $value, -1, PREG_SPLIT_NO_EMPTY); + if ( + ! is_array($value) && + (strpos($value, ',') !== false || strpos($value, '|') !== false) + ) { + $value = preg_split('~[,|]~', $value, -1, PREG_SPLIT_NO_EMPTY); } if (! is_array($value)) { $value = array($value); @@ -301,38 +304,56 @@ abstract class AbstractQuery extends Query // TODO: REALLY?? continue; } - // Value starting with minus: negation - if ($val[0] === '-') { + $not = false; + $force = false; + $op = '='; + $wildcard = false; + + if ($val[0] === '-' || $val[0] === '!') { + // Value starting with minus or !: negation $val = substr($val, 1); - if (strpos($val, '*') === false) { - $and[] = $this->db->quoteInto($column . ' != ?', $val); - } else { - $and[] = $this->db->quoteInto( - $column . ' NOT LIKE ?', - str_replace('*', '%', $val) - ); - } - } elseif ($val[0] === '+') { // Value starting with +: enforces AND + $not = true; + } + + if ($val[0] === '+') { + // Value starting with +: enforces AND // TODO: depends on correct URL handling, not given in all - // ZF versions + // ZF versions. $val = substr($val, 1); - if (strpos($val, '*') === false) { - $and[] = $this->db->quoteInto($column . ' = ?', $val); - } else { - $and[] = $this->db->quoteInto( - $column . ' LIKE ?', - str_replace('*', '%', $val) - ); - } - } else { // All others ar ORs: - if (strpos($val, '*') === false) { - $or[] = $this->db->quoteInto($column . ' = ?', $val); - } else { - $or[] = $this->db->quoteInto( - $column . ' LIKE ?', - str_replace('*', '%', $val) - ); - } + $force = true; + } + if ($val[0] === '<' || $val[0] === '>') { + $op = $val[0]; + $val = substr($val, 1); + } + if (strpos($val, '*') !== false) { + $wildcard = true; + $val = str_replace('*', '%', $val); + } + + $operator = null; + switch ($op) { + case '=': + if ($not) { + $operator = $wildcard ? 'NOT LIKE' : '!='; + } else { + $operator = $wildcard ? 'LIKE' : '='; + } + break; + case '>': + $operator = $not ? '<=' : '>'; + break; + case '<': + $operator = $not ? '>=' : '<'; + break; + default: + throw new ProgrammingError("'$op' is not a valid operator"); + } + + if ($not || $force) { + $and[] = $this->db->quoteInto($column . ' ' . $operator . ' ?', $val); + } else { + $or[] = $this->db->quoteInto($column . ' ' . $operator . ' ?', $val); } }