refactor(lib): MySQL.Sync and MySQL.Async shared __index method

Apply the same metatable and create both cb and promise variants when one is called.
Improve assertion error messages.
This commit is contained in:
Linden committed 2021-12-24 17:35:27 +11:00
1 parent a047e21f51
commit 03b94c749f
1 file changed
+13 -17
+13 -17
View File
@@ -10,6 +10,7 @@ local MySQL = {}
MySQL.Async = {} MySQL.Async = {}
MySQL.Sync = {} MySQL.Sync = {}
local Store = {} local Store = {}
local type = type
function MySQL.ready(cb) function MySQL.ready(cb)
CreateThread(function() CreateThread(function()
@@ -37,16 +38,16 @@ end
local function safeArgs(query, parameters, cb, transaction) local function safeArgs(query, parameters, cb, transaction)
if type(query) == 'number' then query = Store[query] end if type(query) == 'number' then query = Store[query] end
if transaction then if transaction then
assert(type(query) == 'table', ('A table was expected for the transaction, but instead received %s'):format(query)) assert(type(query) == 'table', ('Transaction query expects a table, received %s'):format(query))
else else
assert(type(query) == 'string', ('A string was expected for the query, but instead received %s'):format(query)) assert(type(query) == 'string', ('Query expects a string, received %s'):format(query))
end end
if cb then if cb then
assert(type(cb) == 'function', ('A callback function was expected, but instead received %s'):format(cb)) assert(type(cb) == 'function', ('Callback expects a function, received %s'):format(cb))
end end
local type = parameters and type(parameters) local type = parameters and type(parameters)
if type and type ~= 'table' and type ~= 'function' then if type and type ~= 'table' and type ~= 'function' then
assert(nil, ('A %s was expected, but instead received %s'):format(cb and 'table' or 'function', parameters)) assert(nil, ('Parameters expected table or function, received %s'):format(parameters))
end end
return query, parameters, cb return query, parameters, cb
end end
@@ -82,7 +83,7 @@ setmetatable(MySQL, {
}) })
return self[method] return self[method]
else else
error(('^1oxmysql is currently %s - unable to trigger exports.oxmysql:%s^0'):format(state, method), 0) error(('^1oxmysql resource state is %s - unable to trigger exports.oxmysql:%s^0'):format(state, method), 0)
end end
end end
}) })
@@ -97,23 +98,19 @@ local alias = {
prepare = 'prepare' prepare = 'prepare'
} }
setmetatable(MySQL.Async, { local mt = {
__index = function(self, key) __index = function(self, key)
if alias[key] then if alias[key] then
self[key] = MySQL[alias[key]] MySQL.Async[key] = MySQL[alias[key]]
MySQL.Sync[key] = MySQL[alias[key]].await
alias[key] = nil
return self[key] return self[key]
end end
end end
}) }
setmetatable(MySQL.Sync, { setmetatable(MySQL.Async, mt)
__index = function(self, key) setmetatable(MySQL.Sync, mt)
if alias[key] then
self[key] = MySQL[alias[key]].await
return self[key]
end
end
})
--[[ --[[
exports.oxmysql:query (previously exports.oxmysql:execute) exports.oxmysql:query (previously exports.oxmysql:execute)
@@ -151,5 +148,4 @@ MySQL.Async.prepare = MySQL.prepare
MySQL.Sync.prepare = MySQL.prepare.await MySQL.Sync.prepare = MySQL.prepare.await
--]] --]]
_ENV.MySQL = MySQL _ENV.MySQL = MySQL