Skip to content

Optimize pgsql queries without parameters #5889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2023
Merged
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
2 changes: 2 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -655,10 +655,12 @@
<referencedFunction name="pg_field_type"/>
<referencedFunction name="pg_free_result"/>
<referencedFunction name="pg_get_result"/>
<referencedFunction name="pg_last_error"/>
<referencedFunction name="pg_num_fields"/>
<referencedFunction name="pg_result_error_field"/>
<referencedFunction name="pg_send_execute"/>
<referencedFunction name="pg_send_prepare"/>
<referencedFunction name="pg_send_query"/>
<referencedFunction name="pg_version"/>
</errorLevel>
</PossiblyInvalidArgument>
Expand Down
17 changes: 15 additions & 2 deletions src/Driver/PgSQL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use function pg_escape_bytea;
use function pg_escape_literal;
use function pg_get_result;
use function pg_last_error;
use function pg_result_error;
use function pg_send_prepare;
use function pg_send_query;
use function pg_version;
use function sprintf;
use function uniqid;
Expand Down Expand Up @@ -67,7 +69,18 @@ public function prepare(string $sql): Statement

public function query(string $sql): Result
{
return $this->prepare($sql)->execute();
if (@pg_send_query($this->connection, $sql) !== true) {
throw new Exception(pg_last_error($this->connection));
}

$result = @pg_get_result($this->connection);
assert($result !== false);
Copy link
Member

Choose a reason for hiding this comment

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

What should happen when a query returns 0 records? Do we still get a PgSql\Result instance?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. An empty result is still a result.

Also, that case is really well covered by functional tests. 😀


if ((bool) pg_result_error($result)) {
throw Exception::fromResult($result);
}

return new Result($result);
}

/** {@inheritdoc} */
Expand All @@ -82,7 +95,7 @@ public function quote($value, $type = ParameterType::STRING)

public function exec(string $sql): int
{
return $this->prepare($sql)->execute()->rowCount();
return $this->query($sql)->rowCount();
}

/** {@inheritdoc} */
Expand Down