Comparison

plugins/mod_storage_sql.lua @ 10677:0054aec3e8c5

mod_storage_sql: Add map_store:find_key() and map_store:delete_key() (+ tests)
author Matthew Wild <mwild1@gmail.com>
date Wed, 11 Mar 2020 15:57:53 +0000
parent 10656:ffa9a20aca8b
child 10680:19692fc5c106
comparison
equal deleted inserted replaced
10676:33c7e4920591 10677:0054aec3e8c5
222 extradata[key] = data; 222 extradata[key] = data;
223 local t, value = assert(serialize(extradata)); 223 local t, value = assert(serialize(extradata));
224 engine:insert(insert_sql, host, username or "", self.store, "", t, value); 224 engine:insert(insert_sql, host, username or "", self.store, "", t, value);
225 end 225 end
226 end 226 end
227 return true;
228 end);
229 if not ok then return nil, result; end
230 return result;
231 end
232
233 function map_store:find_key(key)
234 if type(key) ~= "string" or key == "" then
235 return nil, "find_key only supports non-empty string keys";
236 end
237 local ok, result = engine:transaction(function()
238 local query = [[
239 SELECT "user", "type", "value"
240 FROM "prosody"
241 WHERE "host"=? AND "store"=? AND "key"=?
242 ]];
243
244 local data;
245 for row in engine:select(query, host, self.store, key) do
246 local key_data, err = deserialize(row[2], row[3]);
247 assert(key_data ~= nil, err);
248 if data == nil then
249 data = {};
250 end
251 data[row[1]] = key_data;
252 end
253
254 return data;
255
256 end);
257 if not ok then return nil, result; end
258 return result;
259 end
260
261 function map_store:delete_key(key)
262 if type(key) ~= "string" or key == "" then
263 return nil, "delete_key only supports non-empty string keys";
264 end
265 local ok, result = engine:transaction(function()
266 local delete_sql = [[
267 DELETE FROM "prosody"
268 WHERE "host"=? AND "store"=? AND "key"=?;
269 ]];
270 engine:delete(delete_sql, host, self.store, key);
227 return true; 271 return true;
228 end); 272 end);
229 if not ok then return nil, result; end 273 if not ok then return nil, result; end
230 return result; 274 return result;
231 end 275 end