0 follower

Abstract Class Yiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder

InheritanceYiisoft\Db\Expression\Value\Builder\AbstractStructuredValueBuilder
ImplementsYiisoft\Db\Expression\ExpressionBuilderInterface
SubclassesYiisoft\Db\Expression\Value\Builder\StructuredValueBuilder

Abstract expression builder for {@see StructuredValue}.

Public Methods

Hide inherited 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

Hide inherited 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

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Db\QueryBuilder\QueryBuilderInterface $queryBuilder )
$queryBuilder Yiisoft\Db\QueryBuilder\QueryBuilderInterface

                public function __construct(protected readonly QueryBuilderInterface $queryBuilder) {}

            
build() public method

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);
}

            
buildStringValue() protected abstract method

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;

            
buildSubquery() protected abstract method

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;

            
buildValue() protected abstract method

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;

            
getLazyArrayValue() protected abstract method

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;

            
prepareValues() protected method

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;
}