|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Sqlite; |
|
6
|
|
|
|
|
7
|
|
|
use JsonException; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
use Yiisoft\Db\Constraint\Constraint; |
|
10
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
11
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
|
12
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
13
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
|
14
|
|
|
use Yiisoft\Db\Expression\Expression; |
|
15
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
|
16
|
|
|
use Yiisoft\Db\Query\DMLQueryBuilder as AbstractDMLQueryBuilder; |
|
17
|
|
|
use Yiisoft\Db\Query\Query; |
|
18
|
|
|
use Yiisoft\Db\Query\QueryBuilderInterface; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
final class DMLQueryBuilder extends AbstractDMLQueryBuilder |
|
21
|
|
|
{ |
|
22
|
|
|
public function __construct(private QueryBuilderInterface $queryBuilder) |
|
23
|
|
|
{ |
|
24
|
|
|
parent::__construct($queryBuilder); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @throws Exception|Throwable |
|
29
|
|
|
*/ |
|
30
|
|
|
public function resetSequence(string $tableName, mixed $value = null): string |
|
31
|
|
|
{ |
|
32
|
|
|
$table = $this->queryBuilder->schema()->getTableSchema($tableName); |
|
33
|
|
|
|
|
34
|
|
|
if ($table !== null && $table->getSequenceName() !== null) { |
|
35
|
|
|
$tableName = $this->queryBuilder->quoter()->quoteTableName($tableName); |
|
36
|
|
|
if ($value === null) { |
|
37
|
|
|
$pk = $table->getPrimaryKey(); |
|
38
|
|
|
$key = $this->queryBuilder->quoter()->quoteColumnName(reset($pk)); |
|
39
|
|
|
$value = $this->queryBuilder->command()->setSql("SELECT MAX($key) FROM $tableName")->queryScalar(); |
|
40
|
|
|
} else { |
|
41
|
|
|
$value = (int) $value - 1; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return "UPDATE sqlite_sequence SET seq='$value' WHERE name='{$table->getName()}'"; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($table === null) { |
|
48
|
|
|
throw new InvalidArgumentException("Table not found: $tableName"); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
throw new InvalidArgumentException("There is not sequence associated with table '$tableName'.'"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function upsert(string $table, Query|array $insertColumns, bool|array $updateColumns, array &$params): string |
|
58
|
|
|
{ |
|
59
|
|
|
/** @var Constraint[] $constraints */ |
|
60
|
|
|
$constraints = []; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @psalm-var array<array-key, string> $insertNames |
|
64
|
|
|
* @psalm-var array<array-key, string> $updateNames |
|
65
|
|
|
* @psalm-var array<string, ExpressionInterface|string>|bool $updateColumns |
|
66
|
|
|
*/ |
|
67
|
|
|
[$uniqueNames, $insertNames, $updateNames] = $this->queryBuilder->prepareUpsertColumns( |
|
68
|
|
|
$table, |
|
69
|
|
|
$insertColumns, |
|
70
|
|
|
$updateColumns, |
|
71
|
|
|
$constraints |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
if (empty($uniqueNames)) { |
|
75
|
|
|
return $this->insert($table, $insertColumns, $params); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @psalm-var array<array-key, string> $placeholders |
|
80
|
|
|
*/ |
|
81
|
|
|
[, $placeholders, $values, $params] = $this->queryBuilder->prepareInsertValues($table, $insertColumns, $params); |
|
82
|
|
|
|
|
83
|
|
|
$insertSql = 'INSERT OR IGNORE INTO ' |
|
84
|
|
|
. $this->queryBuilder->quoter()->quoteTableName($table) |
|
85
|
|
|
. (!empty($insertNames) ? ' (' . implode(', ', $insertNames) . ')' : '') |
|
86
|
|
|
. (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : "$values"); |
|
87
|
|
|
|
|
88
|
|
|
if ($updateColumns === false) { |
|
|
|
|
|
|
89
|
|
|
return $insertSql; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$updateCondition = ['or']; |
|
93
|
|
|
$quotedTableName = $this->queryBuilder->quoter()->quoteTableName($table); |
|
94
|
|
|
|
|
95
|
|
|
foreach ($constraints as $constraint) { |
|
96
|
|
|
$constraintCondition = ['and']; |
|
97
|
|
|
/** @psalm-var array<array-key, string> */ |
|
98
|
|
|
$columnsNames = $constraint->getColumnNames(); |
|
99
|
|
|
foreach ($columnsNames as $name) { |
|
100
|
|
|
$quotedName = $this->queryBuilder->quoter()->quoteColumnName($name); |
|
101
|
|
|
$constraintCondition[] = "$quotedTableName.$quotedName=(SELECT $quotedName FROM `EXCLUDED`)"; |
|
102
|
|
|
} |
|
103
|
|
|
$updateCondition[] = $constraintCondition; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($updateColumns === true) { |
|
|
|
|
|
|
107
|
|
|
$updateColumns = []; |
|
108
|
|
|
foreach ($updateNames as $name) { |
|
109
|
|
|
$quotedName = $this->queryBuilder->quoter()->quoteColumnName($name); |
|
110
|
|
|
|
|
111
|
|
|
if (strrpos($quotedName, '.') === false) { |
|
112
|
|
|
$quotedName = "(SELECT $quotedName FROM `EXCLUDED`)"; |
|
113
|
|
|
} |
|
114
|
|
|
$updateColumns[$name] = new Expression($quotedName); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$updateSql = 'WITH "EXCLUDED" (' |
|
119
|
|
|
. implode(', ', $insertNames) |
|
120
|
|
|
. ') AS (' . (!empty($placeholders) |
|
121
|
|
|
? 'VALUES (' . implode(', ', $placeholders) . ')' |
|
122
|
|
|
: ltrim("$values", ' ')) . ') ' . $this->update($table, $updateColumns, $updateCondition, $params); |
|
123
|
|
|
|
|
124
|
|
|
return "$updateSql; $insertSql;"; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths