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
parent f5be2c722a
commit c07720de77
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
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') { if ($entry && $entry['mimetype'] === 'httpd/unix-directory') {
$id = $entry['fileid']; $id = $entry['fileid'];
$sql = 'SELECT SUM(`size`) AS f1 ' .
'FROM `*PREFIX*filecache` ' . $query = $this->connection->getQueryBuilder();
'WHERE `parent` = ? AND `storage` = ? AND `size` >= 0'; $query->selectAlias($query->func()->sum('size'), 'f1')
$result = \OC_DB::executeAudited($sql, [$id, $this->getNumericStorageId()]); ->from('filecache')
if ($row = $result->fetchRow()) { ->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(); $result->closeCursor();
if ($row) {
list($sum) = array_values($row); list($sum) = array_values($row);
$totalSize = 0 + $sum; $totalSize = 0 + $sum;
$entry['size'] += 0; $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 * @return string|null either the storage id string or null if the numeric id is not known
*/ */
public static function getStorageId($numericId) { public static function getStorageId($numericId) {
$sql = 'SELECT `id` FROM `*PREFIX*storages` WHERE `numeric_id` = ?'; $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$result = \OC_DB::executeAudited($sql, [$numericId]); $query->select('id')
if ($row = $result->fetchRow()) { ->from('storages')
->where($query->expr()->eq('numeric_id', $query->createNamedParameter($numericId)));
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
if ($row) {
return $row['id']; return $row['id'];
} else { } else {
return null; return null;
@ -170,9 +175,14 @@ class Storage {
* @param int $delay amount of seconds to delay reconsidering that storage further * @param int $delay amount of seconds to delay reconsidering that storage further
*/ */
public function setAvailability($isAvailable, int $delay = 0) { public function setAvailability($isAvailable, int $delay = 0) {
$sql = 'UPDATE `*PREFIX*storages` SET `available` = ?, `last_checked` = ? WHERE `id` = ?';
$available = $isAvailable ? 1 : 0; $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) { public static function remove($storageId) {
$storageId = self::adjustStorageId($storageId); $storageId = self::adjustStorageId($storageId);
$numericId = self::getNumericStorageId($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)) { if (!is_null($numericId)) {
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?'; $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
\OC_DB::executeAudited($sql, [$numericId]); $query->delete('filecache')
->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
$query->execute();
} }
} }
} }

View File

@ -343,9 +343,11 @@ class ScannerTest extends \Test\TestCase {
$this->assertTrue($this->cache->inCache('folder/bar.txt')); $this->assertTrue($this->cache->inCache('folder/bar.txt'));
$oldFolderId = $this->cache->getId('folder'); $oldFolderId = $this->cache->getId('folder');
// delete the folder without removing the childs // delete the folder without removing the children
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
\OC_DB::executeAudited($sql, [$oldFolderId]); $query->delete('filecache')
->where($query->expr()->eq('fileid', $query->createNamedParameter($oldFolderId)));
$query->execute();
$cachedData = $this->cache->get('folder/bar.txt'); $cachedData = $this->cache->get('folder/bar.txt');
$this->assertEquals($oldFolderId, $cachedData['parent']); $this->assertEquals($oldFolderId, $cachedData['parent']);
@ -367,9 +369,11 @@ class ScannerTest extends \Test\TestCase {
$this->assertTrue($this->cache->inCache('folder/bar.txt')); $this->assertTrue($this->cache->inCache('folder/bar.txt'));
$oldFolderId = $this->cache->getId('folder'); $oldFolderId = $this->cache->getId('folder');
// delete the folder without removing the childs // delete the folder without removing the children
$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
\OC_DB::executeAudited($sql, [$oldFolderId]); $query->delete('filecache')
->where($query->expr()->eq('fileid', $query->createNamedParameter($oldFolderId)));
$query->execute();
$cachedData = $this->cache->get('folder/bar.txt'); $cachedData = $this->cache->get('folder/bar.txt');
$this->assertEquals($oldFolderId, $cachedData['parent']); $this->assertEquals($oldFolderId, $cachedData['parent']);