Improve validation
Signed-off-by: David Arendsen <darendsen@gamepoint.com>
This commit is contained in:
parent
3cff68870f
commit
602228a5e8
6 changed files with 53 additions and 44 deletions
|
|
@ -10,6 +10,7 @@ use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
||||||
use Arendsen\FluxQueryBuilder\Expression\KeyFilter;
|
use Arendsen\FluxQueryBuilder\Expression\KeyFilter;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\From;
|
use Arendsen\FluxQueryBuilder\Functions\From;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Measurement;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Range;
|
use Arendsen\FluxQueryBuilder\Functions\Range;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\RawFunction;
|
use Arendsen\FluxQueryBuilder\Functions\RawFunction;
|
||||||
|
|
||||||
|
|
@ -17,9 +18,7 @@ trait Basics
|
||||||
{
|
{
|
||||||
public function from(array $from): QueryBuilderInterface
|
public function from(array $from): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addRequiredData(QueryBuilder::REQUIRED_INPUT_FROM, $from);
|
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::FROM,
|
|
||||||
new From($from)
|
new From($from)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -27,14 +26,14 @@ trait Basics
|
||||||
|
|
||||||
public function fromBucket(string $bucket): QueryBuilderInterface
|
public function fromBucket(string $bucket): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->from(['bucket' => $bucket]);
|
return $this->from(['bucket' => $bucket]);
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fromMeasurement(string $measurement): QueryBuilderInterface
|
public function fromMeasurement(string $measurement): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addRequiredData(QueryBuilder::REQUIRED_INPUT_MEASUREMENT, $measurement);
|
$this->addToQuery(
|
||||||
$this->addKeyFilter(KeyFilter::setEqualTo('_measurement', $measurement));
|
new Measurement($measurement)
|
||||||
|
);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,7 +46,6 @@ trait Basics
|
||||||
public function addFilter(KeyValue $keyValue): QueryBuilderInterface
|
public function addFilter(KeyValue $keyValue): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::FILTERS,
|
|
||||||
new Filter($keyValue)
|
new Filter($keyValue)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -56,7 +54,6 @@ trait Basics
|
||||||
public function addKeyFilter(KeyFilter $keyFilter): QueryBuilderInterface
|
public function addKeyFilter(KeyFilter $keyFilter): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::FILTERS,
|
|
||||||
new Filter($keyFilter)
|
new Filter($keyFilter)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -65,7 +62,6 @@ trait Basics
|
||||||
public function addFieldFilter(array $fields): QueryBuilderInterface
|
public function addFieldFilter(array $fields): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::FILTERS,
|
|
||||||
new Filter($fields)
|
new Filter($fields)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -73,9 +69,7 @@ trait Basics
|
||||||
|
|
||||||
public function addRange(DateTime $start, ?DateTime $stop = null): QueryBuilderInterface
|
public function addRange(DateTime $start, ?DateTime $stop = null): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addRequiredData(QueryBuilder::REQUIRED_INPUT_RANGE, [$start, $stop]);
|
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::RANGE,
|
|
||||||
new Range($start, $stop)
|
new Range($start, $stop)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -96,7 +90,6 @@ trait Basics
|
||||||
public function addRawFunction(string $input): QueryBuilderInterface
|
public function addRawFunction(string $input): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::RAWFUNCTION,
|
|
||||||
new RawFunction($input)
|
new RawFunction($input)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ namespace Arendsen\FluxQueryBuilder\Builder;
|
||||||
class FluxPart
|
class FluxPart
|
||||||
{
|
{
|
||||||
public const FROM = 'from';
|
public const FROM = 'from';
|
||||||
|
public const MEASUREMENT = 'measurement';
|
||||||
public const RANGE = 'range';
|
public const RANGE = 'range';
|
||||||
public const FILTERS = 'filters';
|
public const FILTERS = 'filters';
|
||||||
public const REDUCE = 'reduce';
|
public const REDUCE = 'reduce';
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ trait Universe
|
||||||
public function addReduce(array $settings, array $identity): QueryBuilderInterface
|
public function addReduce(array $settings, array $identity): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::REDUCE,
|
|
||||||
new Reduce($settings, $identity)
|
new Reduce($settings, $identity)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -28,7 +27,6 @@ trait Universe
|
||||||
public function addSort(array $columns, $desc): QueryBuilderInterface
|
public function addSort(array $columns, $desc): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::SORT,
|
|
||||||
new Sort($columns, $desc)
|
new Sort($columns, $desc)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -37,7 +35,6 @@ trait Universe
|
||||||
public function addMap($query): QueryBuilderInterface
|
public function addMap($query): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::MAP,
|
|
||||||
new Map($query)
|
new Map($query)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -46,7 +43,6 @@ trait Universe
|
||||||
public function addGroup(array $columns, $mode = 'by'): QueryBuilderInterface
|
public function addGroup(array $columns, $mode = 'by'): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::GROUP,
|
|
||||||
new Group($columns, $mode)
|
new Group($columns, $mode)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -55,7 +51,6 @@ trait Universe
|
||||||
public function addLimit(int $limit, int $offset = 0): QueryBuilderInterface
|
public function addLimit(int $limit, int $offset = 0): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::LIMIT,
|
|
||||||
new Limit($limit, $offset)
|
new Limit($limit, $offset)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -64,7 +59,6 @@ trait Universe
|
||||||
public function addWindow($every, array $options = []): QueryBuilderInterface
|
public function addWindow($every, array $options = []): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::WINDOW,
|
|
||||||
new Window($every, $options)
|
new Window($every, $options)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -73,7 +67,6 @@ trait Universe
|
||||||
public function addDuplicate(string $column, string $as): QueryBuilderInterface
|
public function addDuplicate(string $column, string $as): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::DUPLICATE,
|
|
||||||
new Duplicate($column, $as)
|
new Duplicate($column, $as)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -82,7 +75,6 @@ trait Universe
|
||||||
public function addMean(?string $column = ''): QueryBuilderInterface
|
public function addMean(?string $column = ''): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::MEAN,
|
|
||||||
new Mean($column)
|
new Mean($column)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -91,7 +83,6 @@ trait Universe
|
||||||
public function addUnWindow(): QueryBuilderInterface
|
public function addUnWindow(): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::UNWINDOW,
|
|
||||||
new Window('inf')
|
new Window('inf')
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -100,7 +91,6 @@ trait Universe
|
||||||
public function addAggregateWindow($every, $fn, array $options = []): QueryBuilderInterface
|
public function addAggregateWindow($every, $fn, array $options = []): QueryBuilderInterface
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
FluxPart::AGGREGATEWINDOW,
|
|
||||||
new AggregateWindow($every, $fn, $options)
|
new AggregateWindow($every, $fn, $options)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,11 @@ class Limit extends Base
|
||||||
*/
|
*/
|
||||||
private $limit;
|
private $limit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int $offset
|
||||||
|
*/
|
||||||
|
private $offset;
|
||||||
|
|
||||||
public function __construct(int $limit, int $offset = 0)
|
public function __construct(int $limit, int $offset = 0)
|
||||||
{
|
{
|
||||||
$this->limit = $limit;
|
$this->limit = $limit;
|
||||||
|
|
|
||||||
21
src/Functions/Measurement.php
Normal file
21
src/Functions/Measurement.php
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Arendsen\FluxQueryBuilder\Functions;
|
||||||
|
|
||||||
|
class Measurement extends Base
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string $measurement
|
||||||
|
*/
|
||||||
|
private $measurement;
|
||||||
|
|
||||||
|
public function __construct(string $measurement)
|
||||||
|
{
|
||||||
|
$this->measurement = $measurement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
return '|> filter(fn: (r) => r._measurement == "' . (string)$this->measurement . '") ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,26 +6,23 @@ use Exception;
|
||||||
use Arendsen\FluxQueryBuilder\Builder\QueryBuilderInterface;
|
use Arendsen\FluxQueryBuilder\Builder\QueryBuilderInterface;
|
||||||
use Arendsen\FluxQueryBuilder\Builder\Basics;
|
use Arendsen\FluxQueryBuilder\Builder\Basics;
|
||||||
use Arendsen\FluxQueryBuilder\Builder\Universe;
|
use Arendsen\FluxQueryBuilder\Builder\Universe;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\From;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Measurement;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Range;
|
||||||
|
|
||||||
class QueryBuilder implements QueryBuilderInterface
|
class QueryBuilder implements QueryBuilderInterface
|
||||||
{
|
{
|
||||||
use Basics;
|
use Basics;
|
||||||
use Universe;
|
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
|
* Depends on Basics trait
|
||||||
*/
|
*/
|
||||||
private $currentFluxQueryPart = 0;
|
public const REQUIRED_FLUX_QUERY_PARTS = [
|
||||||
|
From::class,
|
||||||
|
Range::class,
|
||||||
|
Measurement::class,
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array $fluxQuery
|
* @var array $fluxQuery
|
||||||
|
|
@ -37,10 +34,15 @@ class QueryBuilder implements QueryBuilderInterface
|
||||||
*/
|
*/
|
||||||
private $requiredData = [];
|
private $requiredData = [];
|
||||||
|
|
||||||
protected function addToQuery($key, $query)
|
protected function addToQuery($query)
|
||||||
{
|
{
|
||||||
$this->fluxQueryParts[$this->currentFluxQueryPart] = $query;
|
$this->fluxQueryParts[] = $query;
|
||||||
$this->currentFluxQueryPart++;
|
|
||||||
|
foreach (self::REQUIRED_FLUX_QUERY_PARTS as $input) {
|
||||||
|
if ($query instanceof $input) {
|
||||||
|
$this->requiredData[] = $query;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function build(): string
|
public function build(): string
|
||||||
|
|
@ -56,15 +58,12 @@ class QueryBuilder implements QueryBuilderInterface
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function addRequiredData(string $key, $value)
|
|
||||||
{
|
|
||||||
$this->requiredData[][$key] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function checkRequired()
|
protected function checkRequired()
|
||||||
{
|
{
|
||||||
foreach (self::REQUIRED_INPUT as $key => $input) {
|
foreach (self::REQUIRED_FLUX_QUERY_PARTS as $key => $input) {
|
||||||
if (!isset($this->requiredData[$key][$input])) {
|
if (isset($this->requiredData[$key]) && !$this->requiredData[$key] instanceof $input) {
|
||||||
|
throw new Exception('You need to put the "' . $input . '" part of the query in the correct order!');
|
||||||
|
} elseif (!isset($this->requiredData[$key])) {
|
||||||
throw new Exception('You need to define the "' . $input . '" part of the query!');
|
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