1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Connection; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\Event\ConnectionClosedListenerInterface; |
6
|
|
|
use Bdf\Prime\Connection\Extensions\LostConnection; |
7
|
|
|
use Bdf\Prime\Connection\Extensions\SchemaChanged; |
8
|
|
|
use Bdf\Prime\Connection\Result\PdoResultSet; |
9
|
|
|
use Bdf\Prime\Connection\Result\ResultSetInterface; |
|
|
|
|
10
|
|
|
use Bdf\Prime\Connection\Result\UpdateResultSet; |
11
|
|
|
use Bdf\Prime\Platform\Sql\SqlPlatform; |
12
|
|
|
use Bdf\Prime\Query\Compiler\Preprocessor\PreprocessorInterface; |
13
|
|
|
use Bdf\Prime\Query\Compiler\SqlCompiler; |
14
|
|
|
use Bdf\Prime\Query\Contract\Compilable; |
15
|
|
|
use Bdf\Prime\Query\Contract\Query\InsertQueryInterface; |
16
|
|
|
use Bdf\Prime\Query\Contract\Query\KeyValueQueryInterface; |
17
|
|
|
use Bdf\Prime\Query\Custom\BulkInsert\BulkInsertQuery; |
18
|
|
|
use Bdf\Prime\Query\Custom\BulkInsert\BulkInsertSqlCompiler; |
19
|
|
|
use Bdf\Prime\Query\Custom\KeyValue\KeyValueQuery; |
20
|
|
|
use Bdf\Prime\Query\Custom\KeyValue\KeyValueSqlCompiler; |
21
|
|
|
use Bdf\Prime\Query\Factory\DefaultQueryFactory; |
22
|
|
|
use Bdf\Prime\Query\Factory\QueryFactoryInterface; |
|
|
|
|
23
|
|
|
use Bdf\Prime\Query\Query; |
24
|
|
|
use Bdf\Prime\Schema\SchemaManager; |
25
|
|
|
use Doctrine\Common\EventManager; |
26
|
|
|
use Doctrine\DBAL\Cache\QueryCacheProfile; |
27
|
|
|
use Doctrine\DBAL\Configuration; |
28
|
|
|
use Doctrine\DBAL\Connection as BaseConnection; |
29
|
|
|
use Doctrine\DBAL\DBALException; |
30
|
|
|
use Doctrine\DBAL\Driver; |
31
|
|
|
use Doctrine\DBAL\Statement; |
32
|
|
|
use PDO; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Connection |
36
|
|
|
* |
37
|
|
|
* @package Bdf\Prime |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
class SimpleConnection extends BaseConnection implements ConnectionInterface |
40
|
|
|
{ |
41
|
|
|
use LostConnection; |
42
|
|
|
use SchemaChanged; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The connection name. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $name; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The schema manager. |
53
|
|
|
* |
54
|
|
|
* @var SchemaManager |
55
|
|
|
*/ |
56
|
|
|
private $schema; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var SqlPlatform |
60
|
|
|
*/ |
61
|
|
|
private $platform; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var QueryFactoryInterface |
65
|
|
|
*/ |
66
|
|
|
private $factory; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* SimpleConnection constructor. |
70
|
|
|
* |
71
|
|
|
* @param array $params |
|
|
|
|
72
|
|
|
* @param Driver $driver |
|
|
|
|
73
|
|
|
* @param Configuration|null $config |
|
|
|
|
74
|
|
|
* @param EventManager|null $eventManager |
|
|
|
|
75
|
|
|
* @throws DBALException |
|
|
|
|
76
|
|
|
*/ |
77
|
148 |
|
public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null) |
78
|
|
|
{ |
79
|
148 |
|
parent::__construct($params, $driver, $config, $eventManager); |
80
|
|
|
|
81
|
148 |
|
$this->factory = new DefaultQueryFactory( |
82
|
148 |
|
$this, |
83
|
148 |
|
new SqlCompiler($this), |
84
|
|
|
[ |
85
|
148 |
|
KeyValueQuery::class => KeyValueSqlCompiler::class, |
86
|
|
|
BulkInsertQuery::class => BulkInsertSqlCompiler::class, |
87
|
|
|
], |
88
|
|
|
[ |
89
|
148 |
|
KeyValueQueryInterface::class => KeyValueQuery::class, |
90
|
|
|
InsertQueryInterface::class => BulkInsertQuery::class, |
91
|
|
|
] |
92
|
|
|
); |
93
|
148 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
|
|
|
|
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
149 |
|
public function setName($name) |
99
|
|
|
{ |
100
|
149 |
|
$this->name = $name; |
101
|
|
|
|
102
|
149 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
29 |
|
public function getName() |
109
|
|
|
{ |
110
|
29 |
|
return $this->name; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
1 |
|
public function isConnected() |
117
|
|
|
{ |
118
|
1 |
|
return $this->_conn !== null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
792 |
|
public function schema() |
125
|
|
|
{ |
126
|
792 |
|
if ($this->schema === null) { |
127
|
126 |
|
$this->schema = new SchemaManager($this); |
128
|
|
|
} |
129
|
|
|
|
130
|
792 |
|
return $this->schema; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
1011 |
|
public function platform() |
137
|
|
|
{ |
138
|
1011 |
|
if ($this->platform === null) { |
139
|
130 |
|
$this->platform = new SqlPlatform( |
140
|
130 |
|
$this->getDatabasePlatform(), |
141
|
130 |
|
$this->getConfiguration()->getTypes() |
|
|
|
|
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
1011 |
|
return $this->platform; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
|
|
|
|
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
3 |
|
public function fromDatabase($value, $type, array $fieldOptions = []) |
152
|
|
|
{ |
153
|
3 |
|
return $this->platform()->types()->fromDatabase($value, $type, $fieldOptions); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
|
|
|
|
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
4 |
|
public function toDatabase($value, $type = null) |
160
|
|
|
{ |
161
|
4 |
|
return $this->platform()->types()->toDatabase($value, $type); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
|
|
|
|
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
652 |
|
public function builder(PreprocessorInterface $preprocessor = null) |
168
|
|
|
{ |
169
|
652 |
|
return $this->factory->make(Query::class, $preprocessor); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
|
|
|
|
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
177 |
|
public function make($query, PreprocessorInterface $preprocessor = null) |
176
|
|
|
{ |
177
|
177 |
|
return $this->factory->make($query, $preprocessor); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
805 |
|
public function factory() |
184
|
|
|
{ |
185
|
805 |
|
return $this->factory; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
|
|
|
|
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
497 |
|
public function from($table) |
192
|
|
|
{ |
193
|
497 |
|
return $this->builder()->from($table); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
|
|
|
|
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
13 |
|
public function delete($table, array $identifier, array $types = array()) |
|
|
|
|
200
|
|
|
{ |
201
|
13 |
|
return $this->from($table)->where($identifier)->delete(); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
|
|
|
|
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
1 |
|
public function update($table, array $data, array $identifier, array $types = array()) |
|
|
|
|
208
|
|
|
{ |
209
|
1 |
|
return $this->from($table)->where($identifier)->update($data, $types); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
|
|
|
|
213
|
|
|
* {@inheritdoc} |
214
|
|
|
*/ |
215
|
403 |
|
public function insert($table, array $data, array $types = array()) |
|
|
|
|
216
|
|
|
{ |
217
|
403 |
|
return $this->from($table)->insert($data, $types); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Executes select query and returns array of object |
222
|
|
|
* |
223
|
|
|
* @param string $query |
224
|
|
|
* @param array $bindings |
225
|
|
|
* @param array $types |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
4 |
|
public function select($query, array $bindings = [], array $types = []) |
230
|
|
|
{ |
231
|
4 |
|
$stmt = $this->executeQuery($query, $bindings, $types); |
232
|
4 |
|
$stmt->setFetchMode(PDO::FETCH_OBJ); |
233
|
|
|
|
234
|
4 |
|
$result = $stmt->fetchAll(); |
235
|
|
|
|
236
|
4 |
|
$stmt->closeCursor(); |
237
|
|
|
|
238
|
4 |
|
return $result; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
|
|
|
|
242
|
|
|
* {@inheritdoc} |
243
|
|
|
*/ |
244
|
810 |
|
public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null) |
245
|
|
|
{ |
246
|
810 |
|
$this->prepareLogger(); |
247
|
|
|
|
248
|
|
|
return $this->runOrReconnect(function() use ($query, $params, $types, $qcp) { |
|
|
|
|
249
|
810 |
|
return parent::executeQuery($query, $params, $types, $qcp); |
250
|
810 |
|
}); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
|
|
|
|
254
|
|
|
* {@inheritdoc} |
255
|
|
|
*/ |
256
|
455 |
|
public function executeUpdate($query, array $params = [], array $types = []) |
257
|
|
|
{ |
258
|
455 |
|
$this->prepareLogger(); |
259
|
|
|
|
260
|
|
|
return $this->runOrReconnect(function() use ($query, $params, $types) { |
|
|
|
|
261
|
455 |
|
return parent::executeUpdate($query, $params, $types); |
262
|
455 |
|
}); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* {@inheritdoc} |
267
|
|
|
*/ |
268
|
6 |
|
public function query() |
269
|
|
|
{ |
270
|
6 |
|
$this->prepareLogger(); |
271
|
|
|
|
272
|
6 |
|
$args = func_get_args(); |
273
|
|
|
|
274
|
|
|
return $this->runOrReconnect(function() use ($args) { |
|
|
|
|
275
|
6 |
|
return parent::query(...$args); |
276
|
6 |
|
}); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
|
|
|
|
280
|
|
|
* {@inheritdoc} |
281
|
|
|
*/ |
282
|
791 |
|
public function exec($statement) |
283
|
|
|
{ |
284
|
791 |
|
$this->prepareLogger(); |
285
|
|
|
|
286
|
|
|
return $this->runOrReconnect(function() use ($statement) { |
|
|
|
|
287
|
791 |
|
return parent::exec($statement); |
288
|
791 |
|
}); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
|
|
|
|
292
|
|
|
* {@inheritdoc} |
293
|
|
|
*/ |
294
|
490 |
|
public function prepare($statement) |
295
|
|
|
{ |
296
|
|
|
return $this->runOrReconnect(function() use ($statement) { |
|
|
|
|
297
|
490 |
|
return parent::prepare($statement); |
298
|
490 |
|
}); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
|
|
|
|
302
|
|
|
* {@inheritdoc} |
303
|
|
|
*/ |
304
|
607 |
|
public function execute(Compilable $query) |
305
|
|
|
{ |
306
|
607 |
|
$statement = $query->compile(); |
307
|
|
|
|
308
|
604 |
|
if ($statement instanceof Statement) { |
309
|
467 |
|
return $this->executePrepared($statement, $query); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
// $statement is a SQL query |
313
|
551 |
|
if ($query->type() === Compilable::TYPE_SELECT) { |
314
|
496 |
|
$stmt = $this->executeQuery($statement, $query->getBindings()); |
315
|
|
|
|
316
|
496 |
|
return new PdoResultSet($stmt); |
317
|
|
|
} |
318
|
|
|
|
319
|
452 |
|
return new UpdateResultSet($this->executeUpdate($statement, $query->getBindings())); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Execute a prepared statement |
324
|
|
|
* |
325
|
|
|
* @param Statement $statement |
326
|
|
|
* @param Compilable $query |
327
|
|
|
* |
328
|
|
|
* @return ResultSetInterface The query result |
329
|
|
|
* @throws DBALException |
|
|
|
|
330
|
|
|
*/ |
331
|
467 |
|
protected function executePrepared(Statement $statement, Compilable $query) |
332
|
|
|
{ |
333
|
467 |
|
$bindings = $query->getBindings(); |
334
|
|
|
|
335
|
467 |
|
$this->prepareLogger(); |
336
|
|
|
|
337
|
|
|
try { |
338
|
467 |
|
$statement->execute($bindings); |
339
|
331 |
|
} catch (DBALException $exception) { |
340
|
|
|
// Prepared query on SQLite for PHP < 7.2 invalidates the query when schema change |
341
|
|
|
// This process may be removed on PHP 7.2 |
342
|
331 |
|
if ($this->causedBySchemaChange($exception)) { |
343
|
326 |
|
$statement = $query->compile(true); |
344
|
326 |
|
$statement->execute($query->getBindings()); |
345
|
6 |
|
} elseif ($this->causedByLostConnection($exception->getPrevious())) { // If the connection is lost, the query must be recompiled |
|
|
|
|
346
|
|
|
$this->close(); |
347
|
|
|
$this->connect(); |
348
|
|
|
|
349
|
|
|
$statement = $query->compile(true); |
350
|
|
|
$statement->execute($query->getBindings()); |
351
|
|
|
} else { |
352
|
6 |
|
throw $exception; |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
465 |
|
return new PdoResultSet($statement); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* {@inheritdoc} |
361
|
|
|
*/ |
362
|
818 |
|
public function beginTransaction() |
363
|
|
|
{ |
364
|
818 |
|
$this->prepareLogger(); |
365
|
|
|
|
366
|
818 |
|
parent::beginTransaction(); |
367
|
818 |
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* {@inheritdoc} |
371
|
|
|
*/ |
372
|
28 |
|
public function commit() |
373
|
|
|
{ |
374
|
28 |
|
$this->prepareLogger(); |
375
|
|
|
|
376
|
28 |
|
parent::commit(); |
377
|
28 |
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* {@inheritdoc} |
381
|
|
|
*/ |
382
|
794 |
|
public function rollBack() |
383
|
|
|
{ |
384
|
794 |
|
$this->prepareLogger(); |
385
|
|
|
|
386
|
794 |
|
return parent::rollBack(); |
|
|
|
|
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* {@inheritdoc} |
391
|
|
|
*/ |
392
|
15 |
|
public function close() |
393
|
|
|
{ |
394
|
15 |
|
parent::close(); |
395
|
|
|
|
396
|
15 |
|
$this->_eventManager->dispatchEvent(ConnectionClosedListenerInterface::EVENT_NAME); |
397
|
15 |
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Log a query |
401
|
|
|
* |
402
|
|
|
* @param string $query |
|
|
|
|
403
|
|
|
* @param int $seconds |
|
|
|
|
404
|
|
|
* @param array $bindings |
|
|
|
|
405
|
|
|
* @param array $types |
|
|
|
|
406
|
|
|
*/ |
407
|
946 |
|
protected function prepareLogger() |
408
|
|
|
{ |
409
|
946 |
|
$logger = $this->getConfiguration()->getSQLLogger(); |
410
|
|
|
|
411
|
946 |
|
if ($logger && $logger instanceof ConnectionAwareInterface) { |
412
|
|
|
$logger->setConnection($this); |
413
|
|
|
} |
414
|
946 |
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Execute a query. Try to reconnect if needed |
418
|
|
|
* |
419
|
|
|
* @param \Closure $callback |
420
|
|
|
* |
421
|
|
|
* @return mixed The query result |
422
|
|
|
* |
423
|
|
|
* @throws DBALException |
|
|
|
|
424
|
|
|
*/ |
425
|
830 |
|
protected function runOrReconnect($callback) |
|
|
|
|
426
|
|
|
{ |
427
|
|
|
try { |
428
|
830 |
|
return $callback(); |
429
|
32 |
|
} catch (DBALException $exception) { |
430
|
32 |
|
if ($this->causedByLostConnection($exception->getPrevious())) { |
431
|
|
|
// Should check for active transaction. |
432
|
|
|
// Only reconnect the start transaction. |
433
|
|
|
// Should raise exception during transaction. |
434
|
2 |
|
$this->close(); |
435
|
2 |
|
$this->connect(); |
436
|
|
|
|
437
|
2 |
|
return $callback(); |
438
|
|
|
} |
439
|
|
|
|
440
|
30 |
|
throw $exception; |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
|