diff --git a/src/Controllers/Database/StructureController.php b/src/Controllers/Database/StructureController.php index 136a74496c..8e0b2f7e1f 100644 --- a/src/Controllers/Database/StructureController.php +++ b/src/Controllers/Database/StructureController.php @@ -12,10 +12,11 @@ use PhpMyAdmin\ConfigStorage\Relation; use PhpMyAdmin\Controllers\InvocableController; use PhpMyAdmin\Current; use PhpMyAdmin\Dbal\DatabaseInterface; +use PhpMyAdmin\Dbal\TableType; use PhpMyAdmin\DbTableExists; use PhpMyAdmin\Favorites\RecentFavoriteTable; use PhpMyAdmin\Favorites\RecentFavoriteTables; -use PhpMyAdmin\Favorites\TableType; +use PhpMyAdmin\Favorites\TableType as FavoriteTableType; use PhpMyAdmin\Html\Generator; use PhpMyAdmin\Http\Response; use PhpMyAdmin\Http\ServerRequest; @@ -245,7 +246,7 @@ final class StructureController implements InvocableController $overallApproxRows = false; $structureTableRows = []; $trackedTables = $this->trackingChecker->getTrackedTables(Current::$database); - $recentFavoriteTables = RecentFavoriteTables::getInstance(TableType::Favorite); + $recentFavoriteTables = RecentFavoriteTables::getInstance(FavoriteTableType::Favorite); foreach ($this->tables as $currentTable) { // Get valid statistics whatever is the table type @@ -959,7 +960,7 @@ final class StructureController implements InvocableController $sort ??= 'Name'; $tableGroup = is_string($tableGroupParam) && $tableGroupParam !== '' ? $tableGroupParam : null; - $tableType = is_string($tableTypeParam) && $tableTypeParam !== '' ? $tableTypeParam : null; + $tableType = is_string($tableTypeParam) ? TableType::tryFrom($tableTypeParam) : null; if ($tableGroup === null && $tableType === null) { // all tables in db @@ -1039,13 +1040,14 @@ final class StructureController implements InvocableController $whereAdded = true; } - if (isset($_REQUEST['tbl_type']) && in_array($_REQUEST['tbl_type'], ['table', 'view'], true)) { + $tableTypeParam = $_REQUEST['tbl_type'] ?? null; + $tableType = is_string($tableTypeParam) ? TableType::tryFrom($tableTypeParam) : null; + if ($tableType !== null) { $tblGroupSql .= $whereAdded ? ' AND' : ' WHERE'; - if ($_REQUEST['tbl_type'] === 'view') { - $tblGroupSql .= " `Table_type` NOT IN ('BASE TABLE', 'SYSTEM VERSIONED')"; - } else { - $tblGroupSql .= " `Table_type` IN ('BASE TABLE', 'SYSTEM VERSIONED')"; - } + $tblGroupSql .= match ($tableType) { + TableType::View => " `Table_type` NOT IN ('BASE TABLE', 'SYSTEM VERSIONED')", + TableType::Table => " `Table_type` IN ('BASE TABLE', 'SYSTEM VERSIONED')", + }; } $dbInfoResult = $this->dbi->query('SHOW FULL TABLES FROM ' . Util::backquote($db) . $tblGroupSql); diff --git a/src/Dbal/DatabaseInterface.php b/src/Dbal/DatabaseInterface.php index 43830cec70..16f21ab9af 100644 --- a/src/Dbal/DatabaseInterface.php +++ b/src/Dbal/DatabaseInterface.php @@ -360,7 +360,7 @@ class DatabaseInterface * @param bool|int $limitCount number of tables to return * @param string $sortBy table attribute to sort by * @param string $sortOrder direction to sort (ASC or DESC) - * @param string|null $tableType whether table or view + * @param TableType|null $tableType whether to list only tables or only views * * @return (string|int|null)[][] list of tables in given db(s) * @@ -374,7 +374,7 @@ class DatabaseInterface bool|int $limitCount = false, string $sortBy = 'Name', string $sortOrder = 'ASC', - string|null $tableType = null, + TableType|null $tableType = null, ConnectionType $connectionType = ConnectionType::User, ): array { if ($limitCount === true) { @@ -507,7 +507,7 @@ class DatabaseInterface // this is why we fall back to SHOW TABLE STATUS even for MySQL >= 50002 if ($tables === []) { $sql = 'SHOW TABLE STATUS FROM ' . Util::backquote($database); - if (($table !== '' && $table !== []) || $tableIsGroup || ($tableType !== null && $tableType !== '')) { + if (($table !== '' && $table !== []) || $tableIsGroup || $tableType !== null) { $sql .= ' WHERE'; $needAnd = false; if (($table !== '' && $table !== []) || $tableIsGroup) { @@ -528,16 +528,15 @@ class DatabaseInterface $needAnd = true; } - if ($tableType !== null && $tableType !== '') { + if ($tableType !== null) { if ($needAnd) { $sql .= ' AND'; } - if ($tableType === 'view') { - $sql .= " `Comment` = 'VIEW'"; - } elseif ($tableType === 'table') { - $sql .= " `Comment` != 'VIEW'"; - } + $sql .= match ($tableType) { + TableType::View => " `Comment` = 'VIEW'", + TableType::Table => " `Comment` != 'VIEW'", + }; } } diff --git a/src/Dbal/TableType.php b/src/Dbal/TableType.php new file mode 100644 index 0000000000..f44ade6f07 --- /dev/null +++ b/src/Dbal/TableType.php @@ -0,0 +1,11 @@ + " AND t.`TABLE_TYPE` NOT IN ('BASE TABLE', 'SYSTEM VERSIONED')", + TableType::Table => " AND t.`TABLE_TYPE` IN ('BASE TABLE', 'SYSTEM VERSIONED')", + null => '', + }; } /** diff --git a/tests/unit/Controllers/Database/StructureControllerTest.php b/tests/unit/Controllers/Database/StructureControllerTest.php index 2ac8476192..a51f0fd999 100644 --- a/tests/unit/Controllers/Database/StructureControllerTest.php +++ b/tests/unit/Controllers/Database/StructureControllerTest.php @@ -544,6 +544,26 @@ class StructureControllerTest extends AbstractTestCase $dbiDummy->assertAllQueriesConsumed(); } + public function testGetDbInfoWithUnknownTableTypeFallsBackToFullList(): void + { + $this->config->selectedServer['DisableIS'] = true; + + $dbiDummy = $this->createDbiDummy(); + $dbiDummy->addResult('SHOW TABLES FROM `test_db`;', [['a_table']], ['Tables_in_test_db']); + $dbiDummy->addResult( + 'SHOW TABLE STATUS FROM `test_db` WHERE `Name` IN (\'a_table\')', + [['a_table', 'InnoDB', '3']], + ['Name', 'Engine', 'Rows'], + ); + $dbi = $this->createDatabaseInterface($dbiDummy, $this->config); + + [$tables, $totalNumTables] = $this->createController($dbi)->getDbInfo('test_db', null, null, null, 'bogus', 0); + + self::assertSame(['a_table'], array_keys($tables)); + self::assertSame(1, $totalNumTables); + $dbiDummy->assertAllQueriesConsumed(); + } + public function testGetDbInfoWithLimitedTableList(): void { $this->config->selectedServer['DisableIS'] = true;