mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-03 22:20:56 +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)
|
local function await(fn, query, parameters)
|
||||||
assert(type(query) == 'string', 'The SQL Query must be a string')
|
local p = promise.new()
|
||||||
local store = #Store+1
|
fn(nil, query, parameters, function(result, error)
|
||||||
Store[store] = query
|
if error then
|
||||||
if cb then cb(store) else return store end
|
return p:reject(error)
|
||||||
|
end
|
||||||
|
|
||||||
|
p:resolve(result)
|
||||||
|
end, GetCurrentResourceName, true)
|
||||||
|
return Await(p)
|
||||||
end
|
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 type = type
|
||||||
|
local queryStore = {}
|
||||||
|
|
||||||
local function safeArgs(query, parameters, cb, transaction)
|
local function safeArgs(query, parameters, cb, transaction)
|
||||||
local queryType = type(query)
|
local queryType = type(query)
|
||||||
|
|
||||||
if queryType == 'number' then
|
if queryType == 'number' then
|
||||||
query = Store[query]
|
query = queryStore[query]
|
||||||
elseif transaction then
|
elseif transaction then
|
||||||
if queryType ~= 'table' then
|
if queryType ~= 'table' then
|
||||||
error(("First argument expected table, received '%s'"):format(query))
|
error(("First argument expected table, received '%s'"):format(query))
|
||||||
@@ -60,49 +55,36 @@ local function safeArgs(query, parameters, cb, transaction)
|
|||||||
return query, parameters, cb
|
return query, parameters, cb
|
||||||
end
|
end
|
||||||
|
|
||||||
local promise = promise
|
|
||||||
local oxmysql = exports.oxmysql
|
local oxmysql = exports.oxmysql
|
||||||
local Await = Citizen.Await
|
|
||||||
local GetCurrentResourceName = GetCurrentResourceName()
|
|
||||||
|
|
||||||
local function await(fn, query, parameters)
|
local mysql_method_mt = {
|
||||||
local p = promise.new()
|
__call = function(self, query, parameters, cb)
|
||||||
fn(nil, query, parameters, function(result, error)
|
query, parameters, cb = safeArgs(query, parameters, cb, self.method == 'transaction')
|
||||||
if error then
|
return oxmysql[self.method](nil, query, parameters, cb, GetCurrentResourceName, false)
|
||||||
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
|
|
||||||
end
|
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 = {
|
local alias = {
|
||||||
fetchAll = 'query',
|
fetchAll = 'query',
|
||||||
@@ -117,8 +99,9 @@ local alias = {
|
|||||||
local alias_mt = {
|
local alias_mt = {
|
||||||
__index = function(self, key)
|
__index = function(self, key)
|
||||||
if alias[key] then
|
if alias[key] then
|
||||||
MySQL.Async[key] = MySQL[alias[key]]
|
local method = MySQL[alias[key]]
|
||||||
MySQL.Sync[key] = MySQL[alias[key]].await
|
MySQL.Async[key] = method
|
||||||
|
MySQL.Sync[key] = method.await
|
||||||
alias[key] = nil
|
alias[key] = nil
|
||||||
return self[key]
|
return self[key]
|
||||||
end
|
end
|
||||||
@@ -128,4 +111,16 @@ local alias_mt = {
|
|||||||
setmetatable(MySQL.Async, alias_mt)
|
setmetatable(MySQL.Async, alias_mt)
|
||||||
setmetatable(MySQL.Sync, 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
|
_ENV.MySQL = MySQL
|
||||||
Reference in new issue
Block a user