|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ___ _ _ |
|
5
|
|
|
* | _ \__ _| |_ __ _| |__ __ _ ___ ___ |
|
6
|
|
|
* | _/ _` | _/ _` | '_ \/ _` (_-</ -_) |
|
7
|
|
|
* |_| \__,_|\__\__,_|_.__/\__,_/__/\___| |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of Kristuff\Patabase. |
|
10
|
|
|
* (c) Kristuff <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
13
|
|
|
* file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* @version 1.0.1 |
|
16
|
|
|
* @copyright 2017-2022 Christophe Buliard |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Kristuff\Patabase; |
|
20
|
|
|
|
|
21
|
|
|
use Kristuff\Patabase\Datasource; |
|
22
|
|
|
use Kristuff\Patabase\Driver; |
|
23
|
|
|
use Kristuff\Patabase\Driver\ServerDriver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class Server |
|
27
|
|
|
* |
|
28
|
|
|
* Represents a connection to SQL database server |
|
29
|
|
|
*/ |
|
30
|
|
|
class Server extends Datasource |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @access protected |
|
34
|
|
|
* @var ServerDriver $driver The Datasource driver |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $driver = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Open a server connection |
|
40
|
|
|
* |
|
41
|
|
|
* @access protected |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function openConnection(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->driver = Driver\DriverFactory::getInstance($this->settings, true); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get a list of active databases in the server |
|
51
|
|
|
* |
|
52
|
|
|
* @access public |
|
53
|
|
|
* @return array A non indexed array containing databases names |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getDatabases(): array |
|
56
|
|
|
{ |
|
57
|
|
|
$sql = $this->driver->sqlShowDatabases(); |
|
58
|
|
|
$query = $this->driver->getConnection()->prepare($sql); |
|
59
|
|
|
$query->execute(); |
|
60
|
|
|
return $query->fetchAll(\PDO::FETCH_COLUMN); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get a list of active users in the server |
|
65
|
|
|
* |
|
66
|
|
|
* @access public |
|
67
|
|
|
* @return array A non indexed array containing users names |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getUsers(): array |
|
70
|
|
|
{ |
|
71
|
|
|
$sql = $this->driver->sqlShowUsers(); |
|
72
|
|
|
$query = $this->driver->getConnection()->prepare($sql); |
|
73
|
|
|
$query->execute(); |
|
74
|
|
|
return $query->fetchAll(\PDO::FETCH_COLUMN); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get whether the given database exists in the server |
|
79
|
|
|
* |
|
80
|
|
|
* @access public |
|
81
|
|
|
* @param string $databaseName The database name |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool True if the given database exists, otherwise false. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function databaseExists($databaseName): bool |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->driver->databaseExists($databaseName); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get whether the given user exists in the server |
|
92
|
|
|
* |
|
93
|
|
|
* @access public |
|
94
|
|
|
* @param string $userName The user name |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool True if the given user exists, otherwise false. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function userExists($userName): bool |
|
99
|
|
|
{ |
|
100
|
|
|
$usrs = $this->getUsers(); |
|
101
|
|
|
return in_array($userName, $usrs); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Create a database and a user for this database |
|
106
|
|
|
* |
|
107
|
|
|
* @access public |
|
108
|
|
|
* @param string $databaseName The database name |
|
109
|
|
|
* @param string $userName The database user name |
|
110
|
|
|
* @param string $password The user password |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool True if the database and user have been created, otherwise false. |
|
113
|
|
|
*/ |
|
114
|
|
|
public function createDatabaseAndUser(string $databaseName, string $userName, string $password): bool |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->createUser($userName, $password) |
|
117
|
|
|
&& $this->createDatabase($databaseName) |
|
118
|
|
|
&& $this->grantUser($databaseName, $userName); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Create a database |
|
123
|
|
|
* |
|
124
|
|
|
* @access public |
|
125
|
|
|
* @param string $databaseName The database name |
|
126
|
|
|
* @param string $owner (optional) The database owner (used in postgres only). Default is null. |
|
127
|
|
|
* |
|
128
|
|
|
* @return bool True if the database has been created, otherwise false. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function createDatabase(string $databaseName, ?string $owner = null): bool |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->driver->createDatabase($databaseName, $owner); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Drop a database |
|
137
|
|
|
* |
|
138
|
|
|
* @access public |
|
139
|
|
|
* @param string $databaseName The database name. |
|
140
|
|
|
* @param bool $ifExists (optional) Set whether the database must be deleted only when exists. |
|
141
|
|
|
* |
|
142
|
|
|
* @return bool True if the database has been dropped or does not exist when $ifExists |
|
143
|
|
|
* is set to True, otherwise false. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function dropDatabase(string $databaseName, bool $ifExists = false): bool |
|
146
|
|
|
{ |
|
147
|
|
|
$sql = trim(sprintf('DROP DATABASE %s %s', |
|
148
|
|
|
$ifExists === true ? 'IF EXISTS': '', |
|
149
|
|
|
$this->driver->escape($databaseName))); |
|
150
|
|
|
return $this->driver->prepareAndExecuteSql($sql); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Create a user |
|
155
|
|
|
* |
|
156
|
|
|
* @access public |
|
157
|
|
|
* @param string $userName The user name |
|
158
|
|
|
* @param string $userpassword The user password |
|
159
|
|
|
* |
|
160
|
|
|
* @return bool True if the user has been created, otherwise false |
|
161
|
|
|
*/ |
|
162
|
|
|
public function createUser(string $userName, string $userPassword): bool |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->driver->createUser($userName, $userPassword); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Grant user permissions on given database |
|
169
|
|
|
* |
|
170
|
|
|
* @access public |
|
171
|
|
|
* @param string $databaseName The database name |
|
172
|
|
|
* @param string $userName The user name |
|
173
|
|
|
* |
|
174
|
|
|
* @return bool True if the user has been granted, otherwise false |
|
175
|
|
|
*/ |
|
176
|
|
|
public function grantUser(string $databaseName, string $userName): bool |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->driver->grantUser($databaseName, $userName); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Drop a user |
|
183
|
|
|
* |
|
184
|
|
|
* @access public |
|
185
|
|
|
* @param string $userName The user name |
|
186
|
|
|
* @param bool $ifExists Set whether the user must be deleted only when exists. |
|
187
|
|
|
* |
|
188
|
|
|
* @return bool True if the user has been dropped or does not exist when $ifExists |
|
189
|
|
|
* is set to True, otherwise false. |
|
190
|
|
|
*/ |
|
191
|
|
|
public function dropUser(string $userName, bool $ifExists = false): bool |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->driver->dropUser($userName, $ifExists); |
|
194
|
|
|
} |
|
195
|
|
|
} |