MySQL 9.3.0
Source Code Documentation
json_input.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, 2025, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef ROUTER_SRC_MYSQL_REST_SERVICE_INCLUDE_MRS_DATABASE_JSON_MAPPER_VIEW_JSON_INPUT_H_
27#define ROUTER_SRC_MYSQL_REST_SERVICE_INCLUDE_MRS_DATABASE_JSON_MAPPER_VIEW_JSON_INPUT_H_
28
29#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
30#include "my_rapidjson_size_t.h"
31#endif
32
33#include <rapidjson/document.h>
34
35#include <functional>
36#include <optional>
37#include <stdexcept>
38#include <string>
39#include <tuple>
40#include <utility>
41#include <vector>
42
43namespace mrs {
44namespace database {
45namespace dv {
46
47class JSONInput {
48 public:
49 JSONInput(const JSONInput &) = default;
50 JSONInput &operator=(const JSONInput &) = default;
51
52 virtual ~JSONInput() = default;
53
54 virtual bool has_new() const { return false; }
55 virtual bool has_old() const { return false; }
56
57 protected:
59};
60
61class JSONInputObject : public JSONInput {
62 public:
65
66 std::string_view new_name() const { return (*new_)->name.GetString(); }
67 std::string_view old_name() const { return (*old_)->name.GetString(); }
68
69 const rapidjson::Value &new_value() const { return (*new_)->value; }
70 const rapidjson::Value &old_value() const { return (*old_)->value; }
71
72 bool has_new() const {
73 return new_.has_value() &&
74 *new_ != rapidjson::Value::ConstMemberIterator();
75 }
76
77 bool has_old() const {
78 return old_.has_value() &&
79 *old_ != rapidjson::Value::ConstMemberIterator();
80 }
81
82 private:
83 friend class JSONInputObject;
84
85 explicit MemberReference(rapidjson::Value::ConstMemberIterator anew)
86 : new_(std::move(anew)) {}
87
88 MemberReference(rapidjson::Value::ConstMemberIterator anew,
89 rapidjson::Value::ConstMemberIterator aold)
90 : new_(std::move(anew)), old_(std::move(aold)) {}
91
92 std::optional<rapidjson::Value::ConstMemberIterator> new_;
93 std::optional<rapidjson::Value::ConstMemberIterator> old_;
94 };
95
97
98 explicit JSONInputObject(const rapidjson::Value &value)
99 : new_value_(&value) {}
100
101 JSONInputObject(std::nullptr_t, const rapidjson::Value &old_value)
102 : old_value_(&old_value) {}
103
104 JSONInputObject(const rapidjson::Value &value,
105 const rapidjson::Value &old_value)
107
108 template <typename S>
109 MemberReference find(const S &name) const {
110 if (!has_new()) return {};
111
112 auto new_m = new_value_->FindMember(name);
113 if (old_value_) {
114 auto old_m = old_value_->FindMember(name);
115
116 return MemberReference(new_m != new_value_->MemberEnd()
117 ? new_m
118 : rapidjson::Value::ConstMemberIterator(),
119 old_m != old_value_->MemberEnd()
120 ? old_m
121 : rapidjson::Value::ConstMemberIterator());
122 } else {
123 return MemberReference(new_m != new_value_->MemberEnd()
124 ? new_m
125 : rapidjson::Value::ConstMemberIterator());
126 }
127 }
128
129 bool has_new() const override { return new_value_ != nullptr; }
130 bool has_old() const override { return old_value_ != nullptr; }
131
132 const rapidjson::Value &new_value() const { return *new_value_; }
133
134 const rapidjson::Value &old_value() const {
135 assert(has_old());
136
137 return *old_value_;
138 }
139
140 rapidjson::Value::ConstObject new_object() const {
141 return new_value_->GetObject();
142 }
143
144 rapidjson::Value::ConstObject old_object() const {
145 assert(has_old());
146
147 return old_value_->GetObject();
148 }
149
150 bool new_empty() const { return new_value_->GetObject().MemberCount() == 0; }
151
152 private:
153 const rapidjson::Value *new_value_ = nullptr;
154 const rapidjson::Value *old_value_ = nullptr;
155};
156
157class JSONInputArray : public JSONInput {
158 public:
160 const rapidjson::Value &new_value() const { return *new_; }
161 const rapidjson::Value &old_value() const { return *old_; }
162
163 bool has_new() const { return new_ != nullptr; }
164 bool has_old() const { return old_ != nullptr; }
165
166 private:
167 friend class JSONInputArray;
168
169 explicit ValueReference(const rapidjson::Value &anew) : new_(&anew) {}
170
171 ValueReference(const rapidjson::Value &anew, const rapidjson::Value &aold)
172 : new_(&anew), old_(&aold) {}
173
174 const rapidjson::Value *new_ = nullptr;
175 const rapidjson::Value *old_ = nullptr;
176 };
177
179 JSONInputArray(const JSONInputArray &) = default;
180
181 explicit JSONInputArray(const rapidjson::Value &value) : new_value_(&value) {}
182
183 JSONInputArray(const rapidjson::Value &value,
184 const rapidjson::Value &old_value)
185 : new_value_(&value), old_value_(&old_value) {}
186
187 JSONInputArray(std::nullptr_t, const rapidjson::Value &old_value)
188 : new_value_(nullptr), old_value_(&old_value) {}
189
190 size_t size() const {
191 if (!new_value_) return 0;
192 return new_value_->Size();
193 }
194
195 ValueReference get(size_t i) const {
196 assert(has_new());
197
198 if (!has_old()) return ValueReference((*new_value_)[i]);
199 if (old_value_->Size() > 0 && old_sorted_.empty())
200 throw std::logic_error("sort_old() must be called first");
201
202 if (i >= new_value_->Size()) throw std::range_error("invalid array index");
203
204 int old_i = old_sorted_.at(i);
205
206 if (old_i < 0)
207 return ValueReference((*new_value_)[i]);
208 else
209 return ValueReference((*new_value_)[i], (*old_value_)[old_i]);
210 }
211
212 bool has_new() const override { return new_value_ != nullptr; }
213 bool has_old() const override { return old_value_ != nullptr; }
214
215 rapidjson::Value::ConstArray new_array() const {
216 return new_value_->GetArray();
217 }
218
219 bool new_empty() const { return !new_value_ || new_value_->Empty(); }
220
221 template <typename KeyType>
222 void sort_old(const std::function<KeyType(const rapidjson::Value &)> &get_key,
223 std::vector<KeyType> &out_missing_from_old) {
224 if (!new_value_ || !old_value_) return;
225 std::vector<std::tuple<KeyType, int>> old_array;
226
227 auto index_in_old_array = [&](const rapidjson::Value &value) -> int {
228 auto key = get_key(value);
229
230 if (!key.empty()) {
231 for (auto iter = old_array.begin(); iter != old_array.end(); ++iter) {
232 if (std::get<0>(*iter) == key) {
233 int i = std::get<1>(*iter);
234 old_array.erase(iter);
235 return i;
236 }
237 }
238 }
239 return -1;
240 };
241
242 int index = 0;
243 for (const auto &ov : old_value_->GetArray()) {
244 old_array.emplace_back(get_key(ov), index++);
245 }
246 for (const auto &obj : new_value_->GetArray()) {
247 old_sorted_.push_back(index_in_old_array(obj));
248 }
249
250 for (const auto &elem : old_array) {
251 out_missing_from_old.push_back(std::get<0>(elem));
252 }
253 }
254
255 private:
256 const rapidjson::Value *new_value_ = nullptr;
257 const rapidjson::Value *old_value_ = nullptr;
258
259 std::vector<int> old_sorted_;
260};
261
263 const std::string &table,
264 const std::string &field = "");
265
267 const std::string &table,
268 const std::string &field = "");
269
271 const std::string &table,
272 const std::string &field = "");
273
274} // namespace dv
275} // namespace database
276} // namespace mrs
277
278#endif // ROUTER_SRC_MYSQL_REST_SERVICE_INCLUDE_MRS_DATABASE_JSON_MAPPER_VIEW_JSON_INPUT_H_
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
Definition: json_input.h:157
bool has_old() const override
Definition: json_input.h:213
bool has_new() const override
Definition: json_input.h:212
JSONInputArray(const JSONInputArray &)=default
const rapidjson::Value * old_value_
Definition: json_input.h:257
rapidjson::Value::ConstArray new_array() const
Definition: json_input.h:215
void sort_old(const std::function< KeyType(const rapidjson::Value &)> &get_key, std::vector< KeyType > &out_missing_from_old)
Definition: json_input.h:222
const rapidjson::Value * new_value_
Definition: json_input.h:256
JSONInputArray(const rapidjson::Value &value, const rapidjson::Value &old_value)
Definition: json_input.h:183
size_t size() const
Definition: json_input.h:190
JSONInputArray()
Definition: json_input.h:178
ValueReference get(size_t i) const
Definition: json_input.h:195
JSONInputArray(const rapidjson::Value &value)
Definition: json_input.h:181
std::vector< int > old_sorted_
Definition: json_input.h:259
JSONInputArray(std::nullptr_t, const rapidjson::Value &old_value)
Definition: json_input.h:187
bool new_empty() const
Definition: json_input.h:219
Definition: json_input.h:61
rapidjson::Value::ConstObject new_object() const
Definition: json_input.h:140
MemberReference find(const S &name) const
Definition: json_input.h:109
const rapidjson::Value * old_value_
Definition: json_input.h:154
JSONInputObject(const rapidjson::Value &value, const rapidjson::Value &old_value)
Definition: json_input.h:104
JSONInputObject(std::nullptr_t, const rapidjson::Value &old_value)
Definition: json_input.h:101
const rapidjson::Value & old_value() const
Definition: json_input.h:134
bool has_old() const override
Definition: json_input.h:130
JSONInputObject()
Definition: json_input.h:96
JSONInputObject(const rapidjson::Value &value)
Definition: json_input.h:98
rapidjson::Value::ConstObject old_object() const
Definition: json_input.h:144
bool new_empty() const
Definition: json_input.h:150
bool has_new() const override
Definition: json_input.h:129
const rapidjson::Value * new_value_
Definition: json_input.h:153
const rapidjson::Value & new_value() const
Definition: json_input.h:132
Definition: json_input.h:47
virtual bool has_new() const
Definition: json_input.h:54
JSONInput()
Definition: json_input.h:58
virtual ~JSONInput()=default
virtual bool has_old() const
Definition: json_input.h:55
JSONInput(const JSONInput &)=default
JSONInput & operator=(const JSONInput &)=default
Define rapidjson::SizeType to be std::uint64_t.
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:75
JSONInputObject make_input_object(const JSONInputArray::ValueReference &ref, const std::string &table, const std::string &field="")
Definition: json_input.cc:33
JSONInputArray make_input_array(const JSONInputObject::MemberReference &ref, const std::string &table, const std::string &field="")
Definition: json_input.cc:54
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Definition: authorize_manager.h:48
Definition: gcs_xcom_synode.h:64
static struct plugin_options_variables ov
Definition: plugin.cc:77
required string key
Definition: replication_asynchronous_connection_failover.proto:60
case opt name
Definition: sslopt-case.h:29
bool has_new() const
Definition: json_input.h:163
const rapidjson::Value & new_value() const
Definition: json_input.h:160
ValueReference(const rapidjson::Value &anew, const rapidjson::Value &aold)
Definition: json_input.h:171
const rapidjson::Value * new_
Definition: json_input.h:174
const rapidjson::Value * old_
Definition: json_input.h:175
bool has_old() const
Definition: json_input.h:164
const rapidjson::Value & old_value() const
Definition: json_input.h:161
ValueReference(const rapidjson::Value &anew)
Definition: json_input.h:169
MemberReference(rapidjson::Value::ConstMemberIterator anew)
Definition: json_input.h:85
std::string_view new_name() const
Definition: json_input.h:66
bool has_old() const
Definition: json_input.h:77
const rapidjson::Value & old_value() const
Definition: json_input.h:70
MemberReference(rapidjson::Value::ConstMemberIterator anew, rapidjson::Value::ConstMemberIterator aold)
Definition: json_input.h:88
std::optional< rapidjson::Value::ConstMemberIterator > old_
Definition: json_input.h:93
const rapidjson::Value & new_value() const
Definition: json_input.h:69
bool has_new() const
Definition: json_input.h:72
std::optional< rapidjson::Value::ConstMemberIterator > new_
Definition: json_input.h:92
std::string_view old_name() const
Definition: json_input.h:67