Final Class Yiisoft\Db\Expression\Function\Builder\LongestBuilder
| Inheritance | Yiisoft\Db\Expression\Function\Builder\LongestBuilder » Yiisoft\Db\Expression\Function\Builder\MultiOperandFunctionBuilder |
|---|---|
| Implements | Yiisoft\Db\Expression\ExpressionBuilderInterface |
Builds SQL representation of function expressions which returns the longest string from a set of operands.
(SELECT value FROM (
SELECT "column1" AS value
UNION
SELECT "column2" AS value
) AS t ORDER BY LENGTH(value) DESC LIMIT 1)
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $queryBuilder | Yiisoft\Db\QueryBuilder\QueryBuilderInterface | Yiisoft\Db\Expression\Function\Builder\MultiOperandFunctionBuilder |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Db\Expression\Function\Builder\MultiOperandFunctionBuilder | |
| build() | Builds a SQL multi-operand function expression from the given {@see MultiOperandFunction} instance. | Yiisoft\Db\Expression\Function\Builder\MultiOperandFunctionBuilder |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| buildFromExpression() | Builds a SQL expression to represent the function which returns the longest string. | Yiisoft\Db\Expression\Function\Builder\LongestBuilder |
| buildOperand() | Builds an operand expression of the multi-operand function. | Yiisoft\Db\Expression\Function\Builder\MultiOperandFunctionBuilder |
Method Details
| public mixed __construct ( Yiisoft\Db\QueryBuilder\QueryBuilderInterface $queryBuilder ) | ||
| $queryBuilder | Yiisoft\Db\QueryBuilder\QueryBuilderInterface | |
public function __construct(protected readonly QueryBuilderInterface $queryBuilder) {}
Defined in: Yiisoft\Db\Expression\Function\Builder\MultiOperandFunctionBuilder::build()
Builds a SQL multi-operand function expression from the given {@see MultiOperandFunction} instance.
| public string build ( Yiisoft\Db\Expression\Function\MultiOperandFunction $expression, array &$params = [] ) | ||
| $expression | Yiisoft\Db\Expression\Function\MultiOperandFunction |
The expression to build. |
| $params | array |
The parameters to be bound to the query. |
| return | string |
SQL multi-operand function expression. |
|---|---|---|
public function build(ExpressionInterface $expression, array &$params = []): string
{
$operands = $expression->getOperands();
if (empty($operands)) {
throw new InvalidArgumentException(
'The ' . $expression::class . ' expression must have at least one operand.',
);
}
if (count($operands) === 1) {
return '(' . $this->buildOperand($operands[0], $params) . ')';
}
return $this->buildFromExpression($expression, $params);
}
Builds a SQL expression to represent the function which returns the longest string.
| protected string buildFromExpression ( Yiisoft\Db\Expression\Function\Greatest $expression, array &$params ) | ||
| $expression | Yiisoft\Db\Expression\Function\Greatest |
The expression to build. |
| $params | array |
The parameters to bind. |
| return | string |
The SQL expression. |
|---|---|---|
protected function buildFromExpression(MultiOperandFunction $expression, array &$params): string
{
$selects = [];
foreach ($expression->getOperands() as $operand) {
$selects[] = 'SELECT ' . $this->buildOperand($operand, $params) . ' AS value';
}
$unions = implode(' UNION ', $selects);
return "(SELECT value FROM ($unions) AS t ORDER BY LENGTH(value) DESC LIMIT 1)";
}
Defined in: Yiisoft\Db\Expression\Function\Builder\MultiOperandFunctionBuilder::buildOperand()
Builds an operand expression of the multi-operand function.
| protected string buildOperand ( mixed $operand, array &$params ) | ||
| $operand | mixed | |
| $params | array | |
protected function buildOperand(mixed $operand, array &$params): string
{
if (is_string($operand)) {
return $this->queryBuilder->getQuoter()->quoteColumnName($operand);
}
return $this->queryBuilder->buildValue($operand, $params);
}
Signup or Login in order to comment.