Class Yiisoft\Db\Expression\Statement\Builder\CaseXBuilder
| Inheritance | Yiisoft\Db\Expression\Statement\Builder\CaseXBuilder |
|---|---|
| Implements | Yiisoft\Db\Expression\ExpressionBuilderInterface |
Builds expressions for Yiisoft\Db\Expression\Statement\CaseX.
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $queryBuilder | Yiisoft\Db\QueryBuilder\QueryBuilderInterface | Yiisoft\Db\Expression\Statement\Builder\CaseXBuilder |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Db\Expression\Statement\Builder\CaseXBuilder | |
| build() | Builds an SQL CASE expression from the given Yiisoft\Db\Expression\Statement\CaseX object. |
Yiisoft\Db\Expression\Statement\Builder\CaseXBuilder |
Protected 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
| public __construct( Yiisoft\Db\QueryBuilder\QueryBuilderInterface $queryBuilder ): mixed | ||
| $queryBuilder | Yiisoft\Db\QueryBuilder\QueryBuilderInterface | |
public function __construct(protected readonly QueryBuilderInterface $queryBuilder) {}
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 |
| $params | array |
The parameters to be bound to the query. |
| return | string |
SQL |
|---|---|---|
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';
}
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),
};
}
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);
}
Signup or Login in order to comment.