Skip to content

Commit 9a8b783

Browse files
committed
Represent table indexes as UnqualifiedNamedObjectSet
1 parent 45549ae commit 9a8b783

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

src/Schema/Table.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
use function array_map;
3838
use function array_merge;
3939
use function array_shift;
40-
use function array_values;
4140
use function count;
4241
use function crc32;
4342
use function dechex;
@@ -62,8 +61,8 @@ final class Table extends AbstractNamedObject
6261
/** @var array<string, string> keys are new names, values are old names */
6362
private array $renamedColumns = [];
6463

65-
/** @var Index[] */
66-
private array $indexes = [];
64+
/** @var UnqualifiedNamedObjectSet<Index> */
65+
private UnqualifiedNamedObjectSet $indexes;
6766

6867
/**
6968
* The names of the indexes that were implicitly created as backing for foreign key constraints.
@@ -121,6 +120,10 @@ public function __construct(
121120
$columnsSet = new UnqualifiedNamedObjectSet(...$columns);
122121
$this->columns = $columnsSet;
123122

123+
/** @var UnqualifiedNamedObjectSet<Index> $indexSet */
124+
$indexSet = new UnqualifiedNamedObjectSet();
125+
$this->indexes = $indexSet;
126+
124127
/** @var OptionallyUnqualifiedNamedObjectSet<UniqueConstraint> $uniqueConstraintSet */
125128
$uniqueConstraintSet = new OptionallyUnqualifiedNamedObjectSet();
126129
$this->uniqueConstraints = $uniqueConstraintSet;
@@ -208,11 +211,11 @@ public function dropIndex(string $name): void
208211
{
209212
$parsedName = $this->parseUnqualifiedName($name);
210213

211-
if (! $this->hasIndex($name)) {
212-
throw IndexDoesNotExist::new($this->name, $parsedName);
214+
try {
215+
$this->indexes->remove($parsedName);
216+
} catch (ObjectDoesNotExist $e) {
217+
throw InvalidTableModification::indexDoesNotExist($this->name, $e);
213218
}
214-
215-
unset($this->indexes[$this->getObjectKey($parsedName)]);
216219
}
217220

218221
/**
@@ -237,22 +240,18 @@ public function renameIndex(string $oldName, ?string $newName = null): self
237240
{
238241
$parsedOldName = $this->parseUnqualifiedName($oldName);
239242

240-
if (! $this->hasIndex($oldName)) {
243+
$index = $this->indexes->get($parsedOldName);
244+
245+
if ($index === null) {
241246
throw IndexDoesNotExist::new($this->name, $parsedOldName);
242247
}
243248

244-
$index = $this->getIndex($oldName);
245-
246249
if ($newName !== null) {
247250
$parsedNewName = $this->parseUnqualifiedName($newName);
248251

249252
if ($this->getObjectKey($parsedOldName) === $this->getObjectKey($parsedNewName)) {
250253
return $this;
251254
}
252-
253-
if ($this->hasIndex($newName)) {
254-
throw IndexAlreadyExists::new($this->name, $parsedNewName);
255-
}
256255
} else {
257256
$parsedNewName = UnqualifiedName::unquoted(
258257
$this->generateNameFromObjectColumnNames(
@@ -269,9 +268,11 @@ public function renameIndex(string $oldName, ?string $newName = null): self
269268
->setName($parsedNewName)
270269
->create();
271270

272-
unset($this->indexes[$this->getObjectKey($parsedOldName)]);
271+
$this->_addIndex($index);
273272

274-
return $this->_addIndex($index);
273+
$this->indexes->remove($parsedOldName);
274+
275+
return $this;
275276
}
276277

277278
/**
@@ -682,7 +683,7 @@ public function hasIndex(string $name): bool
682683
{
683684
$parsedName = $this->parseUnqualifiedName($name);
684685

685-
return isset($this->indexes[$this->getObjectKey($parsedName)]);
686+
return $this->indexes->get($parsedName) !== null;
686687
}
687688

688689
/**
@@ -692,17 +693,19 @@ public function getIndex(string $name): Index
692693
{
693694
$parsedName = $this->parseUnqualifiedName($name);
694695

695-
if (! $this->hasIndex($name)) {
696+
$index = $this->indexes->get($parsedName);
697+
698+
if ($index === null) {
696699
throw IndexDoesNotExist::new($this->name, $parsedName);
697700
}
698701

699-
return $this->indexes[$this->getObjectKey($parsedName)];
702+
return $index;
700703
}
701704

702705
/** @return list<Index> */
703706
public function getIndexes(): array
704707
{
705-
return array_values($this->indexes);
708+
return $this->indexes->toList();
706709
}
707710

708711
/**
@@ -746,12 +749,8 @@ public function getOptions(): array
746749
*/
747750
public function __clone()
748751
{
749-
$this->columns = clone $this->columns;
750-
751-
foreach ($this->indexes as $k => $index) {
752-
$this->indexes[$k] = clone $index;
753-
}
754-
752+
$this->columns = clone $this->columns;
753+
$this->indexes = clone $this->indexes;
755754
$this->uniqueConstraints = clone $this->uniqueConstraints;
756755
$this->foreignKeyConstraints = clone $this->foreignKeyConstraints;
757756
}
@@ -771,34 +770,33 @@ private function _addColumn(Column $column): void
771770
private function _addIndex(Index $index): self
772771
{
773772
$indexName = $index->getObjectName();
774-
$indexKey = $this->getObjectKey($indexName);
775773

776774
$replacedImplicitIndexNames = new UnqualifiedNameSet();
777775

778776
foreach ($this->implicitIndexNames as $implicitIndexName) {
779-
$implicitIndexKey = $this->getObjectKey($implicitIndexName);
777+
$candidate = $this->indexes->get($implicitIndexName);
780778

781-
if (! isset($this->indexes[$implicitIndexKey])) {
779+
if ($candidate === null) {
782780
continue;
783781
}
784782

785-
if (! $this->indexes[$implicitIndexKey]->isFulfilledBy($index)) {
783+
if (! $candidate->isFulfilledBy($index)) {
786784
continue;
787785
}
788786

789787
$replacedImplicitIndexNames->add($implicitIndexName);
790788
}
791789

792-
if (isset($this->indexes[$indexKey]) && ! $replacedImplicitIndexNames->contains($indexName)) {
790+
if ($this->indexes->get($indexName) !== null && ! $replacedImplicitIndexNames->contains($indexName)) {
793791
throw IndexAlreadyExists::new($this->name, $indexName);
794792
}
795793

796794
foreach ($replacedImplicitIndexNames as $replacedImplicitIndexName) {
797-
unset($this->indexes[$this->getObjectKey($replacedImplicitIndexName)]);
795+
$this->indexes->remove($replacedImplicitIndexName);
798796
$this->implicitIndexNames->remove($replacedImplicitIndexName);
799797
}
800798

801-
$this->indexes[$indexKey] = $index;
799+
$this->indexes->add($index);
802800

803801
return $this;
804802
}
@@ -1124,7 +1122,7 @@ private function generateNameFromStringColumnNames(string $prefix, array $column
11241122

11251123
private function renameColumnInIndexes(string $oldKey, UnqualifiedName $newName): void
11261124
{
1127-
foreach ($this->indexes as $key => $index) {
1125+
foreach ($this->indexes as $index) {
11281126
$modified = false;
11291127
$columnNames = [];
11301128
foreach ($index->getIndexedColumns() as $indexedColumn) {
@@ -1141,9 +1139,11 @@ private function renameColumnInIndexes(string $oldKey, UnqualifiedName $newName)
11411139
continue;
11421140
}
11431141

1144-
$this->indexes[$key] = $index->edit()
1145-
->setColumnNames(...$columnNames)
1146-
->create();
1142+
$this->indexes->modify($index->getObjectName(), static function (Index $index) use ($columnNames): Index {
1143+
return $index->edit()
1144+
->setColumnNames(...$columnNames)
1145+
->create();
1146+
});
11471147
}
11481148
}
11491149

0 commit comments

Comments
 (0)