Skip to content

Commit

Permalink
Use the defined func()->count() instead of manual counting
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Nov 8, 2018
1 parent 78fd8ab commit 82fb91b
Show file tree
Hide file tree
Showing 20 changed files with 32 additions and 33 deletions.
4 changes: 2 additions & 2 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function __construct(IDBConnection $db,
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)));

Expand Down Expand Up @@ -1045,7 +1045,7 @@ function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarT
$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'])))
Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/CardDAV/CardDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function __construct(IDBConnection $db,
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)));

Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/Command/CleanupRemoteStorages.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function execute(InputInterface $input, OutputInterface $output) {

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',
Expand Down
2 changes: 1 addition & 1 deletion apps/user_ldap/lib/Mapping/AbstractMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function clearCb(Callable $preCallback, Callable $postCallback): bool {
*/
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();
Expand Down
2 changes: 1 addition & 1 deletion core/Command/Db/ConvertType.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ protected function copyTable(Connection $fromDB, Connection $toDB, Table $table,

$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();
Expand Down
4 changes: 2 additions & 2 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private static function printUpgradePage(\OC\SystemConfig $systemConfig) {
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();
Expand All @@ -323,7 +323,7 @@ private static function printUpgradePage(\OC\SystemConfig $systemConfig) {
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();
Expand Down
9 changes: 3 additions & 6 deletions lib/private/Comments/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected function determineTopmostParentId($id) {
*/
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);
Expand Down Expand Up @@ -552,7 +552,7 @@ public function search(string $search, string $objectType, string $objectId, str
*/
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')))
Expand Down Expand Up @@ -585,10 +585,7 @@ public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $
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')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public function subtract($x, $y) {
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);
}
}
8 changes: 4 additions & 4 deletions lib/private/Files/Cache/Propagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function propagateChange($internalPath, $time, $sizeDifference = 0) {
}, $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));
Expand All @@ -93,7 +93,7 @@ public function propagateChange($internalPath, $time, $sizeDifference = 0) {
// 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)));
Expand Down Expand Up @@ -156,14 +156,14 @@ public function commitBatch() {
$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)));
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Type/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function updateFilecache($ext, $mimeTypeId) {
'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();
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Group/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public function countUsersInGroup(string $gid, string $search = ''): int {
$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)));

Expand Down
2 changes: 1 addition & 1 deletion lib/private/Lock/DBLockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public function releaseAll() {
$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)));

Expand Down
2 changes: 1 addition & 1 deletion lib/private/SystemTag/SystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function haveTag($objIds, string $objectType, string $tagId, bool $all =
$query->select('*')
->setMaxResults(1);
} else {
$query->select($query->createFunction('COUNT(1)'));
$query->select($query->func()->count($query->createNamedParameter(1)));
}

$query->from(self::RELATION_TABLE)
Expand Down
4 changes: 2 additions & 2 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = fa
*/
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')))
Expand Down Expand Up @@ -504,7 +504,7 @@ public function countDisabledUsersOfGroups(array $groups): int {
*/
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')))
Expand Down
5 changes: 3 additions & 2 deletions lib/public/DB/QueryBuilder/IFunctionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ public function add($x, $y);
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 = '');
}
2 changes: 1 addition & 1 deletion tests/lib/Authentication/Token/DefaultTokenMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function resetDatabase() {

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private function resetDatabase() {

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();
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/DB/QueryBuilder/ExpressionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public function testClobComparisons($function, $value, $type, $compareKeyToValue
$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));
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Repair/CleanTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testRun() {
*/
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();

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Repair/RemoveRootSharesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function testRun() {

//Verify
$qb = $this->connection->getQueryBuilder();
$qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
$qb->select($qb->func()->count('*', 'count'))
->from('share');

$cursor = $qb->execute();
Expand Down

0 comments on commit 82fb91b

Please sign in to comment.