1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-26 12:04:50 +01:00

Consume further results after query error

Fixes #32.
This commit is contained in:
Aaron Piotrowski 2020-07-31 23:25:19 -05:00
parent 0db6f6df53
commit 09c2a42ff0
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
3 changed files with 23 additions and 1 deletions

View File

@ -266,13 +266,17 @@ final class PgSqlHandle implements Handle
foreach (self::DIAGNOSTIC_CODES as $fieldCode => $desciption) { foreach (self::DIAGNOSTIC_CODES as $fieldCode => $desciption) {
$diagnostics[$desciption] = \pg_result_error_field($result, $fieldCode); $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: case \PGSQL_BAD_RESPONSE:
$this->close();
throw new FailureException(\pg_result_error($result)); throw new FailureException(\pg_result_error($result));
default: default:
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
$this->close();
throw new FailureException("Unknown result status"); throw new FailureException("Unknown result status");
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
} }

View File

@ -233,12 +233,15 @@ final class PqHandle implements Handle
case pq\Result::NONFATAL_ERROR: case pq\Result::NONFATAL_ERROR:
case pq\Result::FATAL_ERROR: case pq\Result::FATAL_ERROR:
while ($this->handle->busy && $this->handle->getResult());
throw new QueryExecutionError($result->errorMessage, $result->diag, null, $sql ?? ''); throw new QueryExecutionError($result->errorMessage, $result->diag, null, $sql ?? '');
case pq\Result::BAD_RESPONSE: case pq\Result::BAD_RESPONSE:
$this->close();
throw new FailureException($result->errorMessage); throw new FailureException($result->errorMessage);
default: default:
$this->close();
throw new FailureException("Unknown result status"); throw new FailureException("Unknown result status");
} }
} }
@ -274,6 +277,7 @@ final class PqHandle implements Handle
return $result; return $result;
default: default:
$this->close();
throw new FailureException($result->errorMessage); throw new FailureException($result->errorMessage);
} }
} }

View File

@ -682,4 +682,18 @@ abstract class AbstractLinkTest extends AsyncTestCase
$channel = "test"; $channel = "test";
return Promise\all([$this->connection->listen($channel), $this->connection->listen($channel)]); 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());
}
} }