Add comparison operators to KeyValue expression
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
bdc832aaab
commit
32e77b4ccd
5 changed files with 80 additions and 19 deletions
|
|
@ -2,32 +2,74 @@
|
|||
|
||||
namespace Arendsen\FluxQueryBuilder\Expression;
|
||||
|
||||
use Exception;
|
||||
|
||||
class KeyValue extends Base {
|
||||
|
||||
const EQUAL_TO = '==';
|
||||
const NOT_EQUAL_TO = '!=';
|
||||
const GREATER_THAN = '>';
|
||||
const GREATER_EQUAL_TO = '>=';
|
||||
const LESS_THAN = '<';
|
||||
const LESS_EQUAL_TO = '<=';
|
||||
const EQUAL_TO_REGEX = '=~';
|
||||
const NOT_EQUAL_TO_REGEX = '!~';
|
||||
|
||||
const COMPARISON_OPERATORS = [
|
||||
self::EQUAL_TO,
|
||||
self::NOT_EQUAL_TO,
|
||||
self::GREATER_THAN,
|
||||
self::GREATER_EQUAL_TO,
|
||||
self::LESS_THAN,
|
||||
self::LESS_EQUAL_TO,
|
||||
self::EQUAL_TO_REGEX,
|
||||
self::NOT_EQUAL_TO_REGEX,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array $expressions
|
||||
*/
|
||||
private $expressions;
|
||||
|
||||
public function __construct(string $key, string $value)
|
||||
private function __construct(string $key, string $operator, string $value)
|
||||
{
|
||||
$this->expressions[] = 'r.' . $key . ' == "' . $value . '"';
|
||||
$this->checkOperator($operator);
|
||||
$this->expressions[] = 'r.' . $key . ' ' . $operator . ' "' . $value . '"';
|
||||
}
|
||||
|
||||
public static function set(string $key, string $value): KeyValue
|
||||
public static function set(string $key, string $operator, string $value): KeyValue
|
||||
{
|
||||
return new self($key, $value);
|
||||
return new self($key, $operator, $value);
|
||||
}
|
||||
|
||||
public function and(string $key, string $value): KeyValue
|
||||
public static function setEquals(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->expressions[] = 'and r.' . $key . ' == "' . $value . '"';
|
||||
return self::set($key, self::EQUAL_TO, $value);
|
||||
}
|
||||
|
||||
public function and(string $key, string $operator, string $value): KeyValue
|
||||
{
|
||||
$this->checkOperator($operator);
|
||||
$this->expressions[] = 'and r.' . $key . ' ' . $operator . ' "' . $value . '"';
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function or(string $key, string $value): KeyValue
|
||||
public function andEquals(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->expressions[] = 'or r.' . $key . ' == "' . $value . '"';
|
||||
$this->and($key, '==', $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function or(string $key, string $operator, string $value): KeyValue
|
||||
{
|
||||
$this->checkOperator($operator);
|
||||
$this->expressions[] = 'or r.' . $key . ' ' . $operator . ' "' . $value . '"';
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orEquals(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->or($key, '==', $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
|
@ -36,4 +78,12 @@ class KeyValue extends Base {
|
|||
return implode(' ', $this->expressions);
|
||||
}
|
||||
|
||||
protected function checkOperator(string $operator)
|
||||
{
|
||||
if(!in_array($operator, self::COMPARISON_OPERATORS))
|
||||
{
|
||||
throw new Exception('Operator "' . $operator . '" is not supported!');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -64,7 +64,7 @@ class QueryBuilder {
|
|||
$this->addRequiredData(self::REQUIRED_INPUT_MEASUREMENT, $measurement);
|
||||
$this->addToQueryArray(
|
||||
self::FLUX_PART_FILTERS,
|
||||
new Filter(KeyValue::set('_measurement', $measurement))
|
||||
new Filter(KeyValue::setEquals('_measurement', $measurement))
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue