From time to time we get the question how to split a query into a several smaller queries and unifying the result-set before we send it back to the client.
As the client only expects to get one result-set, we have to merge the result-sets from the server into one, like this:
First we need a storage for the result-set we want to build:
res = { }
Each connection gets its own one. We declare it outside of the functions as we want to share it between the result-sets of the same connection.
As an example let me just duplicate a query and send it to the server twice:
function read_query(packet)
if packet:byte() ~= proxy.COM_QUERY then return end
local q = packet:sub(2)
res = { }
if q:sub(1, 6):upper() == "SELECT" then
proxy.queries:append(1, packet)
proxy.queries:append(2, packet)
return proxy.PROXY_SEND_QUERY
end
end …[Read more]