Software / code / prosody
Comparison
plugins/mod_storage_sql2.lua @ 5733:aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 10 Jul 2013 12:08:44 +0200 |
| parent | 5732:4aa1d6f5083a |
| child | 5734:49f1fed6e25e |
comparison
equal
deleted
inserted
replaced
| 5732:4aa1d6f5083a | 5733:aeeced7b0149 |
|---|---|
| 1 | 1 |
| 2 local json = require "util.json"; | 2 local json = require "util.json"; |
| 3 local resolve_relative_path = require "core.configmanager".resolve_relative_path; | 3 local resolve_relative_path = require "core.configmanager".resolve_relative_path; |
| 4 | |
| 5 local unpack = unpack | |
| 6 local function iterator(result) | |
| 7 return function(result) | |
| 8 local row = result(); | |
| 9 if row ~= nil then | |
| 10 return unpack(row); | |
| 11 end | |
| 12 end, result, nil; | |
| 13 end | |
| 4 | 14 |
| 5 local mod_sql = module:require("sql"); | 15 local mod_sql = module:require("sql"); |
| 6 local params = module:get_option("sql"); | 16 local params = module:get_option("sql"); |
| 7 | 17 |
| 8 local engine; -- TODO create engine | 18 local engine; -- TODO create engine |
| 198 return engine:transaction(function() | 208 return engine:transaction(function() |
| 199 return keyval_store_set(data); | 209 return keyval_store_set(data); |
| 200 end); | 210 end); |
| 201 end | 211 end |
| 202 function keyval_store:users() | 212 function keyval_store:users() |
| 203 return engine:transaction(function() | 213 local ok, result = engine:transaction(function() |
| 204 return engine:select("SELECT DISTINCT `user` FROM `prosody` WHERE `host`=? AND `store`=?", host, self.store); | 214 return engine:select("SELECT DISTINCT `user` FROM `prosody` WHERE `host`=? AND `store`=?", host, self.store); |
| 205 end); | 215 end); |
| 216 if not ok then return ok, result end | |
| 217 return iterator(result); | |
| 206 end | 218 end |
| 207 | 219 |
| 208 local driver = {}; | 220 local driver = {}; |
| 209 | 221 |
| 210 function driver:open(store, typ) | 222 function driver:open(store, typ) |
| 218 local sql = "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`" .. | 230 local sql = "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`" .. |
| 219 (username == true and "!=?" or "=?"); | 231 (username == true and "!=?" or "=?"); |
| 220 if username == true or not username then | 232 if username == true or not username then |
| 221 username = ""; | 233 username = ""; |
| 222 end | 234 end |
| 223 return engine:transaction(function() | 235 local ok, result = engine:transaction(function() |
| 224 return engine:select(sql, host, username); | 236 return engine:select(sql, host, username); |
| 225 end); | 237 end); |
| 238 if not ok then return ok, result end | |
| 239 return iterator(result); | |
| 226 end | 240 end |
| 227 | 241 |
| 228 function driver:purge(username) | 242 function driver:purge(username) |
| 229 return engine:transaction(function() | 243 return engine:transaction(function() |
| 230 local stmt,err = engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=?", host, username); | 244 local stmt,err = engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=?", host, username); |