2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
2017-03-17 16:17:24 +01:00
|
|
|
use Amp\Promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $connectionString
|
|
|
|
* @param int $timeout
|
|
|
|
*
|
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-02-16 00:36:10 +01:00
|
|
|
function connect(string $connectionString, int $timeout = 0): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
if (\extension_loaded("pq")) {
|
|
|
|
return PqConnection::connect($connectionString, $timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (\extension_loaded("pgsql")) {
|
|
|
|
return PgSqlConnection::connect($connectionString, $timeout);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param int $connectTimeout
|
|
|
|
*
|
|
|
|
* @return \Amp\Postgres\Pool
|
|
|
|
*/
|
|
|
|
function pool(
|
|
|
|
string $connectionString,
|
|
|
|
int $maxConnections = ConnectionPool::DEFAULT_MAX_CONNECTIONS,
|
|
|
|
int $connectTimeout = ConnectionPool::DEFAULT_CONNECT_TIMEOUT
|
|
|
|
): Pool {
|
|
|
|
return new ConnectionPool($connectionString, $maxConnections, $connectTimeout);
|
|
|
|
}
|