Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/NestedSetSchemaInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PNX\NestedSet;

/**
* Provides Nested Set schema operations.
*/
interface NestedSetSchemaInterface {

/**
* Creates the nested set table.
*/
public function create();

/**
* Drops the nested set table.
*/
public function drop();

}
65 changes: 65 additions & 0 deletions src/Storage/BaseDbalStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace PNX\NestedSet\Storage;

use Doctrine\DBAL\Connection;

/**
* Abstract base class for DBAL storage classes.
*/
abstract class BaseDbalStorage {

/**
* The regex for validating table names.
*/
const VALID_TABLE_REGEX = '/^[a-zA-Z]\w{1,64}$/';

/**
* The database connection.
*
* @var \Doctrine\DBAL\Connection
*/
protected $connection;

/**
* The table name to use for storing the nested set.
*
* @var string
*/
protected $tableName;

/**
* DbalTree constructor.
*
* @param \Doctrine\DBAL\Connection $connection
* The database connection.
* @param string $tableName
* (optional) The table name to use.
*/
public function __construct(Connection $connection, $tableName = 'tree') {
$this->connection = $connection;
if (!$this->validTableName($tableName)) {
throw new \InvalidArgumentException("Table name must match the regex " . self::VALID_TABLE_REGEX);
}
$this->tableName = $tableName;
}

/**
* Checks if the table name is valid.
*
* Table names must:
* - start with a letter
* - only contain letters, numbers, and underscores
* - be maximum 64 characters.
*
* @param string $tableName
* The table name.
*
* @return bool
* TRUE if the table name is valid.
*/
protected function validTableName($tableName) {
return (bool) preg_match(self::VALID_TABLE_REGEX, $tableName);
}

}
44 changes: 4 additions & 40 deletions src/Storage/DbalNestedSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,14 @@

namespace PNX\NestedSet\Storage;

use Doctrine\DBAL\Connection;
use Exception;
use PNX\NestedSet\Node;
use PNX\NestedSet\NestedSetInterface;
use PNX\NestedSet\Node;

/**
* Provides a DBAL implementation of a Tree.
*/
class DbalNestedSet implements NestedSetInterface {

/**
* The regex for validating table names.
*/
const VALID_TABLE_REGEX = '/^[a-zA-Z]\w{1,64}$/';

/**
* The database connection.
*
* @var \Doctrine\DBAL\Connection
*/
protected $connection;

/**
* The table name to use for storing the nested set.
*
* @var string
*/
protected $tableName;

/**
* DbalTree constructor.
*
* @param \Doctrine\DBAL\Connection $connection
* The database connection.
* @param string $tableName
* (optional) The table name to use.
*/
public function __construct(Connection $connection, $tableName = 'tree') {
$this->connection = $connection;
if (!preg_match(self::VALID_TABLE_REGEX, $tableName)) {
throw new \InvalidArgumentException("Table name must match the regex " . self::VALID_TABLE_REGEX);
}
$this->tableName = $tableName;
}
class DbalNestedSet extends BaseDbalStorage implements NestedSetInterface {

/**
* {@inheritdoc}
Expand Down Expand Up @@ -169,10 +133,10 @@ public function findDescendants(Node $node, $depth = 0) {
$query->select('child.id', 'child.revision_id', 'child.left_pos', 'child.right_pos', 'child.depth')
->from($this->tableName, 'child')
->from($this->tableName, 'parent')
->where('child.left_pos > parent.left_pos')
->andWhere('child.right_pos < parent.right_pos')
->andWhere('parent.id = :id')
->andWhere('parent.revision_id = :revision_id')
->andwhere('child.left_pos > parent.left_pos')
->andWhere('child.right_pos < parent.right_pos')
->setParameter(':id', $node->getId())
->setParameter(':revision_id', $node->getRevisionId());
if ($depth > 0) {
Expand Down
46 changes: 46 additions & 0 deletions src/Storage/DbalNestedSetSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace PNX\NestedSet\Storage;

use Doctrine\DBAL\Schema\Schema;
use PNX\NestedSet\NestedSetSchemaInterface;

/**
* Provides DBAL Nested set schema operations.
*/
class DbalNestedSetSchema extends BaseDbalStorage implements NestedSetSchemaInterface {

/**
* {@inheritdoc}
*/
public function create() {
$schema = new Schema();
$tree = $schema->createTable($this->tableName);
$tree->addColumn("id", "integer", ["unsigned" => TRUE]);
$tree->addColumn("revision_id", "integer", ["unsigned" => TRUE]);
$tree->addColumn("left_pos", "integer", ["unsigned" => TRUE]);
$tree->addColumn("right_pos", "integer", ["unsigned" => TRUE]);
$tree->addColumn("depth", "integer", ["unsigned" => TRUE]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need indexes on any column you use in where clauses.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah!


$tree->setPrimaryKey(['id', 'revision_id']);
$tree->addIndex(['id', 'revision_id', 'left_pos', 'right_pos', 'depth']);
$tree->addIndex(['left_pos', 'right_pos']);
$tree->addIndex(['right_pos']);

foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
$this->connection->exec($sql);
}
}

/**
* {@inheritdoc}
*/
public function drop() {
$schema = new Schema();
$schema->dropTable($this->tableName);
foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
$this->connection->exec($sql);
}
}

}
53 changes: 16 additions & 37 deletions tests/Functional/DbalNestedSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Console_Table;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Schema;
use PNX\NestedSet\Node;
use PNX\NestedSet\Storage\DbalNestedSet;
use PNX\NestedSet\Storage\DbalNestedSetSchema;

/**
* Tests the Dbal Tree implementation.
Expand Down Expand Up @@ -37,18 +37,25 @@ class DbalNestedSetTest extends \PHPUnit_Framework_TestCase {
*/
protected $tableName = 'nested_set';

/**
* The nested set schema.
*
* @var \PNX\NestedSet\Storage\DbalNestedSetSchema
*/
protected $schema;

/**
* {@inheritdoc}
*/
protected function setUp() {
if ($this->connection === NULL) {
$this->connection = DriverManager::getConnection([
'url' => 'sqlite:///:memory:',
], new Configuration());
$this->createTable();
$this->loadTestData();
$this->nestedSet = new DbalNestedSet($this->connection, $this->tableName);
}
$this->connection = DriverManager::getConnection([
'url' => 'sqlite:///:memory:',
], new Configuration());

$this->schema = new DbalNestedSetSchema($this->connection, $this->tableName);
$this->schema->create();
$this->loadTestData();
$this->nestedSet = new DbalNestedSet($this->connection, $this->tableName);
}

/**
Expand Down Expand Up @@ -423,34 +430,6 @@ public function testValidateTableInvalidFirstChars() {
$this->nestedSet = new DbalNestedSet($this->connection, "1abc");
}

/**
* Drops the table.
*/
protected function dropTable() {
$schema = new Schema();
$schema->dropTable("tree");
foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
$this->connection->exec($sql);
}
}

/**
* Creates the table.
*/
protected function createTable() {
$schema = new Schema();
$tree = $schema->createTable($this->tableName);
$tree->addColumn("id", "integer", ["unsigned" => TRUE]);
$tree->addColumn("revision_id", "integer", ["unsigned" => TRUE]);
$tree->addColumn("left_pos", "integer", ["unsigned" => TRUE]);
$tree->addColumn("right_pos", "integer", ["unsigned" => TRUE]);
$tree->addColumn("depth", "integer", ["unsigned" => TRUE]);

foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
$this->connection->exec($sql);
}
}

/**
* Loads the test data.
*/
Expand Down