]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use query builder instead of OC_DB in OC\Files\* 23998/head
authorJoas Schilling <coding@schilljs.com>
Fri, 6 Nov 2020 11:06:24 +0000 (12:06 +0100)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Mon, 9 Nov 2020 11:01:09 +0000 (11:01 +0000)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Files/Cache/HomeCache.php
lib/private/Files/Cache/Storage.php
tests/lib/Files/Cache/ScannerTest.php

index b86a31fe4d16ca0b0fff9e0fbf38a1b1aed183f4..e66ed7d972298133d2eee6b10316b40bc44ef775 100644 (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;
index 62228e162908e4df9167af5aede3d082955ba71c..b11cd5799de61312db0b4ac5fb50538da7cfad6c 100644 (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();
                }
        }
 }
index f9c05d52ee58d81fb96eab8390ba2c02f2797b98..29e619cd09bf55acc36d2d5f9158dc67361b7332 100644 (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']);