1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Datatables\Engines; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
6
|
|
|
use Illuminate\Database\Query\Builder; |
7
|
|
|
use Illuminate\Database\Query\Expression; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class QueryBuilderEngine. |
12
|
|
|
* |
13
|
|
|
* @package Yajra\Datatables\Engines |
14
|
|
|
* @author Arjay Angeles <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class QueryBuilderEngine extends BaseEngine |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Builder object. |
20
|
|
|
* |
21
|
|
|
* @var \Illuminate\Database\Query\Builder |
22
|
|
|
*/ |
23
|
|
|
protected $query; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Database connection used. |
27
|
|
|
* |
28
|
|
|
* @var \Illuminate\Database\Connection |
29
|
|
|
*/ |
30
|
|
|
protected $connection; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \Illuminate\Database\Query\Builder $builder |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Builder $builder) |
36
|
|
|
{ |
37
|
|
|
$this->query = $builder; |
38
|
|
|
$this->request = resolve('datatables.request'); |
39
|
|
|
$this->config = resolve('datatables.config'); |
40
|
|
|
$this->columns = $builder->columns; |
41
|
|
|
$this->connection = $builder->getConnection(); |
42
|
|
|
if ($this->config->isDebugging()) { |
43
|
|
|
$this->connection->enableQueryLog(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set auto filter off and run your own filter. |
49
|
|
|
* Overrides global search. |
50
|
|
|
* |
51
|
|
|
* @param callable $callback |
52
|
|
|
* @param bool $globalSearch |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function filter(callable $callback, $globalSearch = false) |
56
|
|
|
{ |
57
|
|
|
$this->overrideGlobalSearch($callback, $this->query, $globalSearch); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Organizes works. |
64
|
|
|
* |
65
|
|
|
* @param bool $mDataSupport |
66
|
|
|
* @return \Illuminate\Http\JsonResponse |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
|
|
public function make($mDataSupport = false) |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
|
|
$this->totalRecords = $this->totalCount(); |
73
|
|
|
|
74
|
|
|
if ($this->totalRecords) { |
75
|
|
|
$this->filterRecords(); |
76
|
|
|
$this->ordering(); |
77
|
|
|
$this->paginate(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$data = $this->transform($this->getProcessedData($mDataSupport)); |
81
|
|
|
|
82
|
|
|
return $this->render($data); |
83
|
|
|
} catch (\Exception $exception) { |
84
|
|
|
return $this->errorResponse($exception); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Count total items. |
90
|
|
|
* |
91
|
|
|
* @return integer |
92
|
|
|
*/ |
93
|
|
|
public function totalCount() |
94
|
|
|
{ |
95
|
|
|
return $this->totalRecords ? $this->totalRecords : $this->count(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Counts current query. |
100
|
|
|
* |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function count() |
104
|
|
|
{ |
105
|
|
|
$builder = $this->prepareCountQuery(); |
106
|
|
|
$table = $this->connection->raw('(' . $builder->toSql() . ') count_row_table'); |
107
|
|
|
|
108
|
|
|
return $this->connection->table($table) |
109
|
|
|
->setBindings($builder->getBindings()) |
110
|
|
|
->count(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Prepare count query builder. |
115
|
|
|
* |
116
|
|
|
* @return \Illuminate\Database\Query\Builder |
117
|
|
|
*/ |
118
|
|
|
protected function prepareCountQuery() |
119
|
|
|
{ |
120
|
|
|
$builder = clone $this->query; |
121
|
|
|
|
122
|
|
|
if ($this->isComplexQuery($builder)) { |
123
|
|
|
$row_count = $this->wrap('row_count'); |
124
|
|
|
$builder->select($this->connection->raw("'1' as {$row_count}")); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $builder; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Check if builder query uses complex sql. |
132
|
|
|
* |
133
|
|
|
* @param \Illuminate\Database\Query\Builder $builder |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
protected function isComplexQuery($builder) |
137
|
|
|
{ |
138
|
|
|
return !Str::contains(Str::lower($builder->toSql()), ['union', 'having', 'distinct', 'order by', 'group by']); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Wrap column with DB grammar. |
143
|
|
|
* |
144
|
|
|
* @param string $column |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
protected function wrap($column) |
148
|
|
|
{ |
149
|
|
|
return $this->connection->getQueryGrammar()->wrap($column); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Perform sorting of columns. |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function ordering() |
158
|
|
|
{ |
159
|
|
|
if ($this->orderCallback) { |
160
|
|
|
call_user_func($this->orderCallback, $this->query); |
161
|
|
|
|
162
|
|
|
return; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
collect($this->request->orderableColumns()) |
166
|
|
|
->map(function ($orderable) { |
167
|
|
|
$orderable['name'] = $this->getColumnName($orderable['column'], true); |
168
|
|
|
|
169
|
|
|
return $orderable; |
170
|
|
|
}) |
171
|
|
|
->reject(function ($orderable) { |
172
|
|
|
return $this->isBlacklisted($orderable['name']) && !$this->hasOrderColumn($orderable['name']); |
173
|
|
|
}) |
174
|
|
|
->each(function ($orderable) { |
175
|
|
|
$column = $this->resolveRelationColumn($orderable['name']); |
176
|
|
|
|
177
|
|
|
if ($this->hasOrderColumn($column)) { |
178
|
|
|
$this->applyOrderColumn($column, $orderable); |
179
|
|
|
} else { |
180
|
|
|
$nullsLastSql = $this->getNullsLastSql($column, $orderable['direction']); |
181
|
|
|
$normalSql = $this->wrap($column) . ' ' . $orderable['direction']; |
182
|
|
|
$sql = $this->nullsLast ? $nullsLastSql : $normalSql; |
183
|
|
|
$this->query->orderByRaw($sql); |
184
|
|
|
} |
185
|
|
|
}); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Check if column has custom sort handler. |
190
|
|
|
* |
191
|
|
|
* @param string $column |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
|
|
protected function hasOrderColumn($column) |
195
|
|
|
{ |
196
|
|
|
return isset($this->columnDef['order'][$column]); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Resolve the proper column name be used. |
201
|
|
|
* |
202
|
|
|
* @param string $column |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
protected function resolveRelationColumn($column) |
206
|
|
|
{ |
207
|
|
|
return $column; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Apply orderColumn custom query. |
212
|
|
|
* |
213
|
|
|
* @param string $column |
214
|
|
|
* @param array $orderable |
215
|
|
|
*/ |
216
|
|
|
protected function applyOrderColumn($column, $orderable): void |
217
|
|
|
{ |
218
|
|
|
$sql = $this->columnDef['order'][$column]['sql']; |
219
|
|
|
$sql = str_replace('$1', $orderable['direction'], $sql); |
220
|
|
|
$bindings = $this->columnDef['order'][$column]['bindings']; |
221
|
|
|
$this->query->orderByRaw($sql, $bindings); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get NULLS LAST SQL. |
226
|
|
|
* |
227
|
|
|
* @param string $column |
228
|
|
|
* @param string $direction |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
protected function getNullsLastSql($column, $direction) |
232
|
|
|
{ |
233
|
|
|
$sql = $this->config->get('datatables.nulls_last_sql', '%s %s NULLS LAST'); |
234
|
|
|
|
235
|
|
|
return sprintf($sql, $column, $direction); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Perform column search. |
240
|
|
|
* |
241
|
|
|
* @return void |
242
|
|
|
*/ |
243
|
|
|
public function columnSearch() |
244
|
|
|
{ |
245
|
|
|
$columns = $this->request->columns(); |
246
|
|
|
|
247
|
|
|
foreach ($columns as $index => $column) { |
248
|
|
|
if (!$this->request->isColumnSearchable($index)) { |
249
|
|
|
continue; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$column = $this->getColumnName($index); |
253
|
|
|
|
254
|
|
|
if ($this->hasFilterColumn($column)) { |
255
|
|
|
$keyword = $this->getColumnSearchKeyword($index, $raw = true); |
256
|
|
|
$this->applyFilterColumn($this->getBaseQueryBuilder(), $column, $keyword); |
257
|
|
|
continue; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$column = $this->resolveRelationColumn($column); |
261
|
|
|
$keyword = $this->getColumnSearchKeyword($index); |
262
|
|
|
$this->compileColumnSearch($index, $column, $keyword); |
263
|
|
|
|
264
|
|
|
$this->isFilterApplied = true; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Check if column has custom filter handler. |
270
|
|
|
* |
271
|
|
|
* @param string $columnName |
272
|
|
|
* @return bool |
273
|
|
|
*/ |
274
|
|
|
public function hasFilterColumn($columnName) |
275
|
|
|
{ |
276
|
|
|
return isset($this->columnDef['filter'][$columnName]); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get column keyword to use for search. |
281
|
|
|
* |
282
|
|
|
* @param int $i |
283
|
|
|
* @param bool $raw |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
protected function getColumnSearchKeyword($i, $raw = false) |
287
|
|
|
{ |
288
|
|
|
$keyword = $this->request->columnKeyword($i); |
289
|
|
|
if ($raw || $this->request->isRegex($i)) { |
290
|
|
|
return $keyword; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $this->setupKeyword($keyword); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Apply filterColumn api search. |
298
|
|
|
* |
299
|
|
|
* @param Builder $query |
300
|
|
|
* @param string $columnName |
301
|
|
|
* @param string $keyword |
302
|
|
|
* @param string $boolean |
303
|
|
|
*/ |
304
|
|
|
protected function applyFilterColumn(Builder $query, $columnName, $keyword, $boolean = 'and') |
305
|
|
|
{ |
306
|
|
|
$callback = $this->columnDef['filter'][$columnName]['method']; |
307
|
|
|
$builder = $query->newQuery(); |
308
|
|
|
$callback($builder, $keyword); |
309
|
|
|
$query->addNestedWhereQuery($builder, $boolean); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Get the base query builder instance. |
314
|
|
|
* |
315
|
|
|
* @param mixed $instance |
316
|
|
|
* @return \Illuminate\Database\Query\Builder |
317
|
|
|
*/ |
318
|
|
|
protected function getBaseQueryBuilder($instance = null) |
319
|
|
|
{ |
320
|
|
|
if (!$instance) { |
321
|
|
|
$instance = $this->query; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
if ($instance instanceof EloquentBuilder) { |
325
|
|
|
return $instance->getQuery(); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return $instance; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Compile queries for column search. |
333
|
|
|
* |
334
|
|
|
* @param int $i |
335
|
|
|
* @param string $column |
336
|
|
|
* @param string $keyword |
337
|
|
|
*/ |
338
|
|
|
protected function compileColumnSearch($i, $column, $keyword) |
339
|
|
|
{ |
340
|
|
|
if ($this->request->isRegex($i)) { |
341
|
|
|
$column = strstr($column, '(') ? $this->connection->raw($column) : $column; |
342
|
|
|
$this->regexColumnSearch($column, $keyword); |
343
|
|
|
} else { |
344
|
|
|
$this->compileQuerySearch($this->query, $column, $keyword, ''); |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Compile regex query column search. |
350
|
|
|
* |
351
|
|
|
* @param mixed $column |
352
|
|
|
* @param string $keyword |
353
|
|
|
*/ |
354
|
|
|
protected function regexColumnSearch($column, $keyword) |
355
|
|
|
{ |
356
|
|
|
switch ($this->connection->getDriverName()) { |
357
|
|
|
case 'oracle': |
358
|
|
|
$sql = !$this->config |
359
|
|
|
->isCaseInsensitive() ? 'REGEXP_LIKE( ' . $column . ' , ? )' : 'REGEXP_LIKE( LOWER(' . $column . ') , ?, \'i\' )'; |
360
|
|
|
break; |
361
|
|
|
|
362
|
|
|
case 'pgsql': |
363
|
|
|
$sql = !$this->config->isCaseInsensitive() ? $column . ' ~ ?' : $column . ' ~* ? '; |
364
|
|
|
break; |
365
|
|
|
|
366
|
|
|
default: |
367
|
|
|
$sql = !$this->config |
368
|
|
|
->isCaseInsensitive() ? $column . ' REGEXP ?' : 'LOWER(' . $column . ') REGEXP ?'; |
369
|
|
|
$keyword = Str::lower($keyword); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
$this->query->whereRaw($sql, [$keyword]); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Compile query builder where clause depending on configurations. |
377
|
|
|
* |
378
|
|
|
* @param mixed $query |
379
|
|
|
* @param string $column |
380
|
|
|
* @param string $keyword |
381
|
|
|
* @param string $relation |
382
|
|
|
*/ |
383
|
|
|
protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
384
|
|
|
{ |
385
|
|
|
$column = $this->addTablePrefix($query, $column); |
386
|
|
|
$column = $this->castColumn($column); |
387
|
|
|
$sql = $column . ' LIKE ?'; |
388
|
|
|
|
389
|
|
|
if ($this->config->isCaseInsensitive()) { |
390
|
|
|
$sql = 'LOWER(' . $column . ') LIKE ?'; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$query->{$relation . 'WhereRaw'}($sql, [$this->prepareKeyword($keyword)]); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Patch for fix about ambiguous field. |
398
|
|
|
* Ambiguous field error will appear when query use join table and search with keyword. |
399
|
|
|
* |
400
|
|
|
* @param mixed $query |
401
|
|
|
* @param string $column |
402
|
|
|
* @return string |
403
|
|
|
*/ |
404
|
|
|
protected function addTablePrefix($query, $column) |
405
|
|
|
{ |
406
|
|
|
if (strpos($column, '.') === false) { |
407
|
|
|
$q = $this->getBaseQueryBuilder($query); |
408
|
|
|
if (!$q->from instanceof Expression) { |
409
|
|
|
$column = $q->from . '.' . $column; |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return $this->wrap($column); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Wrap a column and cast based on database driver. |
418
|
|
|
* |
419
|
|
|
* @param string $column |
420
|
|
|
* @return string |
421
|
|
|
*/ |
422
|
|
|
protected function castColumn($column) |
423
|
|
|
{ |
424
|
|
|
switch ($this->connection->getDriverName()) { |
425
|
|
|
case 'pgsql': |
426
|
|
|
return 'CAST(' . $column . ' as TEXT)'; |
427
|
|
|
case 'firebird': |
428
|
|
|
return 'CAST(' . $column . ' as VARCHAR(255))'; |
429
|
|
|
default: |
430
|
|
|
return $column; |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Prepare search keyword based on configurations. |
436
|
|
|
* |
437
|
|
|
* @param string $keyword |
438
|
|
|
* @return string |
439
|
|
|
*/ |
440
|
|
|
protected function prepareKeyword($keyword) |
441
|
|
|
{ |
442
|
|
|
if ($this->config->isCaseInsensitive()) { |
443
|
|
|
$keyword = Str::lower($keyword); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
if ($this->config->isWildcard()) { |
447
|
|
|
$keyword = $this->wildcardLikeString($keyword); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
if ($this->config->isSmartSearch()) { |
451
|
|
|
$keyword = "%$keyword%"; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
return $keyword; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Perform pagination. |
459
|
|
|
* |
460
|
|
|
* @return void |
461
|
|
|
*/ |
462
|
|
|
public function paging() |
463
|
|
|
{ |
464
|
|
|
$this->query->skip($this->request->input('start')) |
465
|
|
|
->take((int) $this->request->input('length') > 0 ? $this->request->input('length') : 10); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Get paginated results. |
470
|
|
|
* |
471
|
|
|
* @return \Illuminate\Support\Collection |
472
|
|
|
*/ |
473
|
|
|
public function results() |
474
|
|
|
{ |
475
|
|
|
return $this->query->get(); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Add column in collection. |
480
|
|
|
* |
481
|
|
|
* @param string $name |
482
|
|
|
* @param string|callable $content |
483
|
|
|
* @param bool|int $order |
484
|
|
|
* @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
485
|
|
|
*/ |
486
|
|
|
public function addColumn($name, $content, $order = false) |
487
|
|
|
{ |
488
|
|
|
$this->pushToBlacklist($name); |
489
|
|
|
|
490
|
|
|
return parent::addColumn($name, $content, $order); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Get query builder instance. |
495
|
|
|
* |
496
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
497
|
|
|
*/ |
498
|
|
|
public function getQuery() |
499
|
|
|
{ |
500
|
|
|
return $this->query; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Perform global search for the given keyword. |
505
|
|
|
* |
506
|
|
|
* @param string $keyword |
507
|
|
|
*/ |
508
|
|
|
protected function globalSearch($keyword) |
509
|
|
|
{ |
510
|
|
|
$this->query->where(function ($query) use ($keyword) { |
511
|
|
|
$query = $this->getBaseQueryBuilder($query); |
512
|
|
|
|
513
|
|
|
collect($this->request->searchableColumnIndex()) |
514
|
|
|
->map(function ($index) { |
515
|
|
|
return $this->getColumnName($index); |
516
|
|
|
}) |
517
|
|
|
->reject(function ($column) { |
518
|
|
|
return $this->isBlacklisted($column) && !$this->hasFilterColumn($column); |
519
|
|
|
}) |
520
|
|
|
->each(function ($column) use ($keyword, $query) { |
521
|
|
|
if ($this->hasFilterColumn($column)) { |
522
|
|
|
$this->applyFilterColumn($query, $column, $keyword, 'or'); |
523
|
|
|
} else { |
524
|
|
|
$this->compileQuerySearch($query, $column, $keyword); |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
$this->isFilterApplied = true; |
528
|
|
|
}); |
529
|
|
|
}); |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Append debug parameters on output. |
534
|
|
|
* |
535
|
|
|
* @param array $output |
536
|
|
|
* @return array |
537
|
|
|
*/ |
538
|
|
|
protected function showDebugger(array $output) |
539
|
|
|
{ |
540
|
|
|
$output['queries'] = $this->connection->getQueryLog(); |
541
|
|
|
$output['input'] = $this->request->all(); |
542
|
|
|
|
543
|
|
|
return $output; |
544
|
|
|
} |
545
|
|
|
} |
546
|
|
|
|