1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MatthiasMullie\Scrapbook\Adapters; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* MySQL 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 MySQL extends SQL |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
public function set($key, $value, $expire = 0) |
19
|
|
|
{ |
20
|
|
|
$value = $this->serialize($value); |
21
|
|
|
$expire = $this->expire($expire); |
22
|
|
|
|
23
|
|
|
$this->clearExpired(); |
24
|
|
|
|
25
|
|
|
$statement = $this->client->prepare( |
26
|
|
|
"REPLACE INTO $this->table (k, v, e) |
27
|
|
|
VALUES (:key, :value, :expire)" |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$statement->execute(array( |
31
|
|
|
':key' => $key, |
32
|
|
|
':value' => $value, |
33
|
|
|
':expire' => $expire, |
34
|
|
|
)); |
35
|
|
|
|
36
|
|
|
// 1 = insert; 2 = update |
37
|
|
|
return 1 === $statement->rowCount() || 2 === $statement->rowCount(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function setMulti(array $items, $expire = 0) |
44
|
|
|
{ |
45
|
|
|
if (empty($items)) { |
46
|
|
|
return array(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$i = 1; |
50
|
|
|
$query = array(); |
51
|
|
|
$params = array(); |
52
|
|
|
$expire = $this->expire($expire); |
53
|
|
|
|
54
|
|
|
$this->clearExpired(); |
55
|
|
|
|
56
|
|
|
foreach ($items as $key => $value) { |
57
|
|
|
$value = $this->serialize($value); |
58
|
|
|
|
59
|
|
|
$query[] = "(:key$i, :value$i, :expire$i)"; |
60
|
|
|
$params += array( |
61
|
|
|
":key$i" => $key, |
62
|
|
|
":value$i" => $value, |
63
|
|
|
":expire$i" => $expire, |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
++$i; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$statement = $this->client->prepare( |
70
|
|
|
"REPLACE INTO $this->table (k, v, e) |
71
|
|
|
VALUES ".implode(',', $query) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$statement->execute($params); |
75
|
|
|
|
76
|
|
|
/* |
77
|
|
|
* As far as I can tell, there are no conditions under which this can go |
78
|
|
|
* wrong (if item exists or not, REPLACE INTO will work either way), |
79
|
|
|
* except for connection problems, in which case all or none will be |
80
|
|
|
* stored. |
81
|
|
|
* Can't compare with count($items) because rowCount could be 1 or 2, |
82
|
|
|
* depending on if REPLACE was an INSERT or UPDATE. |
83
|
|
|
*/ |
84
|
|
|
$success = $statement->rowCount() > 0; |
85
|
|
|
|
86
|
|
|
return array_fill_keys(array_keys($items), $success); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function add($key, $value, $expire = 0) |
93
|
|
|
{ |
94
|
|
|
$value = $this->serialize($value); |
95
|
|
|
$expire = $this->expire($expire); |
96
|
|
|
|
97
|
|
|
$this->clearExpired(); |
98
|
|
|
|
99
|
|
|
// MySQL-specific way to ignore insert-on-duplicate errors |
100
|
|
|
$statement = $this->client->prepare( |
101
|
|
|
"INSERT IGNORE INTO $this->table (k, v, e) |
102
|
|
|
VALUES (:key, :value, :expire)" |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$statement->execute(array( |
106
|
|
|
':key' => $key, |
107
|
|
|
':value' => $value, |
108
|
|
|
':expire' => $expire, |
109
|
|
|
)); |
110
|
|
|
|
111
|
|
|
return 1 === $statement->rowCount(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function flush() |
118
|
|
|
{ |
119
|
|
|
return false !== $this->client->exec("TRUNCATE TABLE $this->table"); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
protected function init() |
126
|
|
|
{ |
127
|
|
|
$this->client->exec( |
128
|
|
|
"CREATE TABLE IF NOT EXISTS $this->table ( |
129
|
|
|
k VARBINARY(1000) NOT NULL PRIMARY KEY, |
130
|
|
|
v LONGBLOB, |
131
|
|
|
e TIMESTAMP NULL DEFAULT NULL, |
132
|
|
|
KEY e (e) |
133
|
|
|
)" |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|