Merge 0.9->trunk
[prosody.git] / plugins / mod_storage_sql.lua
index c5a711bc39d739a33b2b04f74a5a38a70040fe3a..f6fa94e700930cb9e87536c0b6507c548cd4001b 100644 (file)
@@ -79,8 +79,8 @@ local function connect()
                connection = dbh;
 
                connections[dburi] = dbh;
-               return connection;
        end
+       return connection;
 end
 
 local function create_table()
@@ -298,6 +298,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 +376,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,30 +385,29 @@ 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
        local stmt, err = dosql(sql, host, username);
        if not stmt then
-               return nil, err;
-       end
-       local stores = {};
-       for row in stmt:rows() do
-               stores[#stores+1] = row[1];
+               return rollback(nil, err);
        end
-       return stores;
+       local next = stmt:rows();
+       return commit(function()
+               local row = next();
+               return row and row[1];
+       end);
 end
 
 function driver:purge(username)
        local stmt, err = dosql("DELETE FROM `prosody` WHERE `host`=? AND `user`=?", host, username);
-       if not stmt then return stmt, err; end
+       if not stmt then return rollback(stmt, err); end
        local changed, err = stmt:affected();
-       if not changed then return changed, err; end
-       return true, changed;
+       if not changed then return rollback(changed, err); end
+       return commit(true, changed);
 end
 
-module:add_item("data-driver", driver);
+module:provides("storage", driver);