diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php index bd367973097..cf6183b0238 100644 --- a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php +++ b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php @@ -71,4 +71,8 @@ class FunctionBuilder implements IFunctionBuilder { 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) . ')'); + } } diff --git a/lib/private/User/Database.php b/lib/private/User/Database.php index b2c54cd7c7e..df7f70c5dd2 100644 --- a/lib/private/User/Database.php +++ b/lib/private/User/Database.php @@ -96,6 +96,7 @@ class Database extends ABackend * \OC\User\Database constructor. * * @param EventDispatcher $eventDispatcher + * @param string $table */ public function __construct($eventDispatcher = null) { $this->cache = new CappedMemoryCache(); @@ -159,8 +160,10 @@ class Database extends ABackend $this->fixDI(); // Delete user-group-relation - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?'); - $result = $query->execute([$uid]); + $query = $this->dbConn->getQueryBuilder(); + $query->delete('users') + ->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid)))); + $result = $query->execute(); if (isset($this->cache[$uid])) { unset($this->cache[$uid]); @@ -184,8 +187,15 @@ class Database extends ABackend if ($this->userExists($uid)) { $event = new GenericEvent($password); $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); - $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?'); - $result = $query->execute([\OC::$server->getHasher()->hash($password), $uid]); + + $hasher = \OC::$server->getHasher(); + $hashedPassword = $hasher->hash($password); + + $query = $this->dbConn->getQueryBuilder(); + $query->update('users') + ->set('password', $query->createNamedParameter($hashedPassword)) + ->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid)))); + $result = $query->execute(); return $result ? true : false; } @@ -206,8 +216,12 @@ class Database extends ABackend $this->fixDI(); if ($this->userExists($uid)) { - $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = LOWER(?)'); - $query->execute([$displayName, $uid]); + $query = $this->dbConn->getQueryBuilder(); + $query->update('users') + ->set('displayname', $query->createNamedParameter($displayName)) + ->where($query->expr()->eq('uid_lower', $query->createNamedParameter(mb_strtolower($uid)))); + $query->execute(); + $this->cache[$uid]['displayname'] = $displayName; return true; @@ -253,7 +267,7 @@ class Database extends ABackend ->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) ->orWhere($query->expr()->iLike('configvalue', $query->createPositionalParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))) ->orderBy($query->func()->lower('displayname'), 'ASC') - ->orderBy($query->func()->lower('uid'), 'ASC') + ->orderBy('uid_lower', 'ASC') ->setMaxResults($limit) ->setFirstResult($offset); @@ -406,13 +420,12 @@ class Database extends ABackend public function countUsers() { $this->fixDI(); - $query = \OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); + $query = $this->dbConn->getQueryBuilder(); + $query->select($query->func()->count('uid')) + ->from('users'); $result = $query->execute(); - if ($result === false) { - Util::writeLog('core', \OC_DB::getErrorMessage(), ILogger::ERROR); - return false; - } - return $result->fetchOne(); + + return $result->fetchColumn(); } /** diff --git a/lib/public/DB/QueryBuilder/IFunctionBuilder.php b/lib/public/DB/QueryBuilder/IFunctionBuilder.php index e0e331c0807..51b639028ce 100644 --- a/lib/public/DB/QueryBuilder/IFunctionBuilder.php +++ b/lib/public/DB/QueryBuilder/IFunctionBuilder.php @@ -96,4 +96,12 @@ interface IFunctionBuilder { * @since 14.0.0 */ public function subtract($x, $y); + + /** + * @param mixed $input The input to be counted + * + * @return IQueryFunction + * @since 14.0.0 + */ + public function count($input); } diff --git a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php index 1b998287c98..136650741f5 100644 --- a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php +++ b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php @@ -110,4 +110,14 @@ class FunctionBuilderTest extends TestCase { $this->assertEquals(1, $query->execute()->fetchColumn()); } + + public function testCount() { + $query = $this->connection->getQueryBuilder(); + + $query->select($query->func()->count('appid')); + $query->from('appconfig') + ->setMaxResults(1); + + $this->assertGreaterThan(1, $query->execute()->fetchColumn()); + } }