Get rid of unneeded sql queries

This commit is contained in:
Julius Haertl
2016-10-30 13:21:05 +01:00
parent 83e2cb0a61
commit 0db8b5646a
2 changed files with 76 additions and 71 deletions

View File

@@ -5,20 +5,20 @@
* @author Julius Härtl <jus@bitgrid.net> * @author Julius Härtl <jus@bitgrid.net>
* *
* @license GNU AGPL version 3 or any later version * @license GNU AGPL version 3 or any later version
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version. * License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
namespace OCA\Deck\Db; namespace OCA\Deck\Db;
@@ -28,16 +28,16 @@ use OCP\IDb;
class BoardMapper extends DeckMapper implements IPermissionMapper { class BoardMapper extends DeckMapper implements IPermissionMapper {
private $labelMapper; private $labelMapper;
private $aclMapper; private $aclMapper;
private $stackMapper; private $stackMapper;
public function __construct(IDb $db, LabelMapper $labelMapper, AclMapper $aclMapper, StackMapper $stackMapper) { public function __construct(IDb $db, LabelMapper $labelMapper, AclMapper $aclMapper, StackMapper $stackMapper) {
parent::__construct($db, 'deck_boards', '\OCA\Deck\Db\Board'); parent::__construct($db, 'deck_boards', '\OCA\Deck\Db\Board');
$this->labelMapper = $labelMapper; $this->labelMapper = $labelMapper;
$this->aclMapper = $aclMapper; $this->aclMapper = $aclMapper;
$this->stackMapper = $stackMapper; $this->stackMapper = $stackMapper;
} }
/** /**
@@ -46,41 +46,46 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
* @param bool $withAcl * @param bool $withAcl
* @return \OCP\AppFramework\Db\Entity if not found * @return \OCP\AppFramework\Db\Entity if not found
*/ */
public function find($id, $withLabels=false, $withAcl=false) { public function find($id, $withLabels = false, $withAcl = false) {
$sql = 'SELECT id, title, owner, color, archived FROM `*PREFIX*deck_boards` ' . $sql = 'SELECT id, title, owner, color, archived FROM `*PREFIX*deck_boards` ' .
'WHERE `id` = ?'; 'WHERE `id` = ?';
$board = $this->findEntity($sql, [$id]); $board = $this->findEntity($sql, [$id]);
// Add labels // Add labels
$labels = $this->labelMapper->findAll($id); if ($withLabels) {
$board->setLabels($labels); $labels = $this->labelMapper->findAll($id);
$board->setLabels($labels);
}
// Add acl // Add acl
$acl = $this->aclMapper->findAll($id); if ($withAcl) {
$board->setAcl($acl); $acl = $this->aclMapper->findAll($id);
$board->setAcl($acl);
return $board; }
}
/** return $board;
* Find all boards for a given user }
* @param $userId
* @param null $limit /**
* @param null $offset * Find all boards for a given user
* @return array *
*/ * @param $userId
public function findAllByUser($userId, $limit=null, $offset=null) { * @param null $limit
$sql = 'SELECT id, title, owner, color, archived, 0 as shared FROM oc_deck_boards WHERE owner = ? UNION ' . * @param null $offset
'SELECT boards.id, title, owner, color, archived, 1 as shared FROM oc_deck_boards as boards ' . * @return array
'JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=\'user\' AND boards.owner != ?'; */
$entries = $this->findEntities($sql, [$userId, $userId, $userId], $limit, $offset); public function findAllByUser($userId, $limit = null, $offset = null) {
/* @var Board $entry */ $sql = 'SELECT id, title, owner, color, archived, 0 as shared FROM oc_deck_boards WHERE owner = ? UNION ' .
foreach ($entries as $entry) { 'SELECT boards.id, title, owner, color, archived, 1 as shared FROM oc_deck_boards as boards ' .
$acl = $this->aclMapper->findAll($entry->id); 'JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=\'user\' AND boards.owner != ?';
$entry->setAcl($acl); $entries = $this->findEntities($sql, [$userId, $userId, $userId], $limit, $offset);
} /* @var Board $entry */
return $entries; foreach ($entries as $entry) {
} $acl = $this->aclMapper->findAll($entry->id);
$entry->setAcl($acl);
}
return $entries;
}
/** /**
* Find all boards for a given user * Find all boards for a given user
@@ -91,31 +96,31 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
* @param null $offset * @param null $offset
* @return array * @return array
*/ */
public function findAllByGroups($userId, $groups, $limit=null, $offset=null) { public function findAllByGroups($userId, $groups, $limit = null, $offset = null) {
if(count($groups)<=0) { if (count($groups) <= 0) {
return []; return [];
} }
$sql = 'SELECT boards.id, title, owner, color, archived, 2 as shared FROM oc_deck_boards as boards ' . $sql = 'SELECT boards.id, title, owner, color, archived, 2 as shared FROM oc_deck_boards as boards ' .
'INNER JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE owner != ? AND type=\'group\' AND ('; 'INNER JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE owner != ? AND type=\'group\' AND (';
for($i=0;$i<count($groups);$i++) { for ($i = 0; $i < count($groups); $i++) {
$sql .= 'acl.participant = ? '; $sql .= 'acl.participant = ? ';
if(count($groups)>1 && $i<count($groups)-1) { if (count($groups) > 1 && $i < count($groups) - 1) {
$sql .= ' OR '; $sql .= ' OR ';
} }
} }
$sql .= ');'; $sql .= ');';
$entries = $this->findEntities($sql, array_merge([$userId], $groups), $limit, $offset); $entries = $this->findEntities($sql, array_merge([$userId], $groups), $limit, $offset);
/* @var Board $entry */ /* @var Board $entry */
foreach ($entries as $entry) { foreach ($entries as $entry) {
$acl = $this->aclMapper->findAll($entry->id); $acl = $this->aclMapper->findAll($entry->id);
$entry->setAcl($acl); $entry->setAcl($acl);
} }
return $entries; return $entries;
} }
public function delete(/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ public function delete(/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
\OCP\AppFramework\Db\Entity $entity) { \OCP\AppFramework\Db\Entity $entity) {
// delete acl // delete acl
$acl = $this->aclMapper->findAll($entity->getId()); $acl = $this->aclMapper->findAll($entity->getId());
foreach ($acl as $item) { foreach ($acl as $item) {
$this->aclMapper->delete($item); $this->aclMapper->delete($item);
@@ -132,17 +137,17 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
$this->labelMapper->delete($label); $this->labelMapper->delete($label);
} }
return parent::delete($entity); return parent::delete($entity);
} }
public function isOwner($userId, $boardId) { public function isOwner($userId, $boardId) {
$board = $this->find($boardId); $board = $this->find($boardId);
return ($board->getOwner() === $userId); return ($board->getOwner() === $userId);
} }
public function findBoardId($id) { public function findBoardId($id) {
return $id; return $id;
} }
} }

View File

@@ -97,7 +97,7 @@ class CardService {
$this->cardMapper->update($card); $this->cardMapper->update($card);
} }
// FIXME: return reordered cards without an additional db query // FIXME: return reordered cards without an additional db query
//$cards = $this->cardMapper->findAll($stackId); $cards = $this->cardMapper->findAll($stackId);
return $cards; return $cards;
} }