Skip to content

Commit 15498d0

Browse files
committed
Table: added removing methods
1 parent 46020e9 commit 15498d0

3 files changed

Lines changed: 118 additions & 0 deletions

File tree

src/Table.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ public function setOption($name, $value)
7474
}
7575

7676

77+
/**
78+
* @param string $name
79+
* @return self
80+
*/
81+
public function removeOption($name)
82+
{
83+
unset($this->options[$name]);
84+
return $this;
85+
}
86+
87+
7788
/**
7889
* @return array<string, string>
7990
*/
@@ -110,6 +121,24 @@ public function addColumn($name, $type = NULL, array $parameters = NULL, array $
110121
}
111122

112123

124+
/**
125+
* @param string|Column $name
126+
* @return void
127+
*/
128+
public function removeColumn($name)
129+
{
130+
if ($name instanceof Column) {
131+
$name = $name->getName();
132+
}
133+
134+
if (!isset($this->columns[$name])) {
135+
throw new MissingException("Column '$name' in table '{$this->getName()}' not exists.");
136+
}
137+
138+
unset($this->columns[$name]);
139+
}
140+
141+
113142
/**
114143
* @param string $name
115144
* @return Column|NULL
@@ -159,6 +188,24 @@ public function addIndex($name, $columns = [], $type = Index::TYPE_INDEX)
159188
}
160189

161190

191+
/**
192+
* @param string|Index $name
193+
* @return void
194+
*/
195+
public function removeIndex($name)
196+
{
197+
if ($name instanceof Index) {
198+
$name = $name->getName();
199+
}
200+
201+
if (!isset($this->indexes[$name])) {
202+
throw new MissingException("Index '$name' in table '{$this->getName()}' not exists.");
203+
}
204+
205+
unset($this->indexes[$name]);
206+
}
207+
208+
162209
/**
163210
* @param string $name
164211
* @return Index|NULL
@@ -209,6 +256,24 @@ public function addForeignKey($name, $columns = [], $targetTable = NULL, $target
209256
}
210257

211258

259+
/**
260+
* @param string|ForeignKey $name
261+
* @return void
262+
*/
263+
public function removeForeignKey($name)
264+
{
265+
if ($name instanceof ForeignKey) {
266+
$name = $name->getName();
267+
}
268+
269+
if (!isset($this->foreignKeys[$name])) {
270+
throw new MissingException("Foreign key '$name' in table '{$this->getName()}' not exists.");
271+
}
272+
273+
unset($this->foreignKeys[$name]);
274+
}
275+
276+
212277
/**
213278
* @param string $name
214279
* @return ForeignKey|NULL

src/exceptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class EmptyException extends Exception
1818
}
1919

2020

21+
class MissingException extends Exception
22+
{
23+
}
24+
25+
2126
class OutOfRangeException extends Exception
2227
{
2328
}

tests/SqlSchema/Table.phpt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ test(function () {
8484
});
8585

8686

87+
test(function () {
88+
Assert::exception(function () {
89+
$table = new Table('book');
90+
$table->removeColumn('id');
91+
92+
}, CzProject\SqlSchema\MissingException::class, "Column 'id' in table 'book' not exists.");
93+
94+
$table = new Table('book');
95+
$column = $table->addColumn('id', 'INT');
96+
Assert::same(1, count($table->getColumns()));
97+
98+
$table->removeColumn($column);
99+
Assert::same(0, count($table->getColumns()));
100+
});
101+
102+
87103
test(function () {
88104
Assert::exception(function () {
89105
$table = new Table('book');
@@ -101,6 +117,22 @@ test(function () {
101117
});
102118

103119

120+
test(function () {
121+
Assert::exception(function () {
122+
$table = new Table('book');
123+
$table->removeIndex('id');
124+
125+
}, CzProject\SqlSchema\MissingException::class, "Index 'id' in table 'book' not exists.");
126+
127+
$table = new Table('book');
128+
$index = $table->addIndex('id', Index::TYPE_INDEX);
129+
Assert::same(1, count($table->getIndexes()));
130+
131+
$table->removeIndex($index);
132+
Assert::same(0, count($table->getIndexes()));
133+
});
134+
135+
104136
test(function () {
105137
Assert::exception(function () {
106138
$table = new Table('book');
@@ -116,3 +148,19 @@ test(function () {
116148

117149
}, CzProject\SqlSchema\DuplicateException::class, "Foreign key 'author_id' in table 'book' already exists.");
118150
});
151+
152+
153+
test(function () {
154+
Assert::exception(function () {
155+
$table = new Table('book');
156+
$table->removeForeignKey('author_id');
157+
158+
}, CzProject\SqlSchema\MissingException::class, "Foreign key 'author_id' in table 'book' not exists.");
159+
160+
$table = new Table('book');
161+
$foreignKey = $table->addForeignKey('author_id', [], 'author');
162+
Assert::same(1, count($table->getForeignKeys()));
163+
164+
$table->removeForeignKey($foreignKey);
165+
Assert::same(0, count($table->getForeignKeys()));
166+
});

0 commit comments

Comments
 (0)