Software /
code /
prosody
Comparison
plugins/mod_storage_sql.lua @ 5776:bd0ff8ae98a8
Remove all trailing whitespace
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 09 Aug 2013 17:48:21 +0200 |
parent | 5209:f5d121846d53 |
child | 6165:6a184b16b717 |
comparison
equal
deleted
inserted
replaced
5775:a6c2b8933507 | 5776:bd0ff8ae98a8 |
---|---|
91 if params.driver == "PostgreSQL" then | 91 if params.driver == "PostgreSQL" then |
92 create_sql = create_sql:gsub("`", "\""); | 92 create_sql = create_sql:gsub("`", "\""); |
93 elseif params.driver == "MySQL" then | 93 elseif params.driver == "MySQL" then |
94 create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT"); | 94 create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT"); |
95 end | 95 end |
96 | 96 |
97 local stmt, err = connection:prepare(create_sql); | 97 local stmt, err = connection:prepare(create_sql); |
98 if stmt then | 98 if stmt then |
99 local ok = stmt:execute(); | 99 local ok = stmt:execute(); |
100 local commit_ok = connection:commit(); | 100 local commit_ok = connection:commit(); |
101 if ok and commit_ok then | 101 if ok and commit_ok then |
157 if not ok or not DBI.Connect then | 157 if not ok or not DBI.Connect then |
158 return; -- Halt loading of this module | 158 return; -- Halt loading of this module |
159 end | 159 end |
160 | 160 |
161 params = params or { driver = "SQLite3" }; | 161 params = params or { driver = "SQLite3" }; |
162 | 162 |
163 if params.driver == "SQLite3" then | 163 if params.driver == "SQLite3" then |
164 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); | 164 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); |
165 end | 165 end |
166 | 166 |
167 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified"); | 167 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified"); |
168 | 168 |
169 dburi = db2uri(params); | 169 dburi = db2uri(params); |
170 connection = connections[dburi]; | 170 connection = connections[dburi]; |
171 | 171 |
172 assert(connect()); | 172 assert(connect()); |
173 | 173 |
174 -- Automatically create table, ignore failure (table probably already exists) | 174 -- Automatically create table, ignore failure (table probably already exists) |
175 create_table(); | 175 create_table(); |
176 end | 176 end |
177 | 177 |
178 local function serialize(value) | 178 local function serialize(value) |
207 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end | 207 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end |
208 -- run query | 208 -- run query |
209 local ok, err = stmt:execute(...); | 209 local ok, err = stmt:execute(...); |
210 if not ok and not test_connection() then error("connection failed"); end | 210 if not ok and not test_connection() then error("connection failed"); end |
211 if not ok then return nil, err; end | 211 if not ok then return nil, err; end |
212 | 212 |
213 return stmt; | 213 return stmt; |
214 end | 214 end |
215 local function getsql(sql, ...) | 215 local function getsql(sql, ...) |
216 return dosql(sql, host or "", user or "", store or "", ...); | 216 return dosql(sql, host or "", user or "", store or "", ...); |
217 end | 217 end |
234 end | 234 end |
235 | 235 |
236 local function keyval_store_get() | 236 local function keyval_store_get() |
237 local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?"); | 237 local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?"); |
238 if not stmt then return rollback(nil, err); end | 238 if not stmt then return rollback(nil, err); end |
239 | 239 |
240 local haveany; | 240 local haveany; |
241 local result = {}; | 241 local result = {}; |
242 for row in stmt:rows(true) do | 242 for row in stmt:rows(true) do |
243 haveany = true; | 243 haveany = true; |
244 local k = row.key; | 244 local k = row.key; |
254 return commit(haveany and result or nil); | 254 return commit(haveany and result or nil); |
255 end | 255 end |
256 local function keyval_store_set(data) | 256 local function keyval_store_set(data) |
257 local affected, err = setsql("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?"); | 257 local affected, err = setsql("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?"); |
258 if not affected then return rollback(affected, err); end | 258 if not affected then return rollback(affected, err); end |
259 | 259 |
260 if data and next(data) ~= nil then | 260 if data and next(data) ~= nil then |
261 local extradata = {}; | 261 local extradata = {}; |
262 for key, value in pairs(data) do | 262 for key, value in pairs(data) do |
263 if type(key) == "string" and key ~= "" then | 263 if type(key) == "string" and key ~= "" then |
264 local t, value = serialize(value); | 264 local t, value = serialize(value); |
312 end | 312 end |
313 | 313 |
314 local function map_store_get(key) | 314 local function map_store_get(key) |
315 local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); | 315 local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); |
316 if not stmt then return rollback(nil, err); end | 316 if not stmt then return rollback(nil, err); end |
317 | 317 |
318 local haveany; | 318 local haveany; |
319 local result = {}; | 319 local result = {}; |
320 for row in stmt:rows(true) do | 320 for row in stmt:rows(true) do |
321 haveany = true; | 321 haveany = true; |
322 local k = row.key; | 322 local k = row.key; |
332 return commit(haveany and result[key] or nil); | 332 return commit(haveany and result[key] or nil); |
333 end | 333 end |
334 local function map_store_set(key, data) | 334 local function map_store_set(key, data) |
335 local affected, err = setsql("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); | 335 local affected, err = setsql("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); |
336 if not affected then return rollback(affected, err); end | 336 if not affected then return rollback(affected, err); end |
337 | 337 |
338 if data and next(data) ~= nil then | 338 if data and next(data) ~= nil then |
339 if type(key) == "string" and key ~= "" then | 339 if type(key) == "string" and key ~= "" then |
340 local t, value = serialize(data); | 340 local t, value = serialize(data); |
341 if not t then return rollback(t, value); end | 341 if not t then return rollback(t, value); end |
342 local ok, err = setsql("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", key, t, value); | 342 local ok, err = setsql("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", key, t, value); |
363 | 363 |
364 local list_store = {}; | 364 local list_store = {}; |
365 list_store.__index = list_store; | 365 list_store.__index = list_store; |
366 function list_store:scan(username, from, to, jid, typ) | 366 function list_store:scan(username, from, to, jid, typ) |
367 user,store = username,self.store; | 367 user,store = username,self.store; |
368 | 368 |
369 local cols = {"from", "to", "jid", "typ"}; | 369 local cols = {"from", "to", "jid", "typ"}; |
370 local vals = { from , to , jid , typ }; | 370 local vals = { from , to , jid , typ }; |
371 local stmt, err; | 371 local stmt, err; |
372 local query = "SELECT * FROM `prosodyarchive` WHERE `host`=? AND `user`=? AND `store`=?"; | 372 local query = "SELECT * FROM `prosodyarchive` WHERE `host`=? AND `user`=? AND `store`=?"; |
373 | 373 |
374 query = query.." ORDER BY time"; | 374 query = query.." ORDER BY time"; |
375 --local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); | 375 --local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); |
376 | 376 |
377 return nil, "not-implemented" | 377 return nil, "not-implemented" |
378 end | 378 end |
379 | 379 |
380 local driver = {}; | 380 local driver = {}; |
381 | 381 |