Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable raw arguments in query #51

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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`).
Expand Down
14 changes: 10 additions & 4 deletions src/JsonQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -97,20 +102,21 @@ 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);
}

$input = $this->wrapInput($requestParameter::getParameterName());

return new $requestParameter($input, $this->builder, $this->modelConfig);
return new $requestParameter($input, $this->builder, $this->modelConfig, $rawArguments);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/RequestParameters/AbstractParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ 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
* @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;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/RequestParameters/GroupByParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ 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);
}
}
}
}
8 changes: 7 additions & 1 deletion src/RequestParameters/ReturnsParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ 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);
}
}
}
}
Loading