Diff

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
line wrap: on
line diff
--- a/plugins/mod_storage_sql.lua	Wed Mar 11 14:36:56 2020 +0000
+++ b/plugins/mod_storage_sql.lua	Wed Mar 11 15:57:53 2020 +0000
@@ -230,6 +230,50 @@
 	return result;
 end
 
+function map_store:find_key(key)
+	if type(key) ~= "string" or key == "" then
+		return nil, "find_key only supports non-empty string keys";
+	end
+	local ok, result = engine:transaction(function()
+		local query = [[
+		SELECT "user", "type", "value"
+		FROM "prosody"
+		WHERE "host"=? AND "store"=? AND "key"=?
+		]];
+
+		local data;
+		for row in engine:select(query, host, self.store, key) do
+			local key_data, err = deserialize(row[2], row[3]);
+			assert(key_data ~= nil, err);
+			if data == nil then
+				data = {};
+			end
+			data[row[1]] = key_data;
+		end
+
+		return data;
+
+	end);
+	if not ok then return nil, result; end
+	return result;
+end
+
+function map_store:delete_key(key)
+	if type(key) ~= "string" or key == "" then
+		return nil, "delete_key only supports non-empty string keys";
+	end
+	local ok, result = engine:transaction(function()
+		local delete_sql = [[
+		DELETE FROM "prosody"
+		WHERE "host"=? AND "store"=? AND "key"=?;
+		]];
+		engine:delete(delete_sql, host, self.store, key);
+		return true;
+	end);
+	if not ok then return nil, result; end
+	return result;
+end
+
 local archive_store = {}
 archive_store.caps = {
 	total = true;