|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Db\Expression; |
| 6 | + |
| 7 | +use Yiisoft\Db\Exception\Exception; |
| 8 | +use Yiisoft\Db\Exception\InvalidArgumentException; |
| 9 | +use Yiisoft\Db\Exception\InvalidConfigException; |
| 10 | +use Yiisoft\Db\Exception\NotSupportedException; |
| 11 | +use Yiisoft\Db\Helper\DbArrayHelper; |
| 12 | +use Yiisoft\Db\Query\QueryInterface; |
| 13 | +use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
| 14 | +use Yiisoft\Db\Schema\Column\AbstractStructuredColumn; |
| 15 | +use Yiisoft\Db\Schema\Data\LazyArrayInterface; |
| 16 | + |
| 17 | +use function array_key_exists; |
| 18 | +use function array_keys; |
| 19 | +use function is_string; |
| 20 | + |
| 21 | +/** |
| 22 | + * Abstract expression builder for {@see StructuredExpression}. |
| 23 | + */ |
| 24 | +abstract class AbstractStructuredExpressionBuilder implements ExpressionBuilderInterface |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Builds a SQL expression for a string value. |
| 28 | + * |
| 29 | + * @param string $value The valid SQL string representation of the structured value. |
| 30 | + * @param StructuredExpression $expression The structured expression. |
| 31 | + * @param array $params The binding parameters. |
| 32 | + * |
| 33 | + * @return string The SQL expression representing the structured value. |
| 34 | + */ |
| 35 | + abstract protected function buildStringValue( |
| 36 | + string $value, |
| 37 | + StructuredExpression $expression, |
| 38 | + array &$params |
| 39 | + ): string; |
| 40 | + |
| 41 | + /** |
| 42 | + * Build a structured expression from a sub-query object. |
| 43 | + * |
| 44 | + * @param QueryInterface $query The sub-query object. |
| 45 | + * @param StructuredExpression $expression The structured expression. |
| 46 | + * @param array $params The binding parameters. |
| 47 | + * |
| 48 | + * @return string The sub-query SQL expression representing a structured value. |
| 49 | + */ |
| 50 | + abstract protected function buildSubquery( |
| 51 | + QueryInterface $query, |
| 52 | + StructuredExpression $expression, |
| 53 | + array &$params |
| 54 | + ): string; |
| 55 | + |
| 56 | + /** |
| 57 | + * Builds a SQL expression for a structured value. |
| 58 | + * |
| 59 | + * @param array|object $value The structured value. |
| 60 | + * @param StructuredExpression $expression The structured expression. |
| 61 | + * @param array $params The binding parameters. |
| 62 | + * |
| 63 | + * @return string The SQL expression representing the structured value. |
| 64 | + */ |
| 65 | + abstract protected function buildValue( |
| 66 | + array|object $value, |
| 67 | + StructuredExpression $expression, |
| 68 | + array &$params |
| 69 | + ): string; |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the value of the lazy array as an array or a raw string depending on the implementation. |
| 73 | + * |
| 74 | + * @param LazyArrayInterface $value The lazy array value. |
| 75 | + * |
| 76 | + * @return array|string The value of the lazy array. |
| 77 | + */ |
| 78 | + abstract protected function getLazyArrayValue(LazyArrayInterface $value): array|string; |
| 79 | + |
| 80 | + public function __construct(protected readonly QueryBuilderInterface $queryBuilder) |
| 81 | + { |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * The method builds the raw SQL from the `$expression` that won't be additionally escaped or quoted. |
| 86 | + * |
| 87 | + * @param StructuredExpression $expression The expression to build. |
| 88 | + * @param array $params The binding parameters. |
| 89 | + * |
| 90 | + * @throws Exception |
| 91 | + * @throws InvalidArgumentException |
| 92 | + * @throws InvalidConfigException |
| 93 | + * @throws NotSupportedException |
| 94 | + * |
| 95 | + * @return string The raw SQL that won't be additionally escaped or quoted. |
| 96 | + */ |
| 97 | + public function build(ExpressionInterface $expression, array &$params = []): string |
| 98 | + { |
| 99 | + $value = $expression->getValue(); |
| 100 | + |
| 101 | + if ($value === null) { |
| 102 | + return 'NULL'; |
| 103 | + } |
| 104 | + |
| 105 | + if ($value instanceof LazyArrayInterface) { |
| 106 | + $value = $this->getLazyArrayValue($value); |
| 107 | + } |
| 108 | + |
| 109 | + if (is_string($value)) { |
| 110 | + return $this->buildStringValue($value, $expression, $params); |
| 111 | + } |
| 112 | + |
| 113 | + if ($value instanceof QueryInterface) { |
| 114 | + return $this->buildSubquery($value, $expression, $params); |
| 115 | + } |
| 116 | + |
| 117 | + return $this->buildValue($value, $expression, $params); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns the prepared value of the structured type, where: |
| 122 | + * - object are converted to an array; |
| 123 | + * - array elements are sorted according to the order of structured type columns; |
| 124 | + * - indexed keys are replaced with column names; |
| 125 | + * - missing elements are filled in with default values; |
| 126 | + * - excessive elements are ignored. |
| 127 | + * |
| 128 | + * If the structured type columns are not specified it will only convert the object to an array. |
| 129 | + * |
| 130 | + * @param array|object $value The structured type value. |
| 131 | + * @param StructuredExpression $expression The structured expression. |
| 132 | + */ |
| 133 | + protected function prepareValues(array|object $value, StructuredExpression $expression): array |
| 134 | + { |
| 135 | + $value = DbArrayHelper::toArray($value); |
| 136 | + |
| 137 | + $type = $expression->getType(); |
| 138 | + $columns = $type instanceof AbstractStructuredColumn ? $type->getColumns() : []; |
| 139 | + |
| 140 | + if (empty($columns)) { |
| 141 | + return $value; |
| 142 | + } |
| 143 | + |
| 144 | + $prepared = []; |
| 145 | + $columnNames = array_keys($columns); |
| 146 | + |
| 147 | + foreach ($columnNames as $i => $columnName) { |
| 148 | + $prepared[$columnName] = match (true) { |
| 149 | + array_key_exists($columnName, $value) => $value[$columnName], |
| 150 | + array_key_exists($i, $value) => $value[$i], |
| 151 | + default => $columns[$columnName]->getDefaultValue(), |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | + return $prepared; |
| 156 | + } |
| 157 | +} |
0 commit comments