Skip to content

Commit

Permalink
Examine result resource after prepare
Browse files Browse the repository at this point in the history
Fixes #2.
  • Loading branch information
trowski committed Jul 31, 2017
1 parent d88d009 commit 588d4ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/PgSqlExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ private function createResult($result) {
throw new FailureException(\pg_result_error($result));

default:
// @codeCoverageIgnoreStart
throw new FailureException("Unknown result status");
// @codeCoverageIgnoreEnd
}
}

Expand Down Expand Up @@ -255,8 +257,25 @@ public function prepare(string $sql): Promise {
$this->statements[$name] = $storage = new Internal\StatementStorage;

$storage->promise = call(function () use ($name, $sql) {
yield from $this->send("pg_send_prepare", $name, $sql);
return new PgSqlStatement($name, $sql, $this->executeCallback, $this->deallocateCallback);
/** @var resource $result PostgreSQL result resource. */
$result = yield from $this->send("pg_send_prepare", $name, $sql);

switch (\pg_result_status($result, \PGSQL_STATUS_LONG)) {
case \PGSQL_COMMAND_OK:
return new PgSqlStatement($name, $sql, $this->executeCallback, $this->deallocateCallback);

case \PGSQL_NONFATAL_ERROR:
case \PGSQL_FATAL_ERROR:
throw new QueryError(\pg_result_error($result));

case \PGSQL_BAD_RESPONSE:
throw new FailureException(\pg_result_error($result));

default:
// @codeCoverageIgnoreStart
throw new FailureException("Unknown result status");
// @codeCoverageIgnoreEnd
}
});
$storage->promise->onResolve(function () use ($storage) {
$storage->promise = null;
Expand Down
14 changes: 14 additions & 0 deletions test/AbstractConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ public function testPrepare() {
});
}

/**
* @depends testPrepare
* @expectedException \Amp\Postgres\QueryError
* @expectedExceptionMessage column "invalid" does not exist
*/
public function testPrepareInvalidQuery() {
Loop::run(function () {
$query = "SELECT * FROM test WHERE invalid=\$1";

/** @var \Amp\Postgres\Statement $statement */
$statement = yield $this->connection->prepare($query);
});
}

/**
* @depends testPrepare
*/
Expand Down

0 comments on commit 588d4ff

Please sign in to comment.