-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCommand.php
More file actions
79 lines (61 loc) · 2.35 KB
/
Command.php
File metadata and controls
79 lines (61 loc) · 2.35 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Pgsql;
use InvalidArgumentException;
use LogicException;
use Yiisoft\Db\Driver\Pdo\AbstractPdoCommand;
use function sprintf;
/**
* Implements a database command that can be executed with a PDO (PHP Data Object) database connection for PostgreSQL
* Server.
*/
final class Command extends AbstractPdoCommand
{
public function showDatabases(): array
{
$sql = <<<SQL
SELECT datname FROM pg_database WHERE datistemplate = false AND datname NOT IN ('postgres', 'template0', 'template1')
SQL;
return $this->setSql($sql)->queryColumn();
}
/**
* @see {https://www.postgresql.org/docs/current/sql-refreshmaterializedview.html}
*
* @param string $viewName
* @param bool|null $concurrently Add [ CONCURRENTLY ] to refresh command
* @param bool|null $withData Add [ WITH [ NO ] DATA ] to refresh command
* @return bool
* @throws \Throwable
* @throws \Yiisoft\Db\Exception\Exception
* @throws \Yiisoft\Db\Exception\InvalidConfigException
*/
public function refreshMaterializedView(string $viewName, ?bool $concurrently = null, ?bool $withData = null): bool
{
if ($concurrently || ($concurrently === null || $withData === null)) {
$tableSchema = $this->db->getTableSchema($viewName);
if ($tableSchema) {
$hasUnique = count($this->db->getSchema()->findUniqueIndexes($tableSchema)) > 0;
} else {
throw new InvalidArgumentException(
sprintf('"%s" not found in DB', $viewName)
);
}
if ($concurrently && !$hasUnique) {
throw new LogicException('CONCURRENTLY refresh is not allowed without unique index.');
}
$concurrently = $hasUnique;
}
$sql = 'REFRESH MATERIALIZED VIEW';
if ($concurrently) {
if ($withData === false) {
throw new LogicException('CONCURRENTLY and WITH NO DATA may not be specified together.');
}
$sql .= ' CONCURRENTLY';
}
$sql .= ' ' . $this->db->getQuoter()->quoteTableName($viewName);
if (is_bool($withData)) {
$sql .= ' WITH ' . ($withData ? 'DATA' : 'NO DATA');
}
return $this->setSql($sql)->execute() === 0;
}
}