1
0
mirror of https://github.com/phpmyadmin/phpmyadmin.git synced 2026-10-23 18:29:15 +03:00

Introduce TableType enum for table list type filtering

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kamil Tekiela
2026-07-16 16:35:32 +01:00
parent 9b9ed5485b
commit cb7a87d115
5 changed files with 59 additions and 31 deletions

View File

@@ -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);

View File

@@ -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'",
};
}
}

11
src/Dbal/TableType.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
enum TableType: string
{
case Table = 'table';
case View = 'view';
}

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace PhpMyAdmin\Query;
use PhpMyAdmin\Database\RoutineType;
use PhpMyAdmin\Dbal\TableType;
use PhpMyAdmin\Triggers\Trigger;
use PhpMyAdmin\Util;
@@ -52,22 +53,17 @@ class Generator
/**
* returns a segment of the SQL WHERE clause regarding table type
*
* @param string|null $tableType whether table or view
* @param TableType|null $tableType whether to list only tables or only views
*
* @return string a segment of the WHERE clause
*/
public static function getTableTypeCondition(
string|null $tableType,
): string {
$sqlWhereTable = '';
if ($tableType === 'view') {
$sqlWhereTable .= " AND t.`TABLE_TYPE` NOT IN ('BASE TABLE', 'SYSTEM VERSIONED')";
} elseif ($tableType === 'table') {
$sqlWhereTable .= " AND t.`TABLE_TYPE` IN ('BASE TABLE', 'SYSTEM VERSIONED')";
}
return $sqlWhereTable;
public static function getTableTypeCondition(TableType|null $tableType): string
{
return match ($tableType) {
TableType::View => " AND t.`TABLE_TYPE` NOT IN ('BASE TABLE', 'SYSTEM VERSIONED')",
TableType::Table => " AND t.`TABLE_TYPE` IN ('BASE TABLE', 'SYSTEM VERSIONED')",
null => '',
};
}
/**

View File

@@ -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;