MySQL 8.0.33
Source Code Documentation
json_dom.h
Go to the documentation of this file.
1#ifndef JSON_DOM_INCLUDED
2#define JSON_DOM_INCLUDED
3
4/* Copyright (c) 2015, 2023, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include <assert.h>
27#include <stddef.h>
28#include <functional>
29#include <iterator>
30#include <map>
31#include <memory> // unique_ptr
32#include <new>
33#include <string>
34#include <type_traits> // is_base_of
35#include <utility>
36#include <vector>
37
38#include "field_types.h" // enum_field_types
39#include "my_compiler.h"
40
41#include "my_inttypes.h"
42#include "my_time.h" // my_time_flags_t
44#include "mysql_time.h" // MYSQL_TIME
45#include "prealloced_array.h" // Prealloced_array
46#include "sql-common/json_binary.h" // json_binary::Value
47#include "sql/malloc_allocator.h" // Malloc_allocator
48#include "sql/my_decimal.h" // my_decimal
49
50class Field_json;
51class Json_array;
52class Json_container;
53class Json_dom;
54class Json_object;
55class Json_path;
57class Json_wrapper;
58class String;
59class THD;
60
63
64using Json_dom_ptr = std::unique_ptr<Json_dom>;
65using Json_array_ptr = std::unique_ptr<Json_array>;
66using Json_object_ptr = std::unique_ptr<Json_object>;
67
68/**
69 @file
70 JSON DOM.
71
72 When a JSON value is retrieved from a column, a prior it exists in
73 a binary form, cf. Json_binary::Value class.
74
75 However, when we need to manipulate the JSON values we mostly convert them
76 from binary form to a structured in-memory from called DOM (from domain
77 object model) which uses a recursive tree representation of the JSON value
78 corresponding closely to a parse tree. This form is more suitable for
79 manipulation.
80
81 The JSON type is mostly represented internally as a Json_wrapper which hides
82 if the representation is a binary or DOM one. This makes is possible to avoid
83 building a DOM unless we really need one.
84
85 The file defines two sets of classes: a) The Json_dom hierarchy and
86 b) Json_wrapper and its companion classes Json_wrapper_object_iterator and
87 Json_object_wrapper. For both sets, arrays are traversed using an operator[].
88*/
89
90/**
91 Json values in MySQL comprises the stand set of JSON values plus a
92 MySQL specific set. A Json _number_ type is subdivided into _int_,
93 _uint_, _double_ and _decimal_.
94
95 MySQL also adds four built-in date/time values: _date_, _time_,
96 _datetime_ and _timestamp_. An additional _opaque_ value can
97 store any other MySQL type.
98
99 The enumeration is common to Json_dom and Json_wrapper.
100
101 The enumeration is also used by Json_wrapper::compare() to
102 determine the ordering when comparing values of different types,
103 so the order in which the values are defined in the enumeration,
104 is significant. The expected order is null < number < string <
105 object < array < boolean < date < time < datetime/timestamp <
106 opaque.
107*/
108enum class enum_json_type {
109 J_NULL,
110 J_DECIMAL,
111 J_INT,
112 J_UINT,
113 J_DOUBLE,
114 J_STRING,
115 J_OBJECT,
116 J_ARRAY,
117 J_BOOLEAN,
118 J_DATE,
119 J_TIME,
122 J_OPAQUE,
123 J_ERROR
124};
125
126/**
127 Allocate a new Json_dom object and return a std::unique_ptr which points to
128 it.
129
130 @param args the arguments to pass to the constructor
131
132 @tparam T the type of Json_dom to create
133 @tparam Args the type of the arguments to pass to the constructor
134
135 @return a pointer to the allocated object
136*/
137template <typename T, typename... Args>
138inline std::unique_ptr<T> create_dom_ptr(Args &&... args) {
139 return std::unique_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
140}
141
142/**
143 JSON DOM abstract base class.
144
145 MySQL representation of in-memory JSON objects used by the JSON type
146 Supports access, deep cloning, and updates. See also Json_wrapper and
147 json_binary::Value.
148 Uses heap for space allocation for now. FIXME.
149
150 Class hierarchy:
151 <code><pre>
152 Json_dom (abstract)
153 Json_scalar (abstract)
154 Json_string
155 Json_number (abstract)
156 Json_decimal
157 Json_int
158 Json_uint
159 Json_double
160 Json_boolean
161 Json_null
162 Json_datetime
163 Json_opaque
164 Json_container (abstract)
165 Json_object
166 Json_array
167 </pre></code>
168 At the outset, object and array add/insert/append operations takes
169 a clone unless specified in the method, e.g. add_alias hands the
170 responsibility for the passed in object over to the object.
171*/
172class Json_dom {
173 // so that these classes can call set_parent()
174 friend class Json_object;
175 friend class Json_array;
176
177 private:
178 /**
179 Set the parent dom to which this dom is attached.
180
181 @param[in] parent the parent we're being attached to
182 */
184
185 public:
186 virtual ~Json_dom() = default;
187
188 /**
189 Allocate space on the heap for a Json_dom object.
190
191 @return pointer to the allocated memory, or NULL if memory could
192 not be allocated (in which case my_error() will have been called
193 with the appropriate error message)
194 */
195 void *operator new(size_t size, const std::nothrow_t &) noexcept;
196
197 /**
198 Deallocate the space used by a Json_dom object.
199 */
200 void operator delete(void *ptr) noexcept;
201
202 /**
203 Nothrow delete.
204 */
205 void operator delete(void *ptr, const std::nothrow_t &) noexcept;
206
207 /**
208 Get the parent dom to which this dom is attached.
209
210 @return the parent dom.
211 */
212 Json_container *parent() const { return m_parent; }
213
214 /**
215 @return the type corresponding to the actual Json_dom subclass
216 */
217 virtual enum_json_type json_type() const = 0;
218
219 /**
220 @return true if the object is a subclass of Json_scalar
221 */
222 virtual bool is_scalar() const { return false; }
223
224 /**
225 @return true of the object is a subclass of Json_number
226 */
227 virtual bool is_number() const { return false; }
228
229 /**
230 Compute the depth of a document. This is the value which would be
231 returned by the JSON_DEPTH() system function.
232
233 - for scalar values, empty array and empty object: 1
234 - for non-empty array: 1+ max(depth of array elements)
235 - for non-empty objects: 1+ max(depth of object values)
236
237 For example:
238 "abc", [] and {} have depth 1.
239 ["abc", [3]] and {"a": "abc", "b": [3]} have depth 3.
240
241 @return the depth of the document
242 */
243 virtual uint32 depth() const = 0;
244
245 /**
246 Make a deep clone. The ownership of the returned object is
247 henceforth with the caller.
248
249 @return a cloned Json_dom object.
250 */
251 virtual Json_dom_ptr clone() const = 0;
252
253 /**
254 Parse Json text to DOM (using rapidjson). The text must be valid JSON.
255 The results when supplying an invalid document is undefined.
256 The ownership of the returned object is henceforth with the caller.
257
258 If the parsing fails because of a syntax error, the errmsg and
259 offset arguments will be given values that point to a detailed
260 error message and where the syntax error was located. The caller
261 will have to generate an error message with my_error() in this
262 case.
263
264 If the parsing fails because of some other error (such as out of
265 memory), errmsg will point to a location that holds the value
266 NULL. In this case, parse() will already have called my_error(),
267 and the caller doesn't need to generate an error message.
268
269 @param[in] text the JSON text
270 @param[in] length the length of the text
271 @param[out] errmsg any syntax error message (will be ignored if it is NULL)
272 @param[out] offset the position in the parsed string a syntax error was
273 found (will be ignored if it is NULL)
274 @param[in] error_handler Pointer to a function that should handle
275 reporting of parsing error.
276 @param[in] depth_handler Pointer to a function that should handle error
277 occurred when depth is exceeded.
278
279
280 @result the built DOM if JSON text was parseable, else NULL
281 */
282 static Json_dom_ptr parse(const char *text, size_t length,
283 const JsonParseErrorHandler &error_handler,
284 const JsonDocumentDepthHandler &depth_handler);
285
286 /**
287 Construct a DOM object based on a binary JSON value. The ownership
288 of the returned object is henceforth with the caller.
289
290 @param thd current session
291 @param v the binary value to parse
292 @return a DOM representation of the binary value, or NULL on error
293 */
294 static Json_dom_ptr parse(const json_binary::Value &v);
295
296 /**
297 Get the path location of this dom, measured from the outermost
298 document it nests inside.
299 */
300 Json_path get_location() const;
301
302 /**
303 Finds all of the json sub-documents which match the path expression.
304 Adds a vector element for each match.
305
306 See the header comment for Json_wrapper.seek() for a discussion
307 of complexities involving path expression with more than one
308 ellipsis (**) token.
309
310 @param[in] path the (possibly wildcarded) address of the sub-documents
311 @param[in] legs the number of legs to use from @a path
312 @param[out] hits one element per match
313 @param[in] auto_wrap
314 if true, match a tailing [0] to scalar at that position.
315 @param[in] only_need_one True if we can stop after finding one match
316 @return false on success, true on error
317 */
318 bool seek(const Json_seekable_path &path, size_t legs, Json_dom_vector *hits,
319 bool auto_wrap, bool only_need_one);
320
321 private:
322 /** Parent pointer */
324};
325
326/**
327 Abstract base class of all JSON container types (Json_object and Json_array).
328*/
329class Json_container : public Json_dom {
330 public:
331 /**
332 Replace oldv contained inside this container array or object) with newv. If
333 this container does not contain oldv, calling the method is a no-op.
334
335 @param[in] oldv the value to be replaced
336 @param[in] newv the new value to put in the container
337 */
338 virtual void replace_dom_in_container(const Json_dom *oldv,
339 Json_dom_ptr newv) = 0;
340};
341
342/**
343 A comparator that is used for ordering keys in a Json_object. It
344 orders the keys on length, and lexicographically if the keys have
345 the same length. The ordering is ascending. This ordering was chosen
346 for speed of look-up. See usage in Json_object_map.
347*/
349 bool operator()(const std::string &key1, const std::string &key2) const;
350 bool operator()(const MYSQL_LEX_CSTRING &key1, const std::string &key2) const;
351 bool operator()(const std::string &key1, const MYSQL_LEX_CSTRING &key2) const;
352 // is_transparent must be defined in order to make std::map::find() accept
353 // keys that are of a different type than the key_type of the map. In
354 // particular, this is needed to make it possible to call find() with
355 // MYSQL_LEX_CSTRING arguments and not only std::string arguments. It only has
356 // to be defined, it doesn't matter which type it is set to.
357 using is_transparent = void;
358};
359
360/**
361 A type used to hold JSON object elements in a map, see the
362 Json_object class.
363*/
367
368/**
369 Represents a JSON container value of type "object" (ECMA), type
370 J_OBJECT here.
371*/
372class Json_object final : public Json_container {
373 private:
374 /**
375 Map to hold the object elements.
376 */
378
379 public:
380 Json_object();
382
383 /**
384 Insert a clone of the value into the object. If the key already
385 exists in the object, the existing value is replaced ("last value
386 wins").
387
388 @param[in] key the JSON element key of to be added
389 @param[in] value a JSON value: the element key's value
390 @retval false on success
391 @retval true on failure
392 */
393 bool add_clone(const std::string &key, const Json_dom *value) {
394 return value == nullptr || add_alias(key, value->clone());
395 }
396
397 /**
398 Insert the value into the object. If the key already exists in the
399 object, the existing value is replaced ("last value wins").
400
401 Ownership of the value is effectively transferred to the
402 object and the value will be deallocated by the object so only add
403 values that can be deallocated safely (no stack variables please!)
404
405 New code should prefer #add_alias(const std::string&, Json_dom_ptr)
406 to this function, because that makes the transfer of ownership
407 more explicit. This function might be removed in the future.
408
409 @param[in] key the JSON key of to be added
410 @param[in] value a JSON value: the key's value
411 @retval false on success
412 @retval true on failure
413 */
414 bool add_alias(const std::string &key, Json_dom *value) {
415 return add_alias(key, Json_dom_ptr(value));
416 }
417
418 /**
419 Insert the value into the object. If the key already exists in the
420 object, the existing value is replaced ("last value wins").
421
422 The ownership of the value is transferred to the object.
423
424 @param[in] key the key of the value to be added
425 @param[in] value the value to add
426 @return false on success, true on failure
427 */
428 bool add_alias(const std::string &key, Json_dom_ptr value);
429
430 /**
431 Transfer all of the key/value pairs in the other object into this
432 object. The other object is deleted. If this object and the other
433 object share a key, then the two values of the key are merged.
434
435 @param [in] other a pointer to the object which will be consumed
436 @retval false on success
437 @retval true on failure
438 */
439 bool consume(Json_object_ptr other);
440
441 /**
442 Return the value at key. The value is not cloned, so make
443 one if you need it. Do not delete the returned value, please!
444 If the key is not present, return a null pointer.
445
446 @param[in] key the key of the element whose value we want
447 @return the value associated with the key, or NULL if the key is not found
448 */
449 Json_dom *get(const std::string &key) const;
450 Json_dom *get(const MYSQL_LEX_CSTRING &key) const;
451
452 /**
453 Remove the child element addressed by key. The removed child is deleted.
454
455 @param key the key of the element to remove
456 @retval true if an element was removed
457 @retval false if there was no element with that key
458 */
459 bool remove(const std::string &key);
460
461 /**
462 @return The number of elements in the JSON object.
463 */
464 size_t cardinality() const;
465
466 uint32 depth() const override;
467
468 Json_dom_ptr clone() const override;
469
470 void replace_dom_in_container(const Json_dom *oldv,
471 Json_dom_ptr newv) override;
472
473 /**
474 Remove all elements in the object.
475 */
476 void clear() { m_map.clear(); }
477
478 /**
479 Constant iterator over the elements in the JSON object. Each
480 element is represented as a std::pair where first is a std::string
481 that represents the key name, and second is a pointer to a
482 Json_dom that represents the value.
483 */
484 typedef Json_object_map::const_iterator const_iterator;
485
486 /// Returns a const_iterator that refers to the first element.
487 const_iterator begin() const { return m_map.begin(); }
488
489 /// Returns a const_iterator that refers past the last element.
490 const_iterator end() const { return m_map.end(); }
491
492 /**
493 Implementation of the MergePatch function specified in RFC 7396:
494
495 define MergePatch(Target, Patch):
496 if Patch is an Object:
497 if Target is not an Object:
498 Target = {} # Ignore the contents and set it to an empty Object
499 for each Key/Value pair in Patch:
500 if Value is null:
501 if Key exists in Target:
502 remove the Key/Value pair from Target
503 else:
504 Target[Key] = MergePatch(Target[Key], Value)
505 return Target
506 else:
507 return Patch
508
509 @param patch the object that describes the patch
510 @retval false on success
511 @retval true on memory allocation error
512 */
513 bool merge_patch(Json_object_ptr patch);
514};
515
516/**
517 Represents a JSON array container, i.e. type J_ARRAY here.
518*/
519class Json_array final : public Json_container {
520 private:
521 /// Holds the array values.
522 std::vector<Json_dom_ptr, Malloc_allocator<Json_dom_ptr>> m_v;
523
524 public:
525 Json_array();
526
528
529 /**
530 Append a clone of the value to the end of the array.
531 @param[in] value a JSON value to be appended
532 @retval false on success
533 @retval true on failure
534 */
535 bool append_clone(const Json_dom *value) {
536 return insert_clone(size(), value);
537 }
538
539 /**
540 Append the value to the end of the array.
541
542 Ownership of the value is effectively transferred to the array and
543 the value will be deallocated by the array so only append values
544 that can be deallocated safely (no stack variables please!)
545
546 New code should prefer #append_alias(Json_dom_ptr) to this
547 function, because that makes the transfer of ownership more
548 explicit. This function might be removed in the future.
549
550 @param[in] value a JSON value to be appended
551 @retval false on success
552 @retval true on failure
553 */
554 bool append_alias(Json_dom *value) {
555 return append_alias(Json_dom_ptr(value));
556 }
557
558 /**
559 Append the value to the end of the array and take over the
560 ownership of the value.
561
562 @param value the JSON value to be appended
563 @return false on success, true on failure
564 */
566 return insert_alias(size(), std::move(value));
567 }
568
569 /**
570 Moves all of the elements in the other array to the end of
571 this array. The other array is deleted.
572
573 @param [in] other a pointer to the array which will be consumed
574 @retval false on success
575 @retval true on failure
576 */
577 bool consume(Json_array_ptr other);
578
579 /**
580 Insert a clone of the value at position index of the array. If beyond the
581 end, insert at the end.
582
583 @param[in] index the position at which to insert
584 @param[in] value a JSON value to be inserted
585 @retval false on success
586 @retval true on failure
587 */
588 bool insert_clone(size_t index, const Json_dom *value) {
589 return value == nullptr || insert_alias(index, value->clone());
590 }
591
592 /**
593 Insert the value at position index of the array.
594 If beyond the end, insert at the end.
595
596 Ownership of the value is effectively transferred to the array and
597 the value will be deallocated by the array so only append values
598 that can be deallocated safely (no stack variables please!)
599
600 @param[in] index the position at which to insert
601 @param[in] value a JSON value to be inserted
602 @retval false on success
603 @retval true on failure
604 */
605 bool insert_alias(size_t index, Json_dom_ptr value);
606
607 /**
608 Remove the value at this index. A no-op if index is larger than
609 size. Deletes the value.
610 @param[in] index the index of the value to remove
611 @return true if a value was removed, false otherwise.
612 */
613 bool remove(size_t index);
614
615 /**
616 The cardinality of the array (number of values).
617 @return the size
618 */
619 size_t size() const { return m_v.size(); }
620
621 uint32 depth() const override;
622
623 Json_dom_ptr clone() const override;
624
625 /**
626 Get the value at position index. The value has not been cloned so
627 it is the responsibility of the user to make a copy if needed. Do
628 not try to deallocate the returned value - it is owned by the array
629 and will be deallocated by it in time. It is admissible to modify
630 its contents (in place; without a clone being taken) if it is a
631 compound.
632
633 @param[in] index the array index
634 @return the value at index
635 */
636 Json_dom *operator[](size_t index) const {
637 assert(m_v[index]->parent() == this);
638 return m_v[index].get();
639 }
640
641 /**
642 Remove the values in the array.
643 */
644 void clear() { m_v.clear(); }
645
646 /// Constant iterator over the elements in the JSON array.
648
649 /// Returns a const_iterator that refers to the first element.
650 const_iterator begin() const { return m_v.begin(); }
651
652 /// Returns a const_iterator that refers past the last element.
653 const_iterator end() const { return m_v.end(); }
654
655 void replace_dom_in_container(const Json_dom *oldv,
656 Json_dom_ptr newv) override;
657
658 /// Sort the array
659 void sort(const CHARSET_INFO *cs = nullptr);
660 /**
661 Check if the given value appears in the array
662
663 @param val value to look for
664
665 @returns
666 true value is found
667 false otherwise
668 */
669 bool binary_search(Json_dom *val);
670
671 /**
672 Sort array and remove duplicate elements.
673 Used by multi-value index implementation.
674 */
675 void remove_duplicates(const CHARSET_INFO *cs);
676
677 friend Json_dom;
678};
679
680/**
681 Abstract base class for all Json scalars.
682*/
683class Json_scalar : public Json_dom {
684 public:
685 uint32 depth() const final { return 1; }
686
687 bool is_scalar() const final { return true; }
688};
689
690/**
691 Represents a JSON string value (ECMA), of type J_STRING here.
692*/
693class Json_string final : public Json_scalar {
694 private:
695 std::string m_str; //!< holds the string
696 public:
697 /*
698 Construct a Json_string object.
699 @param args any arguments accepted by std::string's constructors
700 */
701 template <typename... Args>
702 explicit Json_string(Args &&... args)
703 : Json_scalar(), m_str(std::forward<Args>(args)...) {}
704
706
707 Json_dom_ptr clone() const override {
708 return create_dom_ptr<Json_string>(m_str);
709 }
710
711 /**
712 Get the reference to the value of the JSON string.
713 @return the string reference
714 */
715 const std::string &value() const { return m_str; }
716
717 /**
718 Get the number of characters in the string.
719 @return the number of characters
720 */
721 size_t size() const { return m_str.size(); }
722};
723
724/**
725 Abstract base class of all JSON number (ECMA) types (subclasses
726 represent MySQL extensions).
727*/
728class Json_number : public Json_scalar {
729 public:
730 bool is_number() const final { return true; }
731};
732
733/**
734 Represents a MySQL decimal number, type J_DECIMAL.
735*/
736class Json_decimal final : public Json_number {
737 private:
738 my_decimal m_dec; //!< holds the decimal number
739
740 public:
742
743 explicit Json_decimal(const my_decimal &value);
744
745 /**
746 Get the number of bytes needed to store this decimal in a Json_opaque.
747 @return the number of bytes.
748 */
749 int binary_size() const;
750
751 /**
752 Get the binary representation of the wrapped my_decimal, so that this
753 value can be stored inside of a Json_opaque.
754
755 @param dest the destination buffer to which the binary representation
756 is written
757 @return false on success, true on error
758 */
759 bool get_binary(char *dest) const;
760
761 enum_json_type json_type() const override {
763 }
764
765 /**
766 Get a pointer to the MySQL decimal held by this object. Ownership
767 is _not_ transferred.
768 @return the decimal
769 */
770 const my_decimal *value() const { return &m_dec; }
771
772 Json_dom_ptr clone() const override {
773 return create_dom_ptr<Json_decimal>(m_dec);
774 }
775
776 /**
777 Convert a binary value produced by get_binary() back to a my_decimal.
778
779 @details
780 This and two next functions help storage engine to deal with
781 decimal value in a serialized JSON document. This function converts
782 serialized value to my_decimal. The later two functions extract the
783 decimal value from serialized JSON, so SE can index it in multi-valued
784 index.
785
786 @param[in] bin decimal value in binary format
787 @param[in] len length of the binary value
788 @param[out] dec my_decimal object to store the value to
789 @return false on success, true on failure
790 */
791 static bool convert_from_binary(const char *bin, size_t len, my_decimal *dec);
792 /**
793 Returns stored DECIMAL binary
794
795 @param bin serialized Json_decimal object
796
797 @returns
798 pointer to the binary decimal value
799
800 @see #convert_from_binary
801 */
802 static const char *get_encoded_binary(const char *bin) {
803 // Skip stored precision and scale
804 return bin + 2;
805 }
806 /**
807 Returns length of stored DECIMAL binary
808
809 @param length length of serialized Json_decimal object
810
811 @returns
812 length of the binary decimal value
813
814 @see #convert_from_binary
815 */
816 static size_t get_encoded_binary_len(size_t length) {
817 // Skip stored precision and scale
818 return length - 2;
819 }
820};
821
822/**
823 Represents a MySQL double JSON scalar (an extension of the ECMA
824 number value), type J_DOUBLE.
825*/
826class Json_double final : public Json_number {
827 private:
828 double m_f; //!< holds the double value
829 public:
830 explicit Json_double(double value) : Json_number(), m_f(value) {}
831
833
834 Json_dom_ptr clone() const override {
835 return create_dom_ptr<Json_double>(m_f);
836 }
837
838 /**
839 Return the double value held by this object.
840 @return the value
841 */
842 double value() const { return m_f; }
843};
844
845/**
846 Represents a MySQL integer (64 bits signed) JSON scalar (an extension
847 of the ECMA number value), type J_INT.
848*/
849class Json_int final : public Json_number {
850 private:
851 longlong m_i; //!< holds the value
852 public:
854
855 enum_json_type json_type() const override { return enum_json_type::J_INT; }
856
857 /**
858 Return the signed int held by this object.
859 @return the value
860 */
861 longlong value() const { return m_i; }
862
863 /**
864 @return true if the number can be held by a 16 bit signed integer
865 */
866 bool is_16bit() const { return INT_MIN16 <= m_i && m_i <= INT_MAX16; }
867
868 /**
869 @return true if the number can be held by a 32 bit signed integer
870 */
871 bool is_32bit() const { return INT_MIN32 <= m_i && m_i <= INT_MAX32; }
872
873 Json_dom_ptr clone() const override { return create_dom_ptr<Json_int>(m_i); }
874};
875
876/**
877 Represents a MySQL integer (64 bits unsigned) JSON scalar (an extension
878 of the ECMA number value), type J_UINT.
879*/
880
881class Json_uint final : public Json_number {
882 private:
883 ulonglong m_i; //!< holds the value
884 public:
886
888
889 /**
890 Return the unsigned int held by this object.
891 @return the value
892 */
893 ulonglong value() const { return m_i; }
894
895 /**
896 @return true if the number can be held by a 16 bit unsigned
897 integer.
898 */
899 bool is_16bit() const { return m_i <= UINT_MAX16; }
900
901 /**
902 @return true if the number can be held by a 32 bit unsigned
903 integer.
904 */
905 bool is_32bit() const { return m_i <= UINT_MAX32; }
906
907 Json_dom_ptr clone() const override { return create_dom_ptr<Json_uint>(m_i); }
908};
909
910/**
911 Represents a JSON null type (ECMA), type J_NULL here.
912*/
913class Json_null final : public Json_scalar {
914 public:
916 Json_dom_ptr clone() const override { return create_dom_ptr<Json_null>(); }
917};
918
919/**
920 Represents a MySQL date/time value (DATE, TIME, DATETIME or
921 TIMESTAMP) - an extension to the ECMA set of JSON scalar types, types
922 J_DATE, J_TIME, J_DATETIME and J_TIMESTAMP respectively. The method
923 field_type identifies which of the four it is.
924*/
925class Json_datetime final : public Json_scalar {
926 private:
927 MYSQL_TIME m_t; //!< holds the date/time value
928 enum_field_types m_field_type; //!< identifies which type of date/time
929
930 public:
931 /**
932 Constructs a object to hold a MySQL date/time value.
933
934 @param[in] t the time/value
935 @param[in] ft the field type: must be one of MYSQL_TYPE_TIME,
936 MYSQL_TYPE_DATE, MYSQL_TYPE_DATETIME or
937 MYSQL_TYPE_TIMESTAMP.
938 */
940 : Json_scalar(), m_t(t), m_field_type(ft) {}
941
942 enum_json_type json_type() const override;
943
944 Json_dom_ptr clone() const override;
945
946 /**
947 Return a pointer the date/time value. Ownership is _not_ transferred.
948 To identify which time time the value represents, use @c field_type.
949 @return the pointer
950 */
951 const MYSQL_TIME *value() const { return &m_t; }
952
953 /**
954 Return what kind of date/time value this object holds.
955 @return One of MYSQL_TYPE_TIME, MYSQL_TYPE_DATE, MYSQL_TYPE_DATETIME
956 or MYSQL_TYPE_TIMESTAMP.
957 */
959
960 /**
961 Convert the datetime to the packed format used when storing
962 datetime values.
963 @param dest the destination buffer to write the packed datetime to
964 (must at least have size PACKED_SIZE)
965 */
966 void to_packed(char *dest) const;
967
968 /**
969 Convert a packed datetime back to a MYSQL_TIME.
970 @param from the buffer to read from (must have at least PACKED_SIZE bytes)
971 @param ft the field type of the value
972 @param to the MYSQL_TIME to write the value to
973 */
974 static void from_packed(const char *from, enum_field_types ft,
975 MYSQL_TIME *to);
976
977#ifdef MYSQL_SERVER
978 /**
979 Convert a packed datetime to key string for indexing by SE
980 @param from the buffer to read from
981 @param ft the field type of the value
982 @param to the destination buffer
983 @param dec value's decimals
984 */
985 static void from_packed_to_key(const char *from, enum_field_types ft,
986 uchar *to, uint8 dec);
987#endif
988
989 /** Datetimes are packed in eight bytes. */
990 static const size_t PACKED_SIZE = 8;
991};
992
993/**
994 Represents a MySQL value opaquely, i.e. the Json DOM can not
995 serialize or deserialize these values. This should be used to store
996 values that don't map to the other Json_scalar classes. Using the
997 "to_string" method on such values (via Json_wrapper) will yield a base
998 64 encoded string tagged with the MySQL type with this syntax:
999
1000 "base64:typeXX:<base 64 encoded value>"
1001*/
1002class Json_opaque final : public Json_scalar {
1003 private:
1005 std::string m_val;
1006
1007 public:
1008 /**
1009 An opaque MySQL value.
1010
1011 @param[in] mytype the MySQL type of the value
1012 @param[in] args arguments to construct the binary value to be stored
1013 in the DOM (anything accepted by the std::string
1014 constructors)
1015 @see #enum_field_types
1016 @see Class documentation
1017 */
1018 template <typename... Args>
1019 explicit Json_opaque(enum_field_types mytype, Args &&... args)
1020 : Json_scalar(), m_mytype(mytype), m_val(std::forward<Args>(args)...) {}
1021
1023
1024 /**
1025 @return a pointer to the opaque value. Use #size() to get its size.
1026 */
1027 const char *value() const { return m_val.data(); }
1028
1029 /**
1030 @return the MySQL type of the value
1031 */
1032 enum_field_types type() const { return m_mytype; }
1033 /**
1034 @return the size in bytes of the value
1035 */
1036 size_t size() const { return m_val.size(); }
1037
1038 Json_dom_ptr clone() const override;
1039};
1040
1041/**
1042 Represents a JSON true or false value, type J_BOOLEAN here.
1043*/
1044class Json_boolean final : public Json_scalar {
1045 private:
1046 bool m_v; //!< false or true: represents the eponymous JSON literal
1047 public:
1048 explicit Json_boolean(bool value) : Json_scalar(), m_v(value) {}
1049
1050 enum_json_type json_type() const override {
1052 }
1053
1054 /**
1055 @return false for JSON false, true for JSON true
1056 */
1057 bool value() const { return m_v; }
1058
1059 Json_dom_ptr clone() const override {
1060 return create_dom_ptr<Json_boolean>(m_v);
1061 }
1062};
1063
1064/**
1065 Perform quoting on a JSON string to make an external representation
1066 of it. It wraps double quotes (text quotes) around the string (cptr)
1067 and also performs escaping according to the following table:
1068 <pre>
1069 @verbatim
1070 Common name C-style Original unescaped Transformed to
1071 escape UTF-8 bytes escape sequence
1072 notation in UTF-8 bytes
1073 ---------------------------------------------------------------
1074 quote \" %x22 %x5C %x22
1075 backslash \\ %x5C %x5C %x5C
1076 backspace \b %x08 %x5C %x62
1077 formfeed \f %x0C %x5C %x66
1078 linefeed \n %x0A %x5C %x6E
1079 carriage-return \r %x0D %x5C %x72
1080 tab \t %x09 %x5C %x74
1081 unicode \uXXXX A hex number in the %x5C %x75
1082 range of 00-1F, followed by
1083 except for the ones 4 hex digits
1084 handled above (backspace,
1085 formfeed, linefeed,
1086 carriage-return,
1087 and tab).
1088 ---------------------------------------------------------------
1089 @endverbatim
1090 </pre>
1091
1092 @param[in] cptr pointer to string data
1093 @param[in] length the length of the string
1094 @param[in,out] buf the destination buffer
1095 @retval true on error
1096*/
1097bool double_quote(const char *cptr, size_t length, String *buf);
1098
1099/**
1100 Merge two doms. The right dom is either subsumed into the left dom
1101 or the contents of the right dom are transferred to the left dom
1102 and the right dom is deleted. After calling this function, the
1103 caller should not reference the right dom again. It has been
1104 deleted.
1105
1106 Returns NULL if there is a memory allocation failure. In this case
1107 both doms are deleted.
1108
1109 scalars - If any of the documents that are being merged is a scalar,
1110 each scalar document is autowrapped as a single value array before merging.
1111
1112 arrays - When merging a left array with a right array,
1113 then the result is the left array concatenated
1114 with the right array. For instance, [ 1, 2 ] merged with [ 3, 4 ]
1115 is [ 1, 2, 3, 4 ].
1116
1117 array and object - When merging an array with an object,
1118 the object is autowrapped as an array and then the rule above
1119 is applied. So [ 1, 2 ] merged with { "a" : true }
1120 is [ 1, 2, { "a": true } ].
1121
1122 objects - When merging two objects, the two objects are concatenated
1123 into a single, larger object. So { "a" : "foo" } merged with { "b" : 5 }
1124 is { "a" : "foo", "b" : 5 }.
1125
1126 duplicates - When two objects are merged and they share a key,
1127 the values associated with the shared key are merged.
1128
1129 @param [in,out] left The recipient dom.
1130 @param [in,out] right The dom to be consumed
1131
1132 @return A composite dom which subsumes the left and right doms, or NULL
1133 if a failure happened while merging
1134*/
1136
1137/**
1138 How Json_wrapper would handle coercion error
1139*/
1140
1142 CE_WARNING, // Throw a warning, default
1143 CE_ERROR, // Throw an error
1144 CE_IGNORE // Let the caller handle the error
1146
1147/**
1148 Abstraction for accessing JSON values irrespective of whether they
1149 are (started out as) binary JSON values or JSON DOM values. The
1150 purpose of this is to allow uniform access for callers. It allows us
1151 to access binary JSON values without necessarily building a DOM (and
1152 thus having to read the entire value unless necessary, e.g. for
1153 accessing only a single array slot or object field).
1154
1155 Instances of this class are usually created on the stack. In some
1156 cases instances are cached in an Item and reused, in which case they
1157 are allocated from query-duration memory (by allocating them on a
1158 MEM_ROOT).
1159*/
1161 private:
1162 /*
1163 A Json_wrapper wraps either a Json_dom or a json_binary::Value,
1164 never both at the same time.
1165 */
1166 union {
1167 /// The DOM representation, only used if m_is_dom is true.
1168 struct {
1170 /// If true, don't deallocate m_dom_value in destructor.
1173 /// The binary representation, only used if m_is_dom is false.
1175 };
1176 bool m_is_dom; //!< Wraps a DOM iff true
1177 public:
1178 /**
1179 Get the wrapped datetime value in the packed format.
1180
1181 @param[in,out] buffer a char buffer with space for at least
1182 Json_datetime::PACKED_SIZE characters
1183 @return a char buffer that contains the packed representation of the
1184 datetime (may or may not be the same as buffer)
1185 */
1186 const char *get_datetime_packed(char *buffer) const;
1187
1188 /**
1189 Create an empty wrapper. Cf #empty().
1190 */
1191 Json_wrapper() : m_dom{nullptr, true}, m_is_dom(true) {}
1192
1193 /**
1194 Wrap the supplied DOM value (no copy taken). The wrapper takes
1195 ownership, unless alias is true or @c set_alias is called after
1196 construction.
1197 In the latter case the lifetime of the DOM is determined by
1198 the owner of the DOM, so clients need to ensure that that
1199 lifetime is sufficient, lest dead storage is attempted accessed.
1200
1201 @param[in,out] dom_value the DOM value
1202 @param alias Whether the wrapper is an alias to DOM
1203 */
1204 explicit Json_wrapper(Json_dom *dom_value, bool alias = false);
1205
1206 /**
1207 Wrap the supplied DOM value. The wrapper takes over the ownership.
1208 */
1209 explicit Json_wrapper(Json_dom_ptr dom_value)
1210 : Json_wrapper(dom_value.release()) {}
1211
1212 /**
1213 Only meaningful iff the wrapper encapsulates a DOM. Marks the
1214 wrapper as not owning the DOM object, i.e. it will not be
1215 deallocated in the wrapper's destructor. Useful if one wants a wrapper
1216 around a DOM owned by someone else.
1217 */
1218 void set_alias() { m_dom.m_alias = true; }
1219
1220 /**
1221 Wrap a binary value. Does not copy the underlying buffer, so
1222 lifetime is limited the that of the supplied value.
1223
1224 @param[in] value the binary value
1225 */
1226 explicit Json_wrapper(const json_binary::Value &value);
1227
1228 /**
1229 Copy constructor. Does a deep copy of any owned DOM. If a DOM
1230 os not owned (aliased), the copy will also be aliased.
1231 */
1232 Json_wrapper(const Json_wrapper &old);
1233
1234 /**
1235 Move constructor. Take over the ownership of the other wrapper's
1236 DOM, unless it's aliased. If the other wrapper is aliased, this
1237 wrapper becomes an alias too. Any already owned DOM will be
1238 deallocated.
1239
1240 @param old the wrapper whose contents to take over
1241 */
1242 Json_wrapper(Json_wrapper &&old) noexcept;
1243
1244 /**
1245 Assignment operator. Does a deep copy of any owned DOM. If a DOM
1246 os not owned (aliased), the copy will also be aliased. Any owned
1247 DOM in the left side will be deallocated.
1248 */
1249 Json_wrapper &operator=(const Json_wrapper &old);
1250
1251 /**
1252 Move-assignment operator. Take over the ownership of the other
1253 wrapper's DOM, unless it's aliased. If the other wrapper is
1254 aliased, this wrapper becomes an alias too. Any already owned DOM
1255 will be deallocated.
1256
1257 @param old the wrapper whose contents to take over
1258 */
1259 Json_wrapper &operator=(Json_wrapper &&old) noexcept;
1260
1261 ~Json_wrapper();
1262
1263 /**
1264 A Wrapper is defined to be empty if it is passed a NULL value with the
1265 constructor for JSON dom, or if the default constructor is used.
1266
1267 @return true if the wrapper is empty.
1268 */
1269 bool empty() const { return m_is_dom && !m_dom.m_value; }
1270
1271 /**
1272 Does this wrapper contain a DOM?
1273
1274 @retval true if the wrapper contains a DOM representation
1275 @retval false if the wrapper contains a binary representation
1276 */
1277 bool is_dom() const { return m_is_dom; }
1278
1279 /**
1280 Get the wrapped contents in DOM form. The DOM is (still) owned by the
1281 wrapper. If this wrapper originally held a value, it is now converted
1282 to hold (and eventually release) the DOM version.
1283
1284 @return pointer to a DOM object, or NULL if the DOM could not be allocated
1285 */
1286 Json_dom *to_dom();
1287
1288 /**
1289 Gets a pointer to the wrapped Json_dom object, if this wrapper holds a DOM.
1290 If is_dom() returns false, the result of calling this function is undefined.
1291 */
1292 const Json_dom *get_dom() const {
1293 assert(m_is_dom);
1294 return m_dom.m_value;
1295 }
1296
1297 /**
1298 Gets the wrapped json_binary::Value object, if this wrapper holds a binary
1299 JSON value. If is_dom() returns true, the result of calling this function is
1300 undefined.
1301 */
1303 assert(!m_is_dom);
1304 return m_value;
1305 }
1306
1307 /**
1308 Get the wrapped contents in DOM form. Same as to_dom(), except it returns
1309 a clone of the original DOM instead of the actual, internal DOM tree.
1310
1311 @return pointer to a DOM object, or NULL if the DOM could not be allocated
1312 */
1313 Json_dom_ptr clone_dom() const;
1314
1315 /**
1316 Get the wrapped contents in binary value form.
1317
1318 @param[in] thd current session
1319 @param[in,out] str a string that will be filled with the binary value
1320 @retval false on success
1321 @retval true on error
1322 */
1323 bool to_binary(const THD *thd, String *str) const;
1324
1325 /**
1326 Check if the wrapped JSON document is a binary value (a
1327 json_binary::Value), and if that binary is pointing to data stored in the
1328 given string.
1329
1330 This function can be used to check if overwriting the data in the string
1331 might overwrite and corrupt the document contained in this wrapper.
1332
1333 @param str a string which contains JSON binary data
1334 @retval true if the string contains data that the wrapped document
1335 points to from its json_binary::Value representation
1336 @retval false otherwise
1337 */
1338 bool is_binary_backed_by(const String *str) const {
1339 return !m_is_dom && m_value.is_backed_by(str);
1340 }
1341
1342 /**
1343 Format the JSON value to an external JSON string in buffer in
1344 the format of ISO/IEC 10646.
1345
1346 @param[in,out] buffer the formatted string is appended, so make sure
1347 the length is set correctly before calling
1348 @param[in] json_quoted if the JSON value is a string and json_quoted
1349 is false, don't perform quoting on the string.
1350 This is only used by JSON_UNQUOTE.
1351 @param[in] func_name The name of the function that called to_string().
1352
1353 @return false formatting went well, else true
1354 */
1355 bool to_string(String *buffer, bool json_quoted, const char *func_name,
1356 const JsonDocumentDepthHandler &depth_handler) const;
1357
1358 /**
1359 Print this JSON document to the debug trace.
1360
1361 @param[in] message If given, the JSON document is prefixed with
1362 this message.
1363 */
1364 void dbug_print(const char *message,
1365 const JsonDocumentDepthHandler &depth_handler) const;
1366
1367 /**
1368 Format the JSON value to an external JSON string in buffer in the format of
1369 ISO/IEC 10646. Add newlines and indentation for readability.
1370
1371 @param[in,out] buffer the buffer that receives the formatted string
1372 (the string is appended, so make sure the length
1373 is set correctly before calling)
1374 @param[in] func_name the name of the calling function
1375
1376 @retval false on success
1377 @retval true on error
1378 */
1379 bool to_pretty_string(String *buffer, const char *func_name,
1380 const JsonDocumentDepthHandler &depth_handler) const;
1381
1382 // Accessors
1383
1384 /**
1385 Return the type of the wrapped JSON value
1386
1387 @return the type, or Json_dom::J_ERROR if the wrapper does not contain
1388 a JSON value
1389 */
1390 enum_json_type type() const;
1391
1392 /**
1393 Return the MYSQL type of the opaque value, see #type(). Valid for
1394 J_OPAQUE. Calling this method if the type is not J_OPAQUE will give
1395 undefined results.
1396
1397 @return the type
1398 */
1400
1401 /**
1402 If this wrapper holds a JSON array, get an array value by indexing
1403 into the array. Valid for J_ARRAY. Calling this method if the type is
1404 not J_ARRAY will give undefined results.
1405
1406 @return the array value
1407 */
1408 Json_wrapper operator[](size_t index) const;
1409
1410 /**
1411 If this wrapper holds a JSON object, get the value corresponding
1412 to the member key. Valid for J_OBJECT. Calling this method if the type is
1413 not J_OBJECT will give undefined results.
1414
1415 @param[in] key name for identifying member
1416
1417 @return The member value. If there is no member with the specified
1418 name, a value with type Json_dom::J_ERROR is returned.
1419 */
1421
1422 /**
1423 Get a pointer to the data of a JSON string or JSON opaque value.
1424 The data is still owner by the wrapper. The data may not be null
1425 terminated, so use in conjunction with @c get_data_length.
1426 Valid for J_STRING and J_OPAQUE. Calling this method if the type is
1427 not one of those will give undefined results.
1428
1429 @return the pointer
1430 */
1431 const char *get_data() const;
1432
1433 /**
1434 Get the length to the data of a JSON string or JSON opaque value.
1435 Valid for J_STRING and J_OPAQUE. Calling this method if the type is
1436 not one of those will give undefined results.
1437
1438 @return the length
1439 */
1440 size_t get_data_length() const;
1441
1442 /**
1443 Get the MySQL representation of a JSON decimal value.
1444 Valid for J_DECIMAL. Calling this method if the type is
1445 not J_DECIMAL will give undefined results.
1446
1447 @param[out] d the decimal value
1448 @return false on success, true on failure (which would indicate an
1449 internal error)
1450 */
1451 bool get_decimal_data(my_decimal *d) const;
1452
1453 /**
1454 Get the value of a JSON double number.
1455 Valid for J_DOUBLE. Calling this method if the type is
1456 not J_DOUBLE will give undefined results.
1457
1458 @return the value
1459 */
1460 double get_double() const;
1461
1462 /**
1463 Get the value of a JSON signed integer number.
1464 Valid for J_INT. Calling this method if the type is
1465 not J_INT will give undefined results.
1466
1467 @return the value
1468 */
1469 longlong get_int() const;
1470
1471 /**
1472 Get the value of a JSON unsigned integer number.
1473 Valid for J_UINT. Calling this method if the type is
1474 not J_UINT will give undefined results.
1475
1476 @return the value
1477 */
1478 ulonglong get_uint() const;
1479
1480 /**
1481 Get the value of a JSON date/time value. Valid for J_TIME,
1482 J_DATETIME, J_DATE and J_TIMESTAMP. Calling this method if the type
1483 is not one of those will give undefined results.
1484
1485 @param[out] t the date/time value
1486 */
1487 void get_datetime(MYSQL_TIME *t) const;
1488
1489 /**
1490 Get a boolean value (a JSON true or false literal).
1491 Valid for J_BOOLEAN. Calling this method if the type is
1492 not J_BOOLEAN will give undefined results.
1493
1494 @return the value
1495 */
1496 bool get_boolean() const;
1497
1498 /**
1499 Finds all of the json sub-documents which match the path expression.
1500 Puts the matches on an evolving vector of results.
1501 This is a bit inefficient for binary wrappers because you can't
1502 build up a binary array incrementally from its cells. Instead, you
1503 have to turn each cell into a dom and then add the doms to a
1504 dom array.
1505
1506 Calling this if #empty() returns true is an error.
1507
1508 Special care must be taken when the path expression contains more than one
1509 ellipsis (**) token. That is because multiple paths with ellipses may
1510 identify the same value. Consider the following document:
1511
1512 { "a": { "x" : { "b": { "y": { "b": { "z": { "c": 100 } } } } } } }
1513
1514 The innermost value (the number 100) has the following unique,
1515 non-wildcarded address:
1516
1517 $.a.x.b.y.b.z.c
1518
1519 That location is reached by both of the following paths which include
1520 the ellipsis token:
1521
1522 $.a.x.b**.c
1523 $.a.x.b.y.b**.c
1524
1525 And those addresses both satisfy the following path expression which has
1526 two ellipses:
1527
1528 $.a**.b**.c
1529
1530 In this case, we only want to return one instance of $.a.x.b.y.b.z.c
1531
1532 Similarly, special care must be taken if an auto-wrapping array
1533 path leg follows an ellipsis. Consider the following document:
1534
1535 { "a": { "b" : [ 1, 2, 3 ] } }
1536
1537 The first element of the array (the number 1) can be reached with
1538 either of these two non-wildcarded addresses, due to array auto-wrapping:
1539
1540 $.a.b[0]
1541 $.a.b[0][0]
1542
1543 Both of those addresses match the following path expression, which
1544 has an ellipsis followed by an auto-wrapping path leg:
1545
1546 $**[0]
1547
1548 @param[in] path the (possibly wildcarded) address of the sub-documents
1549 @param[in] legs the number of legs to use from @a path
1550 @param[out] hits the result of the search
1551 @param[in] auto_wrap true of we match a final scalar with search for [0]
1552 @param[in] only_need_one True if we can stop after finding one match
1553
1554 @retval false on success
1555 @retval true on error
1556 */
1557 bool seek(const Json_seekable_path &path, size_t legs,
1558 Json_wrapper_vector *hits, bool auto_wrap, bool only_need_one);
1559
1560 /**
1561 Compute the length of a document. This is the value which would be
1562 returned by the JSON_LENGTH() system function. So, this returns
1563
1564 - for scalar values: 1
1565 - for objects: the number of members
1566 - for arrays: the number of cells
1567
1568 @returns 1, the number of members, or the number of cells
1569 */
1570 size_t length() const;
1571
1572 /**
1573 Compare this JSON value to another JSON value.
1574 @param[in] other the other JSON value
1575 @param[in] cs if given, this charset will be used in comparison of
1576 string values
1577 @retval -1 if this JSON value is less than the other JSON value
1578 @retval 0 if the two JSON values are equal
1579 @retval 1 if this JSON value is greater than the other JSON value
1580 */
1581 int compare(const Json_wrapper &other,
1582 const CHARSET_INFO *cs = nullptr) const;
1583
1584 /**
1585 Extract an int (signed or unsigned) from the JSON if possible
1586 coercing if need be.
1587 @param[in] msgnam to use in error message if conversion failed
1588 @param[in] cr_error Whether to raise an error or warning on
1589 data truncation
1590 @param[out] err true <=> error occur during coercion
1591 @param[out] unsigned_flag Whether the value read from JSON data is
1592 unsigned
1593
1594 @returns json value coerced to int
1595 */
1596 longlong coerce_int(const char *msgnam, enum_coercion_error cr_error,
1597 bool *err, bool *unsigned_flag) const;
1598
1599 /// Shorthand for coerce_int(msgnam, CE_WARNING, nullptr, nullptr).
1600 longlong coerce_int(const char *msgnam) const {
1601 return coerce_int(msgnam, CE_WARNING, nullptr, nullptr);
1602 }
1603
1604 /**
1605 Extract a real from the JSON if possible, coercing if need be.
1606
1607 @param[in] msgnam to use in error message if conversion failed
1608 @param[in] cr_error Whether to raise an error or warning on
1609 data truncation
1610 @param[out] err true <=> error occur during coercion
1611 @returns json value coerced to real
1612 */
1613 double coerce_real(const char *msgnam, enum_coercion_error cr_error,
1614 bool *err) const;
1615
1616 /// Shorthand for coerce_real(msgnam, CE_WARNING, nullptr).
1617 double coerce_real(const char *msgnam) const {
1618 return coerce_real(msgnam, CE_WARNING, nullptr);
1619 }
1620
1621 /**
1622 Extract a decimal from the JSON if possible, coercing if need be.
1623
1624 @param[in,out] decimal_value a value buffer
1625 @param[in] msgnam to use in error message if conversion failed
1626 @param[in] cr_error Whether to raise an error or warning on
1627 data truncation
1628 @param[out] err true <=> error occur during coercion
1629 @returns json value coerced to decimal
1630 */
1631 my_decimal *coerce_decimal(my_decimal *decimal_value, const char *msgnam,
1632 enum_coercion_error cr_error, bool *err) const;
1633
1634 /// Shorthand for coerce_decimal(decimal_value, msgnam, CE_WARNING, nullptr).
1635 my_decimal *coerce_decimal(my_decimal *decimal_value, const char *msgnam) {
1636 return coerce_decimal(decimal_value, msgnam, CE_WARNING, nullptr);
1637 }
1638
1639 /**
1640 Extract a date from the JSON if possible, coercing if need be.
1641
1642 @param[in,out] ltime a value buffer
1643 @param msgnam to use in error message if conversion failed
1644 @param[in] cr_error Whether to raise an error or warning on
1645 data truncation
1646 @param[in] date_flags_arg Flags to use for string -> date conversion
1647 @returns json value coerced to date
1648 */
1649 bool coerce_date(MYSQL_TIME *ltime, const char *msgnam,
1651 my_time_flags_t date_flags_arg = 0) const;
1652
1653 /**
1654 Extract a time value from the JSON if possible, coercing if need be.
1655
1656 @param[in,out] ltime a value buffer
1657 @param msgnam to use in error message if conversion failed
1658 @param[in] cr_error Whether to raise an error or warning on
1659 data truncation
1660
1661 @returns json value coerced to time
1662 */
1663 bool coerce_time(MYSQL_TIME *ltime, const char *msgnam,
1664 enum_coercion_error cr_error = CE_WARNING) const;
1665
1666 /**
1667 Make a sort key that can be used by filesort to order JSON values.
1668
1669 @param[out] to a buffer to which the sort key is written
1670 @param[in] length the length of the sort key
1671
1672 @details Key storage format is following:
1673 @verbatim
1674 |<json type>< sort key >|
1675 1 byte / variable length /
1676 @endverbatim
1677
1678 JSON is assumed to be non-sql-null and valid (checked by caller).
1679 Key length contains full length - the len prefix itself, json type and the
1680 sort key.
1681 All numeric types are stored as a number, without distinction to
1682 double/decimal/int/etc. See @c make_json_numeric_sort_key().
1683 Same is done to DATETIME and TIMESTAMP types.
1684 For string and opaque types only the prefix that fits into the output buffer
1685 is stored.
1686 For JSON objects and arrays only their length (number of elements) is
1687 stored, this is a limitation of current implementation.
1688 */
1689 size_t make_sort_key(uchar *to, size_t length) const;
1690
1691 /**
1692 Make a hash key that can be used by sql_executor.cc/unique_hash
1693 in order to support SELECT DISTINCT
1694
1695 @param[in] hash_val An initial hash value.
1696 */
1697 ulonglong make_hash_key(ulonglong hash_val) const;
1698
1699 /**
1700 Calculate the amount of unused space inside a JSON binary value.
1701
1702 @param[out] space the amount of unused space, or zero if this is a DOM
1703 @return false on success
1704 @return true if the JSON binary value was invalid
1705 */
1706 bool get_free_space(size_t *space) const;
1707
1708 /**
1709 Attempt a binary partial update by replacing the value at @a path with @a
1710 new_value. On successful completion, the updated document will be available
1711 in @a result, and this Json_wrapper will point to @a result instead of the
1712 original binary representation. The modifications that have been applied,
1713 will also be collected as binary diffs, which can be retrieved via
1714 TABLE::get_binary_diffs().
1715
1716 @param field the column being updated
1717 @param path the path of the value to update
1718 @param new_value the new value
1719 @param replace true if we use JSON_REPLACE semantics
1720 @param[in,out] result buffer that holds the updated JSON document (is
1721 empty if no partial update has been performed on
1722 this Json_wrapper so far, or contains the binary
1723 representation of the document in this wrapper
1724 otherwise)
1725 @param[out] partially_updated gets set to true if partial update was
1726 successful, also if it was a no-op
1727 @param[out] replaced_path gets set to true if the path was replaced,
1728 will be false if this update is a no-op
1729
1730 @retval false if the update was successful, or if it was determined
1731 that a full update was needed
1732 @retval true if an error occurred
1733 */
1734 bool attempt_binary_update(const Field_json *field,
1735 const Json_seekable_path &path,
1736 Json_wrapper *new_value, bool replace,
1737 String *result, bool *partially_updated,
1738 bool *replaced_path);
1739
1740 /**
1741 Remove a path from a binary JSON document. On successful completion, the
1742 updated document will be available in @a result, and this Json_wrapper will
1743 point to @a result instead of the original binary representation. The
1744 modifications that have been applied, will also be collected as binary
1745 diffs, which can be retrieved via TABLE::get_binary_diffs().
1746
1747 @param field the column being updated
1748 @param path the path to remove from the document
1749 @param[in,out] result buffer that holds the updated JSON document (is
1750 empty if no partial update has been performed on
1751 this Json_wrapper so far, or contains the binary
1752 representation of the document in this wrapper
1753 otherwise)
1754 @param[out] found_path gets set to true if the path is found in the
1755 document, false otherwise
1756
1757 @retval false if the value was successfully updated
1758 @retval true if an error occurred
1759 */
1760 bool binary_remove(const Field_json *field, const Json_seekable_path &path,
1761 String *result, bool *found_path);
1762#ifdef MYSQL_SERVER
1763 /**
1764 Sort contents. Applicable to JSON arrays only.
1765 */
1766 void sort(const CHARSET_INFO *cs = nullptr);
1767 /**
1768 Remove duplicate values. Applicable to JSON arrays only, array will be
1769 sorted.
1770 */
1771 void remove_duplicates(const CHARSET_INFO *cs = nullptr);
1772#endif
1773};
1774
1775/**
1776 Class that iterates over all members of a JSON object that is wrapped in a
1777 Json_wrapper instance.
1778*/
1780 public:
1781 // Type aliases required by ForwardIterator.
1782 using value_type = std::pair<MYSQL_LEX_CSTRING, Json_wrapper>;
1783 using reference = const value_type &;
1784 using pointer = const value_type *;
1785 using difference_type = ptrdiff_t;
1786 using iterator_category = std::forward_iterator_tag;
1787
1788 /**
1789 Creates an iterator that iterates over all members of the given
1790 Json_wrapper, if it wraps a JSON object. If the wrapper does not wrap a JSON
1791 object, the result is undefined.
1792
1793 @param wrapper the Json_wrapper to iterate over
1794 @param begin true to construct an iterator that points to the first member
1795 of the object, false to construct a past-the-end iterator
1796 */
1797 Json_wrapper_object_iterator(const Json_wrapper &wrapper, bool begin);
1798
1799 /// Forward iterators must be default constructible.
1801
1802 /// Advances the iterator to the next element.
1804 if (is_dom())
1805 ++m_iter;
1806 else
1809 return *this;
1810 }
1811
1812 /**
1813 Advances the iterator to the next element and returns an iterator that
1814 points to the current element (post-increment operator).
1815 */
1818 ++(*this);
1819 return copy;
1820 }
1821
1822 /// Checks two iterators for equality.
1823 bool operator==(const Json_wrapper_object_iterator &other) const {
1824 return is_dom() ? m_iter == other.m_iter
1826 }
1827
1828 /// Checks two iterators for inequality.
1829 bool operator!=(const Json_wrapper_object_iterator &other) const {
1830 return !(*this == other);
1831 }
1832
1835 return &m_current_member;
1836 }
1837
1838 reference operator*() { return *this->operator->(); }
1839
1840 private:
1841 /// Pair holding the key and value of the member pointed to by the iterator.
1843 /// True if #m_current_member is initialized.
1845 /// The binary JSON object being iterated over, or nullptr for DOMs.
1847 /// The index of the current member in the binary JSON object.
1849 /// Iterator pointing to the current member in the JSON DOM object.
1851 /// Returns true if iterating over a DOM.
1852 bool is_dom() const { return m_binary_value == nullptr; }
1853 /// Fill #m_current_member with the key and value of the current member.
1855};
1856
1857/**
1858 A wrapper over a JSON object which provides an interface that can be iterated
1859 over with a for-each loop.
1860*/
1862 public:
1864 explicit Json_object_wrapper(const Json_wrapper &wrapper)
1865 : m_wrapper(wrapper) {}
1867 const_iterator cend() const { return const_iterator(m_wrapper, false); }
1868 const_iterator begin() const { return cbegin(); }
1869 const_iterator end() const { return cend(); }
1870
1871 private:
1873};
1874
1875/**
1876 Check if a string contains valid JSON text, without generating a
1877 Json_dom representation of the document.
1878
1879 @param[in] text pointer to the beginning of the string
1880 @param[in] length the length of the string
1881 @return true if the string is valid JSON text, false otherwise
1882*/
1883bool is_valid_json_syntax(const char *text, size_t length);
1884
1885/**
1886 A class that is capable of holding objects of any sub-type of
1887 Json_scalar. Used for pre-allocating space in query-duration memory
1888 for JSON scalars that are to be returned by get_json_atom_wrapper().
1889
1890 This class should be replaced by std::variant when moving to C++17.
1891*/
1893 /// Union of all concrete subclasses of Json_scalar.
1904 /// Constructor which initializes the union to hold a Json_null value.
1906 /// Destructor which delegates to Json_scalar's virtual destructor.
1908 // All members have the same address, and all members are sub-types of
1909 // Json_scalar, so we can take the address of an arbitrary member and
1910 // convert it to Json_scalar.
1911 Json_scalar *scalar = &m_null;
1912 scalar->~Json_scalar();
1913 }
1914 };
1915
1916 /// The buffer in which the Json_scalar value is stored.
1918
1919 /// Pointer to the held scalar, or nullptr if no value is held.
1921
1922 public:
1923 /// Get a pointer to the held object, or nullptr if there is none.
1925
1926 /**
1927 Construct a new Json_scalar value in this Json_scalar_holder.
1928 If a value is already held, the old value is destroyed and replaced.
1929 @tparam T which type of Json_scalar to create
1930 @param args the arguments to T's constructor
1931 */
1932 template <typename T, typename... Args>
1933 void emplace(Args &&... args) {
1934 static_assert(std::is_base_of<Json_scalar, T>::value, "Not a Json_scalar");
1935 static_assert(sizeof(T) <= sizeof(m_buffer), "Buffer is too small");
1937 m_scalar_ptr->~Json_scalar();
1938 ::new (m_scalar_ptr) T(std::forward<Args>(args)...);
1939 }
1940};
1941
1942#endif /* JSON_DOM_INCLUDED */
A field that stores a JSON value.
Definition: field.h:3930
Represents a JSON array container, i.e.
Definition: json_dom.h:519
void sort(const CHARSET_INFO *cs=nullptr)
Sort the array.
Definition: json_dom.cc:1066
Json_dom * operator[](size_t index) const
Get the value at position index.
Definition: json_dom.h:636
Json_array()
Definition: json_dom.cc:964
bool binary_search(Json_dom *val)
Check if the given value appears in the array.
Definition: json_dom.cc:1075
bool remove(size_t index)
Remove the value at this index.
Definition: json_dom.cc:988
const_iterator end() const
Returns a const_iterator that refers past the last element.
Definition: json_dom.h:653
decltype(m_v)::const_iterator const_iterator
Constant iterator over the elements in the JSON array.
Definition: json_dom.h:647
bool append_alias(Json_dom_ptr value)
Append the value to the end of the array and take over the ownership of the value.
Definition: json_dom.h:565
void replace_dom_in_container(const Json_dom *oldv, Json_dom_ptr newv) override
Replace oldv contained inside this container array or object) with newv.
Definition: json_dom.cc:755
bool consume(Json_array_ptr other)
Moves all of the elements in the other array to the end of this array.
Definition: json_dom.cc:966
bool insert_alias(size_t index, Json_dom_ptr value)
Insert the value at position index of the array.
Definition: json_dom.cc:976
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.cc:1006
void clear()
Remove the values in the array.
Definition: json_dom.h:644
size_t size() const
The cardinality of the array (number of values).
Definition: json_dom.h:619
std::vector< Json_dom_ptr, Malloc_allocator< Json_dom_ptr > > m_v
Holds the array values.
Definition: json_dom.h:522
void remove_duplicates(const CHARSET_INFO *cs)
Sort array and remove duplicate elements.
Definition: json_dom.cc:1070
bool append_alias(Json_dom *value)
Append the value to the end of the array.
Definition: json_dom.h:554
friend Json_dom
Definition: json_dom.h:677
bool insert_clone(size_t index, const Json_dom *value)
Insert a clone of the value at position index of the array.
Definition: json_dom.h:588
uint32 depth() const override
Compute the depth of a document.
Definition: json_dom.cc:997
const_iterator begin() const
Returns a const_iterator that refers to the first element.
Definition: json_dom.h:650
bool append_clone(const Json_dom *value)
Append a clone of the value to the end of the array.
Definition: json_dom.h:535
enum_json_type json_type() const override
Definition: json_dom.h:527
Represents a JSON true or false value, type J_BOOLEAN here.
Definition: json_dom.h:1044
bool value() const
Definition: json_dom.h:1057
enum_json_type json_type() const override
Definition: json_dom.h:1050
Json_boolean(bool value)
Definition: json_dom.h:1048
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.h:1059
bool m_v
false or true: represents the eponymous JSON literal
Definition: json_dom.h:1046
Abstract base class of all JSON container types (Json_object and Json_array).
Definition: json_dom.h:329
virtual void replace_dom_in_container(const Json_dom *oldv, Json_dom_ptr newv)=0
Replace oldv contained inside this container array or object) with newv.
Represents a MySQL date/time value (DATE, TIME, DATETIME or TIMESTAMP) - an extension to the ECMA set...
Definition: json_dom.h:925
enum_field_types m_field_type
identifies which type of date/time
Definition: json_dom.h:928
MYSQL_TIME m_t
holds the date/time value
Definition: json_dom.h:927
enum_field_types field_type() const
Return what kind of date/time value this object holds.
Definition: json_dom.h:958
enum_json_type json_type() const override
Definition: json_dom.cc:1212
Json_datetime(const MYSQL_TIME &t, enum_field_types ft)
Constructs a object to hold a MySQL date/time value.
Definition: json_dom.h:939
void to_packed(char *dest) const
Convert the datetime to the packed format used when storing datetime values.
Definition: json_dom.cc:1235
static void from_packed(const char *from, enum_field_types ft, MYSQL_TIME *to)
Convert a packed datetime back to a MYSQL_TIME.
Definition: json_dom.cc:1241
static const size_t PACKED_SIZE
Datetimes are packed in eight bytes.
Definition: json_dom.h:990
static void from_packed_to_key(const char *from, enum_field_types ft, uchar *to, uint8 dec)
Convert a packed datetime to key string for indexing by SE.
Definition: json_dom.cc:1247
const MYSQL_TIME * value() const
Return a pointer the date/time value.
Definition: json_dom.h:951
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.cc:1230
Represents a MySQL decimal number, type J_DECIMAL.
Definition: json_dom.h:736
Json_decimal(const my_decimal &value)
Definition: json_dom.cc:1163
my_decimal m_dec
holds the decimal number
Definition: json_dom.h:738
bool get_binary(char *dest) const
Get the binary representation of the wrapped my_decimal, so that this value can be stored inside of a...
Definition: json_dom.cc:1175
enum_json_type json_type() const override
Definition: json_dom.h:761
const my_decimal * value() const
Get a pointer to the MySQL decimal held by this object.
Definition: json_dom.h:770
static const int MAX_BINARY_SIZE
Definition: json_dom.h:741
static const char * get_encoded_binary(const char *bin)
Returns stored DECIMAL binary.
Definition: json_dom.h:802
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.h:772
static bool convert_from_binary(const char *bin, size_t len, my_decimal *dec)
Convert a binary value produced by get_binary() back to a my_decimal.
Definition: json_dom.cc:1189
int binary_size() const
Get the number of bytes needed to store this decimal in a Json_opaque.
Definition: json_dom.cc:1167
static size_t get_encoded_binary_len(size_t length)
Returns length of stored DECIMAL binary.
Definition: json_dom.h:816
JSON DOM abstract base class.
Definition: json_dom.h:172
virtual bool is_scalar() const
Definition: json_dom.h:222
Json_container * parent() const
Get the parent dom to which this dom is attached.
Definition: json_dom.h:212
bool seek(const Json_seekable_path &path, size_t legs, Json_dom_vector *hits, bool auto_wrap, bool only_need_one)
Finds all of the json sub-documents which match the path expression.
Definition: json_dom.cc:1905
static Json_dom_ptr parse(const char *text, size_t length, const JsonParseErrorHandler &error_handler, const JsonDocumentDepthHandler &depth_handler)
Parse Json text to DOM (using rapidjson).
Definition: json_dom.cc:561
virtual ~Json_dom()=default
virtual uint32 depth() const =0
Compute the depth of a document.
Json_path get_location() const
Get the path location of this dom, measured from the outermost document it nests inside.
Definition: json_dom.cc:1877
void set_parent(Json_container *parent)
Set the parent dom to which this dom is attached.
Definition: json_dom.h:183
virtual bool is_number() const
Definition: json_dom.h:227
Json_container * m_parent
Parent pointer.
Definition: json_dom.h:323
virtual enum_json_type json_type() const =0
virtual Json_dom_ptr clone() const =0
Make a deep clone.
Represents a MySQL double JSON scalar (an extension of the ECMA number value), type J_DOUBLE.
Definition: json_dom.h:826
double m_f
holds the double value
Definition: json_dom.h:828
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.h:834
enum_json_type json_type() const override
Definition: json_dom.h:832
double value() const
Return the double value held by this object.
Definition: json_dom.h:842
Json_double(double value)
Definition: json_dom.h:830
Represents a MySQL integer (64 bits signed) JSON scalar (an extension of the ECMA number value),...
Definition: json_dom.h:849
enum_json_type json_type() const override
Definition: json_dom.h:855
bool is_16bit() const
Definition: json_dom.h:866
Json_int(longlong value)
Definition: json_dom.h:853
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.h:873
longlong m_i
holds the value
Definition: json_dom.h:851
bool is_32bit() const
Definition: json_dom.h:871
longlong value() const
Return the signed int held by this object.
Definition: json_dom.h:861
Represents a JSON null type (ECMA), type J_NULL here.
Definition: json_dom.h:913
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.h:916
enum_json_type json_type() const override
Definition: json_dom.h:915
Abstract base class of all JSON number (ECMA) types (subclasses represent MySQL extensions).
Definition: json_dom.h:728
bool is_number() const final
Definition: json_dom.h:730
A wrapper over a JSON object which provides an interface that can be iterated over with a for-each lo...
Definition: json_dom.h:1861
Json_wrapper_object_iterator const_iterator
Definition: json_dom.h:1863
const_iterator end() const
Definition: json_dom.h:1869
Json_object_wrapper(const Json_wrapper &wrapper)
Definition: json_dom.h:1864
const_iterator begin() const
Definition: json_dom.h:1868
const_iterator cbegin() const
Definition: json_dom.h:1866
const_iterator cend() const
Definition: json_dom.h:1867
const Json_wrapper & m_wrapper
Definition: json_dom.h:1872
Represents a JSON container value of type "object" (ECMA), type J_OBJECT here.
Definition: json_dom.h:372
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.cc:872
bool remove(const std::string &key)
Remove the child element addressed by key.
Definition: json_dom.cc:852
Json_object_map m_map
Map to hold the object elements.
Definition: json_dom.h:377
Json_object_map::const_iterator const_iterator
Constant iterator over the elements in the JSON object.
Definition: json_dom.h:484
enum_json_type json_type() const override
Definition: json_dom.h:381
bool consume(Json_object_ptr other)
Transfer all of the key/value pairs in the other object into this object.
Definition: json_dom.cc:806
Json_object()
Definition: json_dom.cc:348
const_iterator end() const
Returns a const_iterator that refers past the last element.
Definition: json_dom.h:490
size_t cardinality() const
Definition: json_dom.cc:860
uint32 depth() const override
Compute the depth of a document.
Definition: json_dom.cc:862
bool add_clone(const std::string &key, const Json_dom *value)
Insert a clone of the value into the object.
Definition: json_dom.h:393
Json_dom * get(const std::string &key) const
Return the value at key.
Definition: json_dom.cc:844
bool merge_patch(Json_object_ptr patch)
Implementation of the MergePatch function specified in RFC 7396:
Definition: json_dom.cc:884
void clear()
Remove all elements in the object.
Definition: json_dom.h:476
const_iterator begin() const
Returns a const_iterator that refers to the first element.
Definition: json_dom.h:487
bool add_alias(const std::string &key, Json_dom *value)
Insert the value into the object.
Definition: json_dom.h:414
void replace_dom_in_container(const Json_dom *oldv, Json_dom_ptr newv) override
Replace oldv contained inside this container array or object) with newv.
Definition: json_dom.cc:764
Represents a MySQL value opaquely, i.e.
Definition: json_dom.h:1002
enum_field_types type() const
Definition: json_dom.h:1032
size_t size() const
Definition: json_dom.h:1036
enum_json_type json_type() const override
Definition: json_dom.h:1022
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.cc:1280
std::string m_val
Definition: json_dom.h:1005
const char * value() const
Definition: json_dom.h:1027
Json_opaque(enum_field_types mytype, Args &&... args)
An opaque MySQL value.
Definition: json_dom.h:1019
enum_field_types m_mytype
Definition: json_dom.h:1004
A JSON path expression.
Definition: json_path.h:356
A class that is capable of holding objects of any sub-type of Json_scalar.
Definition: json_dom.h:1892
Json_scalar * m_scalar_ptr
Pointer to the held scalar, or nullptr if no value is held.
Definition: json_dom.h:1920
Json_scalar * get()
Get a pointer to the held object, or nullptr if there is none.
Definition: json_dom.h:1924
Any_json_scalar m_buffer
The buffer in which the Json_scalar value is stored.
Definition: json_dom.h:1917
void emplace(Args &&... args)
Construct a new Json_scalar value in this Json_scalar_holder.
Definition: json_dom.h:1933
Abstract base class for all Json scalars.
Definition: json_dom.h:683
uint32 depth() const final
Compute the depth of a document.
Definition: json_dom.h:685
bool is_scalar() const final
Definition: json_dom.h:687
A path expression which can be used to seek to a position inside a JSON value.
Definition: json_path.h:301
Represents a JSON string value (ECMA), of type J_STRING here.
Definition: json_dom.h:693
std::string m_str
holds the string
Definition: json_dom.h:695
Json_string(Args &&... args)
Definition: json_dom.h:702
enum_json_type json_type() const override
Definition: json_dom.h:705
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.h:707
size_t size() const
Get the number of characters in the string.
Definition: json_dom.h:721
const std::string & value() const
Get the reference to the value of the JSON string.
Definition: json_dom.h:715
Represents a MySQL integer (64 bits unsigned) JSON scalar (an extension of the ECMA number value),...
Definition: json_dom.h:881
enum_json_type json_type() const override
Definition: json_dom.h:887
Json_uint(ulonglong value)
Definition: json_dom.h:885
Json_dom_ptr clone() const override
Make a deep clone.
Definition: json_dom.h:907
ulonglong value() const
Return the unsigned int held by this object.
Definition: json_dom.h:893
ulonglong m_i
holds the value
Definition: json_dom.h:883
bool is_16bit() const
Definition: json_dom.h:899
bool is_32bit() const
Definition: json_dom.h:905
Class that iterates over all members of a JSON object that is wrapped in a Json_wrapper instance.
Definition: json_dom.h:1779
bool operator==(const Json_wrapper_object_iterator &other) const
Checks two iterators for equality.
Definition: json_dom.h:1823
const json_binary::Value * m_binary_value
The binary JSON object being iterated over, or nullptr for DOMs.
Definition: json_dom.h:1846
const value_type * pointer
Definition: json_dom.h:1784
size_t m_current_element_index
The index of the current member in the binary JSON object.
Definition: json_dom.h:1848
std::pair< MYSQL_LEX_CSTRING, Json_wrapper > value_type
Definition: json_dom.h:1782
std::forward_iterator_tag iterator_category
Definition: json_dom.h:1786
const Json_wrapper_object_iterator operator++(int)
Advances the iterator to the next element and returns an iterator that points to the current element ...
Definition: json_dom.h:1816
bool is_dom() const
Returns true if iterating over a DOM.
Definition: json_dom.h:1852
void initialize_current_member()
Fill m_current_member with the key and value of the current member.
Definition: json_dom.cc:1297
const value_type & reference
Definition: json_dom.h:1783
bool m_current_member_initialized
True if m_current_member is initialized.
Definition: json_dom.h:1844
bool operator!=(const Json_wrapper_object_iterator &other) const
Checks two iterators for inequality.
Definition: json_dom.h:1829
Json_object::const_iterator m_iter
Iterator pointing to the current member in the JSON DOM object.
Definition: json_dom.h:1850
pointer operator->()
Definition: json_dom.h:1833
Json_wrapper_object_iterator & operator++()
Advances the iterator to the next element.
Definition: json_dom.h:1803
ptrdiff_t difference_type
Definition: json_dom.h:1785
value_type m_current_member
Pair holding the key and value of the member pointed to by the iterator.
Definition: json_dom.h:1842
Json_wrapper_object_iterator()=default
Forward iterators must be default constructible.
reference operator*()
Definition: json_dom.h:1838
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1160
const char * get_data() const
Get a pointer to the data of a JSON string or JSON opaque value.
Definition: json_dom.cc:1794
bool to_string(String *buffer, bool json_quoted, const char *func_name, const JsonDocumentDepthHandler &depth_handler) const
Format the JSON value to an external JSON string in buffer in the format of ISO/IEC 10646.
Definition: json_dom.cc:1696
const Json_dom * get_dom() const
Gets a pointer to the wrapped Json_dom object, if this wrapper holds a DOM.
Definition: json_dom.h:1292
void set_alias()
Only meaningful iff the wrapper encapsulates a DOM.
Definition: json_dom.h:1218
size_t get_data_length() const
Get the length to the data of a JSON string or JSON opaque value.
Definition: json_dom.cc:1804
bool get_decimal_data(my_decimal *d) const
Get the MySQL representation of a JSON decimal value.
Definition: json_dom.cc:1814
ulonglong get_uint() const
Get the value of a JSON unsigned integer number.
Definition: json_dom.cc:1840
bool get_boolean() const
Get a boolean value (a JSON true or false literal).
Definition: json_dom.cc:1868
enum_field_types field_type() const
Return the MYSQL type of the opaque value, see type().
Definition: json_dom.cc:1758
void remove_duplicates(const CHARSET_INFO *cs=nullptr)
Remove duplicate values.
Definition: json_dom.cc:3664
bool m_alias
If true, don't deallocate m_dom_value in destructor.
Definition: json_dom.h:1171
bool get_free_space(size_t *space) const
Calculate the amount of unused space inside a JSON binary value.
Definition: json_dom.cc:3429
Json_dom * to_dom()
Get the wrapped contents in DOM form.
Definition: json_dom.cc:1386
longlong get_int() const
Get the value of a JSON signed integer number.
Definition: json_dom.cc:1832
const char * get_datetime_packed(char *buffer) const
Get the wrapped datetime value in the packed format.
Definition: json_dom.cc:1857
my_decimal * coerce_decimal(my_decimal *decimal_value, const char *msgnam)
Shorthand for coerce_decimal(decimal_value, msgnam, CE_WARNING, nullptr).
Definition: json_dom.h:1635
Json_wrapper operator[](size_t index) const
If this wrapper holds a JSON array, get an array value by indexing into the array.
Definition: json_dom.cc:1780
bool m_is_dom
Wraps a DOM iff true.
Definition: json_dom.h:1176
const json_binary::Value & get_binary_value() const
Gets the wrapped json_binary::Value object, if this wrapper holds a binary JSON value.
Definition: json_dom.h:1302
longlong coerce_int(const char *msgnam, enum_coercion_error cr_error, bool *err, bool *unsigned_flag) const
Extract an int (signed or unsigned) from the JSON if possible coercing if need be.
Definition: json_dom.cc:2706
void get_datetime(MYSQL_TIME *t) const
Get the value of a JSON date/time value.
Definition: json_dom.cc:1848
bool attempt_binary_update(const Field_json *field, const Json_seekable_path &path, Json_wrapper *new_value, bool replace, String *result, bool *partially_updated, bool *replaced_path)
Attempt a binary partial update by replacing the value at path with new_value.
Definition: json_dom.cc:3438
bool coerce_date(MYSQL_TIME *ltime, const char *msgnam, enum_coercion_error cr_error=CE_WARNING, my_time_flags_t date_flags_arg=0) const
Extract a date from the JSON if possible, coercing if need be.
Definition: json_dom.cc:2896
Json_wrapper lookup(const MYSQL_LEX_CSTRING &key) const
If this wrapper holds a JSON object, get the value corresponding to the member key.
Definition: json_dom.cc:1767
enum_json_type type() const
Return the type of the wrapped JSON value.
Definition: json_dom.cc:1725
bool to_pretty_string(String *buffer, const char *func_name, const JsonDocumentDepthHandler &depth_handler) const
Format the JSON value to an external JSON string in buffer in the format of ISO/IEC 10646.
Definition: json_dom.cc:1704
Json_wrapper()
Create an empty wrapper.
Definition: json_dom.h:1191
longlong coerce_int(const char *msgnam) const
Shorthand for coerce_int(msgnam, CE_WARNING, nullptr, nullptr).
Definition: json_dom.h:1600
double coerce_real(const char *msgnam) const
Shorthand for coerce_real(msgnam, CE_WARNING, nullptr).
Definition: json_dom.h:1617
int compare(const Json_wrapper &other, const CHARSET_INFO *cs=nullptr) const
Compare this JSON value to another JSON value.
Definition: json_dom.cc:2435
struct Json_wrapper::@32::@34 m_dom
The DOM representation, only used if m_is_dom is true.
void sort(const CHARSET_INFO *cs=nullptr)
Sort contents.
Definition: json_dom.cc:3659
ulonglong make_hash_key(ulonglong hash_val) const
Make a hash key that can be used by sql_executor.cc/unique_hash in order to support SELECT DISTINCT.
Definition: json_dom.cc:3363
bool seek(const Json_seekable_path &path, size_t legs, Json_wrapper_vector *hits, bool auto_wrap, bool only_need_one)
Finds all of the json sub-documents which match the path expression.
Definition: json_dom.cc:2134
Json_wrapper & operator=(const Json_wrapper &old)
Assignment operator.
Definition: json_dom.cc:1377
bool is_binary_backed_by(const String *str) const
Check if the wrapped JSON document is a binary value (a json_binary::Value), and if that binary is po...
Definition: json_dom.h:1338
double get_double() const
Get the value of a JSON double number.
Definition: json_dom.cc:1824
size_t make_sort_key(uchar *to, size_t length) const
Make a sort key that can be used by filesort to order JSON values.
Definition: json_dom.cc:3257
bool binary_remove(const Field_json *field, const Json_seekable_path &path, String *result, bool *found_path)
Remove a path from a binary JSON document.
Definition: json_dom.cc:3582
size_t length() const
Compute the length of a document.
Definition: json_dom.cc:2170
bool empty() const
A Wrapper is defined to be empty if it is passed a NULL value with the constructor for JSON dom,...
Definition: json_dom.h:1269
Json_wrapper(Json_dom_ptr dom_value)
Wrap the supplied DOM value.
Definition: json_dom.h:1209
my_decimal * coerce_decimal(my_decimal *decimal_value, const char *msgnam, enum_coercion_error cr_error, bool *err) const
Extract a decimal from the JSON if possible, coercing if need be.
Definition: json_dom.cc:2833
void dbug_print(const char *message, const JsonDocumentDepthHandler &depth_handler) const
Print this JSON document to the debug trace.
Definition: json_dom.cc:1712
double coerce_real(const char *msgnam, enum_coercion_error cr_error, bool *err) const
Extract a real from the JSON if possible, coercing if need be.
Definition: json_dom.cc:2783
bool coerce_time(MYSQL_TIME *ltime, const char *msgnam, enum_coercion_error cr_error=CE_WARNING) const
Extract a time value from the JSON if possible, coercing if need be.
Definition: json_dom.cc:2936
~Json_wrapper()
Definition: json_dom.cc:1349
Json_dom * m_value
Definition: json_dom.h:1169
bool is_dom() const
Does this wrapper contain a DOM?
Definition: json_dom.h:1277
bool to_binary(const THD *thd, String *str) const
Get the wrapped contents in binary value form.
Definition: json_dom.cc:1407
Json_dom_ptr clone_dom() const
Get the wrapped contents in DOM form.
Definition: json_dom.cc:1398
json_binary::Value m_value
The binary representation, only used if m_is_dom is false.
Definition: json_dom.h:1174
Malloc_allocator is a C++ STL memory allocator based on my_malloc/my_free.
Definition: malloc_allocator.h:62
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
Class used for reading JSON values that are stored in the binary format.
Definition: json_binary.h:197
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:93
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
This file contains the field type.
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
static const std::string dec("DECRYPTION")
static uint16 key1[1001]
Definition: hp_test2.cc:46
This file specifies the interface for serializing JSON values into binary representation,...
std::unique_ptr< Json_dom > Json_dom_ptr
Definition: json_dom.h:64
Prealloced_array< Json_dom *, 16 > Json_dom_vector
Definition: json_dom.h:62
std::unique_ptr< T > create_dom_ptr(Args &&... args)
Allocate a new Json_dom object and return a std::unique_ptr which points to it.
Definition: json_dom.h:138
std::map< std::string, Json_dom_ptr, Json_key_comparator, Malloc_allocator< std::pair< const std::string, Json_dom_ptr > > > Json_object_map
A type used to hold JSON object elements in a map, see the Json_object class.
Definition: json_dom.h:366
bool is_valid_json_syntax(const char *text, size_t length)
Check if a string contains valid JSON text, without generating a Json_dom representation of the docum...
std::unique_ptr< Json_object > Json_object_ptr
Definition: json_dom.h:66
enum_json_type
Json values in MySQL comprises the stand set of JSON values plus a MySQL specific set.
Definition: json_dom.h:108
std::unique_ptr< Json_array > Json_array_ptr
Definition: json_dom.h:65
bool double_quote(const char *cptr, size_t length, String *buf)
Perform quoting on a JSON string to make an external representation of it.
Definition: json_dom.cc:1131
enum_coercion_error
How Json_wrapper would handle coercion error.
Definition: json_dom.h:1141
@ CE_WARNING
Definition: json_dom.h:1142
@ CE_ERROR
Definition: json_dom.h:1143
@ CE_IGNORE
Definition: json_dom.h:1144
Prealloced_array< Json_wrapper, 16 > Json_wrapper_vector
Definition: json_dom.h:59
Json_dom_ptr merge_doms(Json_dom_ptr left, Json_dom_ptr right)
Merge two doms.
Definition: json_dom.cc:101
std::function< void()> JsonDocumentDepthHandler
Definition: json_error_handler.h:31
std::function< void(const char *parse_err, size_t err_offset)> JsonParseErrorHandler
Definition: json_error_handler.h:30
Header for compiler-dependent features.
It is interface module to fixed precision decimals library.
static constexpr int DECIMAL_MAX_FIELD_SIZE
maximum size of packet length.
Definition: my_decimal.h:79
Some integer typedefs for easier portability.
#define UINT_MAX16
Definition: my_inttypes.h:84
unsigned long long int ulonglong
Definition: my_inttypes.h:55
uint8_t uint8
Definition: my_inttypes.h:62
unsigned char uchar
Definition: my_inttypes.h:51
#define INT_MAX16
Definition: my_inttypes.h:83
#define INT_MIN32
Definition: my_inttypes.h:76
long long int longlong
Definition: my_inttypes.h:54
#define INT_MAX32
Definition: my_inttypes.h:77
#define INT_MIN16
Definition: my_inttypes.h:82
uint32_t uint32
Definition: my_inttypes.h:66
#define UINT_MAX32
Definition: my_inttypes.h:78
Interface for low level time utilities.
unsigned int my_time_flags_t
Flags to str_to_datetime and number_to_datetime.
Definition: my_time.h:93
Time declarations shared between the server and client API: you should not add anything to this heade...
static char * path
Definition: mysqldump.cc:133
static bool replace
Definition: mysqlimport.cc:65
void copy(Shards< COUNT > &dst, const Shards< COUNT > &src) noexcept
Copy the counters, overwrite destination.
Definition: ut0counter.h:353
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1054
Definition: buf0block_hint.cc:29
Definition: commit_order_queue.h:33
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:909
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:419
Definition: varlen_sort.h:183
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2890
required string key
Definition: replication_asynchronous_connection_failover.proto:59
Definition: m_ctype.h:382
A comparator that is used for ordering keys in a Json_object.
Definition: json_dom.h:348
bool operator()(const std::string &key1, const std::string &key2) const
Definition: json_dom.cc:949
void is_transparent
Definition: json_dom.h:357
Definition: mysql_lex_string.h:39
Definition: mysql_time.h:81
Definition: result.h:29
Union of all concrete subclasses of Json_scalar.
Definition: json_dom.h:1894
Json_null m_null
Definition: json_dom.h:1901
Json_boolean m_boolean
Definition: json_dom.h:1900
Json_decimal m_decimal
Definition: json_dom.h:1896
Json_datetime m_datetime
Definition: json_dom.h:1902
Json_double m_double
Definition: json_dom.h:1899
Json_opaque m_opaque
Definition: json_dom.h:1903
~Any_json_scalar()
Destructor which delegates to Json_scalar's virtual destructor.
Definition: json_dom.h:1907
Json_int m_int
Definition: json_dom.h:1897
Any_json_scalar()
Constructor which initializes the union to hold a Json_null value.
Definition: json_dom.h:1905
Json_uint m_uint
Definition: json_dom.h:1898
Json_string m_string
Definition: json_dom.h:1895