MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
row_copy.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, 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// Persistent copy of a Row object
27
28#ifndef MYSQLSHDK_LIBS_DB_ROW_COPY_H_
29#define MYSQLSHDK_LIBS_DB_ROW_COPY_H_
30
31#include <cassert>
32#include <memory>
33#include <stdexcept>
34#include <string>
35#include <utility>
36#include <vector>
37
38#include "database/column.h"
39#include "database/row.h"
41
42namespace shcore {
43namespace polyglot {
44namespace database {
46
47class Mem_row : public IRow {
48 public:
49 Mem_row() = default;
50 Mem_row(const Mem_row &) = delete;
51 Mem_row &operator=(const Mem_row &) = delete;
52 Mem_row(Mem_row &&) = default;
53 Mem_row &operator=(Mem_row &&) = default;
54 ~Mem_row() override = default;
55
56 uint32_t num_fields() const override;
57
58 Type get_type(uint32_t index) const override;
59 bool is_null(uint32_t index) const override;
60 std::string get_as_string(uint32_t index) const override;
61
62 std::string get_string(uint32_t index) const override;
63 int64_t get_int(uint32_t index) const override;
64 uint64_t get_uint(uint32_t index) const override;
65 float get_float(uint32_t index) const override;
66 double get_double(uint32_t index) const override;
67 std::pair<const char *, size_t> get_string_data(
68 uint32_t index) const override;
69 void get_raw_data(uint32_t index, const char **out_data,
70 size_t *out_size) const override;
71 std::tuple<uint64_t, int> get_bit(uint32_t index) const override;
72
73 /** Inserts new field at specified offset. Field value is set to null.
74 *
75 * @param type field type.
76 * @param offset offset at which to insert (default - last column).
77 */
78 void add_field(Type type, uint32_t offset);
79 void add_field(Type type);
80
81 protected:
82 struct Field_data_ {
83 virtual ~Field_data_() = default;
84 };
85
86 template <typename T>
87 struct Field_data : public Field_data_ {
88 explicit Field_data(const T &v) : value(v) {}
89 explicit Field_data(T &&v) noexcept : value(std::move(v)) {}
91 };
92
93 template <typename T>
94 const T &get(size_t field) const {
95 assert(field < _data->fields.size());
96 if (field >= _data->fields.size())
97 throw std::invalid_argument("Attempt to access invalid field");
98 return static_cast<Field_data<T> *>(_data->fields[field].get())->value;
99 }
100
101 struct Data {
102 std::vector<Type> types;
103 std::vector<std::unique_ptr<Field_data_>> fields;
104
105 Data() = default;
106 explicit Data(std::vector<Type> types_)
107 : types(std::move(types_)), fields(types.size()) {}
108
109 explicit Data(const std::vector<Column> &columns) : fields(columns.size()) {
110 for (const auto &c : columns) types.push_back(c.get_type());
111 }
112 };
113 std::shared_ptr<Data> _data;
114 mutable std::string m_raw_data_cache;
115};
116
117/**
118 * A self-contained Row object that owns its own storage, as opposed to
119 * mysql::Row or mysqlx::Row which are references to data owned by the
120 * underlying client library.
121 *
122 * Can be created from the copy-constructor, from any instance of IRow.
123 */
124class Row_copy : public Mem_row {
125 public:
126 Row_copy() = default;
127 virtual ~Row_copy() = default;
128
129 Row_copy(const Row_copy &row) = delete;
130 Row_copy &operator=(const Row_copy &row) = delete;
131 Row_copy(Row_copy &&) = default;
133
134 explicit Row_copy(const IRow &row);
135};
136
137/**
138 * A self-contained Row object like Row_copy, but can be created or modified
139 * programmatically.
140 */
141class Mutable_row : public Mem_row {
142 public:
143 template <class T>
145 uint32_t index, T &&arg) {
146 if (_data->types[index] == Type::Integer)
147 _data->fields[index] = std::unique_ptr<Field_data_>(
148 new Field_data<int64_t>(std::forward<T>(arg)));
149 else if (_data->types[index] == Type::UInteger)
150 _data->fields[index] = std::unique_ptr<Field_data_>(
151 new Field_data<uint64_t>(std::forward<T>(arg)));
152 else
153 throw std::invalid_argument(
154 "Attempt to write integer value to non integer field");
155 }
156
157 template <class T>
159 uint32_t index, T &&arg) {
160 if (_data->types[index] != Type::Float &&
161 _data->types[index] != Type::Double)
162 throw std::invalid_argument(
163 "Attempt to write floating point number to not neither float or "
164 "double field.");
165 _data->fields[index] =
166 std::make_unique<Field_data<T>>(std::forward<T>(arg));
167 }
168
169 template <class T>
171 set_field(uint32_t index, T && /*arg*/) {
172 _data->fields[index] = nullptr;
173 }
174
175 template <class T>
178 set_field(uint32_t index, T &&arg) {
179 if (_data->types[index] >= Type::Integer &&
180 _data->types[index] <= Type::Double)
181 throw std::invalid_argument(
182 "Attempt to write arithmetic type to non arithmetic field");
183 _data->fields[index] = std::unique_ptr<Field_data_>(
184 new Field_data<std::string>(std::string(std::forward<T>(arg))));
185 }
186
187 private:
188 template <class T, class... Args>
189 void set_field(uint32_t start, T &&value, Args... args) {
190 set_field(start, std::forward<T>(value));
191 if (start < _data->fields.size())
192 set_field(start + 1, std::forward<Args>(args)...);
193 }
194
195 public:
196 template <class... Args>
197 void set_row_values(Args... args) {
198 constexpr int n = sizeof...(args);
199 if (n != _data->fields.size())
200 throw std::invalid_argument(
201 "The number of arguments does not match the row size");
202 set_field(0, std::forward<Args>(args)...);
203 }
204
205 explicit Mutable_row(const std::vector<Type> &types) {
206 _data = std::make_shared<Data>(types);
207 }
208
209 explicit Mutable_row(const std::vector<Column> &columns) {
210 _data = std::make_shared<Data>(columns); // only types used
211 }
212
213 template <class... Args>
214 Mutable_row(const std::vector<Type> &types, Args... args) {
215 _data = std::make_shared<Data>(types);
216 set_row_values(std::forward<Args>(args)...);
217 }
218
219 template <class... Args>
220 Mutable_row(const std::vector<Column> &columns, Args... args) {
221 _data = std::make_shared<Data>(columns);
222 set_row_values(std::forward<Args>(args)...);
223 }
224
225 virtual ~Mutable_row() = default;
226};
227
228} // namespace database
229} // namespace polyglot
230} // namespace shcore
231#endif // MYSQLSHDK_LIBS_DB_ROW_COPY_H_
Definition: jit_executor_db_interface.h:82
Definition: row_copy.h:47
Type get_type(uint32_t index) const override
Definition: row_copy.cc:138
int64_t get_int(uint32_t index) const override
Definition: row_copy.cc:189
double get_double(uint32_t index) const override
Definition: row_copy.cc:278
Mem_row & operator=(const Mem_row &)=delete
Mem_row(const Mem_row &)=delete
std::string get_as_string(uint32_t index) const override
Definition: row_copy.cc:147
const T & get(size_t field) const
Definition: row_copy.h:94
void get_raw_data(uint32_t index, const char **out_data, size_t *out_size) const override
Definition: row_copy.cc:246
Mem_row & operator=(Mem_row &&)=default
std::shared_ptr< Data > _data
Definition: row_copy.h:113
bool is_null(uint32_t index) const override
Definition: row_copy.cc:304
std::pair< const char *, size_t > get_string_data(uint32_t index) const override
Definition: row_copy.cc:238
void add_field(Type type, uint32_t offset)
Inserts new field at specified offset.
Definition: row_copy.cc:309
std::string m_raw_data_cache
Definition: row_copy.h:114
std::string get_string(uint32_t index) const override
Definition: row_copy.cc:232
uint32_t num_fields() const override
Definition: row_copy.cc:143
float get_float(uint32_t index) const override
Definition: row_copy.cc:258
std::tuple< uint64_t, int > get_bit(uint32_t index) const override
Definition: row_copy.cc:298
uint64_t get_uint(uint32_t index) const override
Definition: row_copy.cc:209
A self-contained Row object like Row_copy, but can be created or modified programmatically.
Definition: row_copy.h:141
void set_field(uint32_t start, T &&value, Args... args)
Definition: row_copy.h:189
Mutable_row(const std::vector< Type > &types)
Definition: row_copy.h:205
std::enable_if< std::is_same< T, std::nullptr_t >::value >::type set_field(uint32_t index, T &&)
Definition: row_copy.h:171
Mutable_row(const std::vector< Column > &columns, Args... args)
Definition: row_copy.h:220
std::enable_if<!std::is_arithmetic< T >::value &&!std::is_same< T, std::nullptr_t >::value >::type set_field(uint32_t index, T &&arg)
Definition: row_copy.h:178
void set_row_values(Args... args)
Definition: row_copy.h:197
Mutable_row(const std::vector< Type > &types, Args... args)
Definition: row_copy.h:214
Mutable_row(const std::vector< Column > &columns)
Definition: row_copy.h:209
std::enable_if< std::is_floating_point< T >::value >::type set_field(uint32_t index, T &&arg)
Definition: row_copy.h:158
std::enable_if< std::is_integral< T >::value >::type set_field(uint32_t index, T &&arg)
Definition: row_copy.h:144
A self-contained Row object that owns its own storage, as opposed to mysql::Row or mysqlx::Row which ...
Definition: row_copy.h:124
Row_copy & operator=(const Row_copy &row)=delete
Row_copy(const Row_copy &row)=delete
Row_copy & operator=(Row_copy &&)=default
mrs::interface::RestHandler::HttpResult::Type Type
Definition: handler_content_file.cc:42
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
#define T
Definition: jit_executor_value.cc:373
borrowable::binary::Float Float
Definition: classic_protocol_binary.h:316
borrowable::binary::Double Double
Definition: classic_protocol_binary.h:317
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:75
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
size_t size(const char *const c)
Definition: base64.h:46
Definition: file_system_exceptions.h:34
@ Integer
String values, UTF-8 encoding.
Definition: jit_executor_value.h:58
@ UInteger
64bit integer numbers
Definition: jit_executor_value.h:59
Definition: gcs_xcom_synode.h:64
required string type
Definition: replication_group_member_actions.proto:34
Data(std::vector< Type > types_)
Definition: row_copy.h:106
std::vector< Type > types
Definition: row_copy.h:102
std::vector< std::unique_ptr< Field_data_ > > fields
Definition: row_copy.h:103
Data(const std::vector< Column > &columns)
Definition: row_copy.h:109
Field_data(T &&v) noexcept
Definition: row_copy.h:89
Field_data(const T &v)
Definition: row_copy.h:88
int n
Definition: xcom_base.cc:509