|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Blrf\Dbal\Driver\Sqlite; |
| 6 | + |
| 7 | +use Blrf\Dbal\Config; |
| 8 | +use Blrf\Dbal\Result; |
| 9 | +use Blrf\Dbal\ResultStream; |
| 10 | +use Blrf\Dbal\Driver\Connection as DriverConnection; |
| 11 | +use Clue\React\SQLite\Factory; |
| 12 | +use Clue\React\SQLite\DatabaseInterface; |
| 13 | +use Clue\React\SQLite\Result as SqliteResult; |
| 14 | +use React\Promise\PromiseInterface; |
| 15 | + |
| 16 | +use function React\Promise\resolve; |
| 17 | + |
| 18 | +/** |
| 19 | + * React-Php Sqlite driver |
| 20 | + * |
| 21 | + * @see https://github.com/clue/reactphp-sqlite |
| 22 | + */ |
| 23 | +class Connection extends DriverConnection |
| 24 | +{ |
| 25 | + public function connect(): PromiseInterface |
| 26 | + { |
| 27 | + $factory = new Factory(); |
| 28 | + return $factory->open('/' . $this->config->getDb(), SQLITE3_OPEN_READWRITE)->then( |
| 29 | + function (DatabaseInterface $db) { |
| 30 | + return $this->setNativeConnection($db); |
| 31 | + } |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + public function query(): QueryBuilder |
| 36 | + { |
| 37 | + return new QueryBuilder($this); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Execute sql query |
| 42 | + * |
| 43 | + * @param array<int, string> $params |
| 44 | + * @return PromiseInterface<Result> |
| 45 | + */ |
| 46 | + public function execute(string $sql, array $params = []): PromiseInterface |
| 47 | + { |
| 48 | + // @phpstan-ignore-next-line |
| 49 | + return $this->getNativeConnection()->query($sql, $params)->then( |
| 50 | + function (SqliteResult $res) { |
| 51 | + return new Result( |
| 52 | + $res->rows ?? [], |
| 53 | + $res->insertId, |
| 54 | + $res->changed ?? 0, |
| 55 | + 0 |
| 56 | + ); |
| 57 | + } |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public function stream(string $sql, array $params = []): ResultStream |
| 62 | + { |
| 63 | + throw new \Exception('Stream not yet supported on sqlite'); |
| 64 | + } |
| 65 | + |
| 66 | + public function quit(): PromiseInterface |
| 67 | + { |
| 68 | + return $this->getNativeConnection()->quit(); |
| 69 | + } |
| 70 | +} |
0 commit comments