Skip to content

Commit 94d0ec0

Browse files
committed
Support additional databases through adapters
1 parent 835fe23 commit 94d0ec0

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

src/Adapter/Postgres.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace WyriHaximus\React\SimpleORM\Adapter;
4+
5+
use PgAsync\Client as PgClient;
6+
use Plasma\SQL\Grammar\PostgreSQL;
7+
use Plasma\SQL\GrammarInterface;
8+
use Plasma\SQL\QueryBuilder;
9+
use Rx\Observable;
10+
use WyriHaximus\React\SimpleORM\AdapterInterface;
11+
12+
final class Postgres implements AdapterInterface
13+
{
14+
/** @var PgClient */
15+
private $client;
16+
17+
public function __construct(PgClient $client)
18+
{
19+
$this->client = $client;
20+
}
21+
22+
public function query(QueryBuilder $query): Observable
23+
{
24+
return $this->client->executeStatement($query->getQuery(), $query->getParameters());
25+
}
26+
27+
public function getGrammar(): GrammarInterface
28+
{
29+
return new PostgreSQL();
30+
}
31+
}

src/AdapterInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace WyriHaximus\React\SimpleORM;
4+
5+
use Plasma\SQL\GrammarInterface;
6+
use Plasma\SQL\QueryBuilder;
7+
use Rx\Observable;
8+
9+
interface AdapterInterface
10+
{
11+
public function query(QueryBuilder $query): Observable;
12+
13+
public function getGrammar(): GrammarInterface;
14+
}

src/Client.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
final class Client implements ClientInterface
1818
{
19-
/** @var PgClient */
20-
private $client;
19+
/** @var AdapterInterface */
20+
private $adapter;
2121

2222
/** @var EntityInspector */
2323
private $entityInspector;
@@ -31,28 +31,28 @@ final class Client implements ClientInterface
3131
/**
3232
* @param array<int, MiddlewareInterface> $middleware
3333
*/
34-
public static function create(PgClient $client, MiddlewareInterface ...$middleware): self
34+
public static function create(AdapterInterface $adapter, MiddlewareInterface ...$middleware): self
3535
{
36-
return new self($client, new AnnotationReader(), ...$middleware);
36+
return new self($adapter, new AnnotationReader(), ...$middleware);
3737
}
3838

3939
/**
4040
* @param array<int, MiddlewareInterface> $middleware
4141
*/
42-
public static function createWithAnnotationReader(PgClient $client, Reader $annotationReader, MiddlewareInterface ...$middleware): self
42+
public static function createWithAnnotationReader(AdapterInterface $adapter, Reader $annotationReader, MiddlewareInterface ...$middleware): self
4343
{
44-
return new self($client, $annotationReader, ...$middleware);
44+
return new self($adapter, $annotationReader, ...$middleware);
4545
}
4646

4747
/**
4848
* @param array<int, MiddlewareInterface> $middleware
4949
*/
50-
private function __construct(PgClient $client, Reader $annotationReader, MiddlewareInterface ...$middleware)
50+
private function __construct(AdapterInterface $adapter, Reader $annotationReader, MiddlewareInterface ...$middleware)
5151
{
52-
$this->client = $client;
52+
$this->adapter = $adapter;
5353
$this->entityInspector = new EntityInspector($annotationReader);
5454

55-
$middleware[] = new GrammarMiddleware(new PostgreSQL());
55+
$middleware[] = new GrammarMiddleware($adapter->getGrammar());
5656

5757
$this->middlewareRunner = new MiddlewareRunner(...$middleware);
5858
}
@@ -72,7 +72,7 @@ public function query(QueryBuilder $query): Observable
7272
$query,
7373
function (QueryBuilder $query): PromiseInterface
7474
{
75-
return resolve($this->client->executeStatement($query->getQuery(), $query->getParameters()));
75+
return resolve($this->adapter->query($query));
7676
}
7777
));
7878
}

tests/ClientTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace WyriHaximus\React\Tests\SimpleORM;
44

5+
use WyriHaximus\React\SimpleORM\Adapter\Postgres;
56
use function ApiClients\Tools\Rx\observableFromArray;
67
use Doctrine\Common\Annotations\Reader;
78
use PgAsync\Client as PgClient;
@@ -40,7 +41,7 @@ protected function setUp(): void
4041

4142
$this->pgClient = $this->prophesize(PgClient::class);
4243
$this->annotationReader = $this->prophesize(Reader::class);
43-
$this->client = Client::createWithAnnotationReader($this->pgClient->reveal(), $this->annotationReader->reveal());
44+
$this->client = Client::createWithAnnotationReader(new Postgres($this->pgClient->reveal()), $this->annotationReader->reveal());
4445
}
4546

4647
public function testGetRepository(): void

tests/FunctionalTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use React\EventLoop\Factory;
77
use React\EventLoop\LoopInterface;
88
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
9+
use WyriHaximus\React\SimpleORM\Adapter\Postgres;
910
use WyriHaximus\React\SimpleORM\Client;
1011
use WyriHaximus\React\SimpleORM\ClientInterface;
1112
use WyriHaximus\React\SimpleORM\Middleware\QueryCountMiddleware;
@@ -45,15 +46,17 @@ protected function setUp(): void
4546

4647
$this->loop = Factory::create();
4748
$this->client = Client::create(
48-
new PgClient(
49-
[
50-
'host' => 'localhost',
51-
'port' => 5432,
52-
'user' => \getenv('PHINX_DB_USER'),
53-
'password' => \getenv('PHINX_DB_PASSWORD'),
54-
'database' => \getenv('PHINX_DB_DATABASE'),
55-
],
56-
$this->loop
49+
new Postgres(
50+
new PgClient(
51+
[
52+
'host' => 'localhost',
53+
'port' => 5432,
54+
'user' => \getenv('PHINX_DB_USER'),
55+
'password' => \getenv('PHINX_DB_PASSWORD'),
56+
'database' => \getenv('PHINX_DB_DATABASE'),
57+
],
58+
$this->loop
59+
)
5760
),
5861
$this->counter
5962
);

0 commit comments

Comments
 (0)