From 53d3f8b07425544d713860b6f5366927dda985b1 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Tue, 10 Jul 2018 18:50:11 -0500 Subject: [PATCH] Remove resetConnections Moved to constructor argument. --- src/Pool.php | 26 ++++++++++++++++++-------- src/functions.php | 16 +++++++++++----- test/PgSqlPoolTest.php | 2 +- test/PqPoolTest.php | 2 +- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/Pool.php b/src/Pool.php index 1cbac83..a0525b1 100644 --- a/src/Pool.php +++ b/src/Pool.php @@ -5,6 +5,7 @@ namespace Amp\Postgres; use Amp\Coroutine; use Amp\Promise; use Amp\Sql\AbstractPool; +use Amp\Sql\ConnectionConfig; use Amp\Sql\Connector; use Amp\Sql\Pool as SqlPool; use Amp\Sql\ResultSet as SqlResultSet; @@ -24,6 +25,23 @@ final class Pool extends AbstractPool implements Link /** @var bool */ private $resetConnections = true; + /** + * @param ConnectionConfig $config + * @param int $maxConnections + * @param int $idleTimeout + * @param bool $resetConnections True to automatically execute RESET ALL on a connection before use. + * @param Connector|null $connector + */ + public function __construct( + ConnectionConfig $config, + int $maxConnections = SqlPool::DEFAULT_MAX_CONNECTIONS, + int $idleTimeout = SqlPool::DEFAULT_IDLE_TIMEOUT, + bool $resetConnections = true, + Connector $connector = null + ) { + parent::__construct($config, $maxConnections, $idleTimeout, $connector); + } + /** * @return Connector The Connector instance defined by the connector() function. */ @@ -54,14 +72,6 @@ final class Pool extends AbstractPool implements Link return new PooledResultSet($resultSet, $release); } - /** - * @param bool $reset True to automatically execute RESET ALL on a connection before it is used by the pool. - */ - public function resetConnections(bool $reset) - { - $this->resetConnections = $reset; - } - protected function pop(): \Generator { $connection = yield from parent::pop(); diff --git a/src/functions.php b/src/functions.php index 41c8c31..1b1a187 100644 --- a/src/functions.php +++ b/src/functions.php @@ -47,13 +47,19 @@ function connect(SqlConnectionConfig $config): Promise * Create a pool using the global Connector instance. * * @param SqlConnectionConfig $config - * @param int $maxConnections + * @param int $maxConnections + * @param int $idleTimeout + * @param bool $resetConnections * * @return Pool */ -function pool(SqlConnectionConfig $config, int $maxConnections = SqlPool::DEFAULT_MAX_CONNECTIONS): Pool -{ - return new Pool($config, $maxConnections, connector()); +function pool( + SqlConnectionConfig $config, + int $maxConnections = SqlPool::DEFAULT_MAX_CONNECTIONS, + int $idleTimeout = SqlPool::DEFAULT_IDLE_TIMEOUT, + bool $resetConnections = true +): Pool { + return new Pool($config, $maxConnections, $idleTimeout, $resetConnections, connector()); } /** @@ -114,7 +120,7 @@ function encode(array $array): string } $value = (string) $value; - // no break + // no break case "string": return '"' . \str_replace(['\\', '"'], ['\\\\', '\\"'], $value) . '"'; diff --git a/test/PgSqlPoolTest.php b/test/PgSqlPoolTest.php index ada0903..9e896cf 100644 --- a/test/PgSqlPoolTest.php +++ b/test/PgSqlPoolTest.php @@ -38,7 +38,7 @@ class PgSqlPoolTest extends AbstractLinkTest return new Success(new PgSqlConnection($handle, \pg_socket($handle))); })); - $pool = new Pool(new ConnectionConfig('connection string'), \count($this->handles), $connector); + $pool = new Pool(new ConnectionConfig('connection string'), \count($this->handles), Pool::DEFAULT_IDLE_TIMEOUT, true, $connector); $handle = \reset($this->handles); diff --git a/test/PqPoolTest.php b/test/PqPoolTest.php index ffd853b..2185835 100644 --- a/test/PqPoolTest.php +++ b/test/PqPoolTest.php @@ -40,7 +40,7 @@ class PqPoolTest extends AbstractLinkTest return new Success(new PqConnection($handle)); })); - $pool = new Pool(new ConnectionConfig('connection string'), \count($this->handles), $connector); + $pool = new Pool(new ConnectionConfig('connection string'), \count($this->handles), Pool::DEFAULT_IDLE_TIMEOUT, true, $connector); $handle = \reset($this->handles);