Move all children of a folder in a single query

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2017-01-16 16:20:53 +01:00
parent 4279b13270
commit 4b7bc2af0e
No known key found for this signature in database
GPG Key ID: 50F2B59C6DEBBCFE
1 changed files with 26 additions and 17 deletions

View File

@ -37,6 +37,7 @@
namespace OC\Files\Cache; namespace OC\Files\Cache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use OCP\Files\Cache\ICache; use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry; use OCP\Files\Cache\ICacheEntry;
@ -130,7 +131,7 @@ class Cache implements ICache {
$where = 'WHERE `fileid` = ?'; $where = 'WHERE `fileid` = ?';
$params = array($file); $params = array($file);
} }
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, $sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
`storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum` `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum`
FROM `*PREFIX*filecache` ' . $where; FROM `*PREFIX*filecache` ' . $where;
$result = $this->connection->executeQuery($sql, $params); $result = $this->connection->executeQuery($sql, $params);
@ -522,27 +523,35 @@ class Cache implements ICache {
throw new \Exception('Invalid target storage id: ' . $targetStorageId); throw new \Exception('Invalid target storage id: ' . $targetStorageId);
} }
// sql for final update $this->connection->beginTransaction();
$moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?';
if ($sourceData['mimetype'] === 'httpd/unix-directory') { if ($sourceData['mimetype'] === 'httpd/unix-directory') {
//find all child entries //update all child entries
$sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?';
$result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']);
$childEntries = $result->fetchAll();
$sourceLength = strlen($sourcePath); $sourceLength = strlen($sourcePath);
$this->connection->beginTransaction(); $query = $this->connection->getQueryBuilder();
$query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?');
foreach ($childEntries as $child) { $fun = $query->fun();
$newTargetPath = $targetPath . substr($child['path'], $sourceLength); $newPathFunction = $fun->concat(
$query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]); $query->createNamedParameter($targetPath),
$fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))// +1 for the leading slash
);
$query->update('filecache')
->set('storage', $query->createNamedParameter($targetStorageId, IQueryBuilder::PARAM_INT))
->set('path_hash', $fun->md5($newPathFunction))
->set('path', $newPathFunction)
->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%')));
try {
$query->execute();
} catch (\Exception $e) {
$this->connection->rollBack();
throw $e;
} }
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
$this->connection->commit();
} else {
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), \OC_Util::basename($targetPath), $newParentId, $sourceId]);
} }
$sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?';
$this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), \OC_Util::basename($targetPath), $newParentId, $sourceId));
$this->connection->commit();
} else { } else {
$this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath); $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
} }