1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Database implementation |
5
|
|
|
*/ |
6
|
|
|
namespace Ubiquity\db; |
7
|
|
|
|
8
|
|
|
use Ubiquity\exceptions\CacheException; |
9
|
|
|
use Ubiquity\db\traits\DatabaseOperationsTrait; |
10
|
|
|
use Ubiquity\exceptions\DBException; |
11
|
|
|
use Ubiquity\db\traits\DatabaseTransactionsTrait; |
12
|
|
|
use Ubiquity\controllers\Startup; |
13
|
|
|
use Ubiquity\db\traits\DatabaseMetadatas; |
14
|
|
|
use Ubiquity\cache\database\DbCache; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Ubiquity Generic database class. |
18
|
|
|
* Ubiquity\db$Database |
19
|
|
|
* This class is part of Ubiquity |
20
|
|
|
* |
21
|
|
|
* @author jcheron <[email protected]> |
22
|
|
|
* @version 1.1.4 |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class Database { |
26
|
|
|
use DatabaseOperationsTrait,DatabaseTransactionsTrait,DatabaseMetadatas; |
27
|
|
|
|
28
|
|
|
public static $wrappers = [ 'pdo' => \Ubiquity\db\providers\pdo\PDOWrapper::class,'tarantool' => '\Ubiquity\db\providers\tarantool\TarantoolWrapper','mysqli' => '\Ubiquity\db\providers\mysqli\MysqliWrapper','swoole' => '\Ubiquity\db\providers\swoole\SwooleWrapper' ]; |
29
|
|
|
private $dbType; |
30
|
|
|
private $serverName; |
31
|
|
|
private $port; |
32
|
|
|
private $dbName; |
33
|
|
|
private $user; |
34
|
|
|
private $password; |
35
|
|
|
private $cache; |
36
|
|
|
private $options; |
37
|
|
|
public $quote; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* @var \Ubiquity\db\providers\AbstractDbWrapper |
42
|
|
|
*/ |
43
|
|
|
protected $wrapperObject; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* |
48
|
|
|
* @param string $dbWrapperClass |
49
|
|
|
* @param string $dbName |
50
|
|
|
* @param string $serverName |
51
|
|
|
* @param string $port |
52
|
|
|
* @param string $user |
53
|
|
|
* @param string $password |
54
|
|
|
* @param array $options |
55
|
|
|
* @param boolean|string $cache |
56
|
|
|
* @param mixed $pool |
57
|
|
|
*/ |
58
|
147 |
|
public function __construct($dbWrapperClass, $dbType, $dbName, $serverName = "127.0.0.1", $port = "3306", $user = "root", $password = "", $options = [ ], $cache = false, $pool = null) { |
59
|
147 |
|
$this->setDbWrapperClass ( $dbWrapperClass, $dbType ); |
60
|
147 |
|
$this->dbName = $dbName; |
61
|
147 |
|
$this->serverName = $serverName; |
62
|
147 |
|
$this->port = $port; |
63
|
147 |
|
$this->user = $user; |
64
|
147 |
|
$this->password = $password; |
65
|
147 |
|
$this->options = $options; |
66
|
147 |
|
if ($cache !== false) { |
67
|
18 |
|
if ($cache instanceof \Closure) { |
68
|
1 |
|
$this->cache = $cache (); |
69
|
|
|
} else { |
70
|
18 |
|
if (\class_exists ( $cache )) { |
|
|
|
|
71
|
18 |
|
$this->cache = new $cache (); |
72
|
|
|
} else { |
73
|
1 |
|
throw new CacheException ( $cache . " is not a valid value for database cache" ); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
147 |
|
if ($pool && (\method_exists ( $this->wrapperObject, 'pool' ))) { |
78
|
|
|
$this->wrapperObject->setPool ( $pool ); |
79
|
|
|
} |
80
|
147 |
|
} |
81
|
|
|
|
82
|
147 |
|
private function setDbWrapperClass($dbWrapperClass, $dbType) { |
83
|
147 |
|
$this->wrapperObject = new $dbWrapperClass ( $this->dbType = $dbType ); |
84
|
147 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Creates the Db instance and realize a safe connection. |
88
|
|
|
* |
89
|
|
|
* @throws DBException |
90
|
|
|
* @return boolean |
91
|
|
|
*/ |
92
|
137 |
|
public function connect() { |
93
|
|
|
try { |
94
|
137 |
|
$this->_connect (); |
95
|
137 |
|
$this->quote = $this->wrapperObject->quote; |
96
|
137 |
|
return true; |
97
|
1 |
|
} catch ( \Exception $e ) { |
98
|
1 |
|
throw new DBException ( $e->getMessage (), $e->getCode (), $e->getPrevious () ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function getDSN() { |
103
|
1 |
|
return $this->wrapperObject->getDSN ( $this->serverName, $this->port, $this->dbName, $this->dbType ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
* @codeCoverageIgnore |
110
|
|
|
*/ |
111
|
|
|
public function getServerName() { |
112
|
|
|
return $this->serverName; |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
public function setServerName($serverName) { |
116
|
3 |
|
$this->serverName = $serverName; |
117
|
3 |
|
} |
118
|
|
|
|
119
|
3 |
|
public function setDbType($dbType) { |
120
|
3 |
|
$this->dbType = $dbType; |
121
|
3 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
* @codeCoverageIgnore |
128
|
|
|
*/ |
129
|
|
|
public function getPort() { |
130
|
|
|
return $this->port; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
* @codeCoverageIgnore |
137
|
|
|
*/ |
138
|
|
|
public function getDbName() { |
139
|
|
|
return $this->dbName; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
* @codeCoverageIgnore |
146
|
|
|
*/ |
147
|
|
|
public function getUser() { |
148
|
|
|
return $this->user; |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
public static function getAvailableDrivers($dbWrapperClass = \Ubiquity\db\providers\pdo\PDOWrapper::class) { |
152
|
3 |
|
return \call_user_func ( $dbWrapperClass . '::getAvailableDrivers' ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
* @return mixed |
158
|
|
|
* @codeCoverageIgnore |
159
|
|
|
*/ |
160
|
|
|
public function getDbType() { |
161
|
|
|
return $this->dbType; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
* @codeCoverageIgnore |
168
|
|
|
*/ |
169
|
|
|
public function getPassword() { |
170
|
|
|
return $this->password; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* |
175
|
|
|
* @return array |
176
|
|
|
* @codeCoverageIgnore |
177
|
|
|
*/ |
178
|
|
|
public function getOptions() { |
179
|
|
|
return $this->options; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* |
184
|
|
|
* @param string $port |
185
|
|
|
*/ |
186
|
2 |
|
public function setPort($port) { |
187
|
2 |
|
$this->port = $port; |
188
|
2 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* |
192
|
|
|
* @param string $dbName |
193
|
|
|
*/ |
194
|
1 |
|
public function setDbName($dbName) { |
195
|
1 |
|
$this->dbName = $dbName; |
196
|
1 |
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* |
200
|
|
|
* @param string $user |
201
|
|
|
*/ |
202
|
2 |
|
public function setUser($user) { |
203
|
2 |
|
$this->user = $user; |
204
|
2 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* |
208
|
|
|
* @param string $password |
209
|
|
|
*/ |
210
|
1 |
|
public function setPassword($password) { |
211
|
1 |
|
$this->password = $password; |
212
|
1 |
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* |
216
|
|
|
* @param array $options |
217
|
|
|
*/ |
218
|
1 |
|
public function setOptions($options) { |
219
|
1 |
|
$this->options = $options; |
220
|
1 |
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Closes the active connection |
224
|
|
|
*/ |
225
|
71 |
|
public function close() { |
226
|
71 |
|
$this->wrapperObject->close (); |
227
|
71 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Starts and returns a database instance corresponding to an offset in config |
231
|
|
|
* |
232
|
|
|
* @param string $offset |
233
|
|
|
* @param array $config Ubiquity config file content |
234
|
|
|
* @return \Ubiquity\db\Database|NULL |
235
|
|
|
*/ |
236
|
|
|
public static function start(string $offset = null, ?array $config = null): ?self { |
237
|
|
|
$config ??= Startup::$config; |
238
|
|
|
$db = $offset ? ($config ['database'] [$offset] ?? ($config ['database'] ?? [ ])) : ($config ['database'] ?? [ ]); |
239
|
|
|
if ($db ['dbName'] !== '') { |
240
|
|
|
$database = new Database ( $db ['wrapper'] ?? \Ubiquity\db\providers\pdo\PDOWrapper::class, $db ['type'], $db ['dbName'], $db ['serverName'] ?? '127.0.0.1', $db ['port'] ?? 3306, $db ['user'] ?? 'root', $db ['password'] ?? '', $db ['options'] ?? [ ], $db ['cache'] ?? false); |
241
|
|
|
$database->connect (); |
242
|
|
|
return $database; |
243
|
|
|
} |
244
|
|
|
return null; |
245
|
|
|
} |
246
|
|
|
|
247
|
3 |
|
public function quoteValue($value, $type = 2) { |
248
|
3 |
|
return $this->wrapperObject->quoteValue ( ( string ) $value, $type ); |
249
|
|
|
} |
250
|
|
|
|
251
|
2 |
|
public function getUpdateFieldsKeyAndValues($keyAndValues, $fields) { |
252
|
2 |
|
$ret = array (); |
253
|
2 |
|
foreach ( $fields as $field ) { |
254
|
2 |
|
$ret [] = $this->quote . $field . $this->quote . ' = ' . $this->quoteValue ( $keyAndValues [$field] ); |
255
|
|
|
} |
256
|
2 |
|
return \implode ( ',', $ret ); |
257
|
|
|
} |
258
|
|
|
|
259
|
1 |
|
public function getInsertValues($keyAndValues) { |
260
|
1 |
|
$ret = array (); |
261
|
1 |
|
foreach ( $keyAndValues as $value ) { |
262
|
1 |
|
$ret [] = $this->quoteValue ( $value ); |
263
|
|
|
} |
264
|
1 |
|
return \implode ( ',', $ret ); |
265
|
|
|
} |
266
|
|
|
|
267
|
3 |
|
public function getCondition(array $keyValues, $separator = ' AND ') { |
268
|
3 |
|
$retArray = array (); |
269
|
3 |
|
foreach ( $keyValues as $key => $value ) { |
270
|
3 |
|
$retArray [] = $this->quote . $key . $this->quote . " = " . $this->quoteValue ( $value ); |
271
|
|
|
} |
272
|
3 |
|
return \implode ( $separator, $retArray ); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* For databases with Connection pool (retrieve a new dbInstance from pool wrapper) |
277
|
|
|
*/ |
278
|
|
|
public function pool() { |
279
|
|
|
return $this->wrapperObject->pool (); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* For databases with Connection pool (put a dbInstance in pool wrapper) |
284
|
|
|
*/ |
285
|
|
|
public function freePool($db) { |
286
|
|
|
$this->wrapperObject->freePool ( $db ); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public function setPool($pool) { |
290
|
|
|
$this->wrapperObject->setPool ( $pool ); |
291
|
|
|
} |
292
|
|
|
|
293
|
2 |
|
public static function getAvailableWrappers() { |
294
|
2 |
|
$wrappers = [ ]; |
295
|
2 |
|
foreach ( self::$wrappers as $k => $wrapper ) { |
296
|
2 |
|
if (\class_exists ( $wrapper, true )) { |
297
|
2 |
|
$wrappers [$k] = $wrapper; |
298
|
|
|
} |
299
|
|
|
} |
300
|
2 |
|
return $wrappers; |
301
|
|
|
} |
302
|
|
|
|
303
|
25 |
|
public function getSpecificSQL($key, ?array $params = null) { |
304
|
25 |
|
switch ($key) { |
305
|
25 |
|
case 'groupconcat' : |
306
|
24 |
|
return $this->wrapperObject->groupConcat ( $params [0], $params [1] ?? ','); |
307
|
25 |
|
case 'tostring' : |
308
|
25 |
|
return $this->wrapperObject->toStringOperator (); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function setCacheInstance(DbCache $cache) { |
313
|
|
|
$this->cache = $cache; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function getCacheInstance() { |
317
|
|
|
return $this->cache; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|