Skip to content

Commit 09c2a42

Browse files
committed
Consume further results after query error
Fixes #32.
1 parent 0db6f6d commit 09c2a42

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/PgSqlHandle.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,17 @@ private function createResult($result, string $sql)
266266
foreach (self::DIAGNOSTIC_CODES as $fieldCode => $desciption) {
267267
$diagnostics[$desciption] = \pg_result_error_field($result, $fieldCode);
268268
}
269-
throw new QueryExecutionError(\pg_result_error($result), $diagnostics, null, $sql);
269+
$message = \pg_result_error($result);
270+
while (\pg_connection_busy($this->handle) && \pg_get_result($this->handle));
271+
throw new QueryExecutionError($message, $diagnostics, null, $sql);
270272

271273
case \PGSQL_BAD_RESPONSE:
274+
$this->close();
272275
throw new FailureException(\pg_result_error($result));
273276

274277
default:
275278
// @codeCoverageIgnoreStart
279+
$this->close();
276280
throw new FailureException("Unknown result status");
277281
// @codeCoverageIgnoreEnd
278282
}

src/PqHandle.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,15 @@ private function send(?string $sql, callable $method, ...$args): \Generator
233233

234234
case pq\Result::NONFATAL_ERROR:
235235
case pq\Result::FATAL_ERROR:
236+
while ($this->handle->busy && $this->handle->getResult());
236237
throw new QueryExecutionError($result->errorMessage, $result->diag, null, $sql ?? '');
237238

238239
case pq\Result::BAD_RESPONSE:
240+
$this->close();
239241
throw new FailureException($result->errorMessage);
240242

241243
default:
244+
$this->close();
242245
throw new FailureException("Unknown result status");
243246
}
244247
}
@@ -274,6 +277,7 @@ private function fetch(): \Generator
274277
return $result;
275278

276279
default:
280+
$this->close();
277281
throw new FailureException($result->errorMessage);
278282
}
279283
}

test/AbstractLinkTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,4 +682,18 @@ public function testListenOnSameChannel(): Promise
682682
$channel = "test";
683683
return Promise\all([$this->connection->listen($channel), $this->connection->listen($channel)]);
684684
}
685+
686+
public function testQueryAfterErroredQuery(): \Generator
687+
{
688+
try {
689+
$result = yield $this->connection->query("INSERT INTO test (domain, tld) VALUES ('github', 'com')");
690+
} catch (QueryExecutionError $exception) {
691+
// Expected exception due to duplicate key.
692+
}
693+
694+
/** @var CommandResult $result */
695+
$result = yield $this->connection->query("INSERT INTO test (domain, tld) VALUES ('gitlab', 'com')");
696+
697+
$this->assertSame(1, $result->getAffectedRowCount());
698+
}
685699
}

0 commit comments

Comments
 (0)