From dab7077f955a0f87db07dfda0a3708f7db695c08 Mon Sep 17 00:00:00 2001 From: ngasparic Date: Tue, 13 Feb 2024 11:16:40 +0100 Subject: [PATCH 1/3] option for raw arguments 'use_raw_arguments' = 1 this way, we can make query with aggregate function(s), datetime functions, etc. --- src/JsonQuery.php | 15 ++++++++++----- src/RequestParameters/AbstractParameter.php | 14 ++++++++------ src/RequestParameters/GroupByParameter.php | 9 ++++++++- src/RequestParameters/ReturnsParameter.php | 9 ++++++++- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/JsonQuery.php b/src/JsonQuery.php index deeacd2..813e720 100644 --- a/src/JsonQuery.php +++ b/src/JsonQuery.php @@ -65,13 +65,18 @@ public function search(): void */ protected function appendParameterQueries(): void { + // enable raw arguments + // will use raw method rather then basic ones, in group_by and returns + // $builder->selectRaw, $builder->groupByRaw + $rawArguments = boolval($this->input['use_raw_arguments'] ?? false) ?? false; + foreach ($this->registeredParameters as $requestParameter) { if (!$this->parameterExists($requestParameter)) { // TODO: append config query? continue; } - $this->instantiateRequestParameter($requestParameter) + $this->instantiateRequestParameter($requestParameter, $rawArguments) ->run(); } } @@ -97,12 +102,12 @@ protected function parameterExists(string $requestParameter): bool } /** - * @param $requestParameter + * @param string $requestParameter + * @param bool|null $rawArguments * @return AbstractParameter - * * @throws JsonQueryBuilderException */ - protected function instantiateRequestParameter(string $requestParameter): AbstractParameter + protected function instantiateRequestParameter(string $requestParameter, ?bool $rawArguments = false): AbstractParameter { if (!is_subclass_of($requestParameter, AbstractParameter::class)) { throw new JsonQueryBuilderException("$requestParameter must extend " . AbstractParameter::class); @@ -110,7 +115,7 @@ protected function instantiateRequestParameter(string $requestParameter): Abstra $input = $this->wrapInput($requestParameter::getParameterName()); - return new $requestParameter($input, $this->builder, $this->modelConfig); + return new $requestParameter($input, $this->builder, $this->modelConfig, $rawArguments); } /** diff --git a/src/RequestParameters/AbstractParameter.php b/src/RequestParameters/AbstractParameter.php index eccd951..cc0d9e2 100644 --- a/src/RequestParameters/AbstractParameter.php +++ b/src/RequestParameters/AbstractParameter.php @@ -13,19 +13,21 @@ abstract class AbstractParameter public Builder $builder; public ModelConfig $modelConfig; protected array $arguments; + protected bool $rawArguments; /** - * AbstractParameter constructor. - * - * @param array $arguments - * @param Builder $builder - * @param ModelConfig $modelConfig + * AbstractParameter constructor + * @param array $arguments + * @param Builder $builder + * @param ModelConfig $modelConfig + * @param bool|null $rawArguents */ - public function __construct(array $arguments, Builder $builder, ModelConfig $modelConfig) + public function __construct(array $arguments, Builder $builder, ModelConfig $modelConfig, ?bool $rawArguments = false) { $this->arguments = $arguments; $this->builder = $builder; $this->modelConfig = $modelConfig; + $this->rawArguments = $rawArguments; } /** diff --git a/src/RequestParameters/GroupByParameter.php b/src/RequestParameters/GroupByParameter.php index 1214347..5f277de 100644 --- a/src/RequestParameters/GroupByParameter.php +++ b/src/RequestParameters/GroupByParameter.php @@ -13,6 +13,13 @@ public static function getParameterName(): string protected function appendQuery(): void { - $this->builder->groupBy($this->arguments); + if (!$this->rawArguments) { + $this->builder->groupBy($this->arguments); + } + else { + foreach($this->arguments as $arg) { + $this->builder->groupByRaw($arg); + } + } } } diff --git a/src/RequestParameters/ReturnsParameter.php b/src/RequestParameters/ReturnsParameter.php index 907e62a..a104e2c 100644 --- a/src/RequestParameters/ReturnsParameter.php +++ b/src/RequestParameters/ReturnsParameter.php @@ -13,6 +13,13 @@ public static function getParameterName(): string protected function appendQuery(): void { - $this->builder->addSelect($this->arguments); + if (!$this->rawArguments) { + $this->builder->addSelect($this->arguments); + } + else { + foreach($this->arguments as $arg) { + $this->builder->selectRaw( $arg ); + } + } } } From 3628c30a3d1c78b881cda0b18feb8830b32c987c Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 13 Feb 2024 10:16:57 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- src/JsonQuery.php | 5 +++-- src/RequestParameters/AbstractParameter.php | 11 ++++++----- src/RequestParameters/GroupByParameter.php | 5 ++--- src/RequestParameters/ReturnsParameter.php | 7 +++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/JsonQuery.php b/src/JsonQuery.php index 813e720..9a7fb6b 100644 --- a/src/JsonQuery.php +++ b/src/JsonQuery.php @@ -102,9 +102,10 @@ protected function parameterExists(string $requestParameter): bool } /** - * @param string $requestParameter - * @param bool|null $rawArguments + * @param string $requestParameter + * @param bool|null $rawArguments * @return AbstractParameter + * * @throws JsonQueryBuilderException */ protected function instantiateRequestParameter(string $requestParameter, ?bool $rawArguments = false): AbstractParameter diff --git a/src/RequestParameters/AbstractParameter.php b/src/RequestParameters/AbstractParameter.php index cc0d9e2..c6e7cc1 100644 --- a/src/RequestParameters/AbstractParameter.php +++ b/src/RequestParameters/AbstractParameter.php @@ -16,11 +16,12 @@ abstract class AbstractParameter protected bool $rawArguments; /** - * AbstractParameter constructor - * @param array $arguments - * @param Builder $builder - * @param ModelConfig $modelConfig - * @param bool|null $rawArguents + * AbstractParameter constructor. + * + * @param array $arguments + * @param Builder $builder + * @param ModelConfig $modelConfig + * @param bool|null $rawArguents */ public function __construct(array $arguments, Builder $builder, ModelConfig $modelConfig, ?bool $rawArguments = false) { diff --git a/src/RequestParameters/GroupByParameter.php b/src/RequestParameters/GroupByParameter.php index 5f277de..9d5f6e8 100644 --- a/src/RequestParameters/GroupByParameter.php +++ b/src/RequestParameters/GroupByParameter.php @@ -15,9 +15,8 @@ protected function appendQuery(): void { if (!$this->rawArguments) { $this->builder->groupBy($this->arguments); - } - else { - foreach($this->arguments as $arg) { + } else { + foreach ($this->arguments as $arg) { $this->builder->groupByRaw($arg); } } diff --git a/src/RequestParameters/ReturnsParameter.php b/src/RequestParameters/ReturnsParameter.php index a104e2c..191c5d4 100644 --- a/src/RequestParameters/ReturnsParameter.php +++ b/src/RequestParameters/ReturnsParameter.php @@ -15,10 +15,9 @@ protected function appendQuery(): void { if (!$this->rawArguments) { $this->builder->addSelect($this->arguments); - } - else { - foreach($this->arguments as $arg) { - $this->builder->selectRaw( $arg ); + } else { + foreach ($this->arguments as $arg) { + $this->builder->selectRaw($arg); } } } From fd49e7fe8a3022784f0308e0e654a7360195327f Mon Sep 17 00:00:00 2001 From: ngasparic Date: Wed, 14 Feb 2024 12:12:10 +0100 Subject: [PATCH 3/3] readme --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 3eb2cd7..9affa5e 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ be used together with ``limit`` parameter. - `count` - will return record count. - `soft_deleted` - will include soft deleted models in search results. - `doesnt_have_relations` - will only return entries that don't have any of the specified relations. +- `use_raw_arguments` - will use raw SQL for group_by (groupByRaw) & returns (selectRaw) ### Search @@ -116,6 +117,23 @@ case-insensitive search. Will perform a ``SELECT * FROM some_table WHERE first_name LIKE 'foo' AND last_name NOT LIKE 'bar'``. +Example with raw query +{ + "search": { + ... + }, + "group_by": ["year", "month"], + "order_by": ["year", "month"], + "returns": ["DATE_PART('Year', created_at) AS year", "DATE_PART('Month', created_at) AS month", "COUNT(*) AS total"], + "use_raw_arguments": 1 +} + +Will perform: +``select DATE_PART(\'Year\', created_at) AS year, DATE_PART(\'Month\', created_at) AS month, COUNT(*) AS total +from .... +group by year, month order by "year" asc, "month" asc`` + + #### Micro operators - `!` - negates the value. Works only on the beginning of the value (i.e. `!value`).