1 #ifndef PROTOCOL_INCLUDED 2 #define PROTOCOL_INCLUDED 3 4 /* Copyright (c) 2002, 2021, 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 "sql_error.h" 27 #include "my_decimal.h" /* my_decimal */ 28 29 #include "violite.h" /* SSL && enum_vio_type */ 30 31 #ifdef __cplusplus 32 class THD; 33 #define MYSQL_THD THD* 34 #else 35 #define MYSQL_THD void* 36 #endif 37 38 #include "mysql/com_data.h" 39 40 class Send_field; 41 class Proto_field; 42 43 44 class Protocol { 45 public: 46 /** 47 Read packet from client 48 49 @returns 50 -1 fatal error 51 0 ok 52 1 non-fatal error 53 */ 54 virtual int read_packet()= 0; 55 56 /** 57 Reads the command from the protocol and creates a command. 58 59 @param com_data out parameter 60 @param cmd out parameter 61 @param pkt packet to be parsed 62 @param length size of the packet 63 64 @returns 65 -1 fatal protcol error 66 0 ok 67 1 non-fatal protocol or parsing error 68 */ 69 virtual int get_command(COM_DATA *com_data, enum_server_command *cmd)= 0; 70 71 /** 72 Enum used by type() to specify the protocol type 73 */ 74 enum enum_protocol_type 75 { 76 /* 77 Before adding a new type, please make sure 78 there is enough storage for it in Query_cache_query_flags. 79 */ 80 PROTOCOL_TEXT= 0, // text Protocol type used mostly 81 // for the old (MySQL 4.0 protocol) 82 PROTOCOL_BINARY= 1, // binary protocol type 83 PROTOCOL_LOCAL= 2, // local protocol type(intercepts communication) 84 PROTOCOL_ERROR = 3, // error protocol instance 85 PROTOCOL_PLUGIN = 4 // pluggable protocol type 86 }; 87 88 /** 89 Flags available to alter the way the messages are sent to the client 90 */ 91 enum 92 { 93 SEND_NUM_ROWS= 1, 94 SEND_DEFAULTS= 2, 95 SEND_EOF= 4 96 }; 97 98 virtual enum enum_protocol_type type()= 0; 99 100 virtual enum enum_vio_type connection_type()= 0; 101 102 /* Data sending functions */ 103 virtual bool store_null()= 0; 104 virtual bool store_tiny(longlong from)= 0; 105 virtual bool store_short(longlong from)= 0; 106 virtual bool store_long(longlong from)= 0; 107 virtual bool store_longlong(longlong from, bool unsigned_flag)= 0; 108 virtual bool store_decimal(const my_decimal *, uint, uint)= 0; 109 virtual bool store(const char *from, size_t length, 110 const CHARSET_INFO *fromcs)= 0; 111 virtual bool store(float from, uint32 decimals, String *buffer)= 0; 112 virtual bool store(double from, uint32 decimals, String *buffer)= 0; 113 virtual bool store(MYSQL_TIME *time, uint precision)= 0; 114 virtual bool store_date(MYSQL_TIME *time)= 0; 115 virtual bool store_time(MYSQL_TIME *time, uint precision)= 0; 116 virtual bool store(Proto_field *field)= 0; 117 // Convenience wrappers store(int from)118 inline bool store(int from) 119 { return store_long((longlong) from); } store(uint32 from)120 inline bool store(uint32 from) 121 { return store_long((longlong) from); } store(longlong from)122 inline bool store(longlong from) 123 { return store_longlong(from, 0); } store(ulonglong from)124 inline bool store(ulonglong from) 125 { return store_longlong((longlong) from, 1); } 126 /** 127 Send \\0 end terminated string. 128 129 @param from NullS or \\0 terminated string 130 131 @note In most cases one should use store(from, length, cs) instead of 132 this function 133 134 @returns 135 false ok 136 true error 137 */ store(const char * from,const CHARSET_INFO * fromcs)138 inline bool store(const char *from, const CHARSET_INFO *fromcs) 139 { return from ? store(from, strlen(from), fromcs) : store_null(); } store(String * str)140 inline bool store(String *str) 141 { return store((char*) str->ptr(), str->length(), str->charset()); } store(const LEX_STRING & s,const CHARSET_INFO * cs)142 inline bool store(const LEX_STRING &s, const CHARSET_INFO *cs) 143 { return store(s.str, s.length, cs); } 144 145 /** 146 Returns the client capabilities stored on the protocol. 147 The available capabilites are defined in mysql_com.h 148 */ 149 virtual ulong get_client_capabilities()= 0; 150 /** 151 Checks if the client capabilities include the one 152 specified as parameter. 153 154 @returns 155 true if it includes the specified capability 156 false otherwise 157 */ 158 virtual bool has_client_capability(unsigned long client_capability)= 0; 159 160 /** 161 Checks if the protocol's connection with the client is still alive. 162 It should always return true unless the protocol closed the connection. 163 164 @returns 165 true if the connection is still alive 166 false otherwise 167 */ 168 virtual bool connection_alive()= 0; 169 170 /** 171 Result set sending functions 172 173 @details Server uses following schema to send result: 174 ... sending metadata ... 175 | start_result_metadata(...) 176 | start_row() 177 | send_field_metadata(...) 178 | end_row() 179 ... same for each field sent ... 180 | end_result_metadata(...) 181 | 182 ... sending result ... 183 | start_row(...) 184 | store_xxx(...) 185 ... store_xxx(..) is called for each field ... 186 | end_row(...) 187 ... same for each row, until all rows are sent ... 188 | send_ok/eof/error(...) 189 However, a protocol implementation might use different schema. For 190 example, Protocol_callback ignores start/end_row when metadata is being 191 sent. 192 */ 193 194 virtual void start_row()= 0; 195 virtual bool end_row()= 0; 196 virtual void abort_row()= 0; 197 virtual void end_partial_result_set()= 0; 198 199 /** 200 Thread is being shut down, disconnect and free resources 201 202 @param server_shutdown If false then this is normal thread shutdown. If 203 true then the server is shutting down. 204 */ 205 virtual int shutdown(bool server_shutdown= false)= 0; 206 207 /** 208 Returns the read/writing status 209 210 @return 211 @retval 1 Read 212 @retval 2 Write 213 @retval 0 Other(Idle, Killed) 214 */ 215 virtual uint get_rw_status()= 0; 216 /** 217 Returns if the protocol is compressed or not. 218 219 @return 220 @retval false Not compressed 221 @retval true Compressed 222 */ 223 virtual bool get_compression()= 0; 224 /** 225 Prepares the server for metadata sending. 226 Notifies the client that the metadata sending will start. 227 228 @param num_cols Number of columns that will be sent 229 @param flags Flags to alter the metadata sending 230 Can be any of the following: 231 SEND_NUM_ROWS, SEND_DEFAULTS, SEND_EOF 232 @param resultcs Charset to convert to 233 234 @return 235 @retval false Ok 236 @retval true An error occurred 237 */ 238 239 virtual bool start_result_metadata(uint num_cols, uint flags, 240 const CHARSET_INFO *resultcs)= 0; 241 /** 242 Sends field metadata. 243 244 @param field Field metadata to be send to the client 245 @param field Field to be send to the client 246 @param charset Field's charset: in case it is different 247 than the one used by the connection it will 248 be used to convert the value to 249 the connection's charset 250 251 @return 252 @retval false The metadata was successfully sent 253 @retval true An error occurred 254 */ 255 256 virtual bool send_field_metadata(Send_field *field, 257 const CHARSET_INFO *charset)= 0; 258 /** 259 Signals the client that the metadata sending is done. 260 Clears the server after sending the metadata. 261 262 @return 263 @retval false Ok 264 @retval true An error occurred 265 */ 266 virtual bool end_result_metadata()= 0; 267 268 /** 269 Send ok message to the client. 270 271 @param server_status The server status 272 @param statement_warn_count Total number of warnings 273 @param affected_rows Number of rows changed by statement 274 @param last_insert_id Last insert id (Auto_increment id for first 275 row if used) 276 @param message Message to send to the client 277 278 @return 279 @retval false The message was successfully sent 280 @retval true An error occurred and the messages wasn't sent properly 281 */ 282 virtual bool send_ok(uint server_status, uint statement_warn_count, 283 ulonglong affected_rows, ulonglong last_insert_id, 284 const char *message)= 0; 285 /** 286 Send eof message to the client. 287 288 @param server_status The server status 289 @param statement_warn_count Total number of warnings 290 291 @return 292 @retval false The message was successfully sent 293 @retval true An error occurred and the messages wasn't sent properly 294 */ 295 virtual bool send_eof(uint server_status, uint statement_warn_count)= 0; 296 /** 297 Send error message to the client. 298 299 @param sql_errno The error code to send 300 @param err A pointer to the error message 301 @param sqlstate SQL state 302 303 @return 304 @retval false The message was successfully sent 305 @retval true An error occurred and the messages wasn't sent properly 306 */ 307 308 virtual bool send_error(uint sql_errno, const char *err_msg, 309 const char *sql_state)= 0; 310 }; 311 312 #endif /* PROTOCOL_INCLUDED */ 313