Make it possible to also add integers as KeyValue value
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
147b2d9017
commit
eb6568de10
2 changed files with 24 additions and 21 deletions
|
|
@ -31,13 +31,14 @@ class KeyValue extends Base {
|
||||||
*/
|
*/
|
||||||
private $expressions;
|
private $expressions;
|
||||||
|
|
||||||
private function __construct(string $key, string $operator, string $value)
|
private function __construct(string $key, string $operator, $value)
|
||||||
{
|
{
|
||||||
$this->checkOperator($operator);
|
$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);
|
return new self($key, $operator, $value);
|
||||||
}
|
}
|
||||||
|
|
@ -52,22 +53,22 @@ class KeyValue extends Base {
|
||||||
return self::set($key, self::NOT_EQUAL_TO, $value);
|
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);
|
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);
|
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);
|
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);
|
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);
|
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->checkOperator($operator);
|
||||||
$this->expressions[] = 'and r.' . $key . ' ' . $operator . ' "' . $value . '"';
|
$value = is_string($value) ? '"' . $value . '"' : $value;
|
||||||
|
$this->expressions[] = 'and r.' . $key . ' ' . $operator . ' ' . $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -101,25 +103,25 @@ class KeyValue extends Base {
|
||||||
return $this;
|
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);
|
$this->and($key, self::GREATER_THAN, $value);
|
||||||
return $this;
|
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);
|
$this->and($key, self::GREATER_EQUAL_TO, $value);
|
||||||
return $this;
|
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);
|
$this->and($key, self::LESS_THAN, $value);
|
||||||
return $this;
|
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);
|
$this->and($key, self::LESS_EQUAL_TO, $value);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -137,10 +139,11 @@ class KeyValue extends Base {
|
||||||
return $this;
|
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->checkOperator($operator);
|
||||||
$this->expressions[] = 'or r.' . $key . ' ' . $operator . ' "' . $value . '"';
|
$value = is_string($value) ? '"' . $value . '"' : $value;
|
||||||
|
$this->expressions[] = 'or r.' . $key . ' ' . $operator . ' ' . $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -156,25 +159,25 @@ class KeyValue extends Base {
|
||||||
return $this;
|
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);
|
$this->or($key, self::GREATER_THAN, $value);
|
||||||
return $this;
|
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);
|
$this->or($key, self::GREATER_EQUAL_TO, $value);
|
||||||
return $this;
|
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);
|
$this->or($key, self::LESS_THAN, $value);
|
||||||
return $this;
|
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);
|
$this->or($key, self::LESS_EQUAL_TO, $value);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
|
||||||
|
|
@ -97,12 +97,12 @@ final class QueryBuilderTest extends TestCase {
|
||||||
->addMap('r with name: r.user')
|
->addMap('r with name: r.user')
|
||||||
->addGroup(['_field', 'ip'])
|
->addGroup(['_field', 'ip'])
|
||||||
->addReduce(['count' => 'accumulator.count + 1'], ['count' => 0])
|
->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") ' .
|
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: "-3h") ' .
|
||||||
'|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' .
|
'|> 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._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") ';
|
'|> group(columns: ["_field", "ip"], mode: "by") ';
|
||||||
|
|
||||||
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
||||||
|
|
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue