Merge pull request #9881 from nextcloud/user-db-backend-querybuilder

use query builder in all places in the db user backend
This commit is contained in:
Morris Jobke 2018-06-19 14:12:22 +02:00 committed by GitHub
commit 8646f01320
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 13 deletions

View File

@ -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) . ')');
}
}

View File

@ -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();
}
/**

View File

@ -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);
}

View File

@ -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());
}
}