|
1
|
|
|
<?php namespace AgelxNash\Modx\Evo\Database\Drivers; |
|
2
|
|
|
|
|
3
|
|
|
use AgelxNash\Modx\Evo\Database\Exceptions; |
|
4
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
|
5
|
|
|
use Illuminate\Database\Connection; |
|
6
|
|
|
use PDOStatement; |
|
7
|
|
|
use Illuminate\Events\Dispatcher; |
|
8
|
|
|
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; |
|
9
|
|
|
use Illuminate\Container\Container; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
use PDO; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @property Connection $conn |
|
15
|
|
|
*/ |
|
16
|
|
|
class IlluminateDriver extends AbstractDriver |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $connection = 'default'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Capsule |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $capsule; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $affectedRows = 0; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $lastError = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $lastErrorNo = ''; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $driver = 'mysql'; |
|
47
|
|
|
|
|
48
|
|
|
private $elapsedTimeMethod; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritDoc} |
|
52
|
|
|
* @throws \ReflectionException |
|
53
|
|
|
*/ |
|
54
|
32 |
|
public function __construct(array $config = [], $connection = 'default') |
|
55
|
|
|
{ |
|
56
|
32 |
|
$reflection = new ReflectionClass(Capsule::class); |
|
57
|
32 |
|
$property = $reflection->getProperty('instance'); |
|
58
|
32 |
|
$property->setAccessible(true); |
|
59
|
|
|
/** |
|
60
|
|
|
* @var Capsule|null $capsule |
|
61
|
|
|
*/ |
|
62
|
32 |
|
$capsule = $property->getValue(Capsule::class); |
|
|
|
|
|
|
63
|
32 |
|
if ($capsule === null) { |
|
64
|
1 |
|
$this->capsule = new Capsule; |
|
65
|
|
|
|
|
66
|
1 |
|
$this->getCapsule()->setAsGlobal(); |
|
67
|
|
|
} else { |
|
68
|
31 |
|
$this->capsule = $capsule; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
32 |
|
if ($this->hasConnectionName($connection)) { |
|
72
|
31 |
|
if (empty($config)) { |
|
73
|
2 |
|
$config = $this->getCapsule()->getConnection($connection)->getConfig(); |
|
74
|
2 |
|
unset($config['name'], $config['driver']); |
|
75
|
|
|
} else { |
|
76
|
29 |
|
$diff = array_diff_assoc( |
|
77
|
29 |
|
array_merge(['driver' => $this->driver], $config), |
|
78
|
29 |
|
$this->getCapsule()->getConnection($connection)->getConfig() |
|
|
|
|
|
|
79
|
|
|
); |
|
80
|
29 |
|
if (array_intersect(['driver', 'host', 'database', 'password', 'username'], array_keys($diff))) { |
|
81
|
2 |
|
throw new Exceptions\ConnectException( |
|
82
|
2 |
|
sprintf('The connection name "%s" is already used', $connection) |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
31 |
|
$this->connection = $connection; |
|
89
|
|
|
|
|
90
|
31 |
|
$this->useEloquent(); |
|
91
|
|
|
|
|
92
|
31 |
|
$this->setConfig($config); |
|
93
|
31 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the elapsed time since a given starting point. |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $start |
|
99
|
|
|
* @return float |
|
100
|
|
|
*/ |
|
101
|
28 |
|
protected function getElapsedTime($start) |
|
102
|
|
|
{ |
|
103
|
28 |
|
if ($this->elapsedTimeMethod === null) { |
|
104
|
28 |
|
$reflection = new ReflectionClass($this->getConnect()); |
|
105
|
28 |
|
$this->elapsedTimeMethod = $reflection->getMethod('getElapsedTime'); |
|
106
|
28 |
|
$this->elapsedTimeMethod->setAccessible(true); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
28 |
|
return $this->elapsedTimeMethod->invoke($this->getConnect(), $start); |
|
110
|
|
|
} |
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritDoc} |
|
113
|
|
|
* @return Connection |
|
114
|
|
|
*/ |
|
115
|
31 |
|
public function getConnect() |
|
116
|
|
|
{ |
|
117
|
31 |
|
if (! $this->isConnected()) { |
|
118
|
31 |
|
$this->connect(); |
|
119
|
31 |
|
if (! $this->conn->getPdo() instanceof PDO) { |
|
|
|
|
|
|
120
|
2 |
|
$this->conn->reconnect(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
31 |
|
return $this->conn; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
31 |
|
public function isConnected() |
|
130
|
|
|
{ |
|
131
|
31 |
|
return ($this->conn instanceof Connection && $this->conn->getDatabaseName()); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritDoc} |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function getLastError() |
|
138
|
|
|
{ |
|
139
|
1 |
|
$error = $this->getConnect()->getPdo()->errorInfo(); |
|
140
|
1 |
|
return empty($error[2]) ? (empty($this->lastError[2]) ? '' : $this->lastError[2]) : $error[2]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritDoc} |
|
145
|
|
|
*/ |
|
146
|
3 |
|
public function getLastErrorNo() |
|
147
|
|
|
{ |
|
148
|
3 |
|
$error = $this->getConnect()->getPdo()->errorInfo(); |
|
149
|
3 |
|
$out = empty($error[0]) || $error[0] === '00000' ? $this->lastErrorNo : $error[0]; |
|
150
|
|
|
|
|
151
|
3 |
|
return $out; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritDoc} |
|
156
|
|
|
*/ |
|
157
|
31 |
|
public function connect() |
|
158
|
|
|
{ |
|
159
|
|
|
try { |
|
160
|
31 |
|
if (! $this->hasConnectionName($this->connection)) { |
|
161
|
1 |
|
$this->getCapsule()->addConnection([ |
|
162
|
1 |
|
'driver' => $this->driver, |
|
163
|
1 |
|
'host' => $this->getConfig('host'), |
|
164
|
1 |
|
'database' => $this->getConfig('database'), |
|
165
|
1 |
|
'username' => $this->getConfig('username'), |
|
166
|
1 |
|
'password' => $this->getConfig('password'), |
|
167
|
1 |
|
'charset' => $this->getConfig('charset'), |
|
168
|
1 |
|
'collation' => $this->getConfig('collation'), |
|
169
|
1 |
|
'prefix' => $this->getConfig('prefix'), |
|
170
|
1 |
|
], $this->connection); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
31 |
|
$this->conn = $this->getCapsule()->getConnection($this->connection); |
|
174
|
|
|
} catch (\Exception $exception) { |
|
175
|
|
|
$this->conn = null; |
|
176
|
|
|
throw new Exceptions\ConnectException($exception->getMessage(), $exception->getCode()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
31 |
|
return $this->conn; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* {@inheritDoc} |
|
184
|
|
|
*/ |
|
185
|
2 |
|
public function disconnect() |
|
186
|
|
|
{ |
|
187
|
2 |
|
if ($this->isConnected()) { |
|
188
|
2 |
|
$this->conn->disconnect(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
2 |
|
$this->conn = null; |
|
192
|
2 |
|
$this->lastErrorNo = ''; |
|
193
|
2 |
|
$this->lastError = []; |
|
194
|
|
|
|
|
195
|
2 |
|
return true; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* {@inheritDoc} |
|
200
|
|
|
*/ |
|
201
|
28 |
|
public function isResult($result) |
|
202
|
|
|
{ |
|
203
|
28 |
|
return $result instanceof PDOStatement; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param PDOStatement $result |
|
208
|
|
|
* {@inheritDoc} |
|
209
|
|
|
*/ |
|
210
|
1 |
|
public function numFields($result) |
|
211
|
|
|
{ |
|
212
|
1 |
|
return $this->isResult($result) ? $result->columnCount() : 0; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param PDOStatement $result |
|
217
|
|
|
* {@inheritDoc} |
|
218
|
|
|
*/ |
|
219
|
1 |
|
public function fieldName($result, $col = 0) |
|
220
|
|
|
{ |
|
221
|
1 |
|
$field = $this->isResult($result) ? $result->getColumnMeta($col) : []; |
|
222
|
1 |
|
return isset($field['name']) ? $field['name'] : null; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* {@inheritDoc} |
|
227
|
|
|
*/ |
|
228
|
28 |
|
public function setCharset($charset, $method = null) |
|
229
|
|
|
{ |
|
230
|
28 |
|
if ($method === null) { |
|
231
|
1 |
|
$method = $this->getConfig('method'); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
28 |
|
$query = $method . ' ' . $charset; |
|
235
|
|
|
|
|
236
|
28 |
|
return (bool)$this->query($query); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* {@inheritDoc} |
|
241
|
|
|
*/ |
|
242
|
1 |
|
public function selectDb($name) |
|
243
|
|
|
{ |
|
244
|
1 |
|
return $this->getConnect()->getPdo()->exec('USE ' . $name) === 0; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* {@inheritDoc} |
|
249
|
|
|
*/ |
|
250
|
1 |
|
public function escape($data) |
|
251
|
|
|
{ |
|
252
|
|
|
/** |
|
253
|
|
|
* It's not secure |
|
254
|
|
|
* But need for backward compatibility |
|
255
|
|
|
*/ |
|
256
|
|
|
|
|
257
|
1 |
|
$quote = $this->getConnect()->getPdo()->quote($data); |
|
258
|
1 |
|
return strpos($quote, '\'') === 0 ? mb_substr($quote, 1, -1) : $quote; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* {@inheritDoc} |
|
263
|
|
|
* @return bool|PDOStatement |
|
264
|
|
|
*/ |
|
265
|
28 |
|
public function query($sql) |
|
266
|
|
|
{ |
|
267
|
|
|
try { |
|
268
|
28 |
|
$start = microtime(true); |
|
269
|
|
|
|
|
270
|
28 |
|
$result = $this->prepare($sql); |
|
271
|
28 |
|
$this->execute($result); |
|
272
|
|
|
|
|
273
|
28 |
|
if ($this->saveAffectedRows($result) === 0 && $this->isResult($result) && ! $this->isSelectQuery($sql)) { |
|
274
|
28 |
|
$result = true; |
|
275
|
|
|
} |
|
276
|
28 |
|
$this->getConnect()->logQuery($sql, [], $this->getElapsedTime($start)); |
|
277
|
3 |
|
} catch (\Exception $exception) { |
|
278
|
3 |
|
$this->lastError = $this->isResult($result) ? $result->errorInfo() : []; |
|
|
|
|
|
|
279
|
2 |
|
$code = $this->isResult($result) ? $result->errorCode() : ''; |
|
280
|
2 |
|
$this->lastErrorNo = $this->isResult($result) ? (empty($code) ? $exception->getCode() : $code) : ''; |
|
281
|
2 |
|
throw (new Exceptions\QueryException($exception->getMessage(), $exception->getCode())) |
|
282
|
2 |
|
->setQuery($sql); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
28 |
|
return $result; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param PDOStatement $result |
|
290
|
|
|
* {@inheritDoc} |
|
291
|
|
|
*/ |
|
292
|
16 |
|
public function getRecordCount($result) |
|
293
|
|
|
{ |
|
294
|
16 |
|
return $this->isResult($result) ? $result->rowCount() : 0; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @param PDOStatement $result |
|
299
|
|
|
* {@inheritDoc} |
|
300
|
|
|
*/ |
|
301
|
14 |
|
public function getRow($result, $mode = 'assoc') |
|
302
|
|
|
{ |
|
303
|
|
|
switch ($mode) { |
|
304
|
14 |
|
case 'assoc': |
|
305
|
6 |
|
$out = $result->fetch(\PDO::FETCH_ASSOC); |
|
306
|
6 |
|
break; |
|
307
|
10 |
|
case 'num': |
|
308
|
7 |
|
$out = $result->fetch(\PDO::FETCH_NUM); |
|
309
|
7 |
|
break; |
|
310
|
3 |
|
case 'object': |
|
311
|
1 |
|
$out = $result->fetchObject(); |
|
312
|
1 |
|
break; |
|
313
|
2 |
|
case 'both': |
|
314
|
1 |
|
$out = $result->fetch(\PDO::FETCH_BOTH); |
|
315
|
1 |
|
break; |
|
316
|
|
|
default: |
|
317
|
1 |
|
throw new Exceptions\UnknownFetchTypeException( |
|
318
|
1 |
|
"Unknown get type ($mode) specified for fetchRow - must be empty, 'assoc', 'num', 'object' or 'both'." |
|
319
|
|
|
); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
14 |
|
return $out; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* {@inheritDoc} |
|
327
|
|
|
*/ |
|
328
|
4 |
|
public function getVersion() |
|
329
|
|
|
{ |
|
330
|
4 |
|
return $this->getConnect()->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* {@inheritDoc} |
|
335
|
|
|
*/ |
|
336
|
7 |
|
public function getInsertId() |
|
337
|
|
|
{ |
|
338
|
7 |
|
return $this->getConnect()->getPdo()->lastInsertId(); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* @return int |
|
343
|
|
|
*/ |
|
344
|
28 |
|
public function getAffectedRows() |
|
345
|
|
|
{ |
|
346
|
28 |
|
return $this->affectedRows; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* @param PDOStatement|bool $result |
|
351
|
|
|
* @return int |
|
352
|
|
|
*/ |
|
353
|
28 |
|
protected function saveAffectedRows($result) |
|
354
|
|
|
{ |
|
355
|
28 |
|
$this->affectedRows = \is_bool($result) ? 0 : $result->rowCount(); |
|
356
|
28 |
|
return $this->getAffectedRows(); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* @param string $sql |
|
361
|
|
|
* @return PDOStatement|bool |
|
362
|
|
|
* @throws Exceptions\ConnectException |
|
363
|
|
|
*/ |
|
364
|
28 |
|
public function prepare($sql) |
|
365
|
|
|
{ |
|
366
|
28 |
|
$pdo = $this->getConnect()->getPdo(); |
|
367
|
28 |
|
$result = $pdo->prepare( |
|
368
|
28 |
|
$sql, |
|
369
|
|
|
[ |
|
370
|
28 |
|
\PDO::ATTR_CURSOR => \PDO::CURSOR_SCROLL, |
|
371
|
|
|
|
|
372
|
|
|
] |
|
373
|
|
|
); |
|
374
|
|
|
|
|
375
|
28 |
|
if ($this->isResult($result)) { |
|
376
|
28 |
|
$result->setFetchMode(\PDO::FETCH_ASSOC); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
28 |
|
return $result; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* @param PDOStatement|bool $result |
|
384
|
|
|
* @return bool |
|
385
|
|
|
*/ |
|
386
|
28 |
|
public function execute($result) |
|
387
|
|
|
{ |
|
388
|
28 |
|
return $this->isResult($result) ? $result->execute() : (bool)$result; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* @param DispatcherContract|null $dispatcher |
|
393
|
|
|
* @return bool |
|
394
|
|
|
*/ |
|
395
|
31 |
|
public function useEloquent(DispatcherContract $dispatcher = null) |
|
396
|
|
|
{ |
|
397
|
31 |
|
$out = false; |
|
398
|
31 |
|
if ($dispatcher === null) { |
|
399
|
31 |
|
$dispatcher = $this->getCapsule()->getEventDispatcher(); |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
31 |
|
if ($dispatcher === null && class_exists(Dispatcher::class)) { |
|
403
|
4 |
|
$dispatcher = new Dispatcher(new Container); |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
31 |
|
if ($dispatcher !== null) { |
|
407
|
31 |
|
$this->getCapsule()->setEventDispatcher($dispatcher); |
|
408
|
|
|
|
|
409
|
31 |
|
$out = true; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
31 |
|
$this->getCapsule()->bootEloquent(); |
|
413
|
|
|
|
|
414
|
31 |
|
return $out; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* @return Capsule |
|
419
|
|
|
*/ |
|
420
|
32 |
|
public function getCapsule() |
|
421
|
|
|
{ |
|
422
|
32 |
|
return $this->capsule; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* @param $name |
|
427
|
|
|
* @return bool |
|
428
|
|
|
*/ |
|
429
|
32 |
|
public function hasConnectionName($name) |
|
430
|
|
|
{ |
|
431
|
32 |
|
$connections = $this->getCapsule()->getDatabaseManager()->getConnections(); |
|
432
|
32 |
|
return isset($connections[$name]); |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* @param string $query |
|
437
|
|
|
* @return bool |
|
438
|
|
|
*/ |
|
439
|
28 |
|
protected function isSelectQuery($query) |
|
440
|
|
|
{ |
|
441
|
28 |
|
return 0 === mb_stripos(trim($query), 'SELECT'); |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
/** |
|
445
|
|
|
* {@inheritDoc} |
|
446
|
|
|
*/ |
|
447
|
|
|
public function begin ($flag = 0, $name = null) |
|
448
|
|
|
{ |
|
449
|
|
|
return $this->getConnect()->getPdo()->beginTransaction(); |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
/** |
|
453
|
|
|
* {@inheritDoc} |
|
454
|
|
|
*/ |
|
455
|
|
|
public function commit ($flag = 0, $name = null) |
|
456
|
|
|
{ |
|
457
|
|
|
return $this->getConnect()->getPdo()->commit(); |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* {@inheritDoc} |
|
462
|
|
|
*/ |
|
463
|
|
|
public function rollback ($flag = 0, $name = null) |
|
464
|
|
|
{ |
|
465
|
|
|
return $this->getConnect()->getPdo()->rollback(); |
|
466
|
|
|
} |
|
467
|
|
|
} |
|
468
|
|
|
|