+-- luacheck: ignore 212/self
+
local json = require "util.json";
local sql = require "util.sql";
local xml_parse = require "util.xml".parse;
--- Archive store API
+-- luacheck: ignore 512 431/user 431/store
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
return result;
end
function map_store:set(username, key, data)
+ if data == nil then data = self.remove; end
+ return self:set_keys(username, { [key] = data });
+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);
end
sql_query = sql_query:format(t_concat(where, " AND "), query.reverse and "DESC" or "ASC", query.limit and " LIMIT ?" or "");
- module:log("debug", sql_query);
return engine:select(sql_query, unpack(args));
end);
if not ok then return ok, result end
archive_where(query, args, where);
archive_where_id_range(query, args, where);
sql_query = sql_query:format(t_concat(where, " AND "));
- module:log("debug", sql_query);
return engine:delete(sql_query, unpack(args));
end);
end
function module.load()
if prosody.prosodyctl then return; end
+ local engines = module:shared("/*/sql/connections");
local params = normalize_params(module:get_option("sql", default_params));
- engine = sql:create_engine(params, function (engine)
- if module:get_option("sql_manage_tables", true) then
- -- Automatically create table, ignore failure (table probably already exists)
- -- FIXME: we should check in information_schema, etc.
- create_table();
- -- Check whether the table needs upgrading
- if upgrade_table(params, false) then
- module:log("error", "Old database format detected. Please run: prosodyctl mod_%s upgrade", module.name);
- return false, "database upgrade needed";
+ engine = engines[sql.db2uri(params)];
+ if not engine then
+ module:log("debug", "Creating new engine");
+ engine = sql:create_engine(params, function (engine)
+ if module:get_option("sql_manage_tables", true) then
+ -- Automatically create table, ignore failure (table probably already exists)
+ -- FIXME: we should check in information_schema, etc.
+ create_table();
+ -- Check whether the table needs upgrading
+ if upgrade_table(params, false) then
+ module:log("error", "Old database format detected. Please run: prosodyctl mod_%s upgrade", module.name);
+ return false, "database upgrade needed";
+ end
end
- end
- end);
+ end);
+ engines[sql.db2uri(params)] = engine;
+ end
module:provides("storage", driver);
end