Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 22f6d56

Browse files
committed
wl#14293 Add libmysql like Query Service in Server
At present we use the command service (WL#7947) to query locally within the server. This service is tightly coupled with Protocol_classic. That means in order to use the command service you have to provide the Protocol_callbacks. It inconvenient to use besides that it exposes the protocol internals to user. It creates more dependencies with the user code hence make it harder to improve the internals. The users are already familiar with the libmysql C APIs which are well documented and used by the clients (https://dev.mysql.com/doc/c-api/8.0/en/c-api-function- overview.html). " Aim of this worklog is to either enable those client APIs to use within the server or provide equivalent to them. " We evaluated both of these possibilities. We found that second option to be simpler and easy to extend i.e. a component service. We introduce a local query service. It will provide methods that could be used to query within the server. Advantages: 1. Easy to use, already familiar and extendable interface. 2. User need not to know or implement the internal details like protocols. 3. Components and Plugins can also use them. This worklog is identified as one of the subtask of another larger goal that is to have single port for different types of clients to communicate with server. (WL#13894) Approved by: Georgi Kodinov <georgi.kodinov@oracle.com> Approved by: Bharathy Satish <bharathy.x.satish@oracle.com>
1 parent 59fd283 commit 22f6d56

37 files changed

Lines changed: 5494 additions & 352 deletions

‎components/test/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,8 @@ MYSQL_ADD_COMPONENT(test_sensitive_system_variables
159159
MODULE_ONLY
160160
TEST_ONLY
161161
)
162+
MYSQL_ADD_COMPONENT(test_mysql_command_services
163+
test_mysql_command_services.cc
164+
MODULE_ONLY
165+
TEST_ONLY
166+
)
Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
/* Copyright (c) 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+
#include <mysql.h>
24+
#include <mysql/components/component_implementation.h>
25+
#include <mysql/components/my_service.h>
26+
#include <mysql/components/services/mysql_command_services.h>
27+
#include <mysql/components/services/security_context.h>
28+
#include <mysql/components/services/udf_registration.h>
29+
#include <mysql/service_srv_session_info.h>
30+
#include <stdio.h>
31+
#include <string>
32+
33+
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_thd_security_context, thd_security_ctx);
34+
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_account_database_security_context_lookup,
35+
account_db_security_ctx_lookup);
36+
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_security_context_options,
37+
security_ctx_options);
38+
REQUIRES_SERVICE_PLACEHOLDER_AS(udf_registration, udf_srv);
39+
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_factory, cmd_factory_srv);
40+
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_options, cmd_options_srv);
41+
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_query, cmd_query_srv);
42+
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_query_result,
43+
cmd_query_result_srv);
44+
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_field_info, cmd_field_info_srv);
45+
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_error_info, cmd_error_info_srv);
46+
47+
BEGIN_COMPONENT_PROVIDES(test_mysql_command_services)
48+
END_COMPONENT_PROVIDES();
49+
50+
BEGIN_COMPONENT_REQUIRES(test_mysql_command_services)
51+
REQUIRES_SERVICE_AS(udf_registration, udf_srv),
52+
REQUIRES_SERVICE_AS(mysql_thd_security_context, thd_security_ctx),
53+
REQUIRES_SERVICE_AS(mysql_account_database_security_context_lookup,
54+
account_db_security_ctx_lookup),
55+
REQUIRES_SERVICE_AS(mysql_security_context_options, security_ctx_options),
56+
REQUIRES_SERVICE_AS(mysql_command_factory, cmd_factory_srv),
57+
REQUIRES_SERVICE_AS(mysql_command_options, cmd_options_srv),
58+
REQUIRES_SERVICE_AS(mysql_command_query, cmd_query_srv),
59+
REQUIRES_SERVICE_AS(mysql_command_query_result, cmd_query_result_srv),
60+
REQUIRES_SERVICE_AS(mysql_command_field_info, cmd_field_info_srv),
61+
REQUIRES_SERVICE_AS(mysql_command_error_info, cmd_error_info_srv),
62+
END_COMPONENT_REQUIRES();
63+
64+
MYSQL_H mysql_h = nullptr;
65+
MYSQL_LEX_CSTRING user;
66+
MYSQL_LEX_CSTRING host;
67+
68+
static char *test_mysql_command_services_udf(UDF_INIT *, UDF_ARGS *args,
69+
char *result,
70+
unsigned long *length,
71+
unsigned char *,
72+
unsigned char *error) {
73+
*error = 1;
74+
if (args->arg_count == 0) {
75+
return nullptr;
76+
}
77+
78+
MYSQL_RES_H mysql_res = nullptr;
79+
MYSQL_ROW_H row = nullptr;
80+
MYSQL_FIELD_H *fields_h = nullptr;
81+
MYSQL_FIELD_H field_h = nullptr;
82+
unsigned int field_count;
83+
uint64_t row_count = 0;
84+
unsigned int num_column = 0;
85+
std::string result_set;
86+
unsigned int err_no;
87+
char *sqlstate_errmsg[50];
88+
89+
// Execute the SQL specified in the argument.
90+
if (cmd_factory_srv->init(&mysql_h)) {
91+
return nullptr;
92+
}
93+
if (mysql_h) {
94+
if (cmd_factory_srv->connect(mysql_h)) {
95+
return nullptr;
96+
}
97+
} else {
98+
return nullptr;
99+
}
100+
101+
std::string query(args->args[0], args->lengths[0]);
102+
103+
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
104+
cmd_error_info_srv->sql_error(mysql_h, &result);
105+
*length = strlen(result);
106+
goto err;
107+
}
108+
109+
cmd_query_result_srv->store_result(mysql_h, &mysql_res);
110+
if (mysql_res) {
111+
if (cmd_query_srv->affected_rows(mysql_h, &row_count)) {
112+
result = nullptr;
113+
goto err;
114+
}
115+
if (cmd_field_info_srv->num_fields(mysql_res, &num_column)) {
116+
result = nullptr;
117+
goto err;
118+
}
119+
if (cmd_field_info_srv->field_count(mysql_h, &field_count)) {
120+
result = nullptr;
121+
goto err;
122+
}
123+
124+
for (uint64_t i = 0; i < row_count; i++) {
125+
if (cmd_query_result_srv->fetch_row(mysql_res, &row)) {
126+
result = nullptr;
127+
goto err;
128+
}
129+
if (cmd_field_info_srv->fetch_field(mysql_res, &field_h)) {
130+
result = nullptr;
131+
goto err;
132+
}
133+
if (cmd_field_info_srv->fetch_fields(mysql_res, &fields_h)) {
134+
result = nullptr;
135+
goto err;
136+
}
137+
ulong *length = nullptr;
138+
cmd_query_result_srv->fetch_lengths(mysql_res, &length);
139+
for (unsigned int j = 0; j < num_column; j++) {
140+
result_set += row[j];
141+
}
142+
}
143+
/* The caller has the buffer limit, and the size is of MAX_FIELD_WIDTH size
144+
so we are truncating the result of the query output if it has more date
145+
*/
146+
strncpy(
147+
result,
148+
reinterpret_cast<char *>(const_cast<char *>(result_set.c_str())),
149+
(result_set.length() < *length) ? result_set.length() : (*length - 1));
150+
*length =
151+
(result_set.length() < *length) ? result_set.length() : (*length - 1);
152+
result[*length] = '\0';
153+
} else {
154+
cmd_error_info_srv->sql_error(mysql_h, &result);
155+
cmd_error_info_srv->sql_errno(mysql_h, &err_no);
156+
cmd_error_info_srv->sql_state(mysql_h, sqlstate_errmsg);
157+
*length = strlen(result);
158+
}
159+
err:
160+
*error = 0;
161+
cmd_query_result_srv->free_result(mysql_res);
162+
cmd_factory_srv->close(mysql_h);
163+
return result;
164+
}
165+
166+
static char *test_mysql_command_services_apis_udf(UDF_INIT *, UDF_ARGS *args,
167+
char *result,
168+
unsigned long *length,
169+
unsigned char *,
170+
unsigned char *error) {
171+
*error = 1;
172+
if (args->arg_count > 0) {
173+
return nullptr;
174+
}
175+
MYSQL_RES_H mysql_res = nullptr;
176+
MYSQL_ROW_H row = nullptr;
177+
uint64_t row_count = 0;
178+
unsigned int num_column = 0;
179+
std::string result_set;
180+
181+
// Execute the SQL specified in the argument.
182+
if (cmd_factory_srv->init(&mysql_h)) {
183+
return nullptr;
184+
}
185+
if (mysql_h) {
186+
if (cmd_factory_srv->connect(mysql_h)) {
187+
return nullptr;
188+
}
189+
} else {
190+
return nullptr;
191+
}
192+
193+
if (cmd_factory_srv->reset(mysql_h)) {
194+
goto err;
195+
}
196+
197+
/* set AUTOCOMMIT to OFF */
198+
if (cmd_factory_srv->autocommit(mysql_h, false)) {
199+
goto err;
200+
}
201+
202+
{
203+
std::string query("DROP TABLE IF EXISTS test.my_demo_transaction");
204+
205+
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
206+
*length = strlen(result);
207+
goto err;
208+
}
209+
}
210+
211+
/* To get the mysql option value */
212+
void *option_val;
213+
cmd_options_srv->get(mysql_h, MYSQL_OPT_MAX_ALLOWED_PACKET, &option_val);
214+
215+
{
216+
std::string query(
217+
"CREATE TABLE test.my_demo_transaction( "
218+
"col1 int , col2 varchar(30))");
219+
220+
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
221+
goto err;
222+
}
223+
}
224+
225+
{
226+
std::string query(
227+
"INSERT INTO test.my_demo_transaction VALUES(10, 'mysql-1')");
228+
229+
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
230+
goto err;
231+
}
232+
}
233+
234+
/* Commiting the transaction */
235+
if (cmd_factory_srv->commit(mysql_h)) {
236+
goto err;
237+
}
238+
239+
/* now insert the second row, and roll back the transaction */
240+
{
241+
std::string query(
242+
"INSERT INTO test.my_demo_transaction VALUES(20, 'mysql-2')");
243+
244+
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
245+
goto err;
246+
}
247+
}
248+
249+
/* Commiting the transaction */
250+
if (cmd_factory_srv->rollback(mysql_h)) {
251+
goto err;
252+
}
253+
254+
{
255+
std::string query("SELECT * from test.my_demo_transaction");
256+
257+
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
258+
goto err;
259+
}
260+
}
261+
262+
cmd_query_result_srv->store_result(mysql_h, &mysql_res);
263+
if (mysql_res) {
264+
if (cmd_query_srv->affected_rows(mysql_h, &row_count)) {
265+
result = nullptr;
266+
goto err;
267+
}
268+
if (cmd_field_info_srv->num_fields(mysql_res, &num_column)) {
269+
result = nullptr;
270+
goto err;
271+
}
272+
273+
for (uint64_t i = 0; i < row_count; i++) {
274+
if (cmd_query_result_srv->fetch_row(mysql_res, &row)) {
275+
result = nullptr;
276+
goto err;
277+
}
278+
ulong *length = nullptr;
279+
cmd_query_result_srv->fetch_lengths(mysql_res, &length);
280+
for (unsigned int j = 0; j < num_column; j++) {
281+
result_set += row[j];
282+
}
283+
}
284+
cmd_query_result_srv->more_results(mysql_h);
285+
cmd_query_result_srv->next_result(mysql_h);
286+
cmd_query_result_srv->result_metadata(mysql_res);
287+
/* The caller has the buffer limit, and the size is of MAX_FIELD_WIDTH size
288+
so we are truncating the result of the query output if it has more date
289+
*/
290+
strncpy(
291+
result,
292+
reinterpret_cast<char *>(const_cast<char *>(result_set.c_str())),
293+
(result_set.length() < *length) ? result_set.length() : (*length - 1));
294+
*length =
295+
(result_set.length() < *length) ? result_set.length() : (*length - 1);
296+
result[*length] = '\0';
297+
}
298+
err:
299+
*error = 0;
300+
cmd_query_result_srv->free_result(mysql_res);
301+
cmd_factory_srv->close(mysql_h);
302+
return result;
303+
}
304+
305+
static mysql_service_status_t init() {
306+
Udf_func_string udf1 = test_mysql_command_services_udf;
307+
if (udf_srv->udf_register("test_mysql_command_services_udf", STRING_RESULT,
308+
reinterpret_cast<Udf_func_any>(udf1), nullptr,
309+
nullptr)) {
310+
fprintf(stderr, "Can't register the test_mysql_command_services_udf UDF\n");
311+
return 1;
312+
}
313+
Udf_func_string udf2 = test_mysql_command_services_apis_udf;
314+
if (udf_srv->udf_register("test_mysql_command_services_apis_udf",
315+
STRING_RESULT, reinterpret_cast<Udf_func_any>(udf2),
316+
nullptr, nullptr)) {
317+
fprintf(stderr,
318+
"Can't register the test_mysql_command_services_apis_udf UDF\n");
319+
return 1;
320+
}
321+
return 0;
322+
}
323+
324+
static mysql_service_status_t deinit() {
325+
int was_present = 0;
326+
if (udf_srv->udf_unregister("test_mysql_command_services_udf", &was_present))
327+
fprintf(stderr,
328+
"Can't unregister the test_mysql_command_services_udf UDF\n");
329+
if (udf_srv->udf_unregister("test_mysql_command_services_apis_udf",
330+
&was_present))
331+
fprintf(stderr,
332+
"Can't unregister the test_mysql_command_services_apis_udf UDF\n");
333+
return 0; /* success */
334+
}
335+
336+
BEGIN_COMPONENT_METADATA(test_mysql_command_services)
337+
METADATA("mysql.author", "Oracle Corporation"),
338+
METADATA("mysql.license", "GPL"), METADATA("test_property", "1"),
339+
END_COMPONENT_METADATA();
340+
341+
DECLARE_COMPONENT(test_mysql_command_services,
342+
"mysql:test_mysql_command_services")
343+
init, deinit END_DECLARE_COMPONENT();
344+
345+
DECLARE_LIBRARY_COMPONENTS &COMPONENT_REF(test_mysql_command_services)
346+
END_DECLARE_LIBRARY_COMPONENTS

‎include/mysql/components/component_implementation.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,29 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
325325
const_cast <mysql_service_##service##_t **>(&name))) \
326326
}
327327

328+
/**
329+
Use this macro to reference the service placeholder as defined by the
330+
REQUIRES_SERVICE_PLACEHOLDER macro.
331+
332+
Example:
333+
REQUIRES_SERVICE_PLACEHOLDER(foo);
334+
335+
...
336+
337+
code() {
338+
....
339+
SERVICE_PLACEHOLDER(foo)->bar(1);
340+
...
341+
}
342+
343+
BEGIN_COMPONENT_REQUIRES(comp)
344+
REQUIRES_SERVICE(foo);
345+
END_COMPONENT_REQUIRES(comp)
346+
347+
@param service A valid service name.
348+
*/
349+
#define SERIVCE_PLACEHOLDER(service) mysql_service_##service
350+
328351
/**
329352
A macro to end the last declaration started with the BEGIN_COMPONENT_REQUIRES.
330353
*/

0 commit comments

Comments
 (0)