X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=plugins%2Fmod_storage_sql.lua;h=a5bb5bfa5cddb621e32a6a6b0c1515e8891fc6ca;hb=41977f3e683e553a2a96b23b467e2ac6b8c3cec3;hp=c20e4473a47b9c0e35123a3e4577bd6a7e00439c;hpb=e358ec18ba4a5385d20691cb5455c54bf9f18b78;p=prosody.git diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index c20e4473..a5bb5bfa 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -49,7 +49,7 @@ local function db2uri(params) end -local resolve_relative_path = require "core.configmanager".resolve_relative_path; +local resolve_relative_path = require "util.paths".resolve_relative_path; local function test_connection() if not connection then return nil; end @@ -79,8 +79,8 @@ local function connect() connection = dbh; connections[dburi] = dbh; - return connection; end + return connection; end local function create_table() @@ -93,7 +93,7 @@ local function create_table() elseif params.driver == "MySQL" then create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT"); end - + local stmt, err = connection:prepare(create_sql); if stmt then local ok = stmt:execute(); @@ -159,18 +159,18 @@ do -- process options to get a db connection end params = params or { driver = "SQLite3" }; - + if params.driver == "SQLite3" then params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); end - + assert(params.driver and params.database, "Both the SQL driver and the database need to be specified"); dburi = db2uri(params); connection = connections[dburi]; - + assert(connect()); - + -- Automatically create table, ignore failure (table probably already exists) create_table(); end @@ -209,7 +209,7 @@ local function dosql(sql, ...) 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, ...) @@ -228,14 +228,15 @@ local function rollback(...) return ...; end local function commit(...) - if not connection:commit() then return nil, "SQL commit failed"; end + local success,err = connection:commit(); + if not success then return nil, "SQL commit failed: "..tostring(err); end return ...; end local function keyval_store_get() local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?"); if not stmt then return rollback(nil, err); end - + local haveany; local result = {}; for row in stmt:rows(true) do @@ -255,7 +256,7 @@ end local function keyval_store_set(data) local affected, err = setsql("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?"); if not affected then return rollback(affected, err); end - + if data and next(data) ~= nil then local extradata = {}; for key, value in pairs(data) do @@ -298,11 +299,22 @@ function keyval_store:set(username, data) end if success then return ret, err; else return rollback(nil, ret); end end +function keyval_store:users() + local stmt, err = dosql("SELECT DISTINCT `user` FROM `prosody` WHERE `host`=? AND `store`=?", host, self.store); + if not stmt then + return rollback(nil, err); + end + local next = stmt:rows(); + return commit(function() + local row = next(); + return row and row[1]; + 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 rollback(nil, err); end - + local haveany; local result = {}; for row in stmt:rows(true) do @@ -322,7 +334,7 @@ end 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 not affected then return rollback(affected, err); end - + if data and next(data) ~= nil then if type(key) == "string" and key ~= "" then local t, value = serialize(data); @@ -353,31 +365,30 @@ local list_store = {}; list_store.__index = list_store; function list_store:scan(username, from, to, jid, typ) user,store = username,self.store; - + local cols = {"from", "to", "jid", "typ"}; local vals = { from , to , jid , typ }; local stmt, err; 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`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); - + return nil, "not-implemented" end -local driver = { name = "sql" }; +local driver = {}; function driver:open(store, typ) - if not typ then -- default key-value store - return setmetatable({ store = store }, keyval_store); + if typ and typ ~= "keyval" then + return nil, "unsupported-store"; end - return nil, "unsupported-store"; + return setmetatable({ store = store }, keyval_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`=?"); +function driver:stores(username) + local sql = "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`" .. + (username == true and "!=?" or "=?"); if username == true or not username then username = ""; end @@ -385,11 +396,11 @@ function driver:list_stores(username) -- Not to be confused with the list store if not stmt then return rollback(nil, err); end - local stores = {}; - for row in stmt:rows() do - stores[#stores+1] = row[1]; - end - return commit(stores); + local next = stmt:rows(); + return commit(function() + local row = next(); + return row and row[1]; + end); end function driver:purge(username) @@ -400,4 +411,4 @@ function driver:purge(username) return commit(true, changed); end -module:add_item("data-driver", driver); +module:provides("storage", driver);