Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QueryBuilderEngine often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryBuilderEngine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class QueryBuilderEngine extends BaseEngine |
||
24 | { |
||
25 | /** |
||
26 | * @param \Illuminate\Database\Query\Builder $builder |
||
27 | * @param \Yajra\Datatables\Request $request |
||
28 | */ |
||
29 | public function __construct(Builder $builder, Request $request) |
||
30 | { |
||
31 | $this->query = $builder; |
||
32 | $this->init($request, $builder); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Initialize attributes. |
||
37 | * |
||
38 | * @param \Yajra\Datatables\Request $request |
||
39 | * @param \Illuminate\Database\Query\Builder $builder |
||
40 | * @param string $type |
||
41 | */ |
||
42 | protected function init($request, $builder, $type = 'builder') |
||
|
|||
43 | { |
||
44 | $this->request = $request; |
||
45 | $this->query_type = $type; |
||
46 | $this->columns = $builder->columns; |
||
47 | $this->connection = $builder->getConnection(); |
||
48 | $this->prefix = $this->connection->getTablePrefix(); |
||
49 | $this->database = $this->connection->getDriverName(); |
||
50 | if ($this->isDebugging()) { |
||
51 | $this->connection->enableQueryLog(); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Set auto filter off and run your own filter. |
||
57 | * Overrides global search |
||
58 | * |
||
59 | * @param \Closure $callback |
||
60 | * @param bool $globalSearch |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function filter(Closure $callback, $globalSearch = false) |
||
64 | { |
||
65 | $this->overrideGlobalSearch($callback, $this->query, $globalSearch); |
||
66 | |||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Organizes works |
||
72 | * |
||
73 | * @param bool $mDataSupport |
||
74 | * @param bool $orderFirst |
||
75 | * @return \Illuminate\Http\JsonResponse |
||
76 | */ |
||
77 | public function make($mDataSupport = false, $orderFirst = false) |
||
78 | { |
||
79 | return parent::make($mDataSupport, $orderFirst); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Count total items. |
||
84 | * |
||
85 | * @return integer |
||
86 | */ |
||
87 | public function totalCount() |
||
88 | { |
||
89 | return $this->totalRecords ? $this->totalRecords : $this->count(); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Counts current query. |
||
94 | * |
||
95 | * @return int |
||
96 | */ |
||
97 | public function count() |
||
98 | { |
||
99 | $myQuery = clone $this->query; |
||
100 | // if its a normal query ( no union, having and distinct word ) |
||
101 | // replace the select with static text to improve performance |
||
102 | View Code Duplication | if (! Str::contains(Str::lower($myQuery->toSql()), ['union', 'having', 'distinct', 'order by', 'group by'])) { |
|
103 | $row_count = $this->wrap('row_count'); |
||
104 | $myQuery->select($this->connection->raw("'1' as {$row_count}")); |
||
105 | } |
||
106 | |||
107 | return $this->connection->table($this->connection->raw('(' . $myQuery->toSql() . ') count_row_table')) |
||
108 | ->setBindings($myQuery->getBindings())->count(); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Wrap column with DB grammar. |
||
113 | * |
||
114 | * @param string $column |
||
115 | * @return string |
||
116 | */ |
||
117 | protected function wrap($column) |
||
118 | { |
||
119 | return $this->connection->getQueryGrammar()->wrap($column); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Perform global search. |
||
124 | * |
||
125 | * @return void |
||
126 | */ |
||
127 | public function filtering() |
||
128 | { |
||
129 | $this->query->where( |
||
130 | function ($query) { |
||
131 | $globalKeyword = $this->request->keyword(); |
||
132 | $queryBuilder = $this->getQueryBuilder($query); |
||
133 | |||
134 | foreach ($this->request->searchableColumnIndex() as $index) { |
||
135 | $columnName = $this->getColumnName($index); |
||
136 | if ($this->isBlacklisted($columnName)) { |
||
137 | continue; |
||
138 | } |
||
139 | |||
140 | // check if custom column filtering is applied |
||
141 | if (isset($this->columnDef['filter'][$columnName])) { |
||
142 | $columnDef = $this->columnDef['filter'][$columnName]; |
||
143 | // check if global search should be applied for the specific column |
||
144 | $applyGlobalSearch = count($columnDef['parameters']) == 0 || end($columnDef['parameters']) !== false; |
||
145 | if (! $applyGlobalSearch) { |
||
146 | continue; |
||
147 | } |
||
148 | |||
149 | View Code Duplication | if ($columnDef['method'] instanceof Closure) { |
|
150 | $whereQuery = $queryBuilder->newQuery(); |
||
151 | call_user_func_array($columnDef['method'], [$whereQuery, $globalKeyword]); |
||
152 | $queryBuilder->addNestedWhereQuery($whereQuery, 'or'); |
||
153 | } else { |
||
154 | $this->compileColumnQuery( |
||
155 | $queryBuilder, |
||
156 | Helper::getOrMethod($columnDef['method']), |
||
157 | $columnDef['parameters'], |
||
158 | $columnName, |
||
159 | $globalKeyword |
||
160 | ); |
||
161 | } |
||
162 | View Code Duplication | } else { |
|
163 | if (count(explode('.', $columnName)) > 1) { |
||
164 | $eagerLoads = $this->getEagerLoads(); |
||
165 | $parts = explode('.', $columnName); |
||
166 | $relationColumn = array_pop($parts); |
||
167 | $relation = implode('.', $parts); |
||
168 | if (in_array($relation, $eagerLoads)) { |
||
169 | $this->compileRelationSearch( |
||
170 | $queryBuilder, |
||
171 | $relation, |
||
172 | $relationColumn, |
||
173 | $globalKeyword |
||
174 | ); |
||
175 | } else { |
||
176 | $this->compileQuerySearch($queryBuilder, $columnName, $globalKeyword); |
||
177 | } |
||
178 | } else { |
||
179 | $this->compileQuerySearch($queryBuilder, $columnName, $globalKeyword); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | $this->isFilterApplied = true; |
||
184 | } |
||
185 | } |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Perform filter column on selected field. |
||
191 | * |
||
192 | * @param mixed $query |
||
193 | * @param string|Closure $method |
||
194 | * @param mixed $parameters |
||
195 | * @param string $column |
||
196 | * @param string $keyword |
||
197 | */ |
||
198 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
199 | { |
||
200 | if (method_exists($query, $method) |
||
201 | && count($parameters) <= with(new \ReflectionMethod($query, $method))->getNumberOfParameters() |
||
202 | ) { |
||
203 | if (Str::contains(Str::lower($method), 'raw') |
||
204 | || Str::contains(Str::lower($method), 'exists') |
||
205 | ) { |
||
206 | call_user_func_array( |
||
207 | [$query, $method], |
||
208 | $this->parameterize($parameters, $keyword) |
||
209 | ); |
||
210 | } else { |
||
211 | call_user_func_array( |
||
212 | [$query, $method], |
||
213 | $this->parameterize($column, $parameters, $keyword) |
||
214 | ); |
||
215 | } |
||
216 | } |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Build Query Builder Parameters. |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | protected function parameterize() |
||
225 | { |
||
226 | $args = func_get_args(); |
||
227 | $keyword = count($args) > 2 ? $args[2] : $args[1]; |
||
228 | $parameters = Helper::buildParameters($args); |
||
229 | $parameters = Helper::replacePatternWithKeyword($parameters, $keyword, '$1'); |
||
230 | |||
231 | return $parameters; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Get eager loads keys if eloquent. |
||
236 | * |
||
237 | * @return array |
||
238 | */ |
||
239 | protected function getEagerLoads() |
||
240 | { |
||
241 | if ($this->query_type == 'eloquent') { |
||
242 | return array_keys($this->query->getEagerLoads()); |
||
243 | } |
||
244 | |||
245 | return []; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Add relation query on global search. |
||
250 | * |
||
251 | * @param mixed $query |
||
252 | * @param string $relation |
||
253 | * @param string $column |
||
254 | * @param string $keyword |
||
255 | */ |
||
256 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
257 | { |
||
258 | $myQuery = clone $this->query; |
||
259 | |||
260 | /** |
||
261 | * For compile nested relation, we need store all nested relation as array |
||
262 | * and reverse order to apply where query. |
||
263 | * With this method we can create nested sub query with properly relation. |
||
264 | */ |
||
265 | |||
266 | /** |
||
267 | * Store all relation data that require in next step |
||
268 | */ |
||
269 | $relationChunk = []; |
||
270 | |||
271 | /** |
||
272 | * Store last eloquent query builder for get next relation. |
||
273 | */ |
||
274 | $lastQuery = $query; |
||
275 | |||
276 | $relations = explode('.', $relation); |
||
277 | $lastRelation = end($relations); |
||
278 | foreach ($relations as $relation) { |
||
279 | $relationType = $myQuery->getModel()->{$relation}(); |
||
280 | $myQuery->orWhereHas($relation, function ($builder) use ( |
||
281 | $column, |
||
282 | $keyword, |
||
283 | $query, |
||
284 | $relationType, |
||
285 | $relation, |
||
286 | $lastRelation, |
||
287 | &$relationChunk, |
||
288 | &$lastQuery |
||
289 | ) { |
||
290 | $builder->select($this->connection->raw('count(1)')); |
||
291 | |||
292 | // We will perform search on last relation only. |
||
293 | if ($relation == $lastRelation) { |
||
294 | $this->compileQuerySearch($builder, $column, $keyword, ''); |
||
295 | } |
||
296 | |||
297 | // Put require object to next step!! |
||
298 | $relationChunk[$relation] = [ |
||
299 | 'builder' => $builder, |
||
300 | 'relationType' => $relationType, |
||
301 | 'query' => $lastQuery, |
||
302 | ]; |
||
303 | |||
304 | // This is trick make sub query. |
||
305 | $lastQuery = $builder; |
||
306 | }); |
||
307 | |||
308 | // This is trick to make nested relation by pass previous relation to be next query eloquent builder |
||
309 | $myQuery = $relationType; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Reverse them all |
||
314 | */ |
||
315 | $relationChunk = array_reverse($relationChunk, true); |
||
316 | |||
317 | /** |
||
318 | * Create valuable for use in check last relation |
||
319 | */ |
||
320 | end($relationChunk); |
||
321 | $lastRelation = key($relationChunk); |
||
322 | reset($relationChunk); |
||
323 | |||
324 | /** |
||
325 | * Walking ... |
||
326 | */ |
||
327 | foreach ($relationChunk as $relation => $chunk) { |
||
328 | // Prepare variables |
||
329 | $builder = $chunk['builder']; |
||
330 | $relationType = $chunk['relationType']; |
||
331 | $query = $chunk['query']; |
||
332 | $builder = "({$builder->toSql()}) >= 1"; |
||
333 | |||
334 | // Check if it last relation we will use orWhereRaw |
||
335 | if ($lastRelation == $relation) { |
||
336 | $relationMethod = "orWhereRaw"; |
||
337 | } else { |
||
338 | // For case parent relation of nested relation. |
||
339 | // We must use and for properly query and get correct result |
||
340 | $relationMethod = "whereRaw"; |
||
341 | } |
||
342 | |||
343 | if ($relationType instanceof MorphToMany) { |
||
344 | $query->{$relationMethod}($builder, [$relationType->getMorphClass(), $this->prepareKeyword($keyword)]); |
||
345 | } else { |
||
346 | $query->{$relationMethod}($builder, [$this->prepareKeyword($keyword)]); |
||
347 | } |
||
348 | } |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Compile query builder where clause depending on configurations. |
||
353 | * |
||
354 | * @param mixed $query |
||
355 | * @param string $column |
||
356 | * @param string $keyword |
||
357 | * @param string $relation |
||
358 | */ |
||
359 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
360 | { |
||
361 | $column = $this->addTablePrefix($query, $column); |
||
362 | $column = $this->castColumn($column); |
||
363 | $sql = $column . ' LIKE ?'; |
||
364 | |||
365 | if ($this->isCaseInsensitive()) { |
||
366 | $sql = 'LOWER(' . $column . ') LIKE ?'; |
||
367 | } |
||
368 | |||
369 | $query->{$relation . 'WhereRaw'}($sql, [$this->prepareKeyword($keyword)]); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Patch for fix about ambiguous field. |
||
374 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
375 | * |
||
376 | * @param mixed $query |
||
377 | * @param string $column |
||
378 | * @return string |
||
379 | */ |
||
380 | protected function addTablePrefix($query, $column) |
||
381 | { |
||
382 | // Check if field does not have a table prefix |
||
383 | if (strpos($column, '.') === false) { |
||
384 | // Alternative method to check instanceof \Illuminate\Database\Eloquent\Builder |
||
385 | if (method_exists($query, 'getQuery')) { |
||
386 | $q = $query->getQuery(); |
||
387 | } else { |
||
388 | $q = $query; |
||
389 | } |
||
390 | |||
391 | if (! $q->from instanceof Expression) { |
||
392 | // Get table from query and add it. |
||
393 | $column = $q->from . '.' . $column; |
||
394 | } |
||
395 | } |
||
396 | |||
397 | return $this->wrap($column); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Wrap a column and cast in pgsql. |
||
402 | * |
||
403 | * @param string $column |
||
404 | * @return string |
||
405 | */ |
||
406 | protected function castColumn($column) |
||
407 | { |
||
408 | if ($this->database === 'pgsql') { |
||
409 | $column = 'CAST(' . $column . ' as TEXT)'; |
||
410 | } elseif ($this->database === 'firebird') { |
||
411 | $column = 'CAST(' . $column . ' as VARCHAR(255))'; |
||
412 | } |
||
413 | |||
414 | return $column; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Prepare search keyword based on configurations. |
||
419 | * |
||
420 | * @param string $keyword |
||
421 | * @return string |
||
422 | */ |
||
423 | protected function prepareKeyword($keyword) |
||
424 | { |
||
425 | if ($this->isCaseInsensitive()) { |
||
426 | $keyword = Str::lower($keyword); |
||
427 | } |
||
428 | |||
429 | if ($this->isWildcard()) { |
||
430 | $keyword = $this->wildcardLikeString($keyword); |
||
431 | } |
||
432 | |||
433 | if ($this->isSmartSearch()) { |
||
434 | $keyword = "%$keyword%"; |
||
435 | } |
||
436 | |||
437 | return $keyword; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Perform column search. |
||
442 | * |
||
443 | * @return void |
||
444 | */ |
||
445 | public function columnSearch() |
||
446 | { |
||
447 | $columns = (array) $this->request->input('columns'); |
||
448 | |||
449 | foreach ($columns as $index => $column) { |
||
450 | if (! $this->request->isColumnSearchable($index)) { |
||
451 | continue; |
||
452 | } |
||
453 | |||
454 | $column = $this->getColumnName($index); |
||
455 | |||
456 | if (isset($this->columnDef['filter'][$column])) { |
||
457 | $columnDef = $this->columnDef['filter'][$column]; |
||
458 | // get a raw keyword (without wildcards) |
||
459 | $keyword = $this->getSearchKeyword($index, true); |
||
460 | $builder = $this->getQueryBuilder(); |
||
461 | |||
462 | View Code Duplication | if ($columnDef['method'] instanceof Closure) { |
|
463 | $whereQuery = $builder->newQuery(); |
||
464 | call_user_func_array($columnDef['method'], [$whereQuery, $keyword]); |
||
465 | $builder->addNestedWhereQuery($whereQuery); |
||
466 | } else { |
||
467 | $this->compileColumnQuery( |
||
468 | $builder, |
||
469 | $columnDef['method'], |
||
470 | $columnDef['parameters'], |
||
471 | $column, |
||
472 | $keyword |
||
473 | ); |
||
474 | } |
||
475 | View Code Duplication | } else { |
|
476 | if (count(explode('.', $column)) > 1) { |
||
477 | $eagerLoads = $this->getEagerLoads(); |
||
478 | $parts = explode('.', $column); |
||
479 | $relationColumn = array_pop($parts); |
||
480 | $relation = implode('.', $parts); |
||
481 | if (in_array($relation, $eagerLoads)) { |
||
482 | $column = $this->joinEagerLoadedColumn($relation, $relationColumn); |
||
483 | } |
||
484 | } |
||
485 | |||
486 | $keyword = $this->getSearchKeyword($index); |
||
487 | $this->compileColumnSearch($index, $column, $keyword); |
||
488 | } |
||
489 | |||
490 | $this->isFilterApplied = true; |
||
491 | } |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * Get proper keyword to use for search. |
||
496 | * |
||
497 | * @param int $i |
||
498 | * @param bool $raw |
||
499 | * @return string |
||
500 | */ |
||
501 | protected function getSearchKeyword($i, $raw = false) |
||
502 | { |
||
503 | $keyword = $this->request->columnKeyword($i); |
||
504 | if ($raw || $this->request->isRegex($i)) { |
||
505 | return $keyword; |
||
506 | } |
||
507 | |||
508 | return $this->setupKeyword($keyword); |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * Join eager loaded relation and get the related column name. |
||
513 | * |
||
514 | * @param string $relation |
||
515 | * @param string $relationColumn |
||
516 | * @return string |
||
517 | */ |
||
518 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
519 | { |
||
520 | $model = $this->query->getRelation($relation); |
||
521 | switch (true) { |
||
522 | case $model instanceof BelongsToMany: |
||
523 | $pivot = $model->getTable(); |
||
524 | $pivotPK = $model->getExistenceCompareKey(); |
||
525 | $pivotFK = $model->getQualifiedParentKeyName(); |
||
526 | $this->performJoin($pivot, $pivotPK, $pivotFK); |
||
527 | |||
528 | $related = $model->getRelated(); |
||
529 | $table = $related->getTable(); |
||
530 | $tablePK = $related->getForeignKey(); |
||
531 | $foreign = $pivot . '.' . $tablePK; |
||
532 | $other = $related->getQualifiedKeyName(); |
||
533 | |||
534 | $this->query->addSelect($table . '.' . $relationColumn); |
||
535 | break; |
||
536 | |||
537 | case $model instanceof HasOneOrMany: |
||
538 | $table = $model->getRelated()->getTable(); |
||
539 | $foreign = $model->getQualifiedForeignKeyName(); |
||
540 | $other = $model->getQualifiedParentKeyName(); |
||
541 | break; |
||
542 | |||
543 | case $model instanceof BelongsTo: |
||
544 | $table = $model->getRelated()->getTable(); |
||
545 | $foreign = $model->getQualifiedForeignKey(); |
||
546 | $other = $model->getQualifiedOwnerKeyName(); |
||
547 | break; |
||
548 | |||
549 | default: |
||
550 | $table = $model->getRelated()->getTable(); |
||
551 | $foreign = $model->getQualifiedForeignKey(); |
||
552 | $other = $model->getQualifiedOtherKeyName(); |
||
553 | } |
||
554 | |||
555 | $this->performJoin($table, $foreign, $other); |
||
556 | |||
557 | return $table . '.' . $relationColumn; |
||
558 | } |
||
559 | |||
560 | /** |
||
561 | * Perform join query. |
||
562 | * |
||
563 | * @param string $table |
||
564 | * @param string $foreign |
||
565 | * @param string $other |
||
566 | */ |
||
567 | protected function performJoin($table, $foreign, $other) |
||
568 | { |
||
569 | $joins = []; |
||
570 | foreach ((array) $this->getQueryBuilder()->joins as $key => $join) { |
||
571 | $joins[] = $join->table; |
||
572 | } |
||
573 | |||
574 | if (! in_array($table, $joins)) { |
||
575 | $this->getQueryBuilder()->leftJoin($table, $foreign, '=', $other); |
||
576 | } |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * Compile queries for column search. |
||
581 | * |
||
582 | * @param int $i |
||
583 | * @param mixed $column |
||
584 | * @param string $keyword |
||
585 | */ |
||
586 | protected function compileColumnSearch($i, $column, $keyword) |
||
595 | |||
596 | /** |
||
597 | * Compile regex query column search. |
||
598 | * |
||
599 | * @param mixed $column |
||
600 | * @param string $keyword |
||
601 | */ |
||
602 | protected function regexColumnSearch($column, $keyword) |
||
603 | { |
||
604 | if ($this->isOracleSql()) { |
||
605 | $sql = ! $this->isCaseInsensitive() ? 'REGEXP_LIKE( ' . $column . ' , ? )' : 'REGEXP_LIKE( LOWER(' . $column . ') , ?, \'i\' )'; |
||
606 | $this->query->whereRaw($sql, [$keyword]); |
||
607 | } elseif ($this->database == 'pgsql') { |
||
608 | $sql = ! $this->isCaseInsensitive() ? $column . ' ~ ?' : $column . ' ~* ? '; |
||
609 | $this->query->whereRaw($sql, [$keyword]); |
||
610 | } else { |
||
611 | $sql = ! $this->isCaseInsensitive() ? $column . ' REGEXP ?' : 'LOWER(' . $column . ') REGEXP ?'; |
||
612 | $this->query->whereRaw($sql, [Str::lower($keyword)]); |
||
613 | } |
||
614 | } |
||
615 | |||
616 | /** |
||
617 | * Perform sorting of columns. |
||
618 | * |
||
619 | * @return void |
||
620 | */ |
||
621 | public function ordering() |
||
674 | |||
675 | /** |
||
676 | * Get NULLS LAST SQL. |
||
677 | * |
||
678 | * @param string $column |
||
679 | * @param string $direction |
||
680 | * @return string |
||
681 | */ |
||
682 | protected function getNullsLastSql($column, $direction) |
||
688 | |||
689 | /** |
||
690 | * Perform pagination |
||
691 | * |
||
692 | * @return void |
||
693 | */ |
||
694 | public function paging() |
||
699 | |||
700 | /** |
||
701 | * Get results |
||
702 | * |
||
703 | * @return array|static[] |
||
704 | */ |
||
705 | public function results() |
||
709 | } |
||
710 |