- Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathdocument.h
1082 lines (824 loc) · 19 KB
/
document.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0, as
* published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms, as
* designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* Without limiting anything contained in the foregoing, this file,
* which is part of Connector/C++, is also subject to the
* Universal FOSS Exception, version 1.0, a copy of which can be found at
* https://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef MYSQLX_DOCUMENT_H
#defineMYSQLX_DOCUMENT_H
/**
@file
Declaration of DbDoc and related classes.
*/
#include"common.h"
#include<memory>
#include<stdint.h>
#include<limits>
#include<vector>
#include<assert.h>
#undef min
#undef max
namespacemysqlx {
MYSQLX_ABI_BEGIN(2,0)
classValue;
classDbDoc;
classDocResult;
classSessionSettings;
namespaceinternal{
classSchema_detail;
} //internal
using Field = std::string;
// Document class
// ==============
/**
Represents a collection of key-value pairs where value can be a scalar
or another document.
@note Internal document implementation is shared among DbDoc instances
and thus using DbDoc objects should be cheap.
@ingroup devapi_res
*/
classPUBLIC_API DbDoc
: public common::Printable
{
// TODO: move PUBLIC_API stuff to a detail class
classINTERNAL Impl;
DLL_WARNINGS_PUSH
std::shared_ptr<Impl> m_impl;
DLL_WARNINGS_POP
INTERNAL DbDoc(const std::shared_ptr<Impl>&);
constchar* get_json() const;
public:
/**
Create null document instance.
@note Null document is different from empty document that has
no fields.
*/
DbDoc() {}
/**
Creates DbDoc instance out of given JSON string description.
*/
explicitDbDoc(const std::string&);
explicitDbDoc(std::string&&);
/**
Check if document is null
*/
boolisNull() const { returnNULL == m_impl.get(); }
operatorbool() const { return !isNull(); }
/**
Check if named field is a top-level field in the document.
*/
virtualboolhasField(const Field&) const;
/**
Return Value::XXX constant that identifies type of value
stored at given field.
*/
virtualintfieldType(const Field&) const;
/**
Return value of given field.
*/
virtualconst Value& operator[](const Field&) const;
const Value& operator[](constchar *name) const
{
returnthis->operator[](Field(name));
}
const Value& operator[](const mysqlx::string &name) const
{
returnthis->operator[](Field(name));
}
/**
Print JSON description of the document.
*/
virtualvoidprint(std::ostream&) const;
/**
Iterator instance can iterate over (top-level) fields of a document.
A new iterator is obtained from begin() method.
@note Only one instance of an iterator can be used at a time (not
thread safe!).
*/
classIterator;
virtual Iterator begin();
virtual Iterator end();
friend Impl;
friend DocResult;
friend Value;
/// \cond Note: Ignore by doxygen
friend internal::Schema_detail;
/// \endcond
};
classPUBLIC_API DbDoc::Iterator
{
DLL_WARNINGS_PUSH
std::shared_ptr<DbDoc::Impl> m_impl;
DLL_WARNINGS_POP
bool m_end;
public:
Iterator& operator++();
booloperator==(const Iterator&) const;
booloperator!=(const Iterator &other) const { return !(*this == other); }
const Field& operator*();
friend DbDoc;
};
// Value class
// ===========
/**
%Value object can store value of scalar type, string, array or document.
Implicit conversions to and from corresponding C++ types are defined.
If conversion to wrong type is attempted, an error is thrown. If Value
object holds an array or document, then array elements or fields of
the document can be accessed using operator[]. Array values can be used
as STL containers.
Only direct conversions of stored value to the corresponding C++ type
are supported. There are no implicit number->string conversions etc.
Values of type RAW can refer to a region of memory containing raw bytes.
Such values are created from `bytes` and can by casted to `bytes` type.
@note Value object copies the values it stores. Thus, after storing value
in Value object, the original value can be destroyed without invalidating
the copy. This includes RAW Values which hold a copy of bytes.
@ingroup devapi_res
*/
classValue
: public virtual common::Printable
, protected common::Value
{
public:
using string = mysqlx::string;
/**
Possible types of values.
@sa getType()
*/
enum Type
{
VNULL, ///< Null value
UINT64, ///< Unsigned integer
INT64, ///< Signed integer
FLOAT, ///< Float number
DOUBLE, ///< Double number
BOOL, ///< Boolean
STRING, ///< String
DOCUMENT, ///< Document
RAW, ///< Raw bytes
ARRAY, ///< Array of values
};
typedef std::vector<Value>::iterator iterator;
typedef std::vector<Value>::const_iterator const_iterator;
///@name Value Constructors
///@{
Value(); ///< Constructs Null value.
Value(std::nullptr_t); ///< Constructs Null value.
Value(const mysqlx::string &str);
Value(mysqlx::string &&str);
//Value(const char16_t *str) : Value(mysqlx::string(str)) {}
Value(const std::string &str);
Value(std::string &&str);
Value(constchar *str) : Value(std::string(str)) {}
template <typename C>
Value(const std::basic_string<C> &str)
: Value(mysqlx::string(str))
{}
template <typename C>
Value(const C *str)
: Value(mysqlx::string(str))
{}
Value(const bytes&);
Value(int64_t);
Value(uint64_t);
Value(float);
Value(double);
Value(bool);
Value(const DbDoc& doc);
Value(const std::initializer_list<Value> &list);
template <typename Iterator>
Value(Iterator begin_, Iterator end_);
///@}
Value(common::Value &&other);
Value(const common::Value &other);
Value(const Value&) = default;
#ifdef HAVE_MOVE_CTORS
Value(Value&&) = default;
#else
// Note move ctor implemented using move assignment defined below.
Value(Value &&other)
{
*this = std::move(other);
}
#endif
/*
Note: These templates are needed to disambiguate constructor resolution
for integer types.
*/
template <
typename T,
typename std::enable_if<std::is_signed<T>::value>::type* = nullptr
>
Value(T x)
: Value(static_cast<int64_t>(x))
{}
template <
typename T,
typename std::enable_if<std::is_unsigned<T>::value>::type* = nullptr
>
Value(T x)
: Value(static_cast<uint64_t>(x))
{}
Value& operator=(const Value&) = default;
/*
Note: Move assignment is defined explicitly to avoid problems with
virtual Printable base.
*/
Value& operator=(Value&&);
/*
Assignment is implemented in terms of constructors: first an instance
is created from the input data and then move assignment is used to place
the result into this instance.
*/
template<typename T>
Value& operator=(T&& x)
{
try {
*this = Value(std::forward<T>(x));
return *this;
}
CATCH_AND_WRAP
}
virtual~Value() {}
public:
/**
@name Conversion to C++ Types
Attempt to convert value of non-compatible type throws an error.
*/
//@{
operatorint() const;
operatorunsigned() const;
operatorint64_t() const;
operatoruint64_t() const;
operatorfloat() const;
operatordouble() const;
explicitoperatorbool() const;
operatormysqlx::string() const;
explicitoperatorstd::string() const;
template <typename C>
explicitoperator std::basic_string<C>() const
{
returnthis->operatormysqlx::string();
}
explicitoperatorbytes() const;
operatorDbDoc() const;
template<typename T>
T get() const;
//@}
bytes getRawBytes() const
{
try {
size_t len;
const byte *ptr = get_bytes(&len);
return { ptr, len };
}
CATCH_AND_WRAP
}
/**
Return type of the value stored in this instance (or VNULL if no
value is stored).
*/
Type getType() const;
/// Convenience method for checking if value is null.
boolisNull() const
{
return VNULL == getType();
}
/**
Check if document value contains given (top-level) field.
Throws error if this is not a document value.
*/
boolhasField(const Field&) const;
/**
If this value is not a document, throws error. Otherwise
returns value of given field of the document.
*/
const Value& operator[](const Field&) const;
const Value& operator[](constchar *name) const
{ return (*this)[Field(name)]; }
const Value& operator[](const mysqlx::string &name) const
{ return (*this)[Field(name)]; }
/**
Access to elements of an array value.
If non-array value is accessed like an array, an error is thrown.
*/
//@{
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
size_telementCount() const;
const Value& operator[](unsigned) const;
const Value& operator[](int pos) const
{
assert(pos >= 0);
returnoperator[]((unsigned)pos);
}
//@}
/// Print the value to a stream.
voidprint(std::ostream &out) const
{
switch (m_type)
{
case DOC: out << m_doc; return;
case ARR:
{
bool first = true;
out << "[";
for (auto it = m_arr->begin();it!=m_arr->end();++it)
{
if (!first)
{
out << ", ";
}
else
{
first = false;
}
switch (it->get_type())
{
case common::Value::STRING:
case common::Value::USTRING:
case common::Value::EXPR:
out << R"(")" << *it << R"(")";
break;
default:
out << *it;
break;
}
}
out << "]";
return;
}
default: common::Value::print(out); return;
}
}
protected:
enum { VAL, ARR, DOC } m_type = VAL;
voidcheck_type(Type t) const
{
if (getType() != t)
throwError("Invalid value type");
}
boolis_expr() const
{
return VAL == m_type && common::Value::EXPR == common::Value::get_type();
}
voidset_as_expr()
{
common::Value::m_type = common::Value::EXPR;
}
/*
TODO: Instead extend common::Value with array and document types. Requires
moving DbDoc code to the common layer.
*/
typedef std::vector<Value> Array;
DLL_WARNINGS_PUSH
DbDoc m_doc;
// Note: shared with other Value instances for the same array.
std::shared_ptr<Array> m_arr;
DLL_WARNINGS_POP
public:
friend SessionSettings;
friend DbDoc;
///@cond IGNORE
friend mysqlx::string;
///@endcond IGNORE
structINTERNAL Access;
friend Access;
};
staticconst Value nullvalue;
inline
Value& Value::operator=(Value &&other)
{
m_type = other.m_type;
switch (m_type)
{
case VAL:
common::Value::operator=(std::move(other));
break;
case DOC: m_doc = std::move(other.m_doc); break;
case ARR: m_arr = std::move(other.m_arr); break;
default: break;
}
return *this;
}
namespaceinternal {
/*
Helper class to identify usage of expressions
*/
classExpression
: public mysqlx::Value
{
Expression()
{}
template <typename V>
Expression(V&& val)
: Value(std::forward<V>(val))
{
set_as_expr();
}
template <typename V>
Expression(const V& val)
: Value(val)
{
set_as_expr();
}
friend Expression expr(std::string&& s);
friend Expression expr(const std::string& s);
};
/**
Function which indicates that a given string should be treated
as expression.
If `s` is a string value, then in contexts where values are
expected, `expr(s)` treats `s` as a DevAPI expression. For
example statement
table.select("foo > 1").execute();
returns the string `"foo 1"` for each row in the table while
table.select(expr("foo > 1")).execute();
returns true/false, depending on the value of the expression.
@ingroup devapi
*/
inline
internal::Expression expr(std::string&& e)
{
return std::forward<std::string>(e);
}
inline
internal::Expression expr(const std::string& e)
{
return e;
}
} // internal
using internal::expr;
inline
Value::Type Value::getType() const
{
switch (m_type)
{
case ARR: return ARRAY;
case DOC: return DOCUMENT;
case VAL:
switch (common::Value::get_type())
{
case common::Value::VNULL: return VNULL;
case common::Value::UINT64: return UINT64;
case common::Value::INT64: return INT64;
case common::Value::FLOAT: return FLOAT;
case common::Value::DOUBLE: return DOUBLE;
case common::Value::BOOL: return BOOL;
case common::Value::STRING: return STRING;
case common::Value::USTRING: return STRING;
case common::Value::RAW: return RAW;
case common::Value::EXPR: return STRING;
case common::Value::JSON: return DOCUMENT;
}
}
return VNULL; // quiet compiler warning
}
/*
Value type conversions
----------------------
TODO: more informative errors
*/
inline
Value::Value(const std::initializer_list<Value> &list)
: m_type(ARR)
{
try {
m_arr = std::make_shared<Array>(list);
}
CATCH_AND_WRAP
}
template <typename Iterator>
inline
Value::Value(Iterator begin_, Iterator end_)
: m_type(ARR)
{
try {
m_arr = std::make_shared<Array>(begin_, end_);
}
CATCH_AND_WRAP
}
inline
Value::Value(common::Value &&other)
try
: common::Value(std::move(other))
{}
CATCH_AND_WRAP
inline
Value::Value(const common::Value &other)
try
: common::Value(other)
{}
CATCH_AND_WRAP
inlineValue::Value()
{}
inlineValue::Value(std::nullptr_t)
{}
inlineValue::Value(int64_t val)
try
: common::Value(val)
{}
CATCH_AND_WRAP
inlineValue::Value(uint64_t val)
try
: common::Value(val)
{}
CATCH_AND_WRAP
template <>
PUBLIC_API common::Value Value::get<common::Value>() const;
template<>
inline
int Value::get<int>() const
{
try {
int64_t val = get_sint();
if (val > std::numeric_limits<int>::max())
throwError("Numeric conversion overflow");
if (val < std::numeric_limits<int>::min())
throwError("Numeric conversion overflow");
return (int)val;
}
CATCH_AND_WRAP
}
inline
Value::operatorint() const
{
return get<int>();
}
template<>
inlineunsigned Value::get<unsigned>() const
{
try {
uint64_t val = get_uint();
if (val > std::numeric_limits<unsigned>::max())
throwError("Numeric conversion overflow");
return (unsigned)val;
}
CATCH_AND_WRAP
}
inline
Value::operatorunsigned() const
{
return get<unsigned>();
}
template<>
inlineint64_t Value::get<int64_t>() const
{
try {
returnget_sint();
}
CATCH_AND_WRAP
}
inline
Value::operatorint64_t() const
{
return get<int64_t>();
}
template<>
inlineuint64_t Value::get<uint64_t>() const
{
try {
returnget_uint();
}
CATCH_AND_WRAP
}
inline
Value::operatoruint64_t() const
{
return get<uint64_t>();
}
inlineValue::Value(float val)
try
: common::Value(val)
{}
CATCH_AND_WRAP
template<>
inline
float Value::get<float>() const
{
try {
returnget_float();
}
CATCH_AND_WRAP
}
inline
Value::operatorfloat() const
{
return get<float>();
}
inlineValue::Value(double val)
try
: common::Value(val)
{}
CATCH_AND_WRAP
template<>
inline
double Value::get<double>() const
{
try {
returnget_double();
}
CATCH_AND_WRAP
}
inline
Value::operatordouble() const
{
return get<double>();
}
inlineValue::Value(bool val)
try
: common::Value(val)
{}
CATCH_AND_WRAP
template<>
inline
bool Value::get<bool>() const
{
try {
returnget_bool();
}
CATCH_AND_WRAP
}
inline
Value::operatorbool() const
{
return get<bool>();
}
inlineValue::Value(const DbDoc &doc)
try
: m_type(DOC)
, m_doc(doc)
{}
CATCH_AND_WRAP
inlineValue::Value(const mysqlx::string &val)
try
: common::Value(val)
{}
CATCH_AND_WRAP
inlineValue::Value(mysqlx::string &&val)
try
: common::Value(std::move(val))
{}
CATCH_AND_WRAP
inlineValue::Value(const std::string &val)
try
: common::Value(val)
{}
CATCH_AND_WRAP
inlineValue::Value(std::string &&val)
try
: common::Value(std::move(val))
{}
CATCH_AND_WRAP
template<>
inline
std::wstring Value::get<std::wstring>() const
{
try {
returnmysqlx::string(this->get_ustring());
}
CATCH_AND_WRAP
}
template<>
inline
std::string Value::get<std::string>() const
{
try {
returnget_string();
}
CATCH_AND_WRAP
}
inline
Value::operatorstd::string() const
{
return get<std::string>();
}
template<>
inline
mysqlx::string Value::get<mysqlx::string>() const
{
try {
returnthis->get_ustring();
}
CATCH_AND_WRAP
}
inline
Value::operatormysqlx::string() const
{
return get<mysqlx::string>();
}
inlineValue::Value(const bytes &data)
try
: common::Value(data.begin(), data.length())
{}
CATCH_AND_WRAP
template<>
inline
bytes Value::get<bytes>() const
{
returngetRawBytes();
}
inline
Value::operatorbytes() const
{
return get<bytes>();
}
template<>
inline
DbDoc Value::get<DbDoc>() const
{
check_type(DOCUMENT);
return m_doc;
}