Etant assez jeune mysql-proxy pose quelque petits problèmes dans une architecture RW/SPLITTING tel que :
- SET NAMES utf8
- CREATE TEMPORARY TABLE …
- SET @a = 1; SELECT @a;
- Stored Procedures
- SHOW SESSION STATUS
Pour contrer le problème du jeton CREATE TEMPORARY TABLE, qui provoque certaines erreur, l’astuce consistera à parser les commandes Temporary en les interdisant dans notre script. Maintenant voyons comment intégrer notre code dans le fichier “rw-splitting.lua”.
Définissons dans un premier temps ce que nous voulons bloquer :
function make_regexp_from_command(cmd)
local regexp= ‘^%s*’;
for ch in cmd:gmatch(‘(.)’) do
regexp = regexp .. ‘[‘ .. ch:upper() .. ch:lower() .. ‘]’
end
return regexp
end
local CREATE_REGEXP = make_regexp_from_command(‘create’)
queries_to_filter = {
{
prefix = CREATE_REGEXP,
keywords = { ‘CREATE’,’TEMPORARY’,’TABLE’} ,
}
}
function error_result (msg)
proxy.response = {
type = proxy.MYSQLD_PACKET_ERR,
errmsg = msg,
errcode = 7777,
sqlstate = ‘X7777’,
}
return proxy.PROXY_SEND_RESULT
end
–config block
Puis dans la fonction ” function read_query( packet )” après le ” if is_debug then end ” ajouter ceci :
–config block
local query = packet:sub(2)
for i,query_to_filter in pairs(queries_to_filter)
do
if query:match(query_to_filter.prefix) then
local full_tokens = tokenizer.tokenize(query)
local tokens = tokenizer.bare_tokens(full_tokens, true)
local found = 0
local requested = #query_to_filter.keywords
for j,keyword in pairs(query_to_filter.keywords) do
for k, token in pairs(tokens) do
if token:upper() == keyword then
found = found + 1
break
end
end
end
if found == requested then — to be filtered off
return error_result(‘command <‘ ..
table.concat(query_to_filter.keywords,’ ‘) ..
‘> is not allowed’ )
end
end
end
–config block
Et voila maintenant quand vous exécuterez vos requêtes un message sera afficher interdisant les requêtes CREATE TEMPORARY TABLE.