|
1 | 1 | # Swoole Coroutine PostgreSQL Doctrine DBAL Driver |
2 | 2 |
|
3 | | -A `Doctrine\DBAL\Driver` implementation on top of `Swoole\Coroutine\PostgreSQL`. |
4 | | - |
5 | | -## Getting started |
6 | | - |
7 | | -### Install |
8 | | - |
9 | | -```shell |
10 | | -composer require leocavalcante/swoole-postgresql-doctrine-driver |
11 | | -``` |
12 | | - |
13 | | -### Usage |
14 | | - |
15 | | -Doctrine parameters, for both DBAL and ORM projects, accepts the `driverClass` option; it is where we can inject this project's driver: |
16 | | - |
17 | | -```php |
18 | | -use Doctrine\DBAL\{Driver, DriverManager}; |
19 | | - |
20 | | -$params = [ |
21 | | - 'dbname' => 'postgres', |
22 | | - 'user' => 'postgres', |
23 | | - 'password' => 'postgres', |
24 | | - 'host' => 'db', |
25 | | - 'driverClass' => Driver\Swoole\Coroutine\PostgreSQL\Driver::class, |
26 | | - 'poolSize' => 8, |
27 | | -]; |
28 | | - |
29 | | -$conn = DriverManager::getConnection($params); |
30 | | -``` |
31 | | - |
32 | | -*Yes, I deliberately used the `Doctrine\DBAL\Driver` namespace + `Swoole\Coroutine\PostgreSQL` namespace, so it is not confusing.* |
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 | | -## Developing |
71 | | - |
72 | | -### Use Composer through Docker |
73 | | - |
74 | | -```shell |
75 | | -docker-compose run --rm composer install |
76 | | -docker-compose run --rm composer test |
77 | | -``` |
78 | | - |
79 | | -It will build a development image with PHP, Swoole, Swoole's PostgreSQL extension and PCOV for coverage. |
| 3 | +Fork of https://github.com/leocavalcante/swoole-postgresql-doctrine-driver to be used within Project Arca |
0 commit comments