| Total Complexity | 108 |
| Total Lines | 569 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractDQLQueryBuilder 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 AbstractDQLQueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | abstract class AbstractDQLQueryBuilder implements DQLQueryBuilderInterface |
||
| 42 | { |
||
| 43 | protected string $separator = ' '; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array map of condition aliases to condition classes. For example: |
||
| 47 | * |
||
| 48 | * ```php |
||
| 49 | * return [ |
||
| 50 | * 'LIKE' => \Yiisoft\Db\Condition\LikeCondition::class, |
||
| 51 | * ]; |
||
| 52 | * ``` |
||
| 53 | * |
||
| 54 | * This property is used by {@see createConditionFromArray} method. |
||
| 55 | * See default condition classes list in {@see defaultConditionClasses()} method. |
||
| 56 | * |
||
| 57 | * In case you want to add custom conditions support, use the {@see setConditionClasses()} method. |
||
| 58 | * |
||
| 59 | * @see setConditonClasses() |
||
| 60 | * @see defaultConditionClasses() |
||
| 61 | */ |
||
| 62 | protected array $conditionClasses = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @psalm-var array<string, class-string<ExpressionBuilderInterface>> maps expression class to expression builder |
||
| 66 | * class. |
||
| 67 | * |
||
| 68 | * For example: |
||
| 69 | * |
||
| 70 | * ```php |
||
| 71 | * [ |
||
| 72 | * Expression::class => ExpressionBuilder::class |
||
| 73 | * ] |
||
| 74 | * ``` |
||
| 75 | * This property is mainly used by {@see buildExpression()} to build SQL expressions form expression objects. |
||
| 76 | * See default values in {@see defaultExpressionBuilders()} method. |
||
| 77 | * |
||
| 78 | * {@see setExpressionBuilders()} |
||
| 79 | * {@see defaultExpressionBuilders()} |
||
| 80 | */ |
||
| 81 | protected array $expressionBuilders = []; |
||
| 82 | |||
| 83 | public function __construct( |
||
| 84 | private QueryBuilderInterface $queryBuilder, |
||
| 85 | private QuoterInterface $quoter, |
||
| 86 | private SchemaInterface $schema |
||
| 87 | ) { |
||
| 88 | $this->expressionBuilders = $this->defaultExpressionBuilders(); |
||
| 89 | $this->conditionClasses = $this->defaultConditionClasses(); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function build(QueryInterface $query, array $params = []): array |
||
| 138 | } |
||
| 139 | |||
| 140 | public function buildColumns(array|string $columns): string |
||
| 141 | { |
||
| 142 | if (!is_array($columns)) { |
||
|
|
|||
| 143 | if (str_contains($columns, '(')) { |
||
| 144 | return $columns; |
||
| 145 | } |
||
| 146 | |||
| 147 | $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** @psalm-var array<array-key, ExpressionInterface|string> $columns */ |
||
| 151 | foreach ($columns as $i => $column) { |
||
| 152 | if ($column instanceof ExpressionInterface) { |
||
| 153 | $columns[$i] = $this->buildExpression($column); |
||
| 154 | } elseif (!str_contains($column, '(')) { |
||
| 155 | $columns[$i] = $this->quoter->quoteColumnName($column); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** @psalm-var string[] $columns */ |
||
| 160 | return implode(', ', $columns); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function buildCondition(array|string|ExpressionInterface|null $condition, array &$params = []): string |
||
| 164 | { |
||
| 165 | if (is_array($condition)) { |
||
| 166 | if (empty($condition)) { |
||
| 167 | return ''; |
||
| 168 | } |
||
| 169 | |||
| 170 | $condition = $this->createConditionFromArray($condition); |
||
| 171 | } |
||
| 172 | |||
| 173 | if ($condition instanceof ExpressionInterface) { |
||
| 174 | return $this->buildExpression($condition, $params); |
||
| 175 | } |
||
| 176 | |||
| 177 | return $condition ?? ''; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @throws InvalidArgumentException |
||
| 182 | * |
||
| 183 | * @psalm-suppress UndefinedInterfaceMethod |
||
| 184 | * @psalm-suppress MixedMethodCall |
||
| 185 | */ |
||
| 186 | public function buildExpression(ExpressionInterface $expression, array &$params = []): string |
||
| 187 | { |
||
| 188 | $builder = $this->queryBuilder->getExpressionBuilder($expression); |
||
| 189 | return (string) $builder->build($expression, $params); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function buildFrom(array|null $tables, array &$params): string |
||
| 193 | { |
||
| 194 | if (empty($tables)) { |
||
| 195 | return ''; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** @psalm-var string[] */ |
||
| 199 | $tables = $this->quoteTableNames($tables, $params); |
||
| 200 | |||
| 201 | return 'FROM ' . implode(', ', $tables); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function buildGroupBy(array $columns, array &$params = []): string |
||
| 205 | { |
||
| 206 | if (empty($columns)) { |
||
| 207 | return ''; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** @psalm-var array<string, ExpressionInterface|string> $columns */ |
||
| 211 | foreach ($columns as $i => $column) { |
||
| 212 | if ($column instanceof ExpressionInterface) { |
||
| 213 | $columns[$i] = $this->buildExpression($column); |
||
| 214 | if ($column instanceof Expression || $column instanceof QueryInterface) { |
||
| 215 | $params = array_merge($params, $column->getParams()); |
||
| 216 | } |
||
| 217 | } elseif (!str_contains($column, '(')) { |
||
| 218 | $columns[$i] = $this->quoter->quoteColumnName($column); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | return 'GROUP BY ' . implode(', ', $columns); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function buildHaving(array|ExpressionInterface|string|null $condition, array &$params = []): string |
||
| 226 | { |
||
| 227 | $having = $this->buildCondition($condition, $params); |
||
| 228 | |||
| 229 | return ($having === '') ? '' : ('HAVING ' . $having); |
||
| 230 | } |
||
| 231 | |||
| 232 | public function buildJoin(array $joins, array &$params): string |
||
| 233 | { |
||
| 234 | if (empty($joins)) { |
||
| 235 | return ''; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @psalm-var array< |
||
| 240 | * array-key, |
||
| 241 | * array{ |
||
| 242 | * 0?:string, |
||
| 243 | * 1?:array<array-key, Query|string>|string, |
||
| 244 | * 2?:array|ExpressionInterface|string|null |
||
| 245 | * }|null |
||
| 246 | * > $joins |
||
| 247 | */ |
||
| 248 | foreach ($joins as $i => $join) { |
||
| 249 | if (!is_array($join) || !isset($join[0], $join[1])) { |
||
| 250 | throw new Exception( |
||
| 251 | 'A join clause must be specified as an array of join type, join table, and optionally join ' |
||
| 252 | . 'condition.' |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | /* 0:join type, 1:join table, 2:on-condition (optional) */ |
||
| 257 | [$joinType, $table] = $join; |
||
| 258 | |||
| 259 | $tables = $this->quoteTableNames((array) $table, $params); |
||
| 260 | |||
| 261 | /** @var string $table */ |
||
| 262 | $table = reset($tables); |
||
| 263 | $joins[$i] = "$joinType $table"; |
||
| 264 | |||
| 265 | if (isset($join[2])) { |
||
| 266 | $condition = $this->buildCondition($join[2], $params); |
||
| 267 | if ($condition !== '') { |
||
| 268 | $joins[$i] .= ' ON ' . $condition; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** @psalm-var array<string> $joins */ |
||
| 274 | return implode($this->separator, $joins); |
||
| 275 | } |
||
| 276 | |||
| 277 | public function buildLimit(ExpressionInterface|int|null $limit, ExpressionInterface|int|null $offset): string |
||
| 278 | { |
||
| 279 | $sql = ''; |
||
| 280 | |||
| 281 | if ($this->hasLimit($limit)) { |
||
| 282 | $sql = 'LIMIT ' . ($limit instanceof ExpressionInterface ? $this->buildExpression($limit) : (string) $limit); |
||
| 283 | } |
||
| 284 | |||
| 285 | if ($this->hasOffset($offset)) { |
||
| 286 | $sql .= ' OFFSET ' . ($offset instanceof ExpressionInterface ? $this->buildExpression($offset) : (string) $offset); |
||
| 287 | } |
||
| 288 | |||
| 289 | return ltrim($sql); |
||
| 290 | } |
||
| 291 | |||
| 292 | public function buildOrderBy(array $columns, array &$params = []): string |
||
| 293 | { |
||
| 294 | if (empty($columns)) { |
||
| 295 | return ''; |
||
| 296 | } |
||
| 297 | |||
| 298 | $orders = []; |
||
| 299 | |||
| 300 | /** @psalm-var array<string, ExpressionInterface|int|string> $columns */ |
||
| 301 | foreach ($columns as $name => $direction) { |
||
| 302 | if ($direction instanceof ExpressionInterface) { |
||
| 303 | $orders[] = $this->buildExpression($direction); |
||
| 304 | if ($direction instanceof Expression || $direction instanceof QueryInterface) { |
||
| 305 | $params = array_merge($params, $direction->getParams()); |
||
| 306 | } |
||
| 307 | } else { |
||
| 308 | $orders[] = $this->quoter->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : ''); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | return 'ORDER BY ' . implode(', ', $orders); |
||
| 313 | } |
||
| 314 | |||
| 315 | public function buildOrderByAndLimit( |
||
| 316 | string $sql, |
||
| 317 | array $orderBy, |
||
| 318 | ExpressionInterface|int|null $limit, |
||
| 319 | ExpressionInterface|int|null $offset, |
||
| 320 | array &$params = [] |
||
| 321 | ): string { |
||
| 322 | $orderBy = $this->buildOrderBy($orderBy, $params); |
||
| 323 | if ($orderBy !== '') { |
||
| 324 | $sql .= $this->separator . $orderBy; |
||
| 325 | } |
||
| 326 | $limit = $this->buildLimit($limit, $offset); |
||
| 327 | if ($limit !== '') { |
||
| 328 | $sql .= $this->separator . $limit; |
||
| 329 | } |
||
| 330 | |||
| 331 | return $sql; |
||
| 332 | } |
||
| 333 | |||
| 334 | public function buildSelect( |
||
| 335 | array $columns, |
||
| 336 | array &$params, |
||
| 337 | bool|null $distinct = false, |
||
| 338 | string $selectOption = null |
||
| 339 | ): string { |
||
| 340 | $select = $distinct ? 'SELECT DISTINCT' : 'SELECT'; |
||
| 341 | |||
| 342 | if ($selectOption !== null) { |
||
| 343 | $select .= ' ' . $selectOption; |
||
| 344 | } |
||
| 345 | |||
| 346 | if (empty($columns)) { |
||
| 347 | return $select . ' *'; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** @psalm-var array<array-key, ExpressionInterface|string> $columns */ |
||
| 351 | foreach ($columns as $i => $column) { |
||
| 352 | if ($column instanceof ExpressionInterface) { |
||
| 353 | if (is_int($i)) { |
||
| 354 | $columns[$i] = $this->buildExpression($column, $params); |
||
| 355 | } else { |
||
| 356 | $columns[$i] = $this->buildExpression($column, $params) . ' AS ' |
||
| 357 | . $this->quoter->quoteColumnName($i); |
||
| 358 | } |
||
| 359 | } elseif (is_string($i) && $i !== $column) { |
||
| 360 | if (!str_contains($column, '(')) { |
||
| 361 | $column = $this->quoter->quoteColumnName($column); |
||
| 362 | } |
||
| 363 | $columns[$i] = "$column AS " . $this->quoter->quoteColumnName($i); |
||
| 364 | } elseif (!str_contains($column, '(')) { |
||
| 365 | if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_.]+)$/', $column, $matches)) { |
||
| 366 | $columns[$i] = $this->quoter->quoteColumnName($matches[1]) |
||
| 367 | . ' AS ' . $this->quoter->quoteColumnName($matches[2]); |
||
| 368 | } else { |
||
| 369 | $columns[$i] = $this->quoter->quoteColumnName($column); |
||
| 370 | } |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | return $select . ' ' . implode(', ', $columns); |
||
| 375 | } |
||
| 376 | |||
| 377 | public function buildUnion(array $unions, array &$params): string |
||
| 378 | { |
||
| 379 | if (empty($unions)) { |
||
| 380 | return ''; |
||
| 381 | } |
||
| 382 | |||
| 383 | $result = ''; |
||
| 384 | |||
| 385 | /** @psalm-var array<array{query:Query|string, all:bool}> $unions */ |
||
| 386 | foreach ($unions as $i => $union) { |
||
| 387 | $query = $union['query']; |
||
| 388 | if ($query instanceof QueryInterface) { |
||
| 389 | [$unions[$i]['query'], $params] = $this->build($query, $params); |
||
| 390 | } |
||
| 391 | |||
| 392 | $result .= 'UNION ' . ($union['all'] ? 'ALL ' : '') . '( ' . $unions[$i]['query'] . ' ) '; |
||
| 393 | } |
||
| 394 | |||
| 395 | return trim($result); |
||
| 396 | } |
||
| 397 | |||
| 398 | public function buildWhere( |
||
| 399 | array|string|ConditionInterface|ExpressionInterface|null $condition, |
||
| 400 | array &$params = [] |
||
| 401 | ): string { |
||
| 402 | $where = $this->buildCondition($condition, $params); |
||
| 403 | return ($where === '') ? '' : ('WHERE ' . $where); |
||
| 404 | } |
||
| 405 | |||
| 406 | public function buildWithQueries(array $withs, array &$params): string |
||
| 407 | { |
||
| 408 | if (empty($withs)) { |
||
| 409 | return ''; |
||
| 410 | } |
||
| 411 | |||
| 412 | $recursive = false; |
||
| 413 | $result = []; |
||
| 414 | |||
| 415 | /** @psalm-var array<array-key, array{query:string|Query, alias:string, recursive:bool}> $withs */ |
||
| 416 | foreach ($withs as $with) { |
||
| 417 | if ($with['recursive']) { |
||
| 418 | $recursive = true; |
||
| 419 | } |
||
| 420 | |||
| 421 | $query = $with['query']; |
||
| 422 | if ($query instanceof QueryInterface) { |
||
| 423 | [$with['query'], $params] = $this->build($query, $params); |
||
| 424 | } |
||
| 425 | |||
| 426 | $result[] = $with['alias'] . ' AS (' . $with['query'] . ')'; |
||
| 427 | } |
||
| 428 | |||
| 429 | return 'WITH ' . ($recursive ? 'RECURSIVE ' : '') . implode(', ', $result); |
||
| 430 | } |
||
| 431 | |||
| 432 | public function createConditionFromArray(array $condition): ConditionInterface |
||
| 433 | { |
||
| 434 | /** operator format: operator, operand 1, operand 2, ... */ |
||
| 435 | if (isset($condition[0])) { |
||
| 436 | $operator = strtoupper((string) array_shift($condition)); |
||
| 437 | |||
| 438 | /** @var string $className */ |
||
| 439 | $className = $this->conditionClasses[$operator] ?? SimpleCondition::class; |
||
| 440 | |||
| 441 | /** @var ConditionInterface $className */ |
||
| 442 | return $className::fromArrayDefinition($operator, $condition); |
||
| 443 | } |
||
| 444 | |||
| 445 | /** hash format: 'column1' => 'value1', 'column2' => 'value2', ... */ |
||
| 446 | return new HashCondition($condition); |
||
| 447 | } |
||
| 448 | |||
| 449 | public function getExpressionBuilder(ExpressionInterface $expression): object |
||
| 450 | { |
||
| 451 | $className = $expression::class; |
||
| 452 | |||
| 453 | if (!isset($this->expressionBuilders[$className])) { |
||
| 454 | throw new InvalidArgumentException( |
||
| 455 | 'Expression of class ' . $className . ' can not be built in ' . static::class |
||
| 456 | ); |
||
| 457 | } |
||
| 458 | |||
| 459 | return new $this->expressionBuilders[$className]($this->queryBuilder); |
||
| 460 | } |
||
| 461 | |||
| 462 | public function selectExists(string $rawSql): string |
||
| 463 | { |
||
| 464 | return 'SELECT EXISTS(' . $rawSql . ')'; |
||
| 465 | } |
||
| 466 | |||
| 467 | public function setConditionClasses(array $classes): void |
||
| 468 | { |
||
| 469 | $this->conditionClasses = array_merge($this->conditionClasses, $classes); |
||
| 470 | } |
||
| 471 | |||
| 472 | public function setExpressionBuilders(array $builders): void |
||
| 473 | { |
||
| 474 | $this->expressionBuilders = array_merge($this->expressionBuilders, $builders); |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string the separator between different fragments of a SQL statement. |
||
| 479 | * |
||
| 480 | * Defaults to an empty space. This is mainly used by {@see build()} when generating a SQL statement. |
||
| 481 | */ |
||
| 482 | public function setSeparator(string $separator): void |
||
| 483 | { |
||
| 484 | $this->separator = $separator; |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Contains array of default condition classes. Extend this method, if you want to change default condition classes |
||
| 489 | * for the query builder. |
||
| 490 | * |
||
| 491 | * See {@see conditionClasses} docs for details. |
||
| 492 | */ |
||
| 493 | protected function defaultConditionClasses(): array |
||
| 494 | { |
||
| 495 | return [ |
||
| 496 | 'NOT' => Condition\NotCondition::class, |
||
| 497 | 'AND' => Condition\AndCondition::class, |
||
| 498 | 'OR' => Condition\OrCondition::class, |
||
| 499 | 'BETWEEN' => Condition\BetweenCondition::class, |
||
| 500 | 'NOT BETWEEN' => Condition\BetweenCondition::class, |
||
| 501 | 'IN' => Condition\InCondition::class, |
||
| 502 | 'NOT IN' => Condition\InCondition::class, |
||
| 503 | 'LIKE' => Condition\LikeCondition::class, |
||
| 504 | 'NOT LIKE' => Condition\LikeCondition::class, |
||
| 505 | 'OR LIKE' => Condition\LikeCondition::class, |
||
| 506 | 'OR NOT LIKE' => Condition\LikeCondition::class, |
||
| 507 | 'EXISTS' => Condition\ExistsCondition::class, |
||
| 508 | 'NOT EXISTS' => Condition\ExistsCondition::class, |
||
| 509 | ]; |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Contains array of default expression builders. Extend this method and override it, if you want to change default |
||
| 514 | * expression builders for this query builder. |
||
| 515 | * |
||
| 516 | * See {@see expressionBuilders} docs for details. |
||
| 517 | * |
||
| 518 | * @psalm-return array<string, class-string<ExpressionBuilderInterface>> |
||
| 519 | */ |
||
| 520 | protected function defaultExpressionBuilders(): array |
||
| 521 | { |
||
| 522 | return [ |
||
| 523 | Query::class => QueryExpressionBuilder::class, |
||
| 524 | Param::class => ParamBuilder::class, |
||
| 525 | Expression::class => ExpressionBuilder::class, |
||
| 526 | Condition\AbstractConjunctionCondition::class => Condition\Builder\ConjunctionConditionBuilder::class, |
||
| 527 | Condition\NotCondition::class => Condition\Builder\NotConditionBuilder::class, |
||
| 528 | Condition\AndCondition::class => Condition\Builder\ConjunctionConditionBuilder::class, |
||
| 529 | Condition\OrCondition::class => Condition\Builder\ConjunctionConditionBuilder::class, |
||
| 530 | Condition\BetweenCondition::class => Condition\Builder\BetweenConditionBuilder::class, |
||
| 531 | Condition\InCondition::class => Condition\Builder\InConditionBuilder::class, |
||
| 532 | Condition\LikeCondition::class => Condition\Builder\LikeConditionBuilder::class, |
||
| 533 | Condition\ExistsCondition::class => Condition\Builder\ExistsConditionBuilder::class, |
||
| 534 | Condition\SimpleCondition::class => Condition\Builder\SimpleConditionBuilder::class, |
||
| 535 | Condition\HashCondition::class => Condition\Builder\HashConditionBuilder::class, |
||
| 536 | Condition\BetweenColumnsCondition::class => Condition\Builder\BetweenColumnsConditionBuilder::class, |
||
| 537 | ]; |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Extracts table alias if there is one or returns false. |
||
| 542 | * |
||
| 543 | * @psalm-return string[]|bool |
||
| 544 | */ |
||
| 545 | protected function extractAlias(string $table): array|bool |
||
| 546 | { |
||
| 547 | if (preg_match('/^(.*?)(?i:\s+as|)\s+([^ ]+)$/', $table, $matches)) { |
||
| 548 | return $matches; |
||
| 549 | } |
||
| 550 | |||
| 551 | return false; |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Checks to see if the given limit is effective. |
||
| 556 | * |
||
| 557 | * @param mixed $limit the given limit. |
||
| 558 | * |
||
| 559 | * @return bool whether the limit is effective. |
||
| 560 | */ |
||
| 561 | protected function hasLimit(mixed $limit): bool |
||
| 562 | { |
||
| 563 | return ($limit instanceof ExpressionInterface) || ctype_digit((string) $limit); |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Checks to see if the given offset is effective. |
||
| 568 | * |
||
| 569 | * @param mixed $offset the given offset. |
||
| 570 | * |
||
| 571 | * @return bool whether the offset is effective. |
||
| 572 | */ |
||
| 573 | protected function hasOffset(mixed $offset): bool |
||
| 574 | { |
||
| 575 | return ($offset instanceof ExpressionInterface) || (ctype_digit((string)$offset) && (string)$offset !== '0'); |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Quotes table names passed. |
||
| 580 | * |
||
| 581 | * @throws Exception|InvalidConfigException|NotSupportedException |
||
| 582 | */ |
||
| 583 | private function quoteTableNames(array $tables, array &$params): array |
||
| 610 | } |
||
| 611 | } |
||
| 612 |