代码拉取完成,页面将自动刷新
同步操作将从 稀饭放姜/iRTU 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
--- 模块功能:用来存储用户的数据
-- @module socket
-- @author openLuat
-- @license MIT
-- @copyright openLuat
-- @release 2020.03.17
require "utils"
module(..., package.seeall)
local db = {}
db.__index = db
function new(path)
if path == nil then
log.error("db.new:", "Empty path!")
return nil
end
local back = "/" .. path:match("([^/]+)$") .. ".bak"
-- 烧录时的只读文件新建文件名
local o = {path = io.exists(back) and back or path}
if io.exists(o.path) then
local res, val = pcall(dofile, o.path)
if res then
o.sheet = type(val) == "table" and val or json.decode(val)
else
log.error("db.new:", "Irregular data format!")
return nil
end
else
o.sheet = {}
end
return setmetatable(o, db)
end
--- 查询所选key的值
-- @param key: 要查询的键
-- @param[opt=nil]...: 可选可变参数,要查询的其他key
-- @return value: 查询键对应的值
-- @return ... : 其它键对应值
-- @usage db:select("msg")
-- @usage db:select("msg","vbat")
function db:select(key, ...)
local o = {self.sheet[key]}
for _, k in ipairs(arg) do
table.insert(o, self.sheet[k])
end
return unpack(o)
end
--- 持久化用户表到文件
-- @return nil
-- @usage db:serialize()
function db:serialize()
local file = io.open(self.path, "w+b")
if not file then
self.path = "/" .. self.path:match("([^/]+)$") .. ".bak"
file = io.open(self.path, "w+b")
end
local res = file:write("return ")
-- 下面这段代码规避4G的bug
if not res then
self.path = "/" .. self.path:match("([^/]+)$") .. ".bak"
file = io.open(self.path, "w+b")
file:write("return ")
end
io.serialize(file, self.sheet)
file:close()
end
--- 新增键值对
-- @param key: 新增的键
-- @param val: 新增的键值
-- @boolean[opt=nil]re,如果键值存在是否覆盖,true覆盖
-- @return nil
-- @usage db:insert("vbatt",4.39)
-- @usage db:insert("msg",{4.39,"a","b"})
function db:insert(key, val, re)
if re == true or not self.sheet[key] == nil then
return self:update(key, val, true)
end
end
--- 更新键值对
-- @param key: 要更新的键
-- @param val: 要更新的值
-- @boolean[opt=nil] add: 键不存在时是否新增,true为新增
-- @return nil
-- @usage db:update("msg",{1,2})
function db:update(key, val, add)
if type(val) ~= "table" and self.sheet[key] == val then return end
if add or self.sheet[key] ~= nil then
self.sheet[key] = val
self:serialize()
end
end
--- 删除键值对
-- @param key: 要删除的键值对
-- @param[opt=nil]...: 要删除的其它键值对
-- @return nil
-- @usage db:delete("a")
-- @usage db:delete("a",1)
function db:delete(key, ...)
for _, k in ipairs({key, ...}) do
if type(k) == "number" then
table.remove(self.sheet, k)
else
self.sheet[k] = nil
end
end
self:serialize()
end
--- 导入数据表
-- @param sheet: 支持json和table两种格式导入
-- @return table or json string
-- @usage local t = getSheet()
function db:import(sheet)
if type(sheet) == "string" then
self.sheet = json.decode(sheet)
elseif type(sheet) == "table" then
self.sheet = sheet
else
log.info("db:import error!", "sheet type is error!")
return
end
self:serialize()
end
--- 导出数据表
-- @param dbtype: 导出数据类型可选 "string" or "table"
-- @return table or json string
-- @usage local t = getSheet()
function db:export(dbtype)
if dbtype == "string" then
return json.encode(self.sheet)
end
return self.sheet
end
--- 删除数据库文件
-- @param o: 要删除的数据库对象
-- @param[opt=nil]...: 要删除的其他数据库对象
-- @return nil
function remove(o, ...)
for _, k in ipairs({o, ...}) do
os.remove(k.path)
k.path, k.sheet, k.type = nil, nil, nil
end
end
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。