-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdatabase_lsqlite.lua
132 lines (114 loc) · 2.54 KB
/
database_lsqlite.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
--[[
File: database_raw.lua
lSQLite database functions:
load_db()
save_share_db()
get_claim(<pos or index>, direct access)
set_claim(data, index)
get_player_claims(player name)
update_claims(claims table)
]]
error("lSqlite backend is incomplete")
local worldpath = minetest.get_worldpath()
local ie = minetest.request_insecure_environment()
if not ie then
error("Cannot access insecure environment!")
end
local sql = ie.require("lsqlite3")
-- Remove public table
if sqlite3 then
sqlite3 = nil
end
local db = sql.open(worldpath .. "/s_protect.sqlite3")
local function sql_exec(q)
if db:exec(q) ~= sql.OK then
minetest.log("info", "[simple_protection] lSQLite: " .. db:errmsg())
end
end
local function sql_row(q)
q = q .. " LIMIT 1;"
for row in db:nrows(q) do
return row
end
end
sql_exec([[
CREATE TABLE IF NOT EXISTS claims (
id INTEGER PRIMARY KEY AUTOINCREMENT,
x INTEGER,
y INTEGER,
z INTEGER,
owner TEXT,
shared TEXT,
data TEXT
);
CREATE TABLE IF NOT EXISTS shares (
id INTEGER PRIMARY KEY AUTOINCREMENT,
owner TEXT,
shared TEXT,
data TEXT
);
]])
function s_protect.load_db() end
function s_protect.load_shareall() end
function s_protect.save_share_db() end
function s_protect.set_claim(cpos, claim)
local id, row = s_protect.get_claim(cpos)
if not claim then
if not id then
-- Claim never existed
return
end
-- Remove claim
sql_exec(
("DELETE FROM claims WHERE id = %i LIMIT 1;"):format(id)
)
end
if id then
local vals = {}
for k, v in pairs(claim) do
if row[k] ~= v and type(v) == "string" then
vals[#vals + 1] = ("%s = `%s`"):format(k, v)
end
end
if #vals == 0 then
return
end
sql_exec(
("UPDATE claims SET %s WHERE id = %i LIMIT 1;")
:format(table.concat(vals, ","), id)
)
else
sql_exec(
("INSERT INTO claims VALUES (%i, %i, %i, %s, %s, %s);")
:format(pos.x, pos.y, pos.z, claim.owner,
claim.shared or "", claim.data or "")
)
end
end
function s_protect.get_claim(cpos)
local q
if type(pos) == "number" then
-- Direct index
q = "id = " .. cpos
else
q = ("x = %i AND y = %i AND z = %z"):format(cpos.x, cpos.y, cpos.z)
end
local row = sql_row("SELECT id, owner, shared, data FROM claims WHERE " .. q)
if not row then
return
end
local id = row.id
row.id = nil
return id, row
end
function s_protect.get_player_claims(owner)
local q = ("SELECT * FROM claims WHERE owner = %s;"):format(owner)
local vals = {}
for row in db:nrows(q) do
vals[#vals + 1] = row
end
return vals
end
function s_protect.update_claims(updated)
error("Inefficient. To be removed.")
end