Skip to content

Commit

Permalink
Move all children of a folder in a single query
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Apr 14, 2016
1 parent f70589c commit 1c0903f
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions lib/private/files/cache/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function get($file) {
$where = 'WHERE `fileid` = ?';
$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`
FROM `*PREFIX*filecache` ' . $where;
$result = $this->connection->executeQuery($sql, $params);
Expand Down Expand Up @@ -505,27 +505,21 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath);
list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath);

// sql for final update
$moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?';

$this->connection->beginTransaction();
if ($sourceData['mimetype'] === 'httpd/unix-directory') {
//find 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();
//update all child entries
$sourceLength = strlen($sourcePath);
$this->connection->beginTransaction();
$query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?');

foreach ($childEntries as $child) {
$newTargetPath = $targetPath . substr($child['path'], $sourceLength);
$query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]);
}
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
$this->connection->commit();
} else {
$this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]);
$query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET
`storage` = ?,
`path_hash` = MD5(CONCAT(?, SUBSTR(`path`, ?))),
`path` = CONCAT(?, SUBSTR(`path`, ?))
WHERE `storage` = ? AND `path` LIKE ?');
$query->execute([$targetStorageId, $targetPath, $sourceLength + 1,$targetPath, $sourceLength + 1, $sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']);
}

$sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?';
$this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId));
$this->connection->commit();
} else {
$this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
}
Expand Down

0 comments on commit 1c0903f

Please sign in to comment.