Skip to content

Commit 3d21e9d

Browse files
committed
feat(db): add optional callable parameter to insertIgnoreConflict
So that we can add hintShardKey on the used querybuilder for instance Signed-off-by: Thomas Citharel <tcit@tcit.fr>
1 parent 63a1a08 commit 3d21e9d

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, ?callable $callback = null) : 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 ($callback !== null) {
122+
$callback($builder);
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ 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, ?callable $callback = null): int {
4040
$builder = $this->conn->getQueryBuilder();
4141
$builder->insert($table);
4242
$updates = [];
4343
foreach ($values as $key => $value) {
4444
$builder->setValue($key, $builder->createNamedParameter($value));
4545
}
4646

47+
if ($callback !== null) {
48+
$callback($builder);
49+
}
50+
4751
/*
4852
* We can't use ON DUPLICATE KEY UPDATE here because Nextcloud use the CLIENT_FOUND_ROWS flag
4953
* 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, ?callable $callback = null) : 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 ($callback !== null) {
35+
$callback($builder);
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, ?callable $callback = null): 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 ($callback !== null) {
88+
$callback($builder);
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, ?callable $callback = null) : int {
552552
try {
553-
return $this->adapter->insertIgnoreConflict($table, $values);
553+
return $this->adapter->insertIgnoreConflict($table, $values, $callback);
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, ?callable $callback = null): int {
9393
try {
94-
return $this->inner->insertIgnoreConflict($table, $values);
94+
return $this->inner->insertIgnoreConflict($table, $values, $callback);
9595
} catch (Exception $e) {
9696
throw DbalException::wrap($e);
9797
}

lib/public/IDBConnection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,11 @@ 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 null|callable(IQueryBuilder): void $callback callabale that is given
171172
* @return int number of inserted rows
172-
* @since 16.0.0
173+
* @since 16.0.0 - parameter $callback was aded in 34
173174
*/
174-
public function insertIgnoreConflict(string $table, array $values) : int;
175+
public function insertIgnoreConflict(string $table, array $values, ?callable $callback = null) : int;
175176

176177
/**
177178
* Insert or update a row value

0 commit comments

Comments
 (0)