1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yiisoft\Db\Pgsql\Builder; |
||
6 | |||
7 | use Yiisoft\Db\Exception\Exception; |
||
8 | use Yiisoft\Db\Exception\InvalidArgumentException; |
||
9 | use Yiisoft\Db\Exception\InvalidConfigException; |
||
10 | use Yiisoft\Db\Exception\NotSupportedException; |
||
11 | use Yiisoft\Db\Expression\ExpressionBuilderInterface; |
||
12 | use Yiisoft\Db\Expression\ExpressionInterface; |
||
13 | use Yiisoft\Db\Pgsql\StructuredExpression; |
||
14 | use Yiisoft\Db\Query\QueryInterface; |
||
15 | use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
||
16 | |||
17 | use function implode; |
||
18 | |||
19 | /** |
||
20 | * Builds expressions for {@see StructuredExpression} for PostgreSQL Server. |
||
21 | */ |
||
22 | final class StructuredExpressionBuilder implements ExpressionBuilderInterface |
||
23 | { |
||
24 | public function __construct(private QueryBuilderInterface $queryBuilder) |
||
25 | { |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * The method builds the raw SQL from the expression that won't be additionally escaped or quoted. |
||
30 | * |
||
31 | * @param StructuredExpression $expression The expression build. |
||
32 | * @param array $params The binding parameters. |
||
33 | * |
||
34 | * @throws Exception |
||
35 | * @throws InvalidArgumentException |
||
36 | * @throws InvalidConfigException |
||
37 | * @throws NotSupportedException |
||
38 | * |
||
39 | * @return string The raw SQL that won't be additionally escaped or quoted. |
||
40 | */ |
||
41 | public function build(ExpressionInterface $expression, array &$params = []): string |
||
42 | { |
||
43 | $value = $expression->getValue(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
44 | |||
45 | if (empty($value)) { |
||
46 | return 'NULL'; |
||
47 | } |
||
48 | |||
49 | if ($value instanceof QueryInterface) { |
||
50 | [$sql, $params] = $this->queryBuilder->build($value, $params); |
||
51 | return "($sql)" . $this->getTypeHint($expression); |
||
52 | } |
||
53 | |||
54 | /** @psalm-var string[] $placeholders */ |
||
55 | $placeholders = $this->buildPlaceholders($expression, $params); |
||
56 | |||
57 | if (empty($placeholders)) { |
||
58 | return 'NULL'; |
||
59 | } |
||
60 | |||
61 | return 'ROW(' . implode(', ', $placeholders) . ')' . $this->getTypeHint($expression); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Builds a placeholder array out of $expression values. |
||
66 | * |
||
67 | * @param array $params The binding parameters. |
||
68 | * |
||
69 | * @throws Exception |
||
70 | * @throws InvalidArgumentException |
||
71 | * @throws InvalidConfigException |
||
72 | * @throws NotSupportedException |
||
73 | */ |
||
74 | private function buildPlaceholders(StructuredExpression $expression, array &$params): array |
||
75 | { |
||
76 | $value = $expression->getNormalizedValue(); |
||
77 | |||
78 | if (!is_iterable($value)) { |
||
79 | return []; |
||
80 | } |
||
81 | |||
82 | $placeholders = []; |
||
83 | $columns = $expression->getColumns(); |
||
84 | |||
85 | /** @psalm-var int|string $columnName */ |
||
86 | foreach ($value as $columnName => $item) { |
||
87 | if (isset($columns[$columnName])) { |
||
88 | $item = $columns[$columnName]->dbTypecast($item); |
||
89 | } |
||
90 | |||
91 | if ($item instanceof ExpressionInterface) { |
||
92 | $placeholders[] = $this->queryBuilder->buildExpression($item, $params); |
||
93 | } else { |
||
94 | $placeholders[] = $this->queryBuilder->bindParam($item, $params); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $placeholders; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return string The typecast expression based on {@see type}. |
||
103 | */ |
||
104 | private function getTypeHint(StructuredExpression $expression): string |
||
105 | { |
||
106 | $type = $expression->getType(); |
||
107 | |||
108 | if ($type === null) { |
||
109 | return ''; |
||
110 | } |
||
111 | |||
112 | return '::' . $type; |
||
113 | } |
||
114 | } |
||
115 |