Skip to content

Commit c6cdcfb

Browse files
committed
update tests
1 parent 29af244 commit c6cdcfb

File tree

7 files changed

+91
-49
lines changed

7 files changed

+91
-49
lines changed

api.include.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4181,8 +4181,8 @@ public function updateTable(string $tableName, /* object */ $changes): bool
41814181
{
41824182
$table = $this->reflection->getTable($tableName);
41834183
$newTable = ReflectedTable::fromJson((object) array_merge((array) $table->jsonSerialize(), (array) $changes));
4184-
if ($table->getName() != $newTable->getName()) {
4185-
if (!$this->db->definition()->renameTable($table->getName(), $newTable->getName())) {
4184+
if ($table->getRealName() != $newTable->getRealName()) {
4185+
if (!$this->db->definition()->renameTable($table->getRealName(), $newTable->getRealName())) {
41864186
return false;
41874187
}
41884188
}

api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4181,8 +4181,8 @@ public function updateTable(string $tableName, /* object */ $changes): bool
41814181
{
41824182
$table = $this->reflection->getTable($tableName);
41834183
$newTable = ReflectedTable::fromJson((object) array_merge((array) $table->jsonSerialize(), (array) $changes));
4184-
if ($table->getName() != $newTable->getName()) {
4185-
if (!$this->db->definition()->renameTable($table->getName(), $newTable->getName())) {
4184+
if ($table->getRealName() != $newTable->getRealName()) {
4185+
if (!$this->db->definition()->renameTable($table->getRealName(), $newTable->getRealName())) {
41864186
return false;
41874187
}
41884188
}

src/Tqdev/PhpCrudApi/Column/DefinitionService.php

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,26 @@ public function __construct(GenericDB $db, ReflectionService $reflection)
1717
$this->reflection = $reflection;
1818
}
1919

20-
public function updateTable(string $tableName, /* object */ $changes): bool
20+
public function updateTable(ReflectedTable $table, /* object */ $changes): bool
2121
{
22-
$table = $this->reflection->getTable($tableName);
2322
$newTable = ReflectedTable::fromJson((object) array_merge((array) $table->jsonSerialize(), (array) $changes));
24-
if ($table->getName() != $newTable->getName()) {
25-
if (!$this->db->definition()->renameTable($table->getName(), $newTable->getName())) {
23+
if ($table->getRealName() != $newTable->getRealName()) {
24+
if (!$this->db->definition()->renameTable($table->getRealName(), $newTable->getRealName())) {
2625
return false;
2726
}
2827
}
2928
return true;
3029
}
3130

32-
public function updateColumn(string $tableName, string $columnName, /* object */ $changes): bool
31+
public function updateColumn(ReflectedTable $table, ReflectedColumn $column, /* object */ $changes): bool
3332
{
34-
$table = $this->reflection->getTable($tableName);
35-
$column = $table->getColumn($columnName);
36-
3733
// remove constraints on other column
3834
$newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
3935
if ($newColumn->getPk() != $column->getPk() && $table->hasPk()) {
4036
$oldColumn = $table->getPk();
41-
if ($oldColumn->getName() != $columnName) {
37+
if ($oldColumn->getRealName() != $column->getRealName()) {
4238
$oldColumn->setPk(false);
43-
if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $oldColumn->getName(), $oldColumn)) {
39+
if (!$this->db->definition()->removeColumnPrimaryKey($table->getRealName(), $oldColumn->getRealName(), $oldColumn)) {
4440
return false;
4541
}
4642
}
@@ -49,12 +45,12 @@ public function updateColumn(string $tableName, string $columnName, /* object */
4945
// remove constraints
5046
$newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), ['pk' => false, 'fk' => false]));
5147
if ($newColumn->getPk() != $column->getPk() && !$newColumn->getPk()) {
52-
if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $column->getName(), $newColumn)) {
48+
if (!$this->db->definition()->removeColumnPrimaryKey($table->getRealName(), $column->getRealName(), $newColumn)) {
5349
return false;
5450
}
5551
}
5652
if ($newColumn->getFk() != $column->getFk() && !$newColumn->getFk()) {
57-
if (!$this->db->definition()->removeColumnForeignKey($table->getName(), $column->getName(), $newColumn)) {
53+
if (!$this->db->definition()->removeColumnForeignKey($table->getRealName(), $column->getRealName(), $newColumn)) {
5854
return false;
5955
}
6056
}
@@ -63,8 +59,8 @@ public function updateColumn(string $tableName, string $columnName, /* object */
6359
$newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
6460
$newColumn->setPk(false);
6561
$newColumn->setFk('');
66-
if ($newColumn->getName() != $column->getName()) {
67-
if (!$this->db->definition()->renameColumn($table->getName(), $column->getName(), $newColumn)) {
62+
if ($newColumn->getRealName() != $column->getRealName()) {
63+
if (!$this->db->definition()->renameColumn($table->getRealName(), $column->getRealName(), $newColumn)) {
6864
return false;
6965
}
7066
}
@@ -74,25 +70,25 @@ public function updateColumn(string $tableName, string $columnName, /* object */
7470
$newColumn->getPrecision() != $column->getPrecision() ||
7571
$newColumn->getScale() != $column->getScale()
7672
) {
77-
if (!$this->db->definition()->retypeColumn($table->getName(), $newColumn->getName(), $newColumn)) {
73+
if (!$this->db->definition()->retypeColumn($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
7874
return false;
7975
}
8076
}
8177
if ($newColumn->getNullable() != $column->getNullable()) {
82-
if (!$this->db->definition()->setColumnNullable($table->getName(), $newColumn->getName(), $newColumn)) {
78+
if (!$this->db->definition()->setColumnNullable($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
8379
return false;
8480
}
8581
}
8682

8783
// add constraints
8884
$newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
8985
if ($newColumn->getFk()) {
90-
if (!$this->db->definition()->addColumnForeignKey($table->getName(), $newColumn->getName(), $newColumn)) {
86+
if (!$this->db->definition()->addColumnForeignKey($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
9187
return false;
9288
}
9389
}
9490
if ($newColumn->getPk()) {
95-
if (!$this->db->definition()->addColumnPrimaryKey($table->getName(), $newColumn->getName(), $newColumn)) {
91+
if (!$this->db->definition()->addColumnPrimaryKey($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
9692
return false;
9793
}
9894
}
@@ -108,50 +104,48 @@ public function addTable(/* object */$definition)
108104
return true;
109105
}
110106

111-
public function addColumn(string $tableName, /* object */ $definition)
107+
public function addColumn(ReflectedTable $table, /* object */ $definition)
112108
{
113109
$newColumn = ReflectedColumn::fromJson($definition);
114-
if (!$this->db->definition()->addColumn($tableName, $newColumn)) {
110+
if (!$this->db->definition()->addColumn($table->getRealName(), $newColumn)) {
115111
return false;
116112
}
117113
if ($newColumn->getFk()) {
118-
if (!$this->db->definition()->addColumnForeignKey($tableName, $newColumn->getName(), $newColumn)) {
114+
if (!$this->db->definition()->addColumnForeignKey($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
119115
return false;
120116
}
121117
}
122118
if ($newColumn->getPk()) {
123-
if (!$this->db->definition()->addColumnPrimaryKey($tableName, $newColumn->getName(), $newColumn)) {
119+
if (!$this->db->definition()->addColumnPrimaryKey($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
124120
return false;
125121
}
126122
}
127123
return true;
128124
}
129125

130-
public function removeTable(string $tableName)
126+
public function removeTable(ReflectedTable $table)
131127
{
132-
if (!$this->db->definition()->removeTable($tableName)) {
128+
if (!$this->db->definition()->removeTable($table->getRealName())) {
133129
return false;
134130
}
135131
return true;
136132
}
137133

138-
public function removeColumn(string $tableName, string $columnName)
134+
public function removeColumn(ReflectedTable $table, ReflectedColumn $column)
139135
{
140-
$table = $this->reflection->getTable($tableName);
141-
$newColumn = $table->getColumn($columnName);
142-
if ($newColumn->getPk()) {
143-
$newColumn->setPk(false);
144-
if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $newColumn->getName(), $newColumn)) {
136+
if ($column->getPk()) {
137+
$column->setPk(false);
138+
if (!$this->db->definition()->removeColumnPrimaryKey($table->getRealName(), $column->getRealName(), $column)) {
145139
return false;
146140
}
147141
}
148-
if ($newColumn->getFk()) {
149-
$newColumn->setFk("");
150-
if (!$this->db->definition()->removeColumnForeignKey($tableName, $columnName, $newColumn)) {
142+
if ($column->getFk()) {
143+
$column->setFk("");
144+
if (!$this->db->definition()->removeColumnForeignKey($table->getRealName(), $column->getRealName(), $column)) {
151145
return false;
152146
}
153147
}
154-
if (!$this->db->definition()->removeColumn($tableName, $columnName)) {
148+
if (!$this->db->definition()->removeColumn($table->getRealName(), $column->getRealName())) {
155149
return false;
156150
}
157151
return true;

src/Tqdev/PhpCrudApi/Controller/ColumnController.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public function updateTable(ServerRequestInterface $request): ResponseInterface
7373
if (!$this->reflection->hasTable($tableName)) {
7474
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
7575
}
76-
$success = $this->definition->updateTable($tableName, $request->getParsedBody());
76+
$table = $this->reflection->getTable($tableName);
77+
$success = $this->definition->updateTable($table, $request->getParsedBody());
7778
if ($success) {
7879
$this->reflection->refreshTables();
7980
}
@@ -91,7 +92,8 @@ public function updateColumn(ServerRequestInterface $request): ResponseInterface
9192
if (!$table->hasColumn($columnName)) {
9293
return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
9394
}
94-
$success = $this->definition->updateColumn($tableName, $columnName, $request->getParsedBody());
95+
$column = $table->getColumn($columnName);
96+
$success = $this->definition->updateColumn($table, $column, $request->getParsedBody());
9597
if ($success) {
9698
$this->reflection->refreshTable($tableName);
9799
}
@@ -122,7 +124,7 @@ public function addColumn(ServerRequestInterface $request): ResponseInterface
122124
if ($table->hasColumn($columnName)) {
123125
return $this->responder->error(ErrorCode::COLUMN_ALREADY_EXISTS, $columnName);
124126
}
125-
$success = $this->definition->addColumn($tableName, $request->getParsedBody());
127+
$success = $this->definition->addColumn($table, $request->getParsedBody());
126128
if ($success) {
127129
$this->reflection->refreshTable($tableName);
128130
}
@@ -135,7 +137,8 @@ public function removeTable(ServerRequestInterface $request): ResponseInterface
135137
if (!$this->reflection->hasTable($tableName)) {
136138
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
137139
}
138-
$success = $this->definition->removeTable($tableName);
140+
$table = $this->reflection->getTable($tableName);
141+
$success = $this->definition->removeTable($table);
139142
if ($success) {
140143
$this->reflection->refreshTables();
141144
}
@@ -153,7 +156,8 @@ public function removeColumn(ServerRequestInterface $request): ResponseInterface
153156
if (!$table->hasColumn($columnName)) {
154157
return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
155158
}
156-
$success = $this->definition->removeColumn($tableName, $columnName);
159+
$column = $table->getColumn($columnName);
160+
$success = $this->definition->removeColumn($table, $column);
157161
if ($success) {
158162
$this->reflection->refreshTable($tableName);
159163
}

src/Tqdev/PhpCrudApi/Database/GenericDefinition.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function getColumnRenameSQL(string $tableName, string $columnName, Refle
107107
{
108108
$p1 = $this->quote($tableName);
109109
$p2 = $this->quote($columnName);
110-
$p3 = $this->quote($newColumn->getName());
110+
$p3 = $this->quote($newColumn->getRealName());
111111

112112
switch ($this->driver) {
113113
case 'mysql':
@@ -127,7 +127,7 @@ private function getColumnRetypeSQL(string $tableName, string $columnName, Refle
127127
{
128128
$p1 = $this->quote($tableName);
129129
$p2 = $this->quote($columnName);
130-
$p3 = $this->quote($newColumn->getName());
130+
$p3 = $this->quote($newColumn->getRealName());
131131
$p4 = $this->getColumnType($newColumn, true);
132132

133133
switch ($this->driver) {
@@ -144,7 +144,7 @@ private function getSetColumnNullableSQL(string $tableName, string $columnName,
144144
{
145145
$p1 = $this->quote($tableName);
146146
$p2 = $this->quote($columnName);
147-
$p3 = $this->quote($newColumn->getName());
147+
$p3 = $this->quote($newColumn->getRealName());
148148
$p4 = $this->getColumnType($newColumn, true);
149149

150150
switch ($this->driver) {
@@ -216,7 +216,7 @@ private function getSetColumnPkDefaultSQL(string $tableName, string $columnName,
216216

217217
switch ($this->driver) {
218218
case 'mysql':
219-
$p3 = $this->quote($newColumn->getName());
219+
$p3 = $this->quote($newColumn->getRealName());
220220
$p4 = $this->getColumnType($newColumn, true);
221221
return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
222222
case 'pgsql':
@@ -265,7 +265,7 @@ private function getRemoveColumnFkConstraintSQL(string $tableName, string $colum
265265

266266
private function getAddTableSQL(ReflectedTable $newTable): string
267267
{
268-
$tableName = $newTable->getName();
268+
$tableName = $newTable->getRealName();
269269
$p1 = $this->quote($tableName);
270270
$fields = [];
271271
$constraints = [];
@@ -304,7 +304,7 @@ private function getAddTableSQL(ReflectedTable $newTable): string
304304
private function getAddColumnSQL(string $tableName, ReflectedColumn $newColumn): string
305305
{
306306
$p1 = $this->quote($tableName);
307-
$p2 = $this->quote($newColumn->getName());
307+
$p2 = $this->quote($newColumn->getRealName());
308308
$p3 = $this->getColumnType($newColumn, false);
309309

310310
switch ($this->driver) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
===
2+
GET /columns/posts
3+
===
4+
200
5+
Content-Type: application/json; charset=utf-8
6+
Content-Length: 347
7+
8+
{"name":"abc_posts","alias":"posts","type":"table","columns":[{"name":"abc_id","alias":"id","type":"integer","pk":true},{"name":"abc_user_id","alias":"user_id","type":"integer","fk":"users"},{"name":"abc_category_id","alias":"category_id","type":"integer","fk":"categories"},{"name":"abc_content","alias":"content","type":"varchar","length":255}]}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
===
2+
PUT /columns/posts
3+
4+
{"name":"posts2"}
5+
===
6+
200
7+
Content-Type: application/json; charset=utf-8
8+
Content-Length: 4
9+
10+
true
11+
===
12+
GET /columns/posts2
13+
===
14+
200
15+
Content-Type: application/json; charset=utf-8
16+
Content-Length: 257
17+
18+
{"name":"posts2","type":"table","columns":[{"name":"abc_id","type":"integer","pk":true},{"name":"abc_user_id","type":"integer","fk":"users"},{"name":"abc_category_id","type":"integer","fk":"categories"},{"name":"abc_content","type":"varchar","length":255}]}
19+
===
20+
PUT /columns/posts2
21+
22+
{"name":"posts"}
23+
===
24+
200
25+
Content-Type: application/json; charset=utf-8
26+
Content-Length: 4
27+
28+
true
29+
===
30+
GET /columns/posts
31+
===
32+
200
33+
Content-Type: application/json; charset=utf-8
34+
Content-Length: 347
35+
36+
{"name":"abc_posts","alias":"posts","type":"table","columns":[{"name":"abc_id","alias":"id","type":"integer","pk":true},{"name":"abc_user_id","alias":"user_id","type":"integer","fk":"users"},{"name":"abc_category_id","alias":"category_id","type":"integer","fk":"categories"},{"name":"abc_content","alias":"content","type":"varchar","length":255}]}

0 commit comments

Comments
 (0)