aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mod_storage_sql.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2016-02-09 16:56:27 +0100
committerKim Alvefur <zash@zash.se>2016-02-09 16:56:27 +0100
commit315a1fc1ca3d6e316088cf0aec66a738a8955cbf (patch)
tree9ed91a888cfae7f02c1cc5be36d2aff9aab9107c /plugins/mod_storage_sql.lua
parent38cee98c50dbbddd39cfd2cc91370807d22323a3 (diff)
downloadprosody-315a1fc1ca3d6e316088cf0aec66a738a8955cbf.tar.gz
prosody-315a1fc1ca3d6e316088cf0aec66a738a8955cbf.zip
mod_storage_sql: Implement map:set_keys, allowing multiple keys to be set in the same transaction
Diffstat (limited to 'plugins/mod_storage_sql.lua')
-rw-r--r--plugins/mod_storage_sql.lua40
1 files changed, 23 insertions, 17 deletions
diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua
index 31d83f68..5f25c563 100644
--- a/plugins/mod_storage_sql.lua
+++ b/plugins/mod_storage_sql.lua
@@ -127,6 +127,7 @@ end
local map_store = {};
map_store.__index = map_store;
+map_store.remove = {};
function map_store:get(username, key)
local ok, result = engine:transaction(function()
if type(key) == "string" and key ~= "" then
@@ -144,25 +145,30 @@ function map_store:get(username, key)
return result;
end
function map_store:set(username, key, data)
+ return self:set_keys(username, { [key] = data or self.remove });
+end
+function map_store:set_keys(username, keydatas)
local ok, result = engine:transaction(function()
- if type(key) == "string" and key ~= "" then
- engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
- host, username or "", self.store, key);
- if data ~= nil then
- local t, value = assert(serialize(data));
- engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value);
- end
- else
- local extradata = {};
- for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, "") do
- extradata = deserialize(row[1], row[2]);
- break;
+ for key, data in pairs(keydatas) do
+ if type(key) == "string" and key ~= "" then
+ engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
+ host, username or "", self.store, key);
+ if data ~= self.remove then
+ local t, value = assert(serialize(data));
+ engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value);
+ end
+ else
+ local extradata = {};
+ for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, "") do
+ extradata = deserialize(row[1], row[2]);
+ break;
+ end
+ engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
+ host, username or "", self.store, "");
+ extradata[key] = data;
+ local t, value = assert(serialize(extradata));
+ engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, "", t, value);
end
- engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
- host, username or "", self.store, "");
- extradata[key] = data;
- local t, value = assert(serialize(extradata));
- engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, "", t, value);
end
return true;
end);