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();
178 friend internal::Schema_detail;
182class PUBLIC_API
DbDoc::Iterator
185 std::shared_ptr<DbDoc::Impl> m_impl;
191 Iterator& operator++();
192 bool operator==(
const Iterator&)
const;
193 bool operator!=(
const Iterator &other)
const {
return !(*
this == other); }
194 const Field& operator*();
226 :
public virtual common::Printable
227 ,
protected common::Value
253 typedef std::vector<Value>::iterator iterator;
254 typedef std::vector<Value>::const_iterator const_iterator;
260 Value(std::nullptr_t);
266 Value(
const std::string &str);
267 Value(std::string &&str);
270 template <
typename C>
271 Value(
const std::basic_string<C> &str)
272 :
Value(mysqlx::string(str))
275 template <
typename C>
277 :
Value(mysqlx::string(str))
288 Value(
const std::initializer_list<Value> &list);
289 template <
typename Iterator>
290 Value(Iterator begin_, Iterator end_);
294 Value(common::Value &&other);
295 Value(
const common::Value &other);
299#ifdef HAVE_MOVE_CTORS
309 *
this = std::move(other);
321 typename std::enable_if<std::is_signed<T>::value>::type* =
nullptr
324 :
Value(static_cast<int64_t>(x))
329 typename std::enable_if<std::is_unsigned<T>::value>::type* =
nullptr
332 :
Value(static_cast<uint64_t>(x))
336 Value& operator=(
const Value&) =
default;
343 Value& operator=(Value&&);
352 Value& operator=(T&& x)
355 *
this = Value(std::forward<T>(x));
371 operator int()
const;
372 operator unsigned()
const;
373 operator int64_t()
const;
374 operator uint64_t()
const;
376 operator float()
const;
377 operator double()
const;
379 explicit operator bool()
const;
382 explicit operator std::string()
const;
384 template <
typename C>
385 explicit operator std::basic_string<C>()
const
390 explicit operator bytes()
const;
391 operator DbDoc()
const;
404 const byte *ptr = get_bytes(&len);
416 Type getType()
const;
430 bool hasField(
const Field&)
const;
437 const Value& operator[](
const Field&)
const;
440 {
return (*
this)[Field(name)]; }
443 {
return (*
this)[Field(name)]; }
454 const_iterator begin()
const;
456 const_iterator end()
const;
457 size_t elementCount()
const;
459 const Value& operator[](
unsigned)
const;
475 case DOC: out <<
m_doc;
return;
480 for (
auto it =
m_arr->begin();it!=
m_arr->end();++it)
491 switch (it->get_type())
493 case common::Value::STRING:
494 case common::Value::USTRING:
495 case common::Value::EXPR:
496 out << R
"(")" << *it << R"(")";
508 default: common::Value::print(out);
return;
514 enum { VAL, ARR, DOC } m_type = VAL;
519 throw Error(
"Invalid value type");
524 return VAL ==
m_type && common::Value::EXPR == common::Value::get_type();
529 common::Value::m_type = common::Value::EXPR;
561static const Value nullvalue;
567 m_type = other.m_type;
572 common::Value::operator=(std::move(other));
575 case DOC: m_doc = std::move(other.m_doc);
break;
576 case ARR: m_arr = std::move(other.m_arr);
break;
597 template <
typename V>
599 : Value(std::forward<V>(val))
604 template <
typename V>
605 Expression(
const V& val)
611 friend Expression
expr(std::string&& s);
612 friend Expression
expr(
const std::string& s);
636internal::Expression
expr(std::string&& e)
638 return std::forward<std::string>(e);
642internal::Expression expr(
const std::string& e)
658 case ARR:
return ARRAY;
661 switch (common::Value::get_type())
663 case common::Value::VNULL:
return VNULL;
664 case common::Value::UINT64:
return UINT64;
665 case common::Value::INT64:
return INT64;
666 case common::Value::FLOAT:
return FLOAT;
667 case common::Value::DOUBLE:
return DOUBLE;
668 case common::Value::BOOL:
return BOOL;
669 case common::Value::STRING:
return STRING;
670 case common::Value::USTRING:
return STRING;
671 case common::Value::RAW:
return RAW;
672 case common::Value::EXPR:
return STRING;
673 case common::Value::JSON:
return DOCUMENT;
688Value::Value(
const std::initializer_list<Value> &list)
692 m_arr = std::make_shared<Array>(list);
697template <
typename Iterator>
703 m_arr = std::make_shared<Array>(begin_, end_);
712 : common::
Value(std::move(other))
719 : common::
Value(other)
743PUBLIC_API common::Value Value::get<common::Value>()
const;
747int Value::get<int>()
const
750 int64_t val = get_sint();
751 if (val > std::numeric_limits<int>::max())
752 throw Error(
"Numeric conversion overflow");
753 if (val < std::numeric_limits<int>::min())
754 throw Error(
"Numeric conversion overflow");
762Value::operator int()
const
769inline unsigned Value::get<unsigned>()
const
772 uint64_t val = get_uint();
773 if (val > std::numeric_limits<unsigned>::max())
774 throw Error(
"Numeric conversion overflow");
776 return (
unsigned)val;
782Value::operator unsigned()
const
784 return get<unsigned>();
789inline int64_t Value::get<int64_t>()
const
798Value::operator int64_t()
const
800 return get<int64_t>();
805inline uint64_t Value::get<uint64_t>()
const
814Value::operator uint64_t()
const
816 return get<uint64_t>();
828float Value::get<float>()
const
837Value::operator float()
const
851double Value::get<double>()
const
860Value::operator double()
const
862 return get<double>();
875bool Value::get<bool>()
const
884Value::operator bool()
const
907 : common::
Value(std::move(val))
920 : common::
Value(std::move(val))
927std::wstring Value::get<std::wstring>()
const
938std::string Value::get<std::string>()
const
947Value::operator std::string()
const
949 return get<std::string>();
958 return this->get_ustring();
966 return get<mysqlx::string>();
972 : common::
Value(data.begin(), data.length())
978bytes Value::get<bytes>()
const
992DbDoc Value::get<DbDoc>()
const
1001 return get<DbDoc>();
1022 return (*
this)[fld].getType();
1032 throw Error(
"Attempt to iterate over non-array value");
1033 return m_arr->begin();
1040 throw Error(
"Attempt to iterate over non-array value");
1041 return m_arr->begin();
1048 throw Error(
"Attempt to iterate over non-array value");
1049 return m_arr->end();
1056 throw Error(
"Attempt to iterate over non-array value");
1057 return m_arr->end();
1064 return m_arr->at(pos);
1071 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:1020
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:228
bytes getRawBytes() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:400
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:439
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:516
Type getType() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:654
Type
Possible types of values.
Definition: document.h:240
@ DOCUMENT
Document.
Definition: document.h:248
@ ARRAY
Array of values.
Definition: document.h:250
@ DOUBLE
Double number.
Definition: document.h:245
@ INT64
Signed integer.
Definition: document.h:243
@ FLOAT
Float number.
Definition: document.h:244
@ UINT64
Unsigned integer.
Definition: document.h:242
@ VNULL
Null value.
Definition: document.h:241
@ RAW
Raw bytes.
Definition: document.h:249
@ BOOL
Boolean.
Definition: document.h:246
@ STRING
String.
Definition: document.h:247
bool hasField(const Field &) const
Check if document value contains given (top-level) field.
Definition: document.h:1006
friend DbDoc
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:551
iterator begin()
Access to elements of an array value.
Definition: document.h:1029
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:522
DbDoc m_doc
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:541
const Value & operator[](const Field &) const
If this value is not a document, throws error.
Definition: document.h:1013
iterator end()
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:1045
friend Access
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:557
Value(const std::basic_string< C > &str)
Constructs Null value.
Definition: document.h:271
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:442
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:544
bool isNull() const
Convenience method for checking if value is null.
Definition: document.h:420
Value()
Constructs Null value.
Definition: document.h:724
std::vector< Value > Array
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:537
Value(const char *str)
Constructs Null value.
Definition: document.h:268
friend SessionSettings
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:550
void print(std::ostream &out) const
Print the value to a stream.
Definition: document.h:471
Value(const C *str)
Constructs Null value.
Definition: document.h:276
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:460
size_t elementCount() const
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:1068
void set_as_expr()
Return type of the value stored in this instance (or VNULL if no value is stored).
Definition: document.h:527
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:636