diff options
author | Joas Schilling <coding@schilljs.com> | 2018-10-19 16:44:28 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2018-11-08 15:44:45 +0100 |
commit | bb352fb667e87ea0829f1da5f9e85c34bdefe9fa (patch) | |
tree | cd243856dcfec5708528418b55269e3d7feed53a /lib | |
parent | 78fd8ab0fd362fd5d568dfc5b47a02158e62d51c (diff) | |
download | nextcloud-server-bb352fb667e87ea0829f1da5f9e85c34bdefe9fa.tar.gz nextcloud-server-bb352fb667e87ea0829f1da5f9e85c34bdefe9fa.zip |
Use the defined func()->count() instead of manual counting
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/base.php | 4 | ||||
-rw-r--r-- | lib/private/Comments/Manager.php | 9 | ||||
-rw-r--r-- | lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php | 5 | ||||
-rw-r--r-- | lib/private/Files/Cache/Propagator.php | 8 | ||||
-rw-r--r-- | lib/private/Files/Type/Loader.php | 2 | ||||
-rw-r--r-- | lib/private/Group/Database.php | 2 | ||||
-rw-r--r-- | lib/private/Lock/DBLockingProvider.php | 2 | ||||
-rw-r--r-- | lib/private/SystemTag/SystemTagObjectMapper.php | 2 | ||||
-rw-r--r-- | lib/private/User/Manager.php | 4 | ||||
-rw-r--r-- | lib/public/DB/QueryBuilder/IFunctionBuilder.php | 5 |
10 files changed, 21 insertions, 22 deletions
diff --git a/lib/base.php b/lib/base.php index 762646fa4c1..90fa5496200 100644 --- a/lib/base.php +++ b/lib/base.php @@ -312,7 +312,7 @@ class OC { if ($apps->isInstalled('user_ldap')) { $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $result = $qb->selectAlias($qb->createFunction('COUNT(*)'), 'user_count') + $result = $qb->select($qb->func()->count('*', 'user_count')) ->from('ldap_user_mapping') ->execute(); $row = $result->fetch(); @@ -323,7 +323,7 @@ class OC { if (!$tooBig && $apps->isInstalled('user_saml')) { $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $result = $qb->selectAlias($qb->createFunction('COUNT(*)'), 'user_count') + $result = $qb->select($qb->func()->count('*', 'user_count')) ->from('user_saml_users') ->execute(); $row = $result->fetch(); diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index e9bb001f77d..f3865c6504f 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -163,7 +163,7 @@ class Manager implements ICommentsManager { */ protected function updateChildrenInformation($id, \DateTime $cDateTime) { $qb = $this->dbConn->getQueryBuilder(); - $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) + $query = $qb->select($qb->func()->count('id')) ->from('comments') ->where($qb->expr()->eq('parent_id', $qb->createParameter('id'))) ->setParameter('id', $id); @@ -552,7 +552,7 @@ class Manager implements ICommentsManager { */ public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null, $verb = '') { $qb = $this->dbConn->getQueryBuilder(); - $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) + $query = $qb->select($qb->func()->count('id')) ->from('comments') ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) @@ -585,10 +585,7 @@ class Manager implements ICommentsManager { public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) { $qb = $this->dbConn->getQueryBuilder(); $query = $qb->select('f.fileid') - ->selectAlias( - $qb->createFunction('COUNT(' . $qb->getColumnName('c.id') . ')'), - 'num_ids' - ) + ->addSelect($qb->func()->count('c.id', 'num_ids')) ->from('comments', 'c') ->innerJoin('c', 'filecache', 'f', $qb->expr()->andX( $qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')), diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php index cf6183b0238..a9d2f6f9a35 100644 --- a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php +++ b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php @@ -72,7 +72,8 @@ class FunctionBuilder implements IFunctionBuilder { return new QueryFunction($this->helper->quoteColumnName($x) . ' - ' . $this->helper->quoteColumnName($y)); } - public function count($input) { - return new QueryFunction('COUNT(' . $this->helper->quoteColumnName($input) . ')'); + public function count($count, $alias = '') { + $alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : ''; + return new QueryFunction('COUNT(' . $this->helper->quoteColumnName($count) . ')' . $alias); } } diff --git a/lib/private/Files/Cache/Propagator.php b/lib/private/Files/Cache/Propagator.php index 321fd7a66e8..ae51b2e52d8 100644 --- a/lib/private/Files/Cache/Propagator.php +++ b/lib/private/Files/Cache/Propagator.php @@ -82,7 +82,7 @@ class Propagator implements IPropagator { }, $parentHashes); $builder->update('filecache') - ->set('mtime', $builder->createFunction('GREATEST(`mtime`, ' . $builder->createNamedParameter((int)$time, IQueryBuilder::PARAM_INT) . ')')) + ->set('mtime', $builder->createFunction('GREATEST(' . $builder->getColumnName('mtime') . ', ' . $builder->createNamedParameter((int)$time, IQueryBuilder::PARAM_INT) . ')')) ->set('etag', $builder->createNamedParameter($etag, IQueryBuilder::PARAM_STR)) ->where($builder->expr()->eq('storage', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))) ->andWhere($builder->expr()->in('path_hash', $hashParams)); @@ -93,7 +93,7 @@ class Propagator implements IPropagator { // we need to do size separably so we can ignore entries with uncalculated size $builder = $this->connection->getQueryBuilder(); $builder->update('filecache') - ->set('size', $builder->createFunction('`size` + ' . $builder->createNamedParameter($sizeDifference))) + ->set('size', $builder->func()->add('size', $builder->createNamedParameter($sizeDifference))) ->where($builder->expr()->eq('storage', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))) ->andWhere($builder->expr()->in('path_hash', $hashParams)) ->andWhere($builder->expr()->gt('size', $builder->expr()->literal(-1, IQueryBuilder::PARAM_INT))); @@ -156,14 +156,14 @@ class Propagator implements IPropagator { $storageId = (int)$this->storage->getStorageCache()->getNumericId(); $query->update('filecache') - ->set('mtime', $query->createFunction('GREATEST(`mtime`, ' . $query->createParameter('time') . ')')) + ->set('mtime', $query->createFunction('GREATEST(' . $query->getColumnName('mtime') . ', ' . $query->createParameter('time') . ')')) ->set('etag', $query->expr()->literal(uniqid())) ->where($query->expr()->eq('storage', $query->expr()->literal($storageId, IQueryBuilder::PARAM_INT))) ->andWhere($query->expr()->eq('path_hash', $query->createParameter('hash'))); $sizeQuery = $this->connection->getQueryBuilder(); $sizeQuery->update('filecache') - ->set('size', $sizeQuery->createFunction('`size` + ' . $sizeQuery->createParameter('size'))) + ->set('size', $sizeQuery->func()->add('size', $sizeQuery->createParameter('size'))) ->where($query->expr()->eq('storage', $query->expr()->literal($storageId, IQueryBuilder::PARAM_INT))) ->andWhere($query->expr()->eq('path_hash', $query->createParameter('hash'))) ->andWhere($sizeQuery->expr()->gt('size', $sizeQuery->expr()->literal(-1, IQueryBuilder::PARAM_INT))); diff --git a/lib/private/Files/Type/Loader.php b/lib/private/Files/Type/Loader.php index 41c561dcd22..d1419dca4dc 100644 --- a/lib/private/Files/Type/Loader.php +++ b/lib/private/Files/Type/Loader.php @@ -169,7 +169,7 @@ class Loader implements IMimeTypeLoader { 'mimetype', $update->createNamedParameter($folderMimeTypeId) )) ->andWhere($update->expr()->like( - $update->createFunction('LOWER(' . $update->getColumnName('name') . ')'), + $update->func()->lower('name'), $update->createNamedParameter('%' . $this->dbConnection->escapeLikeParameter('.' . $ext)) )); return $update->execute(); diff --git a/lib/private/Group/Database.php b/lib/private/Group/Database.php index c77ae9e2ee8..53c413f0fa9 100644 --- a/lib/private/Group/Database.php +++ b/lib/private/Group/Database.php @@ -354,7 +354,7 @@ class Database extends ABackend $this->fixDI(); $query = $this->dbConn->getQueryBuilder(); - $query->selectAlias($query->createFunction('COUNT(*)'), 'num_users') + $query->select($query->func()->count('*', 'num_users')) ->from('group_user') ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); diff --git a/lib/private/Lock/DBLockingProvider.php b/lib/private/Lock/DBLockingProvider.php index 6adb7488217..0c5f4fa2010 100644 --- a/lib/private/Lock/DBLockingProvider.php +++ b/lib/private/Lock/DBLockingProvider.php @@ -303,7 +303,7 @@ class DBLockingProvider extends AbstractLockingProvider { $builder = $this->connection->getQueryBuilder(); $query = $builder->update('file_locks') - ->set('lock', $builder->createFunction('`lock` -1')) + ->set('lock', $builder->func()->subtract('lock', $builder->expr()->literal(1))) ->where($builder->expr()->in('key', $builder->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY))) ->andWhere($builder->expr()->gt('lock', new Literal(0))); diff --git a/lib/private/SystemTag/SystemTagObjectMapper.php b/lib/private/SystemTag/SystemTagObjectMapper.php index 8ecc46b4357..f8a1b03cd01 100644 --- a/lib/private/SystemTag/SystemTagObjectMapper.php +++ b/lib/private/SystemTag/SystemTagObjectMapper.php @@ -217,7 +217,7 @@ class SystemTagObjectMapper implements ISystemTagObjectMapper { $query->select('*') ->setMaxResults(1); } else { - $query->select($query->createFunction('COUNT(1)')); + $query->select($query->func()->count($query->expr()->literal(1))); } $query->from(self::RELATION_TABLE) diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php index 80f504d8888..4243ced2e98 100644 --- a/lib/private/User/Manager.php +++ b/lib/private/User/Manager.php @@ -446,7 +446,7 @@ class Manager extends PublicEmitter implements IUserManager { */ public function countDisabledUsers(): int { $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) + $queryBuilder->select($queryBuilder->func()->count('*')) ->from('preferences') ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) @@ -504,7 +504,7 @@ class Manager extends PublicEmitter implements IUserManager { */ public function countSeenUsers() { $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) + $queryBuilder->select($queryBuilder->func()->count('*')) ->from('preferences') ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) diff --git a/lib/public/DB/QueryBuilder/IFunctionBuilder.php b/lib/public/DB/QueryBuilder/IFunctionBuilder.php index 51b639028ce..1e17290f145 100644 --- a/lib/public/DB/QueryBuilder/IFunctionBuilder.php +++ b/lib/public/DB/QueryBuilder/IFunctionBuilder.php @@ -98,10 +98,11 @@ interface IFunctionBuilder { public function subtract($x, $y); /** - * @param mixed $input The input to be counted + * @param mixed $count The input to be counted + * @param string $alias Alias for the counter * * @return IQueryFunction * @since 14.0.0 */ - public function count($input); + public function count($count, $alias = ''); } |