From b6f850dbd635219eb27cd59f5be407bbd4903653 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sat, 19 Mar 2022 16:51:09 +1100 Subject: [PATCH] Revert "refactor(lib/MySQL): use coroutine instead of promise" This reverts commit 7582973e4e5deaaadc06490e6a6c2682efefe312. Calling coroutine.yield causes issues with the Lua scrt's scheduling. Notably, calling Citizen.Wait will result in the thread never being resumed. --- lib/MySQL.lua | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/MySQL.lua b/lib/MySQL.lua index 86c594c..6b578e5 100644 --- a/lib/MySQL.lua +++ b/lib/MySQL.lua @@ -1,3 +1,11 @@ +-- 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) @@ -49,15 +57,17 @@ 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 co = coroutine.running() + local p = promise.new() fn(nil, query, parameters, function(result) - return coroutine.resume(co, result) + p:resolve(result) end, GetCurrentResourceName) - return coroutine.yield() + return Await(p) end setmetatable(MySQL, { @@ -112,3 +122,38 @@ 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 +--]]