|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under |
|
5
|
|
|
* the terms of the GPL-3 license. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Konrad Abicht <[email protected]> |
|
8
|
|
|
* (c) Benjamin Nowack |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace sweetrdf\InMemoryStoreSqlite; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use PDO; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* PDO SQLite adapter. |
|
21
|
|
|
*/ |
|
22
|
|
|
class PDOSQLiteAdapter |
|
23
|
|
|
{ |
|
24
|
|
|
private ?\PDO $db; |
|
25
|
|
|
|
|
26
|
|
|
private int $lastRowCount = 0; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Sent queries. |
|
30
|
|
|
*/ |
|
31
|
|
|
private array $queries = []; |
|
32
|
|
|
|
|
33
|
103 |
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
103 |
|
$this->checkRequirements(); |
|
36
|
|
|
|
|
37
|
|
|
// use in-memory |
|
38
|
103 |
|
$dsn = 'sqlite::memory:'; |
|
39
|
|
|
|
|
40
|
103 |
|
$this->db = new PDO($dsn); |
|
41
|
|
|
|
|
42
|
103 |
|
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
43
|
|
|
|
|
44
|
|
|
// errors lead to exceptions |
|
45
|
103 |
|
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
46
|
|
|
|
|
47
|
|
|
// default fetch mode is associative |
|
48
|
103 |
|
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); |
|
49
|
|
|
|
|
50
|
|
|
/* |
|
51
|
|
|
* These PRAGMAs may speed up insert operations a bit. |
|
52
|
|
|
* Because database runs exclusively in memory for a process |
|
53
|
|
|
* journal mode etc. is not relevant. |
|
54
|
|
|
*/ |
|
55
|
103 |
|
$this->db->query('PRAGMA synchronous = OFF;'); |
|
56
|
103 |
|
$this->db->query('PRAGMA journal_mode = OFF;'); |
|
57
|
103 |
|
$this->db->query('PRAGMA locking_mode = EXCLUSIVE;'); |
|
58
|
103 |
|
$this->db->query('PRAGMA page_size = 4096;'); |
|
59
|
|
|
|
|
60
|
|
|
/* |
|
61
|
|
|
* define CONCAT function (otherwise SQLite will throw an exception) |
|
62
|
|
|
*/ |
|
63
|
103 |
|
$this->db->sqliteCreateFunction('CONCAT', function ($pattern, $string) { |
|
64
|
2 |
|
$result = ''; |
|
65
|
|
|
|
|
66
|
2 |
|
foreach (\func_get_args() as $str) { |
|
67
|
2 |
|
$result .= $str; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
return $result; |
|
71
|
103 |
|
}); |
|
72
|
|
|
|
|
73
|
|
|
/* |
|
74
|
|
|
* define REGEXP function (otherwise SQLite will throw an exception) |
|
75
|
|
|
*/ |
|
76
|
103 |
|
$this->db->sqliteCreateFunction('REGEXP', function ($pattern, $string) { |
|
77
|
1 |
|
if (0 < preg_match('/'.$pattern.'/i', $string)) { |
|
78
|
1 |
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
return false; |
|
82
|
103 |
|
}, 2); |
|
83
|
|
|
|
|
84
|
103 |
|
$this->createTables(); |
|
85
|
103 |
|
} |
|
86
|
|
|
|
|
87
|
103 |
|
public function checkRequirements() |
|
88
|
|
|
{ |
|
89
|
103 |
|
if (false == \extension_loaded('pdo_sqlite')) { |
|
|
|
|
|
|
90
|
|
|
throw new Exception('Extension pdo_sqlite is not loaded.'); |
|
91
|
|
|
} |
|
92
|
103 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function deleteAllTables(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$this->exec( |
|
97
|
|
|
'SELECT "drop table " || name || ";" |
|
98
|
|
|
FROM sqlite_master |
|
99
|
|
|
WHERE type = "table";' |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Creates all required tables. |
|
105
|
|
|
*/ |
|
106
|
103 |
|
private function createTables(): void |
|
107
|
|
|
{ |
|
108
|
|
|
// triple |
|
109
|
103 |
|
$sql = 'CREATE TABLE IF NOT EXISTS triple ( |
|
110
|
|
|
t INTEGER PRIMARY KEY AUTOINCREMENT, |
|
111
|
|
|
s INTEGER UNSIGNED NOT NULL, |
|
112
|
|
|
p INTEGER UNSIGNED NOT NULL, |
|
113
|
|
|
o INTEGER UNSIGNED NOT NULL, |
|
114
|
|
|
o_lang_dt INTEGER UNSIGNED NOT NULL, |
|
115
|
|
|
o_comp TEXT NOT NULL, -- normalized value for ORDER BY operations |
|
116
|
|
|
s_type INTEGER UNSIGNED NOT NULL DEFAULT 0, -- uri/bnode => 0/1 |
|
117
|
|
|
o_type INTEGER UNSIGNED NOT NULL DEFAULT 0 -- uri/bnode/literal => 0/1/2 |
|
118
|
|
|
)'; |
|
119
|
|
|
|
|
120
|
103 |
|
$this->exec($sql); |
|
121
|
|
|
|
|
122
|
|
|
// g2t |
|
123
|
103 |
|
$sql = 'CREATE TABLE IF NOT EXISTS g2t ( |
|
124
|
|
|
g INTEGER UNSIGNED NOT NULL, |
|
125
|
|
|
t INTEGER UNSIGNED NOT NULL, |
|
126
|
|
|
UNIQUE (g,t) |
|
127
|
|
|
)'; |
|
128
|
|
|
|
|
129
|
103 |
|
$this->exec($sql); |
|
130
|
|
|
|
|
131
|
|
|
// id2val |
|
132
|
103 |
|
$sql = 'CREATE TABLE IF NOT EXISTS id2val ( |
|
133
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT, |
|
134
|
|
|
val TEXT NOT NULL, |
|
135
|
|
|
val_type INTEGER NOT NULL DEFAULT 0, -- uri/bnode/literal => 0/1/2 |
|
136
|
|
|
UNIQUE (id,val_type) |
|
137
|
|
|
)'; |
|
138
|
|
|
|
|
139
|
103 |
|
$this->exec($sql); |
|
140
|
|
|
|
|
141
|
|
|
// s2val |
|
142
|
103 |
|
$sql = 'CREATE TABLE IF NOT EXISTS s2val ( |
|
143
|
|
|
id INTEGER UNSIGNED NOT NULL, |
|
144
|
|
|
val_hash TEXT NOT NULL, |
|
145
|
|
|
val TEXT NOT NULL, |
|
146
|
|
|
UNIQUE (id) |
|
147
|
|
|
)'; |
|
148
|
|
|
|
|
149
|
103 |
|
$this->exec($sql); |
|
150
|
|
|
|
|
151
|
|
|
// o2val |
|
152
|
103 |
|
$sql = 'CREATE TABLE IF NOT EXISTS o2val ( |
|
153
|
|
|
id INTEGER NOT NULL, |
|
154
|
|
|
val_hash TEXT NOT NULL, |
|
155
|
|
|
val TEXT NOT NULL, |
|
156
|
|
|
UNIQUE (id) |
|
157
|
|
|
)'; |
|
158
|
|
|
|
|
159
|
103 |
|
$this->exec($sql); |
|
160
|
103 |
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* It gets all tables from the current database. |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function getAllTables(): array |
|
166
|
|
|
{ |
|
167
|
1 |
|
$tables = $this->fetchList('SELECT name FROM sqlite_master WHERE type="table";'); |
|
168
|
1 |
|
$result = []; |
|
169
|
1 |
|
foreach ($tables as $table) { |
|
170
|
|
|
// ignore SQLite tables |
|
171
|
1 |
|
if (false !== strpos($table['name'], 'sqlite_')) { |
|
172
|
1 |
|
continue; |
|
173
|
|
|
} |
|
174
|
1 |
|
$result[] = $table['name']; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
1 |
|
return $result; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
2 |
|
public function getServerVersion() |
|
181
|
|
|
{ |
|
182
|
2 |
|
return $this->fetchRow('select sqlite_version()')['sqlite_version()']; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function getAffectedRows(): int |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->lastRowCount; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return void |
|
192
|
|
|
*/ |
|
193
|
1 |
|
public function disconnect() |
|
194
|
|
|
{ |
|
195
|
|
|
// FYI: https://stackoverflow.com/questions/18277233/pdo-closing-connection |
|
196
|
1 |
|
$this->db = null; |
|
197
|
1 |
|
} |
|
198
|
|
|
|
|
199
|
20 |
|
public function escape($value) |
|
200
|
|
|
{ |
|
201
|
20 |
|
$quoted = $this->db->quote($value); |
|
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
/* |
|
204
|
|
|
* fixes the case, that we have double quoted strings like: |
|
205
|
|
|
* ''x1'' |
|
206
|
|
|
* |
|
207
|
|
|
* remember, this value will be surrounded by quotes later on! |
|
208
|
|
|
* so we don't send it back with quotes around. |
|
209
|
|
|
*/ |
|
210
|
20 |
|
if ("'" == substr($quoted, 0, 1)) { |
|
211
|
20 |
|
$quoted = substr($quoted, 1, \strlen($quoted) - 2); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
20 |
|
return $quoted; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
87 |
|
public function fetchList(string $sql, array $params = []): array |
|
218
|
|
|
{ |
|
219
|
|
|
// save query |
|
220
|
87 |
|
$this->queries[] = [ |
|
221
|
87 |
|
'query' => $sql, |
|
222
|
87 |
|
'by_function' => 'fetchList', |
|
223
|
|
|
]; |
|
224
|
|
|
|
|
225
|
87 |
|
$stmt = $this->db->prepare($sql); |
|
226
|
86 |
|
$stmt->execute($params); |
|
227
|
86 |
|
$rows = $stmt->fetchAll(); |
|
228
|
86 |
|
$stmt->closeCursor(); |
|
229
|
|
|
|
|
230
|
86 |
|
return $rows; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return bool|array |
|
235
|
|
|
*/ |
|
236
|
88 |
|
public function fetchRow(string $sql, array $params = []) |
|
237
|
|
|
{ |
|
238
|
|
|
// save query |
|
239
|
88 |
|
$this->queries[] = [ |
|
240
|
88 |
|
'query' => $sql, |
|
241
|
88 |
|
'by_function' => 'fetchRow', |
|
242
|
|
|
]; |
|
243
|
|
|
|
|
244
|
88 |
|
$row = false; |
|
245
|
88 |
|
$stmt = $this->db->prepare($sql); |
|
246
|
88 |
|
$stmt->execute($params); |
|
247
|
88 |
|
$rows = $stmt->fetchAll(); |
|
248
|
88 |
|
if (0 < \count($rows)) { |
|
249
|
83 |
|
$row = array_values($rows)[0]; |
|
250
|
|
|
} |
|
251
|
88 |
|
$stmt->closeCursor(); |
|
252
|
|
|
|
|
253
|
88 |
|
return $row; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
77 |
|
public function getPDO() |
|
257
|
|
|
{ |
|
258
|
77 |
|
return $this->db; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
public function getErrorCode() |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->db->errorCode(); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
13 |
|
public function getErrorMessage() |
|
267
|
|
|
{ |
|
268
|
13 |
|
return $this->db->errorInfo()[2]; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
public function getLastInsertId() |
|
272
|
|
|
{ |
|
273
|
|
|
return $this->db->lastInsertId(); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
8 |
|
public function getNumberOfRows($sql) |
|
277
|
|
|
{ |
|
278
|
|
|
// save query |
|
279
|
8 |
|
$this->queries[] = [ |
|
280
|
8 |
|
'query' => $sql, |
|
281
|
8 |
|
'by_function' => 'getNumberOfRows', |
|
282
|
|
|
]; |
|
283
|
|
|
|
|
284
|
8 |
|
$stmt = $this->db->prepare($sql); |
|
285
|
7 |
|
$stmt->execute(); |
|
286
|
7 |
|
$rowCount = \count($stmt->fetchAll()); |
|
287
|
7 |
|
$stmt->closeCursor(); |
|
288
|
|
|
|
|
289
|
7 |
|
return $rowCount; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
85 |
|
public function simpleQuery(string $sql, array $params = []): bool |
|
293
|
|
|
{ |
|
294
|
|
|
// save query |
|
295
|
85 |
|
$this->queries[] = [ |
|
296
|
85 |
|
'query' => $sql, |
|
297
|
85 |
|
'by_function' => 'simpleQuery', |
|
298
|
|
|
]; |
|
299
|
|
|
|
|
300
|
85 |
|
$stmt = $this->db->prepare($sql, $params); |
|
301
|
84 |
|
$stmt->execute(); |
|
302
|
84 |
|
$this->lastRowCount = $stmt->rowCount(); |
|
303
|
84 |
|
$stmt->closeCursor(); |
|
304
|
|
|
|
|
305
|
84 |
|
return true; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Encapsulates internal PDO::exec call. |
|
310
|
|
|
* This allows us to extend it, e.g. with caching functionality. |
|
311
|
|
|
* |
|
312
|
|
|
* @param string $sql |
|
313
|
|
|
* |
|
314
|
|
|
* @return int number of affected rows |
|
315
|
|
|
*/ |
|
316
|
103 |
|
public function exec($sql) |
|
317
|
|
|
{ |
|
318
|
|
|
// save query |
|
319
|
103 |
|
$this->queries[] = [ |
|
320
|
103 |
|
'query' => $sql, |
|
321
|
103 |
|
'by_function' => 'exec', |
|
322
|
|
|
]; |
|
323
|
|
|
|
|
324
|
103 |
|
return $this->db->exec($sql); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* @return int ID of new entry |
|
329
|
|
|
* |
|
330
|
|
|
* @throws Exception if invalid table name was given |
|
331
|
|
|
*/ |
|
332
|
78 |
|
public function insert(string $table, array $data): int |
|
333
|
|
|
{ |
|
334
|
78 |
|
$columns = array_keys($data); |
|
335
|
|
|
|
|
336
|
|
|
// we reject fishy table names |
|
337
|
78 |
|
if (1 !== preg_match('/^[a-zA-Z0-9_]+$/i', $table)) { |
|
338
|
1 |
|
throw new Exception('Invalid table name given.'); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/* |
|
342
|
|
|
* start building SQL |
|
343
|
|
|
*/ |
|
344
|
77 |
|
$sql = 'INSERT INTO '.$table.' ('.implode(', ', $columns); |
|
345
|
77 |
|
$sql .= ') VALUES ('; |
|
346
|
|
|
|
|
347
|
|
|
// add placeholders for each value; collect values |
|
348
|
77 |
|
$placeholders = []; |
|
349
|
77 |
|
$params = []; |
|
350
|
77 |
|
foreach ($data as $v) { |
|
351
|
77 |
|
$placeholders[] = '?'; |
|
352
|
77 |
|
$params[] = $v; |
|
353
|
|
|
} |
|
354
|
77 |
|
$sql .= implode(', ', $placeholders); |
|
355
|
|
|
|
|
356
|
77 |
|
$sql .= ')'; |
|
357
|
|
|
|
|
358
|
|
|
/* |
|
359
|
|
|
* SQL looks like the following now: |
|
360
|
|
|
* INSERT INTO foo (bar) (?) |
|
361
|
|
|
*/ |
|
362
|
|
|
|
|
363
|
|
|
// Setup and run prepared statement |
|
364
|
77 |
|
$stmt = $this->db->prepare($sql); |
|
365
|
77 |
|
$stmt->execute($params); |
|
366
|
|
|
|
|
367
|
77 |
|
return $this->db->lastInsertId(); |
|
|
|
|
|
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.