📄 mysql_client_test.c
字号:
}/* For given array of queries, open query_count cursors and fetch from them in simultaneous manner. In case there was an error in one of the cursors, continue reading from the rest.*/enum fetch_type { USE_ROW_BY_ROW_FETCH= 0, USE_STORE_RESULT= 1 };my_bool fetch_n(const char **query_list, unsigned query_count, enum fetch_type fetch_type){ unsigned open_statements= query_count; int rc, error_count= 0; Stmt_fetch *fetch_array= (Stmt_fetch*) calloc(1, sizeof(Stmt_fetch) * query_count); Stmt_fetch *fetch; DBUG_ENTER("fetch_n"); for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch) { /* Init will exit(1) in case of error */ stmt_fetch_init(fetch, fetch - fetch_array, query_list[fetch - fetch_array]); } if (fetch_type == USE_STORE_RESULT) { for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch) { rc= mysql_stmt_store_result(fetch->handle); check_execute(fetch->handle, rc); } } while (open_statements) { for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch) { if (fetch->is_open && (rc= stmt_fetch_fetch_row(fetch))) { open_statements--; /* We try to fetch from the rest of the statements in case of error */ if (rc != MYSQL_NO_DATA) { fprintf(stderr, "Got error reading rows from statement %d,\n" "query is: %s,\n" "error message: %s", (int) (fetch - fetch_array), fetch->query, mysql_stmt_error(fetch->handle)); error_count++; } } } } if (error_count) fprintf(stderr, "Fetch FAILED"); else { unsigned total_row_count= 0; for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch) total_row_count+= fetch->row_count; if (!opt_silent) printf("Success, total rows fetched: %d\n", total_row_count); } for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch) stmt_fetch_close(fetch); free(fetch_array); DBUG_RETURN(error_count != 0);}/* Separate thread query to test some cases */static my_bool thread_query(char *query){ MYSQL *l_mysql; my_bool error; error= 0; if (!opt_silent) fprintf(stdout, "\n in thread_query(%s)", query); if (!(l_mysql= mysql_init(NULL))) { myerror("mysql_init() failed"); return 1; } if (!(mysql_real_connect(l_mysql, opt_host, opt_user, opt_password, current_db, opt_port, opt_unix_socket, 0))) { myerror("connection failed"); error= 1; goto end; } l_mysql->reconnect= 1; if (mysql_query(l_mysql, (char *)query)) { fprintf(stderr, "Query failed (%s)\n", mysql_error(l_mysql)); error= 1; goto end; } mysql_commit(l_mysql);end: mysql_close(l_mysql); return error;}/* Query processing */static void test_debug_example(){ int rc; MYSQL_RES *result; myheader("test_debug_example"); rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_debug_example"); myquery(rc); rc= mysql_query(mysql, "CREATE TABLE test_debug_example(" "id INT PRIMARY KEY AUTO_INCREMENT, " "name VARCHAR(20), xxx INT)"); myquery(rc); rc= mysql_query(mysql, "INSERT INTO test_debug_example (name) " "VALUES ('mysql')"); myquery(rc); rc= mysql_query(mysql, "UPDATE test_debug_example SET name='updated' " "WHERE name='deleted'"); myquery(rc); rc= mysql_query(mysql, "SELECT * FROM test_debug_example where name='mysql'"); myquery(rc); result= mysql_use_result(mysql); mytest(result); (void) my_process_result_set(result); mysql_free_result(result); rc= mysql_query(mysql, "DROP TABLE test_debug_example"); myquery(rc);}/* Test autocommit feature for BDB tables */static void test_tran_bdb(){ MYSQL_RES *result; MYSQL_ROW row; int rc; myheader("test_tran_bdb"); /* set AUTOCOMMIT to OFF */ rc= mysql_autocommit(mysql, FALSE); myquery(rc); rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction"); myquery(rc); /* create the table 'mytran_demo' of type BDB' or 'InnoDB' */ rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction( " "col1 int , col2 varchar(30)) TYPE= BDB"); myquery(rc); /* insert a row and commit the transaction */ rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')"); myquery(rc); rc= mysql_commit(mysql); myquery(rc); /* now insert the second row, and roll back the transaction */ rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')"); myquery(rc); rc= mysql_rollback(mysql); myquery(rc); /* delete first row, and roll it back */ rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10"); myquery(rc); rc= mysql_rollback(mysql); myquery(rc); /* test the results now, only one row should exist */ rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction"); myquery(rc); /* get the result */ result= mysql_store_result(mysql); mytest(result); (void) my_process_result_set(result); mysql_free_result(result); /* test the results now, only one row should exist */ rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction"); myquery(rc); /* get the result */ result= mysql_use_result(mysql); mytest(result); row= mysql_fetch_row(result); mytest(row); row= mysql_fetch_row(result); mytest_r(row); mysql_free_result(result); mysql_autocommit(mysql, TRUE);}/* Test autocommit feature for InnoDB tables */static void test_tran_innodb(){ MYSQL_RES *result; MYSQL_ROW row; int rc; myheader("test_tran_innodb"); /* set AUTOCOMMIT to OFF */ rc= mysql_autocommit(mysql, FALSE); myquery(rc); rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction"); myquery(rc); /* create the table 'mytran_demo' of type BDB' or 'InnoDB' */ rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction(col1 int, " "col2 varchar(30)) TYPE= InnoDB"); myquery(rc); /* insert a row and commit the transaction */ rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')"); myquery(rc); rc= mysql_commit(mysql); myquery(rc); /* now insert the second row, and roll back the transaction */ rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')"); myquery(rc); rc= mysql_rollback(mysql); myquery(rc); /* delete first row, and roll it back */ rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10"); myquery(rc); rc= mysql_rollback(mysql); myquery(rc); /* test the results now, only one row should exist */ rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction"); myquery(rc); /* get the result */ result= mysql_store_result(mysql); mytest(result); (void) my_process_result_set(result); mysql_free_result(result); /* test the results now, only one row should exist */ rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction"); myquery(rc); /* get the result */ result= mysql_use_result(mysql); mytest(result); row= mysql_fetch_row(result); mytest(row); row= mysql_fetch_row(result); mytest_r(row); mysql_free_result(result); mysql_autocommit(mysql, TRUE);}/* Test for BUG#7242 */static void test_prepare_insert_update(){ MYSQL_STMT *stmt; int rc; int i; const char *testcase[]= { "CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B))", "INSERT t1 VALUES (1,2,10), (3,4,20)", "INSERT t1 VALUES (5,6,30), (7,4,40), (8,9,60) ON DUPLICATE KEY UPDATE c=c+100", "SELECT * FROM t1", "INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0", "SELECT * FROM t1", "INSERT t1 VALUES (2,1,11), (7,4,40) ON DUPLICATE KEY UPDATE c=c+VALUES(a)", NULL}; const char **cur_query; myheader("test_prepare_insert_update"); for (cur_query= testcase; *cur_query; cur_query++) { printf("\nRunning query: %s", *cur_query); strmov(query, *cur_query); stmt= mysql_simple_prepare(mysql, query); check_stmt(stmt); verify_param_count(stmt, 0); rc= mysql_stmt_execute(stmt); check_execute(stmt, rc); /* try the last query several times */ if (!cur_query[1]) { for (i=0; i < 3;i++) { printf("\nExecuting last statement again"); rc= mysql_stmt_execute(stmt); check_execute(stmt, rc); rc= mysql_stmt_execute(stmt); check_execute(stmt, rc); } } mysql_stmt_close(stmt); } rc= mysql_commit(mysql); myquery(rc);}/* Test simple prepares of all DML statements */static void test_prepare_simple(){ MYSQL_STMT *stmt; int rc; myheader("test_prepare_simple"); rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_simple"); myquery(rc); rc= mysql_query(mysql, "CREATE TABLE test_prepare_simple(" "id int, name varchar(50))"); myquery(rc); /* insert */ strmov(query, "INSERT INTO test_prepare_simple VALUES(?, ?)"); stmt= mysql_simple_prepare(mysql, query); check_stmt(stmt); verify_param_count(stmt, 2); mysql_stmt_close(stmt); /* update */ strmov(query, "UPDATE test_prepare_simple SET id=? " "WHERE id=? AND CONVERT(name USING utf8)= ?"); stmt= mysql_simple_prepare(mysql, query); check_stmt(stmt); verify_param_count(stmt, 3); mysql_stmt_close(stmt); /* delete */ strmov(query, "DELETE FROM test_prepare_simple WHERE id=10"); stmt= mysql_simple_prepare(mysql, query); check_stmt(stmt); verify_param_count(stmt, 0); rc= mysql_stmt_execute(stmt); check_execute(stmt, rc); mysql_stmt_close(stmt); /* delete */ strmov(query, "DELETE FROM test_prepare_simple WHERE id=?"); stmt= mysql_simple_prepare(mysql, query); check_stmt(stmt); verify_param_count(stmt, 1); mysql_stmt_close(stmt); /* select */ strmov(query, "SELECT * FROM test_prepare_simple WHERE id=? " "AND CONVERT(name USING utf8)= ?"); stmt= mysql_simple_prepare(mysql, query); check_stmt(stmt); verify_param_count(stmt, 2); mysql_stmt_close(stmt); /* now fetch the results ..*/ rc= mysql_commit(mysql); myquery(rc);}/* Test simple prepare field results */static void test_prepare_field_result(){ MYSQL_STMT *stmt; MYSQL_RES *result; int rc; myheader("test_prepare_field_result"); rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_field_result"); myquery(rc); rc= mysql_query(mysql, "CREATE TABLE test_prepare_field_result(int_c int, " "var_c varchar(50), ts_c timestamp(14), " "char_c char(4), date_c date, extra tinyint)"); myquery(rc); /* insert */ strmov(query, "SELECT int_c, var_c, date_c as date, ts_c, char_c FROM " " test_prepare_field_result as t1 WHERE int_c=?"); stmt= mysql_simple_prepare(mysql, query); check_stmt(stmt); verify_param_count(stmt, 1); result= mysql_stmt_result_metadata(stmt); mytest(result); my_print_result_metadata(result); if (!opt_silent) fprintf(stdout, "\n\n field attributes:\n"); verify_prepare_field(result, 0, "int_c", "int_c", MYSQL_TYPE_LONG, "t1", "test_prepare_field_result", current_db, 11, 0); verify_prepare_field(result, 1, "var_c", "var_c", MYSQL_TYPE_VAR_STRING, "t1", "test_prepare_field_result", current_db, 50, 0); verify_prepare_field(result, 2, "date", "date_c", MYSQL_TYPE_DATE, "t1", "test_prepare_field_result", current_db, 10, 0); verify_prepare_field(result, 3, "ts_c", "ts_c", MYSQL_TYPE_TIMESTAMP, "t1", "test_prepare_field_result", current_db, 19, 0); verify_prepare_field(result, 4, "char_c", "char_c", (mysql_get_server_version(mysql) <= 50000 ? MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING), "t1", "test_prepare_field_result", current_db, 4, 0); verify_field_count(result, 5); mysql_free_result(result); mysql_stmt_close(stmt);}/* Test simple prepare field results */static void test_prepare_syntax(){ MYSQL_STMT *stmt; int rc; myheader("test_prepare_syntax"); rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_syntax"); myquery(rc); rc= mysql_query(mysql, "CREATE TABLE test_prepare_syntax(" "id int, name varchar(50), extra int)"); myquery(rc); strmov(query, "INSERT INTO test_prepare_syntax VALUES(?"); stmt= mysql_simple_prepare(mysql, query); check_stmt_r(stmt); strmov(query, "SELECT id, name FROM test_prepare_syntax WHERE id=? AND WHERE"); stmt= mysql_simple_prepare(mysql, query); check_stmt_r(stmt); /* now fetch the results ..*/ rc= mysql_commit(mysql); myquery(rc);}/* Test a simple prepare */static void test_prepare(){ MYSQL_STMT *stmt; int rc, i; int int_data, o_int_data; char str_data[50], data[50]; char tiny_data, o_tiny_data; short small_data, o_small_data; longlong big_data, o_big_data; float real_data, o_real_data; double double_data, o_double_data; ulong length[7], len; my_bool is_null[7]; char llbuf[22]; MYSQL_BIND bind[7]; myheader("test_prepare");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -