blob: 1e9af93c94a957066a4eedcd881098c6d2042d2e (
plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\SQLite3;
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Exception\NoIdentityValue;
use SQLite3;
use function assert;
use function sprintf;
final class Connection implements ConnectionInterface
{
/** @internal The connection can be only instantiated by its driver. */
public function __construct(private readonly SQLite3 $connection)
{
}
public function prepare(string $sql): Statement
{
try {
$statement = $this->connection->prepare($sql);
} catch (\Exception $e) {
throw Exception::new($e);
}
assert($statement !== false);
return new Statement($this->connection, $statement);
}
public function query(string $sql): Result
{
try {
$result = $this->connection->query($sql);
} catch (\Exception $e) {
throw Exception::new($e);
}
assert($result !== false);
return new Result($result, $this->connection->changes());
}
public function quote(string $value): string
{
return sprintf('\'%s\'', SQLite3::escapeString($value));
}
public function exec(string $sql): int
{
try {
$this->connection->exec($sql);
} catch (\Exception $e) {
throw Exception::new($e);
}
return $this->connection->changes();
}
public function lastInsertId(): int
{
$value = $this->connection->lastInsertRowID();
if ($value === 0) {
throw NoIdentityValue::new();
}
return $value;
}
public function beginTransaction(): void
{
try {
$this->connection->exec('BEGIN');
} catch (\Exception $e) {
throw Exception::new($e);
}
}
public function commit(): void
{
try {
$this->connection->exec('COMMIT');
} catch (\Exception $e) {
throw Exception::new($e);
}
}
public function rollBack(): void
{
try {
$this->connection->exec('ROLLBACK');
} catch (\Exception $e) {
throw Exception::new($e);
}
}
public function getNativeConnection(): SQLite3
{
return $this->connection;
}
public function getServerVersion(): string
{
return SQLite3::version()['versionString'];
}
}
|