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