Skip to content

Commit ef1b958

Browse files
author
Marko Dupor
committed
Micro optimizations + cache
1 parent 83d13ee commit ef1b958

10 files changed

+76
-50
lines changed

src/Config/ModelConfig.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
namespace Voice\JsonQueryBuilder\Config;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\Cache;
67
use Illuminate\Support\Facades\Config;
78
use Illuminate\Support\Facades\DB;
89
use Illuminate\Support\Facades\Schema;
910

1011
class ModelConfig
1112
{
13+
const CACHE_PREFIX = 'table_def_';
14+
const CACHE_TTL = 86400;
15+
1216
private Model $model;
1317
private array $config;
1418

@@ -98,7 +102,6 @@ protected function getForbiddenColumns(array $forbiddenKeys): array
98102
return $forbiddenKeys;
99103
}
100104

101-
102105
/**
103106
* Will return column and column type array for a calling model.
104107
* Column types will equal Eloquent column types
@@ -108,14 +111,20 @@ protected function getForbiddenColumns(array $forbiddenKeys): array
108111
public function getModelColumns(): array
109112
{
110113
$table = $this->model->getTable();
111-
$columns = Schema::getColumnListing($table);
112114

115+
if (Cache::has(self::CACHE_PREFIX . $table)) {
116+
return Cache::get(self::CACHE_PREFIX . $table);
117+
}
118+
119+
$columns = Schema::getColumnListing($table);
113120
$modelColumns = [];
114121

115122
foreach ($columns as $column) {
116123
$modelColumns[$column] = DB::getSchemaBuilder()->getColumnType($table, $column);
117124
}
118125

126+
Cache::put(self::CACHE_PREFIX . $table, $modelColumns, self::CACHE_TTL);
127+
119128
return $modelColumns;
120129
}
121130
}

src/JsonQuery.php

+51-16
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44

55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Support\Arr;
7-
use Illuminate\Support\Facades\Log;
87
use Voice\JsonQueryBuilder\Config\ModelConfig;
98
use Voice\JsonQueryBuilder\Config\RequestParametersConfig;
109
use Voice\JsonQueryBuilder\Exceptions\SearchException;
1110
use Voice\JsonQueryBuilder\RequestParameters\AbstractParameter;
1211

1312
class JsonQuery
1413
{
15-
protected Builder $builder;
16-
protected array $input;
17-
protected ModelConfig $modelConfig;
18-
protected RequestParametersConfig $requestParametersConfig;
14+
protected Builder $builder;
15+
protected array $input;
16+
protected ModelConfig $modelConfig;
17+
protected array $registeredParameters;
1918

2019
/*
2120
* TODO: datum od danas toliko dana
@@ -33,12 +32,20 @@ public function __construct(Builder $builder, array $input)
3332
$this->builder = $builder;
3433
$this->input = $input;
3534

35+
$this->forbidForExistingModels();
36+
37+
$this->modelConfig = new ModelConfig($this->builder->getModel());
38+
$this->registeredParameters = (new RequestParametersConfig())->registered;
39+
}
40+
41+
/**
42+
* @throws SearchException
43+
*/
44+
protected function forbidForExistingModels(): void
45+
{
3646
if ($this->builder->getModel()->exists) {
3747
throw new SearchException("[Search] Searching is not allowed on already loaded models.");
3848
}
39-
40-
$this->modelConfig = new ModelConfig($builder->getModel());
41-
$this->requestParametersConfig = new RequestParametersConfig();
4249
}
4350

4451
/**
@@ -50,7 +57,6 @@ public function search(): void
5057
{
5158
$this->appendParameterQueries();
5259
$this->appendConfigQueries();
53-
Log::info('[Search] SQL: ' . $this->builder->toSql() . " Bindings: " . implode(', ', $this->builder->getBindings()));
5460
}
5561

5662
/**
@@ -60,14 +66,15 @@ public function search(): void
6066
*/
6167
protected function appendParameterQueries(): void
6268
{
63-
foreach ($this->requestParametersConfig->registered as $requestParameter) {
64-
$requestParameter = $this->instantiateRequestParameter($requestParameter);
69+
foreach ($this->registeredParameters as $requestParameter) {
6570

66-
if (!($this->parameterExists($requestParameter))) {
71+
if (!$this->parameterExists($requestParameter)) {
72+
// append config query?
6773
continue;
6874
}
6975

70-
$requestParameter->run();
76+
$this->instantiateRequestParameter($requestParameter)
77+
->run();
7178
}
7279
}
7380

@@ -79,13 +86,41 @@ protected function appendConfigQueries(): void
7986
// TODO: implement...or not
8087
}
8188

89+
/**
90+
* @param string $requestParameter
91+
* @return bool
92+
*/
93+
protected function parameterExists(string $requestParameter): bool
94+
{
95+
/**
96+
* @var AbstractParameter $requestParameter
97+
*/
98+
return Arr::has($this->input, $requestParameter::getParameterName());
99+
}
100+
101+
/**
102+
* @param $requestParameter
103+
* @return AbstractParameter
104+
*/
82105
protected function instantiateRequestParameter($requestParameter): AbstractParameter
83106
{
84-
return new $requestParameter($this->input, $this->builder, $this->modelConfig);
107+
/**
108+
* @var AbstractParameter $requestParameter
109+
*/
110+
$input = $this->wrapInput($requestParameter::getParameterName());
111+
return new $requestParameter($input, $this->builder, $this->modelConfig);
85112
}
86113

87-
protected function parameterExists(AbstractParameter $requestParameter): bool
114+
/**
115+
* Get input for given parameter name and wrap it as an array if it's not already an array.
116+
*
117+
* @param string $parameterName
118+
* @return array
119+
*/
120+
protected function wrapInput(string $parameterName): array
88121
{
89-
return Arr::has($this->input, $requestParameter->getParameterName());
122+
return Arr::wrap(
123+
Arr::get($this->input, $parameterName)
124+
);
90125
}
91126
}

