-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6660 from morozov/table-editor
Introduce table editor
- Loading branch information
Showing
9 changed files
with
219 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Schema\Exception; | ||
|
||
use Doctrine\DBAL\Schema\SchemaException; | ||
use LogicException; | ||
|
||
/** @psalm-immutable */ | ||
final class InvalidTableDefinition extends LogicException implements SchemaException | ||
{ | ||
public static function nameNotSet(): self | ||
{ | ||
return new self('Table name is not set.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Schema; | ||
|
||
use Doctrine\DBAL\Schema\Exception\InvalidTableDefinition; | ||
|
||
final class TableEditor | ||
{ | ||
private ?string $name = null; | ||
|
||
/** @var array<Column> */ | ||
private array $columns = []; | ||
|
||
/** @var array<Index> */ | ||
private array $indexes = []; | ||
|
||
/** @var array<UniqueConstraint> */ | ||
private array $uniqueConstraints = []; | ||
|
||
/** @var array<ForeignKeyConstraint> */ | ||
private array $foreignKeyConstraints = []; | ||
|
||
/** @var array<string, mixed> */ | ||
private array $options = []; | ||
|
||
private ?TableConfiguration $configuration = null; | ||
|
||
/** @internal Use {@link Table::editor()} or {@link Table::edit()} to create an instance */ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function setName(string $name): self | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<Column> $columns */ | ||
public function setColumns(array $columns): self | ||
{ | ||
$this->columns = $columns; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<Index> $indexes */ | ||
public function setIndexes(array $indexes): self | ||
{ | ||
$this->indexes = $indexes; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<UniqueConstraint> $uniqueConstraints */ | ||
public function setUniqueConstraints(array $uniqueConstraints): self | ||
{ | ||
$this->uniqueConstraints = $uniqueConstraints; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<ForeignKeyConstraint> $foreignKeyConstraints */ | ||
public function setForeignKeyConstraints(array $foreignKeyConstraints): self | ||
{ | ||
$this->foreignKeyConstraints = $foreignKeyConstraints; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<string, mixed> $options */ | ||
public function setOptions(array $options): self | ||
{ | ||
$this->options = $options; | ||
|
||
return $this; | ||
} | ||
|
||
public function setConfiguration(TableConfiguration $configuration): self | ||
{ | ||
$this->configuration = $configuration; | ||
|
||
return $this; | ||
} | ||
|
||
public function create(): Table | ||
{ | ||
if ($this->name === null) { | ||
throw InvalidTableDefinition::nameNotSet(); | ||
} | ||
|
||
return new Table( | ||
$this->name, | ||
$this->columns, | ||
$this->indexes, | ||
$this->uniqueConstraints, | ||
$this->foreignKeyConstraints, | ||
$this->options, | ||
$this->configuration, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Schema; | ||
|
||
use Doctrine\DBAL\Schema\Exception\InvalidTableDefinition; | ||
use Doctrine\DBAL\Schema\Table; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TableEditorTest extends TestCase | ||
{ | ||
public function testSetName(): void | ||
{ | ||
$table = (new Table('accounts')) | ||
->edit() | ||
->setName('contacts') | ||
->create(); | ||
|
||
self::assertSame('contacts', $table->getName()); | ||
} | ||
|
||
public function testNameNotSet(): void | ||
{ | ||
$this->expectException(InvalidTableDefinition::class); | ||
|
||
Table::editor()->create(); | ||
} | ||
} |