From fb17749e1831bb0df49712994ca414c6e5b7b95b Mon Sep 17 00:00:00 2001 From: Dorian Wouters Date: Sat, 3 Jun 2017 23:11:35 -0400 Subject: [PATCH] Move database connection stuff to mysql_base --- .gitmodules | 3 - depends.txt | 2 + init.lua | 170 +++++----------------------------------------------- mysql | 1 - 4 files changed, 17 insertions(+), 159 deletions(-) delete mode 100644 .gitmodules create mode 100644 depends.txt delete mode 160000 mysql diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 4377b47..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "mysql"] - path = mysql - url = https://github.com/luapower/mysql diff --git a/depends.txt b/depends.txt new file mode 100644 index 0000000..2170244 --- /dev/null +++ b/depends.txt @@ -0,0 +1,2 @@ +mysql_base + diff --git a/init.lua b/init.lua index 512ed0b..1e7a81d 100644 --- a/init.lua +++ b/init.lua @@ -1,146 +1,29 @@ local modname = minetest.get_current_modname() local modpath = minetest.get_modpath(modname) -local thismod = {} +local thismod = { + enabled = false +} _G[modname] = thismod +if not mysql_base.enabled then + minetest.log('action', modname .. ": mysql_base disabled, not loading mod") + return +end + local singleplayer = minetest.is_singleplayer() -- Caching is OK since you can't open a game to -- multiplayer unless you restart it. if not minetest.setting_get(modname .. '.enable_singleplayer') and singleplayer then - core.log('action', modname .. ": Not adding auth handler because of singleplayer game") + minetest.log('action', modname .. ": Not adding auth handler because of singleplayer game") return end -local function setoverlay(tab, orig) - local mt = getmetatable(tab) or {} - mt.__index = function (tab, key) - if rawget(tab, key) ~= nil then - return rawget(tab, key) - else - return orig[key] - end - end - setmetatable(tab, mt) -end - -local function string_splitdots(s) - local temp = {} - local index = 0 - local last_index = string.len(s) - while true do - local i, e = string.find(s, '%.', index) - if i and e then - local next_index = e + 1 - local word_bound = i - 1 - table.insert(temp, string.sub(s, index, word_bound)) - index = next_index - else - if index > 0 and index <= last_index then - table.insert(temp, string.sub(s, index, last_index)) - elseif index == 0 then - temp = nil - end - break - end - end - return temp -end - -local mysql -do -- MySQL module loading - local env = { - require = function (module) - if module == 'mysql_h' then - return dofile(modpath .. '/mysql/mysql_h.lua') - else - return require(module) - end - end - } - setoverlay(env, _G) - local fn, msg = loadfile(modpath .. '/mysql/mysql.lua') - if not fn then error(msg) end - setfenv(fn, env) - local status - status, mysql = pcall(fn, {}) - if not status then - error(modname .. ' failed to load MySQL FFI interface: ' .. mysql) - end -end +enabled = true do - local get - do - get = function (name) return minetest.setting_get(modname .. '.' .. name) end - local cfgfile = get('cfgfile') - if type(cfgfile) == 'string' and cfgfile ~= '' then - local file = io.open(cfgfile, 'rb') - if not file then - error(modname .. ' failed to load specified config file at ' .. cfgfile) - end - local cfg, msg = minetest.deserialize(file:read('*a')) - file:close() - if not cfg then - error(modname .. ' failed to parse specified config file at ' .. cfgfile .. ': ' .. msg) - end - get = function (name) - if type(name) ~= 'string' or name == '' then - return nil - end - local parts = string_splitdots(name) - if not parts then - return cfg[name] - end - local tbl = cfg[parts[1]] - for n = 2, #parts do - if tbl == nil then - return nil - end - tbl = tbl[parts[n]] - end - return tbl - end - end - end + local get = mysql_base.mkget(modname) - local conn, dbname - do - -- MySQL API backend - mysql.config(get('db.api')) - - local connopts = get('db.connopts') - if (get('db.db') == nil) and (type(connopts) == 'table' and connopts.db == nil) then - error(modname .. ": missing database name parameter") - end - if type(connopts) ~= 'table' then - connopts = {} - -- Traditional connection parameters - connopts.host, connopts.user, connopts.port, connopts.pass, connopts.db = - get('db.host') or 'localhost', get('db.user'), get('db.port'), get('db.pass'), get('db.db') - end - connopts.charset = 'utf8' - connopts.options = connopts.options or {} - connopts.options.MYSQL_OPT_RECONNECT = true - conn = mysql.connect(connopts) - dbname = connopts.db - thismod.conn = conn - - -- LuaPower's MySQL interface throws an error when the connection fails, no need to check if - -- it succeeded. - - -- Ensure UTF-8 is in use. - -- If you use another encoding, kill yourself (unless it's UTF-32). - conn:query("SET NAMES 'utf8'") - conn:query("SET CHARACTER SET utf8") - conn:query("SET character_set_results = 'utf8', character_set_client = 'utf8'," .. - "character_set_connection = 'utf8', character_set_database = 'utf8'," .. - "character_set_server = 'utf8'") - - local set = function(setting, val) conn:query('SET ' .. setting .. '=' .. val) end - pcall(set, 'wait_timeout', 3600) - pcall(set, 'autocommit', 1) - pcall(set, 'max_allowed_packet', 67108864) - end + local conn, dbname = mysql_base.conn, mysql_base.dbname local tables = {} do -- Tables and schema settings @@ -184,18 +67,9 @@ do end end - local function table_exists(name) - conn:query("SHOW TABLES LIKE '" .. tables.auths.name .. "'") - local res = conn:store_result() - local exists = (res:row_count() ~= 0) - res:free() - return exists - end - thismod.table_exists = table_exists - local auth_table_created -- Auth table existence check and setup - if not table_exists(tables.auths.name) then + if not mysql_base.table_exists(tables.auths.name) then -- Auth table doesn't exist, create it local S = tables.auths.schema conn:query('CREATE TABLE ' .. tables.auths.name .. ' (' .. @@ -419,20 +293,6 @@ end minetest.register_authentication_handler(thismod.auth_handler) minetest.log('action', modname .. ": Registered auth handler") -local function ping() - if thismod.conn then - if not thismod.conn:ping() then - minetest.log('error', modname .. ": failed to ping database") - end - end - minetest.after(1800, ping) -end -minetest.after(10, ping) - -minetest.register_on_shutdown(function() - if thismod.conn then - thismod.get_auth_stmt:free_result() - thismod.conn:close() - thismod.conn = nil - end +mysql_base.register_on_shutdown(function() + thismod.get_auth_stmt:free_result() end) diff --git a/mysql b/mysql deleted file mode 160000 index 4bc8277..0000000 --- a/mysql +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4bc82774707695bf934682eb401ed44ab06d63d5