Guest User

Untitled

a guest
Oct 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. #include <my_global.h>
  5. #include <my_sys.h>
  6. #include <mysql.h>
  7.  
  8.  
  9. /*
  10. nixnutz@linux-fuxh:~/mysql_api> echo $CFG; echo ""; sh -c "gcc -o progname `$CFG --cflags` statement_explain_meta.c `$CFG --libs` && ./progname"
  11. /home/nixnutz/ftp/mysql-5.6.2-m5/install/bin/mysql_config
  12.  
  13.  
  14. Server: 5.6.2-m5-debug
  15. Protocol: 10
  16. Client: 5.1.49
  17.  
  18. column = 'ALL'
  19. length = 3
  20. stmt->fields[3].type = 253
  21. stmt->fields[3].max_length = 15
  22. stmt->fields[3].length = 30
  23. NOTE: data has been truncated!
  24. sqlstate = '00000'
  25.  
  26. column = 'unique_subquery'
  27. length = 15
  28. stmt->fields[3].type = 253
  29. stmt->fields[3].max_length = 15
  30. stmt->fields[3].length = 30
  31. NOTE: data has been truncated!
  32. sqlstate = '00000'
  33.  
  34.  
  35. */
  36.  
  37. int exit_failure(const char *msg, MYSQL *mysql, MYSQL_RES *res, MYSQL_STMT *stmt);
  38.  
  39. /* Connection parameter - change */
  40. const char *con_host = "127.0.0.1";
  41. const char *con_user = "root";
  42. const char *con_pass = "root";
  43. const char *con_db = "test";
  44. unsigned int con_port = 3306;
  45. const char *con_socket = "/tmp/mysql.sock";
  46. unsigned long con_flags = 0;
  47.  
  48. const char* drop = "DROP TABLE IF EXISTS mytest_table";
  49. const char* create = "CREATE TABLE mytest_table(id INT PRIMARY KEY NOT NULL, idParent INT)";
  50. const char* insert = "INSERT INTO mytest_table(id, idParent) VALUES (1, -1), (2, 1)";
  51. const char* explain = "EXPLAIN SELECT id FROM mytest_table WHERE idParent <> -1 AND idParent NOT IN ( SELECT id FROM mytest_table);";
  52.  
  53. int main(void) {
  54. MYSQL *conn;
  55. MYSQL_STMT *stmt;
  56. MYSQL_RES *res;
  57. MYSQL_FIELD *column;
  58.  
  59. MYSQL_BIND bind[10];
  60. // int column_value;
  61. char column_value[255];
  62. unsigned long length;
  63. int i;
  64. my_bool is_null;
  65. my_bool error;
  66. my_bool attr_get;
  67.  
  68. unsigned int ret;
  69.  
  70. printf("\n");
  71.  
  72. conn = mysql_init(NULL);
  73. if (conn == NULL)
  74. exit_failure("mysql_init() failed", NULL, NULL, NULL);
  75.  
  76. if (mysql_real_connect(conn, con_host, con_user, con_pass, con_db, con_port, con_socket, con_flags) == NULL)
  77. exit_failure("mysql_real_connect() failed", conn, NULL, NULL);
  78.  
  79. printf("Server: %s\n", mysql_get_server_info(conn));
  80. printf("Protocol: %u\n", mysql_get_proto_info(conn));
  81. printf("Client: %s\n\n", mysql_get_client_info());
  82.  
  83. if (mysql_query(conn, drop) ||
  84. mysql_query(conn, create) ||
  85. mysql_query(conn, insert))
  86. exit_failure("Setup failed", conn, NULL, NULL);
  87.  
  88. stmt = mysql_stmt_init(conn);
  89. if (mysql_stmt_prepare(stmt, explain, strlen(explain)))
  90. exit_failure("mysql_stmt_prepare() failed", conn, NULL, stmt);
  91.  
  92. if (mysql_stmt_execute(stmt))
  93. exit_failure("mysql_stmt_execute() failed", conn, NULL, stmt);
  94.  
  95. attr_get = 1;
  96. if (mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &attr_get))
  97. exit_failure("mysql_stmt_attr_set() failed", conn, NULL, stmt);
  98.  
  99. if (mysql_stmt_store_result(stmt))
  100. exit_failure("mysql_stmt_store_result() failed", conn, NULL, stmt);
  101.  
  102. {
  103. MYSQL_RES * res = mysql_stmt_result_metadata(stmt);
  104. MYSQL_FIELD * field;
  105. if (res) {
  106. while ((field = mysql_fetch_field(res))) {
  107. printf("name = [%s]\n", field->name);
  108. printf("db = [%s]\n", field->db);
  109. printf("db_length = %u\n", field->db_length);
  110. }
  111. mysql_free_result(res);
  112. }
  113. }
  114.  
  115. mysql_stmt_close(stmt);
  116. mysql_close(conn);
  117. printf("\n");
  118.  
  119. return EXIT_SUCCESS;
  120. }
  121.  
  122. int exit_failure(const char *msg, MYSQL *mysql, MYSQL_RES *res, MYSQL_STMT *stmt) {
  123. printf("ERROR: %s\n", msg);
  124. if (stmt) {
  125. printf("[%u] %s\n", mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
  126. mysql_stmt_close(stmt);
  127. }
  128. if (mysql) {
  129. if (mysql_errno(mysql))
  130. printf("[%u] %s\n", mysql_errno(mysql), mysql_error(mysql));
  131. if (res)
  132. mysql_free_result(res);
  133. mysql_close(mysql);
  134. }
  135. printf("\n");
  136. exit(EXIT_FAILURE);
  137. }
Add Comment
Please, Sign In to add comment