add database crud operation function to plugin system

This commit is contained in:
zhongqin
2026-05-19 02:20:48 +08:00
parent 493be8f7de
commit 15c6094de7
22 changed files with 2201 additions and 1002 deletions
+64
View File
@@ -17,6 +17,70 @@ export function dbExec(sql, params) {
const result = RaisFastHost.dbExecute(sql, JSON.stringify(params ?? []));
return JSON.parse(result);
}
export function dbInsert(table, data, options) {
const dataStr = typeof data === "string" ? data : JSON.stringify(data);
const optStr = options == null ? "{}" : (typeof options === "string" ? options : JSON.stringify(options));
const result = JSON.parse(RaisFastHost.dbInsert(table, dataStr, optStr));
if (result.error) throw new Error(result.error);
return result;
}
export function dbFetchOne(table, where, options) {
const whereStr = where == null ? "{}" : (typeof where === "string" ? where : JSON.stringify(where));
const optStr = options == null ? "{}" : (typeof options === "string" ? options : JSON.stringify(options));
const result = JSON.parse(RaisFastHost.dbFetchOne(table, whereStr, optStr));
if (result.error) throw new Error(result.error);
return result.data;
}
export function dbFetchAll(table, where, options) {
const whereStr = where == null ? "{}" : (typeof where === "string" ? where : JSON.stringify(where));
const optStr = options == null ? "{}" : (typeof options === "string" ? options : JSON.stringify(options));
const result = JSON.parse(RaisFastHost.dbFetchAll(table, whereStr, optStr));
if (result.error) throw new Error(result.error);
return result;
}
export function dbUpdate(table, data, where, options) {
const dataStr = typeof data === "string" ? data : JSON.stringify(data);
const whereStr = where == null ? "{}" : (typeof where === "string" ? where : JSON.stringify(where));
const optStr = options == null ? "{}" : (typeof options === "string" ? options : JSON.stringify(options));
const result = JSON.parse(RaisFastHost.dbUpdate(table, dataStr, whereStr, optStr));
if (result.error) throw new Error(result.error);
return result;
}
export function dbDelete(table, where, options) {
const whereStr = where == null ? "{}" : (typeof where === "string" ? where : JSON.stringify(where));
const optStr = options == null ? "{}" : (typeof options === "string" ? options : JSON.stringify(options));
const result = JSON.parse(RaisFastHost.dbDelete(table, whereStr, optStr));
if (result.error) throw new Error(result.error);
return result;
}
export function dbCount(table, where, options) {
const whereStr = where == null ? "{}" : (typeof where === "string" ? where : JSON.stringify(where));
const optStr = options == null ? "{}" : (typeof options === "string" ? options : JSON.stringify(options));
const result = JSON.parse(RaisFastHost.dbCount(table, whereStr, optStr));
if (result.error) throw new Error(result.error);
return result.count;
}
export function dbIncrement(table, columns, where, options) {
const colStr = typeof columns === "string" ? columns : JSON.stringify(columns);
const whereStr = where == null ? "{}" : (typeof where === "string" ? where : JSON.stringify(where));
const optStr = options == null ? "{}" : (typeof options === "string" ? options : JSON.stringify(options));
const result = JSON.parse(RaisFastHost.dbIncrement(table, colStr, whereStr, optStr));
if (result.error) throw new Error(result.error);
return result;
}
export function dbSum(table, column, where, options) {
const whereStr = where == null ? "{}" : (typeof where === "string" ? where : JSON.stringify(where));
const optStr = options == null ? "{}" : (typeof options === "string" ? options : JSON.stringify(options));
const result = JSON.parse(RaisFastHost.dbSum(table, column, whereStr, optStr));
if (result.error) throw new Error(result.error);
return result.sum;
}
export function dbGroupBy(table, options) {
const optStr = options == null ? "{}" : (typeof options === "string" ? options : JSON.stringify(options));
const result = JSON.parse(RaisFastHost.dbGroupBy(table, optStr));
if (result.error) throw new Error(result.error);
return result;
}
export function dbBegin() {
const result = JSON.parse(RaisFastHost.dbBegin());
if (!result.ok)
+73
View File
@@ -19,6 +19,79 @@ function M.dbExec(sql, params)
return RaisFastHost.jsonDecode(result)
end
function M.dbInsert(table, data, options)
local dataStr = type(data) == "string" and data or RaisFastHost.jsonEncode(data)
local optStr = (options == nil) and "{}" or (type(options) == "string" and options or RaisFastHost.jsonEncode(options))
local result = RaisFastHost.jsonDecode(RaisFastHost.dbInsert(table, dataStr, optStr))
if result.error then error(result.error) end
return result
end
function M.dbFetchOne(table, where, options)
local whereStr = (where == nil) and "{}" or (type(where) == "string" and where or RaisFastHost.jsonEncode(where))
local optStr = (options == nil) and "{}" or (type(options) == "string" and options or RaisFastHost.jsonEncode(options))
local result = RaisFastHost.jsonDecode(RaisFastHost.dbFetchOne(table, whereStr, optStr))
if result.error then error(result.error) end
return result.data
end
function M.dbFetchAll(table, where, options)
local whereStr = (where == nil) and "{}" or (type(where) == "string" and where or RaisFastHost.jsonEncode(where))
local optStr = (options == nil) and "{}" or (type(options) == "string" and options or RaisFastHost.jsonEncode(options))
local result = RaisFastHost.jsonDecode(RaisFastHost.dbFetchAll(table, whereStr, optStr))
if result.error then error(result.error) end
return result
end
function M.dbUpdate(table, data, where, options)
local dataStr = type(data) == "string" and data or RaisFastHost.jsonEncode(data)
local whereStr = (where == nil) and "{}" or (type(where) == "string" and where or RaisFastHost.jsonEncode(where))
local optStr = (options == nil) and "{}" or (type(options) == "string" and options or RaisFastHost.jsonEncode(options))
local result = RaisFastHost.jsonDecode(RaisFastHost.dbUpdate(table, dataStr, whereStr, optStr))
if result.error then error(result.error) end
return result
end
function M.dbDelete(table, where, options)
local whereStr = (where == nil) and "{}" or (type(where) == "string" and where or RaisFastHost.jsonEncode(where))
local optStr = (options == nil) and "{}" or (type(options) == "string" and options or RaisFastHost.jsonEncode(options))
local result = RaisFastHost.jsonDecode(RaisFastHost.dbDelete(table, whereStr, optStr))
if result.error then error(result.error) end
return result
end
function M.dbCount(table, where, options)
local whereStr = (where == nil) and "{}" or (type(where) == "string" and where or RaisFastHost.jsonEncode(where))
local optStr = (options == nil) and "{}" or (type(options) == "string" and options or RaisFastHost.jsonEncode(options))
local result = RaisFastHost.jsonDecode(RaisFastHost.dbCount(table, whereStr, optStr))
if result.error then error(result.error) end
return result.count
end
function M.dbIncrement(table, columns, where, options)
local colStr = type(columns) == "string" and columns or RaisFastHost.jsonEncode(columns)
local whereStr = (where == nil) and "{}" or (type(where) == "string" and where or RaisFastHost.jsonEncode(where))
local optStr = (options == nil) and "{}" or (type(options) == "string" and options or RaisFastHost.jsonEncode(options))
local result = RaisFastHost.jsonDecode(RaisFastHost.dbIncrement(table, colStr, whereStr, optStr))
if result.error then error(result.error) end
return result
end
function M.dbSum(table, column, where, options)
local whereStr = (where == nil) and "{}" or (type(where) == "string" and where or RaisFastHost.jsonEncode(where))
local optStr = (options == nil) and "{}" or (type(options) == "string" and options or RaisFastHost.jsonEncode(options))
local result = RaisFastHost.jsonDecode(RaisFastHost.dbSum(table, column, whereStr, optStr))
if result.error then error(result.error) end
return result.sum
end
function M.dbGroupBy(table, options)
local optStr = (options == nil) and "{}" or (type(options) == "string" and options or RaisFastHost.jsonEncode(options))
local result = RaisFastHost.jsonDecode(RaisFastHost.dbGroupBy(table, optStr))
if result.error then error(result.error) end
return result
end
function M.dbBegin()
local result = RaisFastHost.jsonDecode(RaisFastHost.dbBegin())
if not result.ok then error("dbBegin failed") end