From 9244adee30f3b1fadea5086bae5ac47370a7e6fa Mon Sep 17 00:00:00 2001 From: Raul Ferreira Fuentes Date: Mon, 11 Apr 2022 18:44:27 +0200 Subject: [PATCH] Various small fixes in mapper queries Signed-off-by: Raul Ferreira Fuentes --- lib/Db/AclMapper.php | 4 +-- lib/Db/AttachmentMapper.php | 63 ++++++++++++------------------------- lib/Db/LabelMapper.php | 5 +-- 3 files changed, 25 insertions(+), 47 deletions(-) diff --git a/lib/Db/AclMapper.php b/lib/Db/AclMapper.php index 5a200c09e..95e1af55a 100644 --- a/lib/Db/AclMapper.php +++ b/lib/Db/AclMapper.php @@ -34,7 +34,7 @@ class AclMapper extends DeckMapper implements IPermissionMapper { * @param IDBConnection $db */ public function __construct(IDBConnection $db) { - parent::__construct($db, 'deck_boards', Board::class); + parent::__construct($db, 'deck_board_acl', Board::class); } /** @@ -47,7 +47,7 @@ class AclMapper extends DeckMapper implements IPermissionMapper { public function findAll($boardId, $limit = null, $offset = null) { $qb = $this->db->getQueryBuilder(); $qb->select('*') - ->from($this->getTableName()) + ->from('deck_board_acl') ->where($qb->expr()->eq('board_id', $qb->createNamedParameter($boardId, IQueryBuilder::PARAM_INT))) ->setMaxResults($limit) ->setFirstResult($offset); diff --git a/lib/Db/AttachmentMapper.php b/lib/Db/AttachmentMapper.php index 46e63f7c4..6b98b895b 100644 --- a/lib/Db/AttachmentMapper.php +++ b/lib/Db/AttachmentMapper.php @@ -52,10 +52,11 @@ class AttachmentMapper extends DeckMapper implements IPermissionMapper { } /** - * @param $id - * @return Entity|Attachment - * @throws \OCP\AppFramework\Db\DoesNotExistException - * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException + * @param int $id + * @return Attachment + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException + * @throws \OCP\DB\Exception */ public function find($id) { $qb = $this->db->getQueryBuilder(); @@ -63,43 +64,31 @@ class AttachmentMapper extends DeckMapper implements IPermissionMapper { ->from('deck_attachment') ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); - $cursor = $qb->execute(); - $row = $cursor->fetch(PDO::FETCH_ASSOC); - if ($row === false) { - $cursor->closeCursor(); - throw new DoesNotExistException('Did expect one result but found none when executing' . $qb); - } - - $row2 = $cursor->fetch(); - $cursor->closeCursor(); - if ($row2 !== false) { - throw new MultipleObjectsReturnedException('Did not expect more than one result when executing' . $query); - } - - return $this->mapRowToEntity($row); + return $this->findEntity($qb); } + /** + * @param int $cardId + * @param string $data + * @return Attachment + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException + * @throws \OCP\DB\Exception + */ public function findByData($cardId, $data) { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from('deck_attachment') ->where($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->eq('data', $qb->createNamedParameter($data, IQueryBuilder::PARAM_STR))); - $cursor = $qb->execute(); - $row = $cursor->fetch(PDO::FETCH_ASSOC); - if ($row === false) { - $cursor->closeCursor(); - throw new DoesNotExistException('Did expect one result but found none when executing' . $qb); - } - $cursor->closeCursor(); - return $this->mapRowToEntity($row); + + return $this->findEntity($qb); } /** - * Find all attachments for a card - * * @param $cardId - * @return array + * @return Entity[] + * @throws \OCP\DB\Exception */ public function findAll($cardId) { $qb = $this->db->getQueryBuilder(); @@ -109,13 +98,7 @@ class AttachmentMapper extends DeckMapper implements IPermissionMapper { ->andWhere($qb->expr()->eq('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))); - $entities = []; - $cursor = $qb->execute(); - while ($row = $cursor->fetch()) { - $entities[] = $this->mapRowToEntity($row); - } - $cursor->closeCursor(); - return $entities; + return $this->findEntities($qb); } /** @@ -139,13 +122,7 @@ class AttachmentMapper extends DeckMapper implements IPermissionMapper { ->andWhere($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT))); } - $entities = []; - $cursor = $qb->execute(); - while ($row = $cursor->fetch()) { - $entities[] = $this->mapRowToEntity($row); - } - $cursor->closeCursor(); - return $entities; + return $this->findEntities($qb); } diff --git a/lib/Db/LabelMapper.php b/lib/Db/LabelMapper.php index e613dd2af..be1b68c23 100644 --- a/lib/Db/LabelMapper.php +++ b/lib/Db/LabelMapper.php @@ -75,7 +75,7 @@ class LabelMapper extends DeckMapper implements IPermissionMapper { */ public function findAssignedLabelsForCard($cardId, $limit = null, $offset = null): array { $qb = $this->db->getQueryBuilder(); - $qb->select('l.*,card_id') + $qb->select('l.*', 'card_id') ->from($this->getTableName(), 'l') ->innerJoin('l', 'deck_assigned_labels', 'al', 'l.id = al.label_id') ->where($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT))) @@ -95,7 +95,8 @@ class LabelMapper extends DeckMapper implements IPermissionMapper { */ public function findAssignedLabelsForBoard($boardId, $limit = null, $offset = null): array { $qb = $this->db->getQueryBuilder(); - $qb->select('c.id as card_id', 'l.id as id', 'l.title as title', 'l.color as color') + $qb->select('l.id as id', 'l.title as title', 'l.color as color') + ->selectAlias('c.id', 'card_id') ->from($this->getTableName(), 'l') ->innerJoin('l', 'deck_assigned_labels', 'al', 'al.label_id = l.id') ->innerJoin('l', 'deck_cards', 'c', 'al.card_id = c.id')