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

« back to all changes in this revision

Viewing changes to tags/mysql-proxy-0.6.1/examples/tutorial-resultset.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
--[[
 
2
 
 
3
   Copyright (C) 2007 MySQL AB
 
4
 
 
5
   This program is free software; you can redistribute it and/or modify
 
6
   it under the terms of the GNU General Public License as published by
 
7
   the Free Software Foundation; version 2 of the License.
 
8
 
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with this program; if not, write to the Free Software
 
16
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
--]]
 
19
 
 
20
-- init the query-counter if it isn't done yet
 
21
if not proxy.global.query_counter then
 
22
        proxy.global.query_counter = 0
 
23
end
 
24
 
 
25
local query_counter = 0
 
26
 
 
27
---
 
28
-- read_query() can return a resultset
 
29
--
 
30
-- You can use read_query() to return a result-set. 
 
31
--
 
32
-- @param packet the mysql-packet sent by the client
 
33
--
 
34
-- @return 
 
35
--   * nothing to pass on the packet as is, 
 
36
--   * proxy.PROXY_SEND_QUERY to send the queries from the proxy.queries queue
 
37
--   * proxy.PROXY_SEND_RESULT to send your own result-set
 
38
--
 
39
function read_query( packet )
 
40
        -- a new query came in in this connection
 
41
        proxy.global.query_counter = proxy.global.query_counter + 1
 
42
        query_counter = query_counter + 1
 
43
 
 
44
        if string.byte(packet) == proxy.COM_QUERY then
 
45
                --[[
 
46
 
 
47
                we use a simple string-match to split commands are word-boundaries
 
48
                
 
49
                mysql> show querycounter
 
50
 
 
51
                is split into 
 
52
                command = "show"
 
53
                option  = "querycounter"
 
54
                
 
55
                spaces are ignored, the case has to be as is.
 
56
 
 
57
                mysql> show myerror
 
58
 
 
59
                returns a error-packet
 
60
 
 
61
                --]]
 
62
                
 
63
                -- try to match the string up to the first non-alphanum
 
64
                local f_s, f_e, command = string.find(packet, "^%s*(%w+)", 2)
 
65
                local option
 
66
 
 
67
                if f_e then
 
68
                        -- if that match, take the next sub-string as option
 
69
                        f_s, f_e, option = string.find(packet, "^%s+(%w+)", f_e + 1)
 
70
                end
 
71
        
 
72
                -- we got our commands, execute it
 
73
                if string.lower(command) == "show" and string.lower(option) == "querycounter" then
 
74
                        ---
 
75
                        -- proxy.PROXY_SEND_RESULT requires 
 
76
                        --
 
77
                        -- proxy.response.type to be either 
 
78
                        -- * proxy.MYSQLD_PACKET_OK or
 
79
                        -- * proxy.MYSQLD_PACKET_ERR
 
80
                        --
 
81
                        -- for proxy.MYSQLD_PACKET_OK you need a resultset
 
82
                        -- * fields
 
83
                        -- * rows
 
84
                        --
 
85
                        -- for proxy.MYSQLD_PACKET_ERR
 
86
                        -- * errmsg
 
87
                        proxy.response.type = proxy.MYSQLD_PACKET_OK
 
88
                        proxy.response.resultset = {
 
89
                                fields = { 
 
90
                                        { type = proxy.MYSQL_TYPE_LONG, name = "global_query_counter", },
 
91
                                        { type = proxy.MYSQL_TYPE_LONG, name = "query_counter", },
 
92
                                }, 
 
93
                                rows = { 
 
94
                                        { proxy.global.query_counter, query_counter }
 
95
                                }
 
96
                        }
 
97
 
 
98
                        -- we have our result, send it back
 
99
                        return proxy.PROXY_SEND_RESULT
 
100
                elseif string.lower(command) == "show" and string.lower(option) == "myerror" then
 
101
                        proxy.response.type = proxy.MYSQLD_PACKET_ERR
 
102
                        proxy.response.errmsg = "my first error"
 
103
                        
 
104
                        return proxy.PROXY_SEND_RESULT
 
105
                end
 
106
        end
 
107
end
 
108
 
 
109