Skip to content

Commit b6ef2e9

Browse files
committed
documentation
1 parent b790113 commit b6ef2e9

File tree

4 files changed

+120
-123
lines changed

4 files changed

+120
-123
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ These are all the configuration options and their default value between brackets
5757
- "username": Username of the user connecting to the database (no default)
5858
- "password": Password of the user connecting to the database (no default)
5959
- "database": Database the connecting is made to (no default)
60-
- "mapping": Comma separated list of table/column mappings (no mappping)
6160
- "tables": Comma separated list of tables to publish (defaults to 'all')
61+
- "mapping": Comma separated list of table/column mappings (no mappping)
6262
- "middlewares": List of middlewares to load (`cors`)
6363
- "controllers": List of controllers to load (`records,geojson,openapi,status`)
6464
- "customControllers": List of user custom controllers to load (no default)
@@ -719,6 +719,7 @@ You can tune the middleware behavior using middleware specific configuration par
719719
- "reconnect.portHandler": Handler to implement retrieval of the database port ("")
720720
- "reconnect.databaseHandler": Handler to implement retrieval of the database name ("")
721721
- "reconnect.tablesHandler": Handler to implement retrieval of the table names ("")
722+
- "reconnect.mappingHandler": Handler to implement retrieval of the name mapping ("")
722723
- "reconnect.usernameHandler": Handler to implement retrieval of the database username ("")
723724
- "reconnect.passwordHandler": Handler to implement retrieval of the database password ("")
724725
- "authorization.tableHandler": Handler to implement table authorization rules ("")

api.include.php

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4177,9 +4177,8 @@ public function __construct(GenericDB $db, ReflectionService $reflection)
41774177
$this->reflection = $reflection;
41784178
}
41794179

