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