Skip to content

Commit 6835dc7

Browse files
committed
Implement mapper feature
1 parent 2eefad8 commit 6835dc7

20 files changed

+1010
-332
lines changed

api.include.php

Lines changed: 365 additions & 110 deletions
Large diffs are not rendered by default.

api.php

Lines changed: 365 additions & 110 deletions
Large diffs are not rendered by default.

composer.lock

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function __construct(Config $config)
5757
$config->getPort(),
5858
$config->getDatabase(),
5959
$config->getTables(),
60+
$config->getMapping(),
6061
$config->getUsername(),
6162
$config->getPassword()
6263
);

src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedColumn.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ReflectedColumn implements \JsonSerializable
1111
const DEFAULT_SCALE = 4;
1212

1313
private $name;
14+
private $realName;
1415
private $type;
1516
private $length;
1617
private $precision;
@@ -19,9 +20,10 @@ class ReflectedColumn implements \JsonSerializable
1920
private $pk;
2021
private $fk;
2122

22-
public function __construct(string $name, string $type, int $length, int $precision, int $scale, bool $nullable, bool $pk, string $fk)
23+
public function __construct(string $name, string $realName, string $type, int $length, int $precision, int $scale, bool $nullable, bool $pk, string $fk)
2324
{
2425
$this->name = $name;
26+
$this->realName = $realName;
2527
$this->type = $type;
2628
$this->length = $length;
2729
$this->precision = $precision;
@@ -73,6 +75,7 @@ private static function getDataSize(int $length, int $precision, int $scale): st
7375
public static function fromReflection(GenericReflection $reflection, array $columnResult): ReflectedColumn
7476
{
7577
$name = $columnResult['COLUMN_NAME'];
78+
$realName = $columnResult['COLUMN_REAL_NAME'];
7679
$dataType = $columnResult['DATA_TYPE'];
7780
$length = (int) $columnResult['CHARACTER_MAXIMUM_LENGTH'];
7881
$precision = (int) $columnResult['NUMERIC_PRECISION'];
@@ -84,20 +87,21 @@ public static function fromReflection(GenericReflection $reflection, array $colu
8487
$nullable = in_array(strtoupper($columnResult['IS_NULLABLE']), ['TRUE', 'YES', 'T', 'Y', '1']);
8588
$pk = false;
8689
$fk = '';
87-
return new ReflectedColumn($name, $type, $length, $precision, $scale, $nullable, $pk, $fk);
90+
return new ReflectedColumn($name, $realName, $type, $length, $precision, $scale, $nullable, $pk, $fk);
8891
}
8992

9093
public static function fromJson(/* object */$json): ReflectedColumn
9194
{
92-
$name = $json->name;
95+
$name = $json->alias ?? $json->name;
96+
$realName = $json->name;
9397
$type = $json->type;
9498
$length = isset($json->length) ? (int) $json->length : 0;
9599
$precision = isset($json->precision) ? (int) $json->precision : 0;
96100
$scale = isset($json->scale) ? (int) $json->scale : 0;
97101
$nullable = isset($json->nullable) ? (bool) $json->nullable : false;
98102
$pk = isset($json->pk) ? (bool) $json->pk : false;
99103
$fk = isset($json->fk) ? $json->fk : '';
100-
return new ReflectedColumn($name, $type, $length, $precision, $scale, $nullable, $pk, $fk);
104+
return new ReflectedColumn($name, $realName, $type, $length, $precision, $scale, $nullable, $pk, $fk);
101105
}
102106

103107
private function sanitize()
@@ -112,6 +116,11 @@ public function getName(): string
112116
return $this->name;
113117
}
114118

119+
public function getRealName(): string
120+
{
121+
return $this->realName;
122+
}
123+
115124
public function getNullable(): bool
116125
{
117126
return $this->nullable;
@@ -194,8 +203,9 @@ public function getFk(): string
194203

195204
public function serialize()
196205
{
197-
return [
198-
'name' => $this->name,
206+
$json = [
207+
'name' => $this->realName,
208+
'alias' => $this->name!=$this->realName?$this->name:null,
199209
'type' => $this->type,
200210
'length' => $this->length,
201211
'precision' => $this->precision,
@@ -204,10 +214,11 @@ public function serialize()
204214
'pk' => $this->pk,
205215
'fk' => $this->fk,
206216
];
217+
return array_filter($json);
207218
}
208219

209220
public function jsonSerialize()
210221
{
211-
return array_filter($this->serialize());
222+
return $this->serialize();
212223
}
213224
}

src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedDatabase.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,34 @@
77
class ReflectedDatabase implements \JsonSerializable
88
{
99
private $tableTypes;
10+
private $tableRealNames;
1011

11-
public function __construct(array $tableTypes)
12+
public function __construct(array $tableTypes, array $tableRealNames)
1213
{
1314
$this->tableTypes = $tableTypes;
15+
$this->tableRealNames = $tableRealNames;
1416
}
1517

1618
public static function fromReflection(GenericReflection $reflection): ReflectedDatabase
1719
{
1820
$tableTypes = [];
21+
$tableRealNames = [];
1922
foreach ($reflection->getTables() as $table) {
2023
$tableName = $table['TABLE_NAME'];
21-
$tableType = $table['TABLE_TYPE'];
2224
if (in_array($tableName, $reflection->getIgnoredTables())) {
2325
continue;
2426
}
25-
$tableTypes[$tableName] = $tableType;
27+
$tableTypes[$tableName] = $table['TABLE_TYPE'];
28+
$tableRealNames[$tableName] = $table['TABLE_REAL_NAME'];
2629
}
27-
return new ReflectedDatabase($tableTypes);
30+
return new ReflectedDatabase($tableTypes, $tableRealNames);
2831
}
2932

3033
public static function fromJson(/* object */$json): ReflectedDatabase
3134
{
32-
$tableTypes = (array) $json->tables;
33-
return new ReflectedDatabase($tableTypes);
35+
$tableTypes = (array) $json->types;
36+
$tableRealNames = (array) $json->realNames;
37+
return new ReflectedDatabase($tableTypes, $tableRealNames);
3438
}
3539

3640
public function hasTable(string $tableName): bool
@@ -43,6 +47,11 @@ public function getType(string $tableName): string
4347
return isset($this->tableTypes[$tableName]) ? $this->tableTypes[$tableName] : '';
4448
}
4549

50+
public function getRealName(string $tableName): string
51+
{
52+
return isset($this->tableRealNames[$tableName]) ? $this->tableRealNames[$tableName] : '';
53+
}
54+
4655
public function getTableNames(): array
4756
{
4857
return array_keys($this->tableTypes);
@@ -54,13 +63,15 @@ public function removeTable(string $tableName): bool
5463
return false;
5564
}
5665
unset($this->tableTypes[$tableName]);
66+
unset($this->tableRealNames[$tableName]);
5767
return true;
5868
}
5969

6070
public function serialize()
6171
{
6272
return [
63-
'tables' => $this->tableTypes,
73+
'types' => $this->tableTypes,
74+
'realNames' => $this->tableRealNames,
6475
];
6576
}
6677

src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedTable.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
class ReflectedTable implements \JsonSerializable
88
{
99
private $name;
10+
private $realName;
1011
private $type;
1112
private $columns;
1213
private $pk;
1314
private $fks;
1415

15-
public function __construct(string $name, string $type, array $columns)
16+
public function __construct(string $name, string $realName, string $type, array $columns)
1617
{
1718
$this->name = $name;
19+
$this->realName = $realName;
1820
$this->type = $type;
1921
// set columns
2022
$this->columns = [];
@@ -40,7 +42,7 @@ public function __construct(string $name, string $type, array $columns)
4042
}
4143
}
4244

43-
public static function fromReflection(GenericReflection $reflection, string $name, string $type): ReflectedTable
45+
public static function fromReflection(GenericReflection $reflection, string $name, string $realName, string $type): ReflectedTable
4446
{
4547
// set columns
4648
$columns = [];
@@ -82,20 +84,21 @@ public static function fromReflection(GenericReflection $reflection, string $nam
8284
$columns[$columnName]->setFk($table);
8385
}
8486
}
85-
return new ReflectedTable($name, $type, array_values($columns));
87+
return new ReflectedTable($name, $realName, $type, array_values($columns));
8688
}
8789

