Comparison

plugins/mod_storage_sql.lua @ 7274:e0727512bb99

mod_storage_sql: Allow loops over results to end on their own
author Kim Alvefur <zash@zash.se>
date Sun, 13 Mar 2016 17:43:33 +0100
parent 7273:7e659f87973d
child 7305:c02e3d8f23fc
comparison
equal deleted inserted replaced
7273:7e659f87973d 7274:e0727512bb99
131 local map_store = {}; 131 local map_store = {};
132 map_store.__index = map_store; 132 map_store.__index = map_store;
133 map_store.remove = {}; 133 map_store.remove = {};
134 function map_store:get(username, key) 134 function map_store:get(username, key)
135 local ok, result = engine:transaction(function() 135 local ok, result = engine:transaction(function()
136 local data;
136 if type(key) == "string" and key ~= "" then 137 if type(key) == "string" and key ~= "" then
137 for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=? LIMIT 1", host, username or "", self.store, key) do 138 for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=? LIMIT 1", host, username or "", self.store, key) do
138 return deserialize(row[1], row[2]); 139 data = deserialize(row[1], row[2]);
139 end 140 end
141 return data;
140 else 142 else
141 for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=? LIMIT 1", host, username or "", self.store, "") do 143 for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=? LIMIT 1", host, username or "", self.store, "") do
142 local data = deserialize(row[1], row[2]); 144 data = deserialize(row[1], row[2]);
143 return data and data[key] or nil; 145 end
144 end 146 return data and data[key] or nil;
145 end 147 end
146 end); 148 end);
147 if not ok then return nil, result; end 149 if not ok then return nil, result; end
148 return result; 150 return result;
149 end 151 end
163 end 165 end
164 else 166 else
165 local extradata = {}; 167 local extradata = {};
166 for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=? LIMIT 1", host, username or "", self.store, "") do 168 for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=? LIMIT 1", host, username or "", self.store, "") do
167 extradata = deserialize(row[1], row[2]); 169 extradata = deserialize(row[1], row[2]);
168 break;
169 end 170 end
170 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", 171 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
171 host, username or "", self.store, ""); 172 host, username or "", self.store, "");
172 extradata[key] = data; 173 extradata[key] = data;
173 local t, value = assert(serialize(extradata)); 174 local t, value = assert(serialize(extradata));
258 259
259 -- Total matching 260 -- Total matching
260 if query.total then 261 if query.total then
261 local stats = engine:select("SELECT COUNT(*) FROM `prosodyarchive` WHERE " .. t_concat(where, " AND "), unpack(args)); 262 local stats = engine:select("SELECT COUNT(*) FROM `prosodyarchive` WHERE " .. t_concat(where, " AND "), unpack(args));
262 if stats then 263 if stats then
263 local _total = stats() 264 for row in stats do
264 total = _total and _total[1]; 265 total = row[1];
266 end
265 end 267 end
266 if query.limit == 0 then -- Skip the real query 268 if query.limit == 0 then -- Skip the real query
267 return noop, total; 269 return noop, total;
268 end 270 end
269 end 271 end