Lua runner with rich builtin stdlib
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
a/lua/stdlib/sqlite.lua

40 lines
1.3 KiB

-- Lua-side of the sqlite module.
-- The connection methods execute/query/queryOne/lastInsertRowid/changes/close
-- are provided by Rust. This file adds the transaction helpers on top of the
-- same connection so users can write con:transaction(fn), con:begin(), etc.
--
-- Rust passes in `Connection`: the shared method table that every connection's
-- metatable indexes into (mlua hides the metatable itself from Lua).
return function(Connection)
--- Begin a transaction.
function Connection:begin()
self:execute("BEGIN")
end
--- Commit the current transaction.
function Connection:commit()
self:execute("COMMIT")
end
--- Roll back the current transaction.
function Connection:rollback()
self:execute("ROLLBACK")
end
--- Run `fn` inside a transaction: commit on success, roll back and re-raise
--- on error. The original error is re-raised unchanged (level 0).
function Connection:transaction(fn)
if type(fn) ~= "function" then
error("sqlite: transaction requires a function argument (got " .. type(fn) .. ")", 2)
end
self:begin()
local ok, err = pcall(fn)
if ok then
self:commit()
else
self:rollback()
error(err, 0)
end
end
end