3 declare(strict_types
=1);
7 use PhpMyAdmin\Identifiers\DatabaseName
;
8 use PhpMyAdmin\Identifiers\TableName
;
10 use function in_array
;
13 final class DbTableExists
15 /** @psalm-var list<non-empty-string> */
16 private array $tables = [];
18 public function __construct(private readonly DatabaseInterface
$dbi)
22 public function selectDatabase(DatabaseName
$databaseName): bool
24 return $this->dbi
->selectDb($databaseName);
28 * Check if a table exists in the given database.
29 * It will return true if the table exists, regardless if it's temporary or permanent.
31 public function hasTable(DatabaseName
$database, TableName
$table): bool
33 if (in_array($database->getName() . '.' . $table->getName(), $this->tables
, true)) {
37 if ($this->tableExists($database, $table)) {
38 $this->tables
[] = $database->getName() . '.' . $table->getName();
46 private function tableExists(DatabaseName
$database, TableName
$table): bool
48 // SHOW TABLES doesn't show temporary tables, so try select.
49 return $this->dbi
->tryQuery(sprintf(
50 'SELECT 1 FROM %s.%s LIMIT 1;',
51 Util
::backquote($database),
52 Util
::backquote($table),