8890
public static function fromJson( /* object */$json): ReflectedTable
8991
{
90-
$name = $json->name;
92+
$name = $json->alias??$json->name;
93+
$realName = $json->name;
9194
$type = isset($json->type) ? $json->type : 'table';
9295
$columns = [];
9396
if (isset($json->columns) && is_array($json->columns)) {
9497
foreach ($json->columns as $column) {
9598
$columns[] = ReflectedColumn::fromJson($column);
9699
}
97100
}
98-
return new ReflectedTable($name, $type, $columns);
101+
return new ReflectedTable($name, $realName, $type, $columns);
99102
}
100103

101104
public function hasColumn(string $columnName): bool
@@ -118,6 +121,11 @@ public function getName(): string
118121
return $this->name;
119122
}
120123

124+
public function getRealName(): string
125+
{
126+
return $this->realName;
127+
}
128+
121129
public function getType(): string
122130
{
123131
return $this->type;
@@ -155,11 +163,13 @@ public function removeColumn(string $columnName): bool
155163

156164
public function serialize()
157165
{
158-
return [
159-
'name' => $this->name,
166+
$json = [
167+
'name' => $this->realName,
168+
'alias' => $this->name!=$this->realName?$this->name:null,
160169
'type' => $this->type,
161170
'columns' => array_values($this->columns),
162171
];
172+
return array_filter($json);
163173
}
164174

165175
public function jsonSerialize()

src/Tqdev/PhpCrudApi/Column/ReflectionService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ private function loadTable(string $tableName, bool $useCache): ReflectedTable
5555
$table = ReflectedTable::fromJson(json_decode(gzuncompress($data)));
5656
} else {
5757
$tableType = $this->database()->getType($tableName);
58-
$table = ReflectedTable::fromReflection($this->db->reflection(), $tableName, $tableType);
58+
$tableRealName = $this->database()->getRealName($tableName);
59+
$table = ReflectedTable::fromReflection($this->db->reflection(), $tableName, $tableRealName, $tableType);
5960
$data = gzcompress(json_encode($table, JSON_UNESCAPED_UNICODE));
6061
$this->cache->set($key, $data, $this->ttl);
6162
}

src/Tqdev/PhpCrudApi/Config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Config
1212
'password' => null,
1313
'database' => null,
1414
'tables' => '',
15+
'mapping' => '',
1516
'middlewares' => 'cors,errors',
1617
'controllers' => 'records,geojson,openapi,status',
1718
'customControllers' => '',
@@ -153,6 +154,12 @@ public function getTables(): array
153154
return array_filter(array_map('trim', explode(',', $this->values['tables'])));
154155
}
155156

157+
public function getMapping(): array
158+
{
159+
$mapping = array_map(function($v){ return explode('=', $v); }, array_filter(array_map('trim', explode(',', $this->values['mapping']))));
160+
return array_combine(array_column($mapping,0),array_column($mapping,1));
161+
}
162+
156163
public function getMiddlewares(): array
157164
{
158165
return $this->values['middlewares'];

src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getOffsetLimit(int $offset, int $limit): string
3535

3636
private function quoteColumnName(ReflectedColumn $column): string
3737
{
38-
return '"' . $column->getName() . '"';
38+
return '"' . $column->getRealName() . '"';
3939
}
4040

4141
public function getOrderBy(ReflectedTable $table, array $columnOrdering): string

0 commit comments

Comments
 (0)