From cc071baaef2de8e9d9c2be81a4a17c94705c8feb Mon Sep 17 00:00:00 2001 From: Sartor Date: Fri, 20 Mar 2020 18:26:03 +0200 Subject: [PATCH 1/3] PgSql now shows text of failed query in Exception --- src/PgSqlHandle.php | 10 ++++++---- src/QueryExecutionError.php | 2 +- test/AbstractLinkTest.php | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/PgSqlHandle.php b/src/PgSqlHandle.php index de135d3..2e7981f 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); }); } 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']); } } From de9fe58c076b87ec1330d5efde5aa1c004c42f59 Mon Sep 17 00:00:00 2001 From: Sartor Date: Mon, 23 Mar 2020 13:17:40 +0200 Subject: [PATCH 2/3] Sql show on error in using pq --- src/PgSqlHandle.php | 3 ++- src/PqHandle.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/PgSqlHandle.php b/src/PgSqlHandle.php index 2e7981f..4678238 100644 --- a/src/PgSqlHandle.php +++ b/src/PgSqlHandle.php @@ -248,7 +248,7 @@ private function send(callable $function, ...$args): \Generator * @throws FailureException * @throws QueryError */ - private function createResult($result, &$sql) + private function createResult($result, $sql) { switch (\pg_result_status($result, \PGSQL_STATUS_LONG)) { case \PGSQL_EMPTY_QUERY: @@ -398,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..68f515c 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); From c313afb743550517e8a3db8c333713d158c1f306 Mon Sep 17 00:00:00 2001 From: Sartor Date: Mon, 23 Mar 2020 13:48:41 +0200 Subject: [PATCH 3/3] array_merge namespace style fix --- src/PqHandle.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PqHandle.php b/src/PqHandle.php index 68f515c..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, array_merge($result->diag, ['sql' => $args[0] ?? ''])); + throw new QueryExecutionError($result->errorMessage, \array_merge($result->diag, ['sql' => $args[0] ?? ''])); case pq\Result::BAD_RESPONSE: throw new FailureException($result->errorMessage);