MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
object.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 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,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License 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
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_DATABASE_ENTRY_OBJECT_H_
27#define ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_DATABASE_ENTRY_OBJECT_H_
28
29#include <functional>
30#include <map>
31#include <memory>
32#include <optional>
33#include <string>
34#include <utility>
35#include <vector>
36
41
42namespace mrs {
43namespace database {
44namespace entry {
45
46class Table;
47
48enum class IdGenerationType {
49 NONE, // not auto-generated
50 AUTO_INCREMENT, // auto-increment by mysql
51 REVERSE_UUID // pre-generate as UUID_TO_BIN(UUID(), 1)
52};
53
54enum class KindType { PARAMETERS, RESULT };
55
56enum class ModeType { kNONE, kIN, kOUT, kIN_OUT };
57
58class ObjectField;
59class Column;
60class Object;
61
63 public:
65 std::shared_ptr<Column> field;
66};
67
69 public:
70 virtual ~ObjectField() = default;
71
72 ObjectField(const ObjectField &) = default;
73 ObjectField &operator=(const ObjectField &) = default;
74
76 std::string name;
77 int position = 0;
78 bool enabled = true; // include in the returned JSON object
79 bool allow_filtering = true;
80 bool allow_sorting = true;
81
82 protected:
83 ObjectField() = default;
84};
85
86class Column : public ObjectField {
87 public:
88 Column() = default;
89 Column(const Column &f) = default;
90
91 Column &operator=(const Column &) = default;
92
93 std::string column_name;
94 std::string datatype;
97 bool not_null = false;
98 bool is_primary = false;
99 bool is_unique = false;
100 bool is_generated = false;
101 bool is_foreign = false;
102 bool is_row_owner = false;
103 std::optional<bool> with_check;
104 std::optional<bool> with_update;
105 uint32_t srid{0};
106
107 bool is_auto_generated_id() const {
109 }
110};
111
112// tables that are joined to the root table or others
114 public:
117
119
120 using ColumnMapping = std::vector<std::pair<std::string, std::string>>;
121
122 std::shared_ptr<Table> ref_table;
124 bool to_many = false;
125 bool unnest = false;
126};
127
128class ParameterField : public Column {
129 public:
130 ParameterField() = default;
131 ParameterField(const ParameterField &f) = default;
132
134
136};
137
138class Table {
139 public:
140 virtual ~Table() = default;
141
142 std::string schema;
143 std::string table;
144
145 std::string table_alias;
146
147 std::vector<std::shared_ptr<ObjectField>> fields;
148 std::optional<OwnerUserField> user_ownership_field;
149
151 bool with_check_ = true;
152
153 inline bool with_insert() const {
154 return crud_operations & Operation::Values::valueCreate;
155 }
156
157 inline bool with_update() const {
158 return crud_operations & Operation::Values::valueUpdate;
159 }
160
161 inline bool with_update(const Column &column) const {
162 if (column.with_update.has_value()) return column.with_update.value();
163
164 return crud_operations & Operation::Values::valueUpdate;
165 }
166
167 inline bool with_update_any_column() const {
168 if (with_update()) return true;
169
170 bool updatable = false;
171
172 foreach_field<Column, bool>([&updatable](const Column &column) {
173 if (column.with_update.value_or(false)) {
174 updatable = true;
175 return false;
176 }
177 return true;
178 });
179
180 return updatable;
181 }
182
183 inline bool with_delete() const {
184 return crud_operations & Operation::Values::valueDelete;
185 }
186
187 inline bool with_check(const Column &column) const {
188 if (column.with_check.has_value()) return column.with_check.value();
189 // PKs always default to being checked and ignore table level CHECK
190 if (column.is_primary) return true;
191
192 return with_check_;
193 }
194
195 bool needs_etag() const;
196
197 // used to determine if object can be updated
198 bool unnests_to_value = false;
199
200 inline std::shared_ptr<ObjectField> get_field(std::string_view name) const {
201 for (const auto &f : fields) {
202 if (f->name == name) return f;
203 }
204 return {};
205 }
206
207 inline std::shared_ptr<ObjectField> get_field_or_throw(
208 std::string_view name) const {
209 for (const auto &f : fields) {
210 if (f->name == name) return f;
211 }
212 throw std::invalid_argument("Invalid object field reference " +
213 std::string(name));
214 }
215
216 inline std::shared_ptr<Column> get_column(
217 const entry::UniversalId &id) const {
218 for (const auto &f : fields) {
219 if (auto df = std::dynamic_pointer_cast<Column>(f); df) {
220 if (df->id == id) return df;
221 }
222 }
223 return {};
224 }
225
226 inline std::shared_ptr<Column> get_column(
227 std::string_view column_name) const {
228 for (const auto &f : fields) {
229 if (auto df = std::dynamic_pointer_cast<Column>(f); df) {
230 if (df->column_name == column_name) return df;
231 }
232 }
233 return {};
234 }
235
236 inline std::shared_ptr<Column> get_column_or_throw(
237 std::string_view column_name) const {
238 for (const auto &f : fields) {
239 if (auto df = std::dynamic_pointer_cast<Column>(f); df) {
240 if (df->column_name == column_name) return df;
241 }
242 }
243 throw std::invalid_argument("Invalid column reference " +
244 std::string(column_name));
245 }
246
247 inline std::string table_key() const { return schema + "." + table; }
248
249 std::shared_ptr<Column> get_column_with_field_name(
250 const std::string &name) const {
251 for (const auto &c : fields) {
252 if (c->name == name) return std::dynamic_pointer_cast<Column>(c);
253 }
254 return {};
255 }
256
257 std::vector<const Column *> primary_key() const {
258 std::vector<const Column *> cols;
259 foreach_field<Column, bool>([&cols](const Column &column) {
260 if (column.is_primary) cols.push_back(&column);
261 return false;
262 });
263 return cols;
264 }
265
267 return foreach_field<Column, const Column *>(
268 [](const Column &column) -> const Column * {
269 if (column.is_auto_generated_id()) return &column;
270 return nullptr;
271 });
272 }
273
275 return foreach_field<Column, const Column *>(
276 [](const Column &column) -> const Column * {
277 if (column.is_row_owner) return &column;
278 return nullptr;
279 });
280 }
281
282 template <typename T, typename R>
283 R foreach_field(std::function<R(T &)> fn) const {
284 for (auto &field : fields) {
285 if (auto df = std::dynamic_pointer_cast<T>(field)) {
286 if (auto r = fn(*df)) {
287 return r;
288 }
289 }
290 }
291 return R();
292 }
293
294 template <typename T, typename R>
295 R foreach_field(std::function<R(const T &)> fn) const {
296 for (const auto &field : fields) {
297 if (auto df = std::dynamic_pointer_cast<T>(field)) {
298 if (auto r = fn(*df)) {
299 return r;
300 }
301 }
302 }
303 return R();
304 }
305
306 template <typename R>
307 R foreach_field(std::function<R(const Column &)> column_fn,
308 std::function<R(const ForeignKeyReference &)> fk_fn) const {
309 for (const auto &field : fields) {
310 if (auto df = std::dynamic_pointer_cast<Column>(field)) {
311 if (auto r = column_fn(*df)) {
312 return r;
313 }
314 } else if (auto df =
315 std::dynamic_pointer_cast<ForeignKeyReference>(field)) {
316 if (auto r = fk_fn(*df)) {
317 return r;
318 }
319 }
320 }
321 return R();
322 }
323
325 const Table &parent) const {
326 auto fk =
327 parent.foreach_field<ForeignKeyReference, const ForeignKeyReference *>(
328 [this](const ForeignKeyReference &ref) {
329 if (ref.to_many && ref.ref_table.get() == this) {
330 return &ref;
331 }
332 return static_cast<const ForeignKeyReference *>(nullptr);
333 });
334
335 if (!fk) throw std::logic_error("FK to parent not found");
336
337 return *fk;
338 }
339
340 bool is_editable(bool &has_unnested_1n) const;
341
342 protected:
343 mutable std::optional<bool> needs_etag_;
344 std::string as_graphql(int depth, bool extended) const;
345};
346
347class Object : public Table {
348 public:
349 std::string name;
351
352 bool is_read_only() const;
353
354 std::string as_graphql(bool extended = false) const;
355};
356
358
359} // namespace entry
360} // namespace database
361} // namespace mrs
362
363#endif // ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_DATABASE_ENTRY_OBJECT_H_
Definition: object.h:86
bool is_foreign
Definition: object.h:101
std::string column_name
Definition: object.h:93
Column & operator=(const Column &)=default
bool not_null
Definition: object.h:97
bool is_generated
Definition: object.h:100
IdGenerationType id_generation
Definition: object.h:96
uint32_t srid
Definition: object.h:105
bool is_unique
Definition: object.h:99
std::string datatype
Definition: object.h:94
bool is_auto_generated_id() const
Definition: object.h:107
std::optional< bool > with_update
Definition: object.h:104
bool is_row_owner
Definition: object.h:102
bool is_primary
Definition: object.h:98
std::optional< bool > with_check
Definition: object.h:103
Column(const Column &f)=default
ColumnType type
Definition: object.h:95
bool to_many
Definition: object.h:124
ColumnMapping column_mapping
Definition: object.h:123
std::vector< std::pair< std::string, std::string > > ColumnMapping
Definition: object.h:120
std::shared_ptr< Table > ref_table
Definition: object.h:122
ForeignKeyReference & operator=(const ForeignKeyReference &)=default
bool unnest
Definition: object.h:125
ForeignKeyReference(const ForeignKeyReference &)=default
Definition: object.h:68
bool allow_filtering
Definition: object.h:79
int position
Definition: object.h:77
bool allow_sorting
Definition: object.h:80
entry::UniversalId id
Definition: object.h:75
bool enabled
Definition: object.h:78
ObjectField(const ObjectField &)=default
std::string name
Definition: object.h:76
ObjectField & operator=(const ObjectField &)=default
Definition: object.h:347
KindType kind
Definition: object.h:350
std::string as_graphql(bool extended=false) const
Definition: object.cc:182
std::string name
Definition: object.h:349
bool is_read_only() const
Definition: object.cc:175
std::shared_ptr< Column > field
Definition: object.h:65
UniversalId uid
Definition: object.h:64
Definition: object.h:128
ParameterField(const ParameterField &f)=default
ModeType mode
Definition: object.h:135
ParameterField & operator=(const ParameterField &)=default
Definition: object.h:138
std::shared_ptr< Column > get_column(std::string_view column_name) const
Definition: object.h:226
R foreach_field(std::function< R(T &)> fn) const
Definition: object.h:283
R foreach_field(std::function< R(const T &)> fn) const
Definition: object.h:295
std::vector< std::shared_ptr< ObjectField > > fields
Definition: object.h:147
virtual ~Table()=default
Operation::ValueType crud_operations
Definition: object.h:150
const ForeignKeyReference & get_reference_to_parent(const Table &parent) const
Definition: object.h:324
std::optional< OwnerUserField > user_ownership_field
Definition: object.h:148
bool with_update() const
Definition: object.h:157
bool with_insert() const
Definition: object.h:153
bool with_check_
Definition: object.h:151
std::vector< const Column * > primary_key() const
Definition: object.h:257
std::shared_ptr< Column > get_column_or_throw(std::string_view column_name) const
Definition: object.h:236
std::string table_key() const
Definition: object.h:247
std::string schema
Definition: object.h:142
bool with_check(const Column &column) const
Definition: object.h:187
std::string table_alias
Definition: object.h:145
bool needs_etag() const
Definition: object.cc:126
R foreach_field(std::function< R(const Column &)> column_fn, std::function< R(const ForeignKeyReference &)> fk_fn) const
Definition: object.h:307
bool unnests_to_value
Definition: object.h:198
std::shared_ptr< ObjectField > get_field(std::string_view name) const
Definition: object.h:200
std::shared_ptr< Column > get_column(const entry::UniversalId &id) const
Definition: object.h:216
bool with_update_any_column() const
Definition: object.h:167
std::string table
Definition: object.h:143
bool is_editable(bool &has_unnested_1n) const
Definition: object.cc:150
const Column * try_get_generated_id_column() const
Definition: object.h:266
std::shared_ptr< ObjectField > get_field_or_throw(std::string_view name) const
Definition: object.h:207
std::shared_ptr< Column > get_column_with_field_name(const std::string &name) const
Definition: object.h:249
bool with_update(const Column &column) const
Definition: object.h:161
const Column * try_get_row_ownership_column() const
Definition: object.h:274
std::optional< bool > needs_etag_
Definition: object.h:343
std::string as_graphql(int depth, bool extended) const
Definition: object.cc:32
bool with_delete() const
Definition: object.h:183
#define T
Definition: jit_executor_value.cc:373
MysqlCacheManager::Object Object
Definition: mysql_cache_manager.cc:101
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
ColumnType
Definition: column_type.h:48
IdGenerationType
Definition: object.h:48
ModeType
Definition: object.h:56
KindType
Definition: object.h:54
entry::ForeignKeyReference ForeignKeyReference
Definition: object_checksum.cc:47
entry::Table Table
Definition: object_checksum.cc:48
Definition: authorize_manager.h:48
mrs::database::entry::UniversalId UniversalId
Definition: universal_id.h:33
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
case opt name
Definition: sslopt-case.h:29
Definition: completion_hash.h:35
int ValueType
Definition: set_operation.h:34
Definition: universal_id.h:45