-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryBuilder.php
277 lines (220 loc) · 7.66 KB
/
QueryBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
namespace Pandawa\Component\Eloquent;
use Illuminate\Contracts\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection as ModelCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Pandawa\Contracts\Eloquent\Cache\CacheHandlerInterface;
use Pandawa\Contracts\Eloquent\CriterionInterface;
use Pandawa\Contracts\Eloquent\QueryBuilderInterface;
/**
* @template TModel
* @implements QueryBuilderInterface<TModel>
*
* @author Iqbal Maulana <iq.bluejack@gmail.com>
*/
class QueryBuilder implements QueryBuilderInterface
{
protected Builder|EloquentBuilder $queryBuilder;
protected Model $model;
protected bool $cacheEnabled = false;
public function __construct(protected readonly ?CacheHandlerInterface $cacheHandler = null)
{
}
protected function isCacheEnabled(): bool
{
return true == $this->cacheEnabled && null !== $this->cacheHandler;
}
public function setModel(Model $model): static
{
$this->model = $model;
$this->queryBuilder = $this->model->newQuery();
return $this;
}
public function getModel(): Model
{
return $this->model;
}
public function enableCache(bool $enabled = true): static
{
$this->cacheEnabled = $enabled;
return $this;
}
public function withCriteria(CriterionInterface|array $criteria): static
{
if (is_array($criteria)) {
foreach ($criteria as $criterion) {
$this->withCriteria($criterion);
}
return $this;
}
$criteria->apply($this->queryBuilder);
return $this;
}
public function get(array $columns = ['*']): Collection|ModelCollection
{
if ($this->isCacheEnabled() && null !== $results = $this->cacheHandler?->getByQuery($this)) {
return tap($results, function () {
$this->refresh();
});
}
$query = $this;
return tap($this->queryBuilder->get($columns), function (Collection|ModelCollection $results) use ($query) {
if ($results->count() && $this->isCacheEnabled()) {
$this->cacheHandler->rememberQuery($query, $results);
}
$this->refresh();
});
}
public function findByKey(int|string $key): ?Model
{
if ($this->isCacheEnabled() && null !== $model = $this->cacheHandler?->getByKey($this->model, $key)) {
return tap($model, function () {
$this->refresh();
});
}
return tap($this->queryBuilder->whereKey($key)->first(), function (?Model $model) {
if ($model && $this->isCacheEnabled()) {
$this->cacheHandler?->rememberModel($model);
}
$this->refresh();
});
}
public function first(array $columns = ['*']): ?Model
{
if ($this->isCacheEnabled() && null !== $result = $this->cacheHandler?->getByQuery($this)) {
return tap($result, function () {
$this->refresh();
});
}
$query = $this;
return tap($this->queryBuilder->first($columns), function (?Model $model) use ($query) {
if (null !== $model && $this->isCacheEnabled()) {
$this->cacheHandler?->rememberQuery($query, $model);
}
$this->refresh();
});
}
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null): LengthAwarePaginator
{
if ($this->isCacheEnabled() && null !== $results = $this->cacheHandler?->getByQuery($this)) {
return tap($results, function () {
$this->refresh();
});
}
$query = $this;
return tap($this->queryBuilder->paginate($perPage, $columns, $pageName, $page), function (LengthAwarePaginator $page) use ($query) {
if ($this->isCacheEnabled()) {
$this->cacheHandler->rememberQuery($query, $page);
}
$this->refresh();
});
}
public function cursor(): LazyCollection
{
return tap($this->queryBuilder->cursor(), function () {
$this->refresh();
});
}
public function chunk($count, callable $callback): bool
{
return tap($this->queryBuilder->chunk($count, $callback), function () {
$this->refresh();
});
}
public function count($columns = '*'): int
{
if ($this->isCacheEnabled() && null !== $results = $this->cacheHandler?->getByQuery($this)) {
return tap($results, function () {
$this->refresh();
});
}
$query = $this;
return tap($this->queryBuilder->count($columns), function (int $count) use ($query) {
if ($this->isCacheEnabled()) {
$this->cacheHandler->rememberQuery($query, $count);
}
$this->refresh();
});
}
public function min($column): mixed
{
if ($this->isCacheEnabled() && null !== $results = $this->cacheHandler?->getByQuery($this)) {
return tap($results, function () {
$this->refresh();
});
}
$query = $this;
return tap($this->queryBuilder->min($column), function (mixed $min) use ($query) {
if ($this->isCacheEnabled()) {
$this->cacheHandler->rememberQuery($query, $min);
}
$this->refresh();
});
}
public function max($column): mixed
{
if ($this->isCacheEnabled() && null !== $results = $this->cacheHandler?->getByQuery($this)) {
return tap($results, function () {
$this->refresh();
});
}
$query = $this;
return tap($this->queryBuilder->max($column), function (mixed $max) use ($query) {
if ($this->isCacheEnabled()) {
$this->cacheHandler->rememberQuery($query, $max);
}
$this->refresh();
});
}
public function sum($column): mixed
{
if ($this->isCacheEnabled() && null !== $results = $this->cacheHandler?->getByQuery($this)) {
return tap($results, function () {
$this->refresh();
});
}
$query = $this;
return tap($this->queryBuilder->sum($column), function (mixed $sum) use ($query) {
if ($this->isCacheEnabled()) {
$this->cacheHandler->rememberQuery($query, $sum);
}
$this->refresh();
});
}
public function avg($column): mixed
{
if ($this->isCacheEnabled() && null !== $results = $this->cacheHandler?->getByQuery($this)) {
return tap($results, function () {
$this->refresh();
});
}
$query = $this;
return tap($this->queryBuilder->avg($column), function (mixed $avg) use ($query) {
if ($this->isCacheEnabled()) {
$this->cacheHandler->rememberQuery($query, $avg);
}
$this->refresh();
});
}
public function average($column): mixed
{
return $this->avg($column);
}
public function __call(string $method, array $args): mixed
{
$result = $this->queryBuilder->{$method}(...$args);
if ($result instanceof Builder) {
return $this;
}
return $result;
}
protected function refresh(): void
{
$this->setModel($this->model);
}
}