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));
374 operator int()
const;
375 operator unsigned()
const;
376 operator int64_t()
const;
377 operator uint64_t()
const;
379 operator float()
const;
380 operator double()
const;
382 explicit operator bool()
const;
385 explicit operator std::string()
const;
387 template <
typename C>
388 explicit operator std::basic_string<C>()
const
393 explicit operator bytes()
const;
394 operator DbDoc()
const;
407 const byte *ptr = get_bytes(&len);
419 Type getType()
const;
433 bool hasField(
const Field&)
const;
440 const Value& operator[](
const Field&)
const;
443 {
return (*
this)[Field(name)]; }
446 {
return (*
this)[Field(name)]; }
457 const_iterator begin()
const;
459 const_iterator end()
const;
460 size_t elementCount()
const;
462 const Value& operator[](
unsigned)
const;
478 case DOC: out <<
m_doc;
return;
483 for (
auto it =
m_arr->begin();it!=
m_arr->end();++it)
494 switch (it->get_type())
496 case common::Value::STRING:
497 case common::Value::USTRING:
498 case common::Value::EXPR:
499 out << R
"(")" << *it << R"(")";
511 default: common::Value::print(out);
return;
517 enum { VAL, ARR, DOC } m_type = VAL;
522 throw Error(
"Invalid value type");
527 return VAL ==
m_type && common::Value::EXPR == common::Value::get_type();
532 common::Value::m_type = common::Value::EXPR;
564static const Value nullvalue;
570 m_type = other.m_type;
575 common::Value::operator=(std::move(other));
578 case DOC: m_doc = std::move(other.m_doc);
break;
579 case ARR: m_arr = std::move(other.m_arr);
break;
600 template <
typename V>
602 : Value(std::forward<V>(val))
607 template <
typename V>
608 Expression(
const V& val)
614 friend Expression
expr(std::string&& s);
615 friend Expression
expr(
const std::string& s);
639internal::Expression
expr(std::string&& e)
641 return std::forward<std::string>(e);
645internal::Expression expr(
const std::string& e)
661 case ARR:
return ARRAY;
664 switch (common::Value::get_type())
666 case common::Value::VNULL:
return VNULL;
667 case common::Value::UINT64:
return UINT64;
668 case common::Value::INT64:
return INT64;
669 case common::Value::FLOAT:
return FLOAT;
670 case common::Value::DOUBLE:
return DOUBLE;
671 case common::Value::BOOL:
return BOOL;
672 case common::Value::STRING:
return STRING;
673 case common::Value::USTRING:
return STRING;
674 case common::Value::RAW:
return RAW;
675 case common::Value::EXPR:
return STRING;
676 case common::Value::JSON:
return DOCUMENT;
691Value::Value(
const std::initializer_list<Value> &list)
695 m_arr = std::make_shared<Array>(list);
700template <
typename Iterator>
706 m_arr = std::make_shared<Array>(begin_, end_);
715 : common::
Value(std::move(other))
722 : common::
Value(other)
746PUBLIC_API common::Value Value::get<common::Value>()
const;
750int Value::get<int>()
const
753 int64_t val = get_sint();
754 if (val > std::numeric_limits<int>::max())
755 throw Error(
"Numeric conversion overflow");
756 if (val < std::numeric_limits<int>::min())
757 throw Error(
"Numeric conversion overflow");
765Value::operator int()
const
772inline unsigned Value::get<unsigned>()
const
775 uint64_t val = get_uint();
776 if (val > std::numeric_limits<unsigned>::max())
777 throw Error(
"Numeric conversion overflow");
779 return (
unsigned)val;
785Value::operator unsigned()
const
787 return get<unsigned>();
792inline int64_t Value::get<int64_t>()
const
801Value::operator int64_t()
const
803 return get<int64_t>();
808inline uint64_t Value::get<uint64_t>()
const
817Value::operator uint64_t()
const
819 return get<uint64_t>();
831float Value::get<float>()
const
840Value::operator float()
const
854double Value::get<double>()
const
863Value::operator double()
const
865 return get<double>();
878bool Value::get<bool>()
const
887Value::operator bool()
const
910 : common::
Value(std::move(val))
923 : common::
Value(std::move(val))
930std::wstring Value::get<std::wstring>()
const
941std::string Value::get<std::string>()
const
950Value::operator std::string()
const
952 return get<std::string>();
961 return this->get_ustring();
969 return get<mysqlx::string>();
975 : common::
Value(data.begin(), data.length())
981bytes Value::get<bytes>()
const
995DbDoc Value::get<DbDoc>()
const
1004 return get<DbDoc>();
1025 return (*
this)[fld].getType();
1035 throw Error(
"Attempt to iterate over non-array value");
1036 return m_arr->begin();
1043 throw Error(
"Attempt to iterate over non-array value");
1044 return m_arr->begin();
1051 throw Error(
"Attempt to iterate over non-array value");
1052 return m_arr->end();
1059 throw Error(
"Attempt to iterate over non-array value");
1060 return m_arr->end();
1067 return m_arr->at(pos);
1074 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:1023
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:818
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:403
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:442
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:519
Type getType() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:657
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:1009
friend DbDoc
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:554
iterator begin()
Access to elements of an array value.
Definition: document.h:1032
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:525
DbDoc m_doc
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:544
const Value & operator[](const Field &) const
If this value is not a document, throws error.
Definition: document.h:1016
iterator end()
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:1048
friend Access
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:560
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:445
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:547
bool isNull() const
Convenience method for checking if value is null.
Definition: document.h:423
Value()
Constructs Null value.
Definition: document.h:727
std::vector< Value > Array
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:540
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:553
void print(std::ostream &out) const
Print the value to a stream.
Definition: document.h:474
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:463
size_t elementCount() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:1071
void set_as_expr()
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:530
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:639