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