X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=plugins%2Fmod_storage_sql.lua;h=53f1ea0a47602508d6594011897d8c509ffb5ca9;hb=6d7a9cba5240a142b1a1e397e6248cf13c261cca;hp=e3eb3c7747deafe475b67689967f34f490545a31;hpb=f510b0e6a574444d33d79dc9199d15783f923634;p=prosody.git diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index e3eb3c77..53f1ea0a 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -3,16 +3,16 @@ DB Tables: Prosody - key-value, map - | host | user | store | key | subkey | type | value | + | host | user | store | key | type | value | ProsodyArchive - list | host | user | store | key | time | stanzatype | jsonvalue | Mapping: Roster - Prosody - | host | user | "roster" | "contactjid" | item-subkey | type | value | - | host | user | "roster" | NULL | NULL | "json" | roster[false] data | + | host | user | "roster" | "contactjid" | type | value | + | host | user | "roster" | NULL | "json" | roster[false] data | Account - Prosody - | host | user | "accounts" | "username" | NULL | type | value | + | host | user | "accounts" | "username" | type | value | Offline - ProsodyArchive | host | user | "offline" | "contactjid" | time | "message" | json|XML | @@ -25,16 +25,17 @@ local tonumber = tonumber; local pairs = pairs; local next = next; local setmetatable = setmetatable; -local json = { stringify = function(s) return require"util.serialization".serialize(s) end, parse = require"util.serialization".deserialze }; +local json = require "util.json"; local connection = ...; local host,user,store = module.host; +local params = module:get_option("sql"); do -- process options to get a db connection local DBI = require "DBI"; - local params = module:get_option("sql") or { driver = "SQLite3", database = "prosody.sqlite" }; - assert(params and params.driver and params.database, "invalid params"); + params = params or { driver = "SQLite3", database = "prosody.sqlite" }; + assert(params.driver and params.database, "invalid params"); prosody.unlock_globals(); local dbh, err = DBI.Connect( @@ -49,16 +50,16 @@ do -- process options to get a db connection connection = dbh; if params.driver == "SQLite3" then -- auto initialize - local stmt = assert(connection:prepare("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='Prosody';")); + local stmt = assert(connection:prepare("SELECT COUNT(*) FROM `sqlite_master` WHERE `type`='table' AND `name`='Prosody';")); local ok = assert(stmt:execute()); local count = stmt:fetch()[1]; if count == 0 then - local stmt = assert(connection:prepare("CREATE TABLE Prosody (host TEXT, user TEXT, store TEXT, key TEXT, subkey TEXT, type TEXT, value TEXT);")); + local stmt = assert(connection:prepare("CREATE TABLE `Prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);")); assert(stmt:execute()); - assert(connection:commit()); module:log("debug", "Initialized new SQLite3 database"); end - --print("===", json.stringify()) + assert(connection:commit()); + --print("===", json.encode()) end end @@ -67,7 +68,7 @@ local function serialize(value) if t == "string" or t == "boolean" or t == "number" then return t, tostring(value); elseif t == "table" then - local value,err = json.stringify(value); + local value,err = json.encode(value); if value then return "json", value; end return nil, err; end @@ -79,17 +80,20 @@ local function deserialize(t, value) if value == "true" then return true; elseif value == "false" then return false; end elseif t == "number" then return tonumber(value); - elseif value == "json" then - return json.parse(value); + elseif t == "json" then + return json.decode(value); end end local function getsql(sql, ...) + if params.driver == "PostgreSQL" then + sql = sql:gsub("`", "\""); + end -- do prepared statement stuff local stmt, err = connection:prepare(sql); - if not stmt then return nil, err; 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, user, store, ...); + local ok, err = stmt:execute(host or "", user or "", store or "", ...); if not ok then return nil, err; end return stmt; @@ -115,7 +119,7 @@ local keyval_store = {}; keyval_store.__index = keyval_store; function keyval_store:get(username) user,store = username,self.store; - local stmt, err = getsql("SELECT * FROM Prosody WHERE host IS ? AND user IS ? AND store IS ? AND subkey IS NULL"); + local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=?"); if not stmt then return nil, err; end local haveany; @@ -124,28 +128,28 @@ function keyval_store:get(username) haveany = true; local k = row.key; local v = deserialize(row.type, row.value); - if v then - if k then result[k] = v; elseif type(v) == "table" then + if k and v then + if k ~= "" then result[k] = v; elseif type(v) == "table" then for a,b in pairs(v) do result[a] = b; end end end end - return haveany and result or nil; + return commit(haveany and result or nil); end function keyval_store:set(username, data) user,store = username,self.store; -- start transaction - local affected, err = setsql("DELETE FROM Prosody WHERE host IS ? AND user IS ? AND store IS ? AND subkey IS NULL"); + local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=?"); if data and next(data) ~= nil then local extradata = {}; for key, value in pairs(data) do - if type(key) == "string" then + if type(key) == "string" and key ~= "" then local t, value = serialize(value); if not t then return rollback(t, value); end - local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,type,value) VALUES (?,?,?,?,?,?)", key, t, value); + local ok, err = setsql("INSERT INTO `Prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", key, t, value); if not ok then return rollback(ok, err); end else extradata[key] = value; @@ -154,7 +158,7 @@ function keyval_store:set(username, data) if next(extradata) ~= nil then local t, extradata = serialize(extradata); if not t then return rollback(t, extradata); end - local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,type,value) VALUES (?,?,?,?,?,?)", nil, t, extradata); + local ok, err = setsql("INSERT INTO `Prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", "", t, extradata); if not ok then return rollback(ok, err); end end end @@ -165,47 +169,38 @@ local map_store = {}; map_store.__index = map_store; function map_store:get(username, key) user,store = username,self.store; - local stmt, err = getsql("SELECT * FROM Prosody WHERE host IS ? AND user IS ? AND store IS ? AND key IS ?", 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 local haveany; local result = {}; for row in stmt:rows(true) do haveany = true; - local k = row.subkey; + local k = row.key; local v = deserialize(row.type, row.value); - if v then - if k then result[k] = v; elseif type(v) == "table" then + if k and v then + if k ~= "" then result[k] = v; elseif type(v) == "table" then for a,b in pairs(v) do result[a] = b; end end end end - return haveany and result or nil; + return commit(haveany and result[key] or nil); end function map_store:set(username, key, data) user,store = username,self.store; -- start transaction - local affected, err = setsql("DELETE FROM Prosody WHERE host IS ? AND user IS ? AND store IS ? AND key IS ?", key); + local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); if data and next(data) ~= nil then - local extradata = {}; - for subkey, value in pairs(data) do - if type(subkey) == "string" then - local t, value = serialize(value); - if not t then return rollback(t, value); end - local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,subkey,type,value) VALUES (?,?,?,?,?,?)", key, subkey, t, value); - if not ok then return rollback(ok, err); end - else - extradata[subkey] = value; - end - end - if next(extradata) ~= nil then - local t, extradata = serialize(extradata); - if not t then return rollback(t, extradata); end - local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,subkey,type,value) VALUES (?,?,?,?,?,?)", key, nil, t, extradata); + if type(key) == "string" and key ~= "" then + local t, value = serialize(data); + if not t then return rollback(t, value); end + local ok, err = setsql("INSERT INTO `Prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", key, t, value); if not ok then return rollback(ok, err); end + else + -- TODO non-string keys end end return commit(true); @@ -219,10 +214,10 @@ function list_store:scan(username, from, to, jid, typ) local cols = {"from", "to", "jid", "typ"}; local vals = { from , to , jid , typ }; local stmt, err; - local query = "SELECT * FROM ProsodyArchive WHERE host IS ? AND user IS ? AND store IS ?"; + local query = "SELECT * FROM `ProsodyArchive` WHERE `host`=? AND `user`=? AND `store`=?"; query = query.." ORDER BY time"; - --local stmt, err = getsql("SELECT * FROM Prosody WHERE host IS ? AND user IS ? AND store IS ? AND key IS ?", key); + --local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); return nil, "not-implemented" end