|
| 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 |
0 commit comments