Software /
code /
prosody
Comparison
plugins/mod_storage_sql.lua @ 4007:062b849ca088
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 |
parent | 4004:c1b3ecbed6c0 |
child | 4096:3b991ceb228e |
comparison
equal
deleted
inserted
replaced
4006:32073a0fbcca | 4007:062b849ca088 |
---|---|
23 local tostring = tostring; | 23 local tostring = tostring; |
24 local tonumber = tonumber; | 24 local tonumber = tonumber; |
25 local pairs = pairs; | 25 local pairs = pairs; |
26 local next = next; | 26 local next = next; |
27 local setmetatable = setmetatable; | 27 local setmetatable = setmetatable; |
28 local xpcall = xpcall; | |
28 local json = require "util.json"; | 29 local json = require "util.json"; |
29 | 30 |
30 local connection; | 31 local connection; |
31 local host,user,store = module.host; | 32 local host,user,store = module.host; |
32 local params = module:get_option("sql"); | 33 local params = module:get_option("sql"); |
113 local function commit(...) | 114 local function commit(...) |
114 if not connection:commit() then return nil, "SQL commit failed"; end | 115 if not connection:commit() then return nil, "SQL commit failed"; end |
115 return ...; | 116 return ...; |
116 end | 117 end |
117 | 118 |
118 local keyval_store = {}; | 119 local function keyval_store_get() |
119 keyval_store.__index = keyval_store; | |
120 function keyval_store:get(username) | |
121 user,store = username,self.store; | |
122 local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=?"); | 120 local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=?"); |
123 if not stmt then return nil, err; end | 121 if not stmt then return nil, err; end |
124 | 122 |
125 local haveany; | 123 local haveany; |
126 local result = {}; | 124 local result = {}; |
136 end | 134 end |
137 end | 135 end |
138 end | 136 end |
139 return commit(haveany and result or nil); | 137 return commit(haveany and result or nil); |
140 end | 138 end |
141 function keyval_store:set(username, data) | 139 local function keyval_store_set(data) |
142 user,store = username,self.store; | |
143 -- start transaction | |
144 local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=?"); | 140 local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=?"); |
145 | 141 |
146 if data and next(data) ~= nil then | 142 if data and next(data) ~= nil then |
147 local extradata = {}; | 143 local extradata = {}; |
148 for key, value in pairs(data) do | 144 for key, value in pairs(data) do |
163 end | 159 end |
164 end | 160 end |
165 return commit(true); | 161 return commit(true); |
166 end | 162 end |
167 | 163 |
168 local map_store = {}; | 164 local keyval_store = {}; |
169 map_store.__index = map_store; | 165 keyval_store.__index = keyval_store; |
170 function map_store:get(username, key) | 166 function keyval_store:get(username) |
171 user,store = username,self.store; | 167 user,store = username,self.store; |
168 local success, ret, err = xpcall(keyval_store_get, debug.traceback); | |
169 if success then return ret, err; else return rollback(nil, ret); end | |
170 end | |
171 function keyval_store:set(username, data) | |
172 user,store = username,self.store; | |
173 local success, ret, err = xpcall(function() return keyval_store_set(data); end, debug.traceback); | |
174 if success then return ret, err; else return rollback(nil, ret); end | |
175 end | |
176 | |
177 local function map_store_get(key) | |
172 local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); | 178 local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); |
173 if not stmt then return nil, err; end | 179 if not stmt then return nil, err; end |
174 | 180 |
175 local haveany; | 181 local haveany; |
176 local result = {}; | 182 local result = {}; |
186 end | 192 end |
187 end | 193 end |
188 end | 194 end |
189 return commit(haveany and result[key] or nil); | 195 return commit(haveany and result[key] or nil); |
190 end | 196 end |
191 function map_store:set(username, key, data) | 197 local function map_store_set(key, data) |
192 user,store = username,self.store; | |
193 -- start transaction | |
194 local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); | 198 local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); |
195 | 199 |
196 if data and next(data) ~= nil then | 200 if data and next(data) ~= nil then |
197 if type(key) == "string" and key ~= "" then | 201 if type(key) == "string" and key ~= "" then |
198 local t, value = serialize(data); | 202 local t, value = serialize(data); |
204 end | 208 end |
205 end | 209 end |
206 return commit(true); | 210 return commit(true); |
207 end | 211 end |
208 | 212 |
213 local map_store = {}; | |
214 map_store.__index = map_store; | |
215 function map_store:get(username, key) | |
216 user,store = username,self.store; | |
217 local success, ret, err = xpcall(function() return map_store_get(key); end, debug.traceback); | |
218 if success then return ret, err; else return rollback(nil, ret); end | |
219 end | |
220 function map_store:set(username, key, data) | |
221 user,store = username,self.store; | |
222 local success, ret, err = xpcall(function() return map_store_set(key, data); end, debug.traceback); | |
223 if success then return ret, err; else return rollback(nil, ret); end | |
224 end | |
225 | |
209 local list_store = {}; | 226 local list_store = {}; |
210 list_store.__index = list_store; | 227 list_store.__index = list_store; |
211 function list_store:scan(username, from, to, jid, typ) | 228 function list_store:scan(username, from, to, jid, typ) |
212 user,store = username,self.store; | 229 user,store = username,self.store; |
213 | 230 |