Skip to content

Commit

Permalink
Use transactions when simulating queries (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw authored Feb 20, 2022
1 parent 39616d7 commit 99a43d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/QueryReflection/MysqliQueryReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ private function simulateQuery(string $queryString)
return $this->cache[$queryString] = null;
}

$this->db->begin_transaction(\MYSQLI_TRANS_START_READ_ONLY);

try {
$result = $this->db->query($simulatedQuery);

Expand All @@ -144,6 +146,8 @@ private function simulateQuery(string $queryString)
return $this->cache[$queryString] = $resultInfo;
} catch (mysqli_sql_exception $e) {
return $this->cache[$queryString] = $e;
} finally {
$this->db->rollback();
}
}
}
12 changes: 12 additions & 0 deletions src/QueryReflection/PdoQueryReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,22 @@ private function simulateQuery(string $queryString)
return $this->cache[$queryString] = null;
}

try {
$this->pdo->beginTransaction();
} catch (PDOException $e) {
// not all drivers may support transactions
}

try {
$stmt = $this->pdo->query($simulatedQuery);
} catch (PDOException $e) {
return $this->cache[$queryString] = $e;
} finally {
try {
$this->pdo->rollBack();
} catch (PDOException $e) {
// not all drivers may support transactions
}
}

$this->cache[$queryString] = [];
Expand Down

0 comments on commit 99a43d0

Please sign in to comment.