1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/* Copyright (c) 2022, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <mysql.h>
#include <mysql/components/component_implementation.h>
#include <mysql/components/my_service.h>
#include <mysql/components/services/mysql_command_services.h>
#include <mysql/components/services/security_context.h>
#include <mysql/components/services/udf_registration.h>
#include <mysql/service_srv_session_info.h>
#include <stdio.h>
#include <string>
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_thd_security_context, thd_security_ctx);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_account_database_security_context_lookup,
account_db_security_ctx_lookup);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_security_context_options,
security_ctx_options);
REQUIRES_SERVICE_PLACEHOLDER_AS(udf_registration, udf_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_factory, cmd_factory_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_options, cmd_options_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_query, cmd_query_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_query_result,
cmd_query_result_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_field_info, cmd_field_info_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_error_info, cmd_error_info_srv);
REQUIRES_SERVICE_PLACEHOLDER_AS(mysql_command_field_metadata,
cmd_field_meta_srv);
BEGIN_COMPONENT_PROVIDES(test_mysql_command_services)
END_COMPONENT_PROVIDES();
BEGIN_COMPONENT_REQUIRES(test_mysql_command_services)
REQUIRES_SERVICE_AS(udf_registration, udf_srv),
REQUIRES_SERVICE_AS(mysql_thd_security_context, thd_security_ctx),
REQUIRES_SERVICE_AS(mysql_account_database_security_context_lookup,
account_db_security_ctx_lookup),
REQUIRES_SERVICE_AS(mysql_security_context_options, security_ctx_options),
REQUIRES_SERVICE_AS(mysql_command_factory, cmd_factory_srv),
REQUIRES_SERVICE_AS(mysql_command_options, cmd_options_srv),
REQUIRES_SERVICE_AS(mysql_command_query, cmd_query_srv),
REQUIRES_SERVICE_AS(mysql_command_query_result, cmd_query_result_srv),
REQUIRES_SERVICE_AS(mysql_command_field_info, cmd_field_info_srv),
REQUIRES_SERVICE_AS(mysql_command_error_info, cmd_error_info_srv),
REQUIRES_SERVICE_AS(mysql_command_field_metadata, cmd_field_meta_srv),
END_COMPONENT_REQUIRES();
MYSQL_H mysql_h = nullptr;
MYSQL_LEX_CSTRING user;
MYSQL_LEX_CSTRING host;
static char *test_mysql_command_services_udf(UDF_INIT *, UDF_ARGS *args,
char *result,
unsigned long *length,
unsigned char *,
unsigned char *error) {
*error = 1;
if (args->arg_count == 0) {
return nullptr;
}
MYSQL_RES_H mysql_res = nullptr;
MYSQL_ROW_H row = nullptr;
MYSQL_FIELD_H *fields_h = nullptr;
MYSQL_FIELD_H field_h = nullptr;
unsigned int field_count;
uint64_t row_count = 0;
unsigned int num_column = 0;
std::string result_set;
unsigned int err_no;
char *sqlstate_errmsg[50];
/* reset to empty as a start */
*result = 0;
// Execute the SQL specified in the argument.
if (cmd_factory_srv->init(&mysql_h)) {
return nullptr;
}
if (mysql_h) {
if (cmd_factory_srv->connect(mysql_h)) {
return nullptr;
}
} else {
return nullptr;
}
std::string query(args->args[0], args->lengths[0]);
std::size_t number_of_query_executions{1U};
if (args->arg_count > 1U && args->arg_type[1] == INT_RESULT) {
number_of_query_executions = *reinterpret_cast<long long *>(args->args[1]);
}
for (std::size_t u{0U}; u < number_of_query_executions; ++u) {
result_set.clear();
// It is OK to call free_result() with nullptr MYSQL_RES_H.
cmd_query_result_srv->free_result(mysql_res);
mysql_res = nullptr;
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
cmd_error_info_srv->sql_error(mysql_h, &result);
*length = strlen(result);
goto err;
}
cmd_query_result_srv->store_result(mysql_h, &mysql_res);
if (mysql_res) {
if (cmd_query_srv->affected_rows(mysql_h, &row_count)) {
result = nullptr;
goto err;
}
if (cmd_field_info_srv->num_fields(mysql_res, &num_column)) {
result = nullptr;
goto err;
}
if (cmd_field_info_srv->field_count(mysql_h, &field_count)) {
result = nullptr;
goto err;
}
if (field_count > 0) {
if (cmd_field_info_srv->fetch_field(mysql_res, &field_h)) {
result = nullptr;
goto err;
}
if (cmd_field_info_srv->fetch_fields(mysql_res, &fields_h)) {
result = nullptr;
goto err;
}
const char *field_name = nullptr, *table_name = nullptr,
*db_name = nullptr;
if (cmd_field_meta_srv->get(field_h, MYSQL_COMMAND_FIELD_METADATA_NAME,
&field_name) ||
!field_name) {
result = nullptr;
goto err;
}
if (cmd_field_meta_srv->get(field_h,
MYSQL_COMMAND_FIELD_METADATA_TABLE_NAME,
&table_name)) {
result = nullptr;
goto err;
}
if (cmd_field_meta_srv->get(field_h,
MYSQL_COMMAND_FIELD_METADATA_TABLE_DB_NAME,
&db_name)) {
result = nullptr;
goto err;
}
}
for (uint64_t i = 0; i < row_count; i++) {
if (cmd_query_result_srv->fetch_row(mysql_res, &row)) {
result = nullptr;
goto err;
}
ulong *length = nullptr;
if (cmd_query_result_srv->fetch_lengths(mysql_res, &length)) {
result = nullptr;
goto err;
}
for (unsigned int j = 0; j < num_column; j++) {
result_set += row[j];
}
}
/* The caller has the buffer limit, and the size is of MAX_FIELD_WIDTH
size so we are truncating the result of the query output if it has more
date
*/
if (u == 0U) {
/* Make sure we return results from the very first execution */
strncpy(
result,
reinterpret_cast<char *>(const_cast<char *>(result_set.c_str())),
(result_set.length() < *length) ? result_set.length()
: (*length - 1));
*length = (result_set.length() < *length) ? result_set.length()
: (*length - 1);
result[*length] = '\0';
}
} else {
if (u == 0U) {
cmd_error_info_srv->sql_error(mysql_h, &result);
cmd_error_info_srv->sql_errno(mysql_h, &err_no);
cmd_error_info_srv->sql_state(mysql_h, sqlstate_errmsg);
*length = strlen(result);
}
}
}
err:
*error = 0;
cmd_query_result_srv->free_result(mysql_res);
cmd_factory_srv->close(mysql_h);
return result;
}
static char *test_mysql_command_services_apis_udf(UDF_INIT *, UDF_ARGS *args,
char *result,
unsigned long *length,
unsigned char *,
unsigned char *error) {
*error = 1;
if (args->arg_count > 0) {
return nullptr;
}
MYSQL_RES_H mysql_res = nullptr;
MYSQL_ROW_H row = nullptr;
uint64_t row_count = 0;
unsigned int num_column = 0;
std::string result_set;
// Execute the SQL specified in the argument.
if (cmd_factory_srv->init(&mysql_h)) {
return nullptr;
}
if (mysql_h) {
if (cmd_factory_srv->connect(mysql_h)) {
return nullptr;
}
} else {
return nullptr;
}
if (cmd_factory_srv->reset(mysql_h)) {
goto err;
}
/* set AUTOCOMMIT to OFF */
if (cmd_factory_srv->autocommit(mysql_h, false)) {
goto err;
}
{
std::string query("DROP TABLE IF EXISTS test.my_demo_transaction");
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
*length = strlen(result);
goto err;
}
}
/* To get the mysql option value */
void *option_val;
cmd_options_srv->get(mysql_h, MYSQL_OPT_MAX_ALLOWED_PACKET, &option_val);
{
std::string query(
"CREATE TABLE test.my_demo_transaction( "
"col1 int , col2 varchar(30))");
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
goto err;
}
}
{
std::string query(
"INSERT INTO test.my_demo_transaction VALUES(10, 'mysql-1')");
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
goto err;
}
}
/* Commiting the transaction */
if (cmd_factory_srv->commit(mysql_h)) {
goto err;
}
/* now insert the second row, and roll back the transaction */
{
std::string query(
"INSERT INTO test.my_demo_transaction VALUES(20, 'mysql-2')");
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
goto err;
}
}
/* Commiting the transaction */
if (cmd_factory_srv->rollback(mysql_h)) {
goto err;
}
{
std::string query("SELECT * from test.my_demo_transaction");
if (cmd_query_srv->query(mysql_h, query.data(), query.length())) {
goto err;
}
}
cmd_query_result_srv->store_result(mysql_h, &mysql_res);
if (mysql_res) {
if (cmd_query_srv->affected_rows(mysql_h, &row_count)) {
result = nullptr;
goto err;
}
if (cmd_field_info_srv->num_fields(mysql_res, &num_column)) {
result = nullptr;
goto err;
}
for (uint64_t i = 0; i < row_count; i++) {
if (cmd_query_result_srv->fetch_row(mysql_res, &row)) {
result = nullptr;
goto err;
}
ulong *length = nullptr;
cmd_query_result_srv->fetch_lengths(mysql_res, &length);
for (unsigned int j = 0; j < num_column; j++) {
result_set += row[j];
}
}
cmd_query_result_srv->more_results(mysql_h);
cmd_query_result_srv->next_result(mysql_h);
cmd_query_result_srv->result_metadata(mysql_res);
/* The caller has the buffer limit, and the size is of MAX_FIELD_WIDTH size
so we are truncating the result of the query output if it has more date
*/
strncpy(
result,
reinterpret_cast<char *>(const_cast<char *>(result_set.c_str())),
(result_set.length() < *length) ? result_set.length() : (*length - 1));
*length =
(result_set.length() < *length) ? result_set.length() : (*length - 1);
result[*length] = '\0';
}
*error = 0;
err:
cmd_query_result_srv->free_result(mysql_res);
cmd_factory_srv->close(mysql_h);
return result;
}
static mysql_service_status_t init() {
Udf_func_string udf1 = test_mysql_command_services_udf;
if (udf_srv->udf_register("test_mysql_command_services_udf", STRING_RESULT,
reinterpret_cast<Udf_func_any>(udf1), nullptr,
nullptr)) {
fprintf(stderr, "Can't register the test_mysql_command_services_udf UDF\n");
return 1;
}
Udf_func_string udf2 = test_mysql_command_services_apis_udf;
if (udf_srv->udf_register("test_mysql_command_services_apis_udf",
STRING_RESULT, reinterpret_cast<Udf_func_any>(udf2),
nullptr, nullptr)) {
fprintf(stderr,
"Can't register the test_mysql_command_services_apis_udf UDF\n");
return 1;
}
return 0;
}
static mysql_service_status_t deinit() {
int was_present = 0;
if (udf_srv->udf_unregister("test_mysql_command_services_udf", &was_present))
fprintf(stderr,
"Can't unregister the test_mysql_command_services_udf UDF\n");
if (udf_srv->udf_unregister("test_mysql_command_services_apis_udf",
&was_present))
fprintf(stderr,
"Can't unregister the test_mysql_command_services_apis_udf UDF\n");
return 0; /* success */
}
BEGIN_COMPONENT_METADATA(test_mysql_command_services)
METADATA("mysql.author", "Oracle Corporation"),
METADATA("mysql.license", "GPL"), METADATA("test_property", "1"),
END_COMPONENT_METADATA();
DECLARE_COMPONENT(test_mysql_command_services,
"mysql:test_mysql_command_services")
init, deinit END_DECLARE_COMPONENT();
DECLARE_LIBRARY_COMPONENTS &COMPONENT_REF(test_mysql_command_services)
END_DECLARE_LIBRARY_COMPONENTS
|