Skip to content

Commit 1e5b6d9

Browse files
committed
fix: --desc handling
But tables having the column `id` can be sorted properly.
1 parent dd89dc2 commit 1e5b6d9

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

system/Commands/Database/ShowTableInfo.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,13 @@ private function showDataOfTable(string $tableName, int $limitRows, int $limitFi
162162
$thead = $this->db->getFieldNames($tableName);
163163
$this->restoreDBPrefix();
164164

165-
$this->tbody = $this->makeTableRows($tableName, $limitRows, $limitFieldValue);
165+
// If there is a field named `id`, sort by it.
166+
$sortField = null;
167+
if (in_array('id', $thead, true)) {
168+
$sortField = 'id';
169+
}
170+
171+
$this->tbody = $this->makeTableRows($tableName, $limitRows, $limitFieldValue, $sortField);
166172
CLI::table($this->tbody, $thead);
167173
}
168174

@@ -203,13 +209,21 @@ private function makeTbodyForShowAllTables(array $tables): array
203209
return $this->tbody;
204210
}
205211

206-
private function makeTableRows(string $tableName, int $limitRows, int $limitFieldValue): array
207-
{
212+
private function makeTableRows(
213+
string $tableName,
214+
int $limitRows,
215+
int $limitFieldValue,
216+
?string $sortField = null
217+
): array {
208218
$this->tbody = [];
209219

210220
$this->removeDBPrefix();
211221
$builder = $this->db->table($tableName);
212-
$rows = $builder->limit($limitRows)->get()->getResultArray();
222+
$builder->limit($limitRows);
223+
if ($sortField !== null) {
224+
$builder->orderBy($sortField, $this->sortDesc ? 'DESC' : 'ASC');
225+
}
226+
$rows = $builder->get()->getResultArray();
213227
$this->restoreDBPrefix();
214228

215229
foreach ($rows as $row) {
@@ -222,7 +236,7 @@ private function makeTableRows(string $tableName, int $limitRows, int $limitFiel
222236
$this->tbody[] = $row;
223237
}
224238

225-
if ($this->sortDesc) {
239+
if ($sortField === null && $this->sortDesc) {
226240
krsort($this->tbody);
227241
}
228242

tests/system/Commands/Database/ShowTableInfoTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,24 @@ public function testDbTableLimitRows(): void
169169
EOL;
170170
$this->assertStringContainsString($expected, $result);
171171
}
172+
173+
public function testDbTableAllOptions(): void
174+
{
175+
command('db:table db_user --limit-rows 2 --limit-field-value 5 --desc');
176+
177+
$result = $this->getResultWithoutControlCode();
178+
179+
$expected = 'Data of Table "db_user":';
180+
$this->assertStringContainsString($expected, $result);
181+
182+
$expected = <<<'EOL'
183+
+----+----------+----------+---------+------------+------------+------------+
184+
| id | name | email | country | created_at | updated_at | deleted_at |
185+
+----+----------+----------+---------+------------+------------+------------+
186+
| 4 | Chris... | chris... | UK | | | |
187+
| 3 | Richa... | richa... | US | | | |
188+
+----+----------+----------+---------+------------+------------+------------+
189+
EOL;
190+
$this->assertStringContainsString($expected, $result);
191+
}
172192
}

0 commit comments

Comments
 (0)