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