Test Failed
Push — change-visibility-abstractdmlq... ( 251401...461eb4 )
by Wilmer
27:36 queued 23:37
created

DMLQueryBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql;
6
7
use Yiisoft\Db\Exception\InvalidArgumentException;
8
use Yiisoft\Db\Expression\Expression;
9
use Yiisoft\Db\Query\QueryInterface;
10
use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder;
11
12
use function implode;
13
use function reset;
14
15
/**
16
 * Implements a DML (Data Manipulation Language) SQL statements for PostgreSQL Server.
17
 */
18
final class DMLQueryBuilder extends AbstractDMLQueryBuilder
19
{
20
    public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string
21
    {
22
        $sql = $this->insert($table, $columns, $params);
23
24
        $tableSchema = $this->schema->getTableSchema($table);
0 ignored issues
show
Bug introduced by
The property schema is declared private in Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder and cannot be accessed from this context.
Loading history...
25
26
        $returnColumns = [];
27
        if ($tableSchema !== null) {
28
            $returnColumns = $tableSchema->getPrimaryKey();
29
        }
30
31
        if (!empty($returnColumns)) {
32
            $returning = [];
33
            foreach ($returnColumns as $name) {
34
                $returning[] = $this->quoter->quoteColumnName($name);
0 ignored issues
show
Bug introduced by
The property quoter is declared private in Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder and cannot be accessed from this context.
Loading history...
35
            }
36
            $sql .= ' RETURNING ' . implode(', ', $returning);
37
        }
38
39
        return $sql;
40
    }
41
42
    public function resetSequence(string $tableName, int|string $value = null): string
43
    {
44
        $table = $this->schema->getTableSchema($tableName);
0 ignored issues
show
Bug introduced by
The property schema is declared private in Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder and cannot be accessed from this context.
Loading history...
45
46
        if ($table !== null && ($sequence = $table->getSequenceName()) !== null) {
47
            /**
48
             * @link https://www.postgresql.org/docs/8.1/static/functions-sequence.html
49
             */
50
            $sequence = $this->quoter->quoteTableName($sequence);
0 ignored issues
show
Bug introduced by
The property quoter is declared private in Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder and cannot be accessed from this context.
Loading history...
51
            $tableName = $this->quoter->quoteTableName($tableName);
52
53
            if ($value === null) {
54
                $pk = $table->getPrimaryKey();
55
                $key = $this->quoter->quoteColumnName(reset($pk));
56
                $value = "(SELECT COALESCE(MAX($key),0) FROM $tableName)+1";
57
            }
58
59
            return "SELECT SETVAL('$sequence',$value,false)";
60
        }
61
62
        if ($table === null) {
63
            throw new InvalidArgumentException("Table not found: '$tableName'.");
64
        }
65
66
        throw new InvalidArgumentException("There is not sequence associated with table '$tableName'.");
67
    }
68
69
    public function upsert(
70
        string $table,
71
        QueryInterface|array $insertColumns,
72
        $updateColumns,
73
        array &$params = []
74
    ): string {
75
        $insertSql = $this->insert($table, $insertColumns, $params);
76
77
        /** @psalm-var array $uniqueNames */
78
        [$uniqueNames, , $updateNames] = $this->prepareUpsertColumns(
79
            $table,
80
            $insertColumns,
81
            $updateColumns,
82
        );
83
84
        if (empty($uniqueNames)) {
85
            return $insertSql;
86
        }
87
88
        if ($updateNames === []) {
0 ignored issues
show
introduced by
The condition $updateNames === array() is always false.
Loading history...
89
            /** there are no columns to update */
90
            $updateColumns = false;
91
        }
92
93
        if ($updateColumns === false) {
94
            return "$insertSql ON CONFLICT DO NOTHING";
95
        }
96
97
        if ($updateColumns === true) {
98
            $updateColumns = [];
99
100
            /** @psalm-var string $name */
101
            foreach ($updateNames as $name) {
0 ignored issues
show
Bug introduced by
The expression $updateNames of type null is not traversable.
Loading history...
102
                $updateColumns[$name] = new Expression(
103
                    'EXCLUDED.' . $this->quoter->quoteColumnName($name)
0 ignored issues
show
Bug introduced by
The property quoter is declared private in Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder and cannot be accessed from this context.
Loading history...
104
                );
105
            }
106
        }
107
108
        /**
109
         * @psalm-var array $updateColumns
110
         * @psalm-var string[] $uniqueNames
111
         * @psalm-var string[] $updates
112
         */
113
        [$updates, $params] = $this->prepareUpdateSets($table, $updateColumns, $params);
114
115
        return $insertSql
116
            . ' ON CONFLICT (' . implode(', ', $uniqueNames) . ') DO UPDATE SET ' . implode(', ', $updates);
117
    }
118
}
119