Skip to content

Commit 58b304d

Browse files
committed
DB: always connect with PGSQL_CONNECT_FORCE_NEW flag [ToDo Docs why]
1 parent fd32552 commit 58b304d

File tree

6 files changed

+10
-42
lines changed

6 files changed

+10
-42
lines changed

docs/db.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ Pass `TRUE` as the second parameter to force new connection (otherwise, existing
1818

1919
> Personal note: I'm thinking about removing this in the next big release.
2020
21-
You can create a blank `Connection` object and set connection parameters on this object with functions `setConnectionConfig()` and `setConnectForceNew()`. You must set it before `connect()` is executed.
21+
You can create a blank `Connection` object and set connection parameters on this object with function `setConnectionConfig()` later. You must set it before `connect()` is executed.
2222

2323
```php
2424
$connection = new Forrest79\PhPgSql\Db\Connection();
2525
$connection->setConnectionConfig('host=localhost port=5432 dbname=test user=user1 password=xyz111 connect_timeout=5');
26-
$connection->setConnectForceNew(TRUE);
2726
```
2827

2928
Once you have a connection, you can manually connect it:

src/Db/Connection.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class Connection
88
{
99
private string $connectionConfig;
1010

11-
private bool $connectForceNew;
12-
1311
private int $errorVerbosity;
1412

1513
private AsyncHelper $asyncHelper;
@@ -28,10 +26,9 @@ class Connection
2826
/**
2927
* @throws Exceptions\ConnectionException
3028
*/
31-
public function __construct(string $connectionConfig = '', bool $connectForceNew = FALSE)
29+
public function __construct(string $connectionConfig = '')
3230
{
3331
$this->connectionConfig = $connectionConfig;
34-
$this->connectForceNew = $connectForceNew;
3532

3633
$this->errorVerbosity = \PGSQL_ERRORS_DEFAULT;
3734

@@ -50,12 +47,7 @@ public function connect(): static
5047
throw Exceptions\ConnectionException::noConfig();
5148
}
5249

53-
$connectType = 0;
54-
if ($this->connectForceNew === TRUE) {
55-
$connectType |= \PGSQL_CONNECT_FORCE_NEW;
56-
}
57-
58-
$resource = @\pg_connect($this->connectionConfig, $connectType); // intentionally @
50+
$resource = @\pg_connect($this->connectionConfig, \PGSQL_CONNECT_FORCE_NEW); // intentionally @
5951
if ($resource === FALSE) {
6052
throw Exceptions\ConnectionException::connectionFailed();
6153
} elseif (\pg_connection_status($resource) === \PGSQL_CONNECTION_BAD) {
@@ -97,7 +89,7 @@ public function ping(): bool
9789
public function setConnectionConfig(string $config): static
9890
{
9991
if ($this->isConnected()) {
100-
throw Exceptions\ConnectionException::cantChangeWhenConnected('config');
92+
throw Exceptions\ConnectionException::cantChangeConnectionConfigWhenConnected();
10193
}
10294

10395
$this->connectionConfig = $config;
@@ -112,18 +104,6 @@ public function getConnectionConfig(): string
112104
}
113105

114106

115-
public function setConnectForceNew(bool $forceNew = TRUE): static
116-
{
117-
if ($this->isConnected()) {
118-
throw Exceptions\ConnectionException::cantChangeWhenConnected('forceNew');
119-
}
120-
121-
$this->connectForceNew = $forceNew;
122-
123-
return $this;
124-
}
125-
126-
127107
public function setErrorVerbosity(int $errorVerbosity): static
128108
{
129109
if ($this->errorVerbosity !== $errorVerbosity) {

src/Db/Exceptions/ConnectionException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class ConnectionException extends Exception
66
{
77
public const NO_CONFIG = 1;
8-
public const CANT_CHANGE_WHEN_CONNECTED = 2;
8+
public const CANT_CHANGE_CONNECTION_CONFIG_WHEN_CONNECTED = 2;
99
public const CONNECTION_FAILED = 3;
1010
public const BAD_CONNECTION = 4;
1111
public const CANT_GET_NOTICES = 5;
@@ -22,9 +22,9 @@ public static function noConfig(): self
2222
}
2323

2424

25-
public static function cantChangeWhenConnected(string $type): self
25+
public static function cantChangeConnectionConfigWhenConnected(): self
2626
{
27-
return new self(\sprintf('You can\'t change \'%s\' when connected.', $type), self::CANT_CHANGE_WHEN_CONNECTED);
27+
return new self('You can\'t change \'connectionConfig\' when connected.', self::CANT_CHANGE_CONNECTION_CONFIG_WHEN_CONNECTED);
2828
}
2929

3030

tests/Integration/AsyncTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public function testError(): void
207207

208208
protected function createConnection(): Db\Connection
209209
{
210-
return new Db\Connection($this->getTestConnectionConfig(), FALSE);
210+
return new Db\Connection($this->getTestConnectionConfig());
211211
}
212212

213213
}

tests/Integration/BasicTest.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,6 @@ public function testConnectionNoConfig(): void
8888
}
8989

9090

91-
public function testConnectionForceNew(): void
92-
{
93-
$this->connection->setConnectForceNew(TRUE);
94-
Tester\Assert::true($this->connection->ping());
95-
}
96-
97-
9891
public function testFailedConnection(): void
9992
{
10093
$this->connection->setConnectionConfig(\str_replace('user=', 'user=non-existing-user-', $this->getConfig()));
@@ -111,16 +104,11 @@ public function testChangeConnectionSettingsAfterConnected(): void
111104

112105
Tester\Assert::exception(function (): void {
113106
$this->connection->setConnectionConfig('');
114-
}, Db\Exceptions\ConnectionException::class, NULL, Db\Exceptions\ConnectionException::CANT_CHANGE_WHEN_CONNECTED);
115-
116-
Tester\Assert::exception(function (): void {
117-
$this->connection->setConnectForceNew();
118-
}, Db\Exceptions\ConnectionException::class, NULL, Db\Exceptions\ConnectionException::CANT_CHANGE_WHEN_CONNECTED);
107+
}, Db\Exceptions\ConnectionException::class, NULL, Db\Exceptions\ConnectionException::CANT_CHANGE_CONNECTION_CONFIG_WHEN_CONNECTED);
119108

120109
$this->connection->close();
121110

122111
$this->connection->setConnectionConfig('');
123-
$this->connection->setConnectForceNew();
124112
}
125113

126114

tests/Integration/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function tearDown(): void
3535
{
3636
$this->connection->close();
3737
$this->adminConnection->query('DROP DATABASE ?', Db\Sql\Literal::create($this->dbname));
38+
$this->adminConnection->close();
3839
}
3940

4041

0 commit comments

Comments
 (0)