|
| 1 | +/** |
| 2 | + * @file reg_test_3493-USE_with_comment-t.cpp |
| 3 | + * @brief This test verifies that a 'USE' statement is properly tracked by |
| 4 | + * ProxySQL, even when executed with a leading comment. |
| 5 | + * @details For being sure that the feature is properly supported the |
| 6 | + * test performs the following actions: |
| 7 | + * |
| 8 | + * 1. Open a MYSQL connection to ProxySQL. |
| 9 | + * 2. Drops and creates a new database called 'reg_test_3493_use_comment'. |
| 10 | + * 3. Checks the currently selected database in **a new backend database |
| 11 | + * connection** by means of the connection annotation |
| 12 | + * "create_new_connection=1". This way it's ensured that ProxySQL is |
| 13 | + * properly keeping track of the database selected in the issued 'USE' |
| 14 | + * statement. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <cstring> |
| 18 | +#include <vector> |
| 19 | +#include <string> |
| 20 | +#include <stdio.h> |
| 21 | + |
| 22 | +#include <mysql.h> |
| 23 | + |
| 24 | +#include "tap.h" |
| 25 | +#include "command_line.h" |
| 26 | +#include "utils.h" |
| 27 | +#include "json.hpp" |
| 28 | + |
| 29 | +using nlohmann::json; |
| 30 | + |
| 31 | +void parse_result_json_column(MYSQL_RES *result, json& j) { |
| 32 | + if(!result) return; |
| 33 | + MYSQL_ROW row; |
| 34 | + |
| 35 | + while ((row = mysql_fetch_row(result))) { |
| 36 | + j = json::parse(row[0]); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +int main(int argc, char** argv) { |
| 41 | + CommandLine cl; |
| 42 | + |
| 43 | + if (cl.getEnv()) { |
| 44 | + diag("Failed to get the required environmental variables."); |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + MYSQL* proxysql_mysql = mysql_init(NULL); |
| 49 | + |
| 50 | + if ( |
| 51 | + !mysql_real_connect( |
| 52 | + proxysql_mysql, cl.host, cl.username, cl.password, NULL, cl.port, NULL, 0 |
| 53 | + ) |
| 54 | + ) { |
| 55 | + fprintf( |
| 56 | + stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, |
| 57 | + mysql_error(proxysql_mysql) |
| 58 | + ); |
| 59 | + return EXIT_FAILURE; |
| 60 | + } |
| 61 | + |
| 62 | + // Prepare the DB for the test |
| 63 | + MYSQL_QUERY(proxysql_mysql, "DROP DATABASE IF EXISTS reg_test_3493_use_comment"); |
| 64 | + MYSQL_QUERY(proxysql_mysql, "CREATE DATABASE reg_test_3493_use_comment"); |
| 65 | + |
| 66 | + int err = mysql_query(proxysql_mysql, "/*+ placeholder_comment */ USE reg_test_3493_use_comment"); |
| 67 | + if (err) { |
| 68 | + diag( |
| 69 | + "'USE' command failed with error code '%d' and error '%s'", |
| 70 | + err, mysql_error(proxysql_mysql) |
| 71 | + ); |
| 72 | + return EXIT_FAILURE; |
| 73 | + } |
| 74 | + |
| 75 | + // Perform the 'SELECT DATABASE()' query in a new backend connection, to |
| 76 | + // verify that ProxySQL is properly tracking the previously performed 'USE' |
| 77 | + // statement. |
| 78 | + MYSQL_QUERY(proxysql_mysql, "/*+ ;create_new_connection=1 */ SELECT DATABASE()"); |
| 79 | + MYSQL_RES* result = mysql_store_result(proxysql_mysql); |
| 80 | + if (result == nullptr) { |
| 81 | + diag("Invalid 'MYSQL_RES' returned from 'SELECT DATABASE()'"); |
| 82 | + return EXIT_FAILURE; |
| 83 | + } |
| 84 | + |
| 85 | + MYSQL_ROW row = mysql_fetch_row(result); |
| 86 | + if (row == nullptr) { |
| 87 | + diag("Invalid 'MYSQL_ROW' returned from 'SELECT DATABASE()'"); |
| 88 | + return EXIT_FAILURE; |
| 89 | + } |
| 90 | + |
| 91 | + std::string database_name { row[0] }; |
| 92 | + |
| 93 | + ok( |
| 94 | + database_name == "reg_test_3493_use_comment", |
| 95 | + "Selected DB name should be equal to actual DB name: (Exp: '%s') == (Act: '%s')", |
| 96 | + "reg_test_3493_use_comment", |
| 97 | + database_name.c_str() |
| 98 | + ); |
| 99 | + |
| 100 | + // Drop created database |
| 101 | + MYSQL_QUERY(proxysql_mysql, "DROP DATABASE IF EXISTS reg_test_3493_use_comment"); |
| 102 | + |
| 103 | + mysql_close(proxysql_mysql); |
| 104 | + |
| 105 | + return exit_status(); |
| 106 | +} |
0 commit comments