-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.lua
50 lines (46 loc) · 1.12 KB
/
db.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
local cqueues = require("cqueues")
local lsqlite3 = require("lsqlite3")
local config = require("config")
local handle_i = {}
local handle_m = { __index = handle_i }
function handle_i:exec(log, sql, ...)
local stmt = self.db_:prepare(sql)
if not stmt then
error(self.db_:errmsg(), 2)
end
if stmt:bind_values(...) ~= lsqlite3.OK then
error(self.db_:errmsg(), 2)
end
local result = {}
local names = stmt:get_names()
while true do
local res = stmt:step()
if res == lsqlite3.BUSY then
log("db is busy, retrying in $ seconds", config.db.busy_delay)
cqueues.sleep(config.db.busy_delay)
elseif res == lsqlite3.DONE then
break
elseif res == lsqlite3.ROW then
local row = {}
local values = stmt:get_values()
for i = 1, #names do
row[names[i]] = values[i]
end
table.insert(result, row)
elseif res == lsqlite3.CONSTRAINT then
log("constraint violation, aborting")
return nil, "constraint"
else
error(self.db_:errmsg(), 2)
end
end
return result
end
local function handle(path)
return setmetatable({
db_ = assert(lsqlite3.open(path)),
}, handle_m)
end
return {
handle = handle,
}