1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-27 12:34:40 +01:00
postgres/lib/functions.php

42 lines
1.1 KiB
PHP
Raw Normal View History

2016-09-14 16:27:39 +02:00
<?php declare(strict_types = 1);
namespace Amp\Postgres;
2016-11-15 18:06:21 +01:00
use Interop\Async\Promise;
2016-09-14 16:27:39 +02:00
/**
* @param string $connectionString
* @param int $timeout
*
2016-11-15 18:06:21 +01:00
* @return \Interop\Async\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.
*/
2016-11-15 18:06:21 +01:00
function connect(string $connectionString, int $timeout = null): 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);
}
throw new \Error("This lib requires either pecl-pq or ext-pgsql");
}
/**
* @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);
}