1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver\PgSql\Db; |
4
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Db\Server as AbstractServer; |
6
|
|
|
|
7
|
|
|
use Lagdo\DbAdmin\Driver\Db\StatementInterface; |
8
|
|
|
|
9
|
|
|
use function in_array; |
10
|
|
|
use function is_a; |
11
|
|
|
use function intval; |
12
|
|
|
|
13
|
|
|
class Server extends AbstractServer |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @inheritDoc |
17
|
|
|
*/ |
18
|
|
|
public function databases(bool $flush) |
19
|
|
|
{ |
20
|
|
|
$query = "SELECT datname FROM pg_database WHERE has_database_privilege(datname, 'CONNECT') " . |
21
|
|
|
"AND datname not in ('postgres','template0','template1') ORDER BY datname"; |
22
|
|
|
return $this->driver->values($query); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritDoc |
27
|
|
|
*/ |
28
|
|
|
public function databaseSize(string $database) |
29
|
|
|
{ |
30
|
|
|
$statement = $this->driver->execute("SELECT pg_database_size(" . $this->driver->quote($database) . ")"); |
31
|
|
|
if (is_a($statement, StatementInterface::class) && ($row = $statement->fetchRow())) { |
32
|
|
|
return intval($row[0]); |
33
|
|
|
} |
34
|
|
|
return 0; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
|
|
public function databaseCollation(string $database, array $collations) |
41
|
|
|
{ |
42
|
|
|
return $this->driver->result("SELECT datcollate FROM pg_database WHERE datname = " . $this->driver->quote($database)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
|
|
public function collations() |
49
|
|
|
{ |
50
|
|
|
//! supported in CREATE DATABASE |
51
|
|
|
return []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
|
|
public function isInformationSchema(string $database) |
58
|
|
|
{ |
59
|
|
|
return $database == "information_schema"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
|
|
public function isSystemSchema(string $database) |
66
|
|
|
{ |
67
|
|
|
return in_array($database, ['information_schema', |
68
|
|
|
'pg_catalog', 'pg_toast', 'postgres', 'template0', 'template1']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
|
|
public function createDatabase(string $database, string $collation) |
75
|
|
|
{ |
76
|
|
|
$result = $this->driver->execute("CREATE DATABASE " . $this->driver->escapeId($database) . |
77
|
|
|
($collation ? " ENCODING " . $this->driver->escapeId($collation) : "")); |
78
|
|
|
return $result !== false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritDoc |
83
|
|
|
*/ |
84
|
|
|
public function dropDatabase(string $database) |
85
|
|
|
{ |
86
|
|
|
// Cannot drop the connected database. |
87
|
|
|
if ($this->driver->database() === $database) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
$result = $this->driver->execute('DROP DATABASE ' . $this->driver->escapeId($database)); |
91
|
|
|
return $result !== false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
|
|
public function renameDatabase(string $name, string $collation) |
98
|
|
|
{ |
99
|
|
|
//! current database cannot be renamed |
100
|
|
|
$currName = $this->driver->escapeId($this->driver->database()); |
101
|
|
|
$nextName = $this->driver->escapeId($name); |
102
|
|
|
$result = $this->driver->execute("ALTER DATABASE $currName RENAME TO $nextName"); |
103
|
|
|
return $result !== false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
|
|
public function routineLanguages() |
110
|
|
|
{ |
111
|
|
|
return $this->driver->values("SELECT LOWER(lanname) FROM pg_catalog.pg_language"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
*/ |
117
|
|
|
public function variables() |
118
|
|
|
{ |
119
|
|
|
return $this->driver->keyValues("SHOW ALL"); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function processes() |
126
|
|
|
{ |
127
|
|
|
return $this->driver->rows("SELECT * FROM pg_stat_activity ORDER BY " . ($this->driver->minVersion(9.2) ? "pid" : "procpid")); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritDoc |
132
|
|
|
*/ |
133
|
|
|
public function processAttr(array $process, string $key, string $val): string |
134
|
|
|
{ |
135
|
|
|
if ($key == "current_query" && $val != "<IDLE>") { |
136
|
|
|
return '<code>' . $this->utils->str->shortenUtf8($val, 50) . '</code>' . $this->utils->trans->lang('Clone'); |
137
|
|
|
} |
138
|
|
|
return parent::processAttr($process, $key, $val); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
*/ |
144
|
|
|
public function statusVariables() |
145
|
|
|
{ |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @inheritDoc |
150
|
|
|
*/ |
151
|
|
|
// public function killProcess($val) |
152
|
|
|
// { |
153
|
|
|
// return $this->driver->execute("SELECT pg_terminate_backend(" . $this->utils->str->number($val) . ")"); |
154
|
|
|
// } |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
|
|
// public function connectionId() |
160
|
|
|
// { |
161
|
|
|
// return "SELECT pg_backend_pid()"; |
162
|
|
|
// } |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @inheritDoc |
166
|
|
|
*/ |
167
|
|
|
// public function maxConnections() |
168
|
|
|
// { |
169
|
|
|
// return $this->driver->result("SHOW max_connections"); |
170
|
|
|
// } |
171
|
|
|
} |
172
|
|
|
|