Skip to content

Commit 912177f

Browse files
staabmclxmstaab
andauthored
output simulated query on syntax errors when in debug-mode (#85)
Co-authored-by: Markus Staab <[email protected]>
1 parent 2a9ee14 commit 912177f

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/QueryReflection/MysqliQueryReflector.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,29 @@ public function __construct(mysqli $mysqli)
6565

6666
public function validateQueryString(string $queryString): ?Error
6767
{
68-
try {
69-
$simulatedQuery = QuerySimulation::simulate($queryString);
70-
if (null === $simulatedQuery) {
71-
return null;
72-
}
68+
$simulatedQuery = QuerySimulation::simulate($queryString);
69+
if (null === $simulatedQuery) {
70+
return null;
71+
}
7372

73+
try {
7474
$this->db->query($simulatedQuery);
7575

7676
return null;
7777
} catch (mysqli_sql_exception $e) {
7878
if (\in_array($e->getCode(), [self::MYSQL_SYNTAX_ERROR_CODE, self::MYSQL_UNKNOWN_COLUMN_IN_FIELDLIST, self::MYSQL_UNKNOWN_TABLE], true)) {
79-
// make error string consistent across mysql/mariadb
79+
8080
$message = $e->getMessage();
81+
82+
// make error string consistent across mysql/mariadb
8183
$message = str_replace(' MySQL server', ' MySQL/MariaDB server', $message);
8284
$message = str_replace(' MariaDB server', ' MySQL/MariaDB server', $message);
8385

86+
// to ease debugging, print the error we simulated
87+
if ($e->getCode() === self::MYSQL_SYNTAX_ERROR_CODE && QueryReflection::getRuntimeConfiguration()->isDebugEnabled()) {
88+
$message = $message ."\n\nSimulated query: ". $simulatedQuery;
89+
}
90+
8491
return new Error($message, $e->getCode());
8592
}
8693

@@ -93,11 +100,12 @@ public function validateQueryString(string $queryString): ?Error
93100
*/
94101
public function getResultType(string $queryString, int $fetchType): ?Type
95102
{
103+
$simulatedQuery = QuerySimulation::simulate($queryString);
104+
if (null === $simulatedQuery) {
105+
return null;
106+
}
107+
96108
try {
97-
$simulatedQuery = QuerySimulation::simulate($queryString);
98-
if (null === $simulatedQuery) {
99-
return null;
100-
}
101109
$result = $this->db->query($simulatedQuery);
102110

103111
if (!$result instanceof mysqli_result) {

src/QueryReflection/RuntimeConfiguration.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ final class RuntimeConfiguration
1515
* @var self::ERROR_MODE*
1616
*/
1717
private $errorMode = self::ERROR_MODE_DEFAULT;
18+
/**
19+
* @var bool
20+
*/
21+
private $debugMode = false;
1822

1923
public static function create(): self
2024
{
@@ -24,13 +28,24 @@ public static function create(): self
2428
/**
2529
* @param self::ERROR_MODE* $mode
2630
*/
27-
public function errorMode($mode): self
31+
public function errorMode(string $mode): self
2832
{
2933
$this->errorMode = $mode;
3034

3135
return $this;
3236
}
3337

38+
public function debugMode(bool $mode): self
39+
{
40+
$this->debugMode = $mode;
41+
42+
return $this;
43+
}
44+
45+
public function isDebugEnabled():bool {
46+
return $this->debugMode;
47+
}
48+
3449
public function throwsPdoExceptions(PhpVersion $phpVersion): bool
3550
{
3651
if (self::ERROR_MODE_EXCEPTION === $this->errorMode) {

0 commit comments

Comments
 (0)