Skip to content

Commit b5c9de2

Browse files
Merge pull request #20502 from kamil-tekiela/Refactor-DataDictionaryController
Refactor DataDictionaryController
2 parents 2e69b73 + 1028e79 commit b5c9de2

3 files changed

Lines changed: 10 additions & 35 deletions

File tree

‎psalm-baseline.xml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,6 @@
690690
<code><![CDATA[$foreigner['foreign_field']]]></code>
691691
<code><![CDATA[$relation]]></code>
692692
</MixedOperand>
693-
<PossiblyNullArrayOffset>
694-
<code><![CDATA[$pkArray]]></code>
695-
</PossiblyNullArrayOffset>
696693
</file>
697694
<file src="src/Controllers/Database/DesignerController.php">
698695
<MixedArgument>

‎src/Controllers/Database/DataDictionaryController.php‎

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,21 @@ public function __invoke(ServerRequest $request): Response
4242

4343
$this->dbi->selectDb(Current::$database);
4444
$tablesNames = $this->dbi->getTables(Current::$database);
45+
$tablesFull = $this->dbi->getTablesFull(Current::$database);
4546

4647
$tables = [];
4748
foreach ($tablesNames as $tableName) {
48-
$showComment = (string) $this->dbi->getTable(Current::$database, $tableName)
49-
->getStatusInfo('TABLE_COMMENT');
49+
$showComment = (string) ($tablesFull[$tableName]['TABLE_COMMENT'] ?? '');
5050

51-
$primaryKeys = $this->getPrimaryKeys(
52-
$this->dbi->getTableIndexes(Current::$database, $tableName),
53-
);
51+
$primaryKeys = Index::getPrimary($this->dbi, $tableName, Current::$database)?->getColumns() ?? [];
5452

5553
$foreigners = $relationParameters->relationFeature !== null
5654
? $this->relation->getForeigners(Current::$database, $tableName)
5755
: null;
5856

59-
$columnsComments = $this->relation->getComments(Current::$database, $tableName);
57+
$mimeMap = $relationParameters->browserTransformationFeature !== null
58+
? $this->transformations->getMime(Current::$database, $tableName, true)
59+
: null;
6060

6161
$columns = $this->dbi->getColumns(Current::$database, $tableName);
6262
$rows = [];
@@ -74,11 +74,8 @@ public function __invoke(ServerRequest $request): Response
7474
}
7575

7676
$mime = '';
77-
if ($relationParameters->browserTransformationFeature !== null) {
78-
$mimeMap = $this->transformations->getMime(Current::$database, $tableName, true);
79-
if (is_array($mimeMap) && isset($mimeMap[$row->field]['mimetype'])) {
80-
$mime = str_replace('_', '/', $mimeMap[$row->field]['mimetype']);
81-
}
77+
if (isset($mimeMap[$row->field]['mimetype'])) {
78+
$mime = str_replace('_', '/', $mimeMap[$row->field]['mimetype']);
8279
}
8380

8481
$rows[$row->field] = [
@@ -88,7 +85,7 @@ public function __invoke(ServerRequest $request): Response
8885
'print_type' => $extractedColumnSpec['print_type'],
8986
'is_nullable' => $row->isNull,
9087
'default' => $row->default,
91-
'comment' => $columnsComments[$row->field] ?? '',
88+
'comment' => $row->comment,
9289
'mime' => $mime,
9390
'relation' => $relation,
9491
];
@@ -112,23 +109,4 @@ public function __invoke(ServerRequest $request): Response
112109

113110
return $this->response->response();
114111
}
115-
116-
/**
117-
* @param array<int, array<string, string|null>> $indexes index data
118-
*
119-
* @return array<true> The list of primary key columns
120-
*/
121-
private function getPrimaryKeys(array $indexes): array
122-
{
123-
$pkArray = [];
124-
foreach ($indexes as $row) {
125-
if ($row['Key_name'] !== 'PRIMARY') {
126-
continue;
127-
}
128-
129-
$pkArray[$row['Column_name']] = true;
130-
}
131-
132-
return $pkArray;
133-
}
134112
}

‎tests/unit/Controllers/Database/DataDictionaryControllerTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testController(): void
2626

