refactor(lib/lua): safeargs type checks and cb reassignment

This commit is contained in:
Linden committed 2022-07-12 13:33:53 +10:00
1 parent 3e1426a859
commit 1b8bf6a081
1 file changed
+19 -56
+19 -56
View File
@@ -1,11 +1,3 @@
-- lib/MySQL.lua provides complete compatibility for resources designed for mysql-async
-- As of v2.0.0 this is the preferred method of interacting with oxmysql
-- * You can use mysql-async syntax or oxmysql syntax (refer to issue #77 or line 118)
-- * Using this lib provides minor improvements to performance and helps debug poor queries
-- * If using mysql-async syntax, a resource is not explicity bound to using oxmysql
-- todo: new annotations; need to see if I can get it working with metatables, otherwise it'll need stubs
local Store = {}
local function addStore(query, cb)
@@ -32,28 +24,35 @@ local MySQL = {
local type = type
local function safeArgs(query, parameters, cb, transaction)
if type(query) == 'number' then query = Store[query] end
local queryType = type(query)
if transaction then
if type(query) ~= 'table' then
if queryType == 'number' then
query = Store[query]
elseif transaction then
if queryType ~= 'table' then
error(("First argument expected table, received '%s'"):format(query))
end
else
if type(query) ~= 'string' then
error(("First argument expected string, received '%s'"):format(query))
end
elseif queryType ~= 'string' then
error(("First argument expected string, received '%s'"):format(query))
end
if parameters then
local type = type(parameters)
if type ~= 'table' and type ~= 'function' then
local paramType = type(parameters)
if paramType ~= 'table' and paramType ~= 'function' then
error(("Second argument expected table or function, received '%s'"):format(parameters))
end
if paramType == 'function' or parameters.__cfx_functionReference then
cb = parameters
parameters = nil
end
end
if cb then
local type = type(cb)
if type ~= 'function' and (type == 'table' and not cb.__cfx_functionReference) then
if cb and parameters then
local cbType = type(cb)
if cbType ~= 'function' and (cbType == 'table' and not cb.__cfx_functionReference) then
error(("Third argument expected function, received '%s'"):format(cb))
end
end
@@ -130,39 +129,3 @@ setmetatable(MySQL.Async, alias_mt)
setmetatable(MySQL.Sync, alias_mt)
_ENV.MySQL = MySQL
--[[
exports.oxmysql:query (previously exports.oxmysql:execute)
MySQL.Async.fetchAll = MySQL.query
MySQL.Sync.fetchAll = MySQL.query.await
exports.oxmysql:scalar
MySQL.Async.fetchScalar = MySQL.scalar
MySQL.Sync.fetchScalar = MySQL.scalar.await
exports.oxmysql:single
MySQL.Async.fetchSingle = MySQL.single
MySQL.Sync.fetchSingle = MySQL.single.await
exports.oxmysql:insert
MySQL.Async.insert = MySQL.insert
MySQL.Sync.insert = MySQL.insert.await
exports.oxmysql:update
MySQL.Async.execute = MySQL.update
MySQL.Sync.execute = MySQL.update.await
exports.oxmysql:transaction
MySQL.Async.transaction = MySQL.transaction
MySQL.Sync.transaction = MySQL.transaction.await
exports.oxmysql:prepare
MySQL.Async.prepare = MySQL.prepare
MySQL.Sync.prepare = MySQL.prepare.await
--]]