Skip to content
This repository was archived by the owner on Aug 20, 2020. It is now read-only.

Commit fb17749

Browse files
committed
Move database connection stuff to mysql_base
1 parent 567ba48 commit fb17749

File tree

4 files changed

+17
-159
lines changed

4 files changed

+17
-159
lines changed

.gitmodules

-3
This file was deleted.

depends.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mysql_base
2+

init.lua

+15-155
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,29 @@
11
local modname = minetest.get_current_modname()
22
local modpath = minetest.get_modpath(modname)
33

4-
local thismod = {}
4+
local thismod = {
5+
enabled = false
6+
}
57
_G[modname] = thismod
68

9+
if not mysql_base.enabled then
10+
minetest.log('action', modname .. ": mysql_base disabled, not loading mod")
11+
return
12+
end
13+
714
local singleplayer = minetest.is_singleplayer() -- Caching is OK since you can't open a game to
815
-- multiplayer unless you restart it.
916
if not minetest.setting_get(modname .. '.enable_singleplayer') and singleplayer then
10-
core.log('action', modname .. ": Not adding auth handler because of singleplayer game")
17+
minetest.log('action', modname .. ": Not adding auth handler because of singleplayer game")
1118
return
1219
end
1320

14-
local function setoverlay(tab, orig)
15-
local mt = getmetatable(tab) or {}
16-
mt.__index = function (tab, key)
17-
if rawget(tab, key) ~= nil then
18-
return rawget(tab, key)
19-
else
20-
return orig[key]
21-
end
22-
end
23-
setmetatable(tab, mt)
24-
end
25-
26-
local function string_splitdots(s)
27-
local temp = {}
28-
local index = 0
29-
local last_index = string.len(s)
30-
while true do
31-
local i, e = string.find(s, '%.', index)
32-
if i and e then
33-
local next_index = e + 1
34-
local word_bound = i - 1
35-
table.insert(temp, string.sub(s, index, word_bound))
36-
index = next_index
37-
else
38-
if index > 0 and index <= last_index then
39-
table.insert(temp, string.sub(s, index, last_index))
40-
elseif index == 0 then
41-
temp = nil
42-
end
43-
break
44-
end
45-
end
46-
return temp
47-
end
48-
49-
local mysql
50-
do -- MySQL module loading
51-
local env = {
52-
require = function (module)
53-
if module == 'mysql_h' then
54-
return dofile(modpath .. '/mysql/mysql_h.lua')
55-
else
56-
return require(module)
57-
end
58-
end
59-
}
60-
setoverlay(env, _G)
61-
local fn, msg = loadfile(modpath .. '/mysql/mysql.lua')
62-
if not fn then error(msg) end
63-
setfenv(fn, env)
64-
local status
65-
status, mysql = pcall(fn, {})
66-
if not status then
67-
error(modname .. ' failed to load MySQL FFI interface: ' .. mysql)
68-
end
69-
end
21+
enabled = true
7022

7123
do
72-
local get
73-
do
74-
get = function (name) return minetest.setting_get(modname .. '.' .. name) end
75-
local cfgfile = get('cfgfile')
76-
if type(cfgfile) == 'string' and cfgfile ~= '' then
77-
local file = io.open(cfgfile, 'rb')
78-
if not file then
79-
error(modname .. ' failed to load specified config file at ' .. cfgfile)
80-
end
81-
local cfg, msg = minetest.deserialize(file:read('*a'))
82-
file:close()
83-
if not cfg then
84-
error(modname .. ' failed to parse specified config file at ' .. cfgfile .. ': ' .. msg)
85-
end
86-
get = function (name)
87-
if type(name) ~= 'string' or name == '' then
88-
return nil
89-
end
90-
local parts = string_splitdots(name)
91-
if not parts then
92-
return cfg[name]
93-
end
94-
local tbl = cfg[parts[1]]
95-
for n = 2, #parts do
96-
if tbl == nil then
97-
return nil
98-
end
99-
tbl = tbl[parts[n]]
100-
end
101-
return tbl
102-
end
103-
end
104-
end
105-
106-
local conn, dbname
107-
do
108-
-- MySQL API backend
109-
mysql.config(get('db.api'))
110-
111-
local connopts = get('db.connopts')
112-
if (get('db.db') == nil) and (type(connopts) == 'table' and connopts.db == nil) then
113-
error(modname .. ": missing database name parameter")
114-
end
115-
if type(connopts) ~= 'table' then
116-
connopts = {}
117-
-- Traditional connection parameters
118-
connopts.host, connopts.user, connopts.port, connopts.pass, connopts.db =
119-
get('db.host') or 'localhost', get('db.user'), get('db.port'), get('db.pass'), get('db.db')
120-
end
121-
connopts.charset = 'utf8'
122-
connopts.options = connopts.options or {}
123-
connopts.options.MYSQL_OPT_RECONNECT = true
124-
conn = mysql.connect(connopts)
125-
dbname = connopts.db
126-
thismod.conn = conn
24+
local get = mysql_base.mkget(modname)
12725

128-
-- LuaPower's MySQL interface throws an error when the connection fails, no need to check if
129-
-- it succeeded.
130-
131-
-- Ensure UTF-8 is in use.
132-
-- If you use another encoding, kill yourself (unless it's UTF-32).
133-
conn:query("SET NAMES 'utf8'")
134-
conn:query("SET CHARACTER SET utf8")
135-
conn:query("SET character_set_results = 'utf8', character_set_client = 'utf8'," ..
136-
"character_set_connection = 'utf8', character_set_database = 'utf8'," ..
137-
"character_set_server = 'utf8'")
138-
139-
local set = function(setting, val) conn:query('SET ' .. setting .. '=' .. val) end
140-
pcall(set, 'wait_timeout', 3600)
141-
pcall(set, 'autocommit', 1)
142-
pcall(set, 'max_allowed_packet', 67108864)
143-
end
26+
local conn, dbname = mysql_base.conn, mysql_base.dbname
14427

14528
local tables = {}
14629
do -- Tables and schema settings
@@ -184,18 +67,9 @@ do
18467
end
18568
end
18669

187-
local function table_exists(name)
188-
conn:query("SHOW TABLES LIKE '" .. tables.auths.name .. "'")
189-
local res = conn:store_result()
190-
local exists = (res:row_count() ~= 0)
191-
res:free()
192-
return exists
193-
end
194-
thismod.table_exists = table_exists
195-
19670
local auth_table_created
19771
-- Auth table existence check and setup
198-
if not table_exists(tables.auths.name) then
72+
if not mysql_base.table_exists(tables.auths.name) then
19973
-- Auth table doesn't exist, create it
20074
local S = tables.auths.schema
20175
conn:query('CREATE TABLE ' .. tables.auths.name .. ' (' ..
@@ -419,20 +293,6 @@ end
419293
minetest.register_authentication_handler(thismod.auth_handler)
420294
minetest.log('action', modname .. ": Registered auth handler")
421295

422-
local function ping()
423-
if thismod.conn then
424-
if not thismod.conn:ping() then
425-
minetest.log('error', modname .. ": failed to ping database")
426-
end
427-
end
428-
minetest.after(1800, ping)
429-
end
430-
minetest.after(10, ping)
431-
432-
minetest.register_on_shutdown(function()
433-
if thismod.conn then
434-
thismod.get_auth_stmt:free_result()
435-
thismod.conn:close()
436-
thismod.conn = nil
437-
end
296+
mysql_base.register_on_shutdown(function()
297+
thismod.get_auth_stmt:free_result()
438298
end)

mysql

-1
This file was deleted.

0 commit comments

Comments
 (0)