List:Internals« Previous MessageNext Message »
From:Jan Kneschke Date:May 2 2008 4:21pm
Subject:Re: mysql proxy questions
View as plain text  
Vangelis Katsikaros wrote:
> Hello
> 
> More specificly I have difficulty in doing the following things:
> * Client sends a query and then the server returns a resultset. How can
> I store the resultset in order to send it later to the client (in the
> meantime proxy and server exchange other queries and I want to send the
> resultset of the client's query in the end of the exchage).
> There are some examples that cover some parts of what I am trying to do
> like (http://forge.mysql.com/tools/tool.php?id=129) . However I can't
> find reference for the proxy.response or other things.

As an example let me duplicate a query and UNION its result-set:

-- our storage for the union'ed resultset
res = { }

-- duplicate a SELECT query
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

---
-- join the results
function read_query_result(inj)
        -- copy the result-set from this query to the union-storage
        for row in inj.resultset.rows do
                res[#res + 1] = row
        end

        -- wait until we have received all resultsets
        if inj.id ~= 2 then
                return proxy.PROXY_IGNORE_RESULT
        end

        -- we have all result-sets, create a new one
        proxy.response = {
                type = proxy.MYSQLD_PACKET_OK,
                resultset = {
                        rows = res
                }
        }

        -- copy the fields from the last result-set
        local fields = {}
        for n = 1, #inj.resultset.fields do
                fields[#fields + 1] = {
                        type = inj.resultset.fields[n].type,
                        name = inj.resultset.fields[n].name,
                }
        end

        proxy.response.resultset.fields = fields

        -- send it
        return proxy.PROXY_SEND_RESULT
end


> * How can I understand whether the response from the server (for example
> after an INSERT) is something like "Query OK, 1 row affected" without
> warnings or errors. Is the "proxy.MYSQLD_PACKET_OK" enough or is this
> about the network level?

see examples/tutorial-packets.lua

if inj.resultset.type == proxy.MYSQLD_PACKET_OK then
  if inj.resultset.affected_rows then
     print(inj.resultset.affected_rows)
  end
end

We have 2 kinds of OK-resultsets:

* one with a fields and rows
* one with affected_rows and insert_id

> Vangelis Katsikaros

cheers,
  Jan
-- 
Jan Kneschke, Enterprise Tools, Senior Software Dev, MySQL GmbH
D-80335 München Dachauer Str.37, GF: Kaj Arnö - HRB München 162140
Thread
mysql proxy questionsVangelis Katsikaros2 May 2008
  • Re: mysql proxy questionsJan Kneschke2 May 2008
    • Re: mysql proxy questionsVangelis Katsikaros2 May 2008
      • Re: mysql proxy questionsJay Pipes2 May 2008