MySQL 9.0.0
Source Code Documentation
protocol_callback.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef PROTOCOL_CALLBACK_INCLUDED
25#define PROTOCOL_CALLBACK_INCLUDED
26
27/**
28@file
29 Interface of the Protocol_callback class, which is used by the Command
30 service as proxy protocol.
31*/
32
33#include <stddef.h>
34#include <sys/types.h>
35
36#include "my_command.h"
37#include "my_inttypes.h"
39#include "mysql/strings/dtoa.h"
41#include "mysql_time.h"
42#include "sql/protocol.h"
43#include "violite.h"
44
45class Item_param;
46class Send_field;
47class String;
48class my_decimal;
49template <class T>
50class List;
51union COM_DATA;
52
53class Protocol_callback final : public Protocol {
54 public:
56 enum cs_text_or_binary t_or_b, void *cbs_ctx)
57 : callbacks_ctx(cbs_ctx),
58 callbacks(*cbs),
61 text_or_binary(t_or_b),
62 in_meta_sending(false) {}
63
64 /**
65 Forces read of packet from the connection
66
67 @return bytes read
68 @retval -1 failure
69 */
70 int read_packet() override;
71
72 /**
73 Reads from the line and parses the data into union COM_DATA
74
75 @return bytes read
76 @retval -1 failure
77 */
78 int get_command(COM_DATA *com_data, enum_server_command *cmd) override;
79
80 /**
81 Returns the type of the protocol
82
83 @retval false success
84 @retval true failure
85 */
86 enum enum_protocol_type type() const override { return PROTOCOL_PLUGIN; }
87
88 /**
89 Returns the type of the connection
90
91 @return enum enum_vio_type
92 */
93 enum enum_vio_type connection_type() const override;
94
95 /**
96 Sends null value
97
98 @retval false success
99 @retval true failure
100 */
101 bool store_null() override;
102
103 /**
104 Sends TINYINT value
105
106 @param from value
107
108 @retval false success
109 @retval true failure
110 */
111 bool store_tiny(longlong from, uint32) override;
112
113 /**
114 Sends SMALLINT value
115
116 @param from value
117
118 @retval false success
119 @retval true failure
120 */
121 bool store_short(longlong from, uint32) override;
122
123 /**
124 Sends INT/INTEGER value
125
126 @param from value
127
128 @retval false success
129 @retval true failure
130 */
131 bool store_long(longlong from, uint32) override;
132
133 /**
134 Sends BIGINT value
135
136 @param from value
137 @param is_unsigned from is unsigned
138
139 @retval false success
140 @retval true failure
141 */
142 bool store_longlong(longlong from, bool is_unsigned, uint32) override;
143
144 /**
145 Sends DECIMAL value
146
147 @param d value
148
149 @retval false success
150 @retval true failure
151 */
152 bool store_decimal(const my_decimal *d, uint, uint) override;
153
154 /**
155 Sends string (CHAR/VARCHAR/TEXT/BLOB) value
156
157 @retval false success
158 @retval true failure
159 */
160 bool store_string(const char *from, size_t length,
161 const CHARSET_INFO *fromcs) override;
162
163 /**
164 Sends FLOAT value
165
166 @param from value
167 @param decimals number of digits to use after decimal point, unless it is
168 DECIMAL_NOT_SPECIFIED
169
170 @retval false success
171 @retval true failure
172 */
173 bool store_float(float from, uint32 decimals, uint32) override;
174
175 /**
176 Sends DOUBLE value
177
178 @param from value
179 @param decimals number of digits to use after decimal point, unless it is
180 DECIMAL_NOT_SPECIFIED
181
182 @retval false success
183 @retval true failure
184 */
185 bool store_double(double from, uint32 decimals, uint32) override;
186
187 /**
188 Sends DATETIME value
189
190 @param time value
191 @param precision fractional seconds precision. 0 ... DATETIME_MAX_DECIMALS
192
193 @retval false success
194 @retval true failure
195 */
196 bool store_datetime(const MYSQL_TIME &time, uint precision) override;
197
198 /**
199 Sends DATE value
200
201 @param time value
202
203 @retval false success
204 @retval true failure
205 */
206 bool store_date(const MYSQL_TIME &time) override;
207
208 /**
209 Sends TIME value
210
211 @param time value
212 @param precision fractional seconds precision. 0 ... DATETIME_MAX_DECIMALS
213
214 @retval false success
215 @retval true failure
216 */
217 bool store_time(const MYSQL_TIME &time, uint precision) override;
218
219 /**
220 Sends Field
221
222 @param field the field to be sent through the protocol
223
224 @retval false success
225 @retval true failure
226 */
227 bool store_field(const Field *field) override;
228
229 /**
230 Returns the capabilities supported by the protocol
231 */
232 ulong get_client_capabilities() override;
233
234 /**
235 Checks if the protocol supports a capability
236
237 @param capability the capability
238
239 @retval true supports
240 @retval false does not support
241 */
242 bool has_client_capability(unsigned long capability) override;
243
244 /**
245 Called BEFORE sending data row or before field_metadata
246 */
247 void start_row() override;
248
249 /**
250 Called AFTER sending data row or before field_metadata
251 */
252 bool end_row() override;
253
254 /**
255 Called when a row is aborted
256 */
257 void abort_row() override;
258
259 /**
260 Called in case of error while sending data
261 */
262 void end_partial_result_set() override;
263
264 /**
265 Called when the server shuts down the connection (THD is being destroyed).
266 In this regard, this is also called when the server shuts down. The callback
267 implementor can differentiate between those 2 events by inspecting the
268 shutdown_type parameter.
269
270 @param server_shutdown Whether this is a normal connection shutdown (false)
271 or a server shutdown (true).
272
273 @retval 0 success
274 @retval !0 failure
275 */
276 int shutdown(bool server_shutdown = false) override;
277
278 /**
279 This function always returns true as in many places in the server this
280 is a prerequisite for continuing operations.
281
282 @retval true alive
283 */
284 bool connection_alive() const override;
285
286 /**
287 Should return protocol's reading/writing status. Returns 0 (idle) as it
288 this is the best guess that can be made as there is no callback for
289 get_rw_status().
290 */
291 uint get_rw_status() override;
292
293 /**
294 Checks if compression is enabled
295
296 @retval true enabled
297 @retval false disabled
298 */
299 bool get_compression() override;
300
301 /**
302 Checks if compression is enabled and return compression method name
303
304 @return algorithm name if compression is supported else null
305 */
306 char *get_compression_algorithm() override;
307
308 /**
309 Checks if compression is enabled and return compression level.
310
311 @return compression level if compression is supported else 0
312 */
313 uint get_compression_level() override;
314
315 /**
316 Called BEFORE sending metadata
317
318 @param num_cols Number of columns in the result set
319 @param flags flags to be used to alter the way the messages are sent to
320 the client, see Protocol class for SEND_NUM_ROWS, SEND_DEFAULTS,
321 SEND_EOF
322 @param resultcs The character set of the results. Can be different from the
323 one in the field metadata.
324
325 @retval true failure
326 @retval false success
327 */
328 bool start_result_metadata(uint num_cols, uint flags,
329 const CHARSET_INFO *resultcs) override;
330
331 /**
332 Sends metadata of one field. Called for every column in the result set.
333
334 @param field Field's metadata
335 @param cs Charset
336
337 @retval true failure
338 @retval false success
339 */
340 bool send_field_metadata(Send_field *field, const CHARSET_INFO *cs) override;
341
342 /**
343 Called AFTER sending metadata
344
345 @retval true failure
346 @retval false success
347 */
348 bool end_result_metadata() override;
349
350 /**
351 Sends OK
352
353 @param server_status Bit field with different statuses. See SERVER_STATUS_*
354 @param warn_count Warning count from the execution
355 @param affected_rows Rows changed/deleted during the operation
356 @param last_insert_id ID of the last insert row, which has AUTO_INCROMENT
357 column
358 @param message Textual message from the execution. May be NULL.
359
360 @retval true failure
361 @retval false success
362 */
363 bool send_ok(uint server_status, uint warn_count, ulonglong affected_rows,
364 ulonglong last_insert_id, const char *message) override;
365
366 /**
367 Sends end of file.
368
369
370 This will be called once all data has been sent.
371
372 @param server_status Bit field with different statuses. See SERVER_STATUS_*
373 @param warn_count The warning count generated by the execution of the
374 statement.
375
376 @retval true failure
377 @retval false success
378 */
379 bool send_eof(uint server_status, uint warn_count) override;
380
381 /**
382 Sends error
383
384 @param sql_errno Error number, beginning from 1000
385 @param err_msg The error message
386 @param sql_state The SQL state - 5 char string
387
388 @retval true failure
389 @retval false success
390 */
391 bool send_error(uint sql_errno, const char *err_msg,
392 const char *sql_state) override;
393
394 bool store_ps_status(ulong stmt_id, uint column_count, uint param_count,
395 ulong cond_count) override;
396
397 bool send_parameters(List<Item_param> *parameters,
398 bool is_sql_prepare) override;
399 bool flush() override;
400
403
404 private:
405 /**
406 Set output parameters to variables bound at PS execution.
407
408 This method handles the case when preparing and executing was done
409 through SQL (not by COM_STMT_PREPARE/COM_STMT_EXECUTE) in which
410 output parameters are not going to be send to client (or
411 'st_command_service_cbs'), instead they will set concrete session
412 variables.
413
414 @param parameters List of PS/SP parameters (both input and output).
415
416 @retval false success
417 @retval true failure
418 */
420
423 unsigned long client_capabilities;
427};
428
429#endif /* PROTOCOL_CALLBACK_INCLUDED */
Definition: field.h:577
Dynamic parameters used as placeholders ('?') inside prepared statements.
Definition: item.h:4774
Definition: sql_list.h:467
Definition: protocol_callback.h:53
bool store_date(const MYSQL_TIME &time) override
Sends DATE value.
Definition: protocol_callback.cc:155
bool connection_alive() const override
This function always returns true as in many places in the server this is a prerequisite for continui...
Definition: protocol_callback.cc:280
enum enum_vio_type connection_type() const override
Returns the type of the connection.
Definition: protocol_callback.cc:84
Protocol_callback(const struct st_command_service_cbs *cbs, enum cs_text_or_binary t_or_b, void *cbs_ctx)
Definition: protocol_callback.h:55
int read_packet() override
Forces read of packet from the connection.
Definition: protocol_callback.cc:65
bool client_capabilities_set
Definition: protocol_callback.h:424
ulong get_client_capabilities() override
Returns the capabilities supported by the protocol.
Definition: protocol_callback.cc:182
bool send_error(uint sql_errno, const char *err_msg, const char *sql_state) override
Sends error.
Definition: protocol_callback.cc:426
bool store_double(double from, uint32 decimals, uint32) override
Sends DOUBLE value.
Definition: protocol_callback.cc:143
bool set_variables_from_parameters(List< Item_param > *parameters)
Set output parameters to variables bound at PS execution.
Definition: protocol_callback.cc:503
int get_command(COM_DATA *com_data, enum_server_command *cmd) override
Reads from the line and parses the data into union COM_DATA.
Definition: protocol_callback.cc:74
bool send_field_metadata(Send_field *field, const CHARSET_INFO *cs) override
Sends metadata of one field.
Definition: protocol_callback.cc:346
bool store_field(const Field *field) override
Sends Field.
Definition: protocol_callback.cc:166
bool get_compression() override
Checks if compression is enabled.
Definition: protocol_callback.cc:305
bool store_time(const MYSQL_TIME &time, uint precision) override
Sends TIME value.
Definition: protocol_callback.cc:160
bool send_ok(uint server_status, uint warn_count, ulonglong affected_rows, ulonglong last_insert_id, const char *message) override
Sends OK.
Definition: protocol_callback.cc:394
char * get_compression_algorithm() override
Checks if compression is enabled and return compression method name.
Definition: protocol_callback.cc:313
bool end_result_metadata() override
Called AFTER sending metadata.
Definition: protocol_callback.cc:374
bool send_parameters(List< Item_param > *parameters, bool is_sql_prepare) override
Sends the OUT-parameters to the client.
Definition: protocol_callback.cc:455
int shutdown(bool server_shutdown=false) override
Called when the server shuts down the connection (THD is being destroyed).
Definition: protocol_callback.cc:261
void start_row() override
Called BEFORE sending data row or before field_metadata.
Definition: protocol_callback.cc:198
uint get_rw_status() override
Should return protocol's reading/writing status.
Definition: protocol_callback.cc:297
bool send_eof(uint server_status, uint warn_count) override
Sends end of file.
Definition: protocol_callback.cc:412
struct st_command_service_cbs callbacks
Definition: protocol_callback.h:422
bool end_row() override
Called AFTER sending data row or before field_metadata.
Definition: protocol_callback.cc:226
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Called BEFORE sending metadata.
Definition: protocol_callback.cc:330
bool store_longlong(longlong from, bool is_unsigned, uint32) override
Sends BIGINT value.
Definition: protocol_callback.cc:109
enum enum_protocol_type type() const override
Returns the type of the protocol.
Definition: protocol_callback.h:86
uint get_compression_level() override
Checks if compression is enabled and return compression level.
Definition: protocol_callback.cc:321
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Sends DATETIME value.
Definition: protocol_callback.cc:149
bool store_null() override
Sends null value.
Definition: protocol_callback.cc:88
void abort_row() override
Called when a row is aborted.
Definition: protocol_callback.cc:236
bool store_ps_status(ulong stmt_id, uint column_count, uint param_count, ulong cond_count) override
Sends prepared statement's id and metadata to the client after prepare.
Definition: protocol_callback.cc:433
unsigned long client_capabilities
Definition: protocol_callback.h:423
void * callbacks_ctx
Definition: protocol_callback.h:421
bool store_string(const char *from, size_t length, const CHARSET_INFO *fromcs) override
Sends string (CHAR/VARCHAR/TEXT/BLOB) value.
Definition: protocol_callback.cc:130
bool store_float(float from, uint32 decimals, uint32) override
Sends FLOAT value.
Definition: protocol_callback.cc:137
bool in_meta_sending
Definition: protocol_callback.h:426
bool flush() override
Used for the classic protocol.
Definition: protocol_callback.cc:403
bool store_short(longlong from, uint32) override
Sends SMALLINT value.
Definition: protocol_callback.cc:99
enum cs_text_or_binary text_or_binary
Definition: protocol_callback.h:425
void end_partial_result_set() override
Called in case of error while sending data.
Definition: protocol_callback.cc:243
bool store_long(longlong from, uint32) override
Sends INT/INTEGER value.
Definition: protocol_callback.cc:104
bool store_decimal(const my_decimal *d, uint, uint) override
Sends DECIMAL value.
Definition: protocol_callback.cc:125
bool has_client_capability(unsigned long capability) override
Checks if the protocol supports a capability.
Definition: protocol_callback.cc:190
bool store_tiny(longlong from, uint32) override
Sends TINYINT value.
Definition: protocol_callback.cc:94
Definition: protocol.h:33
virtual bool store_short(longlong from, uint32 zerofill)=0
virtual bool store_long(longlong from, uint32 zerofill)=0
enum_protocol_type
Enum used by type() to specify the protocol type.
Definition: protocol.h:101
@ PROTOCOL_PLUGIN
Definition: protocol.h:107
Definition: field.h:4709
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:95
static int flags[50]
Definition: hp_test1.cc:40
A better implementation of the UNIX ctype(3) library.
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:48
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
long long int longlong
Definition: my_inttypes.h:55
uint32_t uint32
Definition: my_inttypes.h:67
Time declarations shared between the server and client API: you should not add anything to this heade...
constexpr value_type is_unsigned
Definition: classic_protocol_constants.h:273
Definition: commit_order_queue.h:34
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Header file for the Command service.
cs_text_or_binary
Definition: service_command.h:389
Definition: m_ctype.h:421
Definition: mysql_time.h:82
Definition: service_command.h:328
Definition: com_data.h:104
Vio Lite.
enum_vio_type
Definition: violite.h:79