From e5031598f6316807d69e805e72af9d8ed7672f59 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 8 Feb 2016 20:23:12 +0100 Subject: mod_storage_sql: Support non-string keys in map store --- plugins/mod_storage_sql.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'plugins') diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index 57d964e2..31d83f68 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -134,7 +134,10 @@ function map_store:get(username, key) return deserialize(row[1], row[2]); end else - error("TODO: non-string keys"); + for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, "") do + local data = deserialize(row[1], row[2]); + return data and data[key] or nil; + end end end); if not ok then return nil, result; end @@ -150,7 +153,16 @@ function map_store:set(username, key, data) engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value); end else - error("TODO: non-string keys"); + 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); -- cgit v1.2.3 From 07e67e32f3f78dcae90684dbff2adcc535887be7 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 9 Feb 2016 16:56:27 +0100 Subject: mod_storage_sql: Implement map:set_keys, allowing multiple keys to be set in the same transaction --- plugins/mod_storage_sql.lua | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'plugins') 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); -- cgit v1.2.3 From bb71bd86fa1c64fa83aa309e006556ccf0e751b6 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 9 Feb 2016 17:24:04 +0100 Subject: mod_storage_sql: Silence some harmless luacheck warnings --- plugins/mod_storage_sql.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'plugins') diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index 5f25c563..34fcacee 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -1,4 +1,6 @@ +-- luacheck: ignore 212/self + local json = require "util.json"; local sql = require "util.sql"; local xml_parse = require "util.xml".parse; @@ -125,6 +127,7 @@ end --- Archive store API +-- luacheck: ignore 512 431/user 431/store local map_store = {}; map_store.__index = map_store; map_store.remove = {}; -- cgit v1.2.3