0 follower

Class Yiisoft\Db\Expression\Statement\Builder\CaseXBuilder

InheritanceYiisoft\Db\Expression\Statement\Builder\CaseXBuilder
ImplementsYiisoft\Db\Expression\ExpressionBuilderInterface

Builds expressions for Yiisoft\Db\Expression\Statement\CaseX.

Protected Methods

Hide inherited methods

Method Description Defined By
buildCaseValue() Builds the case value part of the CASE expression based on their type. Yiisoft\Db\Expression\Statement\Builder\CaseXBuilder
buildCondition() Builds the condition part of the CASE expression based on their type. Yiisoft\Db\Expression\Statement\Builder\CaseXBuilder

Property Details

Method Details

Hide inherited methods

__construct() public method

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

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

            
build() public method

Builds an SQL CASE expression from the given Yiisoft\Db\Expression\Statement\CaseX object.

public build( Yiisoft\Db\Expression\Statement\CaseX $expression, array &$params = [] ): string
$expression Yiisoft\Db\Expression\Statement\CaseX

The CASE expression to build.

$params array

The parameters to be bound to the query.

return string

SQL CASE expression.

                public function build(ExpressionInterface $expression, array &$params = []): string
{
    $sql = 'CASE';
    if ($expression->value !== null) {
        $sql .= ' ' . $this->buildCaseValue($expression->value, $params);
    }
    foreach ($expression->whenThen as $whenThen) {
        $sql .= ' WHEN ' . $this->buildCondition($whenThen->when, $params);
        $sql .= ' THEN ' . $this->queryBuilder->buildValue($whenThen->then, $params);
    }
    if ($expression->hasElse()) {
        $sql .= ' ELSE ' . $this->queryBuilder->buildValue($expression->else, $params);
    }
    return $sql . ' END';
}

            
buildCaseValue() protected method

Builds the case value part of the CASE expression based on their type.

protected buildCaseValue( mixed $value, array &$params ): string
$value mixed
$params array
return string

The SQL condition string.

                protected function buildCaseValue(mixed $value, array &$params): string
{
    /**
     * @var string
     * @psalm-suppress MixedArgument
     */
    return match (gettype($value)) {
        GettypeResult::ARRAY => $this->queryBuilder->buildCondition($value, $params),
        GettypeResult::STRING => $this->queryBuilder->getQuoter()->quoteColumnName($value),
        default => $this->queryBuilder->buildValue($value, $params),
    };
}

            
buildCondition() protected method

Builds the condition part of the CASE expression based on their type.

protected buildCondition( mixed $condition, array &$params ): string
$condition mixed
$params array
return string

The SQL condition string.

                protected function buildCondition(mixed $condition, array &$params): string
{
    if (is_array($condition)) {
        return $this->queryBuilder->buildCondition($condition, $params);
    }
    return $this->queryBuilder->buildValue($condition, $params);
}