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 |
||
| 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->getBaseQueryBuilder()); |
||
| 161 | |||
| 162 | return; |
||
| 163 | } |
||
| 164 | |||
| 165 | foreach ($this->request->orderableColumns() as $orderable) { |
||
| 166 | $column = $this->getColumnName($orderable['column'], true); |
||
| 167 | |||
| 168 | if ($this->isBlacklisted($column) && !$this->hasOrderColumn($column)) { |
||
| 169 | continue; |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($this->hasOrderColumn($column)) { |
||
| 173 | $this->applyOrderColumn($column, $orderable); |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | |||
| 177 | $column = $this->resolveRelationColumn($column); |
||
| 178 | if ($this->nullsLast) { |
||
| 179 | $this->getBaseQueryBuilder()->orderByRaw($this->getNullsLastSql($column, $orderable['direction'])); |
||
| 180 | } else { |
||
| 181 | $this->getBaseQueryBuilder()->orderBy($column, $orderable['direction']); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get the base query builder instance. |
||
| 188 | * |
||
| 189 | * @param mixed $instance |
||
| 190 | * @return \Illuminate\Database\Query\Builder |
||
| 191 | */ |
||
| 192 | protected function getBaseQueryBuilder($instance = null) |
||
| 193 | { |
||
| 194 | if (!$instance) { |
||
| 195 | $instance = $this->query; |
||
| 196 | } |
||
| 197 | |||
| 198 | if ($instance instanceof EloquentBuilder) { |
||
| 199 | return $instance->getQuery(); |
||
| 200 | } |
||
| 201 | |||
| 202 | return $instance; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Check if column has custom sort handler. |
||
| 207 | * |
||
| 208 | * @param string $column |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | protected function hasOrderColumn($column) |
||
| 212 | { |
||
| 213 | return isset($this->columnDef['order'][$column]); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Apply orderColumn custom query. |
||
| 218 | * |
||
| 219 | * @param string $column |
||
| 220 | * @param array $orderable |
||
| 221 | */ |
||
| 222 | protected function applyOrderColumn($column, $orderable): void |
||
| 223 | { |
||
| 224 | $sql = $this->columnDef['order'][$column]['sql']; |
||
| 225 | $sql = str_replace('$1', $orderable['direction'], $sql); |
||
| 226 | $bindings = $this->columnDef['order'][$column]['bindings']; |
||
| 227 | $this->query->orderByRaw($sql, $bindings); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Resolve the proper column name be used. |
||
| 232 | * |
||
| 233 | * @param string $column |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | protected function resolveRelationColumn($column) |
||
| 237 | { |
||
| 238 | return $column; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get NULLS LAST SQL. |
||
| 243 | * |
||
| 244 | * @param string $column |
||
| 245 | * @param string $direction |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | protected function getNullsLastSql($column, $direction) |
||
| 249 | { |
||
| 250 | $sql = $this->config->get('datatables.nulls_last_sql', '%s %s NULLS LAST'); |
||
| 251 | |||
| 252 | return sprintf($sql, $column, $direction); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Perform column search. |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | public function columnSearch() |
||
| 261 | { |
||
| 262 | $columns = $this->request->columns(); |
||
| 263 | |||
| 264 | foreach ($columns as $index => $column) { |
||
| 265 | if (!$this->request->isColumnSearchable($index)) { |
||
| 266 | continue; |
||
| 267 | } |
||
| 268 | |||
| 269 | $column = $this->getColumnName($index); |
||
| 270 | |||
| 271 | if ($this->hasFilterColumn($column)) { |
||
| 272 | $keyword = $this->getColumnSearchKeyword($index, $raw = true); |
||
| 273 | $this->applyFilterColumn($this->getBaseQueryBuilder(), $column, $keyword); |
||
| 274 | continue; |
||
| 275 | } |
||
| 276 | |||
| 277 | $column = $this->resolveRelationColumn($column); |
||
| 278 | $keyword = $this->getColumnSearchKeyword($index); |
||
| 279 | $this->compileColumnSearch($index, $column, $keyword); |
||
| 280 | |||
| 281 | $this->isFilterApplied = true; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Check if column has custom filter handler. |
||
| 287 | * |
||
| 288 | * @param string $columnName |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public function hasFilterColumn($columnName) |
||
| 292 | { |
||
| 293 | return isset($this->columnDef['filter'][$columnName]); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get column keyword to use for search. |
||
| 298 | * |
||
| 299 | * @param int $i |
||
| 300 | * @param bool $raw |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | protected function getColumnSearchKeyword($i, $raw = false) |
||
| 304 | { |
||
| 305 | $keyword = $this->request->columnKeyword($i); |
||
| 306 | if ($raw || $this->request->isRegex($i)) { |
||
| 307 | return $keyword; |
||
| 308 | } |
||
| 309 | |||
| 310 | return $this->setupKeyword($keyword); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Apply filterColumn api search. |
||
| 315 | * |
||
| 316 | * @param Builder $query |
||
| 317 | * @param string $columnName |
||
| 318 | * @param string $keyword |
||
| 319 | * @param string $boolean |
||
| 320 | */ |
||
| 321 | protected function applyFilterColumn(Builder $query, $columnName, $keyword, $boolean = 'and') |
||
| 322 | { |
||
| 323 | $callback = $this->columnDef['filter'][$columnName]['method']; |
||
| 324 | $builder = $query->newQuery(); |
||
| 325 | $callback($builder, $keyword); |
||
| 326 | $query->addNestedWhereQuery($builder, $boolean); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Compile queries for column search. |
||
| 331 | * |
||
| 332 | * @param int $i |
||
| 333 | * @param string $column |
||
| 334 | * @param string $keyword |
||
| 335 | */ |
||
| 336 | protected function compileColumnSearch($i, $column, $keyword) |
||
| 337 | { |
||
| 338 | if ($this->request->isRegex($i)) { |
||
| 339 | $column = strstr($column, '(') ? $this->connection->raw($column) : $column; |
||
| 340 | $this->regexColumnSearch($column, $keyword); |
||
| 341 | } else { |
||
| 342 | $this->compileQuerySearch($this->query, $column, $keyword, ''); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Compile regex query column search. |
||
| 348 | * |
||
| 349 | * @param mixed $column |
||
| 350 | * @param string $keyword |
||
| 351 | */ |
||
| 352 | protected function regexColumnSearch($column, $keyword) |
||
| 353 | { |
||
| 354 | switch ($this->connection->getDriverName()) { |
||
| 355 | case 'oracle': |
||
| 356 | $sql = !$this->config |
||
| 357 | ->isCaseInsensitive() ? 'REGEXP_LIKE( ' . $column . ' , ? )' : 'REGEXP_LIKE( LOWER(' . $column . ') , ?, \'i\' )'; |
||
| 358 | break; |
||
| 359 | |||
| 360 | case 'pgsql': |
||
| 361 | $sql = !$this->config->isCaseInsensitive() ? $column . ' ~ ?' : $column . ' ~* ? '; |
||
| 362 | break; |
||
| 363 | |||
| 364 | default: |
||
| 365 | $sql = !$this->config |
||
| 366 | ->isCaseInsensitive() ? $column . ' REGEXP ?' : 'LOWER(' . $column . ') REGEXP ?'; |
||
| 367 | $keyword = Str::lower($keyword); |
||
| 368 | } |
||
| 369 | |||
| 370 | $this->query->whereRaw($sql, [$keyword]); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Compile query builder where clause depending on configurations. |
||
| 375 | * |
||
| 376 | * @param mixed $query |
||
| 377 | * @param string $column |
||
| 378 | * @param string $keyword |
||
| 379 | * @param string $relation |
||
| 380 | */ |
||
| 381 | protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
||
| 382 | { |
||
| 383 | $column = $this->addTablePrefix($query, $column); |
||
| 384 | $column = $this->castColumn($column); |
||
| 385 | $sql = $column . ' LIKE ?'; |
||
| 386 | |||
| 387 | if ($this->config->isCaseInsensitive()) { |
||
| 388 | $sql = 'LOWER(' . $column . ') LIKE ?'; |
||
| 389 | } |
||
| 390 | |||
| 391 | $query->{$relation . 'WhereRaw'}($sql, [$this->prepareKeyword($keyword)]); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Patch for fix about ambiguous field. |
||
| 396 | * Ambiguous field error will appear when query use join table and search with keyword. |
||
| 397 | * |
||
| 398 | * @param mixed $query |
||
| 399 | * @param string $column |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | protected function addTablePrefix($query, $column) |
||
| 403 | { |
||
| 404 | if (strpos($column, '.') === false) { |
||
| 405 | $q = $this->getBaseQueryBuilder($query); |
||
| 406 | if (!$q->from instanceof Expression) { |
||
| 407 | $column = $q->from . '.' . $column; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | return $this->wrap($column); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Wrap a column and cast based on database driver. |
||
| 416 | * |
||
| 417 | * @param string $column |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | protected function castColumn($column) |
||
| 421 | { |
||
| 422 | switch ($this->connection->getDriverName()) { |
||
| 423 | case 'pgsql': |
||
| 424 | return 'CAST(' . $column . ' as TEXT)'; |
||
| 425 | case 'firebird': |
||
| 426 | return 'CAST(' . $column . ' as VARCHAR(255))'; |
||
| 427 | default: |
||
| 428 | return $column; |
||
| 429 | } |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Prepare search keyword based on configurations. |
||
| 434 | * |
||
| 435 | * @param string $keyword |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | protected function prepareKeyword($keyword) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Perform pagination. |
||
| 457 | * |
||
| 458 | * @return void |
||
| 459 | */ |
||
| 460 | public function paging() |
||
| 461 | { |
||
| 462 | $this->query->skip($this->request->input('start')) |
||
| 463 | ->take((int) $this->request->input('length') > 0 ? $this->request->input('length') : 10); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get paginated results. |
||
| 468 | * |
||
| 469 | * @return \Illuminate\Support\Collection |
||
| 470 | */ |
||
| 471 | public function results() |
||
| 472 | { |
||
| 473 | return $this->query->get(); |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Add column in collection. |
||
| 478 | * |
||
| 479 | * @param string $name |
||
| 480 | * @param string|callable $content |
||
| 481 | * @param bool|int $order |
||
| 482 | * @return \Yajra\Datatables\Engines\BaseEngine|\Yajra\Datatables\Engines\QueryBuilderEngine |
||
| 483 | */ |
||
| 484 | public function addColumn($name, $content, $order = false) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Get query builder instance. |
||
| 493 | * |
||
| 494 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 495 | */ |
||
| 496 | public function getQuery() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Perform global search for the given keyword. |
||
| 503 | * |
||
| 504 | * @param string $keyword |
||
| 505 | */ |
||
| 506 | protected function globalSearch($keyword) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Append debug parameters on output. |
||
| 532 | * |
||
| 533 | * @param array $output |
||
| 534 | * @return array |
||
| 535 | */ |
||
| 536 | protected function showDebugger(array $output) |
||
| 543 | } |
||
| 544 |