|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Core\DatabaseManager; |
|
4
|
|
|
|
|
5
|
|
|
use Db3v4l\API\Interfaces\DatabaseManager; |
|
6
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\Executor; |
|
7
|
|
|
use Db3v4l\Core\SqlAction\Command; |
|
8
|
|
|
|
|
9
|
|
|
class PostgreSQL extends BaseManager implements DatabaseManager |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Returns the sql 'action' used to list all available databases |
|
13
|
|
|
* @return Command |
|
|
|
|
|
|
14
|
|
|
* @todo for each database, retrieve the charset/collation |
|
|
|
|
|
|
15
|
|
|
*/ |
|
16
|
|
|
public function getListDatabasesSqlAction() |
|
17
|
|
|
{ |
|
18
|
|
|
return new Command( |
|
19
|
|
|
'SELECT datname AS "Database" FROM pg_database ORDER BY datname;', |
|
20
|
|
|
function ($output, $executor) { |
|
21
|
|
|
/** @var Executor $executor */ |
|
|
|
|
|
|
22
|
|
|
return $executor->resultSetToArray($output); |
|
23
|
|
|
} |
|
24
|
|
|
); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Returns the sql 'action' used to create a new db and accompanying user |
|
29
|
|
|
* @param string $dbName Max 63 chars for Postgres |
|
|
|
|
|
|
30
|
|
|
* @param string $userName |
|
|
|
|
|
|
31
|
|
|
* @param string $password |
|
|
|
|
|
|
32
|
|
|
* @param string $charset charset/collation name |
|
|
|
|
|
|
33
|
|
|
* @return Command |
|
|
|
|
|
|
34
|
|
|
* @todo prevent sql injection! |
|
|
|
|
|
|
35
|
|
|
*/ |
|
36
|
|
|
public function getCreateDatabaseSqlAction($dbName, $userName, $password, $charset = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$collation = $this->getCollationName($charset); |
|
39
|
|
|
|
|
40
|
|
|
$statements = [ |
|
41
|
|
|
// q: do we need to add 'TEMPLATE template0' ? |
|
42
|
|
|
// see f.e. https://www.vertabelo.com/blog/collations-in-postgresql/ |
|
43
|
|
|
"CREATE DATABASE \"$dbName\"" . ($collation !== null ? " ENCODING $collation" : '') . ';', |
|
44
|
|
|
]; |
|
45
|
|
|
if ($userName != '') { |
|
46
|
|
|
$statements[] = "COMMIT;"; |
|
47
|
|
|
$statements[] = "CREATE USER \"$userName\" WITH PASSWORD '$password'" . ';'; |
|
48
|
|
|
$statements[] = "GRANT ALL ON DATABASE \"$dbName\" TO \"$userName\""; // q: should we avoid granting CREATE? |
|
49
|
|
|
} |
|
50
|
|
|
return new Command($statements); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns the sql 'action' used to drop a db and associated user account |
|
55
|
|
|
* @param string $dbName |
|
|
|
|
|
|
56
|
|
|
* @param string $userName |
|
|
|
|
|
|
57
|
|
|
* @return Command |
|
|
|
|
|
|
58
|
|
|
* @param bool $ifExists |
|
|
|
|
|
|
59
|
|
|
* @todo prevent sql injection! |
|
|
|
|
|
|
60
|
|
|
*/ |
|
61
|
|
|
public function getDropDatabaseSqlAction($dbName, $userName, $ifExists = false) |
|
62
|
|
|
{ |
|
63
|
|
|
$ifClause = ''; |
|
64
|
|
|
if ($ifExists) { |
|
65
|
|
|
$ifClause = 'IF EXISTS'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$statements = [ |
|
69
|
|
|
"DROP DATABASE {$ifClause} \"$dbName\";", |
|
70
|
|
|
]; |
|
71
|
|
|
if ($userName != '') { |
|
72
|
|
|
$statements[] = "DROP USER {$ifClause} \"$userName\";"; |
|
73
|
|
|
} |
|
74
|
|
|
return new Command($statements); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the sql 'action' used to list all existing db users |
|
79
|
|
|
* @return Command |
|
|
|
|
|
|
80
|
|
|
*/ |
|
81
|
|
|
public function getListUsersSqlAction() |
|
82
|
|
|
{ |
|
83
|
|
|
return new Command( |
|
84
|
|
|
'SELECT usename AS "User" FROM pg_catalog.pg_user ORDER BY usename;', |
|
85
|
|
|
function ($output, $executor) { |
|
86
|
|
|
/** @var Executor $executor */ |
|
|
|
|
|
|
87
|
|
|
return $executor->resultSetToArray($output); |
|
88
|
|
|
} |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
|
|
|
|
|
93
|
|
|
* @param string $userName |
|
|
|
|
|
|
94
|
|
|
* @param bool $ifExists |
|
|
|
|
|
|
95
|
|
|
* @return Command |
|
|
|
|
|
|
96
|
|
|
* @todo prevent sql injection! |
|
|
|
|
|
|
97
|
|
|
*/ |
|
98
|
|
|
public function getDropUserSqlAction($userName, $ifExists = false) |
|
99
|
|
|
{ |
|
100
|
|
|
$ifClause = ''; |
|
101
|
|
|
if ($ifExists) { |
|
102
|
|
|
$ifClause = 'IF EXISTS'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return new Command([ |
|
|
|
|
|
|
106
|
|
|
"DROP USER {$ifClause} \"$userName\";" |
|
107
|
|
|
]); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns the sql 'action' used to list all available collations |
|
112
|
|
|
* @return Command |
|
|
|
|
|
|
113
|
|
|
*/ |
|
114
|
|
|
public function getListCollationsSqlAction() |
|
115
|
|
|
{ |
|
116
|
|
|
return new Command( |
|
117
|
|
|
'SELECT collname AS Collation FROM pg_collation ORDER BY collname', |
|
118
|
|
|
function ($output, $executor) { |
|
119
|
|
|
/** @var Executor $executor */ |
|
|
|
|
|
|
120
|
|
|
return $executor->resultSetToArray($output); |
|
121
|
|
|
} |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns the sql 'action' used to retrieve the db instance version info |
|
127
|
|
|
* @return Command |
|
|
|
|
|
|
128
|
|
|
*/ |
|
129
|
|
|
public function getRetrieveVersionInfoSqlAction() |
|
130
|
|
|
{ |
|
131
|
|
|
return new Command( |
|
132
|
|
|
'SHOW server_version;', |
|
133
|
|
|
function ($output, $executor) { |
|
134
|
|
|
/** @var Executor $executor */ |
|
|
|
|
|
|
135
|
|
|
return $executor->resultSetToArray($output)[0]; |
|
136
|
|
|
} |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Transform collation name into a supported one |
|
142
|
|
|
* @param null|string $charset so far only 'utf8' is supported... |
|
|
|
|
|
|
143
|
|
|
* @return null|string |
|
|
|
|
|
|
144
|
|
|
* @todo what shall we accept as valid input, ie. 'generic' charset names ? maybe do 2 passes: known-db-charset => generic => specific for each db ? |
|
|
|
|
|
|
145
|
|
|
* see: https://www.iana.org/assignments/character-sets/character-sets.xhtml for IANA names |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function getCollationName($charset) |
|
148
|
|
|
{ |
|
149
|
|
|
if ($charset == null) { |
|
|
|
|
|
|
150
|
|
|
return null; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$charset = trim(strtolower($charset)); |
|
154
|
|
|
|
|
155
|
|
|
// accept official iana charset name, but most dbs prefer 'utf8'... |
|
156
|
|
|
if ($charset == 'utf-8') { |
|
157
|
|
|
$charset = 'utf8'; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $charset; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|