Software /
code /
prosody
Changeset
7153:89fa66d4e502
mod_storage_sql: Implement map:set_keys, allowing multiple keys to be set in the same transaction
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 09 Feb 2016 16:56:27 +0100 (2016-02-09) |
parents | 7152:ca64255bf7cd |
children | 7154:b3b92204802f |
files | plugins/mod_storage_sql.lua |
diffstat | 1 files changed, 23 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/plugins/mod_storage_sql.lua Tue Feb 09 16:54:56 2016 +0100 +++ b/plugins/mod_storage_sql.lua Tue Feb 09 16:56:27 2016 +0100 @@ -127,6 +127,7 @@ 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 @@ 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); + 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 - 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 return true; end);