| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 = ""); |
| 69 |
}; |
| 70 |
|
| 71 |
class Document_path_item : public Wrapper<::Mysqlx::Expr::DocumentPathItem> { |
| 72 |
public: |
| 73 |
Document_path_item(const Base::Type type) { |
| 74 |
m_base.set_type(type); |
| 75 |
} |
| 76 |
Document_path_item(const int index) { |
| 77 |
m_base.set_type(Base::ARRAY_INDEX); |
| 78 |
m_base.set_index(index); |
| 79 |
} |
| 80 |
Document_path_item(const char *value) { |
| 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 = ""); |
| 93 |
Column_identifier( |
| 94 |
const Document_path &path, const std::string &name = "", |
| 95 |
const std::string &table_name = "", |
| 96 |
const std::string &schema_name = ""); |
| 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); |
| 105 |
}; |
| 106 |
|
| 107 |
struct Octets : public Wrapper<::Mysqlx::Datatypes::Scalar_Octets> { |
| 108 |
enum class Content_type { |
| 109 |
k_plain = 0x0000, |
| 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); |
| 118 |
}; |
| 119 |
|
| 120 |
Scalar() = default; |
| 121 |
Scalar(const int value); |
| 122 |
Scalar(const unsigned int value); |
| 123 |
Scalar(const bool value); |
| 124 |
Scalar(const float value); |
| 125 |
Scalar(const double value); |
| 126 |
Scalar(const Scalar::Octets &value); |
| 127 |
Scalar(const char *value); |
| 128 |
Scalar(const Scalar::String &value); |
| 129 |
Scalar(Null value); |
| 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) |
| 147 |
: Any(Scalar(v)) {} |
| 148 |
Any(const Scalar &scalar); |
| 149 |
Any(const Object &obj); |
| 150 |
Any(const Array &array); |
| 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) |
| 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) |
| 184 |
: Expr(Scalar(value)) {} |
| 185 |
Expr(const Scalar &value); |
| 186 |
Expr(const Operator &oper); |
| 187 |
Expr(const Identifier &ident); |
| 188 |
Expr(const Function_call &func); |
| 189 |
Expr(const Column_identifier &id); |
| 190 |
Expr(const Object &obj); |
| 191 |
Expr(const Array &arr); |
| 192 |
Expr(const Placeholder &ph); |
| 193 |
Expr(const Variable &var); |
| 194 |
}; |
| 195 |
|
| 196 |
class Operator : public Wrapper<::Mysqlx::Expr::Operator> { |
| 197 |
public: |
| 198 |
Operator(const std::string &name) { |
| 199 |
m_base.set_name(name); |
| 200 |
} |
| 201 |
|
| 202 |
template <typename... T> |
| 203 |
Operator(const std::string &name, |
| 204 |
T &&...params) { |
| 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) { |
| 221 |
m_base.mutable_name()->CopyFrom(Identifier(name)); |
| 222 |
} |
| 223 |
|
| 224 |
template <typename... T> |
| 225 |
Function_call(const Identifier &name, |
| 226 |
T &&...params) { |
| 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) { |
| 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 = ""); |
| 274 |
Column(const Document_path &path, const std::string &name = "", |
| 275 |
const std::string &alias = ""); |
| 276 |
}; |
| 277 |
|
| 278 |
class Collection : public Wrapper<::Mysqlx::Crud::Collection> { |
| 279 |
public: |
| 280 |
Collection(const std::string &name, |
| 281 |
const std::string &schema = ""); |
| 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 = ""); |
| 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); |
| 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); |
| 303 |
}; |
| 304 |
|
| 305 |
class Limit_expr : public Wrapper<::Mysqlx::Crud::LimitExpr> { |
| 306 |
public: |
| 307 |
Limit_expr() = default; |
| 308 |
Limit_expr(const Expr &row_count); |
| 309 |
Limit_expr(const Expr &row_count, |
| 310 |
const Expr &offset); |
| 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 |
} |
| 508 |
} |
| 509 |
|
| 510 |
#endif |