| Total Complexity | 90 |
| Total Lines | 475 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SelectQuery 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.
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 SelectQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class SelectQuery extends AbstractFacade |
||
| 23 | { |
||
| 24 | use InputFieldTrait; |
||
|
|
|||
| 25 | use SelectTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Get required data for select on tables |
||
| 29 | * |
||
| 30 | * @param SelectEntity $selectEntity |
||
| 31 | * |
||
| 32 | * @return SelectEntity |
||
| 33 | * @throws Exception |
||
| 34 | */ |
||
| 35 | public function prepareSelect(SelectEntity $selectEntity): SelectEntity |
||
| 36 | { |
||
| 37 | $this->setDefaultOptions($selectEntity); |
||
| 38 | |||
| 39 | // From select.inc.php |
||
| 40 | $selectEntity->fields = $this->driver->fields($selectEntity->table); |
||
| 41 | $this->setFieldsOptions($selectEntity); |
||
| 42 | if (!$selectEntity->columns && $this->driver->support("table")) { |
||
| 43 | throw new Exception($this->utils->trans->lang('Unable to select the table') . |
||
| 44 | ($selectEntity->fields ? "." : ": " . $this->driver->error())); |
||
| 45 | } |
||
| 46 | |||
| 47 | $selectEntity->indexes = $this->driver->indexes($selectEntity->table); |
||
| 48 | $this->setForeignKeys($selectEntity); |
||
| 49 | $this->setSelectColumns($selectEntity); |
||
| 50 | |||
| 51 | $this->setSelectWhere($selectEntity); |
||
| 52 | $this->setSelectOrder($selectEntity); |
||
| 53 | $this->setSelectLimit($selectEntity); |
||
| 54 | $this->setPrimaryKey($selectEntity); |
||
| 55 | |||
| 56 | // $set = null; |
||
| 57 | // if(isset($rights["insert"]) || !this->driver->support("table")) { |
||
| 58 | // $set = ""; |
||
| 59 | // foreach((array) $queryOptions["where"] as $val) { |
||
| 60 | // if($foreignKeys[$val["col"]] && count($foreignKeys[$val["col"]]) == 1 && ($val["op"] == "=" |
||
| 61 | // || (!$val["op"] && !preg_match('~[_%]~', $val["val"])) // LIKE in Editor |
||
| 62 | // )) { |
||
| 63 | // $set .= "&set" . urlencode("[" . $this->driver->bracketEscape($val["col"]) . "]") . "=" . urlencode($val["val"]); |
||
| 64 | // } |
||
| 65 | // } |
||
| 66 | // } |
||
| 67 | // $this->admin->selectLinks($tableStatus, $set); |
||
| 68 | |||
| 69 | // if($page == "last") |
||
| 70 | // { |
||
| 71 | // $isGroup = count($group) < count($select); |
||
| 72 | // $found_rows = $this->driver->result($this->driver->getRowCountQuery($table, $where, $isGroup, $group)); |
||
| 73 | // $page = \floor(\max(0, $found_rows - 1) / $limit); |
||
| 74 | // } |
||
| 75 | |||
| 76 | $this->setSelectOptions($selectEntity); |
||
| 77 | $this->setSelectEntity($selectEntity); |
||
| 78 | $this->setSelectQuery($selectEntity); |
||
| 79 | |||
| 80 | return $selectEntity; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param SelectEntity $selectEntity |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | private function setDefaultOptions(SelectEntity $selectEntity): void |
||
| 89 | { |
||
| 90 | $defaultOptions = [ |
||
| 91 | 'columns' => [], |
||
| 92 | 'where' => [], |
||
| 93 | 'order' => [], |
||
| 94 | 'desc' => [], |
||
| 95 | 'fulltext' => [], |
||
| 96 | 'limit' => '50', |
||
| 97 | 'text_length' => '100', |
||
| 98 | 'page' => '1', |
||
| 99 | ]; |
||
| 100 | foreach ($defaultOptions as $name => $value) { |
||
| 101 | if (!isset($this->utils->input->values[$name])) { |
||
| 102 | $this->utils->input->values[$name] = $value; |
||
| 103 | } |
||
| 104 | if (!isset($selectEntity->queryOptions[$name])) { |
||
| 105 | $selectEntity->queryOptions[$name] = $value; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | $page = intval($selectEntity->queryOptions['page']); |
||
| 109 | if ($page > 0) { |
||
| 110 | $page -= 1; // Page numbers start at 0 here, instead of 1. |
||
| 111 | } |
||
| 112 | $selectEntity->queryOptions['page'] = $page; |
||
| 113 | $selectEntity->page = $page; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param SelectEntity $selectEntity |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | private function setFieldsOptions(SelectEntity $selectEntity): void |
||
| 122 | { |
||
| 123 | $selectEntity->rights = []; // privilege => 0 |
||
| 124 | $selectEntity->columns = []; // selectable columns |
||
| 125 | $selectEntity->textLength = 0; |
||
| 126 | foreach ($selectEntity->fields as $key => $field) { |
||
| 127 | $name = $this->admin->fieldName($field); |
||
| 128 | if (isset($field->privileges["select"]) && $name != "") { |
||
| 129 | $selectEntity->columns[$key] = html_entity_decode(strip_tags($name), ENT_QUOTES); |
||
| 130 | if ($this->admin->isShortable($field)) { |
||
| 131 | $this->setSelectTextLength($selectEntity); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | $selectEntity->rights[] = $field->privileges; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Find out foreign keys for each column |
||
| 140 | * |
||
| 141 | * @param SelectEntity $selectEntity |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | private function setForeignKeys(SelectEntity $selectEntity): void |
||
| 146 | { |
||
| 147 | $selectEntity->foreignKeys = []; |
||
| 148 | foreach ($this->driver->foreignKeys($selectEntity->table) as $foreignKey) { |
||
| 149 | foreach ($foreignKey->source as $val) { |
||
| 150 | $selectEntity->foreignKeys[$val][] = $foreignKey; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param array $value |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | private function colHasValidValue(array $value): bool |
||
| 161 | { |
||
| 162 | return $value['fun'] === 'count' || |
||
| 163 | ($value['col'] !== '' && (!$value['fun'] || |
||
| 164 | in_array($value['fun'], $this->driver->functions()) || |
||
| 165 | in_array($value['fun'], $this->driver->grouping()))); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param array $where AND conditions |
||
| 170 | * @param array $foreignKeys |
||
| 171 | * |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | // private function setSelectEmail(array $where, array $foreignKeys) |
||
| 175 | // { |
||
| 176 | // return false; |
||
| 177 | // } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Apply SQL function |
||
| 181 | * |
||
| 182 | * @param string $function |
||
| 183 | * @param string $column escaped column identifier |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function applySqlFunction(string $function, string $column): string |
||
| 188 | { |
||
| 189 | if (!$function) { |
||
| 190 | return $column; |
||
| 191 | } |
||
| 192 | if ($function === 'unixepoch') { |
||
| 193 | return "DATETIME($column, '$function')"; |
||
| 194 | } |
||
| 195 | if ($function === 'count distinct') { |
||
| 196 | return "COUNT(DISTINCT $column)"; |
||
| 197 | } |
||
| 198 | return strtoupper($function) . "($column)"; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param SelectEntity $selectEntity |
||
| 203 | * |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | private function setSelectColumns(SelectEntity $selectEntity): void |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param array $value |
||
| 227 | * @param array $fields |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | private function getWhereCondition(array $value, array $fields): string |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param TableFieldEntity $field |
||
| 260 | * @param array $value |
||
| 261 | * |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | private function selectFieldIsValid(TableFieldEntity $field, array $value): bool |
||
| 265 | { |
||
| 266 | $op = $value['op']; |
||
| 267 | $val = $value['val']; |
||
| 268 | $in = preg_match('~IN$~', $op) ? ',' : ''; |
||
| 269 | return (preg_match('~^[-\d.' . $in . ']+$~', $val) || |
||
| 270 | !preg_match('~' . $this->driver->numberRegex() . '|bit~', $field->type)) && |
||
| 271 | (!preg_match("~[\x80-\xFF]~", $val) || |
||
| 272 | preg_match('~char|text|enum|set~', $field->type)) && |
||
| 273 | (!preg_match('~date|timestamp~', $field->type) || |
||
| 274 | preg_match('~^\d+-\d+-\d+~', $val)); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param array $value |
||
| 279 | * @param array $fields |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | private function getSelectExpression(array $value, array $fields): string |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param IndexEntity $index |
||
| 312 | * @param int $i |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | private function getMatchExpression(IndexEntity $index, int $i): string |
||
| 317 | { |
||
| 318 | $columns = array_map(function ($column) { |
||
| 319 | return $this->driver->escapeId($column); |
||
| 320 | }, $index->columns); |
||
| 321 | $fulltext = $this->utils->input->values['fulltext'][$i] ?? ''; |
||
| 322 | $match = $this->driver->quote($fulltext); |
||
| 323 | if (isset($this->utils->input->values['boolean'][$i])) { |
||
| 324 | $match .= ' IN BOOLEAN MODE'; |
||
| 325 | } |
||
| 326 | return 'MATCH (' . implode(', ', $columns) . ') AGAINST (' . $match . ')'; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param SelectEntity $selectEntity |
||
| 331 | * |
||
| 332 | * @return void |
||
| 333 | */ |
||
| 334 | private function setSelectWhere(SelectEntity $selectEntity): void |
||
| 348 | } |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param SelectEntity $selectEntity |
||
| 354 | * |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | private function setSelectOrder(SelectEntity $selectEntity): void |
||
| 358 | { |
||
| 359 | $values = $this->utils->input->values; |
||
| 360 | $selectEntity->order = []; |
||
| 361 | foreach ($values['order'] as $key => $value) { |
||
| 362 | if ($value !== '') { |
||
| 363 | $regexp = '~^((COUNT\(DISTINCT |[A-Z0-9_]+\()(`(?:[^`]|``)+`|"(?:[^"]|"")+")\)|COUNT\(\*\))$~'; |
||
| 364 | if (preg_match($regexp, $value) !== false) { |
||
| 365 | $value = $this->driver->escapeId($value); |
||
| 366 | } |
||
| 367 | if (isset($values['desc'][$key]) && intval($values['desc'][$key]) !== 0) { |
||
| 368 | $value .= ' DESC'; |
||
| 369 | } |
||
| 370 | $selectEntity->order[] = $value; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param SelectEntity $selectEntity |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | private function setSelectLimit(SelectEntity $selectEntity): void |
||
| 381 | { |
||
| 382 | $selectEntity->limit = intval($this->utils->input->values['limit'] ?? 50); |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param SelectEntity $selectEntity |
||
| 387 | * |
||
| 388 | * @return void |
||
| 389 | */ |
||
| 390 | private function setSelectTextLength(SelectEntity $selectEntity): void |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param SelectEntity $selectEntity |
||
| 397 | * |
||
| 398 | * @return void |
||
| 399 | */ |
||
| 400 | private function setPrimaryKey(SelectEntity $selectEntity): void |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param SelectEntity $selectEntity |
||
| 430 | * |
||
| 431 | * @return void |
||
| 432 | */ |
||
| 433 | public function setSelectQuery(SelectEntity $selectEntity): void |
||
| 434 | { |
||
| 435 | $query = $this->driver->buildSelectQuery($selectEntity->tableSelect); |
||
| 436 | // From adminer.inc.php |
||
| 437 | $selectEntity->query = str_replace("\n", " ", $query); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param SelectEntity $selectEntity |
||
| 442 | * |
||
| 443 | * @return void |
||
| 444 | */ |
||
| 445 | private function setSelectOptions(SelectEntity $selectEntity): void |
||
| 446 | { |
||
| 447 | $selectEntity->options = [ |
||
| 448 | 'columns' => $this->getColumnsOptions($selectEntity->select, |
||
| 449 | $selectEntity->columns, $selectEntity->queryOptions), |
||
| 450 | 'filters' => $this->getFiltersOptions($selectEntity->columns, |
||
| 451 | $selectEntity->indexes, $selectEntity->queryOptions), |
||
| 452 | 'sorting' => $this->getSortingOptions($selectEntity->columns, |
||
| 453 | $selectEntity->queryOptions), |
||
| 454 | 'limit' => $this->getLimitOptions($selectEntity->limit), |
||
| 455 | 'length' => $this->getLengthOptions($selectEntity->textLength), |
||
| 456 | // 'action' => $this->getActionOptions($selectEntity->indexes), |
||
| 457 | ]; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param SelectEntity $selectEntity |
||
| 462 | * |
||
| 463 | * @return void |
||
| 464 | */ |
||
| 465 | private function setSelectEntity(SelectEntity $selectEntity): void |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Print action box in select |
||
| 551 |