makise-co/postgres

基于libpq的纯协程PHP PostgreSQL客户端

v2.0.7 2021-10-13 15:42 UTC

README

基于libpq的纯PHP协程客户端,用于PostgreSQL

灵感来自 amphp/postgres

安装

该软件包可以作为 Composer 依赖安装。

composer require makise-co/postgres

要求

  • PHP 7.4+
  • Swoole 4.4+
  • 对于 PqConnection 需要 ext-pq
  • 对于 PgSqlConnection 需要 ext-pgsql
  • 对于 SwooleConnection 需要 ext-swoole_postgresql

支持的底层驱动程序

基准测试

* (原始) 表示这是一个没有任何抽象和PHP代码执行的基准测试。

* 在这个基准测试中,PgSql (原始) 比Pgsql快得多,因为没有代码将结果转换为原生PHP类型。

所有基准测试都可以在 benchmark 目录中找到。

文档 & 示例

预处理语句和参数化查询支持命名占位符,以及 ? 和标准数字(例如 $1)占位符。

更多示例可以在 examples 目录中找到。

<?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']
    }
});

许可

MIT许可(MIT)。有关更多信息,请参阅 LICENSE