Add more helper methods to KeyValue expression
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
32e77b4ccd
commit
1922f33c5c
9 changed files with 146 additions and 45 deletions
|
|
@ -42,11 +42,46 @@ class KeyValue extends Base {
|
|||
return new self($key, $operator, $value);
|
||||
}
|
||||
|
||||
public static function setEquals(string $key, string $value): KeyValue
|
||||
public static function setEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
return self::set($key, self::EQUAL_TO, $value);
|
||||
}
|
||||
|
||||
public static function setNotEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
return self::set($key, self::NOT_EQUAL_TO, $value);
|
||||
}
|
||||
|
||||
public static function setGreaterThan(string $key, string $value): KeyValue
|
||||
{
|
||||
return self::set($key, self::GREATER_THAN, $value);
|
||||
}
|
||||
|
||||
public static function setGreaterOrEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
return self::set($key, self::GREATER_EQUAL_TO, $value);
|
||||
}
|
||||
|
||||
public static function setLessThan(string $key, string $value): KeyValue
|
||||
{
|
||||
return self::set($key, self::LESS_THAN, $value);
|
||||
}
|
||||
|
||||
public static function setLessOrEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
return self::set($key, self::LESS_EQUAL_TO, $value);
|
||||
}
|
||||
|
||||
public static function setEqualToRegex(string $key, string $value): KeyValue
|
||||
{
|
||||
return self::set($key, self::EQUAL_TO_REGEX, $value);
|
||||
}
|
||||
|
||||
public static function setNotEqualToRegex(string $key, string $value): KeyValue
|
||||
{
|
||||
return self::set($key, self::NOT_EQUAL_TO_REGEX, $value);
|
||||
}
|
||||
|
||||
public function and(string $key, string $operator, string $value): KeyValue
|
||||
{
|
||||
$this->checkOperator($operator);
|
||||
|
|
@ -54,9 +89,51 @@ class KeyValue extends Base {
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function andEquals(string $key, string $value): KeyValue
|
||||
public function andEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->and($key, '==', $value);
|
||||
$this->and($key, self::EQUAL_TO, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function andNotEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->and($key, self::NOT_EQUAL_TO, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function andGreaterThan(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->and($key, self::GREATER_THAN, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function andGreaterOrEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->and($key, self::GREATER_EQUAL_TO, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function andLessThan(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->and($key, self::LESS_THAN, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function andLessOrEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->and($key, self::LESS_EQUAL_TO, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function andEqualToRegex(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->and($key, self::EQUAL_TO_REGEX, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function andNotEqualToRegex(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->and($key, self::NOT_EQUAL_TO_REGEX, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
|
@ -67,9 +144,51 @@ class KeyValue extends Base {
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function orEquals(string $key, string $value): KeyValue
|
||||
public function orEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->or($key, '==', $value);
|
||||
$this->or($key, self::EQUAL_TO, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orNotEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->or($key, self::NOT_EQUAL_TO, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orGreaterThan(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->or($key, self::GREATER_THAN, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orGreaterOrEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->or($key, self::GREATER_EQUAL_TO, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orLessThan(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->or($key, self::LESS_THAN, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orLessOrEqualTo(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->or($key, self::LESS_EQUAL_TO, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orEqualToRegex(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->or($key, self::EQUAL_TO_REGEX, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orNotEqualToRegex(string $key, string $value): KeyValue
|
||||
{
|
||||
$this->or($key, self::NOT_EQUAL_TO_REGEX, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,4 +12,13 @@ abstract class Base {
|
|||
throw new FunctionNotImplementedException('__toString', get_class($this));
|
||||
}
|
||||
|
||||
protected function format(array $settings)
|
||||
{
|
||||
array_walk($settings, function(&$value, $key) {
|
||||
$value = $key . ': ' . (is_string($value) ? '"' . $value . '"' : $value);
|
||||
});
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -19,13 +19,4 @@ class From extends Base {
|
|||
return 'from(' . implode(', ', $this->format($this->settings)) . ') ';
|
||||
}
|
||||
|
||||
protected function format(array $settings)
|
||||
{
|
||||
array_walk($settings, function(&$value, $key) {
|
||||
$value = $key . ': ' . (is_string($value) ? '"' . $value . '"' : $value);
|
||||
});
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -19,13 +19,4 @@ class Range extends Base {
|
|||
return '|> range(' . implode(', ', $this->format($this->settings)) . ') ';
|
||||
}
|
||||
|
||||
protected function format(array $settings)
|
||||
{
|
||||
array_walk($settings, function(&$value, $key) {
|
||||
$value = $key . ': ' . (is_string($value) ? '"' . $value . '"' : $value);
|
||||
});
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -37,13 +37,4 @@ class Reduce extends Base {
|
|||
return $settings;
|
||||
}
|
||||
|
||||
protected function format(array $settings)
|
||||
{
|
||||
array_walk($settings, function(&$value, $key) {
|
||||
$value = $key . ': ' . (is_string($value) ? '"' . $value . '"' : $value);
|
||||
});
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -64,7 +64,7 @@ class QueryBuilder {
|
|||
$this->addRequiredData(self::REQUIRED_INPUT_MEASUREMENT, $measurement);
|
||||
$this->addToQueryArray(
|
||||
self::FLUX_PART_FILTERS,
|
||||
new Filter(KeyValue::setEquals('_measurement', $measurement))
|
||||
new Filter(KeyValue::setEqualTo('_measurement', $measurement))
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,14 +8,14 @@ final class KeyValueExpressionTest extends TestCase {
|
|||
|
||||
public function testSimpleKeyvalue()
|
||||
{
|
||||
$keyvalue = KeyValue::setEquals('_measurement', 'test_measurement')
|
||||
->andEquals('_field', 'user')
|
||||
$keyvalue = KeyValue::setEqualTo('_measurement', 'test_measurement')
|
||||
->andEqualTo('_field', 'user')
|
||||
->or('count', '>=', '1')
|
||||
->and('user', '==', 'my_username')
|
||||
->orEquals('test', 'world');
|
||||
->orNotEqualTo('test', 'world');
|
||||
|
||||
$query = 'r._measurement == "test_measurement" and r._field == "user" or ' .
|
||||
'r.count >= "1" and r.user == "my_username" or r.test == "world"';
|
||||
'r.count >= "1" and r.user == "my_username" or r.test != "world"';
|
||||
|
||||
$this->assertEquals($keyvalue->__toString(), $query);
|
||||
}
|
||||
|
|
@ -24,10 +24,10 @@ final class KeyValueExpressionTest extends TestCase {
|
|||
{
|
||||
$this->expectException(Exception::class);
|
||||
|
||||
$keyvalue = KeyValue::set('_measurement', '9dkda9e', 'test_measurement')
|
||||
->andEquals('_field', 'user')
|
||||
KeyValue::set('_measurement', '9dkda9e', 'test_measurement')
|
||||
->andEqualTo('_field', 'user')
|
||||
->or('_field', '==', 'field2')
|
||||
->andEquals('user', 'my_username');
|
||||
->andEqualTo('user', 'my_username');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -9,10 +9,10 @@ final class FilterFunctionTest extends TestCase {
|
|||
|
||||
public function testSimpleFilter()
|
||||
{
|
||||
$expression = new Filter(KeyValue::setEquals('_measurement', 'test_measurement')
|
||||
->andEquals('_field', 'user')
|
||||
->orEquals('_field', 'field2')
|
||||
->andEquals('user', 'my_username')
|
||||
$expression = new Filter(KeyValue::setEqualTo('_measurement', 'test_measurement')
|
||||
->andEqualTo('_field', 'user')
|
||||
->orEqualTo('_field', 'field2')
|
||||
->andEqualTo('user', 'my_username')
|
||||
);
|
||||
|
||||
$query = '|> filter(fn: (r) => r._measurement == "test_measurement" and r._field == "user" or ' .
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ final class QueryBuilderTest extends TestCase {
|
|||
],
|
||||
'test_measurement',
|
||||
'-360h',
|
||||
KeyValue::setEquals('user', 'username'),
|
||||
KeyValue::setEqualTo('user', 'username'),
|
||||
'from(bucket: "example_bucket") |> range(start: "-360h") |> filter(fn: (r) => r._measurement == "test_measurement") ' .
|
||||
'|> filter(fn: (r) => r.user == "username") '
|
||||
],
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue