Make it possible to also add integers as KeyValue value

Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
davidarendsen 2022-08-10 16:17:21 +00:00
commit eb6568de10
2 changed files with 24 additions and 21 deletions

View file

@ -31,13 +31,14 @@ class KeyValue extends Base {
*/
private $expressions;
private function __construct(string $key, string $operator, string $value)
private function __construct(string $key, string $operator, $value)
{
$this->checkOperator($operator);
$this->expressions[] = 'r.' . $key . ' ' . $operator . ' "' . $value . '"';
$value = is_string($value) ? '"' . $value . '"' : $value;
$this->expressions[] = 'r.' . $key . ' ' . $operator . ' ' . $value;
}
public static function set(string $key, string $operator, string $value): KeyValue
public static function set(string $key, string $operator, $value): KeyValue
{
return new self($key, $operator, $value);
}
@ -52,22 +53,22 @@ class KeyValue extends Base {
return self::set($key, self::NOT_EQUAL_TO, $value);
}
public static function setGreaterThan(string $key, string $value): KeyValue
public static function setGreaterThan(string $key, int $value): KeyValue
{
return self::set($key, self::GREATER_THAN, $value);
}
public static function setGreaterOrEqualTo(string $key, string $value): KeyValue
public static function setGreaterOrEqualTo(string $key, int $value): KeyValue
{
return self::set($key, self::GREATER_EQUAL_TO, $value);
}
public static function setLessThan(string $key, string $value): KeyValue
public static function setLessThan(string $key, int $value): KeyValue
{
return self::set($key, self::LESS_THAN, $value);
}
public static function setLessOrEqualTo(string $key, string $value): KeyValue
public static function setLessOrEqualTo(string $key, int $value): KeyValue
{
return self::set($key, self::LESS_EQUAL_TO, $value);
}
@ -82,10 +83,11 @@ class KeyValue extends Base {
return self::set($key, self::NOT_EQUAL_TO_REGEX, $value);
}
public function and(string $key, string $operator, string $value): KeyValue
public function and(string $key, string $operator, $value): KeyValue
{
$this->checkOperator($operator);
$this->expressions[] = 'and r.' . $key . ' ' . $operator . ' "' . $value . '"';
$value = is_string($value) ? '"' . $value . '"' : $value;
$this->expressions[] = 'and r.' . $key . ' ' . $operator . ' ' . $value;
return $this;
}
@ -101,25 +103,25 @@ class KeyValue extends Base {
return $this;
}
public function andGreaterThan(string $key, string $value): KeyValue
public function andGreaterThan(string $key, int $value): KeyValue
{
$this->and($key, self::GREATER_THAN, $value);
return $this;
}
public function andGreaterOrEqualTo(string $key, string $value): KeyValue
public function andGreaterOrEqualTo(string $key, int $value): KeyValue
{
$this->and($key, self::GREATER_EQUAL_TO, $value);
return $this;
}
public function andLessThan(string $key, string $value): KeyValue
public function andLessThan(string $key, int $value): KeyValue
{
$this->and($key, self::LESS_THAN, $value);
return $this;
}
public function andLessOrEqualTo(string $key, string $value): KeyValue
public function andLessOrEqualTo(string $key, int $value): KeyValue
{
$this->and($key, self::LESS_EQUAL_TO, $value);
return $this;
@ -137,10 +139,11 @@ class KeyValue extends Base {
return $this;
}
public function or(string $key, string $operator, string $value): KeyValue
public function or(string $key, string $operator, $value): KeyValue
{
$this->checkOperator($operator);
$this->expressions[] = 'or r.' . $key . ' ' . $operator . ' "' . $value . '"';
$value = is_string($value) ? '"' . $value . '"' : $value;
$this->expressions[] = 'or r.' . $key . ' ' . $operator . ' ' . $value;
return $this;
}
@ -156,25 +159,25 @@ class KeyValue extends Base {
return $this;
}
public function orGreaterThan(string $key, string $value): KeyValue
public function orGreaterThan(string $key, int $value): KeyValue
{
$this->or($key, self::GREATER_THAN, $value);
return $this;
}
public function orGreaterOrEqualTo(string $key, string $value): KeyValue
public function orGreaterOrEqualTo(string $key, int $value): KeyValue
{
$this->or($key, self::GREATER_EQUAL_TO, $value);
return $this;
}
public function orLessThan(string $key, string $value): KeyValue
public function orLessThan(string $key, int $value): KeyValue
{
$this->or($key, self::LESS_THAN, $value);
return $this;
}
public function orLessOrEqualTo(string $key, string $value): KeyValue
public function orLessOrEqualTo(string $key, int $value): KeyValue
{
$this->or($key, self::LESS_EQUAL_TO, $value);
return $this;

View file

@ -97,12 +97,12 @@ final class QueryBuilderTest extends TestCase {
->addMap('r with name: r.user')
->addGroup(['_field', 'ip'])
->addReduce(['count' => 'accumulator.count + 1'], ['count' => 0])
->addFilter(KeyValue::setGreaterOrEqualTo('count', '1'));
->addFilter(KeyValue::setGreaterOrEqualTo('count', 1)->andGreaterOrEqualTo('count2', 2));
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: "-3h") ' .
'|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' .
'|> filter(fn: (r) => r._measurement == "test_measurement") |> filter(fn: (r) => r._field == "username") ' .
'|> filter(fn: (r) => r.count >= "1") |> map(fn: (r) => ({ r with name: r.user })) ' .
'|> filter(fn: (r) => r.count >= 1 and r.count2 >= 2) |> map(fn: (r) => ({ r with name: r.user })) ' .
'|> group(columns: ["_field", "ip"], mode: "by") ';
$this->assertEquals($expectedQuery, $queryBuilder->build());