1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Datatables\Engines; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Database\Query\Builder; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Yajra\Datatables\Helper; |
9
|
|
|
use Yajra\Datatables\Request; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class QueryBuilderEngine. |
13
|
|
|
* |
14
|
|
|
* @package Yajra\Datatables\Engines |
15
|
|
|
* @author Arjay Angeles <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class QueryBuilderEngine extends BaseEngine |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param \Illuminate\Database\Query\Builder $builder |
21
|
|
|
* @param \Yajra\Datatables\Request $request |
22
|
|
|
*/ |
23
|
|
|
public function __construct(Builder $builder, Request $request) |
24
|
|
|
{ |
25
|
|
|
$this->query = $builder; |
26
|
|
|
$this->init($request, $builder); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initialize attributes. |
31
|
|
|
* |
32
|
|
|
* @param \Yajra\Datatables\Request $request |
33
|
|
|
* @param \Illuminate\Database\Query\Builder $builder |
34
|
|
|
* @param string $type |
35
|
|
|
*/ |
36
|
|
|
protected function init($request, $builder, $type = 'builder') |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$this->request = $request; |
39
|
|
|
$this->query_type = $type; |
40
|
|
|
$this->columns = $builder->columns; |
41
|
|
|
$this->connection = $builder->getConnection(); |
42
|
|
|
$this->prefix = $this->connection->getTablePrefix(); |
43
|
|
|
$this->database = $this->connection->getDriverName(); |
44
|
|
|
if ($this->isDebugging()) { |
45
|
|
|
$this->connection->enableQueryLog(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set auto filter off and run your own filter. |
51
|
|
|
* Overrides global search |
52
|
|
|
* |
53
|
|
|
* @param \Closure $callback |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function filter(Closure $callback) |
57
|
|
|
{ |
58
|
|
|
$this->overrideGlobalSearch($callback, $this->query); |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Organizes works |
65
|
|
|
* |
66
|
|
|
* @param bool $mDataSupport |
67
|
|
|
* @param bool $orderFirst |
68
|
|
|
* @return \Illuminate\Http\JsonResponse |
69
|
|
|
*/ |
70
|
|
|
public function make($mDataSupport = false, $orderFirst = false) |
71
|
|
|
{ |
72
|
|
|
return parent::make($mDataSupport, $orderFirst); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Count total items. |
77
|
|
|
* |
78
|
|
|
* @return integer |
79
|
|
|
*/ |
80
|
|
|
public function totalCount() |
81
|
|
|
{ |
82
|
|
|
return $this->count(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Counts current query. |
87
|
|
|
* |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
|
|
public function count() |
91
|
|
|
{ |
92
|
|
|
$myQuery = clone $this->query; |
93
|
|
|
// if its a normal query ( no union, having and distinct word ) |
94
|
|
|
// replace the select with static text to improve performance |
95
|
|
|
if (! Str::contains(Str::lower($myQuery->toSql()), ['union', 'having', 'distinct', 'order by', 'group by'])) { |
|
|
|
|
96
|
|
|
$row_count = $this->connection->getQueryGrammar()->wrap('row_count'); |
97
|
|
|
$myQuery->select($this->connection->raw("'1' as {$row_count}")); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->connection->table($this->connection->raw('(' . $myQuery->toSql() . ') count_row_table')) |
101
|
|
|
->setBindings($myQuery->getBindings())->count(); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Perform global search. |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function filtering() |
110
|
|
|
{ |
111
|
|
|
$this->query->where( |
112
|
|
|
function ($query) { |
113
|
|
|
$globalKeyword = $this->setupKeyword($this->request->keyword()); |
114
|
|
|
$queryBuilder = $this->getQueryBuilder($query); |
115
|
|
|
|
116
|
|
|
foreach ($this->request->searchableColumnIndex() as $index) { |
117
|
|
|
$columnName = $this->getColumnName($index); |
118
|
|
|
if ($this->isBlacklisted($columnName)) { |
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// check if custom column filtering is applied |
123
|
|
|
if (isset($this->columnDef['filter'][$columnName])) { |
124
|
|
|
$columnDef = $this->columnDef['filter'][$columnName]; |
125
|
|
|
// check if global search should be applied for the specific column |
126
|
|
|
$applyGlobalSearch = count($columnDef['parameters']) == 0 || end($columnDef['parameters']) !== false; |
127
|
|
|
if (! $applyGlobalSearch) { |
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($columnDef['method'] instanceof Closure) { |
132
|
|
|
$whereQuery = $queryBuilder->newQuery(); |
133
|
|
|
call_user_func_array($columnDef['method'], [$whereQuery, $this->request->keyword()]); |
134
|
|
|
$queryBuilder->addNestedWhereQuery($whereQuery, 'or'); |
135
|
|
|
} else { |
136
|
|
|
$this->compileColumnQuery( |
137
|
|
|
$queryBuilder, |
138
|
|
|
Helper::getOrMethod($columnDef['method']), |
139
|
|
|
$columnDef['parameters'], |
140
|
|
|
$columnName, |
141
|
|
|
$this->request->keyword() |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} else { |
145
|
|
|
if (count(explode('.', $columnName)) > 1) { |
146
|
|
|
$eagerLoads = $this->getEagerLoads(); |
147
|
|
|
$parts = explode('.', $columnName); |
148
|
|
|
$relationColumn = array_pop($parts); |
149
|
|
|
$relation = implode('.', $parts); |
150
|
|
|
if (in_array($relation, $eagerLoads)) { |
151
|
|
|
$this->compileRelationSearch( |
152
|
|
|
$queryBuilder, |
153
|
|
|
$relation, |
154
|
|
|
$relationColumn, |
155
|
|
|
$globalKeyword |
156
|
|
|
); |
157
|
|
|
} else { |
158
|
|
|
$this->compileGlobalSearch($queryBuilder, $columnName, $globalKeyword); |
159
|
|
|
} |
160
|
|
|
} else { |
161
|
|
|
$this->compileGlobalSearch($queryBuilder, $columnName, $globalKeyword); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->isFilterApplied = true; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Perform filter column on selected field. |
173
|
|
|
* |
174
|
|
|
* @param mixed $query |
175
|
|
|
* @param string|Closure $method |
176
|
|
|
* @param mixed $parameters |
177
|
|
|
* @param string $column |
178
|
|
|
* @param string $keyword |
179
|
|
|
*/ |
180
|
|
|
protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
181
|
|
|
{ |
182
|
|
|
if (method_exists($query, $method) |
183
|
|
|
&& count($parameters) <= with(new \ReflectionMethod($query, $method))->getNumberOfParameters() |
184
|
|
|
) { |
185
|
|
|
if (Str::contains(Str::lower($method), 'raw') |
|
|
|
|
186
|
|
|
|| Str::contains(Str::lower($method), 'exists') |
|
|
|
|
187
|
|
|
) { |
188
|
|
|
call_user_func_array( |
189
|
|
|
[$query, $method], |
190
|
|
|
$this->parameterize($parameters, $keyword) |
191
|
|
|
); |
192
|
|
|
} else { |
193
|
|
|
call_user_func_array( |
194
|
|
|
[$query, $method], |
195
|
|
|
$this->parameterize($column, $parameters, $keyword) |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Build Query Builder Parameters. |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
protected function parameterize() |
207
|
|
|
{ |
208
|
|
|
$args = func_get_args(); |
209
|
|
|
$keyword = count($args) > 2 ? $args[2] : $args[1]; |
210
|
|
|
$parameters = Helper::buildParameters($args); |
211
|
|
|
$parameters = Helper::replacePatternWithKeyword($parameters, $keyword, '$1'); |
212
|
|
|
|
213
|
|
|
return $parameters; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get eager loads keys if eloquent. |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
protected function getEagerLoads() |
222
|
|
|
{ |
223
|
|
|
if ($this->query_type == 'eloquent') { |
224
|
|
|
return array_keys($this->query->getEagerLoads()); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return []; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Add relation query on global search. |
232
|
|
|
* |
233
|
|
|
* @param mixed $query |
234
|
|
|
* @param string $relation |
235
|
|
|
* @param string $column |
236
|
|
|
* @param string $keyword |
237
|
|
|
*/ |
238
|
|
|
protected function compileRelationSearch($query, $relation, $column, $keyword) |
239
|
|
|
{ |
240
|
|
|
$myQuery = clone $this->query; |
241
|
|
|
$myQuery->orWhereHas($relation, function ($q) use ($column, $keyword, $query) { |
|
|
|
|
242
|
|
|
$sql = $q->select($this->connection->raw('count(1)')) |
243
|
|
|
->where($column, 'like', $keyword) |
244
|
|
|
->toSql(); |
245
|
|
|
$sql = "($sql) >= 1"; |
246
|
|
|
$query->orWhereRaw($sql, [$keyword]); |
247
|
|
|
}); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Add a query on global search. |
252
|
|
|
* |
253
|
|
|
* @param mixed $query |
254
|
|
|
* @param string $column |
255
|
|
|
* @param string $keyword |
256
|
|
|
*/ |
257
|
|
|
protected function compileGlobalSearch($query, $column, $keyword) |
258
|
|
|
{ |
259
|
|
|
if ($this->isSmartSearch()) { |
260
|
|
|
$column = $this->castColumn($column); |
261
|
|
|
$sql = $column . ' LIKE ?'; |
262
|
|
|
if ($this->isCaseInsensitive()) { |
263
|
|
|
$sql = 'LOWER(' . $column . ') LIKE ?'; |
264
|
|
|
$keyword = Str::lower($keyword); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
$query->orWhereRaw($sql, [$keyword]); |
268
|
|
|
} else { // exact match |
269
|
|
|
$query->orWhereRaw("$column like ?", [$keyword]); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Wrap a column and cast in pgsql. |
275
|
|
|
* |
276
|
|
|
* @param string $column |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
|
public function castColumn($column) |
280
|
|
|
{ |
281
|
|
|
$column = $this->connection->getQueryGrammar()->wrap($column); |
282
|
|
|
if ($this->database === 'pgsql') { |
283
|
|
|
$column = 'CAST(' . $column . ' as TEXT)'; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $column; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Perform column search. |
291
|
|
|
* |
292
|
|
|
* @return void |
293
|
|
|
*/ |
294
|
|
|
public function columnSearch() |
295
|
|
|
{ |
296
|
|
|
$columns = $this->request->get('columns', []); |
297
|
|
|
|
298
|
|
|
foreach ($columns as $index => $column) { |
299
|
|
|
if (! $this->request->isColumnSearchable($index)) { |
300
|
|
|
continue; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$column = $this->getColumnName($index); |
304
|
|
|
|
305
|
|
|
if (isset($this->columnDef['filter'][$column])) { |
306
|
|
|
$columnDef = $this->columnDef['filter'][$column]; |
307
|
|
|
// get a raw keyword (without wildcards) |
308
|
|
|
$keyword = $this->getSearchKeyword($index, true); |
309
|
|
|
$builder = $this->getQueryBuilder(); |
310
|
|
|
|
311
|
|
|
if ($columnDef['method'] instanceof Closure) { |
312
|
|
|
$whereQuery = $builder->newQuery(); |
313
|
|
|
call_user_func_array($columnDef['method'], [$whereQuery, $keyword]); |
314
|
|
|
$builder->addNestedWhereQuery($whereQuery); |
315
|
|
|
} else { |
316
|
|
|
$this->compileColumnQuery( |
317
|
|
|
$builder, |
318
|
|
|
$columnDef['method'], |
319
|
|
|
$columnDef['parameters'], |
320
|
|
|
$column, |
321
|
|
|
$keyword |
322
|
|
|
); |
323
|
|
|
} |
324
|
|
|
} else { |
325
|
|
View Code Duplication |
if (count(explode('.', $column)) > 1) { |
|
|
|
|
326
|
|
|
$eagerLoads = $this->getEagerLoads(); |
327
|
|
|
$parts = explode('.', $column); |
328
|
|
|
$relationColumn = array_pop($parts); |
329
|
|
|
$relation = implode('.', $parts); |
330
|
|
|
if (in_array($relation, $eagerLoads)) { |
331
|
|
|
$column = $this->joinEagerLoadedColumn($relation, $relationColumn); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
$column = $this->castColumn($column); |
336
|
|
|
$keyword = $this->getSearchKeyword($index); |
337
|
|
|
$caseInsensitive = $this->isCaseInsensitive(); |
338
|
|
|
|
339
|
|
|
if (! $caseInsensitive) { |
340
|
|
|
$column = strstr($column, '(') ? $this->connection->raw($column) : $column; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
$this->compileColumnSearch($index, $column, $keyword, $caseInsensitive); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
$this->isFilterApplied = true; |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get proper keyword to use for search. |
352
|
|
|
* |
353
|
|
|
* @param int $i |
354
|
|
|
* @param bool $raw |
355
|
|
|
* @return string |
356
|
|
|
*/ |
357
|
|
|
private function getSearchKeyword($i, $raw = false) |
358
|
|
|
{ |
359
|
|
|
$keyword = $this->request->columnKeyword($i); |
360
|
|
|
if ($raw || $this->request->isRegex($i)) { |
361
|
|
|
return $keyword; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
return $this->setupKeyword($keyword); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Compile queries for column search. |
369
|
|
|
* |
370
|
|
|
* @param int $i |
371
|
|
|
* @param mixed $column |
372
|
|
|
* @param string $keyword |
373
|
|
|
* @param bool $caseSensitive |
374
|
|
|
*/ |
375
|
|
|
protected function compileColumnSearch($i, $column, $keyword, $caseSensitive = true) |
376
|
|
|
{ |
377
|
|
|
if ($this->request->isRegex($i)) { |
378
|
|
|
$this->regexColumnSearch($column, $keyword, $caseSensitive); |
379
|
|
|
} elseif ($this->isSmartSearch()) { |
380
|
|
|
$sql = $caseSensitive ? $column . ' LIKE ?' : 'LOWER(' . $column . ') LIKE ?'; |
381
|
|
|
$keyword = $caseSensitive ? $keyword : Str::lower($keyword); |
382
|
|
|
$this->query->whereRaw($sql, [$keyword]); |
|
|
|
|
383
|
|
|
} else { // exact match |
384
|
|
|
$this->query->whereRaw("$column LIKE ?", [$keyword]); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Compile regex query column search. |
390
|
|
|
* |
391
|
|
|
* @param mixed $column |
392
|
|
|
* @param string $keyword |
393
|
|
|
* @param bool $caseSensitive |
394
|
|
|
*/ |
395
|
|
|
protected function regexColumnSearch($column, $keyword, $caseSensitive = true) |
396
|
|
|
{ |
397
|
|
|
if ($this->isOracleSql()) { |
398
|
|
|
$sql = $caseSensitive ? 'REGEXP_LIKE( ' . $column . ' , ? )' : 'REGEXP_LIKE( LOWER(' . $column . ') , ?, \'i\' )'; |
399
|
|
|
$this->query->whereRaw($sql, [$keyword]); |
|
|
|
|
400
|
|
|
} else { |
401
|
|
|
$sql = $caseSensitive ? $column . ' REGEXP ?' : 'LOWER(' . $column . ') REGEXP ?'; |
402
|
|
|
$this->query->whereRaw($sql, [Str::lower($keyword)]); |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Perform sorting of columns. |
408
|
|
|
* |
409
|
|
|
* @return void |
410
|
|
|
*/ |
411
|
|
|
public function ordering() |
412
|
|
|
{ |
413
|
|
|
if ($this->orderCallback) { |
414
|
|
|
call_user_func($this->orderCallback, $this->getQueryBuilder()); |
415
|
|
|
|
416
|
|
|
return; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
foreach ($this->request->orderableColumns() as $orderable) { |
420
|
|
|
$column = $this->getColumnName($orderable['column'], true); |
421
|
|
|
|
422
|
|
|
if ($this->isBlacklisted($column)) { |
423
|
|
|
continue; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
if (isset($this->columnDef['order'][$column])) { |
427
|
|
|
$method = $this->columnDef['order'][$column]['method']; |
428
|
|
|
$parameters = $this->columnDef['order'][$column]['parameters']; |
429
|
|
|
$this->compileColumnQuery( |
430
|
|
|
$this->getQueryBuilder(), |
431
|
|
|
$method, |
432
|
|
|
$parameters, |
433
|
|
|
$column, |
434
|
|
|
$orderable['direction'] |
435
|
|
|
); |
436
|
|
|
} else { |
437
|
|
View Code Duplication |
if (count(explode('.', $column)) > 1) { |
|
|
|
|
438
|
|
|
$eagerLoads = $this->getEagerLoads(); |
439
|
|
|
$parts = explode('.', $column); |
440
|
|
|
$relationColumn = array_pop($parts); |
441
|
|
|
$relation = implode('.', $parts); |
442
|
|
|
|
443
|
|
|
if (in_array($relation, $eagerLoads)) { |
444
|
|
|
$column = $this->joinEagerLoadedColumn($relation, $relationColumn); |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
$this->getQueryBuilder()->orderBy($column, $orderable['direction']); |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Join eager loaded relation and get the related column name. |
455
|
|
|
* |
456
|
|
|
* @param string $relation |
457
|
|
|
* @param string $relationColumn |
458
|
|
|
* @return string |
459
|
|
|
*/ |
460
|
|
|
protected function joinEagerLoadedColumn($relation, $relationColumn) |
461
|
|
|
{ |
462
|
|
|
$table = $this->query->getRelation($relation)->getRelated()->getTable(); |
|
|
|
|
463
|
|
|
$foreign = $this->query->getRelation($relation)->getQualifiedForeignKey(); |
464
|
|
|
$other = $this->query->getRelation($relation)->getQualifiedOtherKeyName(); |
465
|
|
|
$column = $table . '.' . $relationColumn; |
466
|
|
|
|
467
|
|
|
$joins = []; |
468
|
|
|
foreach ((array) $this->getQueryBuilder()->joins as $key => $join) { |
469
|
|
|
$joins[] = $join->table; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
if (! in_array($table, $joins)) { |
473
|
|
|
$this->getQueryBuilder() |
474
|
|
|
->leftJoin($table, $foreign, '=', $other); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
return $column; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Perform pagination |
482
|
|
|
* |
483
|
|
|
* @return void |
484
|
|
|
*/ |
485
|
|
|
public function paging() |
486
|
|
|
{ |
487
|
|
|
$this->query->skip($this->request['start']) |
|
|
|
|
488
|
|
|
->take((int) $this->request['length'] > 0 ? $this->request['length'] : 10); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Get results |
493
|
|
|
* |
494
|
|
|
* @return array|static[] |
495
|
|
|
*/ |
496
|
|
|
public function results() |
497
|
|
|
{ |
498
|
|
|
return $this->query->get(); |
499
|
|
|
} |
500
|
|
|
} |
501
|
|
|
|