mirror of
https://github.com/RaisFast/raisfast.git
synced 2026-09-23 16:02:25 +00:00
add database crud operation function to plugin system
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user