Skip to content

Commit

Permalink
fix the PostgreSQL example using the PDO_PGSQL driver instead of the …
Browse files Browse the repository at this point in the history
…removed PostgreSQL client in Swoole v6

Signed-off-by: Demin Yin <[email protected]>
  • Loading branch information
deminy committed Jan 1, 2025
1 parent ecc41aa commit 3d6f922
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions examples/clients/postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
declare(strict_types=1);

/**
* This example shows how to interact with PostgreSQL.
* This example shows how to interact with PostgreSQL using the PDO_PGSQL driver.
*
* In this example, we make five PostgreSQL connections, and perform a three-second query in each connection. It takes
* barely over three seconds to run this script.
*
* The PostgreSQL client is added to Swoole in 5.0.0. This example won't work with old versions of Swoole.
* The PDO_PGSQL driver is supported in Swoole since v5.1.0. This example won't work with old versions of Swoole.
*
* How to run this script:
* docker compose exec -t client bash -c "./clients/postgresql.php"
Expand All @@ -18,23 +18,21 @@
* docker compose exec -t client bash -c "time ./clients/postgresql.php"
*/

use Swoole\Constant;
use Swoole\Coroutine;
use Swoole\Coroutine\PostgreSQL;

use function Swoole\Coroutine\go;
use function Swoole\Coroutine\run;

// This statement is optional because hook flags are set to SWOOLE_HOOK_ALL by default, and flag SWOOLE_HOOK_ALL has
// flag SWOOLE_HOOK_PDO_PGSQL included already.
Coroutine::set([Constant::OPTION_HOOK_FLAGS => SWOOLE_HOOK_PDO_PGSQL]);

run(function (): void {
$connections = [];
for ($i = 0; $i < 5; $i++) {
$connection = new PostgreSQL();
$connection->connect('host=postgresql port=5432 dbname=test user=username password=password');
$connections[] = $connection;
}

/** @var PostgreSQL $connection */
foreach ($connections as $connection) {
Coroutine::create(function () use ($connection): void {
$stmt = $connection->prepare('SELECT pg_sleep(3)');
go(function (): void {
$pdo = new PDO('pgsql:host=postgresql;port=5432;dbname=test', 'username', 'password');
$stmt = $pdo->prepare('SELECT pg_sleep(3)');
if ($stmt === false) {
echo 'Failed to prepare the statement.', PHP_EOL;
return;
Expand All @@ -46,8 +44,11 @@
// [
// [
// 'pg_sleep' => '',
// 0 => '',
// ]
// ];
$stmt = null;
$pdo = null;
});
}
});

0 comments on commit 3d6f922

Please sign in to comment.