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