-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathArrayExpressionBuilder.php
165 lines (138 loc) · 5.12 KB
/
ArrayExpressionBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Pgsql\Builder;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\ArrayExpression;
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use function implode;
use function in_array;
use function is_iterable;
use function str_repeat;
/**
* Builds expressions for {@see ArrayExpression} for PostgreSQL Server.
*/
final class ArrayExpressionBuilder implements ExpressionBuilderInterface
{
public function __construct(private QueryBuilderInterface $queryBuilder)
{
}
/**
* The Method builds the raw SQL from the expression that won't be additionally escaped or quoted.
*
* @param ArrayExpression $expression The expression build.
* @param array $params The binding parameters.
*
* @throws Exception
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws NotSupportedException
*
* @return string The raw SQL that won't be additionally escaped or quoted.
*/
public function build(ExpressionInterface $expression, array &$params = []): string
{
/** @psalm-var array|mixed|QueryInterface $value */
$value = $expression->getValue();
if ($value === null) {
return 'NULL';
}
if ($value instanceof QueryInterface) {
[$sql, $params] = $this->queryBuilder->build($value, $params);
return $this->buildSubqueryArray($sql, $expression);
}
/** @psalm-var string[] $placeholders */
$placeholders = $this->buildPlaceholders($expression, $params);
return 'ARRAY[' . implode(', ', $placeholders) . ']' . $this->getTypeHint($expression);
}
/**
* Builds a placeholder array out of $expression values.
*
* @param array $params The binding parameters.
*
* @throws Exception
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws NotSupportedException
*/
private function buildPlaceholders(ArrayExpression $expression, array &$params): array
{
$placeholders = [];
/** @psalm-var mixed $value */
$value = $expression->getValue();
if (!is_iterable($value)) {
return $placeholders;
}
if ($expression->getDimension() > 1) {
/** @psalm-var mixed $item */
foreach ($value as $item) {
$placeholders[] = $this->build($this->unnestArrayExpression($expression, $item), $params);
}
return $placeholders;
}
/** @psalm-var ExpressionInterface|int $item */
foreach ($value as $item) {
if ($item instanceof QueryInterface) {
[$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);
} else {
$placeholders[] = $this->queryBuilder->bindParam($item, $params);
}
}
return $placeholders;
}
private function unnestArrayExpression(ArrayExpression $expression, mixed $value): ArrayExpression
{
return new ArrayExpression($value, $expression->getType(), $expression->getDimension() - 1);
}
/**
* @return string The typecast expression based on {@see type}.
*/
private function getTypeHint(ArrayExpression $expression): string
{
$type = $expression->getType();
if ($type === null) {
return '';
}
$dimension = $expression->getDimension();
return '::' . $type . str_repeat('[]', $dimension);
}
/**
* Build an array expression from a sub-query SQL.
*
* @param string $sql The sub-query SQL.
* @param ArrayExpression $expression The array expression.
*
* @return string The sub-query array expression.
*/
private function buildSubqueryArray(string $sql, ArrayExpression $expression): string
{
return 'ARRAY(' . $sql . ')' . $this->getTypeHint($expression);
}
/**
* @return array|bool|ExpressionInterface|float|int|JsonExpression|string|null The cast value or expression.
*/
private function typecastValue(
ArrayExpression $expression,
array|bool|float|int|string|ExpressionInterface|null $value
): array|bool|float|int|string|JsonExpression|ExpressionInterface|null {
if ($value instanceof ExpressionInterface) {
return $value;
}
if (in_array($expression->getType(), ['json', 'jsonb'], true)) {
return new JsonExpression($value);
}
return $value;
}
}