Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / DbTableExists.php
blob7b321630cf53f8547b8ef1b5d8115d0505850ef2
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin;
7 use PhpMyAdmin\Identifiers\DatabaseName;
8 use PhpMyAdmin\Identifiers\TableName;
10 use function in_array;
11 use function sprintf;
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);
27 /**
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)) {
34 return true;
37 if ($this->tableExists($database, $table)) {
38 $this->tables[] = $database->getName() . '.' . $table->getName();
40 return true;
43 return false;
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),
53 )) !== false;