Use query builder instead of OC_DB in OC\Files\*

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-11-06 12:06:24 +01:00 committed by backportbot[bot]
parent 20885847c8
commit adcc942553
3 changed files with 52 additions and 26 deletions

View File

@ -52,12 +52,19 @@ class HomeCache extends Cache {
}
if ($entry && $entry['mimetype'] === 'httpd/unix-directory') {
$id = $entry['fileid'];
$sql = 'SELECT SUM(`size`) AS f1 ' .
'FROM `*PREFIX*filecache` ' .
'WHERE `parent` = ? AND `storage` = ? AND `size` >= 0';
$result = \OC_DB::executeAudited($sql, [$id, $this->getNumericStorageId()]);
if ($row = $result->fetchRow()) {
$result->closeCursor();
$query = $this->connection->getQueryBuilder();
$query->selectAlias($query->func()->sum('size'), 'f1')
->from('filecache')
->where($query->expr()->eq('parent', $query->createNamedParameter($id)))
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($this->getNumericStorageId())))
->andWhere($query->expr()->gte('size', $query->createNamedParameter(0)));
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
if ($row) {
list($sum) = array_values($row);
$totalSize = 0 + $sum;
$entry['size'] += 0;

View File

@ -126,9 +126,14 @@ class Storage {
* @return string|null either the storage id string or null if the numeric id is not known
*/
public static function getStorageId($numericId) {
$sql = 'SELECT `id` FROM `*PREFIX*storages` WHERE `numeric_id` = ?';
$result = \OC_DB::executeAudited($sql, [$numericId]);
if ($row = $result->fetchRow()) {
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->select('id')
->from('storages')
->where($query->expr()->eq('numeric_id', $query->createNamedParameter($numericId)));
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
if ($row) {
return $row['id'];
} else {
return null;
@ -170,9 +175,14 @@ class Storage {
* @param int $delay amount of seconds to delay reconsidering that storage further
*/
public function setAvailability($isAvailable, int $delay = 0) {
$sql = 'UPDATE `*PREFIX*storages` SET `available` = ?, `last_checked` = ? WHERE `id` = ?';
$available = $isAvailable ? 1 : 0;
\OC_DB::executeAudited($sql, [$available, time() + $delay, $this->storageId]);
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->update('storages')
->set('available', $query->createNamedParameter($available))
->set('last_checked', $query->createNamedParameter(time() + $delay))
->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
$query->execute();
}
/**
@ -193,12 +203,17 @@ class Storage {
public static function remove($storageId) {
$storageId = self::adjustStorageId($storageId);
$numericId = self::getNumericStorageId($storageId);
$sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?';
\OC_DB::executeAudited($sql, [$storageId]);
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('storages')
->where($query->expr()->eq('id', $query->createNamedParameter($storageId)));
$query->execute();
if (!is_null($numericId)) {
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?';
\OC_DB::executeAudited($sql, [$numericId]);
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('filecache')
->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
$query->execute();
}
}
}

View File

@ -206,19 +206,19 @@ class ScannerTest extends \Test\TestCase {
public function testBackgroundScanNestedIncompleteFolders() {
$this->storage->mkdir('folder');
$this->scanner->backgroundScan();
$this->storage->mkdir('folder/subfolder1');
$this->storage->mkdir('folder/subfolder2');
$this->storage->mkdir('folder/subfolder1/subfolder3');
$this->cache->put('folder', ['size' => -1]);
$this->cache->put('folder/subfolder1', ['size' => -1]);
// do a scan to get the folders into the cache.
$this->scanner->backgroundScan();
$this->assertTrue($this->cache->inCache('folder/subfolder1/subfolder3'));
$this->storage->file_put_contents('folder/subfolder1/bar1.txt', 'foobar');
$this->storage->file_put_contents('folder/subfolder1/subfolder3/bar3.txt', 'foobar');
$this->storage->file_put_contents('folder/subfolder2/bar2.txt', 'foobar');
@ -227,7 +227,7 @@ class ScannerTest extends \Test\TestCase {
$this->cache->put('folder/subfolder1', ['size' => -1]);
$this->cache->put('folder/subfolder2', ['size' => -1]);
$this->cache->put('folder/subfolder1/subfolder3', ['size' => -1]);
$this->scanner->backgroundScan();
$this->assertTrue($this->cache->inCache('folder/subfolder1/bar1.txt'));
@ -343,9 +343,11 @@ class ScannerTest extends \Test\TestCase {
$this->assertTrue($this->cache->inCache('folder/bar.txt'));
$oldFolderId = $this->cache->getId('folder');
// delete the folder without removing the childs
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
\OC_DB::executeAudited($sql, [$oldFolderId]);
// delete the folder without removing the children
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('filecache')
->where($query->expr()->eq('fileid', $query->createNamedParameter($oldFolderId)));
$query->execute();
$cachedData = $this->cache->get('folder/bar.txt');
$this->assertEquals($oldFolderId, $cachedData['parent']);
@ -367,9 +369,11 @@ class ScannerTest extends \Test\TestCase {
$this->assertTrue($this->cache->inCache('folder/bar.txt'));
$oldFolderId = $this->cache->getId('folder');
// delete the folder without removing the childs
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
\OC_DB::executeAudited($sql, [$oldFolderId]);
// delete the folder without removing the children
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('filecache')
->where($query->expr()->eq('fileid', $query->createNamedParameter($oldFolderId)));
$query->execute();
$cachedData = $this->cache->get('folder/bar.txt');
$this->assertEquals($oldFolderId, $cachedData['parent']);