-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmysql_results.h
More file actions
342 lines (261 loc) · 9.03 KB
/
Copy pathmysql_results.h
File metadata and controls
342 lines (261 loc) · 9.03 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the GNU General Public License Version 2.0 (GPLv2),
// A copy of the GPLv2 is included in this file.
//
//
#pragma once
#include "mysqlclient/mysql.h"
#include "trpc/util/log/logging.h"
#include "trpc/client/mysql/mysql_error_number.h"
namespace trpc::mysql {
constexpr size_t kDynamicBufferInitSize = 64;
struct MysqlResultsOption {
// The initial size of the buffer used to store variable-length data
// when fetching a row in BindType mode.
// In many real-world applications, 64 bytes is sufficient to store common variable-length data
size_t dynamic_buffer_init_size = kDynamicBufferInitSize;
};
///@brief Just specialize class MysqlResults
class OnlyExec {};
class NativeString {};
/// @brief Mode for `MysqlResults`
enum class MysqlResultsMode {
// Bind query result data to tuples.
BindType,
// For SQL which will not return a result set data. But can get the
// num of affected rows by GetAffectedRowNum()
OnlyExec,
// Return result data as vector of string_view.
NativeString,
};
template <typename... Args>
struct ResultSetMapper {
using type = std::vector<std::tuple<Args...>>;
static constexpr MysqlResultsMode mode = MysqlResultsMode::BindType;
};
template <>
struct ResultSetMapper<NativeString> {
using type = std::vector<std::vector<std::string_view>>;
static constexpr MysqlResultsMode mode = MysqlResultsMode::NativeString;
};
template <>
struct ResultSetMapper<OnlyExec> {
using type = std::vector<std::tuple<OnlyExec>>;
static constexpr MysqlResultsMode mode = MysqlResultsMode::OnlyExec;
};
///
///@brief A class used to store the results of a MySQL query executed by the MysqlExecutor class.
///
/// The class template parameter `Args...` is used to define the types of data stored in the result set.
///
///- If `Args...` is `OnlyExec`, the class is intended for operations
/// that execute without returning a result set (e.g., INSERT, UPDATE).
///
///- If `Args...` is `NativeString`, the class handles operations that
/// return a `vector<vector<string_view>>>` result set.
///
///- If `Args...` includes common data types (e.g., int, std::string),
/// the class handles operations that return a `vector<tuple<Args...>>` result set.
/// The template args need to match the query field. Notice that in this case, it will use
/// prepare statement to execute SQL. Pay attention to the bind type args. If the bind type args missmatch
/// the MySQL type in the table, it will be an undefined behaviour and will not raise an error message.
template <typename... Args>
class MysqlResults {
friend class MysqlExecutor;
public:
static constexpr MysqlResultsMode mode = ResultSetMapper<Args...>::mode;
using ResultSetType = typename ResultSetMapper<Args...>::type;
public:
MysqlResults();
explicit MysqlResults(const MysqlResultsOption& option);
MysqlResults(MysqlResults&& other) noexcept;
MysqlResults& operator=(MysqlResults&& other) noexcept;
MysqlResults(const MysqlResults& other) = delete;
MysqlResults& operator=(const MysqlResults&) = delete;
~MysqlResults();
auto& MutableResultSet();
const auto& ResultSet() const;
template <typename T>
bool GetResultSet(T& res);
const std::vector<std::vector<uint8_t>>& GetNullFlag();
const std::vector<std::string>& GetFieldsName() const;
const MysqlResultsOption& GetOption();
size_t GetAffectedRowNum() const;
bool IsValueNull(size_t row_index, size_t col_index) const;
bool OK() const;
void Clear();
const std::string& GetErrorMessage();
int GetErrorNumber() const;
private:
/// @brief Used for NativeString to the mysql_res_.
/// @note Must be called after Clear().
void SetRawMysqlRes(MYSQL_RES* res);
void SetFieldsName(MYSQL_RES* res);
size_t SetAffectedRows(size_t n_rows);
std::string& SetErrorMessage(const std::string& message);
std::string& SetErrorMessage(std::string&& message);
int SetErrorNumber(int error_number);
private:
MysqlResultsOption option_;
ResultSetType result_set_;
std::vector<std::string> fields_name_;
std::vector<std::vector<uint8_t>> null_flags_;
int error_number_{0};
std::string error_message_;
size_t affected_rows_;
bool has_value_;
/// @brief For NativeString, it will represent real data.
MYSQL_RES* mysql_res_{nullptr};
};
template <typename... Args>
const std::vector<std::string>& MysqlResults<Args...>::GetFieldsName() const {
return fields_name_;
}
template <typename... Args>
void MysqlResults<Args...>::SetFieldsName(MYSQL_RES* res) {
if (!res) return;
MYSQL_FIELD* fields_meta = mysql_fetch_fields(res);
unsigned long fields_num = mysql_num_fields(res);
for (unsigned long i = 0; i < fields_num; ++i) fields_name_.emplace_back(fields_meta[i].name);
}
template <typename... Args>
MysqlResults<Args...>& MysqlResults<Args...>::operator=(MysqlResults&& other) noexcept {
if (this != &other) {
option_ = std::move(other.option_);
result_set_ = std::move(other.result_set_);
fields_name_ = std::move(other.fields_name_);
null_flags_ = std::move(other.null_flags_);
error_message_ = std::move(other.error_message_);
affected_rows_ = other.affected_rows_;
has_value_ = other.has_value_;
mysql_res_ = other.mysql_res_;
other.mysql_res_ = nullptr;
}
return *this;
}
template <typename... Args>
MysqlResults<Args...>::MysqlResults(MysqlResults&& other) noexcept
: option_(std::move(other.option_)),
result_set_(std::move(other.result_set_)),
fields_name_(std::move(other.fields_name_)),
null_flags_(std::move(other.null_flags_)),
error_message_(std::move(other.error_message_)),
affected_rows_(other.affected_rows_),
has_value_(other.has_value_),
mysql_res_(other.mysql_res_) {
other.mysql_res_ = nullptr;
}
template <typename... Args>
void MysqlResults<Args...>::SetRawMysqlRes(MYSQL_RES* res) {
TRPC_ASSERT(mysql_res_ == nullptr);
mysql_res_ = res;
}
template <typename... Args>
MysqlResults<Args...>::~MysqlResults() {
if (mysql_res_) {
mysql_free_result(mysql_res_);
mysql_res_ = nullptr;
}
}
template <typename... Args>
std::string& MysqlResults<Args...>::SetErrorMessage(std::string&& message) {
error_message_ = std::move(message);
return error_message_;
}
template <typename... Args>
std::string& MysqlResults<Args...>::SetErrorMessage(const std::string& message) {
error_message_ = message;
return error_message_;
}
template <typename... Args>
const std::string& MysqlResults<Args...>::GetErrorMessage() {
return error_message_;
}
template <typename... Args>
int MysqlResults<Args...>::GetErrorNumber() const {
return error_number_;
}
template <typename... Args>
int MysqlResults<Args...>::SetErrorNumber(int error_number) {
return error_number_ = error_number;
}
template <typename... Args>
MysqlResults<Args...>::MysqlResults() : affected_rows_(0), has_value_(false), mysql_res_(nullptr) {
// dummy
}
template <typename... Args>
MysqlResults<Args...>::MysqlResults(const MysqlResultsOption& option) : MysqlResults() {
option_ = option;
}
template <typename... Args>
auto& MysqlResults<Args...>::MutableResultSet() {
return result_set_;
}
template <typename... Args>
const auto& MysqlResults<Args...>::ResultSet() const {
return result_set_;
}
template <typename... Args>
template <typename T>
bool MysqlResults<Args...>::GetResultSet(T& res) {
if (!has_value_) return false;
if constexpr (mode == MysqlResultsMode::NativeString) {
res.clear();
for (const auto& row : result_set_) {
res.emplace_back(row.begin(), row.end());
}
return true;
} else {
res = std::move(result_set_);
has_value_ = false;
return true;
}
}
template <typename... Args>
const std::vector<std::vector<uint8_t>>& MysqlResults<Args...>::GetNullFlag() {
return null_flags_;
}
template <typename... Args>
const MysqlResultsOption& MysqlResults<Args...>::GetOption() {
return option_;
}
template <typename... Args>
size_t MysqlResults<Args...>::GetAffectedRowNum() const {
return affected_rows_;
}
template <typename... Args>
size_t MysqlResults<Args...>::SetAffectedRows(size_t n_rows) {
affected_rows_ = n_rows;
return affected_rows_;
}
template <typename... Args>
bool MysqlResults<Args...>::IsValueNull(size_t row_index, size_t col_index) const {
if (null_flags_.empty()) return false;
if (row_index >= null_flags_.size() || col_index >= null_flags_[0].size()) return false;
return null_flags_[row_index][col_index];
}
template <typename... Args>
bool MysqlResults<Args...>::OK() const {
return error_number_ == 0;
}
template <typename... Args>
void MysqlResults<Args...>::Clear() {
null_flags_.clear();
error_message_.clear();
fields_name_.clear();
has_value_ = false;
affected_rows_ = 0;
MutableResultSet().clear();
if (mysql_res_) {
mysql_free_result(mysql_res_);
mysql_res_ = nullptr;
}
}
} // namespace trpc::mysql