From 22f15363c29f1ee7227d6aab219f8e6fad932ff7 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Thu, 16 Aug 2018 14:36:41 +0200 Subject: [PATCH 1/4] Migrate MailAccountMapper to the QBMapper Signed-off-by: Christoph Wurst --- lib/Db/MailAccountMapper.php | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/Db/MailAccountMapper.php b/lib/Db/MailAccountMapper.php index 1efab8b65f..90c5d26dc3 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 @@ public function __construct(IDBConnection $db) { * * @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) { From 02dd95d10de041278d8bc05f7f548e44471db60b Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Thu, 16 Aug 2018 14:37:39 +0200 Subject: [PATCH 2/4] Migrate the LocalAttachmentsMapper to the QBMapper Signed-off-by: Christoph Wurst --- lib/Db/LocalAttachmentMapper.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Db/LocalAttachmentMapper.php b/lib/Db/LocalAttachmentMapper.php index c4c932b8f3..bd98e14bf9 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,20 @@ public function __construct(IDBConnection $db) { /** * @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]; + $qb = $this->db->getQueryBuilder(); + $query = $qb + ->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))); - return $this->findEntity($sql, $params); + return $this->findEntity($query); } } From 23726d8b65b260f1cc467413231fd7240ac44148 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Thu, 16 Aug 2018 15:10:59 +0200 Subject: [PATCH 3/4] Migrate CollectedAddressesMapper to QBMapper Signed-off-by: Christoph Wurst --- lib/Db/CollectedAddressMapper.php | 51 ++++++++++++++++--------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/Db/CollectedAddressMapper.php b/lib/Db/CollectedAddressMapper.php index 15b4ebe4e5..f7875e4a0c 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,34 @@ public function __construct(IDBConnection $db) { * * @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)), + $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()->eq('email', $qb->createNamedParameter($email))); + + return count($this->findEntities($dbQuery)) > 0; } public function getTotal() { @@ -68,7 +77,7 @@ public function getTotal() { ->from($this->getTableName()); $result = $qb->execute(); - $count = (int) $result->fetchColumn(0); + $count = (int)$result->fetchColumn(0); $result->closeCursor(); return $count; } @@ -85,16 +94,10 @@ public function getChunk($minId = null) { ->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); } } From 63753d136b88c717fdf71209ddf900367959861f Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Fri, 17 Aug 2018 06:26:32 +0200 Subject: [PATCH 4/4] Fix queries and coding style Signed-off-by: Christoph Wurst --- lib/Db/CollectedAddressMapper.php | 14 ++++++-------- lib/Db/LocalAttachmentMapper.php | 3 ++- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/Db/CollectedAddressMapper.php b/lib/Db/CollectedAddressMapper.php index f7875e4a0c..f7e396c760 100644 --- a/lib/Db/CollectedAddressMapper.php +++ b/lib/Db/CollectedAddressMapper.php @@ -48,13 +48,11 @@ public function findMatching($userId, $query) { $dbQuery = $qb ->select('*') ->from($this->getTableName()) - ->where( - $qb->expr()->eq('user_id', $qb->createNamedParameter($userId)), - $qb->expr()->orX( - $qb->expr()->iLike('email', $qb->createNamedParameter("%$query%")), - $qb->expr()->iLike('display_name', $qb->createNamedParameter("%$query%")) - ) - ); + ->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); } @@ -65,7 +63,7 @@ public function exists($userId, $email) { ->select('*') ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))) - ->andWhere($qb->expr()->eq('email', $qb->createNamedParameter($email))); + ->andWhere($qb->expr()->iLike('email', $qb->createNamedParameter($email))); return count($this->findEntities($dbQuery)) > 0; } diff --git a/lib/Db/LocalAttachmentMapper.php b/lib/Db/LocalAttachmentMapper.php index bd98e14bf9..2c6a182fb2 100644 --- a/lib/Db/LocalAttachmentMapper.php +++ b/lib/Db/LocalAttachmentMapper.php @@ -48,7 +48,8 @@ public function find($userId, $id) { $query = $qb ->select('*') ->from($this->getTableName()) - ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))); + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))) + ->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($id))); return $this->findEntity($query); }