1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mysql; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Exception\Exception; |
8
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
9
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
10
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
11
|
|
|
use Yiisoft\Db\Expression\Expression; |
12
|
|
|
use Yiisoft\Db\Query\Query; |
13
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
14
|
|
|
use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder; |
15
|
|
|
|
16
|
|
|
use function implode; |
17
|
|
|
use function str_replace; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Implements a DML (Data Manipulation Language) SQL statements for MySQL, MariaDB. |
21
|
|
|
*/ |
22
|
|
|
final class DMLQueryBuilder extends AbstractDMLQueryBuilder |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @throws Exception |
26
|
|
|
* @throws NotSupportedException |
27
|
|
|
*/ |
28
|
1 |
|
public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string |
29
|
|
|
{ |
30
|
1 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by Mysql.'); |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
public function resetSequence(string $table, int|string $value = null): string |
34
|
|
|
{ |
35
|
4 |
|
$tableSchema = $this->schema->getTableSchema($table); |
36
|
|
|
|
37
|
4 |
|
if ($tableSchema === null) { |
38
|
1 |
|
throw new InvalidArgumentException("Table not found: '$table'."); |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
$sequenceName = $tableSchema->getSequenceName(); |
42
|
3 |
|
if ($sequenceName === null) { |
43
|
1 |
|
throw new InvalidArgumentException("There is not sequence associated with table '$table'."); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
$tableName = $this->quoter->quoteTableName($table); |
47
|
|
|
|
48
|
2 |
|
if ($value !== null) { |
49
|
1 |
|
return 'ALTER TABLE ' . $tableName . ' AUTO_INCREMENT=' . $value . ';'; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
$key = $tableSchema->getPrimaryKey()[0]; |
53
|
|
|
|
54
|
2 |
|
return "SET @new_autoincrement_value := (SELECT MAX(`$key`) + 1 FROM $tableName); |
55
|
2 |
|
SET @sql = CONCAT('ALTER TABLE $tableName AUTO_INCREMENT =', @new_autoincrement_value); |
56
|
|
|
PREPARE autoincrement_stmt FROM @sql; |
57
|
2 |
|
EXECUTE autoincrement_stmt"; |
58
|
|
|
} |
59
|
|
|
|
60
|
36 |
|
public function upsert( |
61
|
|
|
string $table, |
62
|
|
|
QueryInterface|array $insertColumns, |
63
|
|
|
bool|array $updateColumns, |
64
|
|
|
array &$params |
65
|
|
|
): string { |
66
|
36 |
|
$insertSql = $this->insert($table, $insertColumns, $params); |
67
|
|
|
|
68
|
36 |
|
[$uniqueNames, , $updateNames] = $this->prepareUpsertColumns($table, $insertColumns, $updateColumns); |
69
|
|
|
|
70
|
36 |
|
if (empty($uniqueNames)) { |
71
|
2 |
|
return $insertSql; |
72
|
|
|
} |
73
|
|
|
|
74
|
34 |
|
if ($updateColumns === true) { |
|
|
|
|
75
|
12 |
|
$updateColumns = []; |
76
|
|
|
/** @psalm-var string[] $updateNames */ |
77
|
12 |
|
foreach ($updateNames as $quotedName) { |
78
|
10 |
|
$updateColumns[$quotedName] = new Expression('VALUES(' . $quotedName . ')'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
34 |
|
if (empty($updateColumns)) { |
83
|
14 |
|
return str_replace('INSERT INTO', 'INSERT IGNORE INTO', $insertSql); |
84
|
|
|
} |
85
|
|
|
|
86
|
20 |
|
[$updates, $params] = $this->prepareUpdateSets($table, $updateColumns, $params); |
87
|
|
|
|
88
|
20 |
|
return $insertSql . ' ON DUPLICATE KEY UPDATE ' . implode(', ', $updates); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Prepares a `VALUES` part for an `INSERT` SQL statement. |
93
|
|
|
* |
94
|
|
|
* @param string $table The table to insert new rows into. |
95
|
|
|
* @param array|QueryInterface $columns The column data (name => value) to insert into the table or instance of |
96
|
|
|
* {@see Query} to perform `INSERT INTO ... SELECT` SQL statement. |
97
|
|
|
* @param array $params The binding parameters that will be generated by this method. |
98
|
|
|
* They should be bound to the DB command later. |
99
|
|
|
* |
100
|
|
|
* @throws Exception |
101
|
|
|
* @throws InvalidArgumentException |
102
|
|
|
* @throws InvalidConfigException |
103
|
|
|
* @throws NotSupportedException |
104
|
|
|
* |
105
|
|
|
* @return array Array of quoted column names, placeholders, values, and params. |
106
|
|
|
* @psalm-return array{0: string[], 1: string[], 2: string, 3: array} |
107
|
|
|
*/ |
108
|
87 |
|
protected function prepareInsertValues(string $table, QueryInterface|array $columns, array $params = []): array |
109
|
|
|
{ |
110
|
87 |
|
if (empty($columns)) { |
111
|
5 |
|
return [[], [], 'VALUES ()', []]; |
112
|
|
|
} |
113
|
|
|
|
114
|
82 |
|
return parent::prepareInsertValues($table, $columns, $params); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|