1

I have a MYSQL server and MYSQL-PROXY and I am trying to manipualte the results I send to the client as a response to a SELECT query. I have writen this code in lua:

function string.starts(String,Start)
   return string.sub(String,1,string.len(Start))==Start
end

function read_query_result(inj)
        local fn = 1
        local fields = inj.resultset.fields
        while fields[fn] do
            fn = fn + 1
        end
        fn = fn - 1
        print("FIELD NUMBER: " .. fn)
        for row in inj.resultset.rows do
            print ("--------------")
            for i = 1, fn do
                if (string.starts(fields[i].name,"TEST")) then
                    row[i]="TESTED"
                end
                print ("DATA: " ..  fields[i].name .. " -> " .. row[i])
            end
        end
        return proxy.PROXY_SEND_RESULT
end

I can correctly read the field names and values. I can detect the condition where I want the result modified, but I can not get the data sent to the client.

I see two problems:

  • I am setting the value in the local row variable, but I have not found the way to set the real resultset (inj.Resultset.row[i] or something similar).
  • There is something wrong with return proxy.PROXY_SEND_RESULT, because I am seeing that whenever I comment that sentence I see the results, and If I uncomment it I get an error.

I have not found example code as a reference.

4

1 回答 1

0

好的。解决了。

  • 数据必须插入表中
  • PROXY_SEND_RESULT 需要设置 proxy.response.type。

这是正确的模块:

function read_query_result(inj)
        local fn = 1
        local fields = inj.resultset.fields
        proxy.response.resultset = {fields = {}, rows = {}}
        while fields[fn] do
            table.insert(proxy.response.resultset.fields, {type = proxy.MYSQL_TYPE_STRING, name = fields[fn].name})
            fn = fn + 1
        end
        fn = fn - 1
        for row in inj.resultset.rows do
            for i = 1, fn do
                if (string.starts(fields[i].name,"TEST")) then
                    row[i]="TESTED"
                end
            end
            table.insert(proxy.response.resultset.rows, row )
        end
        proxy.response.type = proxy.MYSQLD_PACKET_OK
        return proxy.PROXY_SEND_RESULT
end
于 2014-01-30T16:10:07.693 回答