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
local commands = require("proxy.commands")
21
local tokenizer = require("proxy.tokenizer")
22
local auto_config = require("proxy.auto-config")
24
if not proxy.global.config.analyze_query then
25
proxy.global.config.analyze_query = {
26
analyze_queries = true,
32
function read_query(packet)
33
local cmd = commands.parse(packet)
35
local r = auto_config.handle(cmd)
36
if r then return r end
38
-- analyzing queries is disabled, just pass them on
39
if not proxy.global.config.analyze_query then
43
-- we only handle normal queries
44
if cmd.type ~= proxy.COM_QUERY then
50
-- cover the query in SHOW SESSION STATUS
51
proxy.queries:append(2, string.char(proxy.COM_QUERY) .. "SHOW SESSION STATUS")
52
proxy.queries:append(1, packet)
53
proxy.queries:append(3, string.char(proxy.COM_QUERY) .. "SHOW SESSION STATUS")
55
return proxy.PROXY_SEND_QUERY
58
function read_query_result(inj)
59
local res = assert(inj.resultset)
62
if not res.query_status or res.query_status == proxy.MYSQLD_PACKET_ERR then
63
-- the query failed, let's clean the queue
68
local tokens = tokenizer.tokenize(inj.query:sub(2))
69
local norm_query = tokenizer.normalize(tokens)
71
o = "# " .. os.date("%Y-%m-%d %H:%M:%S") ..
72
" [".. proxy.connection.server.thread_id ..
73
"] user: " .. proxy.connection.client.username ..
74
", db: " .. proxy.connection.client.default_db .. "\n" ..
75
"Query: " .. string.format("%q", inj.query:sub(2)) .. "\n" ..
76
"Norm_Query: " .. string.format("%q", norm_query) .. "\n" ..
77
"Exec_time: " .. inj.query_time .. " us" .. "\n"
80
elseif inj.id == 2 then
81
-- the first SHOW SESSION STATUS
83
for row in res.rows do
84
-- 1 is the key, 2 is the value
85
baseline[row[1]] = row[2]
87
elseif inj.id == 3 then
90
for row in res.rows do
91
if baseline[row[1]] then
92
local num1 = tonumber(baseline[row[1]])
93
local num2 = tonumber(row[2])
95
if row[1] == "Com_show_status" then
97
elseif row[1] == "Questions" then
99
elseif row[1] == "Last_query_cost" then
103
if num1 and num2 and num2 - num1 > 0 then
104
o = o .. ".. " .. row[1] .. " = " .. (num2 - num1) .. "\n"
112
return proxy.PROXY_IGNORE_RESULT