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\QueryBuilderInterface; |
18
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
19
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
20
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
21
|
|
|
|
22
|
|
|
use function implode; |
23
|
|
|
use function ltrim; |
24
|
|
|
use function reset; |
25
|
|
|
|
26
|
|
|
final class DMLQueryBuilder extends AbstractDMLQueryBuilder |
27
|
|
|
{ |
28
|
336 |
|
public function __construct( |
29
|
|
|
QueryBuilderInterface $queryBuilder, |
30
|
|
|
private QuoterInterface $quoter, |
31
|
|
|
private SchemaInterface $schema |
32
|
|
|
) { |
33
|
336 |
|
parent::__construct($queryBuilder, $quoter, $schema); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws Exception|Throwable |
38
|
|
|
*/ |
39
|
1 |
|
public function resetSequence(string $tableName, mixed $value = null): string |
40
|
|
|
{ |
41
|
1 |
|
$table = $this->schema->getTableSchema($tableName); |
42
|
|
|
|
43
|
1 |
|
if ($table === null) { |
44
|
|
|
throw new InvalidArgumentException("Table not found: $tableName"); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$sequenceName = $table->getSequenceName(); |
48
|
1 |
|
if ($sequenceName === null) { |
49
|
|
|
throw new InvalidArgumentException("There is not sequence associated with table '$tableName'.'"); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$tableName = $this->quoter->quoteTableName($tableName); |
53
|
1 |
|
if ($value !== null) { |
54
|
1 |
|
$value = "'" . ((int) $value - 1) . "'"; |
55
|
|
|
} else { |
56
|
1 |
|
$pk = $table->getPrimaryKey(); |
57
|
1 |
|
$key = $this->quoter->quoteColumnName(reset($pk)); |
58
|
1 |
|
$value = '(SELECT MAX(' . $key . ') FROM ' . $tableName . ')'; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return 'UPDATE sqlite_sequence SET seq=' . $value . " WHERE name='" . $table->getName() . "'"; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function truncateTable(string $table): string |
65
|
|
|
{ |
66
|
1 |
|
return 'DELETE FROM ' . $this->quoter->quoteTableName($table); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException |
71
|
|
|
*/ |
72
|
18 |
|
public function upsert( |
73
|
|
|
string $table, |
74
|
|
|
QueryInterface|array $insertColumns, |
75
|
|
|
bool|array $updateColumns, |
76
|
|
|
array &$params |
77
|
|
|
): string { |
78
|
|
|
/** @var Constraint[] $constraints */ |
79
|
18 |
|
$constraints = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @psalm-var string[] $insertNames |
83
|
|
|
* @psalm-var string[] $updateNames |
84
|
|
|
* @psalm-var array<string, ExpressionInterface|string>|bool $updateColumns |
85
|
|
|
*/ |
86
|
18 |
|
[$uniqueNames, $insertNames, $updateNames] = $this->prepareUpsertColumns( |
87
|
|
|
$table, |
88
|
|
|
$insertColumns, |
89
|
|
|
$updateColumns, |
90
|
|
|
$constraints |
91
|
|
|
); |
92
|
|
|
|
93
|
18 |
|
if (empty($uniqueNames)) { |
94
|
3 |
|
return $this->insert($table, $insertColumns, $params); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @psalm-var string[] $placeholders |
99
|
|
|
*/ |
100
|
15 |
|
[, $placeholders, $values, $params] = $this->prepareInsertValues($table, $insertColumns, $params); |
101
|
|
|
|
102
|
15 |
|
$insertSql = 'INSERT OR IGNORE INTO ' |
103
|
15 |
|
. $this->quoter->quoteTableName($table) |
104
|
15 |
|
. (!empty($insertNames) ? ' (' . implode(', ', $insertNames) . ')' : '') |
105
|
15 |
|
. (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : "$values"); |
106
|
|
|
|
107
|
15 |
|
if ($updateColumns === false) { |
|
|
|
|
108
|
5 |
|
return $insertSql; |
109
|
|
|
} |
110
|
|
|
|
111
|
10 |
|
$updateCondition = ['or']; |
112
|
10 |
|
$quotedTableName = $this->quoter->quoteTableName($table); |
113
|
|
|
|
114
|
10 |
|
foreach ($constraints as $constraint) { |
115
|
10 |
|
$constraintCondition = ['and']; |
116
|
|
|
/** @psalm-var string[] */ |
117
|
10 |
|
$columnsNames = $constraint->getColumnNames(); |
118
|
10 |
|
foreach ($columnsNames as $name) { |
119
|
10 |
|
$quotedName = $this->quoter->quoteColumnName($name); |
120
|
10 |
|
$constraintCondition[] = "$quotedTableName.$quotedName=(SELECT $quotedName FROM `EXCLUDED`)"; |
121
|
|
|
} |
122
|
10 |
|
$updateCondition[] = $constraintCondition; |
123
|
|
|
} |
124
|
|
|
|
125
|
10 |
|
if ($updateColumns === true) { |
|
|
|
|
126
|
4 |
|
$updateColumns = []; |
127
|
4 |
|
foreach ($updateNames as $name) { |
128
|
4 |
|
$quotedName = $this->quoter->quoteColumnName($name); |
129
|
|
|
|
130
|
4 |
|
if (strrpos($quotedName, '.') === false) { |
131
|
4 |
|
$quotedName = "(SELECT $quotedName FROM `EXCLUDED`)"; |
132
|
|
|
} |
133
|
4 |
|
$updateColumns[$name] = new Expression($quotedName); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** @var array $params */ |
138
|
10 |
|
$updateSql = 'WITH "EXCLUDED" (' |
139
|
10 |
|
. implode(', ', $insertNames) |
140
|
10 |
|
. ') AS (' . (!empty($placeholders) |
141
|
4 |
|
? 'VALUES (' . implode(', ', $placeholders) . ')' |
142
|
10 |
|
: ltrim("$values", ' ')) . ') ' . |
143
|
10 |
|
$this->update($table, $updateColumns, $updateCondition, $params); |
144
|
|
|
|
145
|
10 |
|
return "$updateSql; $insertSql;"; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|