Comparison

plugins/mod_storage_sql2.lua @ 6349:0cee68dd35f8

mod_storage_sql2: DELETE then INSERT in map stores
author Kim Alvefur <zash@zash.se>
date Tue, 12 Aug 2014 11:38:12 +0200
parent 6335:eaf6e7986934
child 6534:b89406fa076c
comparison
equal deleted inserted replaced
6348:bffc885dc378 6349:0cee68dd35f8
219 local map_store = {}; 219 local map_store = {};
220 map_store.__index = map_store; 220 map_store.__index = map_store;
221 function map_store:get(username, key) 221 function map_store:get(username, key)
222 local ok, result = engine:transaction(function() 222 local ok, result = engine:transaction(function()
223 if type(key) == "string" and key ~= "" then 223 if type(key) == "string" and key ~= "" then
224 local iter, state, first = engine:select("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", 224 for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, key) do
225 host, username, self.store, key or ""); 225 return deserialize(row[1], row[2]);
226 local row = iter(state, first);
227 if row then
228 return deserialize(row.type, row.value);
229 else
230 return nil;
231 end 226 end
232 else 227 else
233 error("TODO: non-string keys"); 228 error("TODO: non-string keys");
234 end 229 end
235 end); 230 end);
236 if not ok then return nil, result; end 231 if not ok then return nil, result; end
237 return result; 232 return result;
238 end 233 end
239 function map_store:set(username, key, data) 234 function map_store:set(username, key, data)
240 local ok, result = engine:transaction(function() 235 local ok, result = engine:transaction(function()
241 if data == nil then 236 if type(key) == "string" and key ~= "" then
242 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", 237 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
243 host, username, self.store, key or ""); 238 host, username or "", self.store, key);
244 elseif type(key) == "string" and key ~= "" then 239 if data ~= nil then
245 local t, value = assert(serialize(data)); 240 local t, value = assert(serialize(data));
246 engine:update("UPDATE `prosody` SET `type`=?, `value`=? WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", 241 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value);
247 t, value, host, username, self.store, key); 242 end
248 else 243 else
249 error("TODO: non-string keys"); 244 error("TODO: non-string keys");
250 end 245 end
251 return true; 246 return true;
252 end); 247 end);