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