1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\Logging\SQLLogger; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class is caches queries so that they can be run as quickly as possible on subsequent builds |
11
|
|
|
* |
12
|
|
|
* @see \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelper::createDb |
13
|
|
|
*/ |
14
|
|
|
class QueryCachingLogger implements SQLLogger |
15
|
|
|
{ |
16
|
|
|
private $queries = []; |
17
|
|
|
|
18
|
|
|
public function startQuery($sql, array $params = null, array $types = null) |
19
|
|
|
{ |
20
|
|
|
$this->queries[$sql][] = [$params, $types]; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function stopQuery() |
24
|
|
|
{ |
25
|
|
|
return; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function run(Connection $connection): void |
29
|
|
|
{ |
30
|
|
|
foreach ($this->queries as $query => $paramsArray) { |
31
|
|
|
if ('"START TRANSACTION"' === $query) { |
32
|
|
|
$connection->beginTransaction(); |
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
if ('"COMMIT"' === $query) { |
36
|
|
|
$connection->commit(); |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
if ([[[], []]] === $paramsArray) { |
40
|
|
|
$connection->prepare($query)->execute(); |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
$stmt = $connection->prepare($query); |
44
|
|
|
foreach ($paramsArray as $paramsTypes) { |
45
|
|
|
try { |
46
|
|
|
list($params, $types) = $paramsTypes; |
47
|
|
|
$colNum = 1; |
48
|
|
|
foreach ($params as $key => $value) { |
49
|
|
|
$stmt->bindValue($colNum++, $value, $types[$key]); |
50
|
|
|
} |
51
|
|
|
$stmt->execute(); |
52
|
|
|
} catch (\Exception $e) { |
53
|
|
|
if ($connection->isTransactionActive()) { |
54
|
|
|
$connection->rollBack(); |
55
|
|
|
} |
56
|
|
|
throw new DoctrineStaticMetaException( |
57
|
|
|
'Failed running logged query ' . $query . 'with params and types: ' |
58
|
|
|
. print_r($paramsTypes, true), |
59
|
|
|
$e->getCode(), |
60
|
|
|
$e |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|