简体   繁体   中英

mysql-proxy result field manipulation

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.

Ok. Solved.

  • Data has to be inserted in a table
  • PROXY_SEND_RESULT requires proxy.response.type to be set.

This is the correct module:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM