MySQL 8.4.0
Source Code Documentation
protocol_local.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_PROTOCOL_LOCAL_H
25#define SQL_PROTOCOL_LOCAL_H
26
27#include "sql/protocol.h"
29#include "sql/sql_list.h"
30
31class Ed_connection;
32
33/** One result set column. */
34
35struct Ed_column final : public LEX_STRING {
36 /** Implementation note: destructor for this class is never called. */
37};
38
39/** One result set record. */
40
41class Ed_row final {
42 public:
43 const Ed_column &operator[](const unsigned int column_index) const {
44 return *get_column(column_index);
45 }
46 const Ed_column *get_column(const unsigned int column_index) const {
47 assert(column_index < size());
48 return m_column_array + column_index;
49 }
50 size_t size() const { return m_column_count; }
51
52 Ed_row(Ed_column *column_array_arg, size_t column_count_arg)
53 : m_column_array(column_array_arg), m_column_count(column_count_arg) {}
54
55 private:
58};
59
60/**
61 Ed_result_set -- a container with result set rows.
62 @todo Implement support for result set metadata and
63 automatic type conversion.
64*/
65
66class Ed_result_set final {
67 public:
68 operator List<Ed_row> &() { return *m_rows; }
69 unsigned int size() const { return m_rows->elements; }
70 Ed_row *get_fields() { return m_fields; }
71
72 Ed_result_set(List<Ed_row> *rows_arg, Ed_row *fields, size_t column_count,
73 MEM_ROOT *mem_root_arg);
74
75 /** We don't call member destructors, they all are POD types. */
76 ~Ed_result_set() = default;
77
78 size_t get_field_count() const { return m_column_count; }
79
80 static void *operator new(size_t size, MEM_ROOT *mem_root,
81 const std::nothrow_t & = std::nothrow) noexcept {
82 return mem_root->Alloc(size);
83 }
84
85 static void operator delete(void *, size_t) noexcept {
86 // Does nothing because m_mem_root is deallocated in the destructor
87 }
88
89 static void operator delete(
90 void *, MEM_ROOT *, const std::nothrow_t &) noexcept { /* never called */
91 }
92
93 private:
94 Ed_result_set(const Ed_result_set &); /* not implemented */
95 Ed_result_set &operator=(Ed_result_set &); /* not implemented */
96 private:
102 friend class Ed_connection;
103};
104
105/**
106 Protocol_local: a helper class to intercept the result
107 of the data written to the network.
108
109 At the start of every result set, start_result_metadata allocates m_rset to
110 prepare for the results. The metadata is stored on m_current_row which will
111 be transferred to m_fields in end_result_metadata. The memory for the
112 metadata is allocated on m_rset_root.
113
114 Then, for every row of the result received, each of the fields is stored in
115 m_current_row. Then the row is moved to m_rset and m_current_row is cleared
116 to receive the next row. The memory for all the results are also stored in
117 m_rset_root.
118
119 Finally, at the end of the result set, a new instance of Ed_result_set is
120 created on m_rset_root and the result set (m_rset and m_fields) is moved into
121 this instance. The ownership of MEM_ROOT m_rset_root is also transferred to
122 this instance. So, at the end we have a fresh MEM_ROOT, cleared m_rset and
123 m_fields to accept the next result set.
124*/
125
126class Protocol_local final : public Protocol {
127 public:
128 Protocol_local(THD *thd, Ed_connection *ed_connection);
130
131 int read_packet() override;
132
133 int get_command(COM_DATA *com_data, enum_server_command *cmd) override;
134 ulong get_client_capabilities() override;
135 bool has_client_capability(unsigned long client_capability) override;
136 void end_partial_result_set() override;
137 int shutdown(bool server_shutdown = false) override;
138 bool connection_alive() const override;
139 void start_row() override;
140 bool end_row() override;
141 void abort_row() override {}
142 uint get_rw_status() override;
143 bool get_compression() override;
144
145 char *get_compression_algorithm() override;
146 uint get_compression_level() override;
147
148 bool start_result_metadata(uint num_cols, uint flags,
149 const CHARSET_INFO *resultcs) override;
150 bool end_result_metadata() override;
152 const CHARSET_INFO *charset) override;
153 bool flush() override { return true; }
154 bool send_parameters(List<Item_param> *, bool) override { return false; }
155 bool store_ps_status(ulong, uint, uint, ulong) override { return false; }
156
157 protected:
158 bool store_null() override;
159 bool store_tiny(longlong from, uint32) override;
160 bool store_short(longlong from, uint32) override;
161 bool store_long(longlong from, uint32) override;
162 bool store_longlong(longlong from, bool unsigned_flag, uint32) override;
163 bool store_decimal(const my_decimal *, uint, uint) override;
164 bool store_string(const char *from, size_t length,
165 const CHARSET_INFO *cs) override;
166 bool store_datetime(const MYSQL_TIME &time, uint precision) override;
167 bool store_date(const MYSQL_TIME &time) override;
168 bool store_time(const MYSQL_TIME &time, uint precision) override;
169 bool store_float(float value, uint32 decimals, uint32 zerofill) override;
170 bool store_double(double value, uint32 decimals, uint32 zerofill) override;
171 bool store_field(const Field *field) override;
172
173 enum enum_protocol_type type() const override { return PROTOCOL_LOCAL; }
174 enum enum_vio_type connection_type() const override { return VIO_TYPE_LOCAL; }
175
176 bool send_ok(uint server_status, uint statement_warn_count,
177 ulonglong affected_rows, ulonglong last_insert_id,
178 const char *message) override;
179
180 bool send_eof(uint server_status, uint statement_warn_count) override;
181 bool send_error(uint sql_errno, const char *err_msg,
182 const char *sqlstate) override;
183
184 private:
185 bool store_string(const char *str, size_t length, const CHARSET_INFO *src_cs,
186 const CHARSET_INFO *dst_cs);
187
188 bool store_column(const void *data, size_t length);
189 void opt_add_row_to_rset();
190
200};
201
202#endif
Definition: ed_connection.h:33
Ed_result_set – a container with result set rows.
Definition: protocol_local.h:66
Ed_result_set(const Ed_result_set &)
MEM_ROOT m_mem_root
Definition: protocol_local.h:97
Ed_result_set & operator=(Ed_result_set &)
Ed_row * m_fields
Definition: protocol_local.h:100
Ed_result_set(List< Ed_row > *rows_arg, Ed_row *fields, size_t column_count, MEM_ROOT *mem_root_arg)
Initialize an instance of Ed_result_set.
Definition: protocol_local.cc:44
Ed_result_set * m_next_rset
Definition: protocol_local.h:101
Ed_row * get_fields()
Definition: protocol_local.h:70
size_t get_field_count() const
Definition: protocol_local.h:78
List< Ed_row > * m_rows
Definition: protocol_local.h:99
size_t m_column_count
Definition: protocol_local.h:98
unsigned int size() const
Definition: protocol_local.h:69
~Ed_result_set()=default
We don't call member destructors, they all are POD types.
One result set record.
Definition: protocol_local.h:41
const Ed_column * get_column(const unsigned int column_index) const
Definition: protocol_local.h:46
Ed_row(Ed_column *column_array_arg, size_t column_count_arg)
Definition: protocol_local.h:52
size_t size() const
Definition: protocol_local.h:50
size_t m_column_count
Definition: protocol_local.h:57
const Ed_column & operator[](const unsigned int column_index) const
Definition: protocol_local.h:43
Ed_column * m_column_array
Definition: protocol_local.h:56
Definition: field.h:575
Definition: sql_list.h:467
Protocol_local: a helper class to intercept the result of the data written to the network.
Definition: protocol_local.h:126
bool send_parameters(List< Item_param > *, bool) override
Sends the OUT-parameters to the client.
Definition: protocol_local.h:154
bool connection_alive() const override
Checks if the protocol's connection with the client is still alive.
Definition: protocol_local.cc:290
size_t m_column_count
Definition: protocol_local.h:194
bool store_column(const void *data, size_t length)
A helper method to add any column to the current row in its binary form.
Definition: protocol_local.cc:103
bool has_client_capability(unsigned long client_capability) override
Checks if the client capabilities include the one specified as parameter.
Definition: protocol_local.cc:288
bool store_time(const MYSQL_TIME &time, uint precision) override
Store MYSQL_TIME (in binary format)
Definition: protocol_local.cc:209
ulong get_client_capabilities() override
Returns the client capabilities stored on the protocol.
Definition: protocol_local.cc:286
bool end_row() override
Add the current row to the result set.
Definition: protocol_local.cc:319
bool store_tiny(longlong from, uint32) override
Store a tiny int as is (1 byte) in a result set column.
Definition: protocol_local.cc:147
int get_command(COM_DATA *com_data, enum_server_command *cmd) override
Reads the command from the protocol and creates a command.
Definition: protocol_local.cc:360
bool start_result_metadata(uint num_cols, uint flags, const CHARSET_INFO *resultcs) override
Prepares the server for metadata sending.
Definition: protocol_local.cc:332
char * get_compression_algorithm() override
Returns compression algorithm name.
Definition: protocol_local.cc:356
bool store_longlong(longlong from, bool unsigned_flag, uint32) override
Store a "longlong" as is (8 bytes, host order) in a result set column.
Definition: protocol_local.cc:168
bool store_datetime(const MYSQL_TIME &time, uint precision) override
Definition: protocol_local.cc:197
Protocol_local(THD *thd, Ed_connection *ed_connection)
Definition: protocol_local.cc:60
Ed_connection * m_connection
Definition: protocol_local.h:191
bool flush() override
Used for the classic protocol.
Definition: protocol_local.h:153
bool get_compression() override
Returns if the protocol is compressed or not.
Definition: protocol_local.cc:354
uint get_compression_level() override
Returns compression level.
Definition: protocol_local.cc:358
bool store_date(const MYSQL_TIME &time) override
Store MYSQL_TIME (in binary format)
Definition: protocol_local.cc:203
bool store_field(const Field *field) override
Definition: protocol_local.cc:227
bool send_field_metadata(Send_field *field, const CHARSET_INFO *charset) override
Sends field metadata.
Definition: protocol_local.cc:348
~Protocol_local() override
Definition: protocol_local.h:129
MEM_ROOT m_rset_root
Definition: protocol_local.h:192
bool store_ps_status(ulong, uint, uint, ulong) override
Sends prepared statement's id and metadata to the client after prepare.
Definition: protocol_local.h:155
void end_partial_result_set() override
Definition: protocol_local.cc:292
bool store_long(longlong from, uint32) override
Store a "long" as is (4 bytes, host order) in a result set column.
Definition: protocol_local.cc:161
bool send_error(uint sql_errno, const char *err_msg, const char *sqlstate) override
Called to send an error to the client at the end of a statement.
Definition: protocol_local.cc:275
enum enum_protocol_type type() const override
Definition: protocol_local.h:173
bool store_string(const char *from, size_t length, const CHARSET_INFO *cs) override
Convert to cs_results and store a string.
Definition: protocol_local.cc:187
bool send_ok(uint server_status, uint statement_warn_count, ulonglong affected_rows, ulonglong last_insert_id, const char *message) override
Called for statements that don't have a result set, at statement end.
Definition: protocol_local.cc:233
bool store_double(double value, uint32 decimals, uint32 zerofill) override
Definition: protocol_local.cc:221
bool store_decimal(const my_decimal *, uint, uint) override
Store a decimal in string format in a result set column.
Definition: protocol_local.cc:175
Ed_column * m_current_row
Definition: protocol_local.h:195
void start_row() override
Called between two result set rows.
Definition: protocol_local.cc:304
Ed_column * m_current_column
Definition: protocol_local.h:196
List< Ed_row > * m_rset
Definition: protocol_local.h:193
bool send_eof(uint server_status, uint statement_warn_count) override
Called at the end of a result set.
Definition: protocol_local.cc:250
int shutdown(bool server_shutdown=false) override
Thread is being shut down, disconnect and free resources.
Definition: protocol_local.cc:294
bool store_null() override
Add a NULL column to the current row.
Definition: protocol_local.cc:87
enum enum_vio_type connection_type() const override
Definition: protocol_local.h:174
THD * m_thd
Definition: protocol_local.h:199
int read_packet() override
Read packet from client.
Definition: protocol_local.cc:284
bool store_short(longlong from, uint32) override
Store a short as is (2 bytes, host order) in a result set column.
Definition: protocol_local.cc:154
void abort_row() override
Definition: protocol_local.h:141
uint get_rw_status() override
Returns the read/writing status.
Definition: protocol_local.cc:330
bool store_float(float value, uint32 decimals, uint32 zerofill) override
Definition: protocol_local.cc:215
Ed_row * m_fields
Definition: protocol_local.h:197
bool end_result_metadata() override
Signals the client that the metadata sending is done.
Definition: protocol_local.cc:341
void opt_add_row_to_rset()
A helper function to add the current row to the current result set.
Definition: protocol_local.cc:75
bool m_send_metadata
Definition: protocol_local.h:198
Definition: protocol.h:33
enum_protocol_type
Enum used by type() to specify the protocol type.
Definition: protocol.h:101
@ PROTOCOL_LOCAL
Definition: protocol.h:105
Definition: field.h:4639
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:95
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
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
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1073
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
Definition: m_ctype.h:423
One result set column.
Definition: protocol_local.h:35
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
void Clear()
Deallocate all the RAM used.
Definition: my_alloc.cc:172
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:145
Definition: mysql_lex_string.h:35
Definition: mysql_time.h:82
Definition: com_data.h:104
enum_vio_type
Definition: violite.h:79
@ VIO_TYPE_LOCAL
Used internally by the prepared statements.
Definition: violite.h:107