5 Prosody - key-value, map
6 | host | user | store | key | type | value |
8 | host | user | store | key | time | stanzatype | jsonvalue |
12 | host | user | "roster" | "contactjid" | type | value |
13 | host | user | "roster" | NULL | "json" | roster[false] data |
15 | host | user | "accounts" | "username" | type | value |
17 Offline - ProsodyArchive
18 | host | user | "offline" | "contactjid" | time | "message" | json|XML |
23 local tostring = tostring;
24 local tonumber = tonumber;
27 local setmetatable = setmetatable;
28 local xpcall = xpcall;
29 local json = require "util.json";
33 local host,user,store = module.host;
34 local params = module:get_option("sql");
36 local resolve_relative_path = require "core.configmanager".resolve_relative_path;
38 local function test_connection()
39 if not connection then return nil; end
40 if connection:ping() then
43 module:log("debug", "Database connection closed");
47 local function connect()
48 if not test_connection() then
49 prosody.unlock_globals();
50 local dbh, err = DBI.Connect(
51 params.driver, params.database,
52 params.username, params.password,
53 params.host, params.port
55 prosody.lock_globals();
57 module:log("debug", "Database connection failed: %s", tostring(err));
60 module:log("debug", "Successfully connected to database");
61 dbh:autocommit(false); -- don't commit automatically
67 local function create_table()
68 local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);";
69 if params.driver == "PostgreSQL" then
70 create_sql = create_sql:gsub("`", "\"");
73 local stmt = connection:prepare(create_sql);
75 local ok = stmt:execute();
76 local commit_ok = connection:commit();
77 if ok and commit_ok then
78 module:log("info", "Initialized new %s database with prosody table", params.driver);
79 local index_sql = "CREATE INDEX `prosody_index` ON `prosody` (`host`, `user`, `store`, `key`)";
80 if params.driver == "PostgreSQL" then
81 index_sql = index_sql:gsub("`", "\"");
82 elseif params.driver == "MySQL" then
83 index_sql = index_sql:gsub("`([,)])", "`(20)%1");
85 local stmt, err = connection:prepare(index_sql);
86 local ok, commit_ok, commit_err;
88 ok, err = stmt:execute();
89 commit_ok, commit_err = connection:commit();
91 if not(ok and commit_ok) then
92 module:log("warn", "Failed to create index (%s), lookups may not be optimised", err or commit_err);
98 do -- process options to get a db connection
101 params = params or { driver = "SQLite3" };
103 if params.driver == "SQLite3" then
104 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
107 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
111 -- Automatically create table, ignore failure (table probably already exists)
115 local function serialize(value)
116 local t = type(value);
117 if t == "string" or t == "boolean" or t == "number" then
118 return t, tostring(value);
119 elseif t == "table" then
120 local value,err = json.encode(value);
121 if value then return "json", value; end
124 return nil, "Unhandled value type: "..t;
126 local function deserialize(t, value)
127 if t == "string" then return value;
128 elseif t == "boolean" then
129 if value == "true" then return true;
130 elseif value == "false" then return false; end
131 elseif t == "number" then return tonumber(value);
132 elseif t == "json" then
133 return json.decode(value);
137 local function getsql(sql, ...)
138 if params.driver == "PostgreSQL" then
139 sql = sql:gsub("`", "\"");
141 -- do prepared statement stuff
142 local stmt, err = connection:prepare(sql);
143 if not stmt and not test_connection() then error("connection failed"); end
144 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end
146 local ok, err = stmt:execute(host or "", user or "", store or "", ...);
147 if not ok and not test_connection() then error("connection failed"); end
148 if not ok then return nil, err; end
152 local function setsql(sql, ...)
153 local stmt, err = getsql(sql, ...);
154 if not stmt then return stmt, err; end
155 return stmt:affected();
157 local function transact(...)
160 local function rollback(...)
161 if connection then connection:rollback(); end -- FIXME check for rollback error?
164 local function commit(...)
165 if not connection:commit() then return nil, "SQL commit failed"; end
169 local function keyval_store_get()
170 local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?");
171 if not stmt then return rollback(nil, err); end
175 for row in stmt:rows(true) do
178 local v = deserialize(row.type, row.value);
180 if k ~= "" then result[k] = v; elseif type(v) == "table" then
181 for a,b in pairs(v) do
187 return commit(haveany and result or nil);
189 local function keyval_store_set(data)
190 local affected, err = setsql("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?");
191 if not affected then return rollback(affected, err); end
193 if data and next(data) ~= nil then
194 local extradata = {};
195 for key, value in pairs(data) do
196 if type(key) == "string" and key ~= "" then
197 local t, value = serialize(value);
198 if not t then return rollback(t, value); end
199 local ok, err = setsql("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", key, t, value);
200 if not ok then return rollback(ok, err); end
202 extradata[key] = value;
205 if next(extradata) ~= nil then
206 local t, extradata = serialize(extradata);
207 if not t then return rollback(t, extradata); end
208 local ok, err = setsql("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", "", t, extradata);
209 if not ok then return rollback(ok, err); end
215 local keyval_store = {};
216 keyval_store.__index = keyval_store;
217 function keyval_store:get(username)
218 user,store = username,self.store;
219 if not connection and not connect() then return nil, "Unable to connect to database"; end
220 local success, ret, err = xpcall(keyval_store_get, debug.traceback);
221 if not connection and connect() then
222 success, ret, err = xpcall(keyval_store_get, debug.traceback);
224 if success then return ret, err; else return rollback(nil, ret); end
226 function keyval_store:set(username, data)
227 user,store = username,self.store;
228 if not connection and not connect() then return nil, "Unable to connect to database"; end
229 local success, ret, err = xpcall(function() return keyval_store_set(data); end, debug.traceback);
230 if not connection and connect() then
231 success, ret, err = xpcall(function() return keyval_store_set(data); end, debug.traceback);
233 if success then return ret, err; else return rollback(nil, ret); end
236 local function map_store_get(key)
237 local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
238 if not stmt then return rollback(nil, err); end
242 for row in stmt:rows(true) do
245 local v = deserialize(row.type, row.value);
247 if k ~= "" then result[k] = v; elseif type(v) == "table" then
248 for a,b in pairs(v) do
254 return commit(haveany and result[key] or nil);
256 local function map_store_set(key, data)
257 local affected, err = setsql("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
258 if not affected then return rollback(affected, err); end
260 if data and next(data) ~= nil then
261 if type(key) == "string" and key ~= "" then
262 local t, value = serialize(data);
263 if not t then return rollback(t, value); end
264 local ok, err = setsql("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", key, t, value);
265 if not ok then return rollback(ok, err); end
267 -- TODO non-string keys
273 local map_store = {};
274 map_store.__index = map_store;
275 function map_store:get(username, key)
276 user,store = username,self.store;
277 local success, ret, err = xpcall(function() return map_store_get(key); end, debug.traceback);
278 if success then return ret, err; else return rollback(nil, ret); end
280 function map_store:set(username, key, data)
281 user,store = username,self.store;
282 local success, ret, err = xpcall(function() return map_store_set(key, data); end, debug.traceback);
283 if success then return ret, err; else return rollback(nil, ret); end
286 local list_store = {};
287 list_store.__index = list_store;
288 function list_store:scan(username, from, to, jid, typ)
289 user,store = username,self.store;
291 local cols = {"from", "to", "jid", "typ"};
292 local vals = { from , to , jid , typ };
294 local query = "SELECT * FROM `prosodyarchive` WHERE `host`=? AND `user`=? AND `store`=?";
296 query = query.." ORDER BY time";
297 --local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
299 return nil, "not-implemented"
302 local driver = { name = "sql" };
304 function driver:open(store, typ)
305 if not typ then -- default key-value store
306 return setmetatable({ store = store }, keyval_store);
308 return nil, "unsupported-store";
311 module:add_item("data-driver", driver);