|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Driver\Statement; |
|
7
|
|
|
use Doctrine\DBAL\Logging\SQLLogger; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Factories\UuidFactory; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
11
|
|
|
use Ramsey\Uuid\UuidInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* This class is caches queries so that they can be run as quickly as possible on subsequent builds |
|
15
|
|
|
* |
|
16
|
|
|
* @see \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelper::createDb |
|
17
|
|
|
*/ |
|
18
|
|
|
class QueryCachingLogger implements SQLLogger |
|
19
|
|
|
{ |
|
20
|
|
|
private $queries = []; |
|
21
|
|
|
|
|
22
|
|
|
public function startQuery($sql, array $params = null, array $types = null) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->queries[] = [ |
|
25
|
|
|
'sql' => $sql, |
|
26
|
|
|
'params' => $params, |
|
27
|
|
|
'types' => $types, |
|
28
|
|
|
]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function __sleep(): array |
|
32
|
|
|
{ |
|
33
|
|
|
foreach ($this->queries as $id => &$query) { |
|
34
|
|
|
$this->serialiseUuids($query['params']); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return ['queries']; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Ramsey/UUIDs needs to be converted to string in order to be able to deserialised properly |
|
42
|
|
|
* |
|
43
|
|
|
* @param array|null $params |
|
44
|
|
|
*/ |
|
45
|
|
|
private function serialiseUuids(array &$params = null): void |
|
46
|
|
|
{ |
|
47
|
|
|
if (null === $params) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
foreach ($params as &$param) { |
|
51
|
|
|
if ($param instanceof UuidInterface) { |
|
52
|
|
|
$param = $param->toString(); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function __wakeup() |
|
58
|
|
|
{ |
|
59
|
|
|
$factory = new UuidFactory(new \Ramsey\Uuid\UuidFactory()); |
|
60
|
|
|
foreach ($this->queries as &$query) { |
|
61
|
|
|
$this->unserialiseUuids($query, $factory); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function unserialiseUuids(array &$query, UuidFactory $factory): void |
|
66
|
|
|
{ |
|
67
|
|
|
if (null === $query['params']) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
foreach ($query['params'] as $key => &$param) { |
|
71
|
|
|
try { |
|
72
|
|
|
if (null === $param) { |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
switch ($query['types'][$key]) { |
|
76
|
|
|
case MappingHelper::TYPE_UUID: |
|
77
|
|
|
$param = $factory->getOrderedTimeFactory()->fromString($param); |
|
78
|
|
|
continue 2; |
|
79
|
|
|
case MappingHelper::TYPE_NON_ORDERED_BINARY_UUID: |
|
80
|
|
|
case MappingHelper::TYPE_NON_BINARY_UUID: |
|
81
|
|
|
$param = $factory->getUuidFactory()->fromString($param); |
|
82
|
|
|
continue 2; |
|
83
|
|
|
default: |
|
84
|
|
|
continue 2; |
|
85
|
|
|
} |
|
86
|
|
|
} catch (\Exception $e) { |
|
87
|
|
|
throw new \RuntimeException( |
|
88
|
|
|
'Failed deserialising UUID param key ' . $key . ', ' . $param |
|
89
|
|
|
. "\n" . print_r($query, true), |
|
90
|
|
|
$e->getCode(), |
|
91
|
|
|
$e |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function stopQuery() |
|
98
|
|
|
{ |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function run(Connection $connection): void |
|
103
|
|
|
{ |
|
104
|
|
|
|
|
105
|
|
|
foreach ($this->queries as $id => $query) { |
|
106
|
|
|
if ('"START TRANSACTION"' === $query['sql']) { |
|
107
|
|
|
$connection->beginTransaction(); |
|
108
|
|
|
continue; |
|
109
|
|
|
} |
|
110
|
|
|
if ('"COMMIT"' === $query['sql']) { |
|
111
|
|
|
$connection->commit(); |
|
112
|
|
|
continue; |
|
113
|
|
|
} |
|
114
|
|
|
if ($connection->getDatabasePlatform()->getDummySelectSQL() === $query['sql']) { |
|
115
|
|
|
//this is a ping query |
|
116
|
|
|
unset($this->queries[$id]); |
|
117
|
|
|
continue; |
|
118
|
|
|
} |
|
119
|
|
|
if ([] === $query['params']) { |
|
120
|
|
|
$connection->prepare($query)->execute(); |
|
121
|
|
|
continue; |
|
122
|
|
|
} |
|
123
|
|
|
$stmt = $connection->prepare($query['sql']); |
|
124
|
|
|
$this->runQuery($query, $stmt, $connection, $query); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function runQuery(array $query, Statement $stmt, Connection $connection): void |
|
129
|
|
|
{ |
|
130
|
|
|
try { |
|
131
|
|
|
if ($query['params'] !== null) { |
|
132
|
|
|
$colNum = 1; |
|
133
|
|
|
foreach ($query['params'] as $key => $value) { |
|
134
|
|
|
$stmt->bindValue($colNum++, $value, $query['types'][$key]); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
$stmt->execute(); |
|
138
|
|
|
} catch (\Exception $e) { |
|
139
|
|
|
if ($connection->isTransactionActive()) { |
|
140
|
|
|
$connection->rollBack(); |
|
141
|
|
|
} |
|
142
|
|
|
throw new DoctrineStaticMetaException( |
|
143
|
|
|
'Failed running logged query:' . print_r($query, true), |
|
144
|
|
|
$e->getCode(), |
|
145
|
|
|
$e |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.