Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/PgSqlHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for adding this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$diagnostics will be used in QueryExecutionError for error message. 'sql' key will contain failed query

throw new QueryExecutionError(\pg_result_error($result), $diagnostics);

case \PGSQL_BAD_RESPONSE:
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$name is not the query SQL.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought name of statement may be more usefull than nothing. I can remove it

});
}

Expand Down Expand Up @@ -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);
});
}

Expand All @@ -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);
});
}

Expand Down Expand Up @@ -396,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:
Expand Down
2 changes: 1 addition & 1 deletion src/PqHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/QueryExecutionError.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A string should not be used for the code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? parent constructor have exactly string parameter at this place? Any ideas how to pass this without string?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it does, I didn't realize QueryError had a custom constructor taking the SQL query. Not providing it to QueryExecutionError was certainly an oversight before.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I do?

$this->diagnostics = $diagnostics;
}

Expand Down
1 change: 1 addition & 0 deletions test/AbstractLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

Expand Down