~jan-kneschke/mysql-proxy/packet-tracking-assertions

« back to all changes in this revision

Viewing changes to tags/mysql-proxy-0.6.1/lib/proxy/auto-config.lua

  • Committer: Kay Roepke
  • Author(s): Jan Kneschke
  • Date: 2008-01-23 22:00:28 UTC
  • Revision ID: kay@mysql.com-20080123220028-hq2xqb69apa75fnx
first round on mysql-shell based on the proxy code

this is mostly a verification if the proxy-code is flexible enough to handle 
all three scenarios of: client, server and forwarding (proxy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module("proxy.auto-config", package.seeall)
 
2
 
 
3
local tokenizer = require("proxy.tokenizer")
 
4
 
 
5
local function parse_value(fld, token)
 
6
        local t = type(fld)
 
7
 
 
8
        if t == "boolean" then
 
9
                if token.token_name == "TK_INTEGER" then
 
10
                        return (token.text ~= "0")
 
11
                else
 
12
                        print("(auto-config) expected a number, got " .. token.token_name)
 
13
                end
 
14
        elseif t == "number" then
 
15
                if token.token_name == "TK_INTEGER" then
 
16
                        return tonumber(token.text)
 
17
                else
 
18
                        print("(auto-config) expected a number, got " .. token.token_name)
 
19
                end
 
20
        else
 
21
                print("(auto-config) type: " .. t .. " isn't handled yet" )
 
22
        end
 
23
        
 
24
        return
 
25
end
 
26
 
 
27
function handle(cmd)
 
28
        -- handle script-options first
 
29
        if cmd.type ~= proxy.COM_QUERY then return nil end
 
30
        if cmd.query:sub(1, 3):upper() ~= "SET" then return nil end
 
31
        
 
32
        local tokens     = assert(tokenizer.tokenize(cmd.query))
 
33
        local norm_query = tokenizer.normalize(tokens)
 
34
 
 
35
        -- looks like a SET query
 
36
        if tokens[1].token_name ~= "TK_SQL_SET" then return end
 
37
        if tokens[2].token_name ~= "TK_LITERAL" or tokens[2].text:upper() ~= "GLOBAL" then return end
 
38
 
 
39
        local is_ok = false
 
40
 
 
41
        if tokens[3].token_name == "TK_LITERAL" and proxy.global.config[tokens[3].text] ~= nil then
 
42
                if tokens[4].token_name == "TK_DOT" then
 
43
                        -- SET GLOBAL <scope>.<key> = <value> 
 
44
                        if tokens[5].token_name == "TK_LITERAL" and proxy.global.config[tokens[3].text][tokens[5].text] ~= nil then
 
45
                                if tokens[6].token_name == "TK_EQ" then
 
46
                                        -- next one is the value
 
47
                                        local r = parse_value(proxy.global.config[tokens[3].text][tokens[5].text], tokens[7])
 
48
 
 
49
                                        if r ~= nil then
 
50
                                                proxy.global.config[tokens[3].text][tokens[5].text] = r
 
51
 
 
52
                                                is_ok = true
 
53
                                        end
 
54
                                end
 
55
                        end
 
56
                end
 
57
        end
 
58
 
 
59
        if not is_ok then return end
 
60
 
 
61
        proxy.response = {
 
62
                type = proxy.MYSQLD_PACKET_OK
 
63
        }
 
64
 
 
65
        return proxy.PROXY_SEND_RESULT
 
66
end