diff options
author | Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com> | 2024-06-27 11:19:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-27 11:19:12 +0200 |
commit | 2482688fa09d7dc477ad0d23049d55b62d5b3d6c (patch) | |
tree | 6068af538f743ef4ae560e9508ac144c0806bd19 /lib | |
parent | ec39228b89363e378fc4f851c9182985aa174878 (diff) | |
parent | b7243681dd70b25fdefc9fe62c63bab840691c94 (diff) | |
download | nextcloud-server-2482688fa09d7dc477ad0d23049d55b62d5b3d6c.tar.gz nextcloud-server-2482688fa09d7dc477ad0d23049d55b62d5b3d6c.zip |
Merge pull request #45655 from nextcloud/feat/mysql_ignore_conflics
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/DB/AdapterMySQL.php | 28 | ||||
-rw-r--r-- | lib/private/DB/AdapterSqlite.php | 15 |
2 files changed, 43 insertions, 0 deletions
diff --git a/lib/private/DB/AdapterMySQL.php b/lib/private/DB/AdapterMySQL.php index 8e854769e1f..598dbc4de20 100644 --- a/lib/private/DB/AdapterMySQL.php +++ b/lib/private/DB/AdapterMySQL.php @@ -35,4 +35,32 @@ class AdapterMySQL extends Adapter { return $this->collation; } + + public function insertIgnoreConflict(string $table, array $values): int { + $builder = $this->conn->getQueryBuilder(); + $builder->insert($table); + $updates = []; + foreach ($values as $key => $value) { + $builder->setValue($key, $builder->createNamedParameter($value)); + } + + /* + * We can't use ON DUPLICATE KEY UPDATE here because Nextcloud use the CLIENT_FOUND_ROWS flag + * With this flag the MySQL returns the number of selected rows + * instead of the number of affected/modified rows + * It's impossible to change this behaviour at runtime or for a single query + * Then, the result is 1 if a row is inserted and also 1 if a row is updated with same or different values + * + * With INSERT IGNORE, the result is 1 when a row is inserted, 0 otherwise + * + * Risk: it can also ignore other errors like type mismatch or truncated data… + */ + $res = $this->conn->executeStatement( + preg_replace('/^INSERT/i', 'INSERT IGNORE', $builder->getSQL()), + $builder->getParameters(), + $builder->getParameterTypes() + ); + + return $res; + } } diff --git a/lib/private/DB/AdapterSqlite.php b/lib/private/DB/AdapterSqlite.php index e84f62e8d80..24274cbcda6 100644 --- a/lib/private/DB/AdapterSqlite.php +++ b/lib/private/DB/AdapterSqlite.php @@ -76,4 +76,19 @@ class AdapterSqlite extends Adapter { return 0; } } + + public function insertIgnoreConflict(string $table, array $values): int { + $builder = $this->conn->getQueryBuilder(); + $builder->insert($table); + $updates = []; + foreach ($values as $key => $value) { + $builder->setValue($key, $builder->createNamedParameter($value)); + } + + return $this->conn->executeStatement( + $builder->getSQL() . ' ON CONFLICT DO NOTHING', + $builder->getParameters(), + $builder->getParameterTypes() + ); + } } |