MySQL 8.0.39
Source Code Documentation
mysql_query_runner.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2014, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_QUERY_RUNNER_INCLUDED
27#define MYSQL_QUERY_RUNNER_INCLUDED
28
29#include <algorithm>
30#include <atomic>
31#include <functional>
32#include <string>
33#include <vector>
34
36#include "my_inttypes.h"
37#include "mysql.h"
38
39namespace Mysql {
40namespace Tools {
41namespace Base {
42
43/**
44 Helper class to run SQL query on existing MySQL database server connection,
45 receive all data and all errors, warnings and notes returned during query
46 execution. All acquired information is passed to set of callbacks to make
47 data flows more customizable.
48 */
50 public:
51 class Row;
52
53 /**
54 Standard constructor based on MySQL connection.
55 */
56 explicit Mysql_query_runner(MYSQL *connection);
57 /**
58 Copy constructor.
59 */
61
63 /**
64 Adds new callback to be called on every result row of query.
65 If callback return value other than 0 then query execution, passing
66 current row to other callbacks and error messages processing, and
67 Mysql_query_runner::run_query() will return value returned from this
68 callback.
69 Callbacks are called in reverse order of addition, i.e. newest are first.
70 */
72 std::function<int64(const Row &)> *result_callback);
73 /**
74 Adds new callback to be called on every message after query execution,
75 this includes errors, warnings and other notes. Return value from callback
76 of 0 will lead to next handler being called, positive number return value
77 will cause Mysql_query_runner::run_query() will return immediately this
78 value and negative number will continue query execution and other messages
79 processing, but will not pass current message to rest of callbacks.
80 Callbacks are called in reverse order of addition, i.e. newest are first.
81
82 The optional cleanup function is called when the callback is deleted.
83 */
85 std::function<int64(const Message_data &)> *message_callback,
86 std::function<void()> cleanup_callback = nullptr);
87 /**
88 Runs specified query and processes result rows and messages to callbacks.
89 */
90 int64 run_query(std::string query);
91 /**
92 Runs specified query, fills result vector with processed result rows
93 and processes messages to callbacks.
94 */
95 int64 run_query_store(std::string query, std::vector<const Row *> *result);
96 /**
97 Runs specified query with result callback specified. Does not add specified
98 callback to list of callbacks, next queries will not process rows to this
99 callback.
100 */
101 int64 run_query(std::string query,
102 std::function<int64(const Row &)> *result_callback);
103 /**
104 Returns escaped copy of string to use in queries.
105 */
106 std::string escape_string(const std::string &original);
107 /**
108 Escapes specified input string and appends it escaped to destination
109 string.
110 */
111 void append_escape_string(std::string *destination_string,
112 const std::string &original);
113 /**
114 Escapes specified input string specified as characters buffer and its size,
115 and appends it escaped to destination string.
116 */
117 void append_escape_string(std::string *destination_string,
118 const char *original, size_t original_length);
119 /**
120 Converts to HEX specified input string specified as characters buffer and
121 its size, and appends it escaped to destination string.
122 */
123 static void append_hex_string(std::string *destination_string,
124 const char *original, size_t original_length);
125
126 /**
127 Empties memory used by result strings.
128 */
129 static void cleanup_result(const Row &result);
130 /**
131 Empties memory used by result strings.
132 */
133 static void cleanup_result(std::vector<const Row *> *result);
134
136
137 class Row {
138 public:
139 class Iterator;
140
141 Row(MYSQL_RES *mysql_result_info, unsigned int column_count, MYSQL_ROW row);
142 ~Row();
143 std::string operator[](std::size_t index) const;
144 void push_back(char *buff, std::size_t length);
145 const char *get_buffer(std::size_t index, std::size_t &length) const;
146 std::size_t size_of_element(std::size_t index) const;
147 bool is_value_null(std::size_t index) const;
148 std::size_t size() const;
149 Iterator begin() const;
150 Iterator end() const;
152
153 class Iterator {
154 public:
155 Iterator(const Row &row, std::size_t index);
156 bool is_value_null();
157 std::string operator*();
158 void operator++();
159 bool operator==(const Iterator &other);
160 bool operator!=(const Iterator &other);
161
162 private:
163 const Row &m_row;
164 std::size_t m_index;
165 };
166
167 private:
168 void reserve(std::size_t strings, std::size_t buffer_size);
169
170 // Represents table row as a string
171 char *m_buffer;
172 // Represents offsets to each column in m_buffer
173 std::vector<std::size_t> m_buffer_starts;
174 // Total buffer size
175 std::size_t m_buffer_capacity;
176 // Actual buffer size
177 std::size_t m_buffer_size;
179 };
180
181 private:
182 /**
183 Runs specified query and process result rows and messages to callbacks.
184 Does not check for multiple queries being executed in parallel.
185 */
186 int64 run_query_unguarded(std::string query);
187 /**
188 Creates error message from mysql_errno and mysql_error and passes it to
189 callbacks.
190 */
192 /**
193 Creates error message from mysql_errno and mysql_error and passes it to
194 callbacks.
195 */
197 /**
198 Returns parsed Message_type from given MySQL severity string.
199 */
200 Message_type get_message_type_from_severity(std::string severity);
201
203 public:
204 explicit Store_result_helper(std::vector<const Row *> *result);
205 std::function<int64(const Row &)> *get_result_callback();
206
207 private:
208 int64 result_callback(const Row &row);
209
210 std::vector<const Row *> *m_result;
211 };
212
213 std::vector<std::function<int64(const Row &)> *> m_result_callbacks;
214 std::vector<std::pair<std::function<int64(const Message_data &)> *,
215 std::function<void()>>>
217
218 /**
219 Indicates if there is query currently executed. Only one query can be
220 executed in specified time moment.
221 */
222 std::atomic<bool> *m_is_processing;
223
224 /**
225 Indicates if this is original runner or a copy. In case of original the
226 cleanup is performed on destruction.
227 */
229
231};
232
233} // namespace Base
234} // namespace Tools
235} // namespace Mysql
236
237#endif
Structure to represent message from server sent after executing query.
Definition: message_data.h:49
Definition: mysql_query_runner.h:153
std::size_t m_index
Definition: mysql_query_runner.h:164
const Row & m_row
Definition: mysql_query_runner.h:163
void operator++()
Definition: mysql_query_runner.cc:408
bool operator!=(const Iterator &other)
Definition: mysql_query_runner.cc:414
std::string operator*()
Definition: mysql_query_runner.cc:404
bool operator==(const Iterator &other)
Definition: mysql_query_runner.cc:410
bool is_value_null()
Definition: mysql_query_runner.cc:400
Iterator(const Row &row, std::size_t index)
Definition: mysql_query_runner.cc:397
Definition: mysql_query_runner.h:137
void push_back(char *buff, std::size_t length)
Definition: mysql_query_runner.cc:328
~Row()
Definition: mysql_query_runner.cc:318
std::size_t size() const
Definition: mysql_query_runner.cc:370
void reserve(std::size_t strings, std::size_t buffer_size)
Definition: mysql_query_runner.cc:374
std::size_t size_of_element(std::size_t index) const
Definition: mysql_query_runner.cc:360
MYSQL_RES * m_mysql_result_info
Definition: mysql_query_runner.h:178
Iterator end() const
Definition: mysql_query_runner.cc:389
Row(MYSQL_RES *mysql_result_info, unsigned int column_count, MYSQL_ROW row)
Definition: mysql_query_runner.cc:299
Iterator begin() const
Definition: mysql_query_runner.cc:385
std::size_t m_buffer_size
Definition: mysql_query_runner.h:177
char * m_buffer
Definition: mysql_query_runner.h:171
std::vector< std::size_t > m_buffer_starts
Definition: mysql_query_runner.h:173
std::string operator[](std::size_t index) const
Definition: mysql_query_runner.cc:322
bool is_value_null(std::size_t index) const
Definition: mysql_query_runner.cc:366
const char * get_buffer(std::size_t index, std::size_t &length) const
Definition: mysql_query_runner.cc:348
std::size_t m_buffer_capacity
Definition: mysql_query_runner.h:175
MYSQL_RES * get_mysql_result_info() const
Definition: mysql_query_runner.cc:393
int64 result_callback(const Row &row)
Definition: mysql_query_runner.cc:241
std::vector< const Row * > * m_result
Definition: mysql_query_runner.h:210
Store_result_helper(std::vector< const Row * > *result)
Definition: mysql_query_runner.cc:231
std::function< int64(const Row &)> * get_result_callback()
Definition: mysql_query_runner.cc:236
Helper class to run SQL query on existing MySQL database server connection, receive all data and all ...
Definition: mysql_query_runner.h:49
MYSQL * get_low_level_connection() const
Definition: mysql_query_runner.cc:83
bool m_is_original_runner
Indicates if this is original runner or a copy.
Definition: mysql_query_runner.h:228
int64 report_mysql_error()
Creates error message from mysql_errno and mysql_error and passes it to callbacks.
Definition: mysql_query_runner.cc:194
static void cleanup_result(const Row &result)
Empties memory used by result strings.
Definition: mysql_query_runner.cc:246
std::atomic< bool > * m_is_processing
Indicates if there is query currently executed.
Definition: mysql_query_runner.h:222
~Mysql_query_runner()
Definition: mysql_query_runner.cc:54
std::string escape_string(const std::string &original)
Returns escaped copy of string to use in queries.
Definition: mysql_query_runner.cc:250
Mysql_query_runner & add_message_callback(std::function< int64(const Message_data &)> *message_callback, std::function< void()> cleanup_callback=nullptr)
Adds new callback to be called on every message after query execution, this includes errors,...
Definition: mysql_query_runner.cc:76
int64 report_message(Message_data &message)
Creates error message from mysql_errno and mysql_error and passes it to callbacks.
Definition: mysql_query_runner.cc:200
std::vector< std::pair< std::function< int64(const Message_data &)> *, std::function< void()> > > m_message_callbacks
Definition: mysql_query_runner.h:216
MYSQL * m_connection
Definition: mysql_query_runner.h:230
void append_escape_string(std::string *destination_string, const std::string &original)
Escapes specified input string and appends it escaped to destination string.
Definition: mysql_query_runner.cc:258
Mysql_query_runner(MYSQL *connection)
Standard constructor based on MySQL connection.
Definition: mysql_query_runner.cc:42
Mysql_query_runner & add_result_callback(std::function< int64(const Row &)> *result_callback)
Adds new callback to be called on every result row of query.
Definition: mysql_query_runner.cc:70
int64 run_query(std::string query)
Runs specified query and processes result rows and messages to callbacks.
Definition: mysql_query_runner.cc:103
std::vector< std::function< int64(const Row &)> * > m_result_callbacks
Definition: mysql_query_runner.h:213
Message_type get_message_type_from_severity(std::string severity)
Returns parsed Message_type from given MySQL severity string.
Definition: mysql_query_runner.cc:213
int64 run_query_unguarded(std::string query)
Runs specified query and process result rows and messages to callbacks.
Definition: mysql_query_runner.cc:123
int64 run_query_store(std::string query, std::vector< const Row * > *result)
Runs specified query, fills result vector with processed result rows and processes messages to callba...
Definition: mysql_query_runner.cc:87
static void append_hex_string(std::string *destination_string, const char *original, size_t original_length)
Converts to HEX specified input string specified as characters buffer and its size,...
Definition: mysql_query_runner.cc:277
Some integer typedefs for easier portability.
int64_t int64
Definition: my_inttypes.h:68
static char * query
Definition: myisam_ftdump.cc:45
This file defines the client API to MySQL and also the ABI of the dynamically linked libmysqlclient.
char ** MYSQL_ROW
Definition: mysql.h:145
Message_type
Definition: message_data.h:38
Definition: abstract_connection_program.h:38
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
size_t buffer_size(const ConstBufferSequence &buffers) noexcept
Definition: buffer.h:315
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2874
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42
Definition: mysql.h:339
Definition: mysql.h:299
Definition: result.h:30