-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLibsqlDatabase.php
More file actions
224 lines (175 loc) · 5.6 KB
/
LibsqlDatabase.php
File metadata and controls
224 lines (175 loc) · 5.6 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
declare(strict_types=1);
namespace Libsql\Laravel\Database;
use Libsql\Connection;
use Libsql\Database;
use Libsql\Transaction;
class LibsqlDatabase
{
protected Connection $db;
protected Database $conn;
private ?Transaction $tx;
private string $connection_mode;
private bool $in_transaction = false;
private array $lastInsertIds = [];
private int $mode = \PDO::FETCH_ASSOC;
public function __construct(array $config)
{
$config = $this->createConfig($config);
$connectionMode = $this->detectConnectionMode($config);
$this->db = $this->buildConnection($connectionMode, $config);
$this->in_transaction = false;
}
private function createConfig(array $config): array
{
return [
'path' => $config['database'] ?? null,
'url' => $config['url'] ?? null,
'authToken' => $config['password'] ?? null,
'encryptionKey' => $config['encryptionKey'] ?? null,
'syncInterval' => $config['syncInterval'] ?? 0,
'disable_read_your_writes' => $config['read_your_writes'] ?? true,
'webpki' => $config['webpki'] ?? false,
];
}
private function buildConnection(string $mode, array $config): Connection
{
$db = match ($mode) {
'local' => new Database(path: $config['path']),
'remote' => new Database(url: $config['url'], authToken: $config['authToken']),
'remote_replica' => new Database(
path: $config['path'],
url: $config['url'],
authToken: $config['authToken'],
syncInterval: $config['syncInterval'],
readYourWrites: $config['disable_read_your_writes'],
webpki: $config['webpki']
),
default => new Database(':memory:')
};
return $db->connect();
}
private function detectConnectionMode(array $config): string
{
$database = $config['path'];
$url = $config['url'];
$mode = 'unknown';
if ($database === ':memory:' || empty($url)) {
$mode = 'memory';
} elseif (empty($database) && !empty($url)) {
$mode = 'remote';
} elseif (!empty($database) && empty($url)) {
$mode = 'local';
} elseif (!empty($database) && !empty($url)) {
$mode = 'remote_replica';
}
$this->connection_mode = $mode;
return $mode;
}
public function version(): string
{
return '3.45.1';
}
public function inTransaction(): bool
{
return $this->in_transaction;
}
public function sync(): void
{
if ($this->connection_mode !== 'remote_replica') {
throw new \Exception("[Libsql:{$this->connection_mode}] Sync is only available for Remote Replica Connection.", 1);
}
$this->conn->sync();
}
public function getConnectionMode(): string
{
return $this->connection_mode;
}
public function setFetchMode(int $mode, mixed ...$args): bool
{
$this->mode = $mode;
return true;
}
public function beginTransaction(): bool
{
if ($this->inTransaction()) {
throw new \PDOException('Already in a transaction');
}
$this->in_transaction = true;
$this->tx = $this->db->transaction();
return true;
}
public function prepare(string $sql): LibsqlStatement
{
return new LibsqlStatement(
($this->inTransaction() ? $this->tx : $this->db)->prepare($sql),
$sql
);
}
public function exec(string $queryStatement): int
{
$statement = $this->prepare($queryStatement);
$statement->execute();
return $statement->rowCount();
}
public function query(string $sql, array $params = [])
{
$results = $this->db->query($sql, $params)->fetchArray();
$rowValues = array_values($results);
return match ($this->mode) {
\PDO::FETCH_BOTH => array_merge($results, $rowValues),
\PDO::FETCH_ASSOC, \PDO::FETCH_NAMED => $results,
\PDO::FETCH_NUM => $rowValues,
\PDO::FETCH_OBJ => $results,
default => throw new \PDOException('Unsupported fetch mode.'),
};
}
public function setLastInsertId(?string $name = null, ?int $value = null): void
{
if ($name === null) {
$name = 'id';
}
$this->lastInsertIds[$name] = $value;
}
public function lastInsertId(?string $name = null): int|string
{
if ($name === null) {
$name = 'id';
}
return isset($this->lastInsertIds[$name])
? (string) $this->lastInsertIds[$name]
: $this->db->lastInsertId();
}
public function escapeString($input)
{
if ($input === null) {
return 'NULL';
}
return \SQLite3::escapeString($input);
}
public function quote($input)
{
if ($input === null) {
return 'NULL';
}
return "'" . $this->escapeString($input) . "'";
}
public function commit(): bool
{
if (!$this->inTransaction()) {
throw new \PDOException('No active transaction');
}
$this->tx->commit();
$this->in_transaction = false;
return true;
}
public function rollBack(): bool
{
if (!$this->inTransaction()) {
throw new \PDOException('No active transaction');
}
$this->tx->rollback();
$this->in_transaction = false;
return true;
}
}