src/RequestParameters/AbstractParameter.php

+4-21
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,25 @@
33
namespace Voice\JsonQueryBuilder\RequestParameters;
44

55
use Illuminate\Database\Eloquent\Builder;
6-
use Illuminate\Support\Arr;
76
use Voice\JsonQueryBuilder\Config\ModelConfig;
87
use Voice\JsonQueryBuilder\Exceptions\SearchException;
98

109
abstract class AbstractParameter
1110
{
12-
public array $input;
1311
public Builder $builder;
1412
public ModelConfig $modelConfig;
1513
protected array $arguments;
1614

1715
/**
1816
* AbstractParameter constructor.
19-
* @param array $input
17+
* @param array $arguments
2018
* @param Builder $builder
2119
* @param ModelConfig $modelConfig
2220
* @throws SearchException
2321
*/
24-
public function __construct(array $input, Builder $builder, ModelConfig $modelConfig)
22+
public function __construct(array $arguments, Builder $builder, ModelConfig $modelConfig)
2523
{
26-
$this->input = $input;
24+
$this->arguments = $arguments;
2725
$this->builder = $builder;
2826
$this->modelConfig = $modelConfig;
2927
}
@@ -32,7 +30,7 @@ public function __construct(array $input, Builder $builder, ModelConfig $modelCo
3230
* JSON key by which the parameter will be recognized.
3331
* @return string
3432
*/
35-
abstract public function getParameterName(): string;
33+
abstract public static function getParameterName(): string;
3634

3735
/**
3836
* Append the query to Eloquent builder
@@ -45,25 +43,10 @@ abstract public function appendQuery(): void;
4543
*/
4644
public function run()
4745
{
48-
$this->arguments = $this->getArguments();
4946
$this->areArgumentsValid();
50-
5147
$this->appendQuery();
5248
}
5349

54-
/**
55-
* Return values for given JSON key (parameter name)
56-
*
57-
* @return array
58-
* @throws SearchException
59-
*/
60-
protected function getArguments(): array
61-
{
62-
$rawInput = Arr::get($this->input, $this->getParameterName());
63-
64-
return Arr::wrap($rawInput);
65-
}
66-
6750
/**
6851
* Check validity of fetched arguments and throw exception if it fails
6952
* @throws SearchException

src/RequestParameters/CountParameter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class CountParameter extends AbstractParameter
99
{
10-
public function getParameterName(): string
10+
public static function getParameterName(): string
1111
{
1212
return 'count';
1313
}

src/RequestParameters/LimitParameter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class LimitParameter extends AbstractParameter
88
{
9-
public function getParameterName(): string
9+
public static function getParameterName(): string
1010
{
1111
return 'limit';
1212
}
@@ -20,7 +20,7 @@ public function areArgumentsValid(): void
2020

2121
public function appendQuery(): void
2222
{
23-
$this->builder->limit($this->getArguments()[0]);
23+
$this->builder->limit($this->arguments[0]);
2424
}
2525

2626

src/RequestParameters/OffsetParameter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class OffsetParameter extends AbstractParameter
88
{
9-
public function getParameterName(): string
9+
public static function getParameterName(): string
1010
{
1111
return 'offset';
1212
}

src/RequestParameters/OrderByParameter.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44

55
class OrderByParameter extends AbstractParameter
66
{
7-
public function getParameterName(): string
7+
public static function getParameterName(): string
88
{
99
return 'order-by';
1010
}
1111

1212
public function appendQuery(): void
1313
{
14-
$arguments = $this->getArguments();
15-
16-
foreach ($arguments as $column => $direction) {
14+
foreach ($this->arguments as $column => $direction) {
1715
$this->appendSingle($column, $direction);
1816
}
1917
}

src/RequestParameters/RelationsParameter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class RelationsParameter extends AbstractParameter
66
{
7-
public function getParameterName(): string
7+
public static function getParameterName(): string
88
{
99
return 'relations';
1010
}

src/RequestParameters/ReturnsParameter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class ReturnsParameter extends AbstractParameter
66
{
7-
public function getParameterName(): string
7+
public static function getParameterName(): string
88
{
99
return 'returns';
1010
}

src/RequestParameters/SearchParameter.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class SearchParameter extends AbstractParameter
1111
{
12-
public function getParameterName(): string
12+
public static function getParameterName(): string
1313
{
1414
return 'search';
1515
}
@@ -22,6 +22,7 @@ public function appendQuery(): void
2222
$this->builder->where(function () use ($arguments, $operatorsConfig) {
2323
foreach ($arguments as $column => $argument) {
2424
$searchModel = new Search($this->modelConfig, $operatorsConfig, $column, $argument);
25+
// TODO: register to prevent multiple init?
2526
$this->appendSingle($operatorsConfig, $searchModel);
2627
}
2728
});

0 commit comments

Comments
 (0)