From 09c2a42ff039c5e7d67970dda694266fc86f2c56 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Fri, 31 Jul 2020 23:25:19 -0500 Subject: [PATCH] Consume further results after query error Fixes #32. --- src/PgSqlHandle.php | 6 +++++- src/PqHandle.php | 4 ++++ test/AbstractLinkTest.php | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/PgSqlHandle.php b/src/PgSqlHandle.php index 2dc6939..252340d 100644 --- a/src/PgSqlHandle.php +++ b/src/PgSqlHandle.php @@ -266,13 +266,17 @@ final class PgSqlHandle implements Handle foreach (self::DIAGNOSTIC_CODES as $fieldCode => $desciption) { $diagnostics[$desciption] = \pg_result_error_field($result, $fieldCode); } - throw new QueryExecutionError(\pg_result_error($result), $diagnostics, null, $sql); + $message = \pg_result_error($result); + while (\pg_connection_busy($this->handle) && \pg_get_result($this->handle)); + throw new QueryExecutionError($message, $diagnostics, null, $sql); case \PGSQL_BAD_RESPONSE: + $this->close(); throw new FailureException(\pg_result_error($result)); default: // @codeCoverageIgnoreStart + $this->close(); throw new FailureException("Unknown result status"); // @codeCoverageIgnoreEnd } diff --git a/src/PqHandle.php b/src/PqHandle.php index 9de6f98..697c2bd 100644 --- a/src/PqHandle.php +++ b/src/PqHandle.php @@ -233,12 +233,15 @@ final class PqHandle implements Handle case pq\Result::NONFATAL_ERROR: case pq\Result::FATAL_ERROR: + while ($this->handle->busy && $this->handle->getResult()); throw new QueryExecutionError($result->errorMessage, $result->diag, null, $sql ?? ''); case pq\Result::BAD_RESPONSE: + $this->close(); throw new FailureException($result->errorMessage); default: + $this->close(); throw new FailureException("Unknown result status"); } } @@ -274,6 +277,7 @@ final class PqHandle implements Handle return $result; default: + $this->close(); throw new FailureException($result->errorMessage); } } diff --git a/test/AbstractLinkTest.php b/test/AbstractLinkTest.php index 49be982..26786db 100644 --- a/test/AbstractLinkTest.php +++ b/test/AbstractLinkTest.php @@ -682,4 +682,18 @@ abstract class AbstractLinkTest extends AsyncTestCase $channel = "test"; return Promise\all([$this->connection->listen($channel), $this->connection->listen($channel)]); } + + public function testQueryAfterErroredQuery(): \Generator + { + try { + $result = yield $this->connection->query("INSERT INTO test (domain, tld) VALUES ('github', 'com')"); + } catch (QueryExecutionError $exception) { + // Expected exception due to duplicate key. + } + + /** @var CommandResult $result */ + $result = yield $this->connection->query("INSERT INTO test (domain, tld) VALUES ('gitlab', 'com')"); + + $this->assertSame(1, $result->getAffectedRowCount()); + } }