|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ownCloud - Music app |
|
5
|
|
|
* |
|
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
7
|
|
|
* later. See the COPYING file. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Pauli Järvinen <[email protected]> |
|
10
|
|
|
* @copyright Pauli Järvinen 2017 - 2020 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace OCA\Music\Db; |
|
14
|
|
|
|
|
15
|
|
|
use OCP\IDBConnection; |
|
16
|
|
|
|
|
17
|
|
|
use \Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
|
18
|
|
|
|
|
19
|
|
|
class Cache { |
|
20
|
|
|
private $db; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(IDBConnection $db) { |
|
23
|
|
|
$this->db = $db; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $userId |
|
28
|
|
|
* @param string $key |
|
29
|
|
|
* @param string $data |
|
30
|
|
|
*/ |
|
31
|
|
|
public function add(string $userId, string $key, string $data) : void { |
|
32
|
|
|
$sql = 'INSERT INTO `*PREFIX*music_cache` |
|
33
|
|
|
(`user_id`, `key`, `data`) VALUES (?, ?, ?)'; |
|
34
|
|
|
$this->db->executeUpdate($sql, [$userId, $key, $data]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $userId |
|
39
|
|
|
* @param string $key |
|
40
|
|
|
* @param string $data |
|
41
|
|
|
*/ |
|
42
|
|
|
public function update(string $userId, string $key, string $data) : void { |
|
43
|
|
|
$sql = 'UPDATE `*PREFIX*music_cache` SET `data` = ? |
|
44
|
|
|
WHERE `user_id` = ? AND `key` = ?'; |
|
45
|
|
|
$this->db->executeUpdate($sql, [$data, $userId, $key]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $userId |
|
50
|
|
|
* @param string $key |
|
51
|
|
|
* @param string $data |
|
52
|
|
|
*/ |
|
53
|
|
|
public function set(string $userId, string $key, string $data) : void { |
|
54
|
|
|
try { |
|
55
|
|
|
$this->add($userId, $key, $data); |
|
56
|
|
|
} catch (UniqueConstraintViolationException $e) { |
|
57
|
|
|
$this->update($userId, $key, $data); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Remove one or several key-value pairs |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $userId User to target, omit to target all users |
|
65
|
|
|
* @param string $key Key to target, omit to target all keys |
|
66
|
|
|
*/ |
|
67
|
|
|
public function remove(string $userId = null, string $key = null) : void { |
|
68
|
|
|
$sql = 'DELETE FROM `*PREFIX*music_cache`'; |
|
69
|
|
|
$params = []; |
|
70
|
|
|
if ($userId !== null) { |
|
71
|
|
|
$sql .= ' WHERE `user_id` = ?'; |
|
72
|
|
|
$params[] = $userId; |
|
73
|
|
|
|
|
74
|
|
|
if ($key !== null) { |
|
75
|
|
|
$sql .= ' AND `key` = ?'; |
|
76
|
|
|
$params[] = $key; |
|
77
|
|
|
} |
|
78
|
|
|
} elseif ($key !== null) { |
|
79
|
|
|
$sql .= ' WHERE `key` = ?'; |
|
80
|
|
|
$params[] = $key; |
|
81
|
|
|
} |
|
82
|
|
|
$this->db->executeUpdate($sql, $params); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $userId |
|
87
|
|
|
* @param string $key |
|
88
|
|
|
* @return string|null |
|
89
|
|
|
*/ |
|
90
|
|
|
public function get(string $userId, string $key) : ?string { |
|
91
|
|
|
$sql = 'SELECT `data` FROM `*PREFIX*music_cache` |
|
92
|
|
|
WHERE `user_id` = ? AND `key` = ?'; |
|
93
|
|
|
$result = $this->db->executeQuery($sql, [$userId, $key]); |
|
94
|
|
|
$rows = $result->fetchAll(); |
|
95
|
|
|
$result->closeCursor(); |
|
96
|
|
|
|
|
97
|
|
|
return \count($rows) ? $rows[0]['data'] : null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get all key-value pairs of one user, optionally limitting to keys with a given prefix. |
|
102
|
|
|
* @param string $userId |
|
103
|
|
|
* @param string|null $prefix |
|
104
|
|
|
* @return array of arrays with keys 'key', 'data' |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getAll(string $userId, string $prefix = null) : array { |
|
107
|
|
|
$sql = 'SELECT `key`, `data` FROM `*PREFIX*music_cache` |
|
108
|
|
|
WHERE `user_id` = ?'; |
|
109
|
|
|
$params = [$userId]; |
|
110
|
|
|
|
|
111
|
|
|
if (!empty($prefix)) { |
|
112
|
|
|
$sql .= ' AND `key` LIKE ?'; |
|
113
|
|
|
$params[] = $prefix . '%'; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$result = $this->db->executeQuery($sql, $params); |
|
117
|
|
|
$rows = $result->fetchAll(); |
|
118
|
|
|
$result->closeCursor(); |
|
119
|
|
|
|
|
120
|
|
|
return $rows; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Given a cache key and its exact content, return the owning user |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getOwner(string $key, string $data) : ?string { |
|
127
|
|
|
$sql = 'SELECT `user_id` FROM `*PREFIX*music_cache` |
|
128
|
|
|
WHERE `key` = ? AND `data` = ?'; |
|
129
|
|
|
$result = $this->db->executeQuery($sql, [$key, $data]); |
|
130
|
|
|
$rows = $result->fetchAll(); |
|
131
|
|
|
$result->closeCursor(); |
|
132
|
|
|
|
|
133
|
|
|
return \count($rows) ? $rows[0]['user_id'] : null; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|