Test Failed
Pull Request — master (#169)
by Def
05:50 queued 01:57
created

DMLQueryBuilder::resetSequence()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
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
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 21
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use JsonException;
8
use Yiisoft\Db\Constraint\Constraint;
9
use Yiisoft\Db\Exception\Exception;
10
use Yiisoft\Db\Exception\InvalidArgumentException;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Exception\NotSupportedException;
13
use Yiisoft\Db\Expression\Expression;
14
use Yiisoft\Db\Expression\ExpressionInterface;
15
use Yiisoft\Db\QueryBuilder\DMLQueryBuilder as AbstractDMLQueryBuilder;
16
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
17
use Yiisoft\Db\Query\QueryInterface;
18
use Yiisoft\Db\Schema\QuoterInterface;
19
use Yiisoft\Db\Schema\SchemaInterface;
20
21
use function implode;
22
use function in_array;
23
use function ltrim;
24
use function strrpos;
25
use function is_array;
26
27
final class DMLQueryBuilder extends AbstractDMLQueryBuilder
28
{
29 503
    public function __construct(
30
        private QueryBuilderInterface $queryBuilder,
31
        private QuoterInterface $quoter,
32
        private SchemaInterface $schema
33
    ) {
34 503
        parent::__construct($queryBuilder, $quoter, $schema);
35
    }
36
37
    /**
38
     * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
39
     */
40 10
    public function insertEx(string $table, QueryInterface|array $columns, array &$params = []): string
41
    {
42
        /**
43
         * @psalm-var string[] $names
44
         * @psalm-var string[] $placeholders
45
         */
46 10
        [$names, $placeholders, $values, $params] = $this->prepareInsertValues($table, $columns, $params);
47
48
49 10
        $createdCols = $insertedCols = [];
50 10
        $tableSchema = $this->schema->getTableSchema($table);
51 10
        $returnColumns = $tableSchema?->getColumns() ?? [];
52 10
        foreach ($returnColumns as $returnColumn) {
53 10
            if ($returnColumn->isComputed()) {
54 1
                continue;
55
            }
56
57 10
            $dbType = $returnColumn->getDbType();
58 10
            if (in_array($dbType, ['char', 'varchar', 'nchar', 'nvarchar', 'binary', 'varbinary'])) {
59 1
                $dbType .= '(MAX)';
60
            }
61 10
            if ($returnColumn->getDbType() === Schema::TYPE_TIMESTAMP) {
62 3
                $dbType = $returnColumn->isAllowNull() ? 'varbinary(8)' : 'binary(8)';
63
            }
64
65 10
            $quotedName = $this->quoter->quoteColumnName($returnColumn->getName());
66 10
            $createdCols[] = $quotedName . ' ' . $dbType . ' ' . ($returnColumn->isAllowNull() ? 'NULL' : '');
67 10
            $insertedCols[] = 'INSERTED.' . $quotedName;
68
        }
69
70 10
        $sql = 'INSERT INTO '
71 10
            . $this->quoter->quoteTableName($table)
72 10
            . (!empty($names) ? ' (' . implode(', ', $names) . ')' : '')
73 10
            . ' OUTPUT ' . implode(',', $insertedCols) . ' INTO @temporary_inserted'
74 10
            . (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : (string) $values);
75
76
77 10
        return 'SET NOCOUNT ON;DECLARE @temporary_inserted TABLE (' . implode(', ', $createdCols) . ');'
78 10
            . $sql . ';SELECT * FROM @temporary_inserted;';
79
    }
80
81
    /**
82
     * @throws InvalidArgumentException
83
     */
84 4
    public function resetSequence(string $tableName, int|string $value = null): string
85
    {
86 4
        $table = $this->schema->getTableSchema($tableName);
87
88 4
        if ($table === null) {
89 1
            throw new InvalidArgumentException("Table not found: '$tableName'.");
90
        }
91
92 3
        $sequenceName = $table->getSequenceName();
93
94 3
        if ($sequenceName === null) {
95 1
            throw new InvalidArgumentException("There is not sequence associated with table '$tableName'.'");
96
        }
97
98 2
        $tableName = $this->quoter->quoteTableName($tableName);
99
100 2
        if ($value === null) {
101 2
            return "DBCC CHECKIDENT ('$tableName', RESEED, 0) WITH NO_INFOMSGS;DBCC CHECKIDENT ('$tableName', RESEED)";
102
        }
103
104 1
        return "DBCC CHECKIDENT ('$tableName', RESEED, $value)";
105
    }
106
107
    /**
108
     * @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException
109
     */
110 35
    public function upsert(
111
        string $table,
112
        QueryInterface|array $insertColumns,
113
        bool|array $updateColumns,
114
        array &$params = []
115
    ): string {
116 35
        /** @psalm-var Constraint[] $constraints */
117 22
        $constraints = [];
118
119
        /** @psalm-var string[] $insertNames */
120 35
        [$uniqueNames, $insertNames, $updateNames] = $this->prepareUpsertColumns(
121 11
            $table,
122
            $insertColumns,
123
            $updateColumns,
124
            $constraints
125 35
        );
126
127
        if (empty($uniqueNames)) {
128 35
            return $this->insert($table, $insertColumns, $params);
129 35
        }
130 35
131 35
        $onCondition = ['or'];
132 35
        $quotedTableName = $this->quoter->quoteTableName($table);
133 35
134
        foreach ($constraints as $constraint) {
135 35
            $constraintCondition = ['and'];
136 2
137
            $columnNames = $constraint->getColumnNames() ?? [];
138
139 33
            if (is_array($columnNames)) {
140 33
                /** @psalm-var string[] $columnNames */
141
                foreach ($columnNames as $name) {
142 33
                    $quotedName = $this->quoter->quoteColumnName($name);
143 33
                    $constraintCondition[] = "$quotedTableName.$quotedName=[EXCLUDED].$quotedName";
144
                }
145 33
            }
146
147 33
            $onCondition[] = $constraintCondition;
148
        }
149 33
150 33
        $on = $this->queryBuilder->buildCondition($onCondition, $params);
151 33
152
        /** @psalm-var string[] $placeholders */
153
        [, $placeholders, $values, $params] = $this->prepareInsertValues($table, $insertColumns, $params);
154
        $mergeSql = 'MERGE ' . $this->quoter->quoteTableName($table) . ' WITH (HOLDLOCK) '
155 33
            . 'USING (' . (!empty($placeholders)
156
            ? 'VALUES (' . implode(', ', $placeholders) . ')'
157
            : ltrim((string) $values, ' ')) . ') AS [EXCLUDED] (' . implode(', ', $insertNames) . ') ' . "ON ($on)";
158 33
        $insertValues = [];
159
160
        foreach ($insertNames as $name) {
161 33
            $quotedName = $this->quoter->quoteColumnName($name);
162 33
163 33
            if (strrpos($quotedName, '.') === false) {
164 20
                $quotedName = '[EXCLUDED].' . $quotedName;
165 33
            }
166 33
167
            $insertValues[] = $quotedName;
168 33
        }
169 33
170
        $insertSql = 'INSERT (' . implode(', ', $insertNames) . ')' . ' VALUES (' . implode(', ', $insertValues) . ')';
171 33
172 33
        if ($updateNames === []) {
0 ignored issues
show
introduced by
The condition $updateNames === array() is always false.
Loading history...
173
            /** there are no columns to update */
174
            $updateColumns = false;
175 33
        }
176
177
        if ($updateColumns === false) {
0 ignored issues
show
introduced by
The condition $updateColumns === false is always false.
Loading history...
178 33
            return "$mergeSql WHEN NOT MATCHED THEN $insertSql;";
179
        }
180 33
181
        if ($updateColumns === true) {
0 ignored issues
show
introduced by
The condition $updateColumns === true is always false.
Loading history...
182 2
            $updateColumns = [];
183
184
            /** @psalm-var string[] $updateNames */
185 33
            foreach ($updateNames as $name) {
186 14
                $quotedName = $this->quoter->quoteColumnName($name);
187
                if (strrpos($quotedName, '.') === false) {
188
                    $quotedName = '[EXCLUDED].' . $quotedName;
189 19
                }
190 8
191
                $updateColumns[$name] = new Expression($quotedName);
192
            }
193 8
        }
194 8
195 8
        /**
196 8
         * @var array $params
197
         * @psalm-var string[] $updates
198
         * @psalm-var array<string, ExpressionInterface|string> $updateColumns
199 8
         */
200
        [$updates, $params] = $this->prepareUpdateSets($table, $updateColumns, $params);
201
        $updateSql = 'UPDATE SET ' . implode(', ', $updates);
202
203
        return "$mergeSql WHEN MATCHED THEN $updateSql WHEN NOT MATCHED THEN $insertSql;";
204
    }
205
}
206