4180-
public function updateTable(string $tableName, /* object */ $changes): bool
4180+
public function updateTable(ReflectedTable $table, /* object */ $changes): bool
41814181
{
4182-
$table = $this->reflection->getTable($tableName);
41834182
$newTable = ReflectedTable::fromJson((object) array_merge((array) $table->jsonSerialize(), (array) $changes));
41844183
if ($table->getRealName() != $newTable->getRealName()) {
41854184
if (!$this->db->definition()->renameTable($table->getRealName(), $newTable->getRealName())) {
@@ -4189,18 +4188,15 @@ public function updateTable(string $tableName, /* object */ $changes): bool
41894188
return true;
41904189
}
41914190

4192-
public function updateColumn(string $tableName, string $columnName, /* object */ $changes): bool
4191+
public function updateColumn(ReflectedTable $table, ReflectedColumn $column, /* object */ $changes): bool
41934192
{
4194-
$table = $this->reflection->getTable($tableName);
4195-
$column = $table->getColumn($columnName);
4196-
41974193
// remove constraints on other column
41984194
$newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
41994195
if ($newColumn->getPk() != $column->getPk() && $table->hasPk()) {
42004196
$oldColumn = $table->getPk();
4201-
if ($oldColumn->getName() != $columnName) {
4197+
if ($oldColumn->getRealName() != $column->getRealName()) {
42024198
$oldColumn->setPk(false);
4203-
if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $oldColumn->getName(), $oldColumn)) {
4199+
if (!$this->db->definition()->removeColumnPrimaryKey($table->getRealName(), $oldColumn->getRealName(), $oldColumn)) {
42044200
return false;
42054201
}
42064202
}
@@ -4209,12 +4205,12 @@ public function updateColumn(string $tableName, string $columnName, /* object */
42094205
// remove constraints
42104206
$newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), ['pk' => false, 'fk' => false]));
42114207
if ($newColumn->getPk() != $column->getPk() && !$newColumn->getPk()) {
4212-
if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $column->getName(), $newColumn)) {
4208+
if (!$this->db->definition()->removeColumnPrimaryKey($table->getRealName(), $column->getRealName(), $newColumn)) {
42134209
return false;
42144210
}
42154211
}
42164212
if ($newColumn->getFk() != $column->getFk() && !$newColumn->getFk()) {
4217-
if (!$this->db->definition()->removeColumnForeignKey($table->getName(), $column->getName(), $newColumn)) {
4213+
if (!$this->db->definition()->removeColumnForeignKey($table->getRealName(), $column->getRealName(), $newColumn)) {
42184214
return false;
42194215
}
42204216
}
@@ -4223,8 +4219,8 @@ public function updateColumn(string $tableName, string $columnName, /* object */
42234219
$newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
42244220
$newColumn->setPk(false);
42254221
$newColumn->setFk('');
4226-
if ($newColumn->getName() != $column->getName()) {
4227-
if (!$this->db->definition()->renameColumn($table->getName(), $column->getName(), $newColumn)) {
4222+
if ($newColumn->getRealName() != $column->getRealName()) {
4223+
if (!$this->db->definition()->renameColumn($table->getRealName(), $column->getRealName(), $newColumn)) {
42284224
return false;
42294225
}
42304226
}
@@ -4234,25 +4230,25 @@ public function updateColumn(string $tableName, string $columnName, /* object */
42344230
$newColumn->getPrecision() != $column->getPrecision() ||
42354231
$newColumn->getScale() != $column->getScale()
42364232
) {
4237-
if (!$this->db->definition()->retypeColumn($table->getName(), $newColumn->getName(), $newColumn)) {
4233+
if (!$this->db->definition()->retypeColumn($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
42384234
return false;
42394235
}
42404236
}
42414237
if ($newColumn->getNullable() != $column->getNullable()) {
4242-
if (!$this->db->definition()->setColumnNullable($table->getName(), $newColumn->getName(), $newColumn)) {
4238+
if (!$this->db->definition()->setColumnNullable($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
42434239
return false;
42444240
}
42454241
}
42464242

42474243
// add constraints
42484244
$newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
42494245
if ($newColumn->getFk()) {
4250-
if (!$this->db->definition()->addColumnForeignKey($table->getName(), $newColumn->getName(), $newColumn)) {
4246+
if (!$this->db->definition()->addColumnForeignKey($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
42514247
return false;
42524248
}
42534249
}
42544250
if ($newColumn->getPk()) {
4255-
if (!$this->db->definition()->addColumnPrimaryKey($table->getName(), $newColumn->getName(), $newColumn)) {
4251+
if (!$this->db->definition()->addColumnPrimaryKey($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
42564252
return false;
42574253
}
42584254
}
@@ -4268,50 +4264,48 @@ public function addTable(/* object */$definition)
42684264
return true;
42694265
}
42704266

4271-
public function addColumn(string $tableName, /* object */ $definition)
4267+
public function addColumn(ReflectedTable $table, /* object */ $definition)
42724268
{
42734269
$newColumn = ReflectedColumn::fromJson($definition);
4274-
if (!$this->db->definition()->addColumn($tableName, $newColumn)) {
4270+
if (!$this->db->definition()->addColumn($table->getRealName(), $newColumn)) {
42754271
return false;
42764272
}
42774273
if ($newColumn->getFk()) {
4278-
if (!$this->db->definition()->addColumnForeignKey($tableName, $newColumn->getName(), $newColumn)) {
4274+
if (!$this->db->definition()->addColumnForeignKey($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
42794275
return false;
42804276
}
42814277
}
42824278
if ($newColumn->getPk()) {
4283-
if (!$this->db->definition()->addColumnPrimaryKey($tableName, $newColumn->getName(), $newColumn)) {
4279+
if (!$this->db->definition()->addColumnPrimaryKey($table->getRealName(), $newColumn->getRealName(), $newColumn)) {
42844280
return false;
42854281
}
42864282
}
42874283
return true;
42884284
}
42894285

4290-
public function removeTable(string $tableName)
4286+
public function removeTable(ReflectedTable $table)
42914287
{
4292-
if (!$this->db->definition()->removeTable($tableName)) {
4288+
if (!$this->db->definition()->removeTable($table->getRealName())) {
42934289
return false;
42944290
}
42954291
return true;
42964292
}
42974293

4298-
public function removeColumn(string $tableName, string $columnName)
4294+
public function removeColumn(ReflectedTable $table, ReflectedColumn $column)
42994295
{
4300-
$table = $this->reflection->getTable($tableName);
4301-
$newColumn = $table->getColumn($columnName);
4302-
if ($newColumn->getPk()) {
4303-
$newColumn->setPk(false);
4304-
if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $newColumn->getName(), $newColumn)) {
4296+
if ($column->getPk()) {
4297+
$column->setPk(false);
4298+
if (!$this->db->definition()->removeColumnPrimaryKey($table->getRealName(), $column->getRealName(), $column)) {
43054299
return false;
43064300
}
43074301
}
4308-
if ($newColumn->getFk()) {
4309-
$newColumn->setFk("");
4310-
if (!$this->db->definition()->removeColumnForeignKey($tableName, $columnName, $newColumn)) {
4302+
if ($column->getFk()) {
4303+
$column->setFk("");
4304+
if (!$this->db->definition()->removeColumnForeignKey($table->getRealName(), $column->getRealName(), $column)) {
43114305
return false;
43124306
}
43134307
}
4314-
if (!$this->db->definition()->removeColumn($tableName, $columnName)) {
4308+
if (!$this->db->definition()->removeColumn($table->getRealName(), $column->getRealName())) {
43154309
return false;
43164310
}
43174311
return true;
@@ -4525,7 +4519,8 @@ public function updateTable(ServerRequestInterface $request): ResponseInterface
45254519
if (!$this->reflection->hasTable($tableName)) {
45264520
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
45274521
}
4528-
$success = $this->definition->updateTable($tableName, $request->getParsedBody());
4522+
$table = $this->reflection->getTable($tableName);
4523+
$success = $this->definition->updateTable($table, $request->getParsedBody());
45294524
if ($success) {
45304525
$this->reflection->refreshTables();
45314526
}
@@ -4543,7 +4538,8 @@ public function updateColumn(ServerRequestInterface $request): ResponseInterface
45434538
if (!$table->hasColumn($columnName)) {
45444539
return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
45454540
}
4546-
$success = $this->definition->updateColumn($tableName, $columnName, $request->getParsedBody());
4541+
$column = $table->getColumn($columnName);
4542+
$success = $this->definition->updateColumn($table, $column, $request->getParsedBody());
45474543
if ($success) {
45484544
$this->reflection->refreshTable($tableName);
45494545
}
@@ -4574,7 +4570,7 @@ public function addColumn(ServerRequestInterface $request): ResponseInterface
45744570
if ($table->hasColumn($columnName)) {
45754571
return $this->responder->error(ErrorCode::COLUMN_ALREADY_EXISTS, $columnName);
45764572
}
4577-
$success = $this->definition->addColumn($tableName, $request->getParsedBody());
4573+
$success = $this->definition->addColumn($table, $request->getParsedBody());
45784574
if ($success) {
45794575
$this->reflection->refreshTable($tableName);
45804576
}
@@ -4587,7 +4583,8 @@ public function removeTable(ServerRequestInterface $request): ResponseInterface
45874583
if (!$this->reflection->hasTable($tableName)) {
45884584
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
45894585
}
4590-
$success = $this->definition->removeTable($tableName);
4586+
$table = $this->reflection->getTable($tableName);
4587+
$success = $this->definition->removeTable($table);
45914588
if ($success) {
45924589
$this->reflection->refreshTables();
45934590
}
@@ -4605,7 +4602,8 @@ public function removeColumn(ServerRequestInterface $request): ResponseInterface
46054602
if (!$table->hasColumn($columnName)) {
46064603
return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
46074604
}
4608-
$success = $this->definition->removeColumn($tableName, $columnName);
4605+
$column = $table->getColumn($columnName);
4606+
$success = $this->definition->removeColumn($table, $column);
46094607
if ($success) {
46104608
$this->reflection->refreshTable($tableName);
46114609
}
@@ -6039,7 +6037,7 @@ private function getColumnRenameSQL(string $tableName, string $columnName, Refle
60396037
{
60406038
$p1 = $this->quote($tableName);
60416039
$p2 = $this->quote($columnName);
6042-
$p3 = $this->quote($newColumn->getName());
6040+
$p3 = $this->quote($newColumn->getRealName());
60436041

60446042
switch ($this->driver) {
60456043
case 'mysql':
@@ -6059,7 +6057,7 @@ private function getColumnRetypeSQL(string $tableName, string $columnName, Refle
60596057
{
60606058
$p1 = $this->quote($tableName);
60616059
$p2 = $this->quote($columnName);
6062-
$p3 = $this->quote($newColumn->getName());
6060+
$p3 = $this->quote($newColumn->getRealName());
60636061
$p4 = $this->getColumnType($newColumn, true);
60646062

60656063
switch ($this->driver) {
@@ -6076,7 +6074,7 @@ private function getSetColumnNullableSQL(string $tableName, string $columnName,
60766074
{
60776075
$p1 = $this->quote($tableName);
60786076
$p2 = $this->quote($columnName);
6079-
$p3 = $this->quote($newColumn->getName());
6077+
$p3 = $this->quote($newColumn->getRealName());
60806078
$p4 = $this->getColumnType($newColumn, true);
60816079

60826080
switch ($this->driver) {
@@ -6148,7 +6146,7 @@ private function getSetColumnPkDefaultSQL(string $tableName, string $columnName,
61486146

61496147
switch ($this->driver) {
61506148
case 'mysql':
6151-
$p3 = $this->quote($newColumn->getName());
6149+
$p3 = $this->quote($newColumn->getRealName());
61526150
$p4 = $this->getColumnType($newColumn, true);
61536151
return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
61546152
case 'pgsql':
@@ -6197,7 +6195,7 @@ private function getRemoveColumnFkConstraintSQL(string $tableName, string $colum
61976195

61986196
private function getAddTableSQL(ReflectedTable $newTable): string
61996197
{
6200-
$tableName = $newTable->getName();
6198+
$tableName = $newTable->getRealName();
62016199
$p1 = $this->quote($tableName);
62026200
$fields = [];
62036201
$constraints = [];
@@ -6236,7 +6234,7 @@ private function getAddTableSQL(ReflectedTable $newTable): string
62366234
private function getAddColumnSQL(string $tableName, ReflectedColumn $newColumn): string
62376235
{
62386236
$p1 = $this->quote($tableName);
6239-
$p2 = $this->quote($newColumn->getName());
6237+
$p2 = $this->quote($newColumn->getRealName());
62406238
$p3 = $this->getColumnType($newColumn, false);
62416239

62426240
switch ($this->driver) {
@@ -6389,7 +6387,7 @@ class GenericReflection
63896387
private $driver;
63906388
private $database;
63916389
private $tables;
6392-
private $realNameMapper;
6390+
private $mapper;
63936391
private $typeConverter;
63946392

63956393
public function __construct(LazyPdo $pdo, string $driver, string $database, array $tables, RealNameMapper $mapper)
@@ -6398,7 +6396,7 @@ public function __construct(LazyPdo $pdo, string $driver, string $database, arra
63986396
$this->driver = $driver;
63996397
$this->database = $database;
64006398
$this->tables = $tables;
6401-
$this->realNameMapper = $mapper;
6399+
$this->mapper = $mapper;
64026400
$this->typeConverter = new TypeConverter($driver);
64036401
}
64046402

@@ -6481,14 +6479,14 @@ public function getTables(): array
64816479
{
64826480
$sql = $this->getTablesSQL();
64836481
$results = $this->query($sql, [$this->database]);
6484-
foreach ($results as &$result) {
6485-
$result['TABLE_REAL_NAME'] = $result['TABLE_NAME'];
6486-
$result['TABLE_NAME'] = $this->realNameMapper->getTableName($result['TABLE_REAL_NAME']);
6487-
}
64886482
$tables = $this->tables;
64896483
$results = array_filter($results, function ($v) use ($tables) {
64906484
return !$tables || in_array($v['TABLE_NAME'], $tables);
64916485
});
6486+
foreach ($results as &$result) {
6487+
$result['TABLE_REAL_NAME'] = $result['TABLE_NAME'];
6488+
$result['TABLE_NAME'] = $this->mapper->getTableName($result['TABLE_REAL_NAME']);
6489+
}
64926490
foreach ($results as &$result) {
64936491
$map = [];
64946492
switch ($this->driver) {
@@ -6512,12 +6510,12 @@ public function getTables(): array
65126510

65136511
public function getTableColumns(string $tableName, string $type): array
65146512
{
6515-
$tableRealName = $this->realNameMapper->getTableRealName($tableName);
6513+
$tableRealName = $this->mapper->getTableRealName($tableName);
65166514
$sql = $this->getTableColumnsSQL();
65176515
$results = $this->query($sql, [$tableRealName, $this->database]);
65186516
foreach ($results as &$result) {
65196517
$result['COLUMN_REAL_NAME'] = $result['COLUMN_NAME'];
6520-
$result['COLUMN_NAME'] = $this->realNameMapper->getColumnName($tableRealName, $result['COLUMN_REAL_NAME']);
6518+
$result['COLUMN_NAME'] = $this->mapper->getColumnName($tableRealName, $result['COLUMN_REAL_NAME']);
65216519
}
65226520
if ($type == 'view') {
65236521
foreach ($results as &$result) {
@@ -6561,25 +6559,25 @@ public function getTableColumns(string $tableName, string $type): array
65616559

65626560
public function getTablePrimaryKeys(string $tableName): array
65636561
{
6564-
$tableRealName = $this->realNameMapper->getTableRealName($tableName);
6562+
$tableRealName = $this->mapper->getTableRealName($tableName);
65656563
$sql = $this->getTablePrimaryKeysSQL();
65666564
$results = $this->query($sql, [$tableRealName, $this->database]);
65676565
$primaryKeys = [];
65686566
foreach ($results as $result) {
6569-
$primaryKeys[] = $this->realNameMapper->getColumnName($tableRealName, $result['COLUMN_NAME']);
6567+
$primaryKeys[] = $this->mapper->getColumnName($tableRealName, $result['COLUMN_NAME']);
65706568
}
65716569
return $primaryKeys;
65726570
}
65736571

65746572
public function getTableForeignKeys(string $tableName): array
65756573
{
6576-
$tableRealName = $this->realNameMapper->getTableRealName($tableName);
6574+
$tableRealName = $this->mapper->getTableRealName($tableName);
65776575
$sql = $this->getTableForeignKeysSQL();
65786576
$results = $this->query($sql, [$tableRealName, $this->database]);
65796577
$foreignKeys = [];
65806578
foreach ($results as $result) {
6581-
$columnName = $this->realNameMapper->getColumnName($tableRealName, $result['COLUMN_NAME']);
6582-
$otherTableName = $this->realNameMapper->getTableName($result['REFERENCED_TABLE_NAME']);
6579+
$columnName = $this->mapper->getColumnName($tableRealName, $result['COLUMN_NAME']);
6580+
$otherTableName = $this->mapper->getTableName($result['REFERENCED_TABLE_NAME']);
65836581
$foreignKeys[$columnName] = $otherTableName;
65846582
}
65856583
return $foreignKeys;

0 commit comments

Comments
 (0)