MySQL 8.0.40
Source Code Documentation
sql_resultset.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
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#ifndef SQL_RESULTSET_INCLUDE
25#define SQL_RESULTSET_INCLUDE
26
27#include <mysql/plugin.h>
28#include <stddef.h>
29#include <string>
30#include <vector>
31
32#include "my_inttypes.h"
33
34struct Field_type {
35 std::string db_name;
36 std::string table_name;
37 std::string org_table_name;
38 std::string col_name;
39 std::string org_col_name;
40 unsigned long length;
41 unsigned int charsetnr;
42 unsigned int flags;
43 unsigned int decimals;
45};
46
49 Field_value(const Field_value &other);
50 Field_value(const longlong &num, bool unsign = false);
51 Field_value(const decimal_t &decimal);
52 Field_value(const double num);
53 Field_value(const MYSQL_TIME &time);
54 Field_value(const char *str, size_t length);
55 Field_value &operator=(const Field_value &other);
57 union {
59 double v_double;
62 char *v_string;
66 bool has_ptr;
67
68 private:
69 void copy_string(const char *str, size_t length);
70};
71
73 public:
75 : current_row(0),
76 num_cols(0),
77 num_rows(0),
78 num_metarow(0),
81 m_warn_count(0),
84 m_sql_errno(0),
85 m_killed(false) {}
86
88
89 /* new row started for resultset */
90 void new_row();
91
92 /*
93 add new field to existing row
94
95 @param val Field_value to be stored in resulset
96 */
97 void new_field(Field_value *val);
98
99 /* truncate and free resultset rows and field values */
100 void clear();
101
102 /* move row index to next row */
103 bool next();
104
105 /*
106 move row index to particular row
107
108 @param row row position to set
109 */
110 void absolute(int row) { current_row = row; }
111
112 /* move row index to first row */
113 void first() { current_row = 0; }
114
115 /* move row index to last row */
116 void last() { current_row = num_rows > 0 ? num_rows - 1 : 0; }
117
118 /* increment number of rows in resulset */
120
121 /** Set Methods **/
122
123 /*
124 set number of rows in resulset
125
126 @param rows number of rows in resultset
127 */
128 void set_rows(uint rows) { num_rows = rows; }
129
130 /*
131 set number of cols in resulset
132
133 @param rows number of cols in resultset
134 */
135 void set_cols(uint cols) { num_cols = cols; }
136
137 /**
138 set resultset charset info
139
140 @param result_cs charset of resulset
141 */
142 void set_charset(const CHARSET_INFO *result_cs) { m_resultcs = result_cs; }
143
144 /**
145 set server status. check mysql_com for more details
146
147 @param server_status server status
148 */
149 void set_server_status(uint server_status) {
150 m_server_status = server_status;
151 }
152
153 /**
154 set count of warning issued during command execution
155
156 @param warn_count number of warning
157 */
158 void set_warn_count(uint warn_count) { m_warn_count = warn_count; }
159
160 /**
161 set rows affected due to last command execution
162
163 @param affected_rows number of rows affected due to last operation
164 */
165 void set_affected_rows(ulonglong affected_rows) {
166 m_affected_rows = affected_rows;
167 }
168
169 /**
170 set value of the AUTOINCREMENT column for the last INSERT
171
172 @param last_insert_id last inserted value in AUTOINCREMENT column
173 */
174 void set_last_insert_id(ulonglong last_insert_id) {
175 m_last_insert_id = last_insert_id;
176 }
177
178 /**
179 set client message
180
181 @param msg client message
182 */
183 void set_message(std::string msg) { m_message = msg; }
184
185 /**
186 set sql error number saved during error in
187 last command execution
188
189 @param sql_errno sql error number
190 */
192
193 /**
194 set sql error message saved during error in
195 last command execution
196
197 @param msg sql error message
198 */
199 void set_err_msg(std::string msg) { m_err_msg = msg; }
200
201 /**
202 set sql error state saved during error in
203 last command execution
204
205 @param state sql error state
206 */
207 void set_sqlstate(std::string state) { m_sqlstate = state; }
208
209 /* Session was shutdown while command was running */
210 void set_killed() { m_killed = true; /* purecov: inspected */ }
211
212 /** Get Methods **/
213
214 /*
215 get number of rows in resulset
216
217 @return number of rows in resultset
218 */
219 uint get_rows() { return num_rows; }
220
221 /*
222 get number of cols in resulset
223
224 @return number of cols in resultset
225 */
226 uint get_cols() { return num_cols; }
227
228 /**
229 get resultset charset info
230
231 @return charset info
232 */
234
235 /**
236 get server status. check mysql_com for more details
237
238 @return server status
239 */
241
242 /**
243 get count of warning issued during command execution
244
245 @return warn_count
246 */
248
249 /**
250 return rows affected dure to last command execution
251
252 @return affected_row
253 */
255
256 /**
257 get value of the AUTOINCREMENT column for the last INSERT
258
259 @return the sql error number
260 */
262
263 /**
264 get client message
265
266 @return message
267 */
268 std::string get_message() { return m_message; }
269
270 /** Getting error info **/
271 /**
272 get sql error number saved during error in last command execution
273
274 @return the sql error number
275 @retval 0 OK
276 @retval !=0 SQL Error Number
277 */
279
280 /**
281 get sql error message saved during error in last command execution
282
283 @return the sql error message
284 */
285 std::string err_msg() { return m_err_msg; /* purecov: inspected */ }
286
287 /**
288 get sql error state saved during error in last command execution
289
290 @return the sql error state
291 */
292 std::string sqlstate() { return m_sqlstate; }
293
294 /* get long field type column */
295 longlong getLong(uint columnIndex) {
296 return result_value[current_row][columnIndex]->value.v_long;
297 }
298
299 /* get decimal field type column */
300 decimal_t getDecimal(uint columnIndex) {
301 return result_value[current_row][columnIndex]->value.v_decimal;
302 }
303
304 /* get double field type column */
305 double getDouble(uint columnIndex) {
306 return result_value[current_row][columnIndex]->value.v_double;
307 }
308
309 /* get time field type column */
310 MYSQL_TIME getTime(uint columnIndex) {
311 return result_value[current_row][columnIndex]->value.v_time;
312 }
313
314 /* get string field type column */
315 char *getString(uint columnIndex) {
316 if (result_value[current_row][columnIndex] != nullptr)
317 return result_value[current_row][columnIndex]->value.v_string;
318 return const_cast<char *>("");
319 }
320
321 /* resultset metadata functions */
322
323 /* set metadata info */
325 result_meta.push_back(ftype);
326 ++num_metarow;
327 }
328
329 /* get database */
330 std::string get_database(uint rowIndex = 0) {
331 return result_meta[rowIndex].db_name;
332 }
333
334 /* get table alias */
335 std::string get_table(uint rowIndex = 0) {
336 return result_meta[rowIndex].table_name;
337 }
338
339 /* get original table */
340 std::string get_org_table(uint rowIndex = 0) {
341 return result_meta[rowIndex].org_table_name;
342 }
343
344 /* get column name alias */
345 std::string get_column_name(uint rowIndex = 0) {
346 return result_meta[rowIndex].col_name;
347 }
348
349 /* get original column name */
350 std::string get_org_column_name(uint rowIndex = 0) {
351 return result_meta[rowIndex].org_col_name;
352 }
353
354 /* get field width */
355 unsigned long get_length(uint rowIndex = 0) {
356 return result_meta[rowIndex].length;
357 }
358
359 /* get field charsetnr */
360 unsigned int get_charsetnr(uint rowIndex = 0) {
361 return result_meta[rowIndex].charsetnr;
362 }
363
364 /*
365 get field flag.
366 Check
367 https://dev.mysql.com/doc/refman/5.7/en/c-api-data-structures.html
368 for all flags
369 */
370 unsigned int get_flags(uint rowIndex = 0) {
371 return result_meta[rowIndex].flags;
372 }
373
374 /* get the number of decimals for numeric fields */
375 unsigned int get_decimals(uint rowIndex = 0) {
376 return result_meta[rowIndex].decimals;
377 }
378
379 /* get field type. Check enum enum_field_types for whole list */
381 return result_meta[rowIndex].type;
382 }
383
384 /*
385 get status whether session was shutdown while command was running
386
387 @return session shutdown status
388 @retval true session was stopped
389 @retval false session was not stopped
390 */
391 bool get_killed_status() { return m_killed; }
392
393 private:
394 /* resultset store */
395 std::vector<std::vector<Field_value *>> result_value;
396 /* metadata store */
397 std::vector<Field_type> result_meta;
398
399 int current_row; /* current row position */
400 uint num_cols; /* number of columns in resultset/metadata */
401 uint num_rows; /* number of rows in resultset */
402 uint num_metarow; /* number of rows in metadata */
403
404 const CHARSET_INFO *m_resultcs; /* result charset */
405
406 uint m_server_status; /* server status */
407 uint m_warn_count; /* warning count */
408
409 /* rows affected mostly useful for command like update */
411 ulonglong m_last_insert_id; /* last auto-increment column value */
412 std::string m_message; /* client message */
413
414 uint m_sql_errno; /* sql error number */
415 std::string m_err_msg; /* sql error message */
416 std::string m_sqlstate; /* sql error state */
417
418 bool m_killed; /* session killed status */
419};
420
421#endif // SQL_RESULTSET_INCLUDE
Definition: sql_resultset.h:72
void set_warn_count(uint warn_count)
set count of warning issued during command execution
Definition: sql_resultset.h:158
ulonglong get_affected_rows()
return rows affected dure to last command execution
Definition: sql_resultset.h:254
void set_last_insert_id(ulonglong last_insert_id)
set value of the AUTOINCREMENT column for the last INSERT
Definition: sql_resultset.h:174
std::vector< Field_type > result_meta
Definition: sql_resultset.h:397
void increment_rows()
Definition: sql_resultset.h:119
ulonglong m_last_insert_id
Definition: sql_resultset.h:411
void set_sql_errno(uint sql_errno)
set sql error number saved during error in last command execution
Definition: sql_resultset.h:191
ulonglong m_affected_rows
Definition: sql_resultset.h:410
const CHARSET_INFO * get_charset()
get resultset charset info
Definition: sql_resultset.h:233
char * getString(uint columnIndex)
Definition: sql_resultset.h:315
std::vector< std::vector< Field_value * > > result_value
Definition: sql_resultset.h:395
std::string sqlstate()
get sql error state saved during error in last command execution
Definition: sql_resultset.h:292
std::string get_column_name(uint rowIndex=0)
Definition: sql_resultset.h:345
std::string get_org_table(uint rowIndex=0)
Definition: sql_resultset.h:340
void new_field(Field_value *val)
Definition: sql_resultset.cc:134
~Sql_resultset()
Definition: sql_resultset.h:87
MYSQL_TIME getTime(uint columnIndex)
Definition: sql_resultset.h:310
uint get_cols()
Definition: sql_resultset.h:226
enum_field_types get_field_type(uint rowIndex=0)
Definition: sql_resultset.h:380
void first()
Definition: sql_resultset.h:113
std::string get_org_column_name(uint rowIndex=0)
Definition: sql_resultset.h:350
longlong getLong(uint columnIndex)
Definition: sql_resultset.h:295
std::string get_table(uint rowIndex=0)
Definition: sql_resultset.h:335
decimal_t getDecimal(uint columnIndex)
Definition: sql_resultset.h:300
bool get_killed_status()
Definition: sql_resultset.h:391
void set_metadata(Field_type ftype)
Definition: sql_resultset.h:324
void absolute(int row)
Definition: sql_resultset.h:110
std::string get_message()
get client message
Definition: sql_resultset.h:268
void set_rows(uint rows)
Set Methods.
Definition: sql_resultset.h:128
void set_killed()
Definition: sql_resultset.h:210
uint num_metarow
Definition: sql_resultset.h:402
uint num_cols
Definition: sql_resultset.h:400
int current_row
Definition: sql_resultset.h:399
unsigned long get_length(uint rowIndex=0)
Definition: sql_resultset.h:355
void set_err_msg(std::string msg)
set sql error message saved during error in last command execution
Definition: sql_resultset.h:199
std::string m_sqlstate
Definition: sql_resultset.h:416
Sql_resultset()
Definition: sql_resultset.h:74
uint sql_errno()
Getting error info.
Definition: sql_resultset.h:278
uint m_server_status
Definition: sql_resultset.h:406
const CHARSET_INFO * m_resultcs
Definition: sql_resultset.h:404
std::string m_err_msg
Definition: sql_resultset.h:415
std::string err_msg()
get sql error message saved during error in last command execution
Definition: sql_resultset.h:285
void set_sqlstate(std::string state)
set sql error state saved during error in last command execution
Definition: sql_resultset.h:207
void last()
Definition: sql_resultset.h:116
void set_charset(const CHARSET_INFO *result_cs)
set resultset charset info
Definition: sql_resultset.h:142
std::string m_message
Definition: sql_resultset.h:412
void clear()
resultset class
Definition: sql_resultset.cc:104
uint get_warn_count()
get count of warning issued during command execution
Definition: sql_resultset.h:247
uint get_rows()
Get Methods.
Definition: sql_resultset.h:219
void set_cols(uint cols)
Definition: sql_resultset.h:135
uint get_server_status()
get server status.
Definition: sql_resultset.h:240
uint m_sql_errno
Definition: sql_resultset.h:414
void set_message(std::string msg)
set client message
Definition: sql_resultset.h:183
void new_row()
Definition: sql_resultset.cc:130
uint num_rows
Definition: sql_resultset.h:401
unsigned int get_flags(uint rowIndex=0)
Definition: sql_resultset.h:370
unsigned int get_charsetnr(uint rowIndex=0)
Definition: sql_resultset.h:360
void set_affected_rows(ulonglong affected_rows)
set rows affected due to last command execution
Definition: sql_resultset.h:165
ulonglong get_last_insert_id()
get value of the AUTOINCREMENT column for the last INSERT
Definition: sql_resultset.h:261
double getDouble(uint columnIndex)
Definition: sql_resultset.h:305
unsigned int get_decimals(uint rowIndex=0)
Definition: sql_resultset.h:375
uint m_warn_count
Definition: sql_resultset.h:407
std::string get_database(uint rowIndex=0)
Definition: sql_resultset.h:330
bool next()
Definition: sql_resultset.cc:138
bool m_killed
Definition: sql_resultset.h:418
void set_server_status(uint server_status)
set server status.
Definition: sql_resultset.h:149
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
enum_field_types
Column types for MySQL.
Definition: field_types.h:53
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
long long int longlong
Definition: my_inttypes.h:55
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
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:385
Definition: sql_resultset.h:34
unsigned int charsetnr
Definition: sql_resultset.h:41
unsigned int flags
Definition: sql_resultset.h:42
unsigned long length
Definition: sql_resultset.h:40
unsigned int decimals
Definition: sql_resultset.h:43
enum_field_types type
Definition: sql_resultset.h:44
std::string db_name
Definition: sql_resultset.h:35
std::string org_col_name
Definition: sql_resultset.h:39
std::string org_table_name
Definition: sql_resultset.h:37
std::string table_name
Definition: sql_resultset.h:36
std::string col_name
Definition: sql_resultset.h:38
Definition: sql_resultset.h:47
bool is_unsigned
Definition: sql_resultset.h:65
decimal_t v_decimal
Definition: sql_resultset.h:60
Field_value & operator=(const Field_value &other)
Definition: sql_resultset.cc:41
MYSQL_TIME v_time
Definition: sql_resultset.h:61
~Field_value()
Definition: sql_resultset.cc:96
Field_value()
Definition: sql_resultset.cc:29
void copy_string(const char *str, size_t length)
Definition: sql_resultset.cc:79
char * v_string
Definition: sql_resultset.h:62
bool has_ptr
Definition: sql_resultset.h:66
union Field_value::@12 value
size_t v_string_length
Definition: sql_resultset.h:64
longlong v_long
Definition: sql_resultset.h:58
double v_double
Definition: sql_resultset.h:59
Definition: mysql_time.h:82
intg is the number of decimal digits (NOT number of decimal_digit_t's !) before the point frac is the...
Definition: decimal.h:52
unsigned int uint
Definition: uca9-dump.cc:75