1
0
mirror of https://github.com/danog/postgres.git synced 2024-12-15 10:57:22 +01:00
postgres/lib/functions.php

40 lines
1.0 KiB
PHP
Raw Normal View History

2016-12-30 06:21:17 +01:00
<?php
2016-09-14 16:27:39 +02:00
namespace Amp\Postgres;
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
*
* @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.
*
* @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
}