|
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
|
|
|
/** |
|
10
|
|
|
* This DBManager uses schemas as 'database'. |
|
11
|
|
|
* NB: if used with Oracle >= 12, usernames have to be prefixed with c## to be 'common' (ie. in the CDB), so you might |
|
12
|
|
|
* want to run everything inside a pdb... |
|
13
|
|
|
*/ |
|
|
|
|
|
|
14
|
|
|
class Oracle extends BaseManager implements DatabaseManager |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Returns the sql 'action' used to list all available databases |
|
18
|
|
|
* @return Command |
|
|
|
|
|
|
19
|
|
|
* @todo for each database, retrieve the charset/collation |
|
|
|
|
|
|
20
|
|
|
*/ |
|
21
|
|
|
public function getListDatabasesSqlAction() |
|
22
|
|
|
{ |
|
23
|
|
|
return new Command( |
|
24
|
|
|
"SELECT username AS Database FROM all_users WHERE oracle_maintained != 'Y' ORDER BY username;", |
|
25
|
|
|
function ($output, $executor) { |
|
26
|
|
|
/** @var Executor $executor */ |
|
|
|
|
|
|
27
|
|
|
return $executor->resultSetToArray($output); |
|
28
|
|
|
} |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns the sql 'action' used to create a new db and accompanying user |
|
34
|
|
|
* @param string $dbName |
|
|
|
|
|
|
35
|
|
|
* @param string $userName |
|
|
|
|
|
|
36
|
|
|
* @param string $password |
|
|
|
|
|
|
37
|
|
|
* @param string $charset charset/collation name |
|
|
|
|
|
|
38
|
|
|
* @return Command |
|
|
|
|
|
|
39
|
|
|
* @throws \Exception |
|
|
|
|
|
|
40
|
|
|
* @todo prevent sql injection! |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
public function getCreateDatabaseSqlAction($dbName, $userName, $password, $charset = null) |
|
43
|
|
|
{ |
|
44
|
|
|
//$collation = $this->getCollationName($charset); |
|
45
|
|
|
|
|
46
|
|
|
/// @todo throw if $charset is not the same as the db one |
|
47
|
|
|
|
|
48
|
|
|
if ($userName != '' && $userName != $dbName) { |
|
49
|
|
|
throw new \Exception("Can not create a database (schema) on Oracle with a different accompanying user"); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// NB: if we use a quoted identifier for the username, logging in becomes more difficult, as it will require quotes too... |
|
53
|
|
|
$statements = [ |
|
54
|
|
|
"CREATE USER $dbName IDENTIFIED BY \"$password\";", |
|
55
|
|
|
"GRANT CONNECT, RESOURCE TO $dbName;", |
|
56
|
|
|
"GRANT UNLIMITED TABLESPACE TO $dbName;", |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
return new Command($statements); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns the sql 'action' used to drop a db and associated user account |
|
64
|
|
|
* @param string $dbName |
|
|
|
|
|
|
65
|
|
|
* @param string $userName |
|
|
|
|
|
|
66
|
|
|
* @return Command |
|
|
|
|
|
|
67
|
|
|
* @param bool $ifExists |
|
|
|
|
|
|
68
|
|
|
* @bug $ifExists = true not supported |
|
|
|
|
|
|
69
|
|
|
* @throws \Exception |
|
|
|
|
|
|
70
|
|
|
* @todo prevent sql injection! |
|
|
|
|
|
|
71
|
|
|
*/ |
|
72
|
|
|
public function getDropDatabaseSqlAction($dbName, $userName, $ifExists = false) |
|
73
|
|
|
{ |
|
74
|
|
|
if ($userName != '' && $userName != $dbName) { |
|
75
|
|
|
throw new \Exception("Can not drop a database (schema) on Oracle with a different accompanying user"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/// @todo check support for IF EXISTS |
|
79
|
|
|
$statements = [ |
|
80
|
|
|
"DROP USER $dbName CASCADE;", |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
return new Command($statements); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the sql 'action' used to list all existing db users |
|
88
|
|
|
* @return Command |
|
|
|
|
|
|
89
|
|
|
*/ |
|
90
|
|
|
public function getListUsersSqlAction() |
|
91
|
|
|
{ |
|
92
|
|
|
return new Command( |
|
93
|
|
|
// NB: we filter out 'system' users, as there are many... |
|
94
|
|
|
"SELECT username FROM sys.all_users WHERE oracle_maintained != 'Y' ORDER BY username;", |
|
95
|
|
|
function ($output, $executor) { |
|
96
|
|
|
/** @var Executor $executor */ |
|
|
|
|
|
|
97
|
|
|
return $executor->resultSetToArray($output); |
|
98
|
|
|
} |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
|
|
|
|
|
103
|
|
|
* @param string $userName |
|
|
|
|
|
|
104
|
|
|
* @param bool $ifExists |
|
|
|
|
|
|
105
|
|
|
* @return Command |
|
|
|
|
|
|
106
|
|
|
* @bug $ifExists = true not supported |
|
|
|
|
|
|
107
|
|
|
* @todo prevent sql injection! |
|
|
|
|
|
|
108
|
|
|
* @todo we should somehow warn users that this destroys the schema too |
|
|
|
|
|
|
109
|
|
|
*/ |
|
110
|
|
|
public function getDropUserSqlAction($userName, $ifExists = false) |
|
111
|
|
|
{ |
|
112
|
|
|
/// @todo check support for IF EXISTS |
|
113
|
|
|
|
|
114
|
|
|
return new Command([ |
|
|
|
|
|
|
115
|
|
|
"DROP USER $userName CASCADE;", |
|
116
|
|
|
]); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns the sql 'action' used to list all available collations |
|
121
|
|
|
* @return Command |
|
|
|
|
|
|
122
|
|
|
*/ |
|
123
|
|
|
public function getListCollationsSqlAction() |
|
124
|
|
|
{ |
|
125
|
|
|
return new Command( |
|
126
|
|
|
"SELECT value AS Collation FROM v\$nls_valid_values WHERE parameter = 'CHARACTERSET' ORDER BY value;", |
|
127
|
|
|
function ($output, $executor) { |
|
128
|
|
|
/** @var Executor $executor */ |
|
|
|
|
|
|
129
|
|
|
return $executor->resultSetToArray($output); |
|
130
|
|
|
} |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Returns the sql 'action' used to retrieve the db instance version info |
|
136
|
|
|
* @return Command |
|
|
|
|
|
|
137
|
|
|
*/ |
|
138
|
|
|
public function getRetrieveVersionInfoSqlAction() |
|
139
|
|
|
{ |
|
140
|
|
|
return new Command( |
|
141
|
|
|
"SELECT version_full || ' (' || banner || ')' AS version FROM v\$version, v\$instance;", |
|
142
|
|
|
function ($output, $executor) { |
|
143
|
|
|
/** @var Executor $executor */ |
|
|
|
|
|
|
144
|
|
|
return $executor->resultSetToArray($output)[0]; |
|
145
|
|
|
} |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Transform collation name into a supported one |
|
151
|
|
|
* @param null|string $charset so far only 'utf8' is supported... |
|
|
|
|
|
|
152
|
|
|
* @return null|string |
|
|
|
|
|
|
153
|
|
|
* @todo what shall we accept as valid input, ie. 'generic' charset names ? maybe do 2 passes: known-db-charset => generic => specific for each db ? |
|
|
|
|
|
|
154
|
|
|
* see: https://www.iana.org/assignments/character-sets/character-sets.xhtml for IANA names |
|
155
|
|
|
*/ |
|
156
|
|
|
/*protected function getCollationName($charset) |
|
157
|
|
|
{ |
|
158
|
|
|
if ($charset == null) { |
|
159
|
|
|
return null; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$charset = trim(strtolower($charset)); |
|
163
|
|
|
|
|
164
|
|
|
// accept official iana charset name, but most dbs prefer 'utf8'... |
|
165
|
|
|
if ($charset == 'utf-8') { |
|
166
|
|
|
$charset = 'utf8'; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if ($charset == 'utf8') { |
|
170
|
|
|
$charset = 'AL32UTF8'; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $charset; |
|
174
|
|
|
}*/ |
|
175
|
|
|
} |
|
176
|
|
|
|