MySQL  8.0.19
Source Code Documentation
service_parser.h
Go to the documentation of this file.
1 #ifndef MYSQL_SERVICE_PARSER_INCLUDED
2 #define MYSQL_SERVICE_PARSER_INCLUDED
3 /* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
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, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation. The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24 
25 #include <mysql/mysql_lex_string.h>
26 
27 #ifndef MYSQL_ABI_CHECK
28 #include <stdlib.h>
29 #endif
30 
31 class THD;
32 class Item;
33 #define MYSQL_THD THD *
34 typedef Item *MYSQL_ITEM;
35 
36 /**
37  @file include/mysql/service_parser.h
38 
39  Plugin service that provides access to the parser and some operations on the
40  parse tree.
41 */
42 
43 /* See DIGEST_HASH_SIZE in sql/sql_digest.h */
44 #define PARSER_SERVICE_DIGEST_LENGTH 32
45 
46 #define STATEMENT_TYPE_SELECT 1
47 #define STATEMENT_TYPE_UPDATE 2
48 #define STATEMENT_TYPE_INSERT 3
49 #define STATEMENT_TYPE_DELETE 4
50 #define STATEMENT_TYPE_REPLACE 5
51 #define STATEMENT_TYPE_OTHER 6
52 
53 typedef int (*parse_node_visit_function)(MYSQL_ITEM item, unsigned char *arg);
54 
55 typedef int (*sql_condition_handler_function)(int sql_errno,
56  const char *sqlstate,
57  const char *msg, void *state);
58 
59 struct my_thread_handle;
60 
62 
64 
65 typedef void (*mysql_start_thread_t)(MYSQL_THD thd,
66  void *(*callback_fun)(void *), void *arg,
67  struct my_thread_handle *thread_handle);
68 
69 typedef void (*mysql_join_thread_t)(struct my_thread_handle *thread_handle);
70 
72  const MYSQL_LEX_STRING db);
73 
74 /**
75  Parses the query.
76 
77  @param thd The session in which to parse.
78 
79  @param query The query to parse.
80 
81  @param is_prepared If non-zero, the query will be parsed as a prepared
82  statement and won't throw errors when the query string contains '?'.
83 
84  @param handle_condition Callback function that is called if a condition is
85  raised during the preparation, parsing or cleanup after parsing. If this
86  argument is non-NULL, the diagnostics area will be cleared before this
87  function returns.
88 
89  @param condition_handler_state Will be passed to handle_condition when
90  called. Otherwise ignored.
91 
92  @retval 0 Success.
93  @retval 1 Parse error.
94 */
95 typedef int (*mysql_parse_t)(MYSQL_THD thd, const MYSQL_LEX_STRING query,
96  unsigned char is_prepared,
97  sql_condition_handler_function handle_condition,
98  void *condition_handler_state);
99 
101 
102 /**
103  Returns the digest of the last parsed statement in the session.
104 
105  @param thd The session in which the statement was parsed.
106 
107  @param [out] digest An area of at least size PARSER_SERVICE_DIGEST_LENGTH,
108  where the digest is written.
109 
110  @retval 0 Success.
111  @retval 1 Parse error.
112 */
114  unsigned char *digest);
115 
116 /**
117  Returns the number of parameters ('?') in the parsed query.
118  This works only if the last query was parsed as a prepared statement.
119 
120  @param thd The session in which the query was parsed.
121 
122  @return The number of parameter markers.
123 */
125 
126 /**
127  Stores in 'positions' the positions in the last parsed query of each
128  parameter marker('?'). Positions must be an already allocated array of at
129  least mysql_parser_service_st::mysql_get_number_params() size. This works
130  only if the last query was parsed as a prepared statement.
131 
132  @param thd The session in which the query was parsed.
133 
134  @param positions An already allocated array of at least
135  mysql_parser_service_st::mysql_get_number_params() size.
136 
137  @return The number of parameter markers and hence number of written
138  positions.
139 */
140 typedef int (*mysql_extract_prepared_params_t)(MYSQL_THD thd, int *positions);
141 
142 /**
143  Walks the tree depth first and applies a user defined function on each
144  literal.
145 
146  @param thd The session in which the query was parsed.
147 
148  @param processor Will be called for each literal in the parse tree.
149 
150  @param arg Will be passed as argument to each call to 'processor'.
151 */
152 typedef int (*mysql_visit_tree_t)(MYSQL_THD thd,
153  parse_node_visit_function processor,
154  unsigned char *arg);
155 
156 /**
157  Renders the MYSQL_ITEM as a string and returns a reference in the form of
158  a MYSQL_LEX_STRING. The string buffer is allocated by the server and must
159  be freed by mysql_free_string().
160 
161  @param item The literal to print.
162 
163  @return The result of printing the literal.
164 
165  @see mysql_parser_service_st::mysql_free_string().
166 */
168 
169 /**
170  Frees a string buffer allocated by the server.
171 
172  @param string The string whose buffer will be freed.
173 */
174 typedef void (*mysql_free_string_t)(MYSQL_LEX_STRING string);
175 
176 /**
177  Returns the current query string. This string is managed by the server and
178  should @b not be freed by a plugin.
179 
180  @param thd The session in which the query was submitted.
181 
182  @return The query string.
183 */
185 
186 /**
187  Returns the current query in normalized form. This string is managed by
188  the server and should @b not be freed by a plugin.
189 
190  @param thd The session in which the query was submitted.
191 
192  @return The query string normalized.
193 */
195 
196 extern "C" struct mysql_parser_service_st {
213 
214 #ifdef MYSQL_DYNAMIC_PLUGIN
215 
216 #define mysql_parser_current_session() \
217  mysql_parser_service->mysql_current_session()
218 
219 #define mysql_parser_open_session() mysql_parser_service->mysql_open_session()
220 
221 #define mysql_parser_start_thread(thd, func, arg, thread_handle) \
222  mysql_parser_service->mysql_start_thread(thd, func, arg, thread_handle)
223 
224 #define mysql_parser_join_thread(thread_handle) \
225  mysql_parser_service->mysql_join_thread(thread_handle)
226 
227 #define mysql_parser_set_current_database(thd, db) \
228  mysql_parser_service->mysql_set_current_database(thd, db)
229 
230 #define mysql_parser_parse(thd, query, is_prepared, condition_handler, \
231  condition_handler_state) \
232  mysql_parser_service->mysql_parse( \
233  thd, query, is_prepared, condition_handler, condition_handler_state)
234 
235 #define mysql_parser_get_statement_type(thd) \
236  mysql_parser_service->mysql_get_statement_type(thd)
237 
238 #define mysql_parser_get_statement_digest(thd, digest) \
239  mysql_parser_service->mysql_get_statement_digest(thd, digest)
240 
241 #define mysql_parser_get_number_params(thd) \
242  mysql_parser_service->mysql_get_number_params(thd)
243 
244 #define mysql_parser_extract_prepared_params(thd, positions) \
245  mysql_parser_service->mysql_extract_prepared_params(thd, positions)
246 
247 #define mysql_parser_visit_tree(thd, processor, arg) \
248  mysql_parser_service->mysql_visit_tree(thd, processor, arg)
249 
250 #define mysql_parser_item_string(item) \
251  mysql_parser_service->mysql_item_string(item)
252 
253 #define mysql_parser_free_string(string) \
254  mysql_parser_service->mysql_free_string(string)
255 
256 #define mysql_parser_get_query(thd) mysql_parser_service->mysql_get_query(thd)
257 
258 #define mysql_parser_get_normalized_query(thd) \
259  mysql_parser_service->mysql_get_normalized_query(thd)
260 
261 #else
262 typedef void *(*callback_function)(void *);
265 void mysql_parser_start_thread(MYSQL_THD thd, callback_function fun, void *arg,
266  struct my_thread_handle *thread_handle);
267 void mysql_parser_join_thread(struct my_thread_handle *thread_handle);
269  const MYSQL_LEX_STRING db);
271  unsigned char is_prepared,
272  sql_condition_handler_function handle_condition,
273  void *condition_handler_state);
275 int mysql_parser_get_statement_digest(MYSQL_THD thd, unsigned char *digest);
277 int mysql_parser_extract_prepared_params(MYSQL_THD thd, int *positions);
279  unsigned char *arg);
284 
285 #endif /* MYSQL_DYNAMIC_PLUGIN */
286 
287 #endif /* MYSQL_SERVICE_PARSER_INCLUDED */
mysql_parser_get_query
MYSQL_LEX_STRING mysql_parser_get_query(MYSQL_THD thd)
Definition: parser_service.cc:346
MYSQL_THD
#define MYSQL_THD
Definition: service_parser.h:33
Item
Definition: item.h:665
THD
Definition: sql_class.h:764
mysql_parser_start_thread
void mysql_parser_start_thread(MYSQL_THD thd, callback_function fun, void *arg, struct my_thread_handle *thread_handle)
mysql_parser_service_st::mysql_get_statement_type
mysql_get_statement_type_t mysql_get_statement_type
Definition: service_parser.h:203
msg
char msg[1024]
Definition: test_sql_9_sessions.cc:281
callback_function
void *(* callback_function)(void *)
Definition: service_parser.h:262
mysql_parser_service_st::mysql_set_current_database
mysql_set_current_database_t mysql_set_current_database
Definition: service_parser.h:201
mysql_parser_service_st::mysql_extract_prepared_params
mysql_extract_prepared_params_t mysql_extract_prepared_params
Definition: service_parser.h:206
mysql_get_query_t
MYSQL_LEX_STRING(* mysql_get_query_t)(MYSQL_THD thd)
Returns the current query string.
Definition: service_parser.h:184
mysql_parser_free_string
void mysql_parser_free_string(MYSQL_LEX_STRING string)
Definition: parser_service.cc:344
mysql_set_current_database_t
void(* mysql_set_current_database_t)(MYSQL_THD thd, const MYSQL_LEX_STRING db)
Definition: service_parser.h:71
mysql_parser_extract_prepared_params
int mysql_parser_extract_prepared_params(MYSQL_THD thd, int *positions)
Definition: parser_service.cc:318
mysql_current_session_t
MYSQL_THD(* mysql_current_session_t)()
Definition: service_parser.h:61
mysql_parser_service_st::mysql_get_normalized_query
mysql_get_normalized_query_t mysql_get_normalized_query
Definition: service_parser.h:211
mysql_parser_set_current_database
void mysql_parser_set_current_database(MYSQL_THD thd, const MYSQL_LEX_STRING db)
Definition: parser_service.cc:221
mysql_parser_get_statement_digest
int mysql_parser_get_statement_digest(MYSQL_THD thd, unsigned char *digest)
Definition: parser_service.cc:304
mysql_parser_service_st::mysql_get_query
mysql_get_query_t mysql_get_query
Definition: service_parser.h:210
mysql_parser_get_number_params
int mysql_parser_get_number_params(MYSQL_THD thd)
Definition: parser_service.cc:314
mysql_parser_service_st::mysql_free_string
mysql_free_string_t mysql_free_string
Definition: service_parser.h:209
mysql_parser_join_thread
void mysql_parser_join_thread(struct my_thread_handle *thread_handle)
Definition: parser_service.cc:217
mysql_parser_get_normalized_query
MYSQL_LEX_STRING mysql_parser_get_normalized_query(MYSQL_THD thd)
Definition: parser_service.cc:352
mysql_parser_service_st::mysql_start_thread
mysql_start_thread_t mysql_start_thread
Definition: service_parser.h:199
mysql_parser_service_st
Definition: service_parser.h:196
mysql_parser_visit_tree
int mysql_parser_visit_tree(MYSQL_THD thd, parse_node_visit_function processor, unsigned char *arg)
Definition: parser_service.cc:326
mysql_join_thread_t
void(* mysql_join_thread_t)(struct my_thread_handle *thread_handle)
Definition: service_parser.h:69
mysql_parser_current_session
MYSQL_THD mysql_parser_current_session()
Definition: parser_service.cc:138
mysql_parse_t
int(* mysql_parse_t)(MYSQL_THD thd, const MYSQL_LEX_STRING query, unsigned char is_prepared, sql_condition_handler_function handle_condition, void *condition_handler_state)
Parses the query.
Definition: service_parser.h:95
mysql_get_normalized_query_t
MYSQL_LEX_STRING(* mysql_get_normalized_query_t)(MYSQL_THD thd)
Returns the current query in normalized form.
Definition: service_parser.h:194
mysql_get_statement_digest_t
int(* mysql_get_statement_digest_t)(MYSQL_THD thd, unsigned char *digest)
Returns the digest of the last parsed statement in the session.
Definition: service_parser.h:113
mysql_open_session_t
MYSQL_THD(* mysql_open_session_t)()
Definition: service_parser.h:63
mysql_parser_service_st::mysql_visit_tree
mysql_visit_tree_t mysql_visit_tree
Definition: service_parser.h:207
parse_node_visit_function
int(* parse_node_visit_function)(MYSQL_ITEM item, unsigned char *arg)
Definition: service_parser.h:53
mysql_item_string_t
MYSQL_LEX_STRING(* mysql_item_string_t)(MYSQL_ITEM item)
Renders the MYSQL_ITEM as a string and returns a reference in the form of a MYSQL_LEX_STRING.
Definition: service_parser.h:167
mysql_parser_service_st::mysql_get_statement_digest
mysql_get_statement_digest_t mysql_get_statement_digest
Definition: service_parser.h:204
my_thread_handle
Definition: my_thread_bits.h:51
mysql_free_string_t
void(* mysql_free_string_t)(MYSQL_LEX_STRING string)
Frees a string buffer allocated by the server.
Definition: service_parser.h:174
mysql_start_thread_t
void(* mysql_start_thread_t)(MYSQL_THD thd, void *(*callback_fun)(void *), void *arg, struct my_thread_handle *thread_handle)
Definition: service_parser.h:65
query
static char * query
Definition: myisam_ftdump.cc:44
mysql_parser_open_session
MYSQL_THD mysql_parser_open_session()
Definition: parser_service.cc:140
mysql_parser_item_string
MYSQL_LEX_STRING mysql_parser_item_string(MYSQL_ITEM item)
Definition: parser_service.cc:332
MYSQL_LEX_STRING
Definition: mysql_lex_string.h:34
mysql_parser_service_st::mysql_join_thread
mysql_join_thread_t mysql_join_thread
Definition: service_parser.h:200
MYSQL_ITEM
Item * MYSQL_ITEM
Definition: service_parser.h:34
mysql_get_number_params_t
int(* mysql_get_number_params_t)(MYSQL_THD thd)
Returns the number of parameters ('?') in the parsed query.
Definition: service_parser.h:124
mysql_parser_service_st::mysql_parse
mysql_parse_t mysql_parse
Definition: service_parser.h:202
mysql_parser_parse
int mysql_parser_parse(MYSQL_THD thd, const MYSQL_LEX_STRING query, unsigned char is_prepared, sql_condition_handler_function handle_condition, void *condition_handler_state)
Definition: parser_service.cc:232
mysql_parser_service_st::mysql_current_session
mysql_current_session_t mysql_current_session
Definition: service_parser.h:197
mysql_lex_string.h
mysql_parser_service_st::mysql_item_string
mysql_item_string_t mysql_item_string
Definition: service_parser.h:208
sql_condition_handler_function
int(* sql_condition_handler_function)(int sql_errno, const char *sqlstate, const char *msg, void *state)
Definition: service_parser.h:55
mysql_parser_service_st::mysql_get_number_params
mysql_get_number_params_t mysql_get_number_params
Definition: service_parser.h:205
mysql_parser_service_st::mysql_open_session
mysql_open_session_t mysql_open_session
Definition: service_parser.h:198
mysql_extract_prepared_params_t
int(* mysql_extract_prepared_params_t)(MYSQL_THD thd, int *positions)
Stores in 'positions' the positions in the last parsed query of each parameter marker('?...
Definition: service_parser.h:140
mysql_visit_tree_t
int(* mysql_visit_tree_t)(MYSQL_THD thd, parse_node_visit_function processor, unsigned char *arg)
Walks the tree depth first and applies a user defined function on each literal.
Definition: service_parser.h:152
mysql_parser_service
struct mysql_parser_service_st * mysql_parser_service
mysql_get_statement_type_t
int(* mysql_get_statement_type_t)(MYSQL_THD thd)
Definition: service_parser.h:100
mysql_parser_get_statement_type
int mysql_parser_get_statement_type(MYSQL_THD thd)
Definition: parser_service.cc:282