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