The following examples are part of a project I have build before.
You will have to set-up:
2 export function (one each resource)
For sending (as you have now)
And for receiving
You will have to generate an unique identifier for each query, so that each request can be wired back to where you want the data.
-- Resource A
Identifier = {}
function Identifier:newGenerator() return setmetatable( { index = 0 }, { __index = Identifier } ) end
function Identifier:generate()
local index = self.index + 1
self.index = index
return "id:" .. getTickCount() .. "|" .. index
end
-- For new ID's
local identifierGenerator = Identifier:newGenerator()
-- function ()
local id = identifierGenerator:generate()
-- end
Before you send a request you make sure that there is a destination:
-- Resource A
callbacks = {}
-- function ()
local id = identifierGenerator:generate()
-- Set-up the callback destination, anonymous func if you want to keep going where you left of.
callbacks[id] = function (...)
iprint(...)
end
exports.database_connection:db_query(id, "SELECT * FROM `players`")
-- end
Add wiring for receiving and calling back your callback function:
-- Resource A
function dbResponse (id, ...)
if not callbacks[id] then return end
callbacks[id](...)
end
Keep the wiring going in the db resource, so that the ID goes ping pong:
-- Resource B
function db_query(id, ...)
-- The source resource that made this call (important to rename the pre-defined variable, else it will be lost)
local theSourceResource = sourceResource
if (db_settings.db_connection) then
dbQuery(function(query_handle)
local result, num_affected_rows, last_insert_id = dbPoll(query_handle, 0)
call( theSourceResource, "dbResponse", id, result, num_affected_rows, last_insert_id )
end, db_settings.db_connection, ...)
end
end