Add from, range, filter functions to QueryBuilder
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
b2d1bd403c
commit
34845d68a1
5 changed files with 164 additions and 23 deletions
|
|
@ -2,21 +2,23 @@
|
|||
|
||||
namespace Arendsen\FluxQueryBuilder\Function;
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
||||
|
||||
class Filter extends Base {
|
||||
|
||||
/**
|
||||
* @var array $settings
|
||||
* @var KeyValue $keyValue
|
||||
*/
|
||||
private $settings;
|
||||
private $keyValue;
|
||||
|
||||
public function __construct(array $settings)
|
||||
public function __construct(KeyValue $keyValue)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
$this->keyValue = $keyValue;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return '|> filter(fn: (r) => ' . implode(' ', $this->settings) . ') ';
|
||||
return '|> filter(fn: (r) => ' . $this->keyValue . ') ';
|
||||
}
|
||||
|
||||
}
|
||||
31
src/Function/Range.php
Normal file
31
src/Function/Range.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Function;
|
||||
|
||||
class Range extends Base {
|
||||
|
||||
/**
|
||||
* @var array $settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
public function __construct(array $settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2,8 +2,23 @@
|
|||
|
||||
namespace Arendsen\FluxQueryBuilder;
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
||||
use Arendsen\FluxQueryBuilder\Function\Filter;
|
||||
use Arendsen\FluxQueryBuilder\Function\From;
|
||||
use Arendsen\FluxQueryBuilder\Function\Range;
|
||||
use Exception;
|
||||
|
||||
class QueryBuilder {
|
||||
|
||||
const FLUX_PART_FROM = 'from';
|
||||
const FLUX_PART_RANGE = 'range';
|
||||
const FLUX_PART_FILTERS = 'filters';
|
||||
|
||||
/**
|
||||
* @var array $fluxQuery
|
||||
*/
|
||||
private $fluxQueryParts = [];
|
||||
|
||||
/**
|
||||
* @var array $from
|
||||
*/
|
||||
|
|
@ -22,36 +37,96 @@ class QueryBuilder {
|
|||
public function from(array $from): QueryBuilder
|
||||
{
|
||||
$this->from = $from;
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_FROM,
|
||||
new From($this->from)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fromBucket(string $bucket): QueryBuilder {
|
||||
$this->from = ['bucket' => $bucket];
|
||||
$this->from(['bucket' => $bucket]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fromMeasurement(string $measurement): QueryBuilder
|
||||
{
|
||||
$this->measurement = $measurement;
|
||||
$this->addToQueryArray(
|
||||
self::FLUX_PART_FILTERS,
|
||||
new Filter(KeyValue::set('_measurement', $measurement))
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRange(array $range): QueryBuilder
|
||||
{
|
||||
$this->range = $range;
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_RANGE,
|
||||
new Range($range)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRangeStart(string $rangeStart): QueryBuilder
|
||||
{
|
||||
$this->range = ['start' => $rangeStart];
|
||||
$this->addRange(['start' => $rangeStart]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function addToQuery($key, $query)
|
||||
{
|
||||
$this->fluxQueryParts[$key] = $query;
|
||||
}
|
||||
|
||||
protected function addToQueryArray($key, $query)
|
||||
{
|
||||
$this->fluxQueryParts[$key][] = $query;
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
$this->checkRequired();
|
||||
|
||||
$query = '';
|
||||
|
||||
if(isset($this->fluxQueryParts[self::FLUX_PART_FROM])) {
|
||||
$query .= $this->fluxQueryParts[self::FLUX_PART_FROM];
|
||||
}
|
||||
|
||||
if(isset($this->fluxQueryParts[self::FLUX_PART_RANGE])) {
|
||||
$query .= $this->fluxQueryParts[self::FLUX_PART_RANGE];
|
||||
}
|
||||
|
||||
if(isset($this->fluxQueryParts[self::FLUX_PART_FILTERS])) {
|
||||
foreach($this->fluxQueryParts[self::FLUX_PART_FILTERS] as $filter) {
|
||||
$query .= $filter;
|
||||
}
|
||||
}
|
||||
|
||||
return $query;
|
||||
|
||||
//TODO: build the query dynamically here
|
||||
return 'from(bucket: "test_bucket", host: "host", org: "example-org", token: "token") |> ';
|
||||
return 'from(bucket: "test_bucket") |> ';
|
||||
}
|
||||
|
||||
protected function checkRequired()
|
||||
{
|
||||
if(!$this->from)
|
||||
{
|
||||
throw new Exception('You need to define the from part of the query!');
|
||||
}
|
||||
|
||||
if(!$this->measurement)
|
||||
{
|
||||
throw new Exception('You need to define the measurement of the query!');
|
||||
}
|
||||
|
||||
if(!$this->range)
|
||||
{
|
||||
throw new Exception('You need to define the range part of the query!');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue