Skip to content

Commit f73e964

Browse files
authored
Merge pull request #5889 from doctrine/improvement/optimize-pg-query
2 parents 26d1542 + 121cc38 commit f73e964

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

psalm.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,12 @@
655655
<referencedFunction name="pg_field_type"/>
656656
<referencedFunction name="pg_free_result"/>
657657
<referencedFunction name="pg_get_result"/>
658+
<referencedFunction name="pg_last_error"/>
658659
<referencedFunction name="pg_num_fields"/>
659660
<referencedFunction name="pg_result_error_field"/>
660661
<referencedFunction name="pg_send_execute"/>
661662
<referencedFunction name="pg_send_prepare"/>
663+
<referencedFunction name="pg_send_query"/>
662664
<referencedFunction name="pg_version"/>
663665
</errorLevel>
664666
</PossiblyInvalidArgument>

src/Driver/PgSQL/Connection.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
use function pg_escape_bytea;
1818
use function pg_escape_literal;
1919
use function pg_get_result;
20+
use function pg_last_error;
2021
use function pg_result_error;
2122
use function pg_send_prepare;
23+
use function pg_send_query;
2224
use function pg_version;
2325
use function sprintf;
2426
use function uniqid;
@@ -67,7 +69,18 @@ public function prepare(string $sql): Statement
6769

6870
public function query(string $sql): Result
6971
{
70-
return $this->prepare($sql)->execute();
72+
if (@pg_send_query($this->connection, $sql) !== true) {
73+
throw new Exception(pg_last_error($this->connection));
74+
}
75+
76+
$result = @pg_get_result($this->connection);
77+
assert($result !== false);
78+
79+
if ((bool) pg_result_error($result)) {
80+
throw Exception::fromResult($result);
81+
}
82+
83+
return new Result($result);
7184
}
7285

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

8396
public function exec(string $sql): int
8497
{
85-
return $this->prepare($sql)->execute()->rowCount();
98+
return $this->query($sql)->rowCount();
8699
}
87100

88101
/** {@inheritdoc} */

0 commit comments

Comments
 (0)