Diff

plugins/mod_storage_sql.lua @ 5043:2856e1cfbe95

Merge with Zash
author Matthew Wild <mwild1@gmail.com>
date Sat, 28 Jul 2012 20:59:03 +0100
parent 5040:685403a6fee1
child 5054:97385c45e670
line wrap: on
line diff
--- a/plugins/mod_storage_sql.lua	Sat Jul 28 18:50:04 2012 +0100
+++ b/plugins/mod_storage_sql.lua	Sat Jul 28 20:59:03 2012 +0100
@@ -175,7 +175,7 @@
 	end
 end
 
-local function getsql(sql, ...)
+local function dosql(sql, ...)
 	if params.driver == "PostgreSQL" then
 		sql = sql:gsub("`", "\"");
 	end
@@ -184,12 +184,15 @@
 	if not stmt and not test_connection() then error("connection failed"); end
 	if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end
 	-- run query
-	local ok, err = stmt:execute(host or "", user or "", store or "", ...);
+	local ok, err = stmt:execute(...);
 	if not ok and not test_connection() then error("connection failed"); end
 	if not ok then return nil, err; end
 	
 	return stmt;
 end
+local function getsql(sql, ...)
+	return dosql(sql, host or "", user or "", store or "", ...);
+end
 local function setsql(sql, ...)
 	local stmt, err = getsql(sql, ...);
 	if not stmt then return stmt, err; end
@@ -349,4 +352,30 @@
 	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`=?");
+	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];
+	end
+	return stores;
+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
+	local changed, err = stmt:affected();
+	if not changed then return changed, err; end
+	return true, changed;
+end
+
 module:add_item("data-driver", driver);