Passed
Pull Request — master (#303)
by Sergei
03:50
created

CompositeExpressionBuilder::build()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 4
nop 2
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
c 1
b 0
f 0
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 11
    public function __construct(private QueryBuilderInterface $queryBuilder)
25
    {
26 11
    }
27
28
    /**
29
     * The method builds the raw SQL from the expression that won't be additionally escaped or quoted.
30
     *
31
     * @param CompositeExpression $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 11
    public function build(ExpressionInterface $expression, array &$params = []): string
42
    {
43
        /** @psalm-var mixed $value */
44 11
        $value = $expression->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Yiisoft\Db\Expression\ExpressionInterface. It seems like you code against a sub-type of Yiisoft\Db\Expression\ExpressionInterface such as Yiisoft\Db\Expression\ArrayExpression or Yiisoft\Db\Expression\JsonExpression or Yiisoft\Db\Command\Param or Yiisoft\Db\Pgsql\Composite\CompositeExpression or Yiisoft\Db\QueryBuilder\...lumnsConditionInterface or Yiisoft\Db\QueryBuilder\...impleConditionInterface or Yiisoft\Db\QueryBuilder\...\LikeConditionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        /** @scrutinizer ignore-call */ 
45
        $value = $expression->getValue();
Loading history...
45
46 11
        if (empty($value)) {
47 1
            return 'NULL';
48
        }
49
50 10
        if ($value instanceof QueryInterface) {
51 2
            [$sql, $params] = $this->queryBuilder->build($value, $params);
52 2
            return "($sql)" . $this->getTypeHint($expression);
53
        }
54
55
        /** @psalm-var string[] $placeholders */
56 8
        $placeholders = $this->buildPlaceholders($expression, $params);
57
58 8
        if (empty($placeholders)) {
59 1
            return 'NULL';
60
        }
61
62 7
        return 'ROW(' . implode(', ', $placeholders) . ')' . $this->getTypeHint($expression);
63
    }
64
65
    /**
66
     * Builds a placeholder array out of $expression values.
67
     *
68
     * @param array $params The binding parameters.
69
     *
70
     * @throws Exception
71
     * @throws InvalidArgumentException
72
     * @throws InvalidConfigException
73
     * @throws NotSupportedException
74
     */
75 8
    private function buildPlaceholders(CompositeExpression $expression, array &$params): array
76
    {
77 8
        $placeholders = [];
78
79
        /** @psalm-var mixed $value */
80 8
        $value = $expression->getNormalizedValue();
81
82 8
        if (!is_iterable($value)) {
83 1
            return $placeholders;
84
        }
85
86 7
        $columns = (array) $expression->getColumns();
87
88
        /**
89
         * @psalm-var int|string $columnName
90
         * @psalm-var mixed $item
91
         */
92 7
        foreach ($value as $columnName => $item) {
93 7
            if (isset($columns[$columnName])) {
94
                /** @psalm-var mixed $item */
95 2
                $item = $columns[$columnName]->dbTypecast($item);
96
            }
97
98 7
            if ($item instanceof ExpressionInterface) {
99 1
                $placeholders[] = $this->queryBuilder->buildExpression($item, $params);
100
            } else {
101 7
                $placeholders[] = $this->queryBuilder->bindParam($item, $params);
102
            }
103
        }
104
105 7
        return $placeholders;
106
    }
107
108
    /**
109
     * @return string The typecast expression based on {@see type}.
110
     */
111 9
    private function getTypeHint(CompositeExpression $expression): string
112
    {
113 9
        $type = $expression->getType();
114
115 9
        if ($type === null) {
116 5
            return '';
117
        }
118
119 4
        return '::' . $type;
120
    }
121
}
122