summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2018-11-12 16:52:48 +0100
committerGitHub <noreply@github.com>2018-11-12 16:52:48 +0100
commitdcf68617845c6098da8ea347f2dc23ffbf73e06e (patch)
tree920e397031804df95a0b7f99cc525e2c791f3bd8
parent237c70d3cdd9dd87ef7594c4015fdfa7151cb5e5 (diff)
parent93c62d78db7847078727eafd3d8e40836a575cec (diff)
downloadnextcloud-server-dcf68617845c6098da8ea347f2dc23ffbf73e06e.tar.gz
nextcloud-server-dcf68617845c6098da8ea347f2dc23ffbf73e06e.zip
Merge pull request #12411 from nextcloud/bugfix/6160/oc_filecache-unique-constraint
Fix UniqueConstraintViolationException while insert into oc_filecache
-rw-r--r--lib/private/Files/Cache/Cache.php25
1 files changed, 18 insertions, 7 deletions
diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php
index 007bccf0a54..1cb11c70a40 100644
--- a/lib/private/Files/Cache/Cache.php
+++ b/lib/private/Files/Cache/Cache.php
@@ -37,6 +37,7 @@
namespace OC\Files\Cache;
+use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Driver\Statement;
use OCP\Files\Cache\ICache;
@@ -238,6 +239,8 @@ class Cache implements ICache {
*
* @return int file id
* @throws \RuntimeException
+ *
+ * @suppress SqlInjectionChecker
*/
public function insert($file, array $data) {
// normalize file
@@ -268,12 +271,20 @@ class Cache implements ICache {
return trim($item, "`");
}, $queryParts);
$values = array_combine($queryParts, $params);
- if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
- 'storage',
- 'path_hash',
- ])
- ) {
- return (int)$this->connection->lastInsertId('*PREFIX*filecache');
+
+ try {
+ $builder = $this->connection->getQueryBuilder();
+ $builder->insert('filecache');
+
+ foreach ($values as $column => $value) {
+ $builder->setValue($column, $builder->createNamedParameter($value));
+ }
+
+ if ($builder->execute()) {
+ return (int)$this->connection->lastInsertId('*PREFIX*filecache');
+ }
+ } catch(UniqueConstraintViolationException $e) {
+ // entry exists already
}
// The file was created in the mean time
@@ -281,7 +292,7 @@ class Cache implements ICache {
$this->update($id, $data);
return $id;
} else {
- throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.');
+ throw new \RuntimeException('File entry could not be inserted but could also not be selected with getId() in order to perform an update. Please try again.');
}
}