diff --git a/src/PgSqlHandle.php b/src/PgSqlHandle.php index de135d3..4678238 100644 --- a/src/PgSqlHandle.php +++ b/src/PgSqlHandle.php @@ -241,13 +241,14 @@ private function send(callable $function, ...$args): \Generator /** * @param resource $result PostgreSQL result resource. + * @param string $sql SQL query text or statement id for error message * * @return \Amp\Sql\CommandResult|ResultSet * * @throws FailureException * @throws QueryError */ - private function createResult($result) + private function createResult($result, $sql) { switch (\pg_result_status($result, \PGSQL_STATUS_LONG)) { case \PGSQL_EMPTY_QUERY: @@ -265,6 +266,7 @@ private function createResult($result) foreach (self::DIAGNOSTIC_CODES as $fieldCode => $desciption) { $diagnostics[$desciption] = \pg_result_error_field($result, $fieldCode); } + $diagnostics['sql'] = $sql; throw new QueryExecutionError(\pg_result_error($result), $diagnostics); case \PGSQL_BAD_RESPONSE: @@ -286,7 +288,7 @@ private function createResult($result) public function statementExecute(string $name, array $params): Promise { return call(function () use ($name, $params) { - return $this->createResult(yield from $this->send("pg_send_execute", $name, $params)); + return $this->createResult(yield from $this->send("pg_send_execute", $name, $params), $name); }); } @@ -326,7 +328,7 @@ public function query(string $sql): Promise } return call(function () use ($sql) { - return $this->createResult(yield from $this->send("pg_send_query", $sql)); + return $this->createResult(yield from $this->send("pg_send_query", $sql), $sql); }); } @@ -343,7 +345,7 @@ public function execute(string $sql, array $params = []): Promise $params = Internal\replaceNamedParams($params, $names); return call(function () use ($sql, $params) { - return $this->createResult(yield from $this->send("pg_send_query_params", $sql, $params)); + return $this->createResult(yield from $this->send("pg_send_query_params", $sql, $params), $sql); }); } @@ -396,6 +398,7 @@ public function prepare(string $sql): Promise foreach (self::DIAGNOSTIC_CODES as $fieldCode => $description) { $diagnostics[$description] = \pg_result_error_field($result, $fieldCode); } + $diagnostics['sql'] = $modifiedSql; throw new QueryExecutionError(\pg_result_error($result), $diagnostics); case \PGSQL_BAD_RESPONSE: diff --git a/src/PqHandle.php b/src/PqHandle.php index 9f774d6..26e7791 100644 --- a/src/PqHandle.php +++ b/src/PqHandle.php @@ -232,7 +232,7 @@ private function send(callable $method, ...$args): \Generator case pq\Result::NONFATAL_ERROR: case pq\Result::FATAL_ERROR: - throw new QueryExecutionError($result->errorMessage, $result->diag); + throw new QueryExecutionError($result->errorMessage, \array_merge($result->diag, ['sql' => $args[0] ?? ''])); case pq\Result::BAD_RESPONSE: throw new FailureException($result->errorMessage); diff --git a/src/QueryExecutionError.php b/src/QueryExecutionError.php index ab7e8ad..c5e0fd5 100644 --- a/src/QueryExecutionError.php +++ b/src/QueryExecutionError.php @@ -11,7 +11,7 @@ class QueryExecutionError extends QueryError public function __construct(string $message, array $diagnostics, \Throwable $previous = null) { - parent::__construct($message, 0, $previous); + parent::__construct($message, $diagnostics['sql'], $previous); $this->diagnostics = $diagnostics; } diff --git a/test/AbstractLinkTest.php b/test/AbstractLinkTest.php index 49be982..c890e30 100644 --- a/test/AbstractLinkTest.php +++ b/test/AbstractLinkTest.php @@ -117,6 +117,7 @@ public function testQueryWithSyntaxError(): \Generator } catch (QueryExecutionError $exception) { $diagnostics = $exception->getDiagnostics(); $this->assertArrayHasKey("sqlstate", $diagnostics); + $this->assertEquals('SELECT & FROM test', $diagnostics['sql']); } }