2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\CancellationToken;
|
|
|
|
use Amp\Promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $connectionString
|
2017-06-05 06:42:18 +02:00
|
|
|
* @param \Amp\CancellationToken $token
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
2017-03-17 16:17:24 +01:00
|
|
|
* @return \Amp\Promise<\Amp\Postgres\Connection>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\FailureException If connecting fails.
|
|
|
|
* @throws \Error If neither ext-pgsql or pecl-pq is loaded.
|
2017-11-05 22:43:40 +01:00
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2016-09-14 16:27:39 +02:00
|
|
|
*/
|
2017-06-05 06:42:18 +02:00
|
|
|
function connect(string $connectionString, CancellationToken $token = null): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
if (\extension_loaded("pq")) {
|
2017-06-05 06:42:18 +02:00
|
|
|
return PqConnection::connect($connectionString, $token);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
if (\extension_loaded("pgsql")) {
|
2017-06-05 06:42:18 +02:00
|
|
|
return PgSqlConnection::connect($connectionString, $token);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2017-04-15 01:33:31 +02:00
|
|
|
throw new \Error("amphp/postgres requires either pecl-pq or ext-pgsql");
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $connectionString
|
|
|
|
* @param int $maxConnections
|
|
|
|
*
|
|
|
|
* @return \Amp\Postgres\Pool
|
|
|
|
*/
|
2017-06-05 06:42:18 +02:00
|
|
|
function pool(string $connectionString, int $maxConnections = ConnectionPool::DEFAULT_MAX_CONNECTIONS): Pool {
|
|
|
|
return new ConnectionPool($connectionString, $maxConnections);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|