1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MatthiasMullie\Scrapbook\Adapters; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* SQLite adapter. Basically just a wrapper over \PDO, but in an exchangeable |
7
|
|
|
* (KeyValueStore) interface. |
8
|
|
|
* |
9
|
|
|
* @author Matthias Mullie <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2014, Matthias Mullie. All rights reserved |
11
|
|
|
* @license LICENSE MIT |
12
|
|
|
*/ |
13
|
|
|
class SQLite extends MySQL |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
public function setMulti(array $items, $expire = 0) |
19
|
|
|
{ |
20
|
|
|
if (empty($items)) { |
21
|
|
|
return array(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$expire = $this->expire($expire); |
25
|
|
|
|
26
|
|
|
$this->clearExpired(); |
27
|
|
|
|
28
|
|
|
// SQLite < 3.7.11 doesn't support multi-insert/replace! |
29
|
|
|
|
30
|
|
|
$statement = $this->client->prepare( |
31
|
|
|
"REPLACE INTO $this->table (k, v, e) |
32
|
|
|
VALUES (:key, :value, :expire)" |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$success = array(); |
36
|
|
|
foreach ($items as $key => $value) { |
37
|
|
|
$value = $this->serialize($value); |
38
|
|
|
|
39
|
|
|
$statement->execute(array( |
40
|
|
|
':key' => $key, |
41
|
|
|
':value' => $value, |
42
|
|
|
':expire' => $expire, |
43
|
|
|
)); |
44
|
|
|
|
45
|
|
|
$success[$key] = (bool) $statement->rowCount(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $success; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function add($key, $value, $expire = 0) |
55
|
|
|
{ |
56
|
|
|
$value = $this->serialize($value); |
57
|
|
|
$expire = $this->expire($expire); |
58
|
|
|
|
59
|
|
|
$this->clearExpired(); |
60
|
|
|
|
61
|
|
|
// SQLite-specific way to ignore insert-on-duplicate errors |
62
|
|
|
$statement = $this->client->prepare( |
63
|
|
|
"INSERT OR IGNORE INTO $this->table (k, v, e) |
64
|
|
|
VALUES (:key, :value, :expire)" |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$statement->execute(array( |
68
|
|
|
':key' => $key, |
69
|
|
|
':value' => $value, |
70
|
|
|
':expire' => $expire, |
71
|
|
|
)); |
72
|
|
|
|
73
|
|
|
return 1 === $statement->rowCount(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function flush() |
80
|
|
|
{ |
81
|
|
|
return false !== $this->client->exec("DELETE FROM $this->table"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
protected function init() |
88
|
|
|
{ |
89
|
|
|
$this->client->exec( |
90
|
|
|
"CREATE TABLE IF NOT EXISTS $this->table ( |
91
|
|
|
k VARCHAR(255) NOT NULL PRIMARY KEY, |
92
|
|
|
v BLOB, |
93
|
|
|
e TIMESTAMP NULL DEFAULT NULL, |
94
|
|
|
KEY e |
95
|
|
|
)" |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|