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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
/* Copyright (c) 2019, 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 "test_udf_extension.h"
#include <mysql/udf_registration_types.h>
#include <string.h>
#include <sstream>
#include "services_required.h"
namespace udf_ext {
namespace consts {
constexpr const char *charset = "charset";
constexpr const char *collation = "collation";
} // namespace consts
/**
Initializer method for UDF(s) that tests the character set conversion of
return value. It acquires necessory services, validates the input arguments,
fetch the charset of second argument, sets that as charset of return value.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@param [in] expected_arg_count The number of arguments UDf accepts
@param [out] type UDF type if it is collation or charset.s
@retval false UDF is initialized successfully.
@retval true Otherwise.
*/
bool Test_udf_charset::prepare_return_udf(UDF_INIT *initid, UDF_ARGS *args,
const size_t expected_arg_count,
Type type) {
if (Character_set_converter::acquire() || Udf_metadata::acquire()) {
Character_set_converter::release();
*s_message << Error_capture::get_last_error();
return true;
}
set_ext_type(type);
// Consider the second UDF argument to determine the charset of return value
const uint index = 1;
std::string csname;
if (validate_inputs(args, expected_arg_count) ||
fetch_charset_or_collation_from_arg(args, index, csname) ||
set_return_value_charset_or_collation(initid, csname) ||
set_udf_init(initid, args)) {
Character_set_converter::release();
Udf_metadata::release();
return true;
}
return false;
}
/**
Initializer method for UDF(s) that tests the character set conversion of the
UDF arguments. It validates the input arguments, fetches the charset or
collation from the second argument, sets the same as charset of first
argument.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@param [in] expected_arg_count The number of arguments UDf accepts
@retval false UDF is initialized successfully.
@retval true Otherwise.
*/
bool Test_udf_charset::prepare_args_udf(UDF_INIT *initid, UDF_ARGS *args,
const size_t expected_arg_count,
Type type) {
if (Character_set_converter::acquire() || Udf_metadata::acquire()) {
Character_set_converter::release();
*s_message << Error_capture::get_last_error();
return true;
}
set_ext_type(type);
const uint index = 1;
std::string csname;
if (validate_inputs(args, expected_arg_count) ||
fetch_charset_or_collation_from_arg(args, index, csname) ||
set_args_init(args, csname) || set_udf_init(initid, args)) {
Character_set_converter::release();
Udf_metadata::release();
return true;
}
return false;
}
/**
The udf implementation that tests the change in the character set of
return value. It picks the first UDF argument as return value then
it converts the return value into the charset as specified during init()
time. It uses charset converter service to do the actual conversion.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@param [out] result The return value the UDF will return
@param [out] result_len The return value the UDF will return
@retval false UDF is initialized successfully.
@retval true Otherwise.
*/
bool Test_udf_charset::run_return_udf(UDF_INIT *initid, UDF_ARGS *args,
char **result,
unsigned long &result_len) {
return Test_udf_charset_base::run_return_udf(initid, args, result,
result_len);
}
/**
The udf implementation that tests the change in the character set of the
UDF arguments. It expects the arguments values to in the character set which
was specified during the initialization time. It returns the first
UDF argument as it is.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@param [out] result The return value the UDF will return
@param [out] result_len The return value the UDF will return
@retval false Success
@retval true Otherwise.
*/
bool Test_udf_charset::run_args_udf(UDF_INIT *initid, UDF_ARGS *args,
char **result, unsigned long &result_len) {
return Test_udf_charset_base::run_args_udf(initid, args, result, result_len);
}
/**
Free the acquire resources during UDF initialization time.
@param [in] initid A pointer to the UDF_INIT structure
*/
void Test_udf_charset::deinit(UDF_INIT *initid) {
Test_udf_charset_base::deinit(initid);
}
/**
Depending upon the UDF type, fetch the charset/collation name of a
UDF argument
@param [in] args A pointer to the UDF_ARGS structure
@param [in] index Index of the UDF argument
@param [in] name Charset/collation name
@retval false Success
@retval true Otherwise.
*/
bool Test_udf_charset::fetch_charset_or_collation_from_arg(UDF_ARGS *args,
const int index,
std::string &name) {
void *p = nullptr;
if (Udf_metadata::get()->argument_get(args, s_ext_type, index, &p)) {
*s_message << "Unable to fetch extension " << s_ext_type << " of argument "
<< index + 1;
return true;
}
name = ((const char *)p);
return false;
}
/**
Initializer method for UDF(s) that tests the change in the character set of
return value. It validates the input arguments, sets the expected character
set of the return value and initialize the other UDF prerequisites.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@param [in] expected_arg_count The number of arguments UDf accepts
@retval false UDF is initialized successfully.
@retval true Otherwise.
*/
bool Test_udf_charset_const_value::prepare_return_udf(
UDF_INIT *initid, UDF_ARGS *args, const size_t expected_arg_count,
Type type) {
if (Character_set_converter::acquire() || Udf_metadata::acquire()) {
Character_set_converter::release();
*s_message << Error_capture::get_last_error();
return true;
}
set_ext_type(type);
// Consider the second UDF argument to determine the charset of return value
const uint index = 1;
std::string csname;
if (validate_inputs(args, expected_arg_count) ||
fetch_charset_or_collation_from_arg(args, index, csname) ||
set_return_value_charset_or_collation(initid, csname) ||
set_udf_init(initid, args)) {
Character_set_converter::release();
Udf_metadata::release();
return true;
}
return false;
}
/**
Initializer method for UDF(s) that tests the change in the character set
of UDF arguments. It validates the input arguments, sets the expected
character set of the UDF argument.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@param [in] expected_arg_count The number of arguments UDf accepts
@retval false UDF is initialized successfully.
@retval true Otherwise.
*/
bool Test_udf_charset_const_value::prepare_args_udf(
UDF_INIT *initid, UDF_ARGS *args, const size_t expected_arg_count,
Type type) {
if (Character_set_converter::acquire() || Udf_metadata::acquire()) {
Character_set_converter::release();
*s_message << Error_capture::get_last_error();
return true;
}
set_ext_type(type);
std::string csname;
const uint index = 1;
if (validate_inputs(args, expected_arg_count) ||
fetch_charset_or_collation_from_arg(args, index, csname) ||
set_args_init(args, csname) || set_udf_init(initid, args)) {
Character_set_converter::release();
Udf_metadata::release();
return true;
}
return false;
}
/**
The udf implementation that tests the change in the character set of
return value. It picks the first UDF argument as return value, converts
the same into the charset specified during the init method and
returns the converted value
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@param [out] result The return value the UDF will return
@param [out] result_len The return value the UDF will return
@retval false UDF is initialized successfully.
@retval true Otherwise.
*/
bool Test_udf_charset_const_value::run_return_udf(UDF_INIT *initid,
UDF_ARGS *args, char **result,
unsigned long &result_len) {
return Test_udf_charset_base::run_return_udf(initid, args, result,
result_len);
}
/**
The udf implementation that tests the change in the character set of the
UDF arguments. It expects the arguments values to in the character set which
was specified during the initialization time. It returns the first
UDF argument as it is.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@param [out] result The return value the UDF will return
@param [out] result_len The return value the UDF will return
@retval false UDF executed successfully.
@retval true Otherwise.
*/
bool Test_udf_charset_const_value::run_args_udf(UDF_INIT *initid,
UDF_ARGS *args, char **result,
unsigned long &result_len) {
return Test_udf_charset_base::run_args_udf(initid, args, result, result_len);
}
/**
Free the acquire resources during UDF initialization time.
@param [in] initid A pointer to the UDF_INIT structure
*/
void Test_udf_charset_const_value::deinit(UDF_INIT *initid) {
Test_udf_charset_base::deinit(initid);
}
/**
Fetch the charset/collation name from the UDF argument.
@param [in] args A pointer to the UDF_ARGS structure
@param [in] index Index of the argument
@param [in] name Value of the argument
*/
bool Test_udf_charset_const_value::fetch_charset_or_collation_from_arg(
UDF_ARGS *args, const int index, std::string &name) {
name = args->args[index];
if (name.empty()) {
*s_message << s_ext_type << " name cannot be empty. Specify " << s_ext_type
<< " name that is supported by server.";
}
return false;
}
std::stringstream *Test_udf_charset_base::s_message{nullptr};
void Test_udf_charset_base::udf_charset_base_init() {
s_message = new std::stringstream();
}
void Test_udf_charset_base::udf_charset_base_deinit() {
delete s_message;
s_message = nullptr;
}
const char *Test_udf_charset_base::s_ext_type;
/*
Return the last error message if encountered any.
Clear the error stream
*/
std::string Test_udf_charset_base::get_last_error() {
std::string err = s_message->str();
std::stringstream().swap(*s_message); // Reset the the Stringstream
return err;
}
/**
A helper method to validate the UDF arguments type.
@param [in] args A pointer to the UDF_ARGS structure
@param [in] expected_arg_count The number of arguments UDf accepts
@retval false Arguments are validated successfully.
@retval true Otherwise.
*/
bool Test_udf_charset_base::validate_inputs(UDF_ARGS *args,
const size_t expected_arg_count) {
if (!args) {
*s_message << "UDF_ARGS cannot be NULL.";
return true;
}
if (args->arg_count != expected_arg_count) {
*s_message << "Arguments count mismatch. Expected " << expected_arg_count
<< " while specified arguments " << args->arg_count << ".";
return true;
}
for (unsigned int i = 0; i < args->arg_count; i++) {
if (args->arg_type[i] != STRING_RESULT) {
*s_message << "This UDF accepts only string arguments. Specify argument "
<< i + 1 << " as string.";
return true;
}
}
return false;
}
/**
Fetch the expected character set of the return value. It would have been set
during init() time. Fetch the character set of the first UDF argument.
Copy the value for first UDF argument in the buffer and call character
set conversion service to convert buffer 'from' -> 'to' charset.
Return the retrieved buffer.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@param [out] result The return value the UDF will return
@param [out] result_len The return value the UDF will return
@retval false UDF executed successfully.
@retval true Otherwise.
*/
bool Test_udf_charset_base::run_return_udf(UDF_INIT *initid, UDF_ARGS *args,
char **result,
unsigned long &result_len) {
for (uint i = 0; i < args->arg_count; i++) {
if (!args->args[i]) {
*s_message << "Recieved argument " << i + 1
<< " as null. Specify valid argument";
return true;
}
}
void *return_charset_name_ptr = nullptr; // Retrieve the charset of args
if (Udf_metadata::get()->result_get(initid, consts::charset,
&return_charset_name_ptr) &&
!return_charset_name_ptr) {
*s_message << "Could not retrieve requested " << consts::charset
<< " extension argument.";
return true;
}
*result = initid->ptr; // Set the output buffer
// Retrieve the charset of first arg
void *first_arg_charset_ptr = nullptr;
const int index = 0;
if (Udf_metadata::get()->argument_get(args, consts::charset, index,
&first_arg_charset_ptr)) {
*s_message << "Could not retrieve requested " << consts::charset
<< " extension argument.";
return true;
}
// Here, we are guarateed that charset name pointers are not nullptr
const char *out_charset_name =
static_cast<const char *>(return_charset_name_ptr);
const char *in_charset_name =
static_cast<const char *>(first_arg_charset_ptr);
const std::string in_buffer(args->args[index], args->lengths[index]);
if (Character_set_converter::convert(out_charset_name, in_charset_name,
in_buffer, initid->max_length,
*result)) {
*s_message << Character_set_converter::get_last_error();
return true;
}
result_len = strlen(*result);
return false;
}
/**
A helper method to initialize the UDF return structure.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@retval false UDF_INIT structure is set for return value
@retval true Otherwise.
*/
bool Test_udf_charset_base::set_udf_init(UDF_INIT *initid, UDF_ARGS *args) {
/*
We use the first argument for testing purpose.
Max size of the converted string could be in charset utf16.
Therefore, allocate the ample memory accordingly.
*/
size_t length = args->lengths[0] * 4 + 1;
try {
initid->ptr = new char[length];
} catch (...) {
*s_message << "UDF could not allocate the memory. Try after some time once "
"the load on server is reduced.";
return true;
}
initid->max_length = length;
initid->maybe_null = true;
return false;
}
/**
Set the charset or collaion for the first UDF argument.
@param [in] args A pointer to the UDF_ARGS structure
@param [in] name Value to be set.
@retval false Success
@retval true Otherwise
*/
bool Test_udf_charset_base::set_args_init(UDF_ARGS *args,
const std::string &name) {
char *value = const_cast<char *>(name.c_str());
if (Udf_metadata::get()->argument_set(args, s_ext_type, 0,
static_cast<void *>(value))) {
*s_message << "Could not set the " << s_ext_type << " = " << name;
return true;
}
return false;
}
void Test_udf_charset_base::set_ext_type(Type ext_type) {
s_ext_type = consts::charset;
if (ext_type == Type::collation) s_ext_type = consts::collation;
}
/**
The udf implementation that tests the change in the character set of the
UDF arguments. It expects the arguments values to in the character set which
was specified during the initialization time. It returns the first
UDF argument as it is.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] args A pointer to the UDF_ARGS structure
@param [out] result The return value the UDF will return
@param [out] result_len The return value the UDF will return
@retval false UDF is initialized successfully.
@retval true Otherwise.
*/
bool Test_udf_charset_base::run_args_udf(UDF_INIT *initid, UDF_ARGS *args,
char **result,
unsigned long &result_len) {
for (uint i = 0; i < args->arg_count; i++) {
if (!args->args[i]) {
*s_message << "Recieved argument " << i + 1
<< " as null. Specify valid argument";
return true;
}
}
const int index = 0; // Return the first argument
strncpy(initid->ptr, args->args[index], args->lengths[index]);
result_len = args->lengths[index];
*result = initid->ptr;
return false;
}
/* Cleanup the acquired resources during UDF init() */
void Test_udf_charset_base::deinit(UDF_INIT *initd) {
delete[] initd->ptr;
Character_set_converter::release();
Udf_metadata::release();
}
/**
A helper method that sets the charset names in the extension argument of
return value.
@param [in] initid A pointer to the UDF_INIT structure
@param [in] name Value to be set in the extension argument.
@retval false Value is set successfully.
@retval true Otherwise.
*/
bool Test_udf_charset_base::set_return_value_charset_or_collation(
UDF_INIT *initid, const std::string &name) {
char *ret_name = const_cast<char *>(name.c_str());
if (Udf_metadata::get()->result_set(initid, s_ext_type, (void *)(ret_name))) {
*s_message << "Unable to set " << s_ext_type << " : " << name
<< " of result argument. Specify " << s_ext_type
<< " name which is supported by Server.";
return true;
}
return false;
}
} // namespace udf_ext
|