1313use DateTimeImmutable ;
1414use DateTimeInterface ;
1515use DateTimeZone ;
16- use MakiseCo \Database \Driver \MakisePostgres \Bridge \ExceptionMapper ;
1716use MakiseCo \Database \Driver \MakisePostgres \Bridge \OptionsProcessor ;
1817use MakiseCo \Database \Driver \MakisePostgres \Bridge \Statement ;
1918use MakiseCo \Postgres \ConnectionConfig ;
3130use Spiral \Database \Driver \Postgres \PostgresCompiler ;
3231use Spiral \Database \Driver \ReadonlyHandler ;
3332use Spiral \Database \Exception \DriverException ;
33+ use Spiral \Database \Exception \HandlerException ;
3434use Spiral \Database \Exception \StatementException ;
3535use Spiral \Database \Injection \ParameterInterface ;
3636use Spiral \Database \Query \BuilderInterface ;
3939use Spiral \Database \Query \QueryBuilder ;
4040use Spiral \Database \Query \SelectQuery ;
4141use Spiral \Database \Query \UpdateQuery ;
42+ use Spiral \Database \Schema \AbstractTable ;
4243use Spiral \Database \StatementInterface ;
4344use Swoole \Coroutine ;
4445use Throwable ;
4546
47+ use function array_key_exists ;
4648use function array_merge ;
49+ use function is_string ;
50+ use function strpos ;
51+ use function substr ;
4752
4853class PooledMakisePostgresDriver implements DriverInterface
4954{
@@ -95,7 +100,22 @@ class PooledMakisePostgresDriver implements DriverInterface
95100 'queryCache ' => true ,
96101
97102 // disable schema modifications
98- 'readonlySchema ' => false
103+ 'readonlySchema ' => false ,
104+
105+ // default connector is Pq connector
106+ 'connector ' => \MakiseCo \Postgres \Driver \Pq \PqConnector::class,
107+
108+ // minimal connection count in the pool
109+ 'poolMinActive ' => 0 ,
110+
111+ // maximum connection count in the pool
112+ 'poolMaxActive ' => 2 ,
113+
114+ // maximum connection idle time (seconds, int)
115+ 'poolMaxIdleTime ' => 30 ,
116+
117+ // how often pool will check idle connections
118+ 'poolValidationInterval ' => 15.0 ,
99119 ];
100120
101121 public function __construct (array $ options )
@@ -109,7 +129,7 @@ public function __construct(array $options)
109129 ))->withDriver ($ this );
110130 $ this ->queryCompiler = new PostgresCompiler ('"" ' );
111131
112- if (\ array_key_exists ('connection ' , $ options )) {
132+ if (array_key_exists ('connection ' , $ options )) {
113133 OptionsProcessor::parseDsn ($ options ['connection ' ], $ options );
114134 }
115135
@@ -128,7 +148,23 @@ public function __construct(array $options)
128148 $ this ->schemaHandler = new ReadonlyHandler ($ this ->schemaHandler );
129149 }
130150
131- $ this ->pool = new Bridge \PostgresPool ($ this ->connectionConfig , new PqConnector ());
151+ $ this ->pool = new Bridge \PostgresPool (
152+ $ this ->connectionConfig ,
153+ new $ options ['connector ' ]()
154+ );
155+
156+ $ this ->pool ->setMaxActive ($ this ->options ['poolMaxActive ' ]);
157+ $ this ->pool ->setMinActive ($ this ->options ['poolMinActive ' ]);
158+ $ this ->pool ->setMaxIdleTime ($ this ->options ['poolMaxIdleTime ' ]);
159+ $ this ->pool ->setValidationInterval ($ this ->options ['poolValidationInterval ' ]);
160+
161+ if (array_key_exists ('poolMaxWaitTime ' , $ this ->options )) {
162+ $ this ->pool ->setMaxWaitTime ($ this ->options ['poolMaxWaitTime ' ]);
163+ }
164+
165+ if (array_key_exists ('poolMaxLifeTime ' , $ this ->options )) {
166+ $ this ->pool ->setMaxLifeTime ($ this ->options ['poolMaxLifeTime ' ]);
167+ }
132168 }
133169
134170 public function __destruct ()
@@ -251,7 +287,7 @@ public function beginTransaction(string $isolationLevel = null): bool
251287 try {
252288 $ transaction = $ this ->pool ->beginTransaction ($ isolation );
253289 } catch (Throwable $ e ) {
254- $ e = ExceptionMapper::map ($ e , 'BEGIN TRANSACTION ' );
290+ $ e = Bridge \ ExceptionMapper::map ($ e , 'BEGIN TRANSACTION ' );
255291
256292 if (!$ e instanceof StatementException \ConnectionException || !$ this ->options ['reconnect ' ]) {
257293 throw $ e ;
@@ -260,7 +296,7 @@ public function beginTransaction(string $isolationLevel = null): bool
260296 try {
261297 $ transaction = $ this ->pool ->beginTransaction ($ isolation );
262298 } catch (Throwable $ e ) {
263- throw ExceptionMapper::map ($ e , 'BEGIN TRANSACTION ' );
299+ throw Bridge \ ExceptionMapper::map ($ e , 'BEGIN TRANSACTION ' );
264300 }
265301 }
266302
@@ -333,11 +369,11 @@ public function getSource(): string
333369 *
334370 * @param string $table
335371 * @param string|null $prefix
336- * @return \Spiral\Database\Schema\ AbstractTable
372+ * @return AbstractTable
337373 *
338- * @throws \Spiral\Database\Exception\ HandlerException
374+ * @throws HandlerException
339375 */
340- public function getSchema (string $ table , ?string $ prefix = null ): \ Spiral \ Database \ Schema \ AbstractTable
376+ public function getSchema (string $ table , ?string $ prefix = null ): AbstractTable
341377 {
342378 return $ this ->getSchemaHandler ()->getSchema (
343379 $ table ,
@@ -461,7 +497,7 @@ protected function statement(
461497
462498 return new Statement ($ statement , $ result );
463499 } catch (Throwable $ e ) {
464- $ e = ExceptionMapper::map ($ e , Interpolator::interpolate ($ query , $ parameters ));
500+ $ e = Bridge \ ExceptionMapper::map ($ e , Interpolator::interpolate ($ query , $ parameters ));
465501
466502 if (
467503 $ retry
@@ -517,8 +553,8 @@ protected function getParameters(iterable $parameters): array
517553 $ normParams = [];
518554
519555 foreach ($ parameters as $ key => $ param ) {
520- if (\ is_string ($ key ) && 0 === \ strpos ($ key , ': ' )) {
521- $ key = \ substr ($ key , 1 );
556+ if (is_string ($ key ) && 0 === strpos ($ key , ': ' )) {
557+ $ key = substr ($ key , 1 );
522558 }
523559
524560 if ($ param instanceof ParameterInterface) {
@@ -571,4 +607,4 @@ protected function formatDatetime(DateTimeInterface $value): string
571607
572608 return $ datetime ->setTimestamp ($ value ->getTimestamp ())->format (static ::DATETIME );
573609 }
574- }
610+ }
0 commit comments