|
1 | 1 | # Swoole Coroutine PostgreSQL Doctrine DBAL Driver |
2 | 2 |
|
3 | | -Fork of https://github.com/leocavalcante/swoole-postgresql-doctrine-driver to be used within Project Arca |
| 3 | +A `Doctrine\DBAL\Driver` implementation on top of `Swoole\Coroutine\PostgreSQL`. |
| 4 | +Requires ext-openswoole 4.7.1 or higher |
| 5 | + |
| 6 | +## Getting started |
| 7 | + |
| 8 | +### Install |
| 9 | + |
| 10 | +```shell |
| 11 | +composer require scrawler/swoole-postgresql-doctrine |
| 12 | +``` |
| 13 | + |
| 14 | +### Usage |
| 15 | + |
| 16 | +Doctrine parameters, for both DBAL and ORM projects, accepts the `driverClass` option; it is where we can inject this project's driver: |
| 17 | + |
| 18 | +```php |
| 19 | +use Doctrine\DBAL\{Driver, DriverManager}; |
| 20 | + |
| 21 | +$params = [ |
| 22 | + 'dbname' => 'postgres', |
| 23 | + 'user' => 'postgres', |
| 24 | + 'password' => 'postgres', |
| 25 | + 'host' => 'db', |
| 26 | + 'driverClass' =>\Scrawler\Swoole\PostgreSQL\Driver::class, |
| 27 | + 'poolSize' => 8, |
| 28 | +]; |
| 29 | + |
| 30 | +$conn = DriverManager::getConnection($params); |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +#### You are ready to rock inside Coroutines (Fibers): |
| 35 | + |
| 36 | +```php |
| 37 | +Co\run(static function() use ($conn): void { |
| 38 | + $results = []; |
| 39 | + $wg = new Co\WaitGroup(); |
| 40 | + $start_time = time(); |
| 41 | + |
| 42 | + Co::create(static function() use (&$results, $wg, $conn): void { |
| 43 | + $wg->add(); |
| 44 | + $results[] = $conn->executeQuery('select 1, pg_sleep(1)')->fetchOne(); |
| 45 | + $wg->done(); |
| 46 | + }); |
| 47 | + |
| 48 | + Co::create(static function() use (&$results, $wg, $conn): void { |
| 49 | + $wg->add(); |
| 50 | + $results[] = $conn->executeQuery('select 1, pg_sleep(1)')->fetchOne(); |
| 51 | + $wg->done(); |
| 52 | + }); |
| 53 | + |
| 54 | + $wg->wait(); |
| 55 | + $elapsed = time() - $start_time; |
| 56 | + $sum = array_sum($results); |
| 57 | + |
| 58 | + echo "Two pg_sleep(1) queries in $elapsed second, returning: $sum\n"; |
| 59 | +}); |
| 60 | +``` |
| 61 | + |
| 62 | +You should be seeing `Two pg_sleep(1) queries in 1 second, returning: 2` and the total time should **not** be 2 (the sum of `pg_sleep(1)`'s) because they ran concurrently. |
| 63 | + |
| 64 | +```shell |
| 65 | +real 0m1.228s |
| 66 | +user 0m0.036s |
| 67 | +sys 0m0.027s |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | +This Project has been possible because of https://github.com/leocavalcante/swoole-postgresql-doctrine-driver |
0 commit comments