Skip to content

Commit 770a04e

Browse files
committed
Cleanup AllConfig
- Port to QueryBuilder - More typing when possible - Import classes with 'use' Signed-off-by: Carl Schwan <[email protected]>
1 parent 3f55108 commit 770a04e

1 file changed

Lines changed: 66 additions & 70 deletions

File tree

lib/private/AllConfig.php

Lines changed: 66 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,17 @@
3333
namespace OC;
3434

3535
use OC\Cache\CappedMemoryCache;
36+
use OCP\DB\QueryBuilder\IQueryBuilder;
37+
use OCP\IConfig;
3638
use OCP\IDBConnection;
3739
use OCP\PreConditionNotMetException;
3840

3941
/**
4042
* Class to combine all the configuration options ownCloud offers
4143
*/
42-
class AllConfig implements \OCP\IConfig {
43-
/** @var SystemConfig */
44-
private $systemConfig;
45-
46-
/** @var IDBConnection */
47-
private $connection;
44+
class AllConfig implements IConfig {
45+
private SystemConfig $systemConfig;
46+
private ?IDBConnection $connection = null;
4847

4948
/**
5049
* 3 dimensional array with the following structure:
@@ -64,13 +63,10 @@ class AllConfig implements \OCP\IConfig {
6463
* - deleteAllUserValues
6564
* - deleteAppFromAllUsers
6665
*
67-
* @var CappedMemoryCache $userCache
66+
* @var CappedMemoryCache<array> $userCache
6867
*/
69-
private $userCache;
68+
private CappedMemoryCache $userCache;
7069

71-
/**
72-
* @param SystemConfig $systemConfig
73-
*/
7470
public function __construct(SystemConfig $systemConfig) {
7571
$this->userCache = new CappedMemoryCache();
7672
$this->systemConfig = $systemConfig;
@@ -91,7 +87,7 @@ public function __construct(SystemConfig $systemConfig) {
9187
*/
9288
private function fixDIInit() {
9389
if ($this->connection === null) {
94-
$this->connection = \OC::$server->getDatabaseConnection();
90+
$this->connection = \OC::$server->get(IDBConnection::class);
9591
}
9692
}
9793

@@ -195,7 +191,7 @@ public function deleteSystemValue($key) {
195191
* @return string[] the keys stored for the app
196192
*/
197193
public function getAppKeys($appName) {
198-
return \OC::$server->query(\OC\AppConfig::class)->getKeys($appName);
194+
return \OC::$server->get(AppConfig::class)->getKeys($appName);
199195
}
200196

201197
/**
@@ -206,7 +202,7 @@ public function getAppKeys($appName) {
206202
* @param string|float|int $value the value that should be stored
207203
*/
208204
public function setAppValue($appName, $key, $value) {
209-
\OC::$server->query(\OC\AppConfig::class)->setValue($appName, $key, $value);
205+
\OC::$server->get(AppConfig::class)->setValue($appName, $key, $value);
210206
}
211207

212208
/**
@@ -218,7 +214,7 @@ public function setAppValue($appName, $key, $value) {
218214
* @return string the saved value
219215
*/
220216
public function getAppValue($appName, $key, $default = '') {
221-
return \OC::$server->query(\OC\AppConfig::class)->getValue($appName, $key, $default);
217+
return \OC::$server->get(AppConfig::class)->getValue($appName, $key, $default);
222218
}
223219

224220
/**
@@ -228,7 +224,7 @@ public function getAppValue($appName, $key, $default = '') {
228224
* @param string $key the key of the value, under which it was saved
229225
*/
230226
public function deleteAppValue($appName, $key) {
231-
\OC::$server->query(\OC\AppConfig::class)->deleteKey($appName, $key);
227+
\OC::$server->get(AppConfig::class)->deleteKey($appName, $key);
232228
}
233229

234230
/**
@@ -237,7 +233,7 @@ public function deleteAppValue($appName, $key) {
237233
* @param string $appName the appName the configs are stored under
238234
*/
239235
public function deleteAppValues($appName) {
240-
\OC::$server->query(\OC\AppConfig::class)->deleteApp($appName);
236+
\OC::$server->get(AppConfig::class)->deleteApp($appName);
241237
}
242238

243239

@@ -278,7 +274,7 @@ public function setUserValue($userId, $appName, $key, $value, $preCondition = nu
278274
->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId)))
279275
->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($appName)))
280276
->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key)));
281-
$qb->execute();
277+
$qb->executeStatement();
282278

