Skip to content

Commit d4e17e7

Browse files
authored
Merge pull request #527 from utopia-php/feat-pdo-proxy
Add PDO proxy
2 parents 00ad886 + d934b99 commit d4e17e7

File tree

13 files changed

+80
-20
lines changed

13 files changed

+80
-20
lines changed

bin/tasks/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Utopia\Database\Adapter\Mongo;
1414
use Utopia\Database\Adapter\MySQL;
1515
use Utopia\Database\Database;
16+
use Utopia\Database\PDO;
1617
use Utopia\Mongo\Client;
1718
use Utopia\Validator\Text;
1819

bin/tasks/load.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Utopia\Database\Document;
2121
use Utopia\Database\Helpers\Permission;
2222
use Utopia\Database\Helpers\Role;
23+
use Utopia\Database\PDO;
2324
use Utopia\Database\Validator\Authorization;
2425
use Utopia\Mongo\Client;
2526
use Utopia\Validator\Numeric;

composer.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Database/PDO.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Utopia\Database;
4+
5+
/**
6+
* A PDO wrapper that forwards method calls to the internal PDO instance.
7+
*
8+
* @mixin \PDO
9+
*/
10+
class PDO
11+
{
12+
protected \PDO $pdo;
13+
14+
/**
15+
* @param string $dsn
16+
* @param ?string $username
17+
* @param ?string $password
18+
* @param array<mixed> $config
19+
*/
20+
public function __construct(
21+
protected string $dsn,
22+
protected ?string $username,
23+
protected ?string $password,
24+
protected array $config = []
25+
) {
26+
$this->pdo = new \PDO(
27+
$this->dsn,
28+
$this->username,
29+
$this->password,
30+
$this->config
31+
);
32+
}
33+
34+
/**
35+
* @param string $method
36+
* @param array<mixed> $args
37+
* @return mixed
38+
*/
39+
public function __call(string $method, array $args): mixed
40+
{
41+
return $this->pdo->{$method}(...$args);
42+
}
43+
44+
/**
45+
* Create a new connection to the database
46+
*
47+
* @return void
48+
*/
49+
public function reconnect(): void
50+
{
51+
$this->pdo = new \PDO(
52+
$this->dsn,
53+
$this->username,
54+
$this->password,
55+
$this->config
56+
);
57+
}
58+
}

tests/e2e/Adapter/MariaDBTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Tests\E2E\Adapter;
44

5-
use PDO;
65
use Redis;
76
use Utopia\Cache\Adapter\Redis as RedisAdapter;
87
use Utopia\Cache\Cache;
98
use Utopia\Database\Adapter\MariaDB;
109
use Utopia\Database\Database;
10+
use Utopia\Database\PDO;
1111

1212
class MariaDBTest extends Base
1313
{

tests/e2e/Adapter/MirrorTest.php

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

33
namespace Tests\E2E\Adapter;
44

5-
use PDO;
65
use Redis;
76
use Utopia\Cache\Adapter\Redis as RedisAdapter;
87
use Utopia\Cache\Cache;
@@ -18,6 +17,7 @@
1817
use Utopia\Database\Helpers\Permission;
1918
use Utopia\Database\Helpers\Role;
2019
use Utopia\Database\Mirror;
20+
use Utopia\Database\PDO;
2121

2222
class MirrorTest extends Base
2323
{

tests/e2e/Adapter/MySQLTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Tests\E2E\Adapter;
44

5-
use PDO;
65
use Redis;
76
use Utopia\Cache\Adapter\Redis as RedisAdapter;
87
use Utopia\Cache\Cache;
98
use Utopia\Database\Adapter\MySQL;
109
use Utopia\Database\Database;
10+
use Utopia\Database\PDO;
1111

1212
class MySQLTest extends Base
1313
{

tests/e2e/Adapter/PostgresTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Tests\E2E\Adapter;
44

5-
use PDO;
65
use Redis;
76
use Utopia\Cache\Adapter\Redis as RedisAdapter;
87
use Utopia\Cache\Cache;
98
use Utopia\Database\Adapter\Postgres;
109
use Utopia\Database\Database;
10+
use Utopia\Database\PDO;
1111

1212
class PostgresTest extends Base
1313
{

tests/e2e/Adapter/SQLiteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Tests\E2E\Adapter;
44

5-
use PDO;
65
use Redis;
76
use Utopia\Cache\Adapter\Redis as RedisAdapter;
87
use Utopia\Cache\Cache;
98
use Utopia\Database\Adapter\SQLite;
109
use Utopia\Database\Database;
10+
use Utopia\Database\PDO;
1111

1212
class SQLiteTest extends Base
1313
{

tests/e2e/Adapter/SharedTables/MariaDBTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Tests\E2E\Adapter\SharedTables;
44

5-
use PDO;
65
use Redis;
76
use Tests\E2E\Adapter\Base;
87
use Utopia\Cache\Adapter\Redis as RedisAdapter;
98
use Utopia\Cache\Cache;
109
use Utopia\Database\Adapter\MariaDB;
1110
use Utopia\Database\Database;
11+
use Utopia\Database\PDO;
1212

1313
class MariaDBTest extends Base
1414
{

0 commit comments

Comments
 (0)