MySQL 9.5.0
Source Code Documentation
protocol.h
Go to the documentation of this file.
1#ifndef PROTOCOL_INCLUDED
2#define PROTOCOL_INCLUDED
3
4/* Copyright (c) 2002, 2025, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <assert.h>
28
29#include "my_temporal.h" // Time_val
30#include "mysql/com_data.h" // Protocol data
31#include "mysql/mysql_lex_string.h" // LEX_STRING
32#include "mysql_com.h" // mysql_enum_shutdown_level
33#include "mysql_time.h" // MYSQL_TIME
34#include "sql_string.h" // String
35#include "violite.h" /* SSL && enum_vio_type */
36
37class my_decimal;
38class Send_field;
39class Item_param;
40template <class T>
41class List;
42class Field;
43
44struct CHARSET_INFO;
45
46class Protocol {
47 private:
48 /// Pointer to the Protocol below on the stack.
50
51 public:
52 virtual ~Protocol() = default;
53
54 /**
55 Remove the reference to the previous protocol and return it.
56
57 @returns The new top of the Protocol stack.
58 */
60 assert(m_previous_protocol);
61 Protocol *protocol = m_previous_protocol;
62 m_previous_protocol = nullptr;
63 return protocol;
64 }
65
66 /**
67 Set reference to "this" as the previous protocol on the protocol provided
68 as argument.
69
70 @param protocol Protocol to become the top of Protocol stack.
71 */
72 void push_protocol(Protocol *protocol) {
73 assert(!protocol->m_previous_protocol);
74 protocol->m_previous_protocol = this;
75 }
76
77 /**
78 Read packet from client
79
80 @retval -1 fatal error
81 @retval 0 ok
82 @retval 1 non-fatal error
83 */
84 virtual int read_packet() = 0;
85
86 /**
87 Reads the command from the protocol and creates a command.
88
89 @param com_data out parameter
90 @param cmd out parameter
91
92 @returns
93 -1 fatal protocol error
94 0 ok
95 1 non-fatal protocol or parsing error
96 */
97 virtual int get_command(COM_DATA *com_data, enum_server_command *cmd) = 0;
98
99 /**
100 Enum used by type() to specify the protocol type
101 */
103 PROTOCOL_TEXT = 0, // text Protocol type used mostly
104 // for the old (MySQL 4.0 protocol)
105 PROTOCOL_BINARY = 1, // binary protocol type
106 PROTOCOL_LOCAL = 2, // local protocol type(intercepts communication)
107 PROTOCOL_ERROR = 3, // error protocol instance
108 PROTOCOL_PLUGIN = 4 // pluggable protocol type
109 };
110
111 /**
112 Flags available to alter the way the messages are sent to the client
113 */
114 enum { SEND_NUM_ROWS = 1, SEND_DEFAULTS = 2, SEND_EOF = 4 };
115
116 virtual enum enum_protocol_type type() const = 0;
117
118 virtual enum enum_vio_type connection_type() const = 0;
119
120 /* Data sending functions */
121 virtual bool store_null() = 0;
122 virtual bool store_tiny(longlong from, uint32 zerofill) = 0;
123 virtual bool store_short(longlong from, uint32 zerofill) = 0;
124 virtual bool store_long(longlong from, uint32 zerofill) = 0;
125 virtual bool store_longlong(longlong from, bool unsigned_flag,
126 uint32 zerofill) = 0;
127 virtual bool store_decimal(const my_decimal *, uint, uint) = 0;
128 virtual bool store_string(const char *from, size_t length,
129 const CHARSET_INFO *fromcs) = 0;
130 virtual bool store_float(float from, uint32 decimals, uint32 zerofill) = 0;
131 virtual bool store_double(double from, uint32 decimals, uint32 zerofill) = 0;
132 virtual bool store_datetime(const MYSQL_TIME &time, uint precision) = 0;
133 virtual bool store_date(const MYSQL_TIME &time) = 0;
134 virtual bool store_time(const Time_val &time, uint precision) = 0;
135 virtual bool store_field(const Field *field) = 0;
136 // Convenience wrappers
137 bool store(int from) { return store_long(longlong{from}, 0); }
138 bool store(uint32 from) { return store_long(longlong{from}, 0); }
139 bool store(longlong from) { return store_longlong(from, false, 0); }
140 bool store(ulonglong from) {
141 return store_longlong(static_cast<longlong>(from), true, 0);
142 }
143 bool store_tiny(longlong from) { return store_tiny(from, 0); }
144 bool store_short(longlong from) { return store_short(from, 0); }
145 bool store_long(longlong from) { return store_long(from, 0); }
146 bool store_longlong(longlong from, bool unsigned_flag) {
147 return store_longlong(from, unsigned_flag, 0);
148 }
149 /**
150 Send \\0 end terminated string.
151
152 @param from NullS or \\0 terminated string.
153 @param fromcs Character set of the from string.
154
155 @note In most cases one should use store(from, length, cs) instead of
156 this function
157
158 @retval false ok
159 @retval true error
160 */
161 inline bool store(const char *from, const CHARSET_INFO *fromcs) {
162 return from ? store_string(from, strlen(from), fromcs) : store_null();
163 }
164 inline bool store(String *str) {
165 return store_string(str->ptr(), str->length(), str->charset());
166 }
167 inline bool store(const LEX_STRING &s, const CHARSET_INFO *cs) {
168 return store_string(s.str, s.length, cs);
169 }
170
171 /**
172 Returns the client capabilities stored on the protocol.
173 The available capabilities are defined in mysql_com.h
174 */
175 virtual ulong get_client_capabilities() = 0;
176 /**
177 Checks if the client capabilities include the one
178 specified as parameter.
179
180 @retval true if it includes the specified capability
181 @retval false otherwise
182 */
183 virtual bool has_client_capability(unsigned long client_capability) = 0;
184
185 /**
186 Checks if the protocol's connection with the client is still alive.
187 It should always return true unless the protocol closed the connection.
188
189 @retval true if the connection is still alive
190 @retval false otherwise
191 */
192 virtual bool connection_alive() const = 0;
193
194 /**
195 Result set sending functions
196
197 @details Server uses following schema to send result:
198 ... sending metadata ...
199 | start_result_metadata(...)
200 | start_row()
201 | send_field_metadata(...)
202 | end_row()
203 ... same for each field sent ...
204 | end_result_metadata(...)
205 |
206 ... sending result ...
207 | start_row(...)
208 | store_xxx(...)
209 ... store_xxx(..) is called for each field ...
210 | end_row(...)
211 ... same for each row, until all rows are sent ...
212 | send_ok/eof/error(...)
213 However, a protocol implementation might use different schema. For
214 example, Protocol_callback ignores start/end_row when metadata is being
215 sent.
216 */
217
218 virtual void start_row() = 0;
219 virtual bool end_row() = 0;
220 virtual void abort_row() = 0;
221 virtual void end_partial_result_set() = 0;
222
223 /**
224 Thread is being shut down, disconnect and free resources
225
226 @param server_shutdown If false then this is normal thread shutdown. If
227 true then the server is shutting down.
228 */
229 virtual int shutdown(bool server_shutdown = false) = 0;
230 /**
231 Returns the read/writing status
232
233 @retval 1 Read
234 @retval 2 Write
235 @retval 0 Other(Idle, Killed)
236 */
237 virtual uint get_rw_status() = 0;
238 /**
239 Returns if the protocol is compressed or not.
240
241 @retval false Not compressed
242 @retval true Compressed
243 */
244 virtual bool get_compression() = 0;
245 /**
246 Returns compression algorithm name.
247
248 @retval string compression method name
249 @retval NULL if no compression is enabled
250 */
251 virtual char *get_compression_algorithm() = 0;
252 /**
253 Returns compression level.
254
255 @returns compression level
256 */
257 virtual uint get_compression_level() = 0;
258 /**
259 Prepares the server for metadata sending.
260 Notifies the client that the metadata sending will start.
261
262 @param num_cols Number of columns that will be sent
263 @param flags Flags to alter the metadata sending
264 Can be any of the following:
265 SEND_NUM_ROWS, SEND_DEFAULTS, SEND_EOF
266 @param resultcs Charset to convert to
267
268 @retval false Ok
269 @retval true An error occurred
270 */
271
272 virtual bool start_result_metadata(uint num_cols, uint flags,
273 const CHARSET_INFO *resultcs) = 0;
274 /**
275 Sends field metadata.
276
277 @param field Field metadata to be send to the client
278 @param charset Field's charset: in case it is different
279 than the one used by the connection it will
280 be used to convert the value to
281 the connection's charset
282
283 @retval false The metadata was successfully sent
284 @retval true An error occurred
285 */
286
287 virtual bool send_field_metadata(Send_field *field,
288 const CHARSET_INFO *charset) = 0;
289 /**
290 Signals the client that the metadata sending is done.
291 Clears the server after sending the metadata.
292
293 @retval false Ok
294 @retval true An error occurred
295 */
296 virtual bool end_result_metadata() = 0;
297
298 /**
299 Send ok message to the client.
300
301 @param server_status The server status
302 @param statement_warn_count Total number of warnings
303 @param affected_rows Number of rows changed by statement
304 @param last_insert_id Last insert id (Auto_increment id for first
305 row if used)
306 @param message Message to send to the client
307
308 @retval false The message was successfully sent
309 @retval true An error occurred and the messages wasn't sent properly
310 */
311 virtual bool send_ok(uint server_status, uint statement_warn_count,
312 ulonglong affected_rows, ulonglong last_insert_id,
313 const char *message) = 0;
314 /**
315 Send eof message to the client.
316
317 @param server_status The server status
318 @param statement_warn_count Total number of warnings
319
320 @retval false The message was successfully sent
321 @retval true An error occurred and the messages wasn't sent properly
322 */
323 virtual bool send_eof(uint server_status, uint statement_warn_count) = 0;
324 /**
325 Send error message to the client.
326
327 @param sql_errno The error code to send
328 @param err_msg A pointer to the error message
329 @param sql_state SQL state
330
331 @retval false The message was successfully sent
332 @retval true An error occurred and the messages wasn't sent properly
333 */
334
335 virtual bool send_error(uint sql_errno, const char *err_msg,
336 const char *sql_state) = 0;
337
338 /**
339 Used for the classic protocol.
340 Makes the protocol send the messages/data to the client.
341
342 @retval false The flush was successful.
343 @retval true An error occurred.
344 */
345 virtual bool flush() = 0;
346
347 /**
348 Sends prepared statement's id and metadata to the client after prepare.
349
350 @param stmt_id Statement id.
351 @param column_count Number of columns.
352 @param param_count Number of parameters.
353 @param cond_count Number of conditions raised by the current statement.
354
355 @return Error status.
356 @retval false The send was successful.
357 @retval true An error occurred.
358 */
359 virtual bool store_ps_status(ulong stmt_id, uint column_count,
360 uint param_count, ulong cond_count) = 0;
361
362 /**
363 Sends the OUT-parameters to the client.
364
365 @param parameters List of PS/SP parameters (both input and output).
366 @param is_sql_prepare Used for the legacy protocol. If we're dealing with
367 sql prepare then text protocol will be used.
368
369 @return Error status.
370 @retval false Success.
371 @retval true Error.
372 */
373 virtual bool send_parameters(List<Item_param> *parameters,
374 bool is_sql_prepare) = 0;
375};
376
377#endif /* PROTOCOL_INCLUDED */
Definition: field.h:570
Dynamic parameters used as placeholders ('?') inside prepared statements.
Definition: item.h:4882
Definition: sql_list.h:494
Definition: protocol.h:33
bool store(uint32 from)
Definition: protocol.h:138
virtual bool flush()=0
Used for the classic protocol.
virtual bool store_field(const Field *field)=0
virtual bool store_ps_status(ulong stmt_id, uint column_count, uint param_count, ulong cond_count)=0
Sends prepared statement's id and metadata to the client after prepare.
virtual bool store_null()=0
virtual bool store_float(float from, uint32 decimals, uint32 zerofill)=0
virtual bool store_decimal(const my_decimal *, uint, uint)=0
virtual bool store_short(longlong from, uint32 zerofill)=0
virtual bool store_long(longlong from, uint32 zerofill)=0
@ SEND_DEFAULTS
Definition: protocol.h:114
@ SEND_NUM_ROWS
Definition: protocol.h:114
@ SEND_EOF
Definition: protocol.h:114
bool store(longlong from)
Definition: protocol.h:139
virtual bool store_string(const char *from, size_t length, const CHARSET_INFO *fromcs)=0
virtual ~Protocol()=default
virtual bool store_datetime(const MYSQL_TIME &time, uint precision)=0
virtual bool end_result_metadata()=0
Signals the client that the metadata sending is done.
Protocol * pop_protocol()
Remove the reference to the previous protocol and return it.
Definition: protocol.h:59
bool store_long(longlong from)
Definition: protocol.h:145
bool store(ulonglong from)
Definition: protocol.h:140
virtual void abort_row()=0
virtual uint get_compression_level()=0
Returns compression level.
void push_protocol(Protocol *protocol)
Set reference to "this" as the previous protocol on the protocol provided as argument.
Definition: protocol.h:72
virtual bool send_ok(uint server_status, uint statement_warn_count, ulonglong affected_rows, ulonglong last_insert_id, const char *message)=0
Send ok message to the client.
bool store_longlong(longlong from, bool unsigned_flag)
Definition: protocol.h:146
virtual char * get_compression_algorithm()=0
Returns compression algorithm name.
virtual int shutdown(bool server_shutdown=false)=0
Thread is being shut down, disconnect and free resources.
virtual int get_command(COM_DATA *com_data, enum_server_command *cmd)=0
Reads the command from the protocol and creates a command.
bool store_tiny(longlong from)
Definition: protocol.h:143
virtual void start_row()=0
Result set sending functions.
virtual bool end_row()=0
virtual bool send_error(uint sql_errno, const char *err_msg, const char *sql_state)=0
Send error message to the client.
bool store(String *str)
Definition: protocol.h:164
virtual bool get_compression()=0
Returns if the protocol is compressed or not.
Protocol * m_previous_protocol
Pointer to the Protocol below on the stack.
Definition: protocol.h:49
virtual int read_packet()=0
Read packet from client.
virtual uint get_rw_status()=0
Returns the read/writing status.
virtual bool store_double(double from, uint32 decimals, uint32 zerofill)=0
virtual enum enum_vio_type connection_type() const =0
virtual bool send_eof(uint server_status, uint statement_warn_count)=0
Send eof message to the client.
virtual bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs)=0
Prepares the server for metadata sending.
bool store(const char *from, const CHARSET_INFO *fromcs)
Send \0 end terminated string.
Definition: protocol.h:161
virtual bool connection_alive() const =0
Checks if the protocol's connection with the client is still alive.
virtual enum enum_protocol_type type() const =0
bool store(const LEX_STRING &s, const CHARSET_INFO *cs)
Definition: protocol.h:167
virtual bool send_field_metadata(Send_field *field, const CHARSET_INFO *charset)=0
Sends field metadata.
enum_protocol_type
Enum used by type() to specify the protocol type.
Definition: protocol.h:102
@ PROTOCOL_ERROR
Definition: protocol.h:107
@ PROTOCOL_PLUGIN
Definition: protocol.h:108
@ PROTOCOL_BINARY
Definition: protocol.h:105
@ PROTOCOL_LOCAL
Definition: protocol.h:106
@ PROTOCOL_TEXT
Definition: protocol.h:103
virtual bool store_time(const Time_val &time, uint precision)=0
bool store_short(longlong from)
Definition: protocol.h:144
virtual bool send_parameters(List< Item_param > *parameters, bool is_sql_prepare)=0
Sends the OUT-parameters to the client.
virtual bool store_date(const MYSQL_TIME &time)=0
bool store(int from)
Definition: protocol.h:137
virtual bool store_tiny(longlong from, uint32 zerofill)=0
virtual bool has_client_capability(unsigned long client_capability)=0
Checks if the client capabilities include the one specified as parameter.
virtual bool store_longlong(longlong from, bool unsigned_flag, uint32 zerofill)=0
virtual void end_partial_result_set()=0
virtual ulong get_client_capabilities()=0
Returns the client capabilities stored on the protocol.
Definition: field.h:4481
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:169
Time_val is a temporal type that represents only time.
Definition: my_temporal.h:55
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:97
Definition of COM_DATA to be used with the Command service as data input structure.
static int flags[50]
Definition: hp_test1.cc:40
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:48
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
Server classes for temporal handling (DATE, TIME, DATETIME)
Common definition between mysql server & client.
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1078
constexpr value_type zerofill
Definition: classic_protocol_constants.h:274
const std::string charset("charset")
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
Our own string classes, used pervasively throughout the executor.
Definition: m_ctype.h:421
Definition: mysql_lex_string.h:35
char * str
Definition: mysql_lex_string.h:36
size_t length
Definition: mysql_lex_string.h:37
Definition: mysql_time.h:82
Definition: com_data.h:104
Vio Lite.
enum_vio_type
Definition: violite.h:79