diff options
author | Christoph Wurst <ChristophWurst@users.noreply.github.com> | 2018-08-17 09:47:14 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-17 09:47:14 +0300 |
commit | 1ecf67c6af75e9a82e33a105bb1d785903bde566 (patch) | |
tree | 8f0df352f5d2dc946cc48e82745a7fb2c7450444 | |
parent | ec6c0a69b21ea1f898f9a0a75fcfc02ca5814444 (diff) | |
parent | 63753d136b88c717fdf71209ddf900367959861f (diff) |
Merge pull request #1036 from nextcloud/refactor/query-builder-mapper
Migrate to query builder mappers
-rw-r--r-- | lib/Db/CollectedAddressMapper.php | 49 | ||||
-rw-r--r-- | lib/Db/LocalAttachmentMapper.php | 18 | ||||
-rw-r--r-- | lib/Db/MailAccountMapper.php | 28 |
3 files changed, 57 insertions, 38 deletions
diff --git a/lib/Db/CollectedAddressMapper.php b/lib/Db/CollectedAddressMapper.php index 15b4ebe4e..f7e396c76 100644 --- a/lib/Db/CollectedAddressMapper.php +++ b/lib/Db/CollectedAddressMapper.php @@ -22,11 +22,11 @@ namespace OCA\Mail\Db; -use OCP\AppFramework\Db\Mapper; +use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; -class CollectedAddressMapper extends Mapper { +class CollectedAddressMapper extends QBMapper { /** * @param IDBConnection $db @@ -40,25 +40,32 @@ class CollectedAddressMapper extends Mapper { * * @param string $userId * @param string $query + * * @return CollectedAddress[] */ public function findMatching($userId, $query) { - $sql = 'SELECT * FROM *PREFIX*mail_collected_addresses WHERE `user_id` = ? AND (`email` ILIKE ? OR `display_name` ILIKE ?)'; - $params = [ - $userId, - '%' . $query . '%', - '%' . $query . '%', - ]; - return $this->findEntities($sql, $params); + $qb = $this->db->getQueryBuilder(); + $dbQuery = $qb + ->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))) + ->andWhere($qb->expr()->orX( + $qb->expr()->iLike('email', $qb->createNamedParameter("%$query%")), + $qb->expr()->iLike('display_name', $qb->createNamedParameter("%$query%")) + )); + + return $this->findEntities($dbQuery); } public function exists($userId, $email) { - $sql = 'SELECT * FROM *PREFIX*mail_collected_addresses WHERE `user_id` = ? AND `email` ILIKE ?'; - $params = [ - $userId, - $email - ]; - return count($this->findEntities($sql, $params)) > 0; + $qb = $this->db->getQueryBuilder(); + $dbQuery = $qb + ->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))) + ->andWhere($qb->expr()->iLike('email', $qb->createNamedParameter($email))); + + return count($this->findEntities($dbQuery)) > 0; } public function getTotal() { @@ -68,7 +75,7 @@ class CollectedAddressMapper extends Mapper { ->from($this->getTableName()); $result = $qb->execute(); - $count = (int) $result->fetchColumn(0); + $count = (int)$result->fetchColumn(0); $result->closeCursor(); return $count; } @@ -85,16 +92,10 @@ class CollectedAddressMapper extends Mapper { ->setMaxResults(100); if (!is_null($minId)) { $query = $query->where($qb->expr()->gte('id', - $qb->createNamedParameter($minId))); + $qb->createNamedParameter($minId))); } - $result = $query->execute(); - $rows = $result->fetchAll(); - $result->closeCursor(); - - return array_map(function(array $data) { - return CollectedAddress::fromRow($data); - }, $rows); + return $this->findEntities($query); } } diff --git a/lib/Db/LocalAttachmentMapper.php b/lib/Db/LocalAttachmentMapper.php index c4c932b8f..2c6a182fb 100644 --- a/lib/Db/LocalAttachmentMapper.php +++ b/lib/Db/LocalAttachmentMapper.php @@ -23,10 +23,10 @@ namespace OCA\Mail\Db; use OCP\AppFramework\Db\DoesNotExistException; -use OCP\AppFramework\Db\Mapper; +use OCP\AppFramework\Db\QBMapper; use OCP\IDBConnection; -class LocalAttachmentMapper extends Mapper { +class LocalAttachmentMapper extends QBMapper { /** * @param IDBConnection $db @@ -37,15 +37,21 @@ class LocalAttachmentMapper extends Mapper { /** * @throws DoesNotExistException + * * @param int $userId * @param int $id + * * @return LocalAttachment */ public function find($userId, $id) { - $sql = 'SELECT * FROM `' . $this->getTableName() . '` WHERE user_id = ? and id = ?'; - $params = [$userId, $id]; - - return $this->findEntity($sql, $params); + $qb = $this->db->getQueryBuilder(); + $query = $qb + ->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))) + ->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($id))); + + return $this->findEntity($query); } } diff --git a/lib/Db/MailAccountMapper.php b/lib/Db/MailAccountMapper.php index 1efab8b65..90c5d26dc 100644 --- a/lib/Db/MailAccountMapper.php +++ b/lib/Db/MailAccountMapper.php @@ -24,10 +24,10 @@ namespace OCA\Mail\Db; -use OCP\AppFramework\Db\Mapper; +use OCP\AppFramework\Db\QBMapper; use OCP\IDBConnection; -class MailAccountMapper extends Mapper { +class MailAccountMapper extends QBMapper { /** * @param IDBConnection $db @@ -40,31 +40,43 @@ class MailAccountMapper extends Mapper { * * @param string $userId * @param int $accountId + * * @return MailAccount */ public function find($userId, $accountId) { - $sql = 'SELECT * FROM `' . $this->getTableName() . '` WHERE user_id = ? and id = ?'; - $params = [$userId, $accountId]; + $qb = $this->db->getQueryBuilder(); + $query = $qb + ->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))) + ->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($accountId))); - return $this->findEntity($sql, $params); + return $this->findEntity($qb); } /** * Finds all Mail Accounts by user id existing for this user + * * @param string $userId the id of the user that we want to find * @param $userId + * * @return MailAccount[] */ public function findByUserId($userId) { - $sql = 'SELECT * FROM ' . $this->getTableName() . ' WHERE user_id = ?'; - $params = [$userId]; + $qb = $this->db->getQueryBuilder(); + $query = $qb + ->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))); - return $this->findEntities($sql, $params); + return $this->findEntities($query); } /** * Saves an User Account into the database + * * @param MailAccount $account + * * @return MailAccount */ public function save(MailAccount $account) { |