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;
|
namespace Arendsen\FluxQueryBuilder\Expression;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
class KeyValue extends Base {
|
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
|
* @var array $expressions
|
||||||
*/
|
*/
|
||||||
private $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;
|
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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,4 +78,12 @@ class KeyValue extends Base {
|
||||||
return implode(' ', $this->expressions);
|
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->addRequiredData(self::REQUIRED_INPUT_MEASUREMENT, $measurement);
|
||||||
$this->addToQueryArray(
|
$this->addToQueryArray(
|
||||||
self::FLUX_PART_FILTERS,
|
self::FLUX_PART_FILTERS,
|
||||||
new Filter(KeyValue::set('_measurement', $measurement))
|
new Filter(KeyValue::setEquals('_measurement', $measurement))
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,26 @@ final class KeyValueExpressionTest extends TestCase {
|
||||||
|
|
||||||
public function testSimpleKeyvalue()
|
public function testSimpleKeyvalue()
|
||||||
{
|
{
|
||||||
$keyvalue = KeyValue::set('_measurement', 'test_measurement')
|
$keyvalue = KeyValue::setEquals('_measurement', 'test_measurement')
|
||||||
->and('_field', 'user')
|
->andEquals('_field', 'user')
|
||||||
->or('_field', 'field2')
|
->or('count', '>=', '1')
|
||||||
->and('user', 'my_username');
|
->and('user', '==', 'my_username')
|
||||||
|
->orEquals('test', 'world');
|
||||||
|
|
||||||
$query = 'r._measurement == "test_measurement" and r._field == "user" or ' .
|
$query = 'r._measurement == "test_measurement" and r._field == "user" or ' .
|
||||||
'r._field == "field2" and r.user == "my_username"';
|
'r.count >= "1" and r.user == "my_username" or r.test == "world"';
|
||||||
|
|
||||||
$this->assertEquals($keyvalue->__toString(), $query);
|
$this->assertEquals($keyvalue->__toString(), $query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInvalidOperator()
|
||||||
|
{
|
||||||
|
$this->expectException(Exception::class);
|
||||||
|
|
||||||
|
$keyvalue = KeyValue::set('_measurement', '9dkda9e', 'test_measurement')
|
||||||
|
->andEquals('_field', 'user')
|
||||||
|
->or('_field', '==', 'field2')
|
||||||
|
->andEquals('user', 'my_username');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -9,10 +9,10 @@ final class FilterFunctionTest extends TestCase {
|
||||||
|
|
||||||
public function testSimpleFilter()
|
public function testSimpleFilter()
|
||||||
{
|
{
|
||||||
$expression = new Filter(KeyValue::set('_measurement', 'test_measurement')
|
$expression = new Filter(KeyValue::setEquals('_measurement', 'test_measurement')
|
||||||
->and('_field', 'user')
|
->andEquals('_field', 'user')
|
||||||
->or('_field', 'field2')
|
->orEquals('_field', 'field2')
|
||||||
->and('user', 'my_username')
|
->andEquals('user', 'my_username')
|
||||||
);
|
);
|
||||||
|
|
||||||
$query = '|> filter(fn: (r) => r._measurement == "test_measurement" and r._field == "user" or ' .
|
$query = '|> filter(fn: (r) => r._measurement == "test_measurement" and r._field == "user" or ' .
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ final class QueryBuilderTest extends TestCase {
|
||||||
],
|
],
|
||||||
'test_measurement',
|
'test_measurement',
|
||||||
'-360h',
|
'-360h',
|
||||||
new KeyValue('user', 'username'),
|
KeyValue::setEquals('user', 'username'),
|
||||||
'from(bucket: "example_bucket") |> range(start: "-360h") |> filter(fn: (r) => r._measurement == "test_measurement") ' .
|
'from(bucket: "example_bucket") |> range(start: "-360h") |> filter(fn: (r) => r._measurement == "test_measurement") ' .
|
||||||
'|> filter(fn: (r) => r.user == "username") '
|
'|> filter(fn: (r) => r.user == "username") '
|
||||||
],
|
],
|
||||||
|
|
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue