Debian Codegraph @

unittest/gunit/xplugin/xpl/mysqlx_pb_wrapper.h

raw symbols SCIP
1 /* Copyright (c) 2015, 2026, 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 UNITTEST_GUNIT_XPLUGIN_XPL_MYSQLX_PB_WRAPPER_H_
25 #define UNITTEST_GUNIT_XPLUGIN_XPL_MYSQLX_PB_WRAPPER_H_
26
27 #include <cstddef>
28 #include <initializer_list>
29 #include <map>
30 #include <string>
31 #include <utility>
32 #include <vector>
33
34 #include "plugin/x/src/ngs/protocol/protocol_protobuf.h"
35
36 namespace xpl {
37 namespace test {
38
39 class Operator;
40 class Function_call;
41 class Object;
42 class Array;
43
44 template <typename T>
45 class Wrapper {
46 public:
47 using Base = T;
48 operator const T &() const { return m_base; }
49 const T &base() const { return m_base; }
50
51 protected:
52 T m_base;
53 };
54
55 template <typename T, typename B = typename T::Base>
56 class Repeated_field_list
57 : public Wrapper<::google::protobuf::RepeatedPtrField<B>> {
58 public:
59 Repeated_field_list() = default;
60 Repeated_field_list(const std::initializer_list<T> &list) {
61 for (const T &e : list) *this->m_base.Add() = e;
62 }
63 };
64
65 class Identifier : public Wrapper<::Mysqlx::Expr::Identifier> {
66 public:
67 Identifier(const std::string &name = "",
68 const std::string &schema_name = ""); // NOLINT(runtime/explicit)
69 };
70
71 class Document_path_item : public Wrapper<::Mysqlx::Expr::DocumentPathItem> {
72 public:
73 Document_path_item(const Base::Type type) { // NOLINT(runtime/explicit)
74 m_base.set_type(type);
75 }
76 Document_path_item(const int index) { // NOLINT(runtime/explicit)
77 m_base.set_type(Base::ARRAY_INDEX);
78 m_base.set_index(index);
79 }
80 Document_path_item(const char *value) { // NOLINT(runtime/explicit)
81 m_base.set_type(Base::MEMBER);
82 m_base.set_value(value);
83 }
84 };
85
86 using Document_path = Repeated_field_list<Document_path_item>;
87
88 class Column_identifier : public Wrapper<::Mysqlx::Expr::ColumnIdentifier> {
89 public:
90 Column_identifier(
91 const std::string &name = "", const std::string &table_name = "",
92 const std::string &schema_name = ""); // NOLINT(runtime/explicit)
93 Column_identifier(
94 const Document_path &path, const std::string &name = "",
95 const std::string &table_name = "",
96 const std::string &schema_name = ""); // NOLINT(runtime/explicit)
97 };
98
99 class Scalar : public Wrapper<::Mysqlx::Datatypes::Scalar> {
100 public:
101 struct Null {};
102
103 struct String : public Wrapper<::Mysqlx::Datatypes::Scalar_String> {
104 String(const std::string &value); // NOLINT(runtime/explicit)
105 };
106
107 struct Octets : public Wrapper<::Mysqlx::Datatypes::Scalar_Octets> {
108 enum class Content_type {
109 k_plain = 0x0000, // default value; general use of octets
110 k_geometry = Mysqlx::Resultset::GEOMETRY,
111 k_json = Mysqlx::Resultset::JSON,
112 k_xml = Mysqlx::Resultset::XML
113 };
114
115 Octets(const std::string &value,
116 const Content_type type =
117 Content_type::k_plain); // NOLINT(runtime/explicit)
118 };
119
120 Scalar() = default;
121 Scalar(const int value); // NOLINT(runtime/explicit)
122 Scalar(const unsigned int value); // NOLINT(runtime/explicit)
123 Scalar(const bool value); // NOLINT(runtime/explicit)
124 Scalar(const float value); // NOLINT(runtime/explicit)
125 Scalar(const double value); // NOLINT(runtime/explicit)
126 Scalar(const Scalar::Octets &value); // NOLINT(runtime/explicit)
127 Scalar(const char *value); // NOLINT(runtime/explicit)
128 Scalar(const Scalar::String &value); // NOLINT(runtime/explicit)
129 Scalar(Null value); // NOLINT(runtime/explicit)
130 };
131
132 class Any : public Wrapper<::Mysqlx::Datatypes::Any> {
133 public:
134 class Array;
135
136 class Object : public Wrapper<::Mysqlx::Datatypes::Object> {
137 public:
138 struct Fld;
139 Object() = default;
140 Object(const std::initializer_list<Fld> &list);
141 Object(const std::string &key, Any *value);
142 };
143
144 Any() = default;
145 template <typename T>
146 Any(const T &v) // NOLINT(runtime/explicit)
147 : Any(Scalar(v)) {}
148 Any(const Scalar &scalar); // NOLINT(runtime/explicit)
149 Any(const Object &obj); // NOLINT(runtime/explicit)
150 Any(const Array &array); // NOLINT(runtime/explicit)
151 };
152
153 struct Any::Object::Fld {
154 std::string key;
155 Any value;
156 };
157
158 class Any::Array : public Wrapper<::Mysqlx::Datatypes::Array> {
159 public:
160 Array() = default;
161 Array(const std::initializer_list<Any> &list) {
162 for (const Any &e : list) m_base.add_value()->CopyFrom(e);
163 }
164 };
165
166 class Placeholder {
167 public:
168 explicit Placeholder(const ::google::protobuf::uint32 v) : value(v) {}
169 const ::google::protobuf::uint32 value;
170 };
171
172 class Variable {
173 public:
174 Variable(const std::string &name) // NOLINT(runtime/explicit)
175 : value(name) {}
176 const std::string value;
177 };
178
179 class Expr : public Wrapper<::Mysqlx::Expr::Expr> {
180 public:
181 Expr() = default;
182 template <typename T>
183 Expr(const T &value) // NOLINT(runtime/explicit)
184 : Expr(Scalar(value)) {}
185 Expr(const Scalar &value); // NOLINT(runtime/explicit)
186 Expr(const Operator &oper); // NOLINT(runtime/explicit)
187 Expr(const Identifier &ident); // NOLINT(runtime/explicit)
188 Expr(const Function_call &func); // NOLINT(runtime/explicit)
189 Expr(const Column_identifier &id); // NOLINT(runtime/explicit)
190 Expr(const Object &obj); // NOLINT(runtime/explicit)
191 Expr(const Array &arr); // NOLINT(runtime/explicit)
192 Expr(const Placeholder &ph); // NOLINT(runtime/explicit)
193 Expr(const Variable &var); // NOLINT(runtime/explicit)
194 };
195
196 class Operator : public Wrapper<::Mysqlx::Expr::Operator> {
197 public:
198 Operator(const std::string &name) { // NOLINT(runtime/explicit)
199 m_base.set_name(name);
200 }
201
202 template <typename... T>
203 Operator(const std::string &name,
204 T &&...params) { // NOLINT(runtime/explicit)
205 m_base.set_name(name);
206 add_param(std::forward<T>(params)...);
207 }
208
209 template <typename... T>
210 void add_param(const Expr &first, T &&...rest) {
211 add_param(first);
212 add_param(std::forward<T>(rest)...);
213 }
214
215 void add_param(const Expr &value) { *m_base.add_param() = value; }
216 };
217
218 class Function_call : public Wrapper<::Mysqlx::Expr::FunctionCall> {
219 public:
220 Function_call(const std::string &name) { // NOLINT(runtime/explicit)
221 m_base.mutable_name()->CopyFrom(Identifier(name));
222 }
223
224 template <typename... T>
225 Function_call(const Identifier &name,
226 T &&...params) { // NOLINT(runtime/explicit)
227 m_base.mutable_name()->CopyFrom(name);
228 add_param(std::forward<T>(params)...);
229 }
230
231 template <typename... T>
232 Function_call(const std::string &name,
233 T &&...params) { // NOLINT(runtime/explicit)
234 m_base.mutable_name()->CopyFrom(Identifier(name));
235 add_param(std::forward<T>(params)...);
236 }
237
238 private:
239 template <typename... T>
240 void add_param(const Expr &first, T &&...rest) {
241 add_param(first);
242 add_param(std::forward<T>(rest)...);
243 }
244
245 void add_param(const Expr &value) { *m_base.add_param() = value; }
246 };
247
248 class Object : public Wrapper<::Mysqlx::Expr::Object> {
249 public:
250 struct Fld {
251 std::string key;
252 Expr value;
253 };
254
255 Object() = default;
256 Object(const std::string &key, Expr *value);
257 Object(const std::initializer_list<Fld> &list);
258 };
259
260 class Array : public Wrapper<::Mysqlx::Expr::Array> {
261 public:
262 Array() = default;
263 Array(const std::initializer_list<Expr> &list) {
264 for (const auto &e : list) {
265 *m_base.mutable_value()->Add() = e;
266 }
267 }
268 };
269
270 class Column : public Wrapper<::Mysqlx::Crud::Column> {
271 public:
272 Column(const std::string &name,
273 const std::string &alias = ""); // NOLINT(runtime/explicit)
274 Column(const Document_path &path, const std::string &name = "",
275 const std::string &alias = ""); // NOLINT(runtime/explicit)
276 };
277
278 class Collection : public Wrapper<::Mysqlx::Crud::Collection> {
279 public:
280 Collection(const std::string &name,
281 const std::string &schema = ""); // NOLINT(runtime/explicit)
282 };
283
284 using Data_model = ::Mysqlx::Crud::DataModel;
285
286 class Projection : public Wrapper<::Mysqlx::Crud::Projection> {
287 public:
288 Projection(const Expr &source,
289 const std::string &alias = ""); // NOLINT(runtime/explicit)
290 };
291
292 class Order : public Wrapper<::Mysqlx::Crud::Order> {
293 public:
294 using Direction = Base::Direction;
295 Order(const Expr &expr,
296 const Direction dir = Base::Order::ASC); // NOLINT(runtime/explicit)
297 };
298
299 class Limit : public Wrapper<::Mysqlx::Crud::Limit> {
300 public:
301 Limit(const uint64_t row_count = 0,
302 const uint64_t offset = 0); // NOLINT(runtime/explicit)
303 };
304
305 class Limit_expr : public Wrapper<::Mysqlx::Crud::LimitExpr> {
306 public:
307 Limit_expr() = default;
308 Limit_expr(const Expr &row_count); // NOLINT(runtime/explicit)
309 Limit_expr(const Expr &row_count,
310 const Expr &offset); // NOLINT(runtime/explicit)
311 };
312
313 class Update_operation : public Wrapper<::Mysqlx::Crud::UpdateOperation> {
314 public:
315 using Update_type = Base::UpdateType;
316 Update_operation(const Update_type &update_type,
317 const Column_identifier &source, const Expr &value);
318 Update_operation(const Update_type &update_type,
319 const Column_identifier &source);
320 Update_operation(const Update_type &update_type, const Document_path &source,
321 const Expr &value);
322 Update_operation(const Update_type &update_type, const Document_path &source);
323 };
324
325 using Filter = Expr;
326 using Grouping = Expr;
327 using Grouping_criteria = Expr;
328 using Column_projection_list = Repeated_field_list<Column>;
329 using Grouping_list = Repeated_field_list<Grouping>;
330 using Expression_list = Repeated_field_list<Scalar>;
331 using Field_list = Repeated_field_list<Expr>;
332 using Order_list = Repeated_field_list<Order>;
333 using Projection_list = Repeated_field_list<Projection>;
334 using Value_list = Repeated_field_list<Expr>;
335 using Operation_list = Repeated_field_list<Update_operation>;
336 using Any_list = Repeated_field_list<Any>;
337
338 class Row_list : public Wrapper<::google::protobuf::RepeatedPtrField<
339 ::Mysqlx::Crud::Insert_TypedRow>> {
340 public:
341 Row_list() = default;
342 Row_list(const std::initializer_list<Value_list> &list) {
343 for (const auto &v : list) *this->m_base.Add()->mutable_field() = v;
344 }
345 Base::size_type size() const { return m_base.size(); }
346 };
347
348 class Find : public Wrapper<::Mysqlx::Crud::Find> {
349 public:
350 explicit Find(const Collection &coll,
351 const Data_model dm = ::Mysqlx::Crud::DOCUMENT) {
352 *m_base.mutable_collection() = coll;
353 m_base.set_data_model(dm);
354 }
355 Find &args(const Expression_list &arg) {
356 *m_base.mutable_args() = arg;
357 return *this;
358 }
359 Find &projection(const Projection_list &pl) {
360 *m_base.mutable_projection() = pl;
361 return *this;
362 }
363 Find &projection(const Projection &pl) {
364 *m_base.mutable_projection()->Add() = pl;
365 return *this;
366 }
367 Find &criteria(const Filter &ft) {
368 *m_base.mutable_criteria() = ft;
369 return *this;
370 }
371 Find &order(const Order_list &ol) {
372 *m_base.mutable_order() = ol;
373 return *this;
374 }
375 Find &order(const Order &ol) {
376 *m_base.mutable_order()->Add() = ol;
377 return *this;
378 }
379 Find &limit(const Limit &li) {
380 *m_base.mutable_limit() = li;
381 return *this;
382 }
383 Find &grouping(const Grouping_list &gl) {
384 *m_base.mutable_grouping() = gl;
385 return *this;
386 }
387 Find &grouping(const Grouping &gl) {
388 *m_base.mutable_grouping()->Add() = gl;
389 return *this;
390 }
391 Find &grouping_criteria(const Grouping_criteria &gc) {
392 *m_base.mutable_grouping_criteria() = gc;
393 return *this;
394 }
395 Find &locking(const Base::RowLock &rl) {
396 m_base.set_locking(rl);
397 return *this;
398 }
399 };
400
401 class Delete : public Wrapper<::Mysqlx::Crud::Delete> {
402 public:
403 explicit Delete(const Collection &coll,
404 const Data_model dm = ::Mysqlx::Crud::DOCUMENT) {
405 *m_base.mutable_collection() = coll;
406 m_base.set_data_model(dm);
407 }
408 Delete &args(const Expression_list &arg) {
409 *m_base.mutable_args() = arg;
410 return *this;
411 }
412 Delete &criteria(const Filter &ft) {
413 *m_base.mutable_criteria() = ft;
414 return *this;
415 }
416 Delete &order(const Order_list &ol) {
417 *m_base.mutable_order() = ol;
418 return *this;
419 }
420 Delete &order(const Order &ol) {
421 *m_base.mutable_order()->Add() = ol;
422 return *this;
423 }
424 Delete &limit(const Limit &li) {
425 *m_base.mutable_limit() = li;
426 return *this;
427 }
428 };
429
430 class Update : public Wrapper<::Mysqlx::Crud::Update> {
431 public:
432 explicit Update(const Collection &coll,
433 const Data_model dm = ::Mysqlx::Crud::DOCUMENT) {
434 *m_base.mutable_collection() = coll;
435 m_base.set_data_model(dm);
436 }
437 Update &args(const Expression_list &arg) {
438 *m_base.mutable_args() = arg;
439 return *this;
440 }
441 Update &operation(const Operation_list &ol) {
442 *m_base.mutable_operation() = ol;
443 return *this;
444 }
445 Update &operation(const Update_operation &uo) {
446 *m_base.mutable_operation()->Add() = uo;
447 return *this;
448 }
449 Update &criteria(const Filter &ft) {
450 *m_base.mutable_criteria() = ft;
451 return *this;
452 }
453 Update &order(const Order_list &ol) {
454 *m_base.mutable_order() = ol;
455 return *this;
456 }
457 Update &order(const Order &ol) {
458 *m_base.mutable_order()->Add() = ol;
459 return *this;
460 }
461 Update &limit(const Limit &li) {
462 *m_base.mutable_limit() = li;
463 return *this;
464 }
465 };
466
467 class Insert : public Wrapper<::Mysqlx::Crud::Insert> {
468 public:
469 explicit Insert(const Collection &coll,
470 const Data_model dm = ::Mysqlx::Crud::DOCUMENT) {
471 *m_base.mutable_collection() = coll;
472 m_base.set_data_model(dm);
473 }
474 Insert &args(const Expression_list &arg) {
475 *m_base.mutable_args() = arg;
476 return *this;
477 }
478 Insert &projection(const Column_projection_list &pl) {
479 *m_base.mutable_projection() = pl;
480 return *this;
481 }
482 Insert &row(const Row_list &rl) {
483 *m_base.mutable_row() = rl;
484 return *this;
485 }
486 Insert &upsert(const bool f) {
487 m_base.set_upsert(f);
488 return *this;
489 }
490 };
491
492 class Stmt_execute : public Wrapper<::Mysqlx::Sql::StmtExecute> {
493 public:
494 explicit Stmt_execute(const std::string &stmt,
495 const std::string &namespace_ = "sql",
496 const bool compact_metadata = false) {
497 m_base.set_stmt(stmt);
498 m_base.set_namespace_(namespace_);
499 m_base.set_compact_metadata(compact_metadata);
500 }
501 Stmt_execute &args(const Any_list &arg) {
502 *m_base.mutable_args() = arg;
503 return *this;
504 }
505 };
506
507 } // namespace test
508 } // namespace xpl
509
510 #endif // UNITTEST_GUNIT_XPLUGIN_XPL_MYSQLX_PB_WRAPPER_H_