@@ -4177,9 +4177,8 @@ public function __construct(GenericDB $db, ReflectionService $reflection)
4177
4177
$ this ->reflection = $ reflection ;
4178
4178
}
4179
4179
4180
- public function updateTable (string $ tableName , /* object */ $ changes ): bool
4180
+ public function updateTable (ReflectedTable $ table , /* object */ $ changes ): bool
4181
4181
{
4182
- $ table = $ this ->reflection ->getTable ($ tableName );
4183
4182
$ newTable = ReflectedTable::fromJson ((object ) array_merge ((array ) $ table ->jsonSerialize (), (array ) $ changes ));
4184
4183
if ($ table ->getRealName () != $ newTable ->getRealName ()) {
4185
4184
if (!$ this ->db ->definition ()->renameTable ($ table ->getRealName (), $ newTable ->getRealName ())) {
@@ -4189,18 +4188,15 @@ public function updateTable(string $tableName, /* object */ $changes): bool
4189
4188
return true ;
4190
4189
}
4191
4190
4192
- public function updateColumn (string $ tableName , string $ columnName , /* object */ $ changes ): bool
4191
+ public function updateColumn (ReflectedTable $ table , ReflectedColumn $ column , /* object */ $ changes ): bool
4193
4192
{
4194
- $ table = $ this ->reflection ->getTable ($ tableName );
4195
- $ column = $ table ->getColumn ($ columnName );
4196
-
4197
4193
// remove constraints on other column
4198
4194
$ newColumn = ReflectedColumn::fromJson ((object ) array_merge ((array ) $ column ->jsonSerialize (), (array ) $ changes ));
4199
4195
if ($ newColumn ->getPk () != $ column ->getPk () && $ table ->hasPk ()) {
4200
4196
$ oldColumn = $ table ->getPk ();
4201
- if ($ oldColumn ->getName () != $ columnName ) {
4197
+ if ($ oldColumn ->getRealName () != $ column -> getRealName () ) {
4202
4198
$ 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 )) {
4204
4200
return false ;
4205
4201
}
4206
4202
}
@@ -4209,12 +4205,12 @@ public function updateColumn(string $tableName, string $columnName, /* object */
4209
4205
// remove constraints
4210
4206
$ newColumn = ReflectedColumn::fromJson ((object ) array_merge ((array ) $ column ->jsonSerialize (), ['pk ' => false , 'fk ' => false ]));
4211
4207
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 )) {
4213
4209
return false ;
4214
4210
}
4215
4211
}
4216
4212
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 )) {
4218
4214
return false ;
4219
4215
}
4220
4216
}
@@ -4223,8 +4219,8 @@ public function updateColumn(string $tableName, string $columnName, /* object */
4223
4219
$ newColumn = ReflectedColumn::fromJson ((object ) array_merge ((array ) $ column ->jsonSerialize (), (array ) $ changes ));
4224
4220
$ newColumn ->setPk (false );
4225
4221
$ 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 )) {
4228
4224
return false ;
4229
4225
}
4230
4226
}
@@ -4234,25 +4230,25 @@ public function updateColumn(string $tableName, string $columnName, /* object */
4234
4230
$ newColumn ->getPrecision () != $ column ->getPrecision () ||
4235
4231
$ newColumn ->getScale () != $ column ->getScale ()
4236
4232
) {
4237
- if (!$ this ->db ->definition ()->retypeColumn ($ table ->getName (), $ newColumn ->getName (), $ newColumn )) {
4233
+ if (!$ this ->db ->definition ()->retypeColumn ($ table ->getRealName (), $ newColumn ->getRealName (), $ newColumn )) {
4238
4234
return false ;
4239
4235
}
4240
4236
}
4241
4237
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 )) {
4243
4239
return false ;
4244
4240
}
4245
4241
}
4246
4242
4247
4243
// add constraints
4248
4244
$ newColumn = ReflectedColumn::fromJson ((object ) array_merge ((array ) $ column ->jsonSerialize (), (array ) $ changes ));
4249
4245
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 )) {
4251
4247
return false ;
4252
4248
}
4253
4249
}
4254
4250
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 )) {
4256
4252
return false ;
4257
4253
}
4258
4254
}
@@ -4268,50 +4264,48 @@ public function addTable(/* object */$definition)
4268
4264
return true ;
4269
4265
}
4270
4266
4271
- public function addColumn (string $ tableName , /* object */ $ definition )
4267
+ public function addColumn (ReflectedTable $ table , /* object */ $ definition )
4272
4268
{
4273
4269
$ newColumn = ReflectedColumn::fromJson ($ definition );
4274
- if (!$ this ->db ->definition ()->addColumn ($ tableName , $ newColumn )) {
4270
+ if (!$ this ->db ->definition ()->addColumn ($ table -> getRealName () , $ newColumn )) {
4275
4271
return false ;
4276
4272
}
4277
4273
if ($ newColumn ->getFk ()) {
4278
- if (!$ this ->db ->definition ()->addColumnForeignKey ($ tableName , $ newColumn ->getName (), $ newColumn )) {
4274
+ if (!$ this ->db ->definition ()->addColumnForeignKey ($ table -> getRealName () , $ newColumn ->getRealName (), $ newColumn )) {
4279
4275
return false ;
4280
4276
}
4281
4277
}
4282
4278
if ($ newColumn ->getPk ()) {
4283
- if (!$ this ->db ->definition ()->addColumnPrimaryKey ($ tableName , $ newColumn ->getName (), $ newColumn )) {
4279
+ if (!$ this ->db ->definition ()->addColumnPrimaryKey ($ table -> getRealName () , $ newColumn ->getRealName (), $ newColumn )) {
4284
4280
return false ;
4285
4281
}
4286
4282
}
4287
4283
return true ;
4288
4284
}
4289
4285
4290
- public function removeTable (string $ tableName )
4286
+ public function removeTable (ReflectedTable $ table )
4291
4287
{
4292
- if (!$ this ->db ->definition ()->removeTable ($ tableName )) {
4288
+ if (!$ this ->db ->definition ()->removeTable ($ table -> getRealName () )) {
4293
4289
return false ;
4294
4290
}
4295
4291
return true ;
4296
4292
}
4297
4293
4298
- public function removeColumn (string $ tableName , string $ columnName )
4294
+ public function removeColumn (ReflectedTable $ table , ReflectedColumn $ column )
4299
4295
{
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 )) {
4305
4299
return false ;
4306
4300
}
4307
4301
}
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 )) {
4311
4305
return false ;
4312
4306
}
4313
4307
}
4314
- if (!$ this ->db ->definition ()->removeColumn ($ tableName , $ columnName )) {
4308
+ if (!$ this ->db ->definition ()->removeColumn ($ table -> getRealName () , $ column -> getRealName () )) {
4315
4309
return false ;
4316
4310
}
4317
4311
return true ;
@@ -4525,7 +4519,8 @@ public function updateTable(ServerRequestInterface $request): ResponseInterface
4525
4519
if (!$ this ->reflection ->hasTable ($ tableName )) {
4526
4520
return $ this ->responder ->error (ErrorCode::TABLE_NOT_FOUND , $ tableName );
4527
4521
}
4528
- $ success = $ this ->definition ->updateTable ($ tableName , $ request ->getParsedBody ());
4522
+ $ table = $ this ->reflection ->getTable ($ tableName );
4523
+ $ success = $ this ->definition ->updateTable ($ table , $ request ->getParsedBody ());
4529
4524
if ($ success ) {
4530
4525
$ this ->reflection ->refreshTables ();
4531
4526
}
@@ -4543,7 +4538,8 @@ public function updateColumn(ServerRequestInterface $request): ResponseInterface
4543
4538
if (!$ table ->hasColumn ($ columnName )) {
4544
4539
return $ this ->responder ->error (ErrorCode::COLUMN_NOT_FOUND , $ columnName );
4545
4540
}
4546
- $ success = $ this ->definition ->updateColumn ($ tableName , $ columnName , $ request ->getParsedBody ());
4541
+ $ column = $ table ->getColumn ($ columnName );
4542
+ $ success = $ this ->definition ->updateColumn ($ table , $ column , $ request ->getParsedBody ());
4547
4543
if ($ success ) {
4548
4544
$ this ->reflection ->refreshTable ($ tableName );
4549
4545
}
@@ -4574,7 +4570,7 @@ public function addColumn(ServerRequestInterface $request): ResponseInterface
4574
4570
if ($ table ->hasColumn ($ columnName )) {
4575
4571
return $ this ->responder ->error (ErrorCode::COLUMN_ALREADY_EXISTS , $ columnName );
4576
4572
}
4577
- $ success = $ this ->definition ->addColumn ($ tableName , $ request ->getParsedBody ());
4573
+ $ success = $ this ->definition ->addColumn ($ table , $ request ->getParsedBody ());
4578
4574
if ($ success ) {
4579
4575
$ this ->reflection ->refreshTable ($ tableName );
4580
4576
}
@@ -4587,7 +4583,8 @@ public function removeTable(ServerRequestInterface $request): ResponseInterface
4587
4583
if (!$ this ->reflection ->hasTable ($ tableName )) {
4588
4584
return $ this ->responder ->error (ErrorCode::TABLE_NOT_FOUND , $ tableName );
4589
4585
}
4590
- $ success = $ this ->definition ->removeTable ($ tableName );
4586
+ $ table = $ this ->reflection ->getTable ($ tableName );
4587
+ $ success = $ this ->definition ->removeTable ($ table );
4591
4588
if ($ success ) {
4592
4589
$ this ->reflection ->refreshTables ();
4593
4590
}
@@ -4605,7 +4602,8 @@ public function removeColumn(ServerRequestInterface $request): ResponseInterface
4605
4602
if (!$ table ->hasColumn ($ columnName )) {
4606
4603
return $ this ->responder ->error (ErrorCode::COLUMN_NOT_FOUND , $ columnName );
4607
4604
}
4608
- $ success = $ this ->definition ->removeColumn ($ tableName , $ columnName );
4605
+ $ column = $ table ->getColumn ($ columnName );
4606
+ $ success = $ this ->definition ->removeColumn ($ table , $ column );
4609
4607
if ($ success ) {
4610
4608
$ this ->reflection ->refreshTable ($ tableName );
4611
4609
}
@@ -6039,7 +6037,7 @@ private function getColumnRenameSQL(string $tableName, string $columnName, Refle
6039
6037
{
6040
6038
$ p1 = $ this ->quote ($ tableName );
6041
6039
$ p2 = $ this ->quote ($ columnName );
6042
- $ p3 = $ this ->quote ($ newColumn ->getName ());
6040
+ $ p3 = $ this ->quote ($ newColumn ->getRealName ());
6043
6041
6044
6042
switch ($ this ->driver ) {
6045
6043
case 'mysql ' :
@@ -6059,7 +6057,7 @@ private function getColumnRetypeSQL(string $tableName, string $columnName, Refle
6059
6057
{
6060
6058
$ p1 = $ this ->quote ($ tableName );
6061
6059
$ p2 = $ this ->quote ($ columnName );
6062
- $ p3 = $ this ->quote ($ newColumn ->getName ());
6060
+ $ p3 = $ this ->quote ($ newColumn ->getRealName ());
6063
6061
$ p4 = $ this ->getColumnType ($ newColumn , true );
6064
6062
6065
6063
switch ($ this ->driver ) {
@@ -6076,7 +6074,7 @@ private function getSetColumnNullableSQL(string $tableName, string $columnName,
6076
6074
{
6077
6075
$ p1 = $ this ->quote ($ tableName );
6078
6076
$ p2 = $ this ->quote ($ columnName );
6079
- $ p3 = $ this ->quote ($ newColumn ->getName ());
6077
+ $ p3 = $ this ->quote ($ newColumn ->getRealName ());
6080
6078
$ p4 = $ this ->getColumnType ($ newColumn , true );
6081
6079
6082
6080
switch ($ this ->driver ) {
@@ -6148,7 +6146,7 @@ private function getSetColumnPkDefaultSQL(string $tableName, string $columnName,
6148
6146
6149
6147
switch ($ this ->driver ) {
6150
6148
case 'mysql ' :
6151
- $ p3 = $ this ->quote ($ newColumn ->getName ());
6149
+ $ p3 = $ this ->quote ($ newColumn ->getRealName ());
6152
6150
$ p4 = $ this ->getColumnType ($ newColumn , true );
6153
6151
return "ALTER TABLE $ p1 CHANGE $ p2 $ p3 $ p4 " ;
6154
6152
case 'pgsql ' :
@@ -6197,7 +6195,7 @@ private function getRemoveColumnFkConstraintSQL(string $tableName, string $colum
6197
6195
6198
6196
private function getAddTableSQL (ReflectedTable $ newTable ): string
6199
6197
{
6200
- $ tableName = $ newTable ->getName ();
6198
+ $ tableName = $ newTable ->getRealName ();
6201
6199
$ p1 = $ this ->quote ($ tableName );
6202
6200
$ fields = [];
6203
6201
$ constraints = [];
@@ -6236,7 +6234,7 @@ private function getAddTableSQL(ReflectedTable $newTable): string
6236
6234
private function getAddColumnSQL (string $ tableName , ReflectedColumn $ newColumn ): string
6237
6235
{
6238
6236
$ p1 = $ this ->quote ($ tableName );
6239
- $ p2 = $ this ->quote ($ newColumn ->getName ());
6237
+ $ p2 = $ this ->quote ($ newColumn ->getRealName ());
6240
6238
$ p3 = $ this ->getColumnType ($ newColumn , false );
6241
6239
6242
6240
switch ($ this ->driver ) {
@@ -6389,7 +6387,7 @@ class GenericReflection
6389
6387
private $ driver ;
6390
6388
private $ database ;
6391
6389
private $ tables ;
6392
- private $ realNameMapper ;
6390
+ private $ mapper ;
6393
6391
private $ typeConverter ;
6394
6392
6395
6393
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
6398
6396
$ this ->driver = $ driver ;
6399
6397
$ this ->database = $ database ;
6400
6398
$ this ->tables = $ tables ;
6401
- $ this ->realNameMapper = $ mapper ;
6399
+ $ this ->mapper = $ mapper ;
6402
6400
$ this ->typeConverter = new TypeConverter ($ driver );
6403
6401
}
6404
6402
@@ -6481,14 +6479,14 @@ public function getTables(): array
6481
6479
{
6482
6480
$ sql = $ this ->getTablesSQL ();
6483
6481
$ 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
- }
6488
6482
$ tables = $ this ->tables ;
6489
6483
$ results = array_filter ($ results , function ($ v ) use ($ tables ) {
6490
6484
return !$ tables || in_array ($ v ['TABLE_NAME ' ], $ tables );
6491
6485
});
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
+ }
6492
6490
foreach ($ results as &$ result ) {
6493
6491
$ map = [];
6494
6492
switch ($ this ->driver ) {
@@ -6512,12 +6510,12 @@ public function getTables(): array
6512
6510
6513
6511
public function getTableColumns (string $ tableName , string $ type ): array
6514
6512
{
6515
- $ tableRealName = $ this ->realNameMapper ->getTableRealName ($ tableName );
6513
+ $ tableRealName = $ this ->mapper ->getTableRealName ($ tableName );
6516
6514
$ sql = $ this ->getTableColumnsSQL ();
6517
6515
$ results = $ this ->query ($ sql , [$ tableRealName , $ this ->database ]);
6518
6516
foreach ($ results as &$ result ) {
6519
6517
$ 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 ' ]);
6521
6519
}
6522
6520
if ($ type == 'view ' ) {
6523
6521
foreach ($ results as &$ result ) {
@@ -6561,25 +6559,25 @@ public function getTableColumns(string $tableName, string $type): array
6561
6559
6562
6560
public function getTablePrimaryKeys (string $ tableName ): array
6563
6561
{
6564
- $ tableRealName = $ this ->realNameMapper ->getTableRealName ($ tableName );
6562
+ $ tableRealName = $ this ->mapper ->getTableRealName ($ tableName );
6565
6563
$ sql = $ this ->getTablePrimaryKeysSQL ();
6566
6564
$ results = $ this ->query ($ sql , [$ tableRealName , $ this ->database ]);
6567
6565
$ primaryKeys = [];
6568
6566
foreach ($ results as $ result ) {
6569
- $ primaryKeys [] = $ this ->realNameMapper ->getColumnName ($ tableRealName , $ result ['COLUMN_NAME ' ]);
6567
+ $ primaryKeys [] = $ this ->mapper ->getColumnName ($ tableRealName , $ result ['COLUMN_NAME ' ]);
6570
6568
}
6571
6569
return $ primaryKeys ;
6572
6570
}
6573
6571
6574
6572
public function getTableForeignKeys (string $ tableName ): array
6575
6573
{
6576
- $ tableRealName = $ this ->realNameMapper ->getTableRealName ($ tableName );
6574
+ $ tableRealName = $ this ->mapper ->getTableRealName ($ tableName );
6577
6575
$ sql = $ this ->getTableForeignKeysSQL ();
6578
6576
$ results = $ this ->query ($ sql , [$ tableRealName , $ this ->database ]);
6579
6577
$ foreignKeys = [];
6580
6578
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 ' ]);
6583
6581
$ foreignKeys [$ columnName ] = $ otherTableName ;
6584
6582
}
6585
6583
return $ foreignKeys ;
0 commit comments