diff --git a/library/Opus/Db2/Collections.php b/library/Opus/Db2/Collections.php new file mode 100644 index 00000000..46ef645a --- /dev/null +++ b/library/Opus/Db2/Collections.php @@ -0,0 +1,85 @@ +selectNodeById($id); + $row = $select->execute()->fetchAssociative(); + + if ($row === null) { + throw new ModelException("Node $id not found."); + } + + return $row; + } + + /** + * Returns an SQL query builder instance preconfigured with an SQL statement + * for retrieving nodes by ID. + * + * @param int $id Primary key of the node. + * @return QueryBuilder + */ + private function selectNodeById($id) + { + $queryBuilder = $this->getQueryBuilder(); + + $select = $queryBuilder + ->select('*') + ->from($this->name, 'node') + ->where("node.{$this->primary} = ?"); + + return $select->setParameters([$id]); + } + + /** + * Returns the row data for all child nodes of the node with given ID. + * + * @param int $id The ID of the node whose children shall be returned. + * @return array + */ + public function getChildrenById($id) + { + $select = $this->selectChildrenById($id, 'id'); + + return $select->execute()->fetchAllAssociative(); + } + + /** + * Returns an SQL query builder instance preconfigured with an SQL statement + * for fetching all children of the node with the given ID. A second + * parameter allows the selection of the returned columns in the same format + * as \QueryBuilder::select() takes. + * + * @param int $id The ID of the parent node. + * @param mixed $cols The columns to show, defaults to '*'. + * + * @return QueryBuilder + */ + public function selectChildrenById($id, $cols = '*') + { + $queryBuilder = $this->getQueryBuilder(); + + $select = $queryBuilder + ->select($cols) + ->from($this->name, 'node') + ->where("node.{$this->parent} = ?") + ->orderBy("node.{$this->left}", "ASC"); + + return $select->setParameters([$id]); + } + + /** + * Returns the row data for all nodes with the given tree ID. Returns null + * if nothing was found. + * + * @param int $treeId The ID of the nested-set structure whose nodes shall be returned. + * @return array|null + */ + public function getNodesByTreeId($treeId) + { + $queryBuilder = $this->getQueryBuilder(); + + $select = $queryBuilder + ->select('*') + ->from($this->name) + ->where("{$this->tree} = ?") + ->orderBy("{$this->left}", "ASC"); + + $select->setParameters([$treeId]); + + return $queryBuilder->execute()->fetchAllAssociative(); + } +} diff --git a/tests/Opus/CollectionTest.php b/tests/Opus/CollectionTest.php index 62c3d736..d548b7c7 100644 --- a/tests/Opus/CollectionTest.php +++ b/tests/Opus/CollectionTest.php @@ -43,7 +43,7 @@ use Opus\Collection; use Opus\CollectionRole; use Opus\Config; -use Opus\Db\Collections; +use Opus\Db2\Collections; use Opus\Document; use Opus\Model\NotFoundException; use Opus\Model\Xml\Cache; @@ -1235,14 +1235,12 @@ protected function validateNestedSet() { $table = new Collections(); - $select = $table->select()->where('role_id = ?', 1)->order('left_id ASC'); - - $rows = $table->fetchAll($select); + $rows = $table->getNodesByTreeId(1); $this->assertEquals(14, count($rows)); - $this->assertEquals(1, $rows[0]->left_id); - $this->assertEquals(28, $rows[0]->right_id); + $this->assertEquals(1, $rows[0]["left_id"]); + $this->assertEquals(28, $rows[0]["right_id"]); $validator = new NestedSetValidator($table); diff --git a/tests/Opus/TestAsset/NestedSetValidator.php b/tests/Opus/TestAsset/NestedSetValidator.php index 9221f0cb..066cd372 100644 --- a/tests/Opus/TestAsset/NestedSetValidator.php +++ b/tests/Opus/TestAsset/NestedSetValidator.php @@ -35,7 +35,7 @@ namespace OpusTest\TestAsset; -use Opus\Db\NestedSet; +use Opus\Db2\NestedSet; use Opus\LoggingTrait; use Opus\Model\ModelException; @@ -81,8 +81,7 @@ public function __construct($table) */ public function validate($rootId) { - $select = $this->table->select()->where('id = ?', $rootId); - $node = $this->table->fetchRow($select); + $node = $this->table->getNodeById($rootId); $this->counter = (int) $node['left_id']; return $this->validateNode($rootId); // root node } @@ -97,8 +96,7 @@ public function validateNode($nodeId) { $logger = $this->getLogger(); - $select = $this->table->select()->where('id = ?', $nodeId); - $node = $this->table->fetchRow($select); + $node = $this->table->getNodeById($nodeId); $leftId = $node['left_id']; $rightId = $node['right_id']; @@ -121,11 +119,7 @@ public function validateNode($nodeId) // node if ($distance & 1) { // odd; valid - $selectChildren = $this->table->select()->where( - 'parent_id = ?', - $nodeId - )->order('left_id ASC'); - $children = $this->table->fetchAll($selectChildren); + $children = $this->table->getChildrenById($nodeId); foreach ($children as $child) { if ($this->validateNode($child['id']) === false) { return false;