3
Copyright (C) 2007 MySQL AB
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.
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.
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
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
25
local query_counter = 0
28
-- read_query() can return a resultset
30
-- You can use read_query() to return a result-set.
32
-- @param packet the mysql-packet sent by the client
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
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
44
if string.byte(packet) == proxy.COM_QUERY then
47
we use a simple string-match to split commands are word-boundaries
49
mysql> show querycounter
53
option = "querycounter"
55
spaces are ignored, the case has to be as is.
59
returns a error-packet
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)
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)
72
-- we got our commands, execute it
73
if string.lower(command) == "show" and string.lower(option) == "querycounter" then
75
-- proxy.PROXY_SEND_RESULT requires
77
-- proxy.response.type to be either
78
-- * proxy.MYSQLD_PACKET_OK or
79
-- * proxy.MYSQLD_PACKET_ERR
81
-- for proxy.MYSQLD_PACKET_OK you need a resultset
85
-- for proxy.MYSQLD_PACKET_ERR
87
proxy.response.type = proxy.MYSQLD_PACKET_OK
88
proxy.response.resultset = {
90
{ type = proxy.MYSQL_TYPE_LONG, name = "global_query_counter", },
91
{ type = proxy.MYSQL_TYPE_LONG, name = "query_counter", },
94
{ proxy.global.query_counter, query_counter }
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"
104
return proxy.PROXY_SEND_RESULT