Repository navigation
Expand file tree
/
Copy pathStorage.lua
More file actions
309 lines (241 loc) · 8.1 KB
/
Copy pathStorage.lua
File metadata and controls
309 lines (241 loc) · 8.1 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
-- Storage.lua
-- Implements the storage access object, shielding the rest of the code away from the DB
--[[
The cStorage class is the interface to the underlying storage, the SQLite database.
Also, a g_Storage global variable is declared, it holds the single instance of the storage.
--]]
cStorage = {}
cStorage.__index = cStorage
g_Storage = {}
--- Initializes the storage subsystem, creates the g_Storage object
-- Returns true if successful, false if not
function InitializeStorage(a_FilePath)
g_Storage = cStorage:new()
if not(g_Storage:OpenDB(a_FilePath)) then
return false
end
return true
end
function cStorage:new()
return setmetatable({}, cStorage)
end
--- Opens the DB and makes sure it has all the columns needed
-- Returns true if successful, false otherwise
function cStorage:OpenDB(a_FilePath)
assert(a_FilePath)
assert(self)
local errCode, errMsg
self.m_SqliteDB, errCode, errMsg = sqlite3.open(a_FilePath)
if not(self.m_SqliteDB) then
LOGWARNING(PluginPrefix .. "Cannot open spawners.sqlite, error " .. errCode .. " (" .. errMsg ..")")
return false
end
if not(self:CreateTable("Spawners", {"ID INTEGER PRIMARY KEY AUTOINCREMENT", "Name", "WorldName", "PosX", "PosY", "PosZ", "Radius", "Interval", "IsEnabled" })) then
LOGWARNING(PluginPrefix .. "Cannot create DB tables!")
return false
end
return true
end
--- Executes the SQL command given, calling the a_Callback for each result
-- If the SQL command fails, prints it out on the server console and returns false
-- Returns true on success
function cStorage:DBExec(a_SQL, a_Callback, a_CallbackParam)
assert(a_SQL)
assert(self)
local errCode = self.m_SqliteDB:exec(a_SQL, a_Callback, a_CallbackParam)
if (errCode ~= sqlite3.OK) then
LOGWARNING(PluginPrefix .. "Error " .. errCode .. " (" .. self.m_SqliteDB:errmsg() ..
") while processing SQL command >>" .. a_SQL .. "<<"
)
return false
end
return true
end
--- Creates the table of the specified name and columns[]
-- If the table exists, any columns missing are added existing data is kept
function cStorage:CreateTable(a_TableName, a_Columns)
assert(a_TableName)
assert(a_Columns)
assert(self)
-- Try to create the table first
local sql = "CREATE TABLE IF NOT EXISTS '" .. a_TableName .. "' ("
sql = sql .. table.concat(a_Columns, ", ")
sql = sql .. ")"
if not(self:DBExec(sql)) then
LOGWARNING(PluginPrefix .. "Cannot create DB Table " .. a_TableName)
return false
end
-- SQLite doesn't inform us if it created the table or not, so we have to continue anyway
-- Check each column whether it exists
-- Remove all the existing columns from a_Columns:
local RemoveExistingColumn = function(UserData, NumCols, Values, Names)
-- Remove the received column from a_Columns. Search for column name in the Names[] / Values[] pairs
for i = 1, NumCols do
if (Names[i] == "name") then
local ColumnName = Values[i]:lower()
-- Search the a_Columns if they have that column:
for j = 1, #a_Columns do
-- Cut away all column specifiers (after the first space), if any:
local SpaceIdx = string.find(a_Columns[j], " ")
if (SpaceIdx ~= nil) then
SpaceIdx = SpaceIdx - 1
end
local ColumnTemplate = string.lower(string.sub(a_Columns[j], 1, SpaceIdx))
-- If it is a match, remove from a_Columns:
if (ColumnTemplate == ColumnName) then
table.remove(a_Columns, j)
break -- for j
end
end -- for j - a_Columns[]
end
end -- for i - Names[] / Values[]
return 0
end
if not(self:DBExec("PRAGMA table_info(" .. a_TableName .. ")", RemoveExistingColumn)) then
LOGWARNING(PluginPrefix .. "Cannot query DB table structure")
return false
end
-- Create the missing columns
-- a_Columns now contains only those columns that are missing in the DB
if (#a_Columns > 0) then
LOGINFO(PluginPrefix .. "Database table \"" .. a_TableName .. "\" is missing " .. #a_Columns .. " columns, fixing now.")
for idx, ColumnName in ipairs(a_Columns) do
if not(self:DBExec("ALTER TABLE '" .. a_TableName .. "' ADD COLUMN " .. ColumnName)) then
LOGWARNING(PluginPrefix .. "Cannot add DB table \"" .. a_TableName .. "\" column \"" .. ColumnName .. "\"")
return false
end
end
LOGINFO(PluginPrefix .. "Database table \"" .. a_TableName .. "\" columns fixed.")
end
return true
end
--- Adds a new spawner info into the DB.
-- Returns the ID of the new spawner info, or -1 on failure
function cStorage:AddSpawnerInfo(a_SpawnerInfo)
assert(a_SpawnerInfo)
assert(self)
-- Store the spawner info in the DB
local id = -1
local function RememberID(UserData, NumCols, Values, Names)
for i = 1, NumCols do
if (Names[i] == "ID") then
id = Values[i]
end
end
return 0
end
local lcWorldName = string.lower(a_SpawnerInfo.m_WorldName)
local lcSpawnerName = string.lower(a_SpawnerInfo.m_Name)
local enabled = 0
if a_SpawnerInfo.m_IsEnabled then
enabled = 1
end
local sql = "INSERT INTO Spawners (ID, Name, WorldName, PosX, PosY, PosZ, Radius, Interval, IsEnabled) VALUES (NULL, "
local values = string.format("%q, %q, %d, %d, %d, %d, %d, %d", lcSpawnerName, lcWorldName, a_SpawnerInfo.m_PosX, a_SpawnerInfo.m_PosY, a_SpawnerInfo.m_PosZ, a_SpawnerInfo.m_Radius, a_SpawnerInfo.m_Interval, enabled)
sql = sql .. values .. "); SELECT last_insert_rowid() AS ID"
if not(self:DBExec(sql, RememberID)) then
LOGWARNING(PluginPrefix .. "SQL Error while inserting new spawner info")
return -1 -- Indicate an error
end
if (id == -1) then
LOGWARNING(PluginPrefix .. "SQL Error while retrieving INSERTION ID")
return -1 -- Indicate an error
end
return id
end
-- Updates the column to new value
function cStorage:UpdateColumn(a_ID, a_ColumnName, a_NewValue)
assert(a_ID)
assert(a_ColumnName)
assert(a_NewValue ~= nil)
assert(self)
local value
if type(a_NewValue) == "boolean" then
if a_NewValue then
value = 1
else
value = 0
end
elseif type(a_NewValue) == "string" then
value = string.format("%q", a_NewValue)
elseif type(a_NewValue) == "number" then
value = a_NewValue
else
assert("Type not handled: " .. type(a_NewValue))
end
local sql = "UPDATE Spawners SET " .. a_ColumnName .. " = " .. value .. " WHERE ID = " .. a_ID
if not(self:DBExec(sql)) then
LOGWARNING(string.format("%q SQL error while updating %q to %q!", PluginPrefix, a_ColumnName, tostring(value)))
return false
end
return true
end
function cStorage:DelSpawnerInfo(a_ID)
assert(a_ID)
assert(self)
local sql = "DELETE FROM Spawners WHERE ID = " .. a_ID
if not(self:DBExec(sql)) then
LOGWARNING(string.format("%q SQL error while deleting spawner info %d!", PluginPrefix, a_ID))
return false
end
return true
end
function cStorage:GetSpawnerInfo(a_Name)
assert(a_Name)
assert(self)
local spawnerInfo
function GetValues(UserData, NumValues, Values, Names)
if NumValues ~= 9 then
return 0
end
spawnerInfo = cSpawnerInfo:new(
Values[2], -- Name
Values[3], -- WorldName
tonumber(Values[4]), -- PosX
tonumber(Values[5]), -- PosY
tonumber(Values[6]), -- PosZ
tonumber(Values[7]), -- Radius
tonumber(Values[8])) -- Interval
local enabled = tonumber(Values[9]) -- IsEnabled
if enabled == 1 then
spawnerInfo.m_IsEnabled = true
spawnerInfo:Runit()
else
spawnerInfo.m_IsEnabled = false
end
spawnerInfo.m_ID = tonumber(Values[1])
return 0
end
local sql = string.format("SELECT * FROM Spawners WHERE Name = %q", a_Name)
if not(self:DBExec(sql, GetValues)) then
LOGWARNING(PluginPrefix .. "SQL Error while getting spawner info")
return -1 -- Indicate an error
end
return spawnerInfo
end
function cStorage:GetListOfAllSpawnerNames()
assert(self)
local tbNamesEnabled = {}
local empty = true
function GetName(UserData, NumValues, Values, Names)
if NumValues ~= 2 then
return 0
end
empty = false
local enabled = false
if tonumber(Values[2]) == 1 then -- IsEnabled
enabled = true
end
tbNamesEnabled[Values[1]] = enabled
return 0
end
local sql = "SELECT Name, IsEnabled FROM Spawners"
if not(self:DBExec(sql, GetName)) then
LOGWARNING(PluginPrefix .. "SQL Error while getting list of spawner names.")
return -1
end
if empty then
return nil
end
return tbNamesEnabled
end