makise-co / postgres
A Pure coroutine PHP client for PostgreSQL based on libpq
Requires
- php: >=7.4
- ext-swoole: >=4.4
- makise-co/sql-common: ^1.0
Requires (Dev)
- phpstan/phpstan: ^0.12.40
- phpunit/phpunit: ^9.1
- swoole/ide-helper: ^4.5
- symfony/var-dumper: ^5.0
Suggests
- ext-pgsql: To work with PgSql connection driver
- ext-pq: To work with Pq connection driver
- ext-swoole_postgres: To work with Swoole connection driver
This package is auto-updated.
Last update: 2024-11-13 22:01:29 UTC
README
A Pure PHP coroutine client for PostgreSQL based on libpq
Inspired by amphp/postgres
Installation
This package can be installed as a Composer dependency.
composer require makise-co/postgres
Requirements
- PHP 7.4+
- Swoole 4.4+
- ext-pq for
PqConnection
- ext-pgsql for
PgSqlConnection
- ext-swoole_postgresql for
SwooleConnection
Supported underlying drivers
Benchmarks
* (raw) means that is a benchmark without any abstractions and PHP code execution.
* The PgSql (raw) is much faster than Pgsql in this benchmark because there is no code to convert results to native PHP types.
All benchmarks can be found in the benchmark
directory.
Documentation & Examples
Prepared statements and parameterized queries support named placeholders, as well as ?
and standard numeric (i.e. $1
) placeholders.
More examples can be found in the examples
directory.
<?php declare(strict_types=1); use MakiseCo\Postgres\ConnectionConfig; use MakiseCo\Postgres\ConnectionConfigBuilder; use MakiseCo\Postgres\Driver\Pq\PqConnection; use MakiseCo\Postgres\Driver\PgSql\PgSqlConnection; use MakiseCo\Postgres\Driver\Swoole\SwooleConnection; use MakiseCo\SqlCommon\Contracts\ResultSet; use function Swoole\Coroutine\run; run(static function () { $config = (new ConnectionConfigBuilder()) ->withHost('127.0.0.1') ->withPort(5432) ->withUser('makise') ->withPassword('el-psy-congroo') ->withDatabase('cern') ->withSslMode('prefer') ->withEncoding('utf-8') ->withApplicationName('Makise Postgres Driver') ->withSearchPath(['public']) ->withTimezone('UTC') ->withConnectTimeout(1.0) // wait 1 second ->build(); // or: $config = (new ConnectionConfigBuilder()) ->fromArray([ 'host' => '127.0.0.1', 'port' => 5432, 'user' => 'makise', 'password' => 'el-psy-congroo', 'database' => 'cern', 'sslmode' => 'prefer', 'client_encoding' => 'utf-8', // or 'encoding' => 'utf-8', // or 'charset' => 'utf-8', 'application_name' => 'Makise Postgres Driver', 'search_path' => 'public', // array of strings can be passed // or 'schema' => 'public', // array of strings can be passed 'timezone' => 'UTC', 'connect_timeout' => 1.0, ]) ->build(); // or $config = new ConnectionConfig( '127.0.0.1', 5432, 'makise', 'el-psy-congroo', 'makise', [ 'sslmode' => 'prefer', 'client_encoding' => 'utf-8', 'application_name' => 'Makise Postgres Driver', 'options' => [ 'search_path' => 'public', 'timezone' => 'UTC', ], ], 1.0, ); $connection = PqConnection::connect($config); // or $connection = PgSqlConnection::connect($config); // or $connection = SwooleConnection::connect($config); $statement = $connection->prepare("SELECT * FROM test WHERE id = :id"); /** @var ResultSet $result */ $result = $statement->execute(['id' => 1337]); while ($row = $result->fetchAssoc()) { // $row is an array (map) of column values. e.g.: $row['column_name'] } });
License
The MIT License (MIT). Please see LICENSE
for more information.