mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-01 14:38:50 +00:00
refactor(lib/lua): reorganise and rewrite function factory
This commit is contained in:
1 parent
e8f1abcab7
commit
2ad0539107
1 file changed
+58
-63
+58
-63
@@ -1,33 +1,28 @@
|
||||
local Store = {}
|
||||
local promise = promise
|
||||
local Await = Citizen.Await
|
||||
local GetCurrentResourceName = GetCurrentResourceName()
|
||||
local GetResourceState = GetResourceState
|
||||
|
||||
local function addStore(query, cb)
|
||||
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||
local store = #Store+1
|
||||
Store[store] = query
|
||||
if cb then cb(store) else return store end
|
||||
local function await(fn, query, parameters)
|
||||
local p = promise.new()
|
||||
fn(nil, query, parameters, function(result, error)
|
||||
if error then
|
||||
return p:reject(error)
|
||||
end
|
||||
|
||||
p:resolve(result)
|
||||
end, GetCurrentResourceName, true)
|
||||
return Await(p)
|
||||
end
|
||||
|
||||
local MySQL = {
|
||||
Sync = { store = addStore },
|
||||
Async = { store = addStore },
|
||||
|
||||
ready = function(cb)
|
||||
CreateThread(function()
|
||||
repeat
|
||||
Wait(50)
|
||||
until GetResourceState('oxmysql') == 'started'
|
||||
cb()
|
||||
end)
|
||||
end
|
||||
}
|
||||
|
||||
local type = type
|
||||
local queryStore = {}
|
||||
|
||||
local function safeArgs(query, parameters, cb, transaction)
|
||||
local queryType = type(query)
|
||||
|
||||
if queryType == 'number' then
|
||||
query = Store[query]
|
||||
query = queryStore[query]
|
||||
elseif transaction then
|
||||
if queryType ~= 'table' then
|
||||
error(("First argument expected table, received '%s'"):format(query))
|
||||
@@ -60,49 +55,36 @@ local function safeArgs(query, parameters, cb, transaction)
|
||||
return query, parameters, cb
|
||||
end
|
||||
|
||||
local promise = promise
|
||||
local oxmysql = exports.oxmysql
|
||||
local Await = Citizen.Await
|
||||
local GetCurrentResourceName = GetCurrentResourceName()
|
||||
|
||||
local function await(fn, query, parameters)
|
||||
local p = promise.new()
|
||||
fn(nil, query, parameters, function(result, error)
|
||||
if error then
|
||||
return p:reject(error)
|
||||
end
|
||||
|
||||
p:resolve(result)
|
||||
end, GetCurrentResourceName, true)
|
||||
return Await(p)
|
||||
end
|
||||
|
||||
setmetatable(MySQL, {
|
||||
__index = function(self, method)
|
||||
local state = GetResourceState('oxmysql')
|
||||
if state == 'started' or state == 'starting' then
|
||||
self[method] = setmetatable({}, {
|
||||
|
||||
__call = function(_, query, parameters, cb)
|
||||
query, parameters, cb = safeArgs(query, parameters, cb, method == 'transaction')
|
||||
return oxmysql[method](nil, query, parameters, cb, GetCurrentResourceName, false)
|
||||
end,
|
||||
|
||||
__index = function(_, index)
|
||||
assert(index == 'await', ('unable to index MySQL.%s.%s, expected .await'):format(method, index))
|
||||
self[method].await = function(query, parameters)
|
||||
return await(oxmysql[method], safeArgs(query, parameters, nil, method == 'transaction'))
|
||||
end
|
||||
return self[method].await
|
||||
end
|
||||
})
|
||||
|
||||
return self[method]
|
||||
else
|
||||
error(('^1oxmysql resource state is %s - unable to trigger exports.oxmysql:%s^0'):format(state, method), 0)
|
||||
end
|
||||
local mysql_method_mt = {
|
||||
__call = function(self, query, parameters, cb)
|
||||
query, parameters, cb = safeArgs(query, parameters, cb, self.method == 'transaction')
|
||||
return oxmysql[self.method](nil, query, parameters, cb, GetCurrentResourceName, false)
|
||||
end
|
||||
})
|
||||
}
|
||||
|
||||
local MySQL = {
|
||||
ready = function(cb)
|
||||
CreateThread(function()
|
||||
repeat
|
||||
Wait(50)
|
||||
until GetResourceState('oxmysql') == 'started'
|
||||
cb()
|
||||
end)
|
||||
end
|
||||
}
|
||||
|
||||
for _, method in pairs({
|
||||
'scalar', 'single', 'query', 'insert', 'update', 'prepare', 'transaction',
|
||||
}) do
|
||||
MySQL[method] = setmetatable({
|
||||
method = method,
|
||||
await = function(query, parameters)
|
||||
return await(oxmysql[method], safeArgs(query, parameters, nil, method == 'transaction'))
|
||||
end
|
||||
}, mysql_method_mt)
|
||||
end
|
||||
|
||||
local alias = {
|
||||
fetchAll = 'query',
|
||||
@@ -117,8 +99,9 @@ local alias = {
|
||||
local alias_mt = {
|
||||
__index = function(self, key)
|
||||
if alias[key] then
|
||||
MySQL.Async[key] = MySQL[alias[key]]
|
||||
MySQL.Sync[key] = MySQL[alias[key]].await
|
||||
local method = MySQL[alias[key]]
|
||||
MySQL.Async[key] = method
|
||||
MySQL.Sync[key] = method.await
|
||||
alias[key] = nil
|
||||
return self[key]
|
||||
end
|
||||
@@ -128,4 +111,16 @@ local alias_mt = {
|
||||
setmetatable(MySQL.Async, alias_mt)
|
||||
setmetatable(MySQL.Sync, alias_mt)
|
||||
|
||||
local function addStore(query, cb)
|
||||
assert(type(query) == 'string', 'The SQL Query must be a string')
|
||||
|
||||
local storeN = #queryStore + 1
|
||||
queryStore[storeN] = query
|
||||
|
||||
return cb and cb(storeN) or storeN
|
||||
end
|
||||
|
||||
MySQL.Sync = { store = addStore }
|
||||
MySQL.Async = { store = addStore }
|
||||
|
||||
_ENV.MySQL = MySQL
|
||||
Reference in new issue
Block a user