Skip to content

Commit 5de6452

Browse files
author
Catalin Besleaga
committed
WL#15045 Expand the json client library for HeatWave[fix,noclose]
Expanded the json_client_library to include Json_path and its dependencies, Json_dom::seek, Json_dom::parse and their dependencies as well. Got rid of THD, used as parameter in functions signatures by replacing the two places where it was used with current_thd and wrapping the call to be compiled only for the server usage. In order to be used independent of the server, parse method has now an additional parameter called JsonParseErrorHandler that is a callback used to handle parse errors. Added another callback to check_json_depth in order to allow different clients to handle this case. Removed parse_error param from parse_json and errmsg, errpos from Json_dom::parse. Change-Id: I629862fd4e003c0b0b1f8f2db93b1078743bf564
1 parent 4879568 commit 5de6452

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+743
-484
lines changed

client/CMakeLists.txt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2006, 2021, Oracle and/or its affiliates.
1+
# Copyright (c) 2006, 2022, Oracle and/or its affiliates.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0,
@@ -100,7 +100,12 @@ IF(WITH_JSON_BINLOG_LIBRARY)
100100
UNSET(INSTALL_JSON_BINLOG_LIBRARY)
101101
INSTALL(FILES
102102
../sql/json_binary.h
103-
DESTINATION ${INSTALL_INCLUDEDIR}
103+
DESTINATION ${INSTALL_INCLUDEDIR}/sql
104+
COMPONENT Development
105+
)
106+
INSTALL(FILES
107+
../sql-common/json_error_handler.h
108+
DESTINATION ${INSTALL_INCLUDEDIR}/sql-common
104109
COMPONENT Development
105110
)
106111
ELSE()
@@ -128,6 +133,7 @@ ENDIF()
128133
ADD_SHARED_LIBRARY(json_binlog
129134
${CMAKE_SOURCE_DIR}/sql/json_binary.cc
130135
${CMAKE_SOURCE_DIR}/sql/json_dom.cc
136+
${CMAKE_SOURCE_DIR}/sql/json_path.cc
131137
${CMAKE_SOURCE_DIR}/sql/json_syntax_check.cc
132138
${CMAKE_SOURCE_DIR}/sql-common/sql_string.cc
133139

@@ -143,6 +149,7 @@ ADD_SHARED_LIBRARY(json_binlog
143149
ADD_CONVENIENCE_LIBRARY(json_client_library
144150
${CMAKE_SOURCE_DIR}/sql/json_binary.cc
145151
${CMAKE_SOURCE_DIR}/sql/json_dom.cc
152+
${CMAKE_SOURCE_DIR}/sql/json_path.cc
146153
${CMAKE_SOURCE_DIR}/sql/json_syntax_check.cc
147154
${CMAKE_SOURCE_DIR}/sql-common/sql_string.cc
148155

@@ -163,6 +170,15 @@ MERGE_CONVENIENCE_LIBRARIES(json_binlog_static
163170
${INSTALL_JSON_BINLOG_LIBRARY}
164171
)
165172

173+
# This is a test executable which verifies that Json_wrapper::seek,
174+
# Json_dom::seek and Json_dom::parse functions are visible and can be called.
175+
MYSQL_ADD_EXECUTABLE(json_client_library_main
176+
json_client_library_main.cc
177+
INCLUDE_DIRECTORIES ../sql
178+
LINK_LIBRARIES json_client_library strings
179+
SKIP_INSTALL
180+
)
181+
166182
# A test executable which does nothing, except verify that symbols are visible.
167183
# To test standalone build:
168184
# cmake -DINSTALL_MYSQLTESTDIR=0
@@ -172,7 +188,7 @@ MERGE_CONVENIENCE_LIBRARIES(json_binlog_static
172188
# -ljson_binlog
173189
MYSQL_ADD_EXECUTABLE(json_binlog_main
174190
json_binlog_main.cc
175-
INCLUDE_DIRECTORIES ../sql
191+
INCLUDE_DIRECTORIES ../sql ../sql-common
176192
LINK_LIBRARIES json_binlog
177193
SKIP_INSTALL
178194
)
@@ -184,7 +200,7 @@ MYSQL_ADD_EXECUTABLE(json_binlog_main
184200
# -ljson_binlog_static -lpthread
185201
MYSQL_ADD_EXECUTABLE(json_binlog_main_static
186202
json_binlog_main.cc
187-
INCLUDE_DIRECTORIES ../sql
203+
INCLUDE_DIRECTORIES ../sql ../sql-common
188204
LINK_LIBRARIES json_binlog_static
189205
SKIP_INSTALL
190206
)

client/json_binlog_main.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
1+
/* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
22
33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License, version 2.0,
@@ -29,7 +29,7 @@
2929

3030
#include <string>
3131

32-
#include "json_binary.h"
32+
#include "sql/json_binary.h"
3333

3434
int main() {
3535
json_binary::Value value = json_binary::parse_binary(nullptr, 0);
@@ -58,7 +58,7 @@ int main() {
5858
json_binary::Value string_value = json_binary::Value("foo", 3);
5959

6060
std::string string_buf;
61-
string_value.to_std_string(&string_buf);
62-
string_value.to_pretty_std_string(&string_buf);
61+
string_value.to_std_string(&string_buf, [] { assert(false); });
62+
string_value.to_pretty_std_string(&string_buf, [] { assert(false); });
6363
return 0;
6464
}

client/json_client_library_main.cc

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* Copyright (c) 2021,2022, Oracle and/or its affiliates.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License, version 2.0,
5+
as published by the Free Software Foundation.
6+
7+
This program is also distributed with certain software (including
8+
but not limited to OpenSSL) that is licensed under separate terms,
9+
as designated in a particular file or component or in included license
10+
documentation. The authors of MySQL hereby grant you an additional
11+
permission to link the program and your derivative works with the
12+
separately licensed software that they have included with MySQL.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License, version 2.0, for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22+
23+
/*
24+
This is a test executable which verifies that Json_wrapper::seek,
25+
Json_dom::seek and Json_dom::parse functions are visible and can be called.
26+
*/
27+
#include <cstring>
28+
#include <iostream>
29+
#include <string>
30+
31+
#include "sql/json_dom.h"
32+
#include "sql/json_path.h"
33+
34+
#include "sql_string.h"
35+
36+
int main() {
37+
Json_object o;
38+
for (size_t i = 0; i < 1000; ++i) {
39+
o.add_alias(std::to_string(i),
40+
new (std::nothrow)
41+
Json_string(std::string("key_") + std::to_string(i)));
42+
}
43+
44+
// Make sure Json_wrapper::seek is visible
45+
Json_wrapper_vector hits(PSI_NOT_INSTRUMENTED);
46+
bool need_only_one{false};
47+
const char *json_path = R"($**."512")";
48+
Json_path path(PSI_NOT_INSTRUMENTED);
49+
size_t bad_index;
50+
parse_path(std::strlen(json_path), json_path, &path, &bad_index,
51+
[] { assert(false); });
52+
53+
Json_wrapper wr(&o);
54+
wr.set_alias();
55+
wr.seek(path, path.leg_count(), &hits, true, need_only_one);
56+
57+
for (auto &hit : hits) {
58+
String buffer;
59+
hit.to_string(&buffer, false, nullptr, [] { assert(false); });
60+
std::cout << std::string_view{buffer.ptr(), buffer.length()} << std::endl;
61+
}
62+
63+
// Make sure Json_dom::parse is visible and error handling works
64+
{
65+
std::string json{"[{\"key\":123},146]"};
66+
Json_dom_ptr dom(Json_dom::parse(
67+
json.c_str(), json.length(),
68+
[](const char *, size_t) { assert(false); }, [] { assert(false); }));
69+
if (dom != nullptr) std::cout << "1. success" << std::endl;
70+
}
71+
72+
{
73+
std::string json{
74+
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["
75+
R"([[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{"key":123}]]]]]]]]]]]]]]]]]]]]]]]]]])"
76+
"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"
77+
"]]]]"};
78+
Json_dom_ptr dom(Json_dom::parse(
79+
json.c_str(), json.length(),
80+
[](const char *err_mesg, size_t err_code) {
81+
std::cout << "2. parse_error [" << err_code << "]: " << err_mesg
82+
<< std::endl;
83+
},
84+
[] { std::cout << "2. depth_error" << std::endl; }));
85+
if (dom != nullptr) assert(false);
86+
}
87+
88+
{
89+
std::string json{"[&&"};
90+
Json_dom_ptr dom(Json_dom::parse(
91+
json.c_str(), json.length(),
92+
[](const char *err_mesg, size_t err_code) {
93+
std::cout << "3. parse_error [" << err_code << "]: " << err_mesg
94+
<< std::endl;
95+
},
96+
[] { assert(false); }));
97+
if (dom != nullptr) assert(false);
98+
}
99+
100+
return 0;
101+
}

sql-common/json_error_handler.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
/* Copyright (c) 2015, 2022, 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 also distributed 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 included with MySQL.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU General Public License, version 2.0, for more details.
19+
20+
You should have received a copy of the GNU General Public License
21+
along with this program; if not, write to the Free Software
22+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23+
24+
#include "sql-common/json_error_handler.h"
25+
26+
#include "my_sys.h"
27+
#include "mysqld_error.h"
28+
29+
void JsonParseDefaultErrorHandler::operator()(const char *parse_err,
30+
size_t err_offset) const {
31+
my_error(ER_INVALID_JSON_TEXT_IN_PARAM, MYF(0), m_arg_idx + 1, m_func_name,
32+
parse_err, err_offset, "");
33+
}
34+
35+
void JsonDocumentDefaultDepthHandler() {
36+
my_error(ER_JSON_DOCUMENT_TOO_DEEP, MYF(0));
37+
}

sql-common/json_error_handler.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef JSON_ERROR_HANDLER_INCLUDED
2+
#define JSON_ERROR_HANDLER_INCLUDED
3+
4+
/* Copyright (c) 2022, 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 <cstdlib>
27+
#include <functional>
28+
29+
using JsonParseErrorHandler =
30+
std::function<void(const char *parse_err, size_t err_offset)>;
31+
using JsonDocumentDepthHandler = std::function<void()>;
32+
33+
#ifdef MYSQL_SERVER
34+
35+
class JsonParseDefaultErrorHandler {
36+
public:
37+
JsonParseDefaultErrorHandler(const char *func_name, int arg_idx)
38+
: m_func_name(func_name), m_arg_idx(arg_idx) {}
39+
40+
void operator()(const char *parse_err, size_t err_offset) const;
41+
42+
private:
43+
const char *m_func_name;
44+
const int m_arg_idx;
45+
};
46+
47+
void JsonDocumentDefaultDepthHandler();
48+
49+
#endif // MYSQL_SERVER
50+
#endif // JSON_ERROR_HANDLER_INCLUDED

sql/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ SET(SQL_SOURCE
772772
../sql-common/oci/ssl.cc
773773
../sql-common/sql_string.cc
774774
../sql-common/bind_params.cc
775+
../sql-common/json_error_handler.cc
775776
command_service.cc
776777
conn_handler/channel_info.cc
777778
conn_handler/connection_handler_per_thread.cc

0 commit comments

Comments
 (0)