Merge 0.9->trunk
[prosody.git] / plugins / mod_storage_sql.lua
index ea25c90b39d9018349c65874ddb72574f9867213..eed3fec900784518a8860b1d5ed79199a85e397b 100644 (file)
@@ -228,7 +228,8 @@ local function rollback(...)
        return ...;
 end
 local function commit(...)
-       if not connection:commit() then return nil, "SQL commit failed"; end
+       local success,err = connection:commit();
+       if not success then return nil, "SQL commit failed: "..tostring(err); end
        return ...;
 end
 
@@ -298,6 +299,17 @@ function keyval_store:set(username, data)
        end
        if success then return ret, err; else return rollback(nil, ret); end
 end
+function keyval_store:users()
+       local stmt, err = dosql("SELECT DISTINCT `user` FROM `prosody` WHERE `host`=? AND `store`=?", host, self.store);
+       if not stmt then
+               return rollback(nil, err);
+       end
+       local next = stmt:rows();
+       return commit(function()
+               local row = next();
+               return row and row[1];
+       end);
+end
 
 local function map_store_get(key)
        local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
@@ -365,7 +377,7 @@ function list_store:scan(username, from, to, jid, typ)
        return nil, "not-implemented"
 end
 
-local driver = { name = "sql" };
+local driver = {};
 
 function driver:open(store, typ)
        if not typ then -- default key-value store
@@ -374,10 +386,9 @@ function driver:open(store, typ)
        return nil, "unsupported-store";
 end
 
-function driver:list_stores(username) -- Not to be confused with the list store type
-       local sql = (username == true
-               and "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`!=?"
-               or  "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`=?");
+function driver:stores(username)
+       local sql = "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`" ..
+               (username == true and "!=?" or "=?");
        if username == true or not username then
                username = "";
        end
@@ -385,11 +396,11 @@ function driver:list_stores(username) -- Not to be confused with the list store
        if not stmt then
                return rollback(nil, err);
        end
-       local stores = {};
-       for row in stmt:rows() do
-               stores[#stores+1] = row[1];
-       end
-       return commit(stores);
+       local next = stmt:rows();
+       return commit(function()
+               local row = next();
+               return row and row[1];
+       end);
 end
 
 function driver:purge(username)
@@ -400,4 +411,4 @@ function driver:purge(username)
        return commit(true, changed);
 end
 
-module:add_item("data-driver", driver);
+module:provides("storage", driver);