3 declare(strict_types
=1);
7 use PhpMyAdmin\Dbal\DatabaseInterface
;
8 use PhpMyAdmin\Identifiers\DatabaseName
;
9 use PhpMyAdmin\Identifiers\TableName
;
11 use function in_array
;
14 final class DbTableExists
16 /** @psalm-var list<non-empty-string> */
17 private array $tables = [];
19 public function __construct(private readonly DatabaseInterface
$dbi)
23 public function selectDatabase(DatabaseName
$databaseName): bool
25 return $this->dbi
->selectDb($databaseName);
29 * Check if a table exists in the given database.
30 * It will return true if the table exists, regardless if it's temporary or permanent.
32 public function hasTable(DatabaseName
$database, TableName
$table): bool
34 if (in_array($database->getName() . '.' . $table->getName(), $this->tables
, true)) {
38 if ($this->tableExists($database, $table)) {
39 $this->tables
[] = $database->getName() . '.' . $table->getName();
47 private function tableExists(DatabaseName
$database, TableName
$table): bool
49 // SHOW TABLES doesn't show temporary tables, so try select.
50 return $this->dbi
->tryQuery(sprintf(
51 'SELECT 1 FROM %s.%s LIMIT 1;',
52 Util
::backquote($database),
53 Util
::backquote($table),