Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include <my_global.h>
- #include <my_sys.h>
- #include <mysql.h>
- /*
- nixnutz@linux-fuxh:~/mysql_api> echo $CFG; echo ""; sh -c "gcc -o progname `$CFG --cflags` statement_explain_meta.c `$CFG --libs` && ./progname"
- /home/nixnutz/ftp/mysql-5.6.2-m5/install/bin/mysql_config
- Server: 5.6.2-m5-debug
- Protocol: 10
- Client: 5.1.49
- column = 'ALL'
- length = 3
- stmt->fields[3].type = 253
- stmt->fields[3].max_length = 15
- stmt->fields[3].length = 30
- NOTE: data has been truncated!
- sqlstate = '00000'
- column = 'unique_subquery'
- length = 15
- stmt->fields[3].type = 253
- stmt->fields[3].max_length = 15
- stmt->fields[3].length = 30
- NOTE: data has been truncated!
- sqlstate = '00000'
- */
- int exit_failure(const char *msg, MYSQL *mysql, MYSQL_RES *res, MYSQL_STMT *stmt);
- /* Connection parameter - change */
- const char *con_host = "127.0.0.1";
- const char *con_user = "root";
- const char *con_pass = "root";
- const char *con_db = "test";
- unsigned int con_port = 3306;
- const char *con_socket = "/tmp/mysql.sock";
- unsigned long con_flags = 0;
- const char* drop = "DROP TABLE IF EXISTS mytest_table";
- const char* create = "CREATE TABLE mytest_table(id INT PRIMARY KEY NOT NULL, idParent INT)";
- const char* insert = "INSERT INTO mytest_table(id, idParent) VALUES (1, -1), (2, 1)";
- const char* explain = "EXPLAIN SELECT id FROM mytest_table WHERE idParent <> -1 AND idParent NOT IN ( SELECT id FROM mytest_table);";
- int main(void) {
- MYSQL *conn;
- MYSQL_STMT *stmt;
- MYSQL_RES *res;
- MYSQL_FIELD *column;
- MYSQL_BIND bind[10];
- // int column_value;
- char column_value[255];
- unsigned long length;
- int i;
- my_bool is_null;
- my_bool error;
- my_bool attr_get;
- unsigned int ret;
- printf("\n");
- conn = mysql_init(NULL);
- if (conn == NULL)
- exit_failure("mysql_init() failed", NULL, NULL, NULL);
- if (mysql_real_connect(conn, con_host, con_user, con_pass, con_db, con_port, con_socket, con_flags) == NULL)
- exit_failure("mysql_real_connect() failed", conn, NULL, NULL);
- printf("Server: %s\n", mysql_get_server_info(conn));
- printf("Protocol: %u\n", mysql_get_proto_info(conn));
- printf("Client: %s\n\n", mysql_get_client_info());
- if (mysql_query(conn, drop) ||
- mysql_query(conn, create) ||
- mysql_query(conn, insert))
- exit_failure("Setup failed", conn, NULL, NULL);
- stmt = mysql_stmt_init(conn);
- if (mysql_stmt_prepare(stmt, explain, strlen(explain)))
- exit_failure("mysql_stmt_prepare() failed", conn, NULL, stmt);
- if (mysql_stmt_execute(stmt))
- exit_failure("mysql_stmt_execute() failed", conn, NULL, stmt);
- attr_get = 1;
- if (mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &attr_get))
- exit_failure("mysql_stmt_attr_set() failed", conn, NULL, stmt);
- if (mysql_stmt_store_result(stmt))
- exit_failure("mysql_stmt_store_result() failed", conn, NULL, stmt);
- {
- MYSQL_RES * res = mysql_stmt_result_metadata(stmt);
- MYSQL_FIELD * field;
- if (res) {
- while ((field = mysql_fetch_field(res))) {
- printf("name = [%s]\n", field->name);
- printf("db = [%s]\n", field->db);
- printf("db_length = %u\n", field->db_length);
- }
- mysql_free_result(res);
- }
- }
- mysql_stmt_close(stmt);
- mysql_close(conn);
- printf("\n");
- return EXIT_SUCCESS;
- }
- int exit_failure(const char *msg, MYSQL *mysql, MYSQL_RES *res, MYSQL_STMT *stmt) {
- printf("ERROR: %s\n", msg);
- if (stmt) {
- printf("[%u] %s\n", mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
- mysql_stmt_close(stmt);
- }
- if (mysql) {
- if (mysql_errno(mysql))
- printf("[%u] %s\n", mysql_errno(mysql), mysql_error(mysql));
- if (res)
- mysql_free_result(res);
- mysql_close(mysql);
- }
- printf("\n");
- exit(EXIT_FAILURE);
- }
Add Comment
Please, Sign In to add comment