Skip to content

PgSQL driver improvement #5887

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

Closed
wants to merge 1 commit into from
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
2 changes: 2 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,8 @@
<referencedFunction name="pg_field_type"/>
<referencedFunction name="pg_free_result"/>
<referencedFunction name="pg_get_result"/>
<referencedFunction name="pg_query"/>
<referencedFunction name="pg_last_error"/>
<referencedFunction name="pg_num_fields"/>
<referencedFunction name="pg_result_error_field"/>
<referencedFunction name="pg_send_execute"/>
Expand Down
36 changes: 21 additions & 15 deletions src/Driver/PgSQL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use function pg_escape_bytea;
use function pg_escape_literal;
use function pg_get_result;
use function pg_last_error;
use function pg_query;
use function pg_result_error;
use function pg_send_prepare;
use function pg_version;
Expand Down Expand Up @@ -56,7 +58,10 @@ public function prepare(string $sql): Statement
assert($success);

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

if ($result === false) {
throw new Exception(pg_last_error($this->connection));
}

if ((bool) pg_result_error($result)) {
throw Exception::fromResult($result);
Expand All @@ -67,7 +72,17 @@ public function prepare(string $sql): Statement

public function query(string $sql): Result
{
return $this->prepare($sql)->execute();
$result = @pg_query($this->connection, $sql);

if ($result === false) {
throw new Exception(pg_last_error($this->connection));
}

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

return new Result($result);
}

/** {@inheritdoc} */
Expand All @@ -82,7 +97,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 All @@ -101,28 +116,19 @@ public function lastInsertId($name = null)
return $this->query('SELECT LASTVAL()')->fetchOne();
}

/** @return true */
public function beginTransaction(): bool
{
$this->exec('BEGIN');

return true;
return (bool) @pg_query($this->connection, 'BEGIN');
}

/** @return true */
public function commit(): bool
{
$this->exec('COMMIT');

return true;
return (bool) @pg_query($this->connection, 'COMMIT');
}

/** @return true */
public function rollBack(): bool
{
$this->exec('ROLLBACK');

return true;
return (bool) @pg_query($this->connection, 'ROLLBACK');
}

public function getServerVersion(): string
Expand Down