mysql-proxy tutorial - returning a resultset

Developed In: Other — Contributed by: Jan Kneschke

MySQL Proxy is a simple program that sits between your client and MySQL server that can monitor, analyze or transform their communication. See http://forge.mysql.com/wiki/MySQL_Proxy for more information. In this forth part we aren't forwarding a query to the server, but answer it ourself. We can return result-sets and error-packets.
Jan Kneschke
Other
  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. local query_counter = 0
  21.  
  22. ---
  23. -- read_query() can return a resultset
  24. --
  25. -- You can use read_query() to return a result-set.
  26. --
  27. -- @param packet the mysql-packet sent by the client
  28. --
  29. -- @return
  30. -- * nothing to pass on the packet as is,
  31. -- * proxy.PROXY_SEND_QUERY to send the queries from the proxy.queries queue
  32. -- * proxy.PROXY_SEND_RESULT to send your own result-set
  33. --
  34. function read_query( packet )
  35. -- a new query came in in this connection
  36. query_counter = query_counter + 1
  37.  
  38. if string.byte(packet) == proxy.COM_QUERY then
  39. --[[
  40.  
  41. we use a simple string-match to split commands are word-boundaries
  42.  
  43. mysql> show querycounter
  44.  
  45. is split into
  46. command = "show"
  47. option = "querycounter"
  48.  
  49. spaces are ignored, the case has to be as is.
  50.  
  51. mysql> show myerror
  52.  
  53. returns a error-packet
  54.  
  55. --]]
  56.  
  57. -- try to match the string up to the first non-alphanum
  58. local f_s, f_e, command = string.find(packet, "^%s*(%w+)", 2)
  59. local option
  60.  
  61. if f_e then
  62. -- if that match, take the next sub-string as option
  63. f_s, f_e, option = string.find(packet, "^%s+(%w+)", f_e + 1)
  64. end
  65.  
  66. -- we got our commands, execute it
  67. if command == "show" and option == "querycounter" then
  68. ---
  69. -- proxy.PROXY_SEND_RESULT requires
  70. --
  71. -- proxy.response.type to be either
  72. -- * proxy.MYSQLD_PACKET_OK or
  73. -- * proxy.MYSQLD_PACKET_ERR
  74. --
  75. -- for proxy.MYSQLD_PACKET_OK you need a resultset
  76. -- * fields
  77. -- * rows
  78. --
  79. -- for proxy.MYSQLD_PACKET_ERR
  80. -- * errmsg
  81. proxy.response.type = proxy.MYSQLD_PACKET_OK
  82. proxy.response.resultset = {
  83. fields = {
  84. { type = proxy.MYSQL_TYPE_LONG, name = "query_counter", },
  85. },
  86. rows = {
  87. { query_counter }
  88. }
  89. }
  90.  
  91. -- we have our result, send it back
  92. return proxy.PROXY_SEND_RESULT
  93. elseif command == "show" and option == "myerror" then
  94. proxy.response.type = proxy.MYSQLD_PACKET_ERR
  95. proxy.response.errmsg = "my first error"
  96.  
  97. return proxy.PROXY_SEND_RESULT
  98. end
  99. end
  100. end
  101.  
  102.  

Current Tags

You must be logged in to tag this tool

No Comments yet

Votes

  • Rated 4.50 out of 5
Rated 4.50 out of 5 with 2 votes cast.
You must be logged in to vote.

Watches

0 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment