diff options
author | Morris Jobke <hey@morrisjobke.de> | 2019-03-21 21:18:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-21 21:18:32 +0100 |
commit | f5c0ea852e564b1ef18607883c6080db51b72c3b (patch) | |
tree | ec6bd44e6594a7b9f68091cc2ccc8e6ecb9d3e4f | |
parent | 5564a302fe97f243f68b8e676fec52b875aaf6ca (diff) | |
parent | f889beae7eb3c55a28a6d9798a7aadfd0f2336ad (diff) | |
download | nextcloud-server-f5c0ea852e564b1ef18607883c6080db51b72c3b.tar.gz nextcloud-server-f5c0ea852e564b1ef18607883c6080db51b72c3b.zip |
Merge pull request #13721 from oole/bug/12729_postgresql_duplicate_key_value
Issue 12729 postgresql duplicate key value
-rw-r--r-- | lib/private/DB/Adapter.php | 16 | ||||
-rw-r--r-- | lib/private/DB/AdapterPgSql.php | 13 | ||||
-rw-r--r-- | lib/private/DB/Connection.php | 4 | ||||
-rw-r--r-- | lib/private/Lock/DBLockingProvider.php | 17 | ||||
-rw-r--r-- | lib/public/IDBConnection.php | 14 |
5 files changed, 52 insertions, 12 deletions
diff --git a/lib/private/DB/Adapter.php b/lib/private/DB/Adapter.php index b9a5f272c57..5c822dda080 100644 --- a/lib/private/DB/Adapter.php +++ b/lib/private/DB/Adapter.php @@ -126,4 +126,20 @@ class Adapter { return 0; } } + + /** + * @suppress SqlInjectionChecker + */ + public function insertIgnoreConflict(string $table,array $values) : int { + try { + $builder = $this->conn->getQueryBuilder(); + $builder->insert($table); + foreach($values as $key => $value) { + $builder->setValue($key, $builder->createNamedParameter($value)); + } + return $builder->execute(); + } catch(UniqueConstraintViolationException $e) { + return 0; + } + } } diff --git a/lib/private/DB/AdapterPgSql.php b/lib/private/DB/AdapterPgSql.php index 47ef4dff037..af1978d051c 100644 --- a/lib/private/DB/AdapterPgSql.php +++ b/lib/private/DB/AdapterPgSql.php @@ -35,4 +35,17 @@ class AdapterPgSql extends Adapter { $statement = str_ireplace( 'UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement ); return $statement; } + + /** + * @suppress SqlInjectionChecker + */ + public function insertIgnoreConflict(string $table,array $values) : int { + $builder = $this->conn->getQueryBuilder(); + $builder->insert($table); + foreach($values as $key => $value) { + $builder->setValue($key, $builder->createNamedParameter($value)); + } + $queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING'; + return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes()); + } } diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php index c63ef0067c1..506f4bcd4c9 100644 --- a/lib/private/DB/Connection.php +++ b/lib/private/DB/Connection.php @@ -257,6 +257,10 @@ class Connection extends ReconnectWrapper implements IDBConnection { return $this->adapter->insertIfNotExist($table, $input, $compare); } + public function insertIgnoreConflict(string $table, array $values) : int { + return $this->adapter->insertIgnoreConflict($table, $values); + } + private function getType($value) { if (is_bool($value)) { return IQueryBuilder::PARAM_BOOL; diff --git a/lib/private/Lock/DBLockingProvider.php b/lib/private/Lock/DBLockingProvider.php index 6d9c8f1b3d1..bd76385d85d 100644 --- a/lib/private/Lock/DBLockingProvider.php +++ b/lib/private/Lock/DBLockingProvider.php @@ -131,20 +131,13 @@ class DBLockingProvider extends AbstractLockingProvider { * @param int $lock * @return int number of inserted rows */ - protected function initLockField(string $path, int $lock = 0): int { $expire = $this->getExpireTime(); - - try { - $builder = $this->connection->getQueryBuilder(); - return $builder->insert('file_locks') - ->setValue('key', $builder->createNamedParameter($path)) - ->setValue('lock', $builder->createNamedParameter($lock)) - ->setValue('ttl', $builder->createNamedParameter($expire)) - ->execute(); - } catch(UniqueConstraintViolationException $e) { - return 0; - } + return $this->connection->insertIgnoreConflict('file_locks', [ + 'key' => $path, + 'lock' => $lock, + 'ttl' => $expire + ]); } /** diff --git a/lib/public/IDBConnection.php b/lib/public/IDBConnection.php index b3abe464845..4c66896a2fe 100644 --- a/lib/public/IDBConnection.php +++ b/lib/public/IDBConnection.php @@ -120,6 +120,20 @@ interface IDBConnection { */ public function insertIfNotExist($table, $input, array $compare = null); + + /** + * + * Insert a row if the row does not exist. Eventual conflicts during insert will be ignored. + * + * Implementation is not fully finished and should not be used! + * + * @param string $table The table name (will replace *PREFIX* with the actual prefix) + * @param array $values data that should be inserted into the table (column name => value) + * @return int number of inserted rows + * @since 16.0.0 + */ + public function insertIgnoreConflict(string $table,array $values) : int; + /** * Insert or update a row value * |