File: protocol_classic.h

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (274 lines) | stat: -rw-r--r-- 9,752 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#ifndef PROTOCOL_CLASSIC_INCLUDED
#define PROTOCOL_CLASSIC_INCLUDED

/* Copyright (c) 2002, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <stddef.h>
#include <sys/types.h>

#include "field_types.h"  // enum_field_types
#include "m_ctype.h"
#include "my_command.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "mysql_com.h"
#include "mysql_time.h"
#include "sql/protocol.h"  // Protocol
#include "violite.h"

class Item_param;
class Send_field;
class String;
class i_string;
class my_decimal;
template <class T>
class I_List;
template <class T>
class List;
union COM_DATA;
class THD;

class Protocol_classic : public Protocol {
 private:
  ulong m_client_capabilities;
  bool parse_packet(union COM_DATA *data, enum_server_command cmd);
  bool net_store_data_with_conversion(const uchar *from, size_t length,
                                      const CHARSET_INFO *fromcs,
                                      const CHARSET_INFO *tocs);

 protected:
  THD *m_thd;
  String *packet;
  String convert;
  uint field_pos;
  bool send_metadata;
#ifndef NDEBUG
  enum enum_field_types *field_types;
  uint count;
#endif
  uint field_count;
  uint sending_flags;
  ulong input_packet_length;
  uchar *input_raw_packet;
  const CHARSET_INFO *result_cs;

  bool send_ok(uint server_status, uint statement_warn_count,
               ulonglong affected_rows, ulonglong last_insert_id,
               const char *message) override;

  bool send_eof(uint server_status, uint statement_warn_count) override;

  bool send_error(uint sql_errno, const char *err_msg,
                  const char *sql_state) override;
  bool store_ps_status(ulong stmt_id, uint column_count, uint param_count,
                       ulong cond_count) override;

 public:
  bool bad_packet;
  Protocol_classic()
      : send_metadata(false), input_packet_length(0), bad_packet(true) {}
  Protocol_classic(THD *thd)
      : send_metadata(false),
        input_packet_length(0),
        input_raw_packet(nullptr),
        bad_packet(true) {
    init(thd);
  }
  void init(THD *thd_arg);
  bool store_field(const Field *field) final;
  bool store_string(const char *from, size_t length,
                    const CHARSET_INFO *cs) final;
  int read_packet() override;
  int get_command(COM_DATA *com_data, enum_server_command *cmd) override;
  /**
    Parses the passed parameters and creates a command

    @param com_data  out parameter
    @param cmd       in  parameter
    @param pkt       packet to be parsed
    @param length    size of the packet

    @retval false   ok
    @retval true    error
  */
  bool create_command(COM_DATA *com_data, enum_server_command cmd, uchar *pkt,
                      size_t length);
  bool flush() override;
  void end_partial_result_set() override;

  bool end_row() override;
  uint get_rw_status() override;
  bool get_compression() override;

  char *get_compression_algorithm() override;
  uint get_compression_level() override;

  bool start_result_metadata(uint num_cols, uint flags,
                             const CHARSET_INFO *resultcs) override;
  bool end_result_metadata() override;
  bool send_field_metadata(Send_field *field,
                           const CHARSET_INFO *item_charset) override;
  void abort_row() override {}

  /**
    Returns the type of the connection

    @return
      enum enum_vio_type
  */
  enum enum_vio_type connection_type() const override {
    const Vio *v = get_vio();
    return v ? vio_type(v) : NO_VIO_TYPE;
  }

  my_socket get_socket();
  // NET interaction functions
  /* Initialize NET */
  bool init_net(Vio *vio);
  void claim_memory_ownership(bool claim);
  /* Deinitialize NET */
  void end_net();
  /* Write data to NET buffer */
  bool write(const uchar *ptr, size_t len);
  /* Return last error from NET */
  uchar get_error();
  /* Set max allowed packet size */
  void set_max_packet_size(ulong max_packet_size);
  /* Deinitialize VIO */
  int shutdown(bool server_shutdown = false) override;
  /* Wipe NET with zeros */
  void wipe_net();
  /* Check whether VIO is healhty */
  bool connection_alive() const override;
  /* Returns the client capabilities */
  ulong get_client_capabilities() override { return m_client_capabilities; }
  /* Sets the client capabilities */
  void set_client_capabilities(ulong client_capabilities) {
    this->m_client_capabilities = client_capabilities;
  }
  /* Adds  client capability */
  void add_client_capability(ulong client_capability) {
    m_client_capabilities |= client_capability;
  }
  /* Removes a client capability*/
  void remove_client_capability(unsigned long capability) {
    m_client_capabilities &= ~capability;
  }
  /* Returns true if the client has the capability and false otherwise*/
  bool has_client_capability(unsigned long client_capability) override {
    return (bool)(m_client_capabilities & client_capability);
  }
  // TODO: temporary functions. Will be removed.
  // DON'T USE IN ANY NEW FEATURES.
  /* Return NET */
  NET *get_net();
  /* return VIO */
  Vio *get_vio();
  const Vio *get_vio() const;
  /* Set VIO */
  void set_vio(Vio *vio);
  /* Set packet number */
  void set_output_pkt_nr(uint pkt_nr);
  /* Return packet number */
  uint get_output_pkt_nr();
  /* Return packet string */
  String *get_output_packet();
  /* return packet length */
  ulong get_packet_length() { return input_packet_length; }
  /* Return raw packet buffer */
  uchar *get_raw_packet() { return input_raw_packet; }
  /* Set read timeout */
  virtual void set_read_timeout(ulong read_timeout,
                                bool on_full_packet = false);
  /* Set write timeout */
  virtual void set_write_timeout(ulong write_timeout);

  /**
   * Sets the character set expected by the client. This function is for unit
   * tests. It should usually be set by calling start_result_metadata().
   */
  void set_result_character_set(const CHARSET_INFO *charset) {
    result_cs = charset;
  }
};