283279
$this->userCache[$userId][$appName][$key] = (string)$value;
284280
return;
@@ -354,9 +350,12 @@ public function deleteUserValue($userId, $appName, $key) {
354350
// TODO - FIXME
355351
$this->fixDIInit();
356352

357-
$sql = 'DELETE FROM `*PREFIX*preferences` '.
358-
'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
359-
$this->connection->executeUpdate($sql, [$userId, $appName, $key]);
353+
$qb = $this->connection->getQueryBuilder();
354+
$qb->delete('preferences')
355+
->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
356+
->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
357+
->where($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
358+
->executeStatement();
360359

361360
if (isset($this->userCache[$userId][$appName])) {
362361
unset($this->userCache[$userId][$appName][$key]);
@@ -371,10 +370,10 @@ public function deleteUserValue($userId, $appName, $key) {
371370
public function deleteAllUserValues($userId) {
372371
// TODO - FIXME
373372
$this->fixDIInit();
374-
375-
$sql = 'DELETE FROM `*PREFIX*preferences` '.
376-
'WHERE `userid` = ?';
377-
$this->connection->executeUpdate($sql, [$userId]);
373+
$qb = $this->connection->getQueryBuilder();
374+
$qb->delete('preferences')
375+
->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
376+
->executeStatement();
378377

379378
unset($this->userCache[$userId]);
380379
}
@@ -388,9 +387,10 @@ public function deleteAppFromAllUsers($appName) {
388387
// TODO - FIXME
389388
$this->fixDIInit();
390389

391-
$sql = 'DELETE FROM `*PREFIX*preferences` '.
392-
'WHERE `appid` = ?';
393-
$this->connection->executeUpdate($sql, [$appName]);
390+
$qb = $this->connection->getQueryBuilder();
391+
$qb->delete('preferences')
392+
->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
393+
->executeStatement();
394394

395395
foreach ($this->userCache as &$userCache) {
396396
unset($userCache[$appName]);
@@ -420,8 +420,12 @@ public function getAllUserValues(?string $userId): array {
420420
$this->fixDIInit();
421421

422422
$data = [];
423-
$query = 'SELECT `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
424-
$result = $this->connection->executeQuery($query, [$userId]);
423+
424+
$qb = $this->connection->getQueryBuilder();
425+
$result = $qb->select('appid', 'configkey', 'configvalue')
426+
->from('preferences')
427+
->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
428+
->executeQuery();
425429
while ($row = $result->fetch()) {
426430
$appId = $row['appid'];
427431
if (!isset($data[$appId])) {
@@ -450,22 +454,21 @@ public function getUserValueForUsers($appName, $key, $userIds) {
450454
}
451455

452456
$chunkedUsers = array_chunk($userIds, 50, true);
453-
$placeholders50 = implode(',', array_fill(0, 50, '?'));
457+
458+
$qb = $this->connection->getQueryBuilder();
459+
$qb->select('userid', 'configvalue')
460+
->from('preferences')
461+
->where($qb->expr()->eq('appid', $qb->createParameter('appName')))
462+
->andWhere($qb->expr()->eq('configkey', $qb->createParameter('configKey')))
463+
->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds')));
454464

455465
$userValues = [];
456466
foreach ($chunkedUsers as $chunk) {
457-
$queryParams = $chunk;
458-
// create [$app, $key, $chunkedUsers]
459-
array_unshift($queryParams, $key);
460-
array_unshift($queryParams, $appName);
461-
462-
$placeholders = (count($chunk) === 50) ? $placeholders50 : implode(',', array_fill(0, count($chunk), '?'));
463-
464-
$query = 'SELECT `userid`, `configvalue` ' .
465-
'FROM `*PREFIX*preferences` ' .
466-
'WHERE `appid` = ? AND `configkey` = ? ' .
467-
'AND `userid` IN (' . $placeholders . ')';
468-
$result = $this->connection->executeQuery($query, $queryParams);
467+
$qb = $this->connection->getQueryBuilder();
468+
$qb->setParameter('appName', $appName, IQueryBuilder::PARAM_STR);
469+
$qb->setParameter('configKey', $key, IQueryBuilder::PARAM_STR);
470+
$qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
471+
$result = $qb->executeQuery();
469472

470473
while ($row = $result->fetch()) {
471474
$userValues[$row['userid']] = $row['configvalue'];
@@ -487,19 +490,16 @@ public function getUsersForUserValue($appName, $key, $value) {
487490
// TODO - FIXME
488491
$this->fixDIInit();
489492

490-
$sql = 'SELECT `userid` FROM `*PREFIX*preferences` ' .
491-
'WHERE `appid` = ? AND `configkey` = ? ';
492-
493-
if ($this->getSystemValue('dbtype', 'sqlite') === 'oci') {
494-
//oracle hack: need to explicitly cast CLOB to CHAR for comparison
495-
$sql .= 'AND to_char(`configvalue`) = ?';
496-
} else {
497-
$sql .= 'AND `configvalue` = ?';
498-
}
499-
500-
$sql .= ' ORDER BY `userid`';
501-
502-
$result = $this->connection->executeQuery($sql, [$appName, $key, $value]);
493+
$qb = $this->connection->getQueryBuilder();
494+
$result = $qb->select('userid')
495+
->from('preferences')
496+
->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
497+
->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
498+
->andWhere($qb->expr()->eq(
499+
$qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR),
500+
$qb->createNamedParameter($value, IQueryBuilder::PARAM_STR))
501+
)->orderBy('userid')
502+
->executeQuery();
503503

504504
$userIDs = [];
505505
while ($row = $result->fetch()) {
@@ -525,20 +525,16 @@ public function getUsersForUserValueCaseInsensitive($appName, $key, $value) {
525525
// Email address is always stored lowercase in the database
526526
return $this->getUsersForUserValue($appName, $key, strtolower($value));
527527
}
528-
529-
$sql = 'SELECT `userid` FROM `*PREFIX*preferences` ' .
530-
'WHERE `appid` = ? AND `configkey` = ? ';
531-
532-
if ($this->getSystemValue('dbtype', 'sqlite') === 'oci') {
533-
//oracle hack: need to explicitly cast CLOB to CHAR for comparison
534-
$sql .= 'AND LOWER(to_char(`configvalue`)) = ?';
535-
} else {
536-
$sql .= 'AND LOWER(`configvalue`) = ?';
537-
}
538-
539-
$sql .= ' ORDER BY `userid`';
540-
541-
$result = $this->connection->executeQuery($sql, [$appName, $key, strtolower($value)]);
528+
$qb = $this->connection->getQueryBuilder();
529+
$result = $qb->select('userid')
530+
->from('preferences')
531+
->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
532+
->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
533+
->andWhere($qb->expr()->eq(
534+
$qb->func()->lower($qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)),
535+
$qb->createNamedParameter(strtolower($value), IQueryBuilder::PARAM_STR))
536+
)->orderBy('userid')
537+
->executeQuery();
542538

543539
$userIDs = [];
544540
while ($row = $result->fetch()) {

0 commit comments

Comments
 (0)