diff options
author | Morris Jobke <hey@morrisjobke.de> | 2018-06-19 15:12:22 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-19 15:12:22 +0300 |
commit | 8646f01320f685de1a428b6e696c7ea691defffc (patch) | |
tree | 0d1e951f9102b9d32c5a9bdd571d7a4da604e471 /lib | |
parent | 1d65e44dae971d85c785d74ed7f9b4a574d54121 (diff) | |
parent | 4187d2cdb391bbe7518c602a579abd1305787bf2 (diff) |
Merge pull request #9881 from nextcloud/user-db-backend-querybuilder
use query builder in all places in the db user backend
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php | 4 | ||||
-rw-r--r-- | lib/private/User/Database.php | 39 | ||||
-rw-r--r-- | lib/public/DB/QueryBuilder/IFunctionBuilder.php | 8 |
3 files changed, 38 insertions, 13 deletions
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); } |