Add Flux from() and filter() methods

Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
davidarendsen 2022-08-05 15:41:38 +00:00
commit e0e69b4beb
17 changed files with 2095 additions and 0 deletions

15
src/Expression/Base.php Normal file
View file

@ -0,0 +1,15 @@
<?php
namespace Arendsen\FluxQueryBuilder\Expression;
abstract class Base {
/**
* @throws ExpressionNotImplementedException
*/
public function __toString()
{
throw new ExpressionNotImplementedException('__toString', get_class($this));
}
}

View file

@ -0,0 +1,7 @@
<?php
namespace Arendsen\FluxQueryBuilder\Expression;
use Exception;
class ExpressionNotImplementedException extends Exception {}

View file

@ -0,0 +1,30 @@
<?php
namespace Arendsen\FluxQueryBuilder\Expression\Filter;
use Arendsen\FluxQueryBuilder\Expression\Base;
class AndExpression extends Base {
/**
* @var string $key
*/
private $key;
/**
* @var string $value
*/
private $value;
public function __construct(string $key, string $value)
{
$this->key = $key;
$this->value = $value;
}
public function __toString()
{
return 'and r.' . $this->key . ' == "' . $this->value . '"';
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace Arendsen\FluxQueryBuilder\Expression\Filter;
use Arendsen\FluxQueryBuilder\Expression\Base;
class Measurement extends Base {
/**
* @var array $measurement
*/
private $measurement;
public function __construct(string $measurement)
{
$this->measurement = $measurement;
}
public function __toString()
{
return 'r._measurement == "' . $this->measurement . '"';
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace Arendsen\FluxQueryBuilder\Expression\Filter;
use Arendsen\FluxQueryBuilder\Expression\Base;
class OrExpression extends Base {
/**
* @var string $key
*/
private $key;
/**
* @var string $value
*/
private $value;
public function __construct(string $key, string $value)
{
$this->key = $key;
$this->value = $value;
}
public function __toString()
{
return 'or r.' . $this->key . ' == "' . $this->value . '"';
}
}

15
src/Function/Base.php Normal file
View file

@ -0,0 +1,15 @@
<?php
namespace Arendsen\FluxQueryBuilder\Function;
abstract class Base {
/**
* @throws FunctionNotImplementedException
*/
public function __toString()
{
throw new FunctionNotImplementedException('__toString', get_class($this));
}
}

22
src/Function/Filter.php Normal file
View file

@ -0,0 +1,22 @@
<?php
namespace Arendsen\FluxQueryBuilder\Function;
class Filter extends Base {
/**
* @var array $settings
*/
private $settings;
public function __construct(array $settings)
{
$this->settings = $settings;
}
public function __toString()
{
return '|> filter(fn: (r) => ' . implode(' ', $this->settings) . ') ';
}
}

31
src/Function/From.php Normal file
View file

@ -0,0 +1,31 @@
<?php
namespace Arendsen\FluxQueryBuilder\Function;
class From extends Base {
/**
* @var array $settings
*/
private $settings;
public function __construct(array $settings)
{
$this->settings = $settings;
}
public function __toString()
{
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;
}
}

View file

@ -0,0 +1,7 @@
<?php
namespace Arendsen\FluxQueryBuilder\Function;
use Exception;
class FunctionNotImplementedException extends Exception {}

57
src/QueryBuilder.php Normal file
View file

@ -0,0 +1,57 @@
<?php
namespace Arendsen\FluxQueryBuilder;
class QueryBuilder {
/**
* @var array $from
*/
private $from;
/**
* @var string $measurement
*/
private $measurement;
/**
* @var array $range
*/
private $range;
public function from(array $from): QueryBuilder
{
$this->from = $from;
return $this;
}
public function fromBucket(string $bucket): QueryBuilder {
$this->from = ['bucket' => $bucket];
return $this;
}
public function fromMeasurement(string $measurement): QueryBuilder
{
$this->measurement = $measurement;
return $this;
}
public function addRange(array $range): QueryBuilder
{
$this->range = $range;
return $this;
}
public function addRangeStart(string $rangeStart): QueryBuilder
{
$this->range = ['start' => $rangeStart];
return $this;
}
public function build(): string
{
//TODO: build the query dynamically here
return 'from(bucket: "test_bucket", host: "host", org: "example-org", token: "token") |> ';
}
}