/** Class used for the old (MySQL 4.0 protocol). */

class Protocol_text : public Protocol_classic {
 public:
  Protocol_text() = default;
  Protocol_text(THD *thd_arg) : Protocol_classic(thd_arg) {}
  bool store_null() override;
  bool store_tiny(longlong from, uint32 zerofill) override;
  bool store_short(longlong from, uint32 zerofill) override;
  bool store_long(longlong from, uint32 zerofill) override;
  bool store_longlong(longlong from, bool unsigned_flag,
                      uint32 zerofill) override;
  bool store_decimal(const my_decimal *, uint, uint) final;
  bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
  bool store_double(double from, uint32 decimals, uint32 zerofill) override;
  bool store_datetime(const MYSQL_TIME &time, uint precision) override;
  bool store_date(const MYSQL_TIME &time) override;
  bool store_time(const MYSQL_TIME &time, uint precision) override;
  void start_row() override;
  bool send_parameters(List<Item_param> *parameters, bool) override;

  enum enum_protocol_type type() const override { return PROTOCOL_TEXT; }
};

class Protocol_binary final : public Protocol_text {
 private:
  uint bit_fields;

 public:
  Protocol_binary() = default;
  Protocol_binary(THD *thd_arg) : Protocol_text(thd_arg) {}
  void start_row() override;
  bool store_null() override;
  bool store_tiny(longlong from, uint32 zerofill) override;
  bool store_short(longlong from, uint32 zerofill) override;
  bool store_long(longlong from, uint32 zerofill) override;
  bool store_longlong(longlong from, bool unsigned_flag,
                      uint32 zerofill) override;
  // Decimals are sent as text, also over the binary protocol.
  using Protocol_text::store_decimal;
  bool store_datetime(const MYSQL_TIME &time, uint precision) override;
  bool store_date(const MYSQL_TIME &time) override;
  bool store_time(const MYSQL_TIME &time, uint precision) override;
  bool store_float(float nr, uint32 decimals, uint32 zerofill) override;
  bool store_double(double from, uint32 decimals, uint32 zerofill) override;

  bool start_result_metadata(uint num_cols, uint flags,
                             const CHARSET_INFO *resultcs) override;
  bool send_parameters(List<Item_param> *parameters,
                       bool is_sql_prepare) override;

  enum enum_protocol_type type() const override { return PROTOCOL_BINARY; }
};

bool net_send_error(THD *thd, uint sql_errno, const char *err);
bool net_send_error(NET *net, uint sql_errno, const char *err);
uchar *net_store_data(uchar *to, const uchar *from, size_t length);
bool store(Protocol *prot, I_List<i_string> *str_list);
#endif /* PROTOCOL_CLASSIC_INCLUDED */