Skip to content

Commit

Permalink
Close pgsql connection in destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Feb 2, 2023
1 parent ce82b06 commit 9c2a1bb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ parameters:
- '~^Parameter #1 \$row of method Doctrine\\DBAL\\Driver\\PgSQL\\Result\:\:mapNumericRow\(\) expects array<int, string\|null>, array<int\|string, string\|null> given\.$~'

# Ignore isset() checks in destructors.
- '~^Property Doctrine\\DBAL\\Driver\\PgSQL\\Connection\:\:\$connection \(PgSql\\Connection\|resource\) in isset\(\) is not nullable\.$~'
- '~^Property Doctrine\\DBAL\\Driver\\PgSQL\\Statement\:\:\$connection \(PgSql\\Connection\|resource\) in isset\(\) is not nullable\.$~'

# On PHP 7.4, pg_fetch_all() might return false for empty result sets.
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@
<errorLevel type="suppress">
<!-- PgSql objects are represented as resources in PHP 7.4 -->
<referencedFunction name="pg_affected_rows"/>
<referencedFunction name="pg_close"/>
<referencedFunction name="pg_escape_bytea"/>
<referencedFunction name="pg_escape_identifier"/>
<referencedFunction name="pg_escape_literal"/>
Expand Down Expand Up @@ -752,6 +753,7 @@
<directory name="tests"/>

<!-- Ignore isset() checks in destructors. -->
<file name="src/Driver/PgSQL/Connection.php"/>
<file name="src/Driver/PgSQL/Statement.php"/>
</errorLevel>
</RedundantPropertyInitializationCheck>
Expand Down
10 changes: 10 additions & 0 deletions src/Driver/PgSQL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function gettype;
use function is_object;
use function is_resource;
use function pg_close;
use function pg_escape_bytea;
use function pg_escape_literal;
use function pg_get_result;
Expand Down Expand Up @@ -47,6 +48,15 @@ public function __construct($connection)
$this->parser = new Parser(false);
}

public function __destruct()
{
if (! isset($this->connection)) {
return;
}

@pg_close($this->connection);
}

public function prepare(string $sql): Statement
{
$visitor = new ConvertParameters();
Expand Down

0 comments on commit 9c2a1bb

Please sign in to comment.