diff options
20 files changed, 32 insertions, 33 deletions
diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index 60f163abcbf..187ba4ecdcf 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -213,7 +213,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { $principalUri = $this->convertPrincipal($principalUri, true); $query = $this->db->getQueryBuilder(); - $query->select($query->createFunction('COUNT(*)')) + $query->select($query->func()->count('*')) ->from('calendars') ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); @@ -1045,7 +1045,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription $extraData = $this->getDenormalizedData($calendarData); $q = $this->db->getQueryBuilder(); - $q->select($q->createFunction('COUNT(*)')) + $q->select($q->func()->count('*')) ->from('calendarobjects') ->where($q->expr()->eq('calendarid', $q->createNamedParameter($calendarId))) ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($extraData['uid']))) diff --git a/apps/dav/lib/CardDAV/CardDavBackend.php b/apps/dav/lib/CardDAV/CardDavBackend.php index eb94885d177..699248f7b17 100644 --- a/apps/dav/lib/CardDAV/CardDavBackend.php +++ b/apps/dav/lib/CardDAV/CardDavBackend.php @@ -117,7 +117,7 @@ class CardDavBackend implements BackendInterface, SyncSupport { public function getAddressBooksForUserCount($principalUri) { $principalUri = $this->convertPrincipal($principalUri, true); $query = $this->db->getQueryBuilder(); - $query->select($query->createFunction('COUNT(*)')) + $query->select($query->func()->count('*')) ->from('addressbooks') ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); diff --git a/apps/files_sharing/lib/Command/CleanupRemoteStorages.php b/apps/files_sharing/lib/Command/CleanupRemoteStorages.php index 2175982dfc1..a4e5f50b9b2 100644 --- a/apps/files_sharing/lib/Command/CleanupRemoteStorages.php +++ b/apps/files_sharing/lib/Command/CleanupRemoteStorages.php @@ -98,7 +98,7 @@ class CleanupRemoteStorages extends Command { public function countFiles($numericId, OutputInterface $output) { $queryBuilder = $this->connection->getQueryBuilder(); - $queryBuilder->select($queryBuilder->createFunction('COUNT(' . $queryBuilder->getColumnName('fileid') . ')')) + $queryBuilder->select($queryBuilder->func()->count('fileid')) ->from('filecache') ->where($queryBuilder->expr()->eq( 'storage', diff --git a/apps/user_ldap/lib/Mapping/AbstractMapping.php b/apps/user_ldap/lib/Mapping/AbstractMapping.php index c3f09fd6caa..7ec22b6b79c 100644 --- a/apps/user_ldap/lib/Mapping/AbstractMapping.php +++ b/apps/user_ldap/lib/Mapping/AbstractMapping.php @@ -311,7 +311,7 @@ abstract class AbstractMapping { */ public function count() { $qb = $this->dbc->getQueryBuilder(); - $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('ldap_dn') . ')')) + $query = $qb->select($qb->func()->count('ldap_dn')) ->from($this->getTableName()); $res = $query->execute(); $count = $res->fetchColumn(); diff --git a/core/Command/Db/ConvertType.php b/core/Command/Db/ConvertType.php index b6c2c66b3d3..bd26c5e5154 100644 --- a/core/Command/Db/ConvertType.php +++ b/core/Command/Db/ConvertType.php @@ -301,7 +301,7 @@ class ConvertType extends Command implements CompletionAwareInterface { $query = $fromDB->getQueryBuilder(); $query->automaticTablePrefix(false); - $query->selectAlias($query->createFunction('COUNT(*)'), 'num_entries') + $query->select($query->func()->count('*', 'num_entries')) ->from($table->getName()); $result = $query->execute(); $count = $result->fetchColumn(); 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 79f2ff274cf..6d9c8f1b3d1 100644 --- a/lib/private/Lock/DBLockingProvider.php +++ b/lib/private/Lock/DBLockingProvider.php @@ -314,7 +314,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 = ''); } diff --git a/tests/lib/Authentication/Token/DefaultTokenMapperTest.php b/tests/lib/Authentication/Token/DefaultTokenMapperTest.php index bebceba62cf..a40db4461d2 100644 --- a/tests/lib/Authentication/Token/DefaultTokenMapperTest.php +++ b/tests/lib/Authentication/Token/DefaultTokenMapperTest.php @@ -93,7 +93,7 @@ class DefaultTokenMapperTest extends TestCase { private function getNumberOfTokens() { $qb = $this->dbConnection->getQueryBuilder(); - $result = $qb->select($qb->createFunction('count(*) as `count`')) + $result = $qb->select($qb->func()->count('*', 'count')) ->from('authtoken') ->execute() ->fetch(); diff --git a/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php b/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php index 89adef5bb8f..22e1891b51f 100644 --- a/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php +++ b/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php @@ -117,7 +117,7 @@ class PublicKeyTokenMapperTest extends TestCase { private function getNumberOfTokens() { $qb = $this->dbConnection->getQueryBuilder(); - $result = $qb->select($qb->createFunction('count(*) as `count`')) + $result = $qb->select($qb->func()->count('*', 'count')) ->from('authtoken') ->execute() ->fetch(); diff --git a/tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php b/tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php index 1bf42d230fe..f5dc2a07246 100644 --- a/tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php +++ b/tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php @@ -400,7 +400,7 @@ class ExpressionBuilderTest extends TestCase { $this->createConfig($appId, 11, 'underscore'); $query = $this->connection->getQueryBuilder(); - $query->select($query->createFunction('COUNT(*) AS `count`')) + $query->select($query->func()->count('*', 'count')) ->from('appconfig') ->where($query->expr()->eq('appid', $query->createNamedParameter($appId))) ->andWhere(call_user_func([$query->expr(), $function], 'configvalue', $query->createNamedParameter($value, $type), IQueryBuilder::PARAM_STR)); diff --git a/tests/lib/Repair/CleanTagsTest.php b/tests/lib/Repair/CleanTagsTest.php index 115bce49f71..58fc6af6efb 100644 --- a/tests/lib/Repair/CleanTagsTest.php +++ b/tests/lib/Repair/CleanTagsTest.php @@ -117,7 +117,7 @@ class CleanTagsTest extends \Test\TestCase { */ protected function assertEntryCount($tableName, $expected, $message = '') { $qb = $this->connection->getQueryBuilder(); - $result = $qb->select($qb->createFunction('COUNT(*)')) + $result = $qb->select($qb->func()->count('*')) ->from($tableName) ->execute(); diff --git a/tests/lib/Repair/RemoveRootSharesTest.php b/tests/lib/Repair/RemoveRootSharesTest.php index cfb81cb1ecc..9a4882bef29 100644 --- a/tests/lib/Repair/RemoveRootSharesTest.php +++ b/tests/lib/Repair/RemoveRootSharesTest.php @@ -180,7 +180,7 @@ class RemoveRootSharesTest extends \Test\TestCase { //Verify $qb = $this->connection->getQueryBuilder(); - $qb->selectAlias($qb->createFunction('COUNT(*)'), 'count') + $qb->select($qb->func()->count('*', 'count')) ->from('share'); $cursor = $qb->execute(); |