Passed
Push — master ( 90bc48...a2953f )
by Wilmer
08:36 queued 04:19
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 11
    public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string
21
    {
22 11
        $sql = $this->insert($table, $columns, $params);
23
24 11
        $tableSchema = $this->schema->getTableSchema($table);
25
26 11
        $returnColumns = [];
27 11
        if ($tableSchema !== null) {
28 11
            $returnColumns = $tableSchema->getPrimaryKey();
29
        }
30
31 11
        if (!empty($returnColumns)) {
32 10
            $returning = [];
33 10
            foreach ($returnColumns as $name) {
34 10
                $returning[] = $this->quoter->quoteColumnName($name);
35
            }
36 10
            $sql .= ' RETURNING ' . implode(', ', $returning);
37
        }
38
39 11
        return $sql;
40
    }
41
42 5
    public function resetSequence(string $tableName, int|string $value = null): string
43
    {
44 5
        $table = $this->schema->getTableSchema($tableName);
45
46 5
        if ($table !== null && ($sequence = $table->getSequenceName()) !== null) {
47
            /**
48
             * @link https://www.postgresql.org/docs/8.1/static/functions-sequence.html
49
             */
50 3
            $sequence = $this->quoter->quoteTableName($sequence);
51 3
            $tableName = $this->quoter->quoteTableName($tableName);
52
53 3
            if ($value === null) {
54 3
                $pk = $table->getPrimaryKey();
55 3
                $key = $this->quoter->quoteColumnName(reset($pk));
56 3
                $value = "(SELECT COALESCE(MAX($key),0) FROM $tableName)+1";
57
            }
58
59 3
            return "SELECT SETVAL('$sequence',$value,false)";
60
        }
61
62 2
        if ($table === null) {
63 1
            throw new InvalidArgumentException("Table not found: '$tableName'.");
64
        }
65
66 1
        throw new InvalidArgumentException("There is not sequence associated with table '$tableName'.");
67
    }
68
69 34
    public function upsert(
70
        string $table,
71
        QueryInterface|array $insertColumns,
72
        $updateColumns,
73
        array &$params = []
74
    ): string {
75 34
        $insertSql = $this->insert($table, $insertColumns, $params);
76
77
        /** @psalm-var array $uniqueNames */
78 34
        [$uniqueNames, , $updateNames] = $this->prepareUpsertColumns(
79 34
            $table,
80 34
            $insertColumns,
81 34
            $updateColumns,
82 34
        );
83
84 34
        if (empty($uniqueNames)) {
85 2
            return $insertSql;
86
        }
87
88 32
        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 2
            $updateColumns = false;
91
        }
92
93 32
        if ($updateColumns === false) {
94 14
            return "$insertSql ON CONFLICT DO NOTHING";
95
        }
96
97 18
        if ($updateColumns === true) {
98 8
            $updateColumns = [];
99
100
            /** @psalm-var string $name */
101 8
            foreach ($updateNames as $name) {
0 ignored issues
show
Bug introduced by
The expression $updateNames of type null is not traversable.
Loading history...
102 8
                $updateColumns[$name] = new Expression(
103 8
                    'EXCLUDED.' . $this->quoter->quoteColumnName($name)
104 8
                );
105
            }
106
        }
107
108
        /**
109
         * @psalm-var array $updateColumns
110
         * @psalm-var string[] $uniqueNames
111
         * @psalm-var string[] $updates
112
         */
113 18
        [$updates, $params] = $this->prepareUpdateSets($table, $updateColumns, $params);
114
115 18
        return $insertSql
116 18
            . ' ON CONFLICT (' . implode(', ', $uniqueNames) . ') DO UPDATE SET ' . implode(', ', $updates);
117
    }
118
}
119