Improve your contact center performance. See how you can make a difference.
Watch Now
Engage and build your ICT audience with CIOL online advertising.
Know more
Scripting for query modification MySQL Proxy uses Lua scripts to intercept the communication between clients and the server. Many a times it happens that a user misspells a keyword and because of that the system throws an error. So, the user has to rewrite the whole query again. A proxy server can be passed with a Lua script that looks for such commonly misspelt words and corrects them. It then directs the query to the database server. The user would not even be aware of the typo error in his query and will still get the desired result. The following Lua script when passed to the proxy server will catch the client's queries for common typos and replace them with correct ones. For instance, keywords like 'CREATE' being typed as 'CRAETE' or 'SELECT' being typed as 'SLECT'. The following Lua script checks for such typing errors in the queries. Open a text editor and save the script as 'demoScript.lua'.
function read_query( packet ) if string.byte(packet) == proxy.COM_QUERY then local query = string.sub(packet, 2) print ("Query Received " .. query) local replacing = false -- matches "CRAETE" as first word of the query if string.match(string.upper(query), '^%s*CRAETE') then query = string.gsub(query,'^%s*%w+', 'CREATE') replacing = true -- matches "SLECT" as first word of the query elseif string.match(string.upper(query), '^%s*SLECT') then query = string.gsub(query,'^%s*%w+', 'SELECT') replacing = true end if (replacing) then print("Error Replaced with " .. query ) proxy.queries:append(1, string.char(proxy.COM_QUERY) .. query ) return proxy.PROXY_SEND_QUERY end end end
Now start the proxy server and also pass the demoScript along with it as follows:
Mysql-proxy –proxy-lua-script=demoScript.lua
From a separate MySQL client console window, connect to the MySQL Proxy and then create a table with a wrongly spelt word. For instance, 'CREATE' as 'CRAETE.' The code would be as follows:
#mysql –u USERNAME –p PASSWORD –h 127.0.0.1 –P 4040 MySQL> CRAETE table demo (uid int);
Even when we pass an error in the query, the query is caught for errors at the Proxy and after corrections to the query it's directed to the main MySQL server.
In conclusion Thus, by use of such scripts we can create filters or auto-correction for typed errors so that the user gets his query answered even if he had passed an erroneous query.
This was just a simple demo on how queries get modified; the possible uses of the MySQL Proxy are many more, including load balancing and query injections. More can be learnt about them from the MySQL Proxy documentation online.
<< PREVIOUS