|
| 1 | +# Drivers |
| 2 | + |
| 3 | +Database drivers adapt the core database contracts to a real database engine or client library. |
| 4 | + |
| 5 | +The core package does not include a concrete database engine driver. |
| 6 | + |
| 7 | +## Contract |
| 8 | + |
| 9 | +Drivers implement `DatabaseDriverInterface`. |
| 10 | + |
| 11 | +```php |
| 12 | +namespace CommonPHP\Database\Contracts; |
| 13 | + |
| 14 | +interface DatabaseDriverInterface extends DriverInterface |
| 15 | +{ |
| 16 | + public function count(string $query, array $parameters = []): int; |
| 17 | + public function execute(string $query, array $parameters = []): int|bool; |
| 18 | + public function fetchScalar(string $query, array $parameters = [], mixed $default = null): mixed; |
| 19 | + public function fetchOne(string $query, array $parameters = []): array|false; |
| 20 | + public function fetchAll(string $query, array $parameters = [], FetchMode $fetchMode = FetchMode::FETCH_ASSOC): array; |
| 21 | + public function transaction(callable $callback): mixed; |
| 22 | + public function beginTransaction(): void; |
| 23 | + public function commit(): void; |
| 24 | + public function rollBack(): void; |
| 25 | + public function lastInsertId(): string|false; |
| 26 | + public function ping(): bool; |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## AbstractDatabaseDriver |
| 31 | + |
| 32 | +`AbstractDatabaseDriver` provides useful defaults: |
| 33 | + |
| 34 | +- `getName()` returns `static::class`; |
| 35 | +- `prepare()` creates an executable `Query`; |
| 36 | +- `count()` counts `fetchAll()` results; |
| 37 | +- `fetchScalar()` reads the first value from `fetchOne()`; |
| 38 | +- `transaction()` delegates to `Transaction::run()`; |
| 39 | +- protected `parameterType()` detects named, positional, or empty parameters; |
| 40 | +- protected `fetchModeValue()` returns the enum value. |
| 41 | + |
| 42 | +Drivers can override defaults when the underlying engine has a more efficient implementation. |
| 43 | + |
| 44 | +## Minimal Driver Shape |
| 45 | + |
| 46 | +```php |
| 47 | +use CommonPHP\Database\Contracts\AbstractDatabaseDriver; |
| 48 | +use CommonPHP\Database\Enums\FetchMode; |
| 49 | + |
| 50 | +final class ExampleDatabaseDriver extends AbstractDatabaseDriver |
| 51 | +{ |
| 52 | + public function __construct( |
| 53 | + private readonly ExampleClient $client, |
| 54 | + ) { |
| 55 | + } |
| 56 | + |
| 57 | + public function execute(string $query, array $parameters = []): int|bool |
| 58 | + { |
| 59 | + return $this->client->execute($query, $parameters); |
| 60 | + } |
| 61 | + |
| 62 | + public function fetchOne(string $query, array $parameters = []): array|false |
| 63 | + { |
| 64 | + return $this->client->fetchOne($query, $parameters); |
| 65 | + } |
| 66 | + |
| 67 | + public function fetchAll( |
| 68 | + string $query, |
| 69 | + array $parameters = [], |
| 70 | + FetchMode $fetchMode = FetchMode::FETCH_ASSOC, |
| 71 | + ): array { |
| 72 | + return $this->client->fetchAll($query, $parameters, $fetchMode->value); |
| 73 | + } |
| 74 | + |
| 75 | + public function beginTransaction(): void |
| 76 | + { |
| 77 | + $this->client->beginTransaction(); |
| 78 | + } |
| 79 | + |
| 80 | + public function commit(): void |
| 81 | + { |
| 82 | + $this->client->commit(); |
| 83 | + } |
| 84 | + |
| 85 | + public function rollBack(): void |
| 86 | + { |
| 87 | + $this->client->rollBack(); |
| 88 | + } |
| 89 | + |
| 90 | + public function lastInsertId(): string|false |
| 91 | + { |
| 92 | + return $this->client->lastInsertId(); |
| 93 | + } |
| 94 | + |
| 95 | + public function ping(): bool |
| 96 | + { |
| 97 | + return $this->client->ping(); |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Driver Responsibilities |
| 103 | + |
| 104 | +Drivers should own: |
| 105 | + |
| 106 | +- connection strings and client setup; |
| 107 | +- parameter binding; |
| 108 | +- engine-specific fetch mode mapping; |
| 109 | +- transaction calls; |
| 110 | +- driver-specific exception conversion; |
| 111 | +- any query compilation that belongs to that engine. |
| 112 | + |
| 113 | +Drivers should not own: |
| 114 | + |
| 115 | +- application runtime bootstrapping; |
| 116 | +- HTTP request parsing; |
| 117 | +- validation; |
| 118 | +- authentication; |
| 119 | +- session lifecycle; |
| 120 | +- UI rendering. |
0 commit comments