Extract general QueryBuilder logic into an abstract class to make it more maintainable
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
dbc83d596c
commit
df6ff71375
5 changed files with 218 additions and 176 deletions
161
src/Builder/AbstractQueryBuilder.php
Normal file
161
src/Builder/AbstractQueryBuilder.php
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
<?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\AggregateWindow;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
||||
use Arendsen\FluxQueryBuilder\Functions\From;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Range;
|
||||
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;
|
||||
|
||||
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!');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/Builder/FluxPart.php
Normal file
21
src/Builder/FluxPart.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Builder;
|
||||
|
||||
class FluxPart
|
||||
{
|
||||
public const FROM = 'from';
|
||||
public const RANGE = 'range';
|
||||
public const FILTERS = 'filters';
|
||||
public const REDUCE = 'reduce';
|
||||
public const MAP = 'map';
|
||||
public const SORT = 'sort';
|
||||
public const GROUP = 'group';
|
||||
public const LIMIT = 'limit';
|
||||
public const WINDOW = 'window';
|
||||
public const MEAN = 'mean';
|
||||
public const DUPLICATE = 'duplicate';
|
||||
public const UNWINDOW = 'unwindow';
|
||||
public const AGGREGATEWINDOW = 'aggregateWindow';
|
||||
public const RAWFUNCTION = 'raw';
|
||||
}
|
||||
|
|
@ -2,15 +2,10 @@
|
|||
|
||||
namespace Arendsen\FluxQueryBuilder;
|
||||
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
||||
use Arendsen\FluxQueryBuilder\Expression\KeyFilter;
|
||||
use Arendsen\FluxQueryBuilder\Builder\AbstractQueryBuilder;
|
||||
use Arendsen\FluxQueryBuilder\Builder\FluxPart;
|
||||
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
||||
use Arendsen\FluxQueryBuilder\Functions\From;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Range;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Reduce;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Sort;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Map;
|
||||
|
|
@ -20,130 +15,12 @@ use Arendsen\FluxQueryBuilder\Functions\Mean;
|
|||
use Arendsen\FluxQueryBuilder\Functions\RawFunction;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Window;
|
||||
|
||||
class QueryBuilder
|
||||
class QueryBuilder extends AbstractQueryBuilder
|
||||
{
|
||||
public const FLUX_PART_FROM = 'from';
|
||||
public const FLUX_PART_RANGE = 'range';
|
||||
public const FLUX_PART_FILTERS = 'filters';
|
||||
public const FLUX_PART_REDUCE = 'reduce';
|
||||
public const FLUX_PART_MAP = 'map';
|
||||
public const FLUX_PART_SORT = 'sort';
|
||||
public const FLUX_PART_GROUP = 'group';
|
||||
public const FLUX_PART_LIMIT = 'limit';
|
||||
public const FLUX_PART_WINDOW = 'window';
|
||||
public const FLUX_PART_MEAN = 'mean';
|
||||
public const FLUX_PART_DUPLICATE = 'duplicate';
|
||||
public const FLUX_PART_UNWINDOW = 'unwindow';
|
||||
public const FLUX_PART_AGGREGATEWINDOW = 'aggregateWindow';
|
||||
public const FLUX_PART_RAWFUNCTION = 'raw';
|
||||
|
||||
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): QueryBuilder
|
||||
{
|
||||
$this->addRequiredData(self::REQUIRED_INPUT_FROM, $from);
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_FROM,
|
||||
new From($from)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fromBucket(string $bucket): QueryBuilder
|
||||
{
|
||||
$this->from(['bucket' => $bucket]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fromMeasurement(string $measurement): QueryBuilder
|
||||
{
|
||||
$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): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_FILTERS,
|
||||
new Filter($keyValue)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addKeyFilter(KeyFilter $keyFilter): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_FILTERS,
|
||||
new Filter($keyFilter)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addFieldFilter(array $fields): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_FILTERS,
|
||||
new Filter($fields)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRange(DateTime $start, ?DateTime $stop = null): QueryBuilder
|
||||
{
|
||||
$this->addRequiredData(self::REQUIRED_INPUT_RANGE, [$start, $stop]);
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_RANGE,
|
||||
new Range($start, $stop)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRangeStart(DateTime $start): QueryBuilder
|
||||
{
|
||||
$this->addRange($start);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRangeInBetween(DateTime $start, DateTime $stop)
|
||||
{
|
||||
$this->addRange($start, $stop);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addReduce(array $settings, array $identity): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_REDUCE,
|
||||
FluxPart::REDUCE,
|
||||
new Reduce($settings, $identity)
|
||||
);
|
||||
return $this;
|
||||
|
|
@ -152,7 +29,7 @@ class QueryBuilder
|
|||
public function addSort(array $columns, $desc): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_SORT,
|
||||
FluxPart::SORT,
|
||||
new Sort($columns, $desc)
|
||||
);
|
||||
return $this;
|
||||
|
|
@ -161,7 +38,7 @@ class QueryBuilder
|
|||
public function addMap($query): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_MAP,
|
||||
FluxPart::MAP,
|
||||
new Map($query)
|
||||
);
|
||||
return $this;
|
||||
|
|
@ -170,17 +47,17 @@ class QueryBuilder
|
|||
public function addGroup(array $columns, $mode = 'by'): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_GROUP,
|
||||
FluxPart::GROUP,
|
||||
new Group($columns, $mode)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addLimit(int $limit): QueryBuilder
|
||||
public function addLimit(int $limit, int $offset = 0): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_LIMIT,
|
||||
new Limit($limit)
|
||||
FluxPart::LIMIT,
|
||||
new Limit($limit, $offset)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
|
@ -188,7 +65,7 @@ class QueryBuilder
|
|||
public function addWindow($every, array $options = []): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_WINDOW,
|
||||
FluxPart::WINDOW,
|
||||
new Window($every, $options)
|
||||
);
|
||||
return $this;
|
||||
|
|
@ -197,7 +74,7 @@ class QueryBuilder
|
|||
public function addDuplicate(string $column, string $as): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_DUPLICATE,
|
||||
FluxPart::DUPLICATE,
|
||||
new Duplicate($column, $as)
|
||||
);
|
||||
return $this;
|
||||
|
|
@ -206,7 +83,7 @@ class QueryBuilder
|
|||
public function addMean(?string $column = ''): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_MEAN,
|
||||
FluxPart::MEAN,
|
||||
new Mean($column)
|
||||
);
|
||||
return $this;
|
||||
|
|
@ -215,7 +92,7 @@ class QueryBuilder
|
|||
public function addUnWindow(): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_UNWINDOW,
|
||||
FluxPart::UNWINDOW,
|
||||
new Window('inf')
|
||||
);
|
||||
return $this;
|
||||
|
|
@ -224,7 +101,7 @@ class QueryBuilder
|
|||
public function addAggregateWindow($every, $fn, array $options = []): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_AGGREGATEWINDOW,
|
||||
FluxPart::AGGREGATEWINDOW,
|
||||
new AggregateWindow($every, $fn, $options)
|
||||
);
|
||||
return $this;
|
||||
|
|
@ -233,42 +110,9 @@ class QueryBuilder
|
|||
public function addRawFunction(string $input): QueryBuilder
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_RAWFUNCTION,
|
||||
FluxPart::RAWFUNCTION,
|
||||
new RawFunction($input)
|
||||
);
|
||||
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!');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue