Passed
Pull Request — master (#303)
by
unknown
03:42
created

CompositeExpressionBuilder::buildPlaceholders()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 2
b 0
f 0
nc 10
nop 2
dl 0
loc 36
ccs 16
cts 16
cp 1
crap 7
rs 8.8333
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\Composite\CompositeExpression;
14
use Yiisoft\Db\Query\QueryInterface;
15
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
16
17
use function implode;
18
19
/**
20
 * Builds expressions for {@see CompositeExpression} for PostgreSQL Server.
21
 */
22
final class CompositeExpressionBuilder implements ExpressionBuilderInterface
23
{
24 12
    public function __construct(private QueryBuilderInterface $queryBuilder)
25
    {
26 12
    }
27
28
    /**
29
     * The Method builds the raw SQL from the expression that won't be additionally escaped or quoted.
30
     *
31
     * @param ExpressionInterface $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 12
    public function build(ExpressionInterface $expression, array &$params = []): string
42
    {
43 12
        if (!$expression instanceof CompositeExpression) {
44 1
            throw new \InvalidArgumentException(
45 1
                'TypeError: ' . self::class . '::build(): Argument #1 ($expression) must be instance of '
46 1
                . CompositeExpression::class . ', instance of ' . $expression::class . ' given.'
47 1
            );
48
        }
49
50
        /** @psalm-var mixed $value */
51 11
        $value = $expression->getValue();
52
53 11
        if (empty($value)) {
54 1
            return 'NULL';
55
        }
56
57 10
        if ($value instanceof QueryInterface) {
58 2
            [$sql, $params] = $this->queryBuilder->build($value, $params);
59 2
            return "($sql)" . $this->getTypeHint($expression);
60
        }
61
62
        /** @psalm-var string[] $placeholders */
63 8
        $placeholders = $this->buildPlaceholders($expression, $params);
64
65 8
        if (empty($placeholders)) {
66 1
            return 'NULL';
67
        }
68
69 7
        return 'ROW(' . implode(', ', $placeholders) . ')' . $this->getTypeHint($expression);
70
    }
71
72
    /**
73
     * Builds a placeholder array out of $expression values.
74
     *
75
     * @param array $params The binding parameters.
76
     *
77
     * @throws Exception
78
     * @throws InvalidArgumentException
79
     * @throws InvalidConfigException
80
     * @throws NotSupportedException
81
     */
82 8
    private function buildPlaceholders(CompositeExpression $expression, array &$params): array
83
    {
84 8
        $placeholders = [];
85
86
        /** @psalm-var mixed $value */
87 8
        $value = $expression->getNormalizedValue();
88
89 8
        if (!is_iterable($value)) {
90 1
            return $placeholders;
91
        }
92
93 7
        $columns = (array) $expression->getColumns();
94 7
        $columnNames = array_keys($columns);
95
96
        /**
97
         * @psalm-var int|string $columnName
98
         * @psalm-var mixed $item
99
         */
100 7
        foreach ($value as $columnName => $item) {
101 7
            if (is_int($columnName)) {
102 2
                $columnName = $columnNames[$columnName] ?? null;
103
            }
104
105 7
            if ($columnName !== null && isset($columns[$columnName])) {
106
                /** @psalm-var mixed $item */
107 2
                $item = $columns[$columnName]->dbTypecast($item);
108
            }
109
110 7
            if ($item instanceof ExpressionInterface) {
111 1
                $placeholders[] = $this->queryBuilder->buildExpression($item, $params);
112
            } else {
113 7
                $placeholders[] = $this->queryBuilder->bindParam($item, $params);
114
            }
115
        }
116
117 7
        return $placeholders;
118
    }
119
120
    /**
121
     * @return string The typecast expression based on {@see type}.
122
     */
123 9
    private function getTypeHint(CompositeExpression $expression): string
124
    {
125 9
        $type = $expression->getType();
126
127 9
        if ($type === null) {
128 5
            return '';
129
        }
130
131 4
        return '::' . $type;
132
    }
133
}
134