2727
// phpcs:disable Generic.Files.LineLength.TooLong
2828
$dbiDummy->addResult(
29-
'SELECT *, `TABLE_SCHEMA` AS `Db`, `TABLE_NAME` AS `Name`, `TABLE_TYPE` AS `TABLE_TYPE`, `ENGINE` AS `Engine`, `ENGINE` AS `Type`, `VERSION` AS `Version`, `ROW_FORMAT` AS `Row_format`, `TABLE_ROWS` AS `Rows`, `AVG_ROW_LENGTH` AS `Avg_row_length`, `DATA_LENGTH` AS `Data_length`, `MAX_DATA_LENGTH` AS `Max_data_length`, `INDEX_LENGTH` AS `Index_length`, `DATA_FREE` AS `Data_free`, `AUTO_INCREMENT` AS `Auto_increment`, `CREATE_TIME` AS `Create_time`, `UPDATE_TIME` AS `Update_time`, `CHECK_TIME` AS `Check_time`, `TABLE_COLLATION` AS `Collation`, `CHECKSUM` AS `Checksum`, `CREATE_OPTIONS` AS `Create_options`, `TABLE_COMMENT` AS `Comment` FROM `information_schema`.`TABLES` t WHERE `TABLE_SCHEMA` COLLATE utf8_bin = \'test_db\' AND t.`TABLE_NAME` COLLATE utf8_bin = \'test_table\'',
29+
'SELECT *, `TABLE_SCHEMA` AS `Db`, `TABLE_NAME` AS `Name`, `TABLE_TYPE` AS `TABLE_TYPE`, `ENGINE` AS `Engine`, `ENGINE` AS `Type`, `VERSION` AS `Version`, `ROW_FORMAT` AS `Row_format`, `TABLE_ROWS` AS `Rows`, `AVG_ROW_LENGTH` AS `Avg_row_length`, `DATA_LENGTH` AS `Data_length`, `MAX_DATA_LENGTH` AS `Max_data_length`, `INDEX_LENGTH` AS `Index_length`, `DATA_FREE` AS `Data_free`, `AUTO_INCREMENT` AS `Auto_increment`, `CREATE_TIME` AS `Create_time`, `UPDATE_TIME` AS `Update_time`, `CHECK_TIME` AS `Check_time`, `TABLE_COLLATION` AS `Collation`, `CHECKSUM` AS `Checksum`, `CREATE_OPTIONS` AS `Create_options`, `TABLE_COMMENT` AS `Comment` FROM `information_schema`.`TABLES` t WHERE `TABLE_SCHEMA` COLLATE utf8_bin = \'test_db\' ORDER BY Name ASC',
3030
[['ref', 'test_db', 'test_table', 'BASE TABLE', 'InnoDB', '10', 'Dynamic', '3', '5461', '16384', '0', '49152', '0', '4', '2021-11-07 15:21:00', null, null, 'utf8mb4_general_ci', null, '', '', '0', 'N', 'test_db', 'test_table', 'BASE TABLE', 'InnoDB', 'InnoDB', '10', 'Dynamic', '3', '5461', '16384', '0', '49152', '0', '4', '2021-11-07 15:21:00', null, null, 'utf8mb4_general_ci', null, '', '']],
3131
['TABLE_CATALOG', 'TABLE_SCHEMA', 'TABLE_NAME', 'TABLE_TYPE', 'ENGINE', 'VERSION', 'ROW_FORMAT', 'TABLE_ROWS', 'AVG_ROW_LENGTH', 'DATA_LENGTH', 'MAX_DATA_LENGTH', 'INDEX_LENGTH', 'DATA_FREE', 'AUTO_INCREMENT', 'CREATE_TIME', 'UPDATE_TIME', 'CHECK_TIME', 'TABLE_COLLATION', 'CHECKSUM', 'CREATE_OPTIONS', 'TABLE_COMMENT', 'MAX_INDEX_LENGTH', 'TEMPORARY', 'Db', 'Name', 'TABLE_TYPE', 'Engine', 'Type', 'Version', 'Row_format', 'Rows', 'Avg_row_length', 'Data_length', 'Max_data_length', 'Index_length', 'Data_free', 'Auto_increment', 'Create_time', 'Update_time', 'Check_time', 'Collation', 'Checksum', 'Create_options', 'Comment'],
3232
);

0 commit comments

Comments
 (0)