Comparison

plugins/mod_storage_sql.lua @ 8032:aa9f198cb3c9

mod_storage_sql: Rename variables to avoid name clashes [luacheck]
author Kim Alvefur <zash@zash.se>
date Sat, 01 Apr 2017 19:25:34 +0200
parent 8031:ef838b7f8f53
child 8033:6c3cae9b96cb
comparison
equal deleted inserted replaced
8031:ef838b7f8f53 8032:aa9f198cb3c9
30 if t == "string" or t == "boolean" or t == "number" then 30 if t == "string" or t == "boolean" or t == "number" then
31 return t, tostring(value); 31 return t, tostring(value);
32 elseif is_stanza(value) then 32 elseif is_stanza(value) then
33 return "xml", tostring(value); 33 return "xml", tostring(value);
34 elseif t == "table" then 34 elseif t == "table" then
35 local value,err = json.encode(value); 35 local encoded,err = json.encode(value);
36 if value then return "json", value; end 36 if value then return "json", encoded; end
37 return nil, err; 37 return nil, err;
38 end 38 end
39 return nil, "Unhandled value type: "..t; 39 return nil, "Unhandled value type: "..t;
40 end 40 end
41 local function deserialize(t, value) 41 local function deserialize(t, value)
78 78
79 if data and next(data) ~= nil then 79 if data and next(data) ~= nil then
80 local extradata = {}; 80 local extradata = {};
81 for key, value in pairs(data) do 81 for key, value in pairs(data) do
82 if type(key) == "string" and key ~= "" then 82 if type(key) == "string" and key ~= "" then
83 local t, value = assert(serialize(value)); 83 local t, encoded_value = assert(serialize(value));
84 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, user or "", store, key, t, value); 84 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, user or "", store, key, t, encoded_value);
85 else 85 else
86 extradata[key] = value; 86 extradata[key] = value;
87 end 87 end
88 end 88 end
89 if next(extradata) ~= nil then 89 if next(extradata) ~= nil then
90 local t, extradata = assert(serialize(extradata)); 90 local t, encoded_extradata = assert(serialize(extradata));
91 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, user or "", store, "", t, extradata); 91 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, user or "", store, "", t, encoded_extradata);
92 end 92 end
93 end 93 end
94 return true; 94 return true;
95 end 95 end
96 96
184 archive_store.__index = archive_store 184 archive_store.__index = archive_store
185 function archive_store:append(username, key, value, when, with) 185 function archive_store:append(username, key, value, when, with)
186 local user,store = username,self.store; 186 local user,store = username,self.store;
187 when = when or os.time(); 187 when = when or os.time();
188 with = with or ""; 188 with = with or "";
189 local ok, key = engine:transaction(function() 189 local ok, ret = engine:transaction(function()
190 if key then 190 if key then
191 engine:delete("DELETE FROM `prosodyarchive` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, user or "", store, key); 191 engine:delete("DELETE FROM `prosodyarchive` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, user or "", store, key);
192 else 192 else
193 key = uuid.generate(); 193 key = uuid.generate();
194 end 194 end
195 local t, value = assert(serialize(value)); 195 local t, encoded_value = assert(serialize(value));
196 engine:insert("INSERT INTO `prosodyarchive` (`host`, `user`, `store`, `when`, `with`, `key`, `type`, `value`) VALUES (?,?,?,?,?,?,?,?)", host, user or "", store, when, with, key, t, value); 196 engine:insert(insert_sql, host, user or "", store, when, with, key, t, encoded_value);
197 return key; 197 return key;
198 end); 198 end);
199 if not ok then return ok, key; end 199 if not ok then return ok, ret; end
200 return key; 200 return ret; -- the key
201 end 201 end
202 202
203 -- Helpers for building the WHERE clause 203 -- Helpers for building the WHERE clause
204 local function archive_where(query, args, where) 204 local function archive_where(query, args, where)
205 -- Time range, inclusive 205 -- Time range, inclusive