1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql; |
6
|
|
|
|
7
|
|
|
use LogicException; |
8
|
|
|
use Yiisoft\Db\Driver\Pdo\AbstractPdoCommand; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Implements a database command that can be executed with a PDO (PHP Data Object) database connection for PostgreSQL |
12
|
|
|
* Server. |
13
|
|
|
*/ |
14
|
|
|
final class Command extends AbstractPdoCommand |
15
|
1 |
|
{ |
16
|
|
|
public function showDatabases(): array |
17
|
1 |
|
{ |
18
|
|
|
$sql = <<<SQL |
19
|
1 |
|
SELECT datname FROM pg_database WHERE datistemplate = false AND datname NOT IN ('postgres', 'template0', 'template1') |
20
|
|
|
SQL; |
21
|
1 |
|
|
22
|
|
|
return $this->setSql($sql)->queryColumn(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @see {https://www.postgresql.org/docs/current/sql-refreshmaterializedview.html} |
27
|
|
|
* |
28
|
|
|
* @param string $viewName |
29
|
|
|
* @param bool|null $concurrently Add [ CONCURRENTLY ] to refresh command |
30
|
|
|
* @param bool|null $withData Add [ WITH [ NO ] DATA ] to refresh command |
31
|
|
|
* @return void |
32
|
|
|
* @throws \Throwable |
33
|
|
|
* @throws \Yiisoft\Db\Exception\Exception |
34
|
|
|
* @throws \Yiisoft\Db\Exception\InvalidConfigException |
35
|
|
|
*/ |
36
|
|
|
public function refreshMaterializedView(string $viewName, ?bool $concurrently = null, ?bool $withData = null): void |
37
|
|
|
{ |
38
|
|
|
if ($concurrently || ($concurrently === null || $withData === null)) { |
39
|
|
|
|
40
|
|
|
$tableSchema = $this->db->getTableSchema($viewName); |
41
|
|
|
$hasUnique = count($this->db->getSchema()->findUniqueIndexes($tableSchema)) > 0; |
|
|
|
|
42
|
|
|
|
43
|
|
|
if ($concurrently && !$hasUnique) { |
44
|
|
|
throw new LogicException('CONCURRENTLY refresh is not allowed without unique index.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$concurrently = $hasUnique; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$sql = 'REFRESH MATERIALIZED VIEW'; |
51
|
|
|
|
52
|
|
|
if ($concurrently) { |
53
|
|
|
|
54
|
|
|
if ($withData === false) { |
55
|
|
|
throw new LogicException('CONCURRENTLY and WITH NO DATA may not be specified together.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$sql .= ' CONCURRENTLY'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$sql .= ' ' . $this->db->getQuoter()->quoteTableName($viewName); |
62
|
|
|
|
63
|
|
|
if (is_bool($withData)) { |
64
|
|
|
$sql .= ' WITH ' . ($withData ? 'DATA' : 'NO DATA'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->setSql($sql)->execute(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|