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
3 changes: 0 additions & 3 deletions src/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class Node {
* The depth.
*/
public function __construct(NodeKey $nodeKey, int $left, int $right, int $depth) {
if ($nodeKey == NULL) {
throw new \InvalidArgumentException("Node key cannot be NULL");
}
$this->nodeKey = $nodeKey;
if ($left < 1) {
throw new \InvalidArgumentException("Left value must be > 0");
Expand Down
116 changes: 88 additions & 28 deletions src/Storage/DbalNestedSetSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,120 @@

namespace PNX\NestedSet\Storage;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use PNX\NestedSet\NestedSetSchemaInterface;

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

/**
* The table columns.
*/
private const COLUMNS = ['id', 'revision_id', 'left_pos', 'right_pos', 'depth'];

/**
* The primary key columns.
*/
private const PRIMARY_KEY = ['id', 'revision_id'];

/**
* The columns of each index.
*/
private const INDEXES = [
['revision_id'],
['id', 'revision_id', 'left_pos', 'right_pos', 'depth'],
['left_pos', 'right_pos'],
['right_pos'],
];

/**
* {@inheritdoc}
*/
public function create(): void {
$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]);

if (\class_exists('\Doctrine\DBAL\Schema\Name\UnqualifiedName')) {
$schema = new Schema([$this->buildTable()]);
foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
$this->connection->executeStatement($sql);
}
}

/**
* {@inheritdoc}
*/
public function drop(): void {
$this->connection->executeStatement(
$this->connection->getDatabasePlatform()->getDropTableSQL($this->tableName)
);
}

/**
* Builds the nested set table definition.
*/
private function buildTable(): Table {
// Version shim: only constant for a single installed dbal version.
// @phpstan-ignore function.alreadyNarrowedType
if (\method_exists(Table::class, 'editor')) {
// doctrine/dbal >= 4.5.
$editor = Table::editor()->setUnquotedName($this->tableName);
foreach (self::COLUMNS as $column) {
// @phpstan-ignore staticMethod.notFound
$editor->addColumn(Column::editor()->setUnquotedName($column)->setTypeName('integer')->setUnsigned(TRUE)->create());
}
// @phpstan-ignore class.notFound
$editor->addPrimaryKeyConstraint($this->buildPrimaryKeyConstraint());
foreach (self::INDEXES as $columns) {
// @phpstan-ignore staticMethod.notFound
$editor->addIndex(Index::editor()->setUnquotedColumnNames(...$columns));
}
return $editor->create();
}

// doctrine/dbal 3 and < 4.5.
$table = new Table($this->tableName);
foreach (self::COLUMNS as $column) {
// @phpstan-ignore method.deprecated
$table->addColumn($column, 'integer', ['unsigned' => TRUE]);
}
if (\class_exists(UnqualifiedName::class)) {
// doctrine/dbal 4.
// @phpstan-ignore method.notFound, class.notFound
$tree->addPrimaryKeyConstraint(new PrimaryKeyConstraint(UnqualifiedName::unquoted('primary'), [
UnqualifiedName::unquoted('id'),
UnqualifiedName::unquoted('revision_id'),
], FALSE));
// @phpstan-ignore method.deprecated, method.notFound
$table->addPrimaryKeyConstraint($this->buildPrimaryKeyConstraint());
}
else {
// doctrine/dbal 3.
// @phpstan-ignore method.deprecated
$tree->setPrimaryKey(['id', 'revision_id']);
$table->setPrimaryKey(self::PRIMARY_KEY);
}
$tree->addIndex(['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->executeStatement($sql);
foreach (self::INDEXES as $columns) {
// @phpstan-ignore method.deprecated
$table->addIndex($columns);
}
return $table;
}

/**
* {@inheritdoc}
* Builds the primary key constraint for doctrine/dbal 4.
*
* @return \Doctrine\DBAL\Schema\PrimaryKeyConstraint
* The primary key constraint.
*/
public function drop(): void {
$schema = new Schema();
$schema->dropTable($this->tableName);
foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
$this->connection->executeStatement($sql);
// @phpstan-ignore class.notFound
private function buildPrimaryKeyConstraint(): object {
$columnNames = [];
foreach (self::PRIMARY_KEY as $column) {
// @phpstan-ignore class.notFound
$columnNames[] = UnqualifiedName::unquoted($column);
}
// @phpstan-ignore class.notFound
$name = UnqualifiedName::unquoted('primary');
// @phpstan-ignore class.notFound
return new PrimaryKeyConstraint($name, $columnNames, FALSE);
}

}
Loading