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 MySQL 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
|
|
|
/// @todo use 'SHOW DATABASES' for versions < 5 |
20
|
|
|
"SELECT SCHEMA_NAME AS 'Database' FROM information_schema.SCHEMATA ORDER BY SCHEMA_NAME;", |
21
|
|
|
function ($output, $executor) { |
22
|
|
|
/** @var Executor $executor */ |
|
|
|
|
23
|
|
|
return $executor->resultSetToArray($output); |
24
|
|
|
} |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns the sql 'action' used to create a new db and accompanying user |
30
|
|
|
* @param string $dbName |
|
|
|
|
31
|
|
|
* @param string $userName Max 16 chars for MySQL 5.5 |
|
|
|
|
32
|
|
|
* @param string $password |
|
|
|
|
33
|
|
|
* @param string $charset charset/collation name |
|
|
|
|
34
|
|
|
* @return Command |
|
|
|
|
35
|
|
|
* @todo prevent sql injection! |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function getCreateDatabaseSqlAction($dbName, $userName, $password, $charset = null) |
38
|
|
|
{ |
39
|
|
|
$collation = $this->getCollationName($charset); |
40
|
|
|
|
41
|
|
|
/// @todo if mysql version is bigger than 8.0, add `WITH mysql_native_password` |
42
|
|
|
$statements = [ |
43
|
|
|
"CREATE DATABASE `$dbName`" . ($collation !== null ? " CHARACTER SET $collation" : '') . ';' |
44
|
|
|
]; |
45
|
|
|
if ($userName != '') { |
46
|
|
|
$statements[] = "CREATE USER '$userName'@'%' IDENTIFIED BY '$password';"; |
47
|
|
|
$statements[] = "GRANT ALL PRIVILEGES ON `$dbName`.* TO '$userName'@'%';"; |
48
|
|
|
} |
49
|
|
|
return new Command($statements); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the sql 'action' used to drop a db and associated user account |
54
|
|
|
* @param string $dbName |
|
|
|
|
55
|
|
|
* @param string $userName |
|
|
|
|
56
|
|
|
* @return Command |
|
|
|
|
57
|
|
|
* @param bool $ifExists |
|
|
|
|
58
|
|
|
* @bug $ifExists = true not fully supported |
|
|
|
|
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
|
|
|
/// @todo since mysql 5.7, 'DROP USER IF EXISTS' is supported. We could use it... |
73
|
|
|
$statements[] = "DROP USER '$userName'@'%';"; |
74
|
|
|
} |
75
|
|
|
return new Command($statements); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the sql 'action' used to list all existing db users |
80
|
|
|
* @return Command |
|
|
|
|
81
|
|
|
*/ |
82
|
|
|
public function getListUsersSqlAction() |
83
|
|
|
{ |
84
|
|
|
return new Command( |
85
|
|
|
'SELECT DISTINCT User FROM mysql.user ORDER BY User;', |
86
|
|
|
function ($output, $executor) { |
87
|
|
|
/** @var Executor $executor */ |
|
|
|
|
88
|
|
|
return $executor->resultSetToArray($output); |
89
|
|
|
} |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
|
|
|
|
94
|
|
|
* @param string $userName |
|
|
|
|
95
|
|
|
* @param bool $ifExists |
|
|
|
|
96
|
|
|
* @return Command |
|
|
|
|
97
|
|
|
* @bug $ifExists = true not supported |
|
|
|
|
98
|
|
|
* @todo prevent sql injection! |
|
|
|
|
99
|
|
|
*/ |
100
|
|
|
public function getDropUserSqlAction($userName, $ifExists = false) |
101
|
|
|
{ |
102
|
|
|
/*$ifClause = ''; |
103
|
|
|
if ($ifExists) { |
104
|
|
|
$ifClause = 'IF EXISTS'; |
105
|
|
|
}*/ |
106
|
|
|
|
107
|
|
|
/// @todo since mysql 5.7, 'DROP USER IF EXISTS' is supported. We could use it... |
108
|
|
|
return new Command([ |
|
|
|
|
109
|
|
|
"DROP USER '$userName'@'%';" |
110
|
|
|
]); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the sql 'action' used to list all available collations |
115
|
|
|
* @return Command |
|
|
|
|
116
|
|
|
*/ |
117
|
|
|
public function getListCollationsSqlAction() |
118
|
|
|
{ |
119
|
|
|
return new Command( |
120
|
|
|
'SHOW COLLATION;', |
121
|
|
|
function ($output, $executor) { |
122
|
|
|
/** @var Executor $executor */ |
|
|
|
|
123
|
|
|
$lines = $executor->resultSetToArray($output); |
124
|
|
|
$out = []; |
125
|
|
|
foreach($lines as $line) { |
|
|
|
|
126
|
|
|
$parts = explode("|", $line, 3); |
127
|
|
|
$out[] = trim($parts[0]) . ' (' . trim($parts[1]) .')'; |
128
|
|
|
} |
129
|
|
|
return $out; |
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
|
|
|
'SHOW VARIABLES LIKE "version";', |
142
|
|
|
function ($output, $executor) { |
143
|
|
|
/** @var Executor $executor */ |
|
|
|
|
144
|
|
|
$line = $executor->resultSetToArray($output)[0]; |
145
|
|
|
$parts = explode('|', $line); |
146
|
|
|
return trim($parts[1]); |
147
|
|
|
} |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Transform collation name into a supported one |
153
|
|
|
* @param null|string $charset so far only 'utf8' is supported... |
|
|
|
|
154
|
|
|
* @return null|string |
|
|
|
|
155
|
|
|
* @todo what shall we accept as valid input, ie. 'generic' charset names ? maybe do 2 passes: known-db-charset => generic => specific for each db ? |
|
|
|
|
156
|
|
|
* see: https://www.iana.org/assignments/character-sets/character-sets.xhtml for IANA names |
157
|
|
|
*/ |
158
|
|
|
protected function getCollationName($charset) |
159
|
|
|
{ |
160
|
|
|
if ($charset == null) { |
|
|
|
|
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$charset = trim(strtolower($charset)); |
165
|
|
|
|
166
|
|
|
// accept official iana charset name, but most dbs prefer 'utf8'... |
167
|
|
|
if ($charset == 'utf-8') { |
168
|
|
|
$charset = 'utf8'; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $charset; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|