Skip to content

Commit 3b48aae

Browse files
committed
fix: duplicate DBPrefix
1 parent 10e4325 commit 3b48aae

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

system/Commands/Database/ShowTableInfo.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ class ShowTableInfo extends BaseCommand
8484
*/
8585
private bool $sortDesc = false;
8686

87+
private string $DBPrefix;
88+
8789
public function run(array $params)
8890
{
89-
$this->db = Database::connect();
91+
$this->db = Database::connect();
92+
$this->DBPrefix = $this->db->getPrefix();
9093

9194
$tables = $this->db->listTables();
9295

@@ -130,13 +133,26 @@ public function run(array $params)
130133
$this->showDataOfTable($tableName, $limitRows, $limitFieldValue);
131134
}
132135

136+
private function removeDBPrefix(): void
137+
{
138+
$this->db->setPrefix('');
139+
}
140+
141+
private function restoreDBPrefix(): void
142+
{
143+
$this->db->setPrefix($this->DBPrefix);
144+
}
145+
133146
private function showDataOfTable(string $tableName, int $limitRows, int $limitFieldValue)
134147
{
135148
CLI::newLine();
136149
CLI::write("Data of Table \"{$tableName}\":", 'black', 'yellow');
137150
CLI::newLine();
138151

139-
$thead = $this->db->getFieldNames($tableName);
152+
$this->removeDBPrefix();
153+
$thead = $this->db->getFieldNames($tableName);
154+
$this->restoreDBPrefix();
155+
140156
$this->tbody = $this->makeTableRows($tableName, $limitRows, $limitFieldValue);
141157
CLI::table($this->tbody, $thead);
142158
}
@@ -155,6 +171,8 @@ private function showAllTables(array $tables)
155171

156172
private function makeTbodyForShowAllTables(array $tables): array
157173
{
174+
$this->removeDBPrefix();
175+
158176
foreach ($tables as $id => $tableName) {
159177
$table = $this->db->protectIdentifiers($tableName);
160178
$db = $this->db->query("SELECT * FROM {$table}");
@@ -167,6 +185,8 @@ private function makeTbodyForShowAllTables(array $tables): array
167185
];
168186
}
169187

188+
$this->restoreDBPrefix();
189+
170190
if ($this->sortDesc) {
171191
krsort($this->tbody);
172192
}
@@ -178,8 +198,10 @@ private function makeTableRows(string $tableName, int $limitRows, int $limitFiel
178198
{
179199
$this->tbody = [];
180200

201+
$this->removeDBPrefix();
181202
$builder = $this->db->table($tableName);
182203
$rows = $builder->limit($limitRows)->get()->getResultArray();
204+
$this->restoreDBPrefix();
183205

184206
foreach ($rows as $row) {
185207
$row = array_map(
@@ -210,7 +232,9 @@ private function showFieldMetaData(string $tableName): void
210232

211233
$thead = ['Field Name', 'Type', 'Max Length', 'Nullable', 'Default', 'Primary Key'];
212234

235+
$this->removeDBPrefix();
213236
$fields = $this->db->getFieldData($tableName);
237+
$this->restoreDBPrefix();
214238

215239
foreach ($fields as $row) {
216240
$this->tbody[] = [

tests/system/Commands/Database/ShowTableInfoTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ public function testDbTableMetadata(): void
9696
$expected = 'List of Metadata Information in Table "db_migrations":';
9797
$this->assertStringContainsString($expected, $result);
9898

99+
$result = preg_replace('/\s+/', ' ', $result);
99100
$expected = <<<'EOL'
100-
+------------+---------+------------+----------+---------+-------------+
101-
| Field Name | Type | Max Length | Nullable | Default | Primary Key |
102-
+------------+---------+------------+----------+---------+-------------+
101+
| Field Name | Type | Max Length | Nullable | Default | Primary Key |
103102
EOL;
104103
$this->assertStringContainsString($expected, $result);
105104
}

0 commit comments

Comments
 (0)