-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathDbTableExists.php
More file actions
56 lines (44 loc) · 1.43 KB
/
DbTableExists.php
File metadata and controls
56 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
namespace PhpMyAdmin;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Identifiers\DatabaseName;
use PhpMyAdmin\Identifiers\TableName;
use function in_array;
use function sprintf;
final class DbTableExists
{
/** @psalm-var list<non-empty-string> */
private array $tables = [];
public function __construct(private readonly DatabaseInterface $dbi)
{
}
public function selectDatabase(DatabaseName $databaseName): bool
{
return $this->dbi->selectDb($databaseName);
}
/**
* Check if a table exists in the given database.
* It will return true if the table exists, regardless if it's temporary or permanent.
*/
public function hasTable(DatabaseName $database, TableName $table): bool
{
if (in_array($database->getName() . '.' . $table->getName(), $this->tables, true)) {
return true;
}
if ($this->tableExists($database, $table)) {
$this->tables[] = $database->getName() . '.' . $table->getName();
return true;
}
return false;
}
private function tableExists(DatabaseName $database, TableName $table): bool
{
// SHOW TABLES doesn't show temporary tables, so try select.
return $this->dbi->tryQuery(sprintf(
'SELECT 1 FROM %s.%s LIMIT 1;',
Util::backquote($database),
Util::backquote($table),
)) !== false;
}
}