Extract Universe Flux functions into a trait
Signed-off-by: David Arendsen <darendsen@gamepoint.com>
This commit is contained in:
parent
0a4da04e58
commit
3cff68870f
8 changed files with 267 additions and 244 deletions
0
docs/functions/addMap.md
Normal file
0
docs/functions/addMap.md
Normal file
0
docs/functions/addSort.md
Normal file
0
docs/functions/addSort.md
Normal file
0
docs/functions/addWindow.md
Normal file
0
docs/functions/addWindow.md
Normal file
|
|
@ -1,151 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Arendsen\FluxQueryBuilder\Builder;
|
|
||||||
|
|
||||||
use DateTime;
|
|
||||||
use Exception;
|
|
||||||
use Arendsen\FluxQueryBuilder\Builder\FluxPart;
|
|
||||||
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
|
||||||
use Arendsen\FluxQueryBuilder\Expression\KeyFilter;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\From;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Range;
|
|
||||||
|
|
||||||
abstract class AbstractQueryBuilder
|
|
||||||
{
|
|
||||||
public const REQUIRED_INPUT_FROM = 'from';
|
|
||||||
public const REQUIRED_INPUT_RANGE = 'range';
|
|
||||||
public const REQUIRED_INPUT_MEASUREMENT = 'measurement';
|
|
||||||
|
|
||||||
public const REQUIRED_INPUT = [
|
|
||||||
self::REQUIRED_INPUT_FROM,
|
|
||||||
self::REQUIRED_INPUT_RANGE,
|
|
||||||
self::REQUIRED_INPUT_MEASUREMENT,
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int $currentFluxQueryPart
|
|
||||||
*/
|
|
||||||
private $currentFluxQueryPart = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array $fluxQuery
|
|
||||||
*/
|
|
||||||
private $fluxQueryParts = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array $requiredData
|
|
||||||
*/
|
|
||||||
private $requiredData = [];
|
|
||||||
|
|
||||||
public function from(array $from): AbstractQueryBuilder
|
|
||||||
{
|
|
||||||
$this->addRequiredData(self::REQUIRED_INPUT_FROM, $from);
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::FROM,
|
|
||||||
new From($from)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function fromBucket(string $bucket): AbstractQueryBuilder
|
|
||||||
{
|
|
||||||
$this->from(['bucket' => $bucket]);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function fromMeasurement(string $measurement): AbstractQueryBuilder
|
|
||||||
{
|
|
||||||
$this->addRequiredData(self::REQUIRED_INPUT_MEASUREMENT, $measurement);
|
|
||||||
$this->addKeyFilter(KeyFilter::setEqualTo('_measurement', $measurement));
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated
|
|
||||||
*
|
|
||||||
* @param KeyValue $keyValue
|
|
||||||
* @return QueryBuilder
|
|
||||||
*/
|
|
||||||
public function addFilter(KeyValue $keyValue): AbstractQueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::FILTERS,
|
|
||||||
new Filter($keyValue)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addKeyFilter(KeyFilter $keyFilter): AbstractQueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::FILTERS,
|
|
||||||
new Filter($keyFilter)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addFieldFilter(array $fields): AbstractQueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::FILTERS,
|
|
||||||
new Filter($fields)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addRange(DateTime $start, ?DateTime $stop = null): AbstractQueryBuilder
|
|
||||||
{
|
|
||||||
$this->addRequiredData(self::REQUIRED_INPUT_RANGE, [$start, $stop]);
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::RANGE,
|
|
||||||
new Range($start, $stop)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addRangeStart(DateTime $start): AbstractQueryBuilder
|
|
||||||
{
|
|
||||||
$this->addRange($start);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addRangeInBetween(DateTime $start, DateTime $stop)
|
|
||||||
{
|
|
||||||
$this->addRange($start, $stop);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function addToQuery($key, $query)
|
|
||||||
{
|
|
||||||
$this->fluxQueryParts[$this->currentFluxQueryPart] = $query;
|
|
||||||
$this->currentFluxQueryPart++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function build(): string
|
|
||||||
{
|
|
||||||
$this->checkRequired();
|
|
||||||
|
|
||||||
$query = '';
|
|
||||||
|
|
||||||
foreach ($this->fluxQueryParts as $part) {
|
|
||||||
$query .= $part;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $query;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function addRequiredData(string $key, $value)
|
|
||||||
{
|
|
||||||
$this->requiredData[][$key] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function checkRequired()
|
|
||||||
{
|
|
||||||
foreach (self::REQUIRED_INPUT as $key => $input) {
|
|
||||||
if (!isset($this->requiredData[$key][$input])) {
|
|
||||||
throw new Exception('You need to define the "' . $input . '" part of the query!');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
104
src/Builder/Basics.php
Normal file
104
src/Builder/Basics.php
Normal file
|
|
@ -0,0 +1,104 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Arendsen\FluxQueryBuilder\Builder;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
use Arendsen\FluxQueryBuilder\QueryBuilder;
|
||||||
|
use Arendsen\FluxQueryBuilder\Builder\QueryBuilderInterface;
|
||||||
|
use Arendsen\FluxQueryBuilder\Builder\FluxPart;
|
||||||
|
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
||||||
|
use Arendsen\FluxQueryBuilder\Expression\KeyFilter;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\From;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Range;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\RawFunction;
|
||||||
|
|
||||||
|
trait Basics
|
||||||
|
{
|
||||||
|
public function from(array $from): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addRequiredData(QueryBuilder::REQUIRED_INPUT_FROM, $from);
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::FROM,
|
||||||
|
new From($from)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fromBucket(string $bucket): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->from(['bucket' => $bucket]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fromMeasurement(string $measurement): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addRequiredData(QueryBuilder::REQUIRED_INPUT_MEASUREMENT, $measurement);
|
||||||
|
$this->addKeyFilter(KeyFilter::setEqualTo('_measurement', $measurement));
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
|
* @param KeyValue $keyValue
|
||||||
|
* @return QueryBuilder
|
||||||
|
*/
|
||||||
|
public function addFilter(KeyValue $keyValue): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::FILTERS,
|
||||||
|
new Filter($keyValue)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addKeyFilter(KeyFilter $keyFilter): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::FILTERS,
|
||||||
|
new Filter($keyFilter)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addFieldFilter(array $fields): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::FILTERS,
|
||||||
|
new Filter($fields)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addRange(DateTime $start, ?DateTime $stop = null): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addRequiredData(QueryBuilder::REQUIRED_INPUT_RANGE, [$start, $stop]);
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::RANGE,
|
||||||
|
new Range($start, $stop)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addRangeStart(DateTime $start): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addRange($start);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addRangeInBetween(DateTime $start, DateTime $stop): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addRange($start, $stop);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addRawFunction(string $input): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::RAWFUNCTION,
|
||||||
|
new RawFunction($input)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/Builder/QueryBuilderInterface.php
Normal file
8
src/Builder/QueryBuilderInterface.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Arendsen\FluxQueryBuilder\Builder;
|
||||||
|
|
||||||
|
interface QueryBuilderInterface
|
||||||
|
{
|
||||||
|
public function build(): string;
|
||||||
|
}
|
||||||
108
src/Builder/Universe.php
Normal file
108
src/Builder/Universe.php
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Arendsen\FluxQueryBuilder\Builder;
|
||||||
|
|
||||||
|
use Arendsen\FluxQueryBuilder\Builder\QueryBuilderInterface;
|
||||||
|
use Arendsen\FluxQueryBuilder\Builder\FluxPart;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Reduce;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Sort;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Map;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Group;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Limit;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Mean;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Window;
|
||||||
|
|
||||||
|
trait Universe
|
||||||
|
{
|
||||||
|
public function addReduce(array $settings, array $identity): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::REDUCE,
|
||||||
|
new Reduce($settings, $identity)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addSort(array $columns, $desc): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::SORT,
|
||||||
|
new Sort($columns, $desc)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addMap($query): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::MAP,
|
||||||
|
new Map($query)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addGroup(array $columns, $mode = 'by'): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::GROUP,
|
||||||
|
new Group($columns, $mode)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addLimit(int $limit, int $offset = 0): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::LIMIT,
|
||||||
|
new Limit($limit, $offset)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addWindow($every, array $options = []): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::WINDOW,
|
||||||
|
new Window($every, $options)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addDuplicate(string $column, string $as): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::DUPLICATE,
|
||||||
|
new Duplicate($column, $as)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addMean(?string $column = ''): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::MEAN,
|
||||||
|
new Mean($column)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addUnWindow(): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::UNWINDOW,
|
||||||
|
new Window('inf')
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addAggregateWindow($every, $fn, array $options = []): QueryBuilderInterface
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::AGGREGATEWINDOW,
|
||||||
|
new AggregateWindow($every, $fn, $options)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,117 +2,71 @@
|
||||||
|
|
||||||
namespace Arendsen\FluxQueryBuilder;
|
namespace Arendsen\FluxQueryBuilder;
|
||||||
|
|
||||||
use Arendsen\FluxQueryBuilder\Builder\AbstractQueryBuilder;
|
use Exception;
|
||||||
use Arendsen\FluxQueryBuilder\Builder\FluxPart;
|
use Arendsen\FluxQueryBuilder\Builder\QueryBuilderInterface;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
|
use Arendsen\FluxQueryBuilder\Builder\Basics;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
|
use Arendsen\FluxQueryBuilder\Builder\Universe;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Reduce;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Sort;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Map;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Group;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Limit;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Mean;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\RawFunction;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Window;
|
|
||||||
|
|
||||||
class QueryBuilder extends AbstractQueryBuilder
|
class QueryBuilder implements QueryBuilderInterface
|
||||||
{
|
{
|
||||||
public function addReduce(array $settings, array $identity): QueryBuilder
|
use Basics;
|
||||||
|
use Universe;
|
||||||
|
|
||||||
|
public const REQUIRED_INPUT_FROM = 'from';
|
||||||
|
public const REQUIRED_INPUT_RANGE = 'range';
|
||||||
|
public const REQUIRED_INPUT_MEASUREMENT = 'measurement';
|
||||||
|
|
||||||
|
public const REQUIRED_INPUT = [
|
||||||
|
self::REQUIRED_INPUT_FROM,
|
||||||
|
self::REQUIRED_INPUT_RANGE,
|
||||||
|
self::REQUIRED_INPUT_MEASUREMENT,
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int $currentFluxQueryPart
|
||||||
|
*/
|
||||||
|
private $currentFluxQueryPart = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array $fluxQuery
|
||||||
|
*/
|
||||||
|
private $fluxQueryParts = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array $requiredData
|
||||||
|
*/
|
||||||
|
private $requiredData = [];
|
||||||
|
|
||||||
|
protected function addToQuery($key, $query)
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->fluxQueryParts[$this->currentFluxQueryPart] = $query;
|
||||||
FluxPart::REDUCE,
|
$this->currentFluxQueryPart++;
|
||||||
new Reduce($settings, $identity)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addSort(array $columns, $desc): QueryBuilder
|
public function build(): string
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->checkRequired();
|
||||||
FluxPart::SORT,
|
|
||||||
new Sort($columns, $desc)
|
$query = '';
|
||||||
);
|
|
||||||
return $this;
|
foreach ($this->fluxQueryParts as $part) {
|
||||||
|
$query .= $part;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addMap($query): QueryBuilder
|
return $query;
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::MAP,
|
|
||||||
new Map($query)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addGroup(array $columns, $mode = 'by'): QueryBuilder
|
protected function addRequiredData(string $key, $value)
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->requiredData[][$key] = $value;
|
||||||
FluxPart::GROUP,
|
|
||||||
new Group($columns, $mode)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addLimit(int $limit, int $offset = 0): QueryBuilder
|
protected function checkRequired()
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
foreach (self::REQUIRED_INPUT as $key => $input) {
|
||||||
FluxPart::LIMIT,
|
if (!isset($this->requiredData[$key][$input])) {
|
||||||
new Limit($limit, $offset)
|
throw new Exception('You need to define the "' . $input . '" part of the query!');
|
||||||
);
|
}
|
||||||
return $this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function addWindow($every, array $options = []): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::WINDOW,
|
|
||||||
new Window($every, $options)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addDuplicate(string $column, string $as): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::DUPLICATE,
|
|
||||||
new Duplicate($column, $as)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addMean(?string $column = ''): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::MEAN,
|
|
||||||
new Mean($column)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addUnWindow(): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::UNWINDOW,
|
|
||||||
new Window('inf')
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addAggregateWindow($every, $fn, array $options = []): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::AGGREGATEWINDOW,
|
|
||||||
new AggregateWindow($every, $fn, $options)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addRawFunction(string $input): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
FluxPart::RAWFUNCTION,
|
|
||||||
new RawFunction($input)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue