MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
row_by_name.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018, 2025, 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, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the 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 Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef MYSQLSHDK_LIBS_DB_ROW_BY_NAME_H_
27#define MYSQLSHDK_LIBS_DB_ROW_BY_NAME_H_
28
29#include <map>
30#include <memory>
31#include <stdexcept>
32#include <string>
33#include <utility>
34#include <vector>
35
36#include "database/row.h"
37#include "database/row_copy.h"
39#include "utils/utils_string.h"
40
41namespace shcore {
42namespace polyglot {
43namespace database {
44
46 public:
47 explicit Field_names(const std::vector<Column> &metadata) {
48 for (const auto &col : metadata) add(col.get_column_label());
49 }
50
51 Field_names() = default;
52
53 Field_names(const Field_names &) = delete;
54 Field_names &operator=(const Field_names &) = delete;
55 Field_names(Field_names &&) noexcept = default;
56 Field_names &operator=(Field_names &&) noexcept = default;
57
58 void add(const std::string &name) {
59 if (has_field(name)) throw std::invalid_argument("duplicate field " + name);
60
61 uint32_t idx = static_cast<uint32_t>(m_fields.size());
62 m_fields[name] = idx;
63 }
64
65 uint32_t field_index(const std::string &name) {
66 auto it = m_fields.find(name);
67 if (it == m_fields.end())
68 throw std::invalid_argument("invalid field name " + name);
69 return it->second;
70 }
71
72 const std::string &field_name(uint32_t index) {
73 for (const auto &f : m_fields) {
74 if (f.second == index) return f.first;
75 }
76 throw std::invalid_argument("invalid field index " + std::to_string(index));
77 }
78
79 bool has_field(const std::string &field) const {
80 return m_fields.find(field) != m_fields.end();
81 }
82
83 private:
84 std::map<std::string, uint32_t, shcore::Case_insensitive_comparator> m_fields;
85};
86
88 public:
89 Row_ref_by_name() = default;
90 virtual ~Row_ref_by_name() = default;
91
92 Row_ref_by_name(std::shared_ptr<Field_names> field_names,
93 const IRow *row) noexcept
94 : _field_names(std::move(field_names)), _row_ref(row) {}
95
96 Row_ref_by_name(const Row_ref_by_name &) = default;
99 *this = std::move(t);
100 }
102 std::swap(_field_names, t._field_names);
103 std::swap(_row_ref, t._row_ref);
104 return *this;
105 }
106
107 const IRow &operator*() const { return *ref(); }
108
109 explicit operator bool() const {
110 return _field_names && (_row_ref != nullptr);
111 }
112
113 uint32_t num_fields() const { return ref()->num_fields(); }
114
115 Type get_type(const std::string &field) const {
116 try {
117 return ref()->get_type(field_index(field));
118 } catch (const bad_field &e) {
119 throw decorate_bad_field(field, e);
120 }
121 }
122
123 bool is_null(const std::string &field) const {
124 try {
125 return ref()->is_null(field_index(field));
126 } catch (const bad_field &e) {
127 throw decorate_bad_field(field, e);
128 }
129 }
130
131 std::string get_as_string(const std::string &field) const {
132 try {
133 return ref()->get_as_string(field_index(field));
134 } catch (const bad_field &e) {
135 throw decorate_bad_field(field, e);
136 }
137 }
138
139 std::string get_string(const std::string &field) const {
140 try {
141 return ref()->get_string(field_index(field));
142 } catch (const bad_field &e) {
143 throw decorate_bad_field(field, e);
144 }
145 }
146
147 std::string get_string(const std::string &field,
148 const std::string &default_if_null) const {
149 if (is_null(field)) return default_if_null;
150 try {
151 return ref()->get_string(field_index(field));
152 } catch (const bad_field &e) {
153 throw decorate_bad_field(field, e);
154 }
155 }
156
157 std::wstring get_wstring(const std::string &field) const {
158 try {
159 return ref()->get_wstring(field_index(field));
160 } catch (const bad_field &e) {
161 throw decorate_bad_field(field, e);
162 }
163 }
164
165 std::wstring get_wstring(const std::string &field,
166 const std::wstring &default_if_null) const {
167 if (is_null(field)) return default_if_null;
168 try {
169 return ref()->get_wstring(field_index(field));
170 } catch (const bad_field &e) {
171 throw decorate_bad_field(field, e);
172 }
173 }
174
175 int64_t get_int(const std::string &field) const {
176 try {
177 return ref()->get_int(field_index(field));
178 } catch (const bad_field &e) {
179 throw decorate_bad_field(field, e);
180 }
181 }
182
183 int64_t get_int(const std::string &field, int64_t default_if_null) const {
184 if (is_null(field)) return default_if_null;
185 try {
186 return ref()->get_int(field_index(field));
187 } catch (const bad_field &e) {
188 throw decorate_bad_field(field, e);
189 }
190 }
191
192 uint64_t get_uint(const std::string &field) const {
193 try {
194 return ref()->get_uint(field_index(field));
195 } catch (const bad_field &e) {
196 throw decorate_bad_field(field, e);
197 }
198 }
199
200 uint64_t get_uint(const std::string &field, uint64_t default_if_null) const {
201 if (is_null(field)) return default_if_null;
202 try {
203 return ref()->get_uint(field_index(field));
204 } catch (const bad_field &e) {
205 throw decorate_bad_field(field, e);
206 }
207 }
208
209 float get_float(const std::string &field) const {
210 try {
211 return ref()->get_float(field_index(field));
212 } catch (const bad_field &e) {
213 throw decorate_bad_field(field, e);
214 }
215 }
216
217 double get_double(const std::string &field) const {
218 try {
219 return ref()->get_double(field_index(field));
220 } catch (const bad_field &e) {
221 throw decorate_bad_field(field, e);
222 }
223 }
224
225 double get_double(const std::string &field, double default_if_null) const {
226 if (is_null(field)) return default_if_null;
227 try {
228 return ref()->get_double(field_index(field));
229 } catch (const bad_field &e) {
230 throw decorate_bad_field(field, e);
231 }
232 }
233
234 std::pair<const char *, size_t> get_string_data(
235 const std::string &field) const {
236 try {
237 return ref()->get_string_data(field_index(field));
238 } catch (const bad_field &e) {
239 throw decorate_bad_field(field, e);
240 }
241 }
242
243 std::tuple<uint64_t, int> get_bit(const std::string &field) const {
244 try {
245 return ref()->get_bit(field_index(field));
246 } catch (const bad_field &e) {
247 throw decorate_bad_field(field, e);
248 }
249 }
250
251 bool has_field(const std::string &field) const {
252 assert(_field_names);
253 return _field_names->has_field(field);
254 }
255
256 uint32_t field_index(const std::string &field) const {
257 assert(_field_names);
258 return _field_names->field_index(field);
259 }
260
261 const std::string &field_name(uint32_t i) const {
262 assert(_field_names);
263 return _field_names->field_name(i);
264 }
265
266 std::shared_ptr<Field_names> field_names() const { return _field_names; }
267
268 const IRow *ref() const {
269 if (!_row_ref) throw std::invalid_argument("invalid row reference");
270 return _row_ref;
271 }
272
273 protected:
274 std::shared_ptr<Field_names> _field_names;
275 const IRow *_row_ref = nullptr;
276
277 static bad_field decorate_bad_field(const std::string &field,
278 const bad_field &e) {
279 std::string msg = e.what();
280 msg.append(" (").append(field).append(")");
281 return bad_field(msg.c_str(), e.field);
282 }
283};
284
286 public:
288
289 Row_by_name(const std::shared_ptr<Field_names> &field_names, const IRow &row)
291
292 Row_by_name(const std::shared_ptr<Field_names> &field_names,
293 Row_copy &&row_copy) noexcept
295 _row_copy(std::move(row_copy)) {}
296
297 Row_by_name(const Row_by_name &) = delete;
299
301 *this = std::move(o);
302 }
304 if (this == &o) return *this;
305
306 std::swap(static_cast<Row_ref_by_name &>(*this),
307 static_cast<Row_ref_by_name &>(o));
308 std::swap(this->_row_copy, o._row_copy);
309 this->_row_ref = &_row_copy;
310 o._row_ref = &o._row_copy;
311 return *this;
312 }
313
314 explicit Row_by_name(const Row_ref_by_name &rrbn)
316 _row_copy(rrbn.ref() ? Row_copy(*rrbn.ref()) : Row_copy()) {}
317
318 private:
320};
321
322} // namespace database
323} // namespace polyglot
324} // namespace shcore
325
326#endif // MYSQLSHDK_LIBS_DB_ROW_BY_NAME_H_
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
Definition: jit_executor_db_interface.h:82
virtual std::string get_string(uint32_t index) const =0
virtual uint64_t get_uint(uint32_t index) const =0
virtual Type get_type(uint32_t index) const =0
virtual double get_double(uint32_t index) const =0
virtual std::pair< const char *, size_t > get_string_data(uint32_t index) const =0
virtual std::tuple< uint64_t, int > get_bit(uint32_t index) const =0
virtual bool is_null(uint32_t index) const =0
virtual std::wstring get_wstring(uint32_t index) const
Definition: jit_executor_db_interface.cc:33
virtual std::string get_as_string(uint32_t index) const =0
virtual float get_float(uint32_t index) const =0
virtual uint32_t num_fields() const =0
virtual int64_t get_int(uint32_t index) const =0
Definition: row_by_name.h:45
const std::string & field_name(uint32_t index)
Definition: row_by_name.h:72
uint32_t field_index(const std::string &name)
Definition: row_by_name.h:65
bool has_field(const std::string &field) const
Definition: row_by_name.h:79
Field_names(Field_names &&) noexcept=default
std::map< std::string, uint32_t, shcore::Case_insensitive_comparator > m_fields
Definition: row_by_name.h:84
void add(const std::string &name)
Definition: row_by_name.h:58
Field_names & operator=(const Field_names &)=delete
Field_names(const std::vector< Column > &metadata)
Definition: row_by_name.h:47
Field_names(const Field_names &)=delete
Definition: row_by_name.h:285
Row_by_name(const Row_ref_by_name &rrbn)
Definition: row_by_name.h:314
Row_by_name()
Definition: row_by_name.h:287
Row_by_name(Row_by_name &&o) noexcept
Definition: row_by_name.h:300
Row_by_name & operator=(Row_by_name &&o) noexcept
Definition: row_by_name.h:303
Row_by_name(const std::shared_ptr< Field_names > &field_names, Row_copy &&row_copy) noexcept
Definition: row_by_name.h:292
Row_copy _row_copy
Definition: row_by_name.h:319
Row_by_name(const Row_by_name &)=delete
Row_by_name & operator=(const Row_by_name &)=delete
Row_by_name(const std::shared_ptr< Field_names > &field_names, const IRow &row)
Definition: row_by_name.h:289
A self-contained Row object that owns its own storage, as opposed to mysql::Row or mysqlx::Row which ...
Definition: row_copy.h:124
int64_t get_int(const std::string &field, int64_t default_if_null) const
Definition: row_by_name.h:183
std::pair< const char *, size_t > get_string_data(const std::string &field) const
Definition: row_by_name.h:234
Row_ref_by_name(const Row_ref_by_name &)=default
std::wstring get_wstring(const std::string &field) const
Definition: row_by_name.h:157
std::shared_ptr< Field_names > field_names() const
Definition: row_by_name.h:266
std::tuple< uint64_t, int > get_bit(const std::string &field) const
Definition: row_by_name.h:243
Row_ref_by_name(Row_ref_by_name &&t) noexcept
Definition: row_by_name.h:98
Row_ref_by_name & operator=(const Row_ref_by_name &)=default
uint64_t get_uint(const std::string &field, uint64_t default_if_null) const
Definition: row_by_name.h:200
std::string get_as_string(const std::string &field) const
Definition: row_by_name.h:131
static bad_field decorate_bad_field(const std::string &field, const bad_field &e)
Definition: row_by_name.h:277
Row_ref_by_name(std::shared_ptr< Field_names > field_names, const IRow *row) noexcept
Definition: row_by_name.h:92
uint32_t field_index(const std::string &field) const
Definition: row_by_name.h:256
const IRow * ref() const
Definition: row_by_name.h:268
std::string get_string(const std::string &field, const std::string &default_if_null) const
Definition: row_by_name.h:147
std::shared_ptr< Field_names > _field_names
Definition: row_by_name.h:274
float get_float(const std::string &field) const
Definition: row_by_name.h:209
const std::string & field_name(uint32_t i) const
Definition: row_by_name.h:261
std::wstring get_wstring(const std::string &field, const std::wstring &default_if_null) const
Definition: row_by_name.h:165
Type get_type(const std::string &field) const
Definition: row_by_name.h:115
std::string get_string(const std::string &field) const
Definition: row_by_name.h:139
Row_ref_by_name & operator=(Row_ref_by_name &&t) noexcept
Definition: row_by_name.h:101
const IRow & operator*() const
Definition: row_by_name.h:107
bool has_field(const std::string &field) const
Definition: row_by_name.h:251
int64_t get_int(const std::string &field) const
Definition: row_by_name.h:175
double get_double(const std::string &field, double default_if_null) const
Definition: row_by_name.h:225
double get_double(const std::string &field) const
Definition: row_by_name.h:217
bool is_null(const std::string &field) const
Definition: row_by_name.h:123
const IRow * _row_ref
Definition: row_by_name.h:275
uint32_t num_fields() const
Definition: row_by_name.h:113
uint64_t get_uint(const std::string &field) const
Definition: row_by_name.h:192
uint32_t field
Definition: row.h:52
mrs::interface::RestHandler::HttpResult::Type Type
Definition: handler_content_file.cc:42
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:75
Definition: file_system_exceptions.h:34
Definition: gcs_xcom_synode.h:64
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:663
case opt name
Definition: sslopt-case.h:29