service_command.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #ifndef MYSQL_SERVICE_COMMAND_INCLUDED
  2. #define MYSQL_SERVICE_COMMAND_INCLUDED
  3. /* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License, version 2.0,
  6. as published by the Free Software Foundation.
  7. This program is also distributed with certain software (including
  8. but not limited to OpenSSL) that is licensed under separate terms,
  9. as designated in a particular file or component or in included license
  10. documentation. The authors of MySQL hereby grant you an additional
  11. permission to link the program and your derivative works with the
  12. separately licensed software that they have included with MySQL.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License, version 2.0, for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  20. /**
  21. @file
  22. Header file for the Command service. This service is to provide means
  23. of executing different commands, like COM_QUERY, COM_STMT_PREPARE,
  24. in the server.
  25. */
  26. #include "mysql/service_srv_session.h"
  27. #include "mysql/com_data.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include "mysql_time.h"
  32. #include "decimal.h"
  33. #ifndef MYSQL_ABI_CHECK
  34. #include "m_ctype.h"
  35. #include <stdint.h> /* uint32_t */
  36. #endif
  37. /* POD structure for the field metadata from the server */
  38. struct st_send_field
  39. {
  40. const char *db_name;
  41. const char *table_name;
  42. const char *org_table_name;
  43. const char *col_name;
  44. const char *org_col_name;
  45. unsigned long length;
  46. unsigned int charsetnr;
  47. unsigned int flags;
  48. unsigned int decimals;
  49. enum_field_types type;
  50. };
  51. struct st_command_service_cbs
  52. {
  53. /*
  54. For a statement that returns a result, the flow of called callbacks will be:
  55. start_result_metadata()
  56. field_metadata()
  57. ....
  58. end_result_metadata() (in the classic protocol this generates an EOF packet)
  59. start_row()
  60. get_xxx()
  61. ...
  62. end_row()
  63. start_row()
  64. get_xxx()
  65. ...
  66. end_row()
  67. handle_ok() (with data for an EOF packet)
  68. For a statement that does NOT return a result, but only status, like
  69. INSERT, UPDATE, DELETE, REPLACE, TRUNCATE, CREATE, DROP, ALTER, etc. only
  70. handle_ok() will be invoked, in case of success.
  71. All statements that result in an error will invoke handle_error().
  72. For statements that return a result set, handle_error() might be invoked
  73. even after metadata was sent. This will indicate an error during the
  74. execution of the statement.
  75. */
  76. /*** Getting metadata ***/
  77. /**
  78. Indicates beginning of metadata for the result set
  79. @param ctx Plugin's context
  80. @param num_cols Number of fields being sent
  81. @param flags Flags to alter the metadata sending
  82. @param resultcs Charset of the result set
  83. @note resultcs is the charset in which the data should be encoded before
  84. sent to the client. This is the value of the session variable
  85. character_set_results. The implementor most probably will need to save
  86. this value in the context and use it as "to" charset in get_string().
  87. In case of CS_BINARY_REPRESENTATION, get_string() receives as a parameter
  88. the charset of the string, as it is stored on disk.
  89. In case of CS_TEXT_REPRESENTATION, the string value might be already a
  90. stringified value or non-string data, which is in character_set_results.
  91. @returns
  92. 1 an error occured, server will abort the command
  93. 0 ok
  94. */
  95. int (*start_result_metadata)(void *ctx, uint num_cols, uint flags,
  96. const CHARSET_INFO *resultcs);
  97. /**
  98. Field metadata is provided via this callback
  99. @param ctx Plugin's context
  100. @param field Field's metadata (see field.h)
  101. @param charset Field's charset
  102. @returns
  103. 1 an error occured, server will abort the command
  104. 0 ok
  105. */
  106. int (*field_metadata)(void *ctx, struct st_send_field *field,
  107. const CHARSET_INFO *charset);
  108. /**
  109. Indicates end of metadata for the result set
  110. @param ctx Plugin's context
  111. @param server_status Status of server (see mysql_com.h, SERVER_STATUS_*)
  112. @param warn_count Number of warnings generated during execution to the
  113. moment when the metadata is sent.
  114. @returns
  115. 1 an error occured, server will abort the command
  116. 0 ok
  117. */
  118. int (*end_result_metadata)(void *ctx, uint server_status,
  119. uint warn_count);
  120. /**
  121. Indicates the beginning of a new row in the result set/metadata
  122. @param ctx Plugin's context
  123. @returns
  124. 1 an error occured, server will abort the command
  125. 0 ok
  126. */
  127. int (*start_row)(void *ctx);
  128. /**
  129. Indicates the end of the current row in the result set/metadata
  130. @param ctx Plugin's context
  131. @returns
  132. 1 an error occured, server will abort the command
  133. 0 ok
  134. */
  135. int (*end_row)(void *ctx);
  136. /**
  137. An error occured during execution
  138. @details This callback indicates that an error occured during command
  139. execution and the partial row should be dropped. Server will raise error
  140. and return.
  141. @param ctx Plugin's context
  142. @returns
  143. true an error occured, server will abort the command
  144. false ok
  145. */
  146. void (*abort_row)(void *ctx);
  147. /**
  148. Return client's capabilities (see mysql_com.h, CLIENT_*)
  149. @param ctx Plugin's context
  150. @return Bitmap of client's capabilities
  151. */
  152. ulong (*get_client_capabilities)(void *ctx);
  153. /****** Getting data ******/
  154. /**
  155. Receive NULL value from server
  156. @param ctx Plugin's context
  157. @returns
  158. 1 an error occured, server will abort the command
  159. 0 ok
  160. */
  161. int (*get_null)(void * ctx);
  162. /**
  163. Receive TINY/SHORT/LONG value from server
  164. @param ctx Plugin's context
  165. @param value Value received
  166. @note In order to know which type exactly was received, the plugin must
  167. track the metadata that was sent just prior to the result set.
  168. @returns
  169. 1 an error occured, server will abort the command
  170. 0 ok
  171. */
  172. int (*get_integer)(void * ctx, longlong value);
  173. /**
  174. Get LONGLONG value from server
  175. @param ctx Plugin's context
  176. @param value Value received
  177. @param is_unsigned TRUE <=> value is unsigned
  178. @returns
  179. 1 an error occured, server will abort the command
  180. 0 ok
  181. */
  182. int (*get_longlong)(void * ctx, longlong value, uint is_unsigned);
  183. /**
  184. Receive DECIMAL value from server
  185. @param ctx Plugin's context
  186. @param value Value received
  187. @returns
  188. 1 an error occured, server will abort the command
  189. 0 ok
  190. */
  191. int (*get_decimal)(void * ctx, const decimal_t * value);
  192. /**
  193. Receive FLOAT/DOUBLE from server
  194. @param ctx Plugin's context
  195. @param value Value received
  196. @param decimals Number of decimals
  197. @note In order to know which type exactly was received, the plugin must
  198. track the metadata that was sent just prior to the result set.
  199. @returns
  200. 1 an error occured, server will abort the command
  201. 0 ok
  202. */
  203. int (*get_double)(void * ctx, double value, uint32_t decimals);
  204. /**
  205. Get DATE value from server
  206. @param ctx Plugin's context
  207. @param value Value received
  208. @returns
  209. 1 an error occured during storing, server will abort the command
  210. 0 ok
  211. */
  212. int (*get_date)(void * ctx, const MYSQL_TIME * value);
  213. /**
  214. Receive TIME value from server
  215. @param ctx Plugin's context
  216. @param value Value received
  217. @param decimals Number of decimals
  218. @returns
  219. 1 an error occured during storing, server will abort the command
  220. 0 ok
  221. */
  222. int (*get_time)(void * ctx, const MYSQL_TIME * value, uint decimals);
  223. /**
  224. Receive DATETIME value from server
  225. @param ctx Plugin's context
  226. @param value Value received
  227. @param decimals Number of decimals
  228. @returns
  229. 1 an error occured during storing, server will abort the command
  230. 0 ok
  231. */
  232. int (*get_datetime)(void * ctx, const MYSQL_TIME * value, uint decimals);
  233. /**
  234. Get STRING value from server
  235. @param ctx Plugin's context
  236. @param value Data
  237. @param length Data length
  238. @param valuecs Data charset
  239. @note In case of CS_BINARY_REPRESENTATION, get_string() receives as
  240. a parameter the charset of the string, as it is stored on disk.
  241. In case of CS_TEXT_REPRESENTATION, the string value might be already a
  242. stringified value or non-string data, which is in character_set_results.
  243. @see start_result_metadata()
  244. @returns
  245. 1 an error occured, server will abort the command
  246. 0 ok
  247. */
  248. int (*get_string)(void * ctx, const char * value, size_t length,
  249. const CHARSET_INFO * valuecs);
  250. /****** Getting execution status ******/
  251. /**
  252. Command ended with success
  253. @param ctx Plugin's context
  254. @param server_status Status of server (see mysql_com.h,
  255. SERVER_STATUS_*)
  256. @param statement_warn_count Number of warnings thrown during execution
  257. @param affected_rows Number of rows affected by the command
  258. @param last_insert_id Last insert id being assigned during execution
  259. @param message A message from server
  260. */
  261. void (*handle_ok)(void * ctx,
  262. uint server_status, uint statement_warn_count,
  263. ulonglong affected_rows, ulonglong last_insert_id,
  264. const char * message);
  265. /**
  266. Command ended with ERROR
  267. @param ctx Plugin's context
  268. @param sql_errno Error code
  269. @param err_msg Error message
  270. @param sqlstate SQL state correspongin to the error code
  271. */
  272. void (*handle_error)(void * ctx, uint sql_errno, const char * err_msg,
  273. const char * sqlstate);
  274. /**
  275. Callback for shutdown notification from the server.
  276. @param ctx Plugin's context
  277. @param server_shutdown Whether this is a normal connection shutdown (0) or
  278. server shutdown (1).
  279. */
  280. void (*shutdown)(void *ctx, int server_shutdown);
  281. };
  282. enum cs_text_or_binary
  283. {
  284. CS_TEXT_REPRESENTATION= 1, /* Let the server convert everything to string */
  285. CS_BINARY_REPRESENTATION= 2, /* Let the server use native types */
  286. };
  287. extern struct command_service_st {
  288. int (*run_command)(MYSQL_SESSION session,
  289. enum enum_server_command command,
  290. const union COM_DATA * data,
  291. const CHARSET_INFO * client_cs,
  292. const struct st_command_service_cbs * callbacks,
  293. enum cs_text_or_binary text_or_binary,
  294. void * service_callbacks_ctx);
  295. } *command_service;
  296. #ifdef MYSQL_DYNAMIC_PLUGIN
  297. #define command_service_run_command(t, command, data, cset, cbs, t_or_b, ctx) \
  298. command_service->run_command((t), (command), (data), (cset), \
  299. (cbs), (t_or_b), (ctx))
  300. #else
  301. /**
  302. Executes a server command in a session.
  303. There are two cases. Execution in a physical thread :
  304. 1. initialized by the srv_session service
  305. 2. NOT initialized by the srv_session service
  306. In case of 1, if there is currently attached session, and it is
  307. different from the passed one, the former will be automatically
  308. detached. The session to be used for the execution will then be
  309. attached. After the command is executed, the attached session will
  310. not be detached. It will be detached by a next call to run_command()
  311. with another session as parameter. In other words, for all sessions
  312. used in a physical thread, there will be at most one in attached
  313. state.
  314. In case of 2, the current state (current_thd) will be
  315. preserved. Then the given session will move to attached state and
  316. the command will be executed. After the execution the state of the
  317. session will be changed to detached and the preserved state
  318. (current_thd) will be restored.
  319. The client charset is used for commands like COM_QUERY and
  320. COM_STMT_PREPARE to know how to threat the char* fields. This
  321. charset will be used until the next call of run_command when it may
  322. be changed again.
  323. @param session The session
  324. @param command The command to be executed.
  325. @param data The data needed for the command to be executed
  326. @param client_cs The charset for the string data input(COM_QUERY for example)
  327. @param callbacks Callbacks to be used by the server to encode data and
  328. to communicate with the client (plugin) side.
  329. @param text_or_binary Select which representation the server will use for the
  330. data passed to the callbacks. For more information
  331. @see cs_text_or_binary enum
  332. @param service_callbacks_ctx Context passed to the command service callbacks
  333. @return
  334. 0 success
  335. 1 failure
  336. */
  337. int command_service_run_command(MYSQL_SESSION session,
  338. enum enum_server_command command,
  339. const union COM_DATA * data,
  340. const CHARSET_INFO * client_cs,
  341. const struct st_command_service_cbs * callbacks,
  342. enum cs_text_or_binary text_or_binary,
  343. void * service_callbacks_ctx);
  344. #endif /* MYSQL_DYNAMIC_PLUGIN */
  345. #ifdef __cplusplus
  346. }
  347. #endif
  348. #endif