3

I would like to change all SELECT COUNT(*) queries with SELECT COUNT(1) (for this use case).

I have the following lua script, but it does not work somehow:

function read_query( packet )
   if string.byte(packet) == proxy.COM_QUERY then
     local query = string.sub(packet, 2)
     local replacing = false
     if string.match(string.upper(query), 'COUNT(*)') then
         query = string.gsub(query,'COUNT(*)', 'COUNT(1)')
         replacing = true
     end
     if (replacing) then
         proxy.queries:append(1, string.char(proxy.COM_QUERY) .. query )
         return proxy.PROXY_SEND_QUERY
     end
   end
 end

What am I doing wrong?

Jonathan
  • 2,183
  • 4
  • 20
  • 25

1 Answers1

1

If you are searching for the string "COUNT(*)" remember the second parameter is a pattern and not a simple string, you could escape the string using

Inhibit Regular Expression magic characters ^$()%.[]*+-?)

Prefix every non-alphanumeric character (%W) with a % escape character,where %% is the % escape, and %1 is original character

function strPlainText(strText)
    return strText:gsub("(%W)","%%%1")
end

so

if string.match(string.upper(query), strPlainText('COUNT(*)')) then
Jane T
  • 2,081
  • 2
  • 17
  • 23