Class yii\db\pgsql\ArrayExpressionBuilder
| Inheritance | yii\ |
|---|---|
| Implements | yii\ |
| Uses Traits | yii\ |
| Available since version | 2.0.14 |
| Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/pgsql/ArrayExpressionBuilder.php |
Class ArrayExpressionBuilder builds yii\
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $queryBuilder | yii\ |
yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | ExpressionBuilderTrait constructor. | yii\ |
| build() | Method builds the raw SQL from the $expression that will not be additionally escaped or quoted. | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| buildPlaceholders() | Builds placeholders array out of $expression values | yii\ |
| buildSubqueryArray() | Build an array expression from a subquery SQL. | yii\ |
| getTypehint() | yii\ |
|
| typecastValue() | Casts $value to use in $expression | yii\ |
Method Details
Defined in:
yii\
ExpressionBuilderTrait constructor.
| public mixed __construct ( yii\ | ||
| $queryBuilder | yii\ |
|
public function __construct(QueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
}
Method builds the raw SQL from the $expression that will not be additionally escaped or quoted.
| public string build ( yii\ | ||
| $expression | yii\ |
The expression to be built |
| $params | array |
The binding parameters. |
| return | string |
The raw SQL that will not be additionally escaped or quoted. |
|---|---|---|
public function build(ExpressionInterface $expression, array &$params = [])
{
$value = $expression->getValue();
if ($value === null) {
return 'NULL';
}
if ($value instanceof Query) {
list ($sql, $params) = $this->queryBuilder->build($value, $params);
return $this->buildSubqueryArray($sql, $expression);
}
$placeholders = $this->buildPlaceholders($expression, $params);
return 'ARRAY[' . implode(', ', $placeholders) . ']' . $this->getTypehint($expression);
}
Builds placeholders array out of $expression values
| protected array buildPlaceholders ( yii\ | ||
| $expression | yii\ |
|
| $params | array |
The binding parameters. |
protected function buildPlaceholders(ExpressionInterface $expression, &$params)
{
$value = $expression->getValue();
$placeholders = [];
if ($value === null || !is_array($value) && !$value instanceof \Traversable) {
return $placeholders;
}
if ($expression->getDimension() > 1) {
foreach ($value as $item) {
$placeholders[] = $this->build($this->unnestArrayExpression($expression, $item), $params);
}
return $placeholders;
}
foreach ($value as $item) {
if ($item instanceof Query) {
list ($sql, $params) = $this->queryBuilder->build($item, $params);
$placeholders[] = $this->buildSubqueryArray($sql, $expression);
continue;
}
$item = $this->typecastValue($expression, $item);
if ($item instanceof ExpressionInterface) {
$placeholders[] = $this->queryBuilder->buildExpression($item, $params);
continue;
}
$placeholders[] = $this->queryBuilder->bindParam($item, $params);
}
return $placeholders;
}
Build an array expression from a subquery SQL.
| protected string buildSubqueryArray ( string $sql, yii\ | ||
| $sql | string |
The subquery SQL. |
| $expression | yii\ |
|
| return | string |
The subquery array expression. |
|---|---|---|
protected function buildSubqueryArray($sql, ArrayExpression $expression)
{
return 'ARRAY(' . $sql . ')' . $this->getTypehint($expression);
}
| protected string getTypehint ( yii\ | ||
| $expression | yii\ |
|
| return | string |
The typecast expression based on type. |
|---|---|---|
protected function getTypehint(ArrayExpression $expression)
{
if ($expression->getType() === null) {
return '';
}
$result = '::' . $expression->getType();
$result .= str_repeat('[]', $expression->getDimension());
return $result;
}
Casts $value to use in $expression
| protected mixed typecastValue ( yii\ | ||
| $expression | yii\ |
|
| $value | mixed | |
protected function typecastValue(ArrayExpression $expression, $value)
{
if ($value instanceof ExpressionInterface) {
return $value;
}
if (in_array($expression->getType(), [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
return new JsonExpression($value);
}
return $value;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.