Changeset

4073:79fd38ab224b

mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 05 Jan 2011 06:56:36 +0500
parents 4072:ce3d100d9ebb
children 4074:d2c9d9f569f9
files plugins/mod_storage_sql.lua
diffstat 1 files changed, 30 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_storage_sql.lua	Wed Jan 05 06:34:23 2011 +0500
+++ b/plugins/mod_storage_sql.lua	Wed Jan 05 06:56:36 2011 +0500
@@ -25,6 +25,7 @@
 local pairs = pairs;
 local next = next;
 local setmetatable = setmetatable;
+local xpcall = xpcall;
 local json = require "util.json";
 
 local connection;
@@ -115,10 +116,7 @@
 	return ...;
 end
 
-local keyval_store = {};
-keyval_store.__index = keyval_store;
-function keyval_store:get(username)
-	user,store = username,self.store;
+local function keyval_store_get()
 	local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=?");
 	if not stmt then return nil, err; end
 	
@@ -138,9 +136,7 @@
 	end
 	return commit(haveany and result or nil);
 end
-function keyval_store:set(username, data)
-	user,store = username,self.store;
-	-- start transaction
+local function keyval_store_set(data)
 	local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=?");
 	
 	if data and next(data) ~= nil then
@@ -165,10 +161,20 @@
 	return commit(true);
 end
 
-local map_store = {};
-map_store.__index = map_store;
-function map_store:get(username, key)
+local keyval_store = {};
+keyval_store.__index = keyval_store;
+function keyval_store:get(username)
 	user,store = username,self.store;
+	local success, ret, err = xpcall(keyval_store_get, debug.traceback);
+	if success then return ret, err; else return rollback(nil, ret); end
+end
+function keyval_store:set(username, data)
+	user,store = username,self.store;
+	local success, ret, err = xpcall(function() return keyval_store_set(data); end, debug.traceback);
+	if success then return ret, err; else return rollback(nil, ret); 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 "");
 	if not stmt then return nil, err; end
 	
@@ -188,9 +194,7 @@
 	end
 	return commit(haveany and result[key] or nil);
 end
-function map_store:set(username, key, data)
-	user,store = username,self.store;
-	-- start transaction
+local function map_store_set(key, data)
 	local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
 	
 	if data and next(data) ~= nil then
@@ -206,6 +210,19 @@
 	return commit(true);
 end
 
+local map_store = {};
+map_store.__index = map_store;
+function map_store:get(username, key)
+	user,store = username,self.store;
+	local success, ret, err = xpcall(function() return map_store_get(key); end, debug.traceback);
+	if success then return ret, err; else return rollback(nil, ret); end
+end
+function map_store:set(username, key, data)
+	user,store = username,self.store;
+	local success, ret, err = xpcall(function() return map_store_set(key, data); end, debug.traceback);
+	if success then return ret, err; else return rollback(nil, ret); end
+end
+
 local list_store = {};
 list_store.__index = list_store;
 function list_store:scan(username, from, to, jid, typ)