Various small fixes in mapper queries

Signed-off-by: Raul Ferreira Fuentes <raul@nextcloud.com>
This commit is contained in:
Raul Ferreira Fuentes
2022-04-11 18:44:27 +02:00
parent 3265a9de7b
commit 9244adee30
3 changed files with 25 additions and 47 deletions

View File

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

View File

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

View File

@@ -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')