X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;ds=sidebyside;f=plugins%2Fmod_storage_sql.lua;h=b88efb6423b31ee274ab35efc09d4de4b72d9ac8;hb=8ee0370603ca03f15e7d3ea7242c6f1d9b9ef3a0;hp=3f28307d3046b34ad80746cf93d169bc14aff715;hpb=a689938fa45323f67433bcd5554affd3f4b0fc52;p=prosody.git diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index 3f28307d..b88efb64 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -28,36 +28,43 @@ local setmetatable = setmetatable; local xpcall = xpcall; local json = require "util.json"; +local DBI; local connection; local host,user,store = module.host; local params = module:get_option("sql"); local resolve_relative_path = require "core.configmanager".resolve_relative_path; -do -- process options to get a db connection - local DBI = require "DBI"; - - params = params or { driver = "SQLite3" }; - - if params.driver == "SQLite3" then - params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); +local function test_connection() + if not connection then return nil; end + if connection:ping() then + return true; + else + module:log("debug", "Database connection closed"); + connection = nil; end - - assert(params.driver and params.database, "Both the SQL driver and the database need to be specified"); - - prosody.unlock_globals(); - local dbh, err = DBI.Connect( - params.driver, params.database, - params.username, params.password, - params.host, params.port - ); - prosody.lock_globals(); - assert(dbh, err); +end +local function connect() + if not test_connection() then + prosody.unlock_globals(); + local dbh, err = DBI.Connect( + params.driver, params.database, + params.username, params.password, + params.host, params.port + ); + prosody.lock_globals(); + if not dbh then + module:log("debug", "Database connection failed: %s", tostring(err)); + return nil, err; + end + module:log("debug", "Successfully connected to database"); + dbh:autocommit(false); -- don't commit automatically + connection = dbh; + return connection; + end +end - dbh:autocommit(false); -- don't commit automatically - connection = dbh; - - -- Automatically create table, ignore failure (table probably already exists) +local function create_table() local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);"; if params.driver == "PostgreSQL" then create_sql = create_sql:gsub("`", "\""); @@ -69,10 +76,53 @@ do -- process options to get a db connection local commit_ok = connection:commit(); if ok and commit_ok then module:log("info", "Initialized new %s database with prosody table", params.driver); + local index_sql = "CREATE INDEX `prosody_index` ON `prosody` (`host`, `user`, `store`, `key`)"; + if params.driver == "PostgreSQL" then + index_sql = index_sql:gsub("`", "\""); + elseif params.driver == "MySQL" then + index_sql = index_sql:gsub("`([,)])", "`(20)%1"); + end + local stmt, err = connection:prepare(index_sql); + local ok, commit_ok, commit_err; + if stmt then + ok, err = stmt:execute(); + commit_ok, commit_err = connection:commit(); + end + if not(ok and commit_ok) then + module:log("warn", "Failed to create index (%s), lookups may not be optimised", err or commit_err); + end end end end +do -- process options to get a db connection + local ok; + prosody.unlock_globals(); + ok, DBI = pcall(require, "DBI"); + if not ok then + package.loaded["DBI"] = {}; + module:log("error", "Failed to load the LuaDBI library for accessing SQL databases: %s", DBI); + module:log("error", "More information on installing LuaDBI can be found at http://prosody.im/doc/depends#luadbi"); + end + prosody.lock_globals(); + if not ok or not DBI.Connect then + return; -- Halt loading of this module + 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"); + + assert(connect()); + + -- Automatically create table, ignore failure (table probably already exists) + create_table(); +end + local function serialize(value) local t = type(value); if t == "string" or t == "boolean" or t == "number" then @@ -101,9 +151,11 @@ local function getsql(sql, ...) end -- do prepared statement stuff local stmt, err = connection:prepare(sql); + if not stmt and not test_connection() then error("connection failed"); 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 or "", user or "", store or "", ...); + if not ok and not test_connection() then error("connection failed"); end if not ok then return nil, err; end return stmt; @@ -117,7 +169,7 @@ local function transact(...) -- ... end local function rollback(...) - connection:rollback(); -- FIXME check for rollback error? + if connection then connection:rollback(); end -- FIXME check for rollback error? return ...; end local function commit(...) @@ -127,7 +179,7 @@ end 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 + if not stmt then return rollback(nil, err); end local haveany; local result = {}; @@ -147,6 +199,7 @@ local function keyval_store_get() 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 = {}; @@ -174,18 +227,26 @@ local keyval_store = {}; keyval_store.__index = keyval_store; function keyval_store:get(username) user,store = username,self.store; + if not connection and not connect() then return nil, "Unable to connect to database"; end local success, ret, err = xpcall(keyval_store_get, debug.traceback); + if not connection and connect() then + success, ret, err = xpcall(keyval_store_get, debug.traceback); + end if success then return ret, err; else return rollback(nil, ret); end end function keyval_store:set(username, data) user,store = username,self.store; + if not connection and not connect() then return nil, "Unable to connect to database"; end local success, ret, err = xpcall(function() return keyval_store_set(data); end, debug.traceback); + if not connection and connect() then + success, ret, err = xpcall(function() return keyval_store_set(data); end, debug.traceback); + end 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 + if not stmt then return rollback(nil, err); end local haveany; local result = {}; @@ -205,6 +266,7 @@ local function map_store_get(key) 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