Abstract Class Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder
Abstract expression builder for {@see StructuredValue}.
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $queryBuilder | Yiisoft\Db\QueryBuilder\QueryBuilderInterface | Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder | |
| build() | The method builds the raw SQL from the $expression that won't be additionally escaped or quoted. |
Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| buildStringValue() | Builds an SQL expression for a string value. | Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder |
| buildSubquery() | Build a structured expression from a sub-query object. | Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder |
| buildValue() | Builds an SQL expression for a structured value. | Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder |
| getLazyArrayValue() | Returns the value of the lazy array as an array or a raw string depending on the implementation. | Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder |
| prepareValues() | Returns the prepared value of the structured type, where: - object are converted to an array; - array elements are sorted according to the order of structured type columns; - indexed keys are replaced with column names; - missing elements are filled in with default values; - excessive elements are ignored. | Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder |
Property Details
Method Details
| public mixed __construct ( Yiisoft\Db\QueryBuilder\QueryBuilderInterface $queryBuilder ) | ||
| $queryBuilder | Yiisoft\Db\QueryBuilder\QueryBuilderInterface | |
public function __construct(protected readonly QueryBuilderInterface $queryBuilder) {}
The method builds the raw SQL from the $expression that won't be additionally escaped or quoted.
| public string build ( Yiisoft\Db\Expression\Value\StructuredValue $expression, array &$params = [] ) | ||
| $expression | Yiisoft\Db\Expression\Value\StructuredValue |
The expression to build. |
| $params | array |
The binding parameters. |
| return | string |
The raw SQL that won't be additionally escaped or quoted. |
|---|---|---|
public function build(ExpressionInterface $expression, array &$params = []): string
{
$value = $expression->value;
if ($value === null) {
return 'NULL';
}
if ($value instanceof LazyArrayInterface) {
$value = $this->getLazyArrayValue($value);
}
if (is_string($value)) {
return $this->buildStringValue($value, $expression, $params);
}
if ($value instanceof QueryInterface) {
return $this->buildSubquery($value, $expression, $params);
}
return $this->buildValue($value, $expression, $params);
}
Builds an SQL expression for a string value.
| protected abstract string buildStringValue ( string $value, Yiisoft\Db\Expression\Value\StructuredValue $expression, array &$params ) | ||
| $value | string |
The valid SQL string representation of the structured value. |
| $expression | Yiisoft\Db\Expression\Value\StructuredValue |
The structured expression. |
| $params | array |
The binding parameters. |
| return | string |
The SQL expression representing the structured value. |
|---|---|---|
abstract protected function buildStringValue(
string $value,
StructuredValue $expression,
array &$params,
): string;
Build a structured expression from a sub-query object.
| protected abstract string buildSubquery ( Yiisoft\Db\Query\QueryInterface $query, Yiisoft\Db\Expression\Value\StructuredValue $expression, array &$params ) | ||
| $query | Yiisoft\Db\Query\QueryInterface |
The sub-query object. |
| $expression | Yiisoft\Db\Expression\Value\StructuredValue |
The structured expression. |
| $params | array |
The binding parameters. |
| return | string |
The sub-query SQL expression representing a structured value. |
|---|---|---|
abstract protected function buildSubquery(
QueryInterface $query,
StructuredValue $expression,
array &$params,
): string;
Builds an SQL expression for a structured value.
| protected abstract string buildValue ( array|object $value, Yiisoft\Db\Expression\Value\StructuredValue $expression, array &$params ) | ||
| $value | array|object |
The structured value. |
| $expression | Yiisoft\Db\Expression\Value\StructuredValue |
The structured expression. |
| $params | array |
The binding parameters. |
| return | string |
The SQL expression representing the structured value. |
|---|---|---|
abstract protected function buildValue(
array|object $value,
StructuredValue $expression,
array &$params,
): string;
Returns the value of the lazy array as an array or a raw string depending on the implementation.
| protected abstract array|string getLazyArrayValue ( Yiisoft\Db\Schema\Data\LazyArrayInterface $value ) | ||
| $value | Yiisoft\Db\Schema\Data\LazyArrayInterface |
The lazy array value. |
| return | array|string |
The value of the lazy array. |
|---|---|---|
abstract protected function getLazyArrayValue(LazyArrayInterface $value): array|string;
Returns the prepared value of the structured type, where: - object are converted to an array; - array elements are sorted according to the order of structured type columns; - indexed keys are replaced with column names; - missing elements are filled in with default values; - excessive elements are ignored.
If the structured type columns are not specified it will only convert the object to an array.
| protected array prepareValues ( array|object $value, Yiisoft\Db\Expression\Value\StructuredValue $expression ) | ||
| $value | array|object |
The structured type value. |
| $expression | Yiisoft\Db\Expression\Value\StructuredValue |
The structured expression. |
protected function prepareValues(array|object $value, StructuredValue $expression): array
{
$value = DbArrayHelper::toArray($value);
$type = $expression->type;
$columns = $type instanceof AbstractStructuredColumn ? $type->getColumns() : [];
if (empty($columns)) {
return $value;
}
$prepared = [];
$columnNames = array_keys($columns);
foreach ($columnNames as $i => $columnName) {
$prepared[$columnName] = match (true) {
array_key_exists($columnName, $value) => $value[$columnName],
array_key_exists($i, $value) => $value[$i],
default => $columns[$columnName]->getDefaultValue(),
};
}
return $prepared;
}
Signup or Login in order to comment.