local MySQL = {}
MySQL.Async = {}
MySQL.Sync = {}
setmetatable(MySQL, {
__index = function(self, method)
self[method] = setmetatable({}, {
__call = oxmysql[method],
__index = function(_, await)
assert(await == 'await', ('unable to index MySQL.%s.%s, expected .await'):format(method, await))
self[method].await = function(query, parameters)
return Await(oxmysql[method], safeArgs(query, parameters))
end
return self[method].await
end
})
return self[method]
end
})
MySQL.Async.fetchScalar = MySQL.scalar
MySQL.Sync.fetchScalar = MySQL.scalar.await
CreateThread(function()
local r = MySQL.scalar.await('SELECT identifier from users WHERE lastname = ?', {'Linden'})
print('MySQL.scalar.await', r)
MySQL.scalar('SELECT identifier from users WHERE lastname = ?', {'Linden'}, function(r)
print('MySQL.scalar', r)
end)
Wait(0)
print('\n\n')
local r = MySQL.Sync.fetchScalar('SELECT identifier from users WHERE lastname = ?', {'Linden'})
print('MySQL.Sync.fetchScalar', r)
MySQL.Async.fetchScalar('SELECT identifier from users WHERE lastname = ?', {'Linden'}, function(r)
print('MySQL.Async.fetchScalar', r)
end)
end)
exports.oxmysql:fetch("SELECT column FROM table") -- I don't care about the result
local result = Citizen.Await(exports.oxmysql:fetch("SELECT column FROM table")) -- I need the result
But I have no idea how much a promise (not awaited) cost.
Ok so it is not that much, but it is still a win.
Thank you for all the detailed explanations !
This syntax is good !
If it happens to have weirdness, another good syntax could be :
Iād like to be able to use execute for prepare but thatās just another way to confuse the fuck out of people with the mysql-async and ghmatti divide (especially when MySQL.Async.execute exists).
I end up killing execute entirely and opt for query as the general export. Iāll still have _async variants for returning promises too.
Itās in lib/MySQL for compatibility, but thereās not much benefit if any to actually implementing it into the resource considering itās just an array of strings to reference.
Like I said, store exists in lib/MySQL for compatibility. server_script '@oxmysql/lib/MySQL.lua' is the preferred usage as Iāve posted several times here and on github, you get mysql-async syntax.
got a question about the removal of exports.oxmysql:fetch and exports.myoxmysql:fetchSync in your document you dont say what they change to. I have 21 files that use one or the other. should they all change to MySQL.query for fetch and MySQL.query.await for fetchSync?
fetch/fetchSync were deprecated way back in 1.3, and synonymous with execute.
Since weāre pushing for the mysql-async backwards-compatible API then youād be using MySQL.query (and yes, await for Sync) or MySQL.Async.fetchAll.
Weāre using node-mysql2 to create the connection, then handling the the arguments, typecasting, and API to interact with it; you donāt need to set it up in your own resources (and it would be a bit weird to build it into all of your resources when a single one is enough).