Test Failed
Pull Request — master (#258)
by Wilmer
23:34 queued 19:48
created

DMLQueryBuilder::insertWithReturningPks()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 20
ccs 8
cts 8
cp 1
crap 4
rs 9.9
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 573
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 573
            $returnColumns = $tableSchema->getPrimaryKey();
29
        }
30
31 11
        if (!empty($returnColumns)) {
32
            $returning = [];
33 11
            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 11
            }
36
            $sql .= ' RETURNING ' . implode(', ', $returning);
37 11
        }
38 11
39 11
        return $sql;
40
    }
41
42 11
    public function resetSequence(string $tableName, int|string $value = null): string
43 10
    {
44 10
        $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 10
46
        if ($table !== null && ($sequence = $table->getSequenceName()) !== null) {
47 10
            /**
48
             * @link https://www.postgresql.org/docs/8.1/static/functions-sequence.html
49
             */
50 11
            $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 5
            if ($value === null) {
54
                $pk = $table->getPrimaryKey();
55 5
                $key = $this->quoter->quoteColumnName(reset($pk));
56
                $value = "(SELECT COALESCE(MAX($key),0) FROM $tableName)+1";
57 5
            }
58
59
            return "SELECT SETVAL('$sequence',$value,false)";
60
        }
61 3
62 3
        if ($table === null) {
63
            throw new InvalidArgumentException("Table not found: '$tableName'.");
64 3
        }
65 3
66 3
        throw new InvalidArgumentException("There is not sequence associated with table '$tableName'.");
67 3
    }
68
69
    public function upsert(
70 3
        string $table,
71
        QueryInterface|array $insertColumns,
72
        $updateColumns,
73 2
        array &$params = []
74 1
    ): string {
75
        $insertSql = $this->insert($table, $insertColumns, $params);
76
77 1
        /** @psalm-var array $uniqueNames */
78
        [$uniqueNames, , $updateNames] = $this->prepareUpsertColumns(
79
            $table,
80 34
            $insertColumns,
81
            $updateColumns,
82
        );
83
84
        if (empty($uniqueNames)) {
85
            return $insertSql;
86 34
        }
87
88
        if ($updateNames === []) {
0 ignored issues
show
introduced by
The condition $updateNames === array() is always false.
Loading history...
89 34
            /** there are no columns to update */
90 34
            $updateColumns = false;
91 34
        }
92 34
93 34
        if ($updateColumns === false) {
94
            return "$insertSql ON CONFLICT DO NOTHING";
95 34
        }
96 2
97
        if ($updateColumns === true) {
98
            $updateColumns = [];
99 32
100
            /** @psalm-var string $name */
101 2
            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 32
                );
105 14
            }
106
        }
107
108 18
        /**
109 8
         * @psalm-var array $updateColumns
110
         * @psalm-var string[] $uniqueNames
111
         * @psalm-var string[] $updates
112 8
         */
113 8
        [$updates, $params] = $this->prepareUpdateSets($table, $updateColumns, $params);
114 8
115 8
        return $insertSql
116
            . ' ON CONFLICT (' . implode(', ', $uniqueNames) . ') DO UPDATE SET ' . implode(', ', $updates);
117
    }
118
}
119