-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstorage_sqlite.lua
More file actions
175 lines (109 loc) · 3.32 KB
/
storage_sqlite.lua
File metadata and controls
175 lines (109 loc) · 3.32 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
-- Implements the cSQLiteStorage class
-- Load all the queries
local g_Queries = {}
local Path = cPluginManager:Get():GetCurrentPlugin():GetLocalFolder() .. "/Queries"
for _, FileName in ipairs(cFile:GetFolderContents(Path)) do
if (FileName:match("%.sql$")) then
g_Queries[FileName:match("^(.*)%.sql$")] = cFile:ReadWholeFile(Path .. "/" .. FileName)
end
end
cSQLiteStorage = {}
function cSQLiteStorage:new()
local Obj = {}
setmetatable(Obj, cSQLiteStorage)
self.__index = self
local ErrorCode, ErrorMsg;
Obj.DB, ErrorCode, ErrorMsg = sqlite3.open(cPluginManager:Get():GetCurrentPlugin():GetLocalFolder() .. "/storage.sqlite")
if (Obj.DB == nil) then
LOGWARNING("Database could not be opened. Aborting");
error(ErrMsg); -- Abort the plugin
end
-- Initialize the database
Obj:ExecuteCommand("initialize")
return Obj
end
--- Executes a query that was loaded before.
-- The parameters is a dictionary. The key is the name of the parameter.
-- This can be found with a $ or : in front of it in the actual query.
-- If a callback is given it calls that for each row where the parameter is a dictionary
-- Returns true on success, while it returns false with the error message when failing
function cSQLiteStorage:ExecuteCommand(a_QueryName, a_Parameters, a_Callback)
local Commands = assert(g_Queries[a_QueryName], "Requested Query doesn't exist")
for _, Sql in pairs(StringSplit(Commands, ";")) do
local Stmt, ErrCode, ErrMsg = self.DB:prepare(Sql)
if (not Stmt) then
LOGWARNING("Cannot prepare query \"" .. a_QueryName .. "\": " .. (ErrCode or "<unknown>") .. " (" .. (ErrMsg or "<no message>") .. ")")
return false, ErrorMsg or "<no message>"
end
if (a_Parameters ~= nil) then
Stmt:bind_names(a_Parameters)
end
if (a_Callback ~= nil) then
for val in Stmt:nrows() do
if (a_Callback(val)) then
break
end
end
else
Stmt:step()
end
Stmt:finalize()
end
return true
end
-- Finds the UUID in the database and returns its password
function cSQLiteStorage:GetPasswordFromUUID(a_UUID)
local Password;
local Success, ErrorMsg = self:ExecuteCommand("get_password",
{
uuid = a_UUID
},
function(a_Values)
Password = a_Values["password"]
return true
end
)
if (not Success) then
return false, ErrorMsg
end
if (not Password) then
return false, "The player does not exist"
end
return Password
end
-- Registers a username. It the user already exists it updates the password.
function cSQLiteStorage:RegisterOrChangePassword(a_UUID, a_Password)
local Success, ErrorMsg = self:ExecuteCommand("update_password",
{
uuid = a_UUID,
password = cCryptoHash.md5HexString(a_Password)
}
)
if (not Success) then
return false, ErrorMsg
end
return true
end
-- Checks if a UUID is known in the database
function cSQLiteStorage:UUIDExists(a_UUID)
local Pwd, ErrorMsg = self:GetPasswordFromUUID(a_UUID)
if (type(Pwd) == "string") then
return true
end
return false, ErrorMsg
end
-- Removes a UUID from the database
function cSQLiteStorage:DeleteUUID(a_UUID)
local Success, ErrorMsg = self:ExecuteCommand("remove_account",
{
uuid = a_UUID
}
)
if (not Success) then
return false, ErrorMsg
end
return true
end
function cSQLiteStorage:Disable()
-- cSQLiteStorage doesn't have to save the passwords when disabling.
end