Skip to content

Commit 99a43d0

Browse files
authored
Use transactions when simulating queries (#312)
1 parent 39616d7 commit 99a43d0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/QueryReflection/MysqliQueryReflector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ private function simulateQuery(string $queryString)
131131
return $this->cache[$queryString] = null;
132132
}
133133

134+
$this->db->begin_transaction(\MYSQLI_TRANS_START_READ_ONLY);
135+
134136
try {
135137
$result = $this->db->query($simulatedQuery);
136138

@@ -144,6 +146,8 @@ private function simulateQuery(string $queryString)
144146
return $this->cache[$queryString] = $resultInfo;
145147
} catch (mysqli_sql_exception $e) {
146148
return $this->cache[$queryString] = $e;
149+
} finally {
150+
$this->db->rollback();
147151
}
148152
}
149153
}

src/QueryReflection/PdoQueryReflector.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,22 @@ private function simulateQuery(string $queryString)
139139
return $this->cache[$queryString] = null;
140140
}
141141

142+
try {
143+
$this->pdo->beginTransaction();
144+
} catch (PDOException $e) {
145+
// not all drivers may support transactions
146+
}
147+
142148
try {
143149
$stmt = $this->pdo->query($simulatedQuery);
144150
} catch (PDOException $e) {
145151
return $this->cache[$queryString] = $e;
152+
} finally {
153+
try {
154+
$this->pdo->rollBack();
155+
} catch (PDOException $e) {
156+
// not all drivers may support transactions
157+
}
146158
}
147159

148160
$this->cache[$queryString] = [];

0 commit comments

Comments
 (0)