Skip to content

Commit a5b46cb

Browse files
committed
feat(db): add optional hintShardKey parameter to insertIgnoreConflict
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
1 parent def4450 commit a5b46cb

7 files changed

Lines changed: 25 additions & 11 deletions

File tree

lib/private/DB/Adapter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,16 @@ public function insertIfNotExist($table, $input, ?array $compare = null) {
111111
/**
112112
* @throws \OCP\DB\Exception
113113
*/
114-
public function insertIgnoreConflict(string $table, array $values) : int {
114+
public function insertIgnoreConflict(string $table, array $values, array $hintShardKey = []) : int {
115115
try {
116116
$builder = $this->conn->getQueryBuilder();
117117
$builder->insert($table);
118118
foreach ($values as $key => $value) {
119119
$builder->setValue($key, $builder->createNamedParameter($value));
120120
}
121+
if (isset($hintShardKey['column'], $hintShardKey['value'])) {
122+
$builder->hintShardKey($hintShardKey['column'], $hintShardKey['value'], $hintShardKey['overwrite'] ?? false);
123+
}
121124
return $builder->executeStatement();
122125
} catch (DbalException $e) {
123126
if ($e->getReason() === \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {

lib/private/DB/AdapterMySQL.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ protected function getCollation(): string {
3636
return $this->collation;
3737
}
3838

39-
public function insertIgnoreConflict(string $table, array $values): int {
39+
public function insertIgnoreConflict(string $table, array $values, array $hintShardKey = []): int {
4040
$builder = $this->conn->getQueryBuilder();
4141
$builder->insert($table);
42-
$updates = [];
4342
foreach ($values as $key => $value) {
4443
$builder->setValue($key, $builder->createNamedParameter($value));
4544
}
4645

46+
if (isset($hintShardKey['column'], $hintShardKey['value'])) {
47+
$builder->hintShardKey($hintShardKey['column'], $hintShardKey['value'], $hintShardKey['overwrite'] ?? false);
48+
}
49+
4750
/*
4851
* We can't use ON DUPLICATE KEY UPDATE here because Nextcloud use the CLIENT_FOUND_ROWS flag
4952
* With this flag the MySQL returns the number of selected rows

lib/private/DB/AdapterPgSql.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ public function fixupStatement($statement) {
2323
return $statement;
2424
}
2525

26-
public function insertIgnoreConflict(string $table, array $values) : int {
26+
public function insertIgnoreConflict(string $table, array $values, array $hintShardKey = []) : int {
2727
// "upsert" is only available since PgSQL 9.5, but the generic way
2828
// would leave error logs in the DB.
2929
$builder = $this->conn->getQueryBuilder();
3030
$builder->insert($table);
3131
foreach ($values as $key => $value) {
3232
$builder->setValue($key, $builder->createNamedParameter($value));
3333
}
34+
if (isset($hintShardKey['column'], $hintShardKey['value'])) {
35+
$builder->hintShardKey($hintShardKey['column'], $hintShardKey['value'], $hintShardKey['overwrite'] ?? false);
36+
}
3437
$queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING';
3538
return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes());
3639
}

lib/private/DB/AdapterSqlite.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,17 @@ public function insertIfNotExist($table, $input, ?array $compare = null) {
7777
}
7878
}
7979

80-
public function insertIgnoreConflict(string $table, array $values): int {
80+
public function insertIgnoreConflict(string $table, array $values, array $hintShardKey = []): int {
8181
$builder = $this->conn->getQueryBuilder();
8282
$builder->insert($table);
83-
$updates = [];
8483
foreach ($values as $key => $value) {
8584
$builder->setValue($key, $builder->createNamedParameter($value));
8685
}
8786

87+
if (isset($hintShardKey['column'], $hintShardKey['value'])) {
88+
$builder->hintShardKey($hintShardKey['column'], $hintShardKey['value'], $hintShardKey['overwrite'] ?? false);
89+
}
90+
8891
return $this->conn->executeStatement(
8992
$builder->getSQL() . ' ON CONFLICT DO NOTHING',
9093
$builder->getParameters(),

lib/private/DB/Connection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,9 @@ public function insertIfNotExist($table, $input, ?array $compare = null) {
548548
}
549549
}
550550

551-
public function insertIgnoreConflict(string $table, array $values) : int {
551+
public function insertIgnoreConflict(string $table, array $values, array $hintShardKey = []) : int {
552552
try {
553-
return $this->adapter->insertIgnoreConflict($table, $values);
553+
return $this->adapter->insertIgnoreConflict($table, $values, $hintShardKey);
554554
} catch (\Exception $e) {
555555
$this->logDatabaseException($e);
556556
throw $e;

lib/private/DB/ConnectionAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public function insertIfNotExist(string $table, array $input, ?array $compare =
8989
}
9090
}
9191

92-
public function insertIgnoreConflict(string $table, array $values): int {
92+
public function insertIgnoreConflict(string $table, array $values, array $hintShardKey = []): int {
9393
try {
94-
return $this->inner->insertIgnoreConflict($table, $values);
94+
return $this->inner->insertIgnoreConflict($table, $values, $hintShardKey);
9595
} catch (Exception $e) {
9696
throw DbalException::wrap($e);
9797
}

lib/public/IDBConnection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@ public function insertIfNotExist(string $table, array $input, ?array $compare =
168168
*
169169
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
170170
* @param array $values data that should be inserted into the table (column name => value)
171+
* @param array{column: string, value: mixed, overwrite?: bool}|array{} $hintShardKey An array representing the shard key to hint
171172
* @return int number of inserted rows
173+
* @since 34.0.0 Parameter $hintShardKey was added
172174
* @since 16.0.0
173175
*/
174-
public function insertIgnoreConflict(string $table, array $values) : int;
176+
public function insertIgnoreConflict(string $table, array $values, array $hintShardKey = []) : int;
175177

176178
/**
177179
* Insert or update a row value

0 commit comments

Comments
 (0)