-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSQLiteAdapter.php
49 lines (40 loc) · 1.36 KB
/
SQLiteAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace App\Database\Connection;
use Phractico\Core\Infrastructure\Database\Connection;
use Phractico\Core\Infrastructure\Database\Query\Statement;
use Phractico\Core\Infrastructure\Database\Result;
class SQLiteAdapter implements Connection
{
private \SQLite3 $connection;
public function __construct(private readonly string $databaseFilePath) {}
public function connect(): void
{
$this->connection = new \SQLite3($this->databaseFilePath);
}
public function close(): void
{
$this->connection->close();
}
public function performStatement(Statement $statement): Result
{
$stmt = $statement->toArray();
$sqliteStatement = $this->connection->prepare($stmt['sql']);
foreach ($stmt['params'] as $key => $value) {
$sqliteStatement->bindValue($key, $value);
}
$result = $sqliteStatement->execute();
if ($result && $stmt['has_returning_results']) {
return $this->prepareReturningResult($result);
}
return Result::empty();
}
private function prepareReturningResult(\SQLite3Result $statementExecutionResult): Result
{
$result = [];
while ($row = $statementExecutionResult->fetchArray(SQLITE3_ASSOC)) {
$result[] = $row;
}
return Result::from($result);
}
}