31#ifndef MYSQLX_DOCUMENT_H
32#define MYSQLX_DOCUMENT_H
64using Field = std::string;
82 :
public common::Printable
90 std::shared_ptr<Impl> m_impl;
94 INTERNAL
DbDoc(
const std::shared_ptr<Impl>&);
96 const char* get_json()
const;
114 explicit DbDoc(std::string&&);
121 bool isNull()
const {
return NULL == m_impl.get(); }
122 operator bool()
const {
return !isNull(); }
137 virtual int fieldType(
const Field&)
const;
145 const Value& operator[](
const char *name)
const
147 return this->operator[](Field(name));
152 return this->operator[](Field(name));
160 virtual void print(std::ostream&)
const;
172 virtual Iterator begin();
173 virtual Iterator end();
179 friend internal::Schema_detail;
184class PUBLIC_API
DbDoc::Iterator
187 std::shared_ptr<DbDoc::Impl> m_impl;
193 Iterator& operator++();
194 bool operator==(
const Iterator&)
const;
195 bool operator!=(
const Iterator &other)
const {
return !(*
this == other); }
196 const Field& operator*();
228 :
public virtual common::Printable
229 ,
protected common::Value
255 typedef std::vector<Value>::iterator iterator;
256 typedef std::vector<Value>::const_iterator const_iterator;
262 Value(std::nullptr_t);
268 Value(
const std::string &str);
269 Value(std::string &&str);
272 template <
typename C>
273 Value(
const std::basic_string<C> &str)
274 :
Value(mysqlx::string(str))
277 template <
typename C>
279 :
Value(mysqlx::string(str))
290 Value(
const std::initializer_list<Value> &list);
291 template <
typename Iterator>
292 Value(Iterator begin_, Iterator end_);
296 Value(common::Value &&other);
297 Value(
const common::Value &other);
301#ifdef HAVE_MOVE_CTORS
311 *
this = std::move(other);
323 typename std::enable_if<std::is_signed<T>::value>::type* =
nullptr
326 :
Value(static_cast<int64_t>(x))
331 typename std::enable_if<std::is_unsigned<T>::value>::type* =
nullptr
334 :
Value(static_cast<uint64_t>(x))
338 Value& operator=(
const Value&) =
default;
345 Value& operator=(Value&&);
354 Value& operator=(T&& x)
357 *
this = Value(std::forward<T>(x));
373 operator int()
const;
374 operator unsigned()
const;
375 operator int64_t()
const;
376 operator uint64_t()
const;
378 operator float()
const;
379 operator double()
const;
381 explicit operator bool()
const;
384 explicit operator std::string()
const;
386 template <
typename C>
387 explicit operator std::basic_string<C>()
const
392 explicit operator bytes()
const;
393 operator DbDoc()
const;
406 const byte *ptr = get_bytes(&len);
418 Type getType()
const;
432 bool hasField(
const Field&)
const;
439 const Value& operator[](
const Field&)
const;
442 {
return (*
this)[Field(name)]; }
445 {
return (*
this)[Field(name)]; }
456 const_iterator begin()
const;
458 const_iterator end()
const;
459 size_t elementCount()
const;
461 const Value& operator[](
unsigned)
const;
477 case DOC: out <<
m_doc;
return;
482 for (
auto it =
m_arr->begin();it!=
m_arr->end();++it)
493 switch (it->get_type())
495 case common::Value::STRING:
496 case common::Value::USTRING:
497 case common::Value::EXPR:
498 out << R
"(")" << *it << R"(")";
510 default: common::Value::print(out);
return;
516 enum { VAL, ARR, DOC } m_type = VAL;
521 throw Error(
"Invalid value type");
526 return VAL ==
m_type && common::Value::EXPR == common::Value::get_type();
531 common::Value::m_type = common::Value::EXPR;
563static const Value nullvalue;
569 m_type = other.m_type;
574 common::Value::operator=(std::move(other));
577 case DOC: m_doc = std::move(other.m_doc);
break;
578 case ARR: m_arr = std::move(other.m_arr);
break;
599 template <
typename V>
601 : Value(std::forward<V>(val))
606 template <
typename V>
607 Expression(
const V& val)
613 friend Expression
expr(std::string&& s);
614 friend Expression
expr(
const std::string& s);
638internal::Expression
expr(std::string&& e)
640 return std::forward<std::string>(e);
644internal::Expression expr(
const std::string& e)
660 case ARR:
return ARRAY;
663 switch (common::Value::get_type())
665 case common::Value::VNULL:
return VNULL;
666 case common::Value::UINT64:
return UINT64;
667 case common::Value::INT64:
return INT64;
668 case common::Value::FLOAT:
return FLOAT;
669 case common::Value::DOUBLE:
return DOUBLE;
670 case common::Value::BOOL:
return BOOL;
671 case common::Value::STRING:
return STRING;
672 case common::Value::USTRING:
return STRING;
673 case common::Value::RAW:
return RAW;
674 case common::Value::EXPR:
return STRING;
675 case common::Value::JSON:
return DOCUMENT;
690Value::Value(
const std::initializer_list<Value> &list)
694 m_arr = std::make_shared<Array>(list);
699template <
typename Iterator>
705 m_arr = std::make_shared<Array>(begin_, end_);
714 : common::
Value(std::move(other))
721 : common::
Value(other)
745PUBLIC_API common::Value Value::get<common::Value>()
const;
749int Value::get<int>()
const
752 int64_t val = get_sint();
753 if (val > std::numeric_limits<int>::max())
754 throw Error(
"Numeric conversion overflow");
755 if (val < std::numeric_limits<int>::min())
756 throw Error(
"Numeric conversion overflow");
764Value::operator int()
const
771inline unsigned Value::get<unsigned>()
const
774 uint64_t val = get_uint();
775 if (val > std::numeric_limits<unsigned>::max())
776 throw Error(
"Numeric conversion overflow");
778 return (
unsigned)val;
784Value::operator unsigned()
const
786 return get<unsigned>();
791inline int64_t Value::get<int64_t>()
const
800Value::operator int64_t()
const
802 return get<int64_t>();
807inline uint64_t Value::get<uint64_t>()
const
816Value::operator uint64_t()
const
818 return get<uint64_t>();
830float Value::get<float>()
const
839Value::operator float()
const
853double Value::get<double>()
const
862Value::operator double()
const
864 return get<double>();
877bool Value::get<bool>()
const
886Value::operator bool()
const
909 : common::
Value(std::move(val))
922 : common::
Value(std::move(val))
929std::wstring Value::get<std::wstring>()
const
940std::string Value::get<std::string>()
const
949Value::operator std::string()
const
951 return get<std::string>();
960 return this->get_ustring();
968 return get<mysqlx::string>();
974 : common::
Value(data.begin(), data.length())
980bytes Value::get<bytes>()
const
994DbDoc Value::get<DbDoc>()
const
1003 return get<DbDoc>();
1024 return (*
this)[fld].getType();
1034 throw Error(
"Attempt to iterate over non-array value");
1035 return m_arr->begin();
1042 throw Error(
"Attempt to iterate over non-array value");
1043 return m_arr->begin();
1050 throw Error(
"Attempt to iterate over non-array value");
1051 return m_arr->end();
1058 throw Error(
"Attempt to iterate over non-array value");
1059 return m_arr->end();
1066 return m_arr->at(pos);
1073 return m_arr->size();
Represents a collection of key-value pairs where value can be a scalar or another document.
Definition: document.h:83
virtual void print(std::ostream &) const
Print JSON description of the document.
virtual bool hasField(const Field &) const
Check if named field is a top-level field in the document.
DbDoc()
Create null document instance.
Definition: document.h:107
DbDoc(const std::string &)
Creates DbDoc instance out of given JSON string description.
virtual int fieldType(const Field &) const
Return Value::XXX constant that identifies type of value stored at given field.
Definition: document.h:1022
virtual const Value & operator[](const Field &) const
Return value of given field.
bool isNull() const
Check if document is null.
Definition: document.h:121
Result of an operation that returns documents.
Definition: result.h:815
Base class for connector errors.
Definition: common.h:84
Value object can store value of scalar type, string, array or document.
Definition: document.h:230
bytes getRawBytes() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:402
const Value & operator[](const char *name) const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:441
void check_type(Type t) const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:518
Type getType() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:656
Type
Possible types of values.
Definition: document.h:242
@ DOCUMENT
Document.
Definition: document.h:250
@ ARRAY
Array of values.
Definition: document.h:252
@ DOUBLE
Double number.
Definition: document.h:247
@ INT64
Signed integer.
Definition: document.h:245
@ FLOAT
Float number.
Definition: document.h:246
@ UINT64
Unsigned integer.
Definition: document.h:244
@ VNULL
Null value.
Definition: document.h:243
@ RAW
Raw bytes.
Definition: document.h:251
@ BOOL
Boolean.
Definition: document.h:248
@ STRING
String.
Definition: document.h:249
bool hasField(const Field &) const
Check if document value contains given (top-level) field.
Definition: document.h:1008
friend DbDoc
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:553
iterator begin()
Access to elements of an array value.
Definition: document.h:1031
enum mysqlx::abi2::r0::Value::@1 m_type
Return type of the value stored in this instance (or VNULL if no value is stored).
bool is_expr() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:524
DbDoc m_doc
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:543
const Value & operator[](const Field &) const
If this value is not a document, throws error.
Definition: document.h:1015
iterator end()
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:1047
friend Access
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:559
Value(const std::basic_string< C > &str)
Constructs Null value.
Definition: document.h:273
const Value & operator[](const mysqlx::string &name) const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:444
std::shared_ptr< Array > m_arr
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:546
bool isNull() const
Convenience method for checking if value is null.
Definition: document.h:422
Value()
Constructs Null value.
Definition: document.h:726
std::vector< Value > Array
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:539
Value(const char *str)
Constructs Null value.
Definition: document.h:270
friend SessionSettings
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:552
void print(std::ostream &out) const
Print the value to a stream.
Definition: document.h:473
Value(const C *str)
Constructs Null value.
Definition: document.h:278
const Value & operator[](int pos) const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:462
size_t elementCount() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:1070
void set_as_expr()
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:529
T get() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Class representing a region of memory holding raw bytes.
Definition: common.h:298
A wrapper around std::wstring that can perform conversions from/to different character encodings used...
Definition: common.h:114
Type
Types that can be reported in result meta-data.
Definition: result.h:241
internal::Expression expr(std::string &&e)
Function which indicates that a given string should be treated as expression.
Definition: document.h:638