/release/src-rt-6.x.4708/router/mysql/tests/mysql_client_test.c
C | 17759 lines | 12629 code | 3928 blank | 1202 comment | 1355 complexity | 939e031cc2061b1b314f9b0e8bf15d3f MD5 | raw file
Possible License(s): WTFPL, CC-BY-SA-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-2-Clause, GPL-2.0, BSD-3-Clause, 0BSD, LGPL-2.0, GPL-3.0, LGPL-3.0, MIT
Large files files are truncated, but you can click here to view the full file
- /* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; version 2 of the License.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
- /***************************************************************************
- This is a test sample to test the new features in MySQL client-server
- protocol
- Main author: venu ( venu@mysql.com )
- ***************************************************************************/
- /*
- XXX: PLEASE RUN THIS PROGRAM UNDER VALGRIND AND VERIFY THAT YOUR TEST
- DOESN'T CONTAIN WARNINGS/ERRORS BEFORE YOU PUSH.
- */
- /*
- The fw.c file includes all the mysql_client_test framework; this file
- contains only the actual tests, plus the list of test functions to call.
- */
- #include "mysql_client_fw.c"
- /* Query processing */
- static void client_query()
- {
- int rc;
- myheader("client_query");
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE t1("
- "id int primary key auto_increment, "
- "name varchar(20))");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE t1(id int, name varchar(20))");
- myquery_r(rc);
- rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('mysql')");
- myquery(rc);
- rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('monty')");
- myquery(rc);
- rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('venu')");
- myquery(rc);
- rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
- myquery(rc);
- rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
- myquery(rc);
- rc= mysql_query(mysql, "UPDATE t1 SET name= 'updated' "
- "WHERE name= 'deleted'");
- myquery(rc);
- rc= mysql_query(mysql, "UPDATE t1 SET id= 3 WHERE name= 'updated'");
- myquery_r(rc);
- myquery(mysql_query(mysql, "drop table t1"));
- }
- /* Store result processing */
- static void client_store_result()
- {
- MYSQL_RES *result;
- int rc;
- myheader("client_store_result");
- rc= mysql_query(mysql, "SELECT * FROM t1");
- myquery(rc);
- /* get the result */
- result= mysql_store_result(mysql);
- mytest(result);
- (void) my_process_result_set(result);
- mysql_free_result(result);
- }
- /* Fetch the results */
- static void client_use_result()
- {
- MYSQL_RES *result;
- int rc;
- myheader("client_use_result");
- rc= mysql_query(mysql, "SELECT * FROM t1");
- myquery(rc);
- /* get the result */
- result= mysql_use_result(mysql);
- mytest(result);
- (void) my_process_result_set(result);
- mysql_free_result(result);
- }
- /* 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)) ENGINE= 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)) ENGINE= 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++)
- {
- char query[MAX_TEST_QUERY_LENGTH];
- 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;
- char query[MAX_TEST_QUERY_LENGTH];
- 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;
- char query[MAX_TEST_QUERY_LENGTH];
- 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;
- char query[MAX_TEST_QUERY_LENGTH];
- 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 my_bind[7];
- char query[MAX_TEST_QUERY_LENGTH];
- myheader("test_prepare");
- rc= mysql_autocommit(mysql, TRUE);
- myquery(rc);
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 tinyint, "
- "col2 varchar(15), col3 int, "
- "col4 smallint, col5 bigint, "
- "col6 float, col7 double )");
- myquery(rc);
- /* insert by prepare */
- strxmov(query, "INSERT INTO my_prepare VALUES(?, ?, ?, ?, ?, ?, ?)", NullS);
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt(stmt);
- verify_param_count(stmt, 7);
- bzero((char*) my_bind, sizeof(my_bind));
- /* tinyint */
- my_bind[0].buffer_type= MYSQL_TYPE_TINY;
- my_bind[0].buffer= (void *)&tiny_data;
- /* string */
- my_bind[1].buffer_type= MYSQL_TYPE_STRING;
- my_bind[1].buffer= (void *)str_data;
- my_bind[1].buffer_length= 1000; /* Max string length */
- /* integer */
- my_bind[2].buffer_type= MYSQL_TYPE_LONG;
- my_bind[2].buffer= (void *)&int_data;
- /* short */
- my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
- my_bind[3].buffer= (void *)&small_data;
- /* bigint */
- my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
- my_bind[4].buffer= (void *)&big_data;
- /* float */
- my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
- my_bind[5].buffer= (void *)&real_data;
- /* double */
- my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
- my_bind[6].buffer= (void *)&double_data;
- for (i= 0; i < (int) array_elements(my_bind); i++)
- {
- my_bind[i].length= &length[i];
- my_bind[i].is_null= &is_null[i];
- is_null[i]= 0;
- }
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- int_data= 320;
- small_data= 1867;
- big_data= 1000;
- real_data= 2;
- double_data= 6578.001;
- /* now, execute the prepared statement to insert 10 records.. */
- for (tiny_data= 0; tiny_data < 100; tiny_data++)
- {
- length[1]= sprintf(str_data, "MySQL%d", int_data);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- int_data += 25;
- small_data += 10;
- big_data += 100;
- real_data += 1;
- double_data += 10.09;
- }
- mysql_stmt_close(stmt);
- /* now fetch the results ..*/
- rc= mysql_commit(mysql);
- myquery(rc);
- /* test the results now, only one row should exist */
- rc= my_stmt_result("SELECT * FROM my_prepare");
- DIE_UNLESS(tiny_data == (char) rc);
- stmt= mysql_simple_prepare(mysql, "SELECT * FROM my_prepare");
- check_stmt(stmt);
- rc= mysql_stmt_bind_result(stmt, my_bind);
- check_execute(stmt, rc);
- /* get the result */
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- o_int_data= 320;
- o_small_data= 1867;
- o_big_data= 1000;
- o_real_data= 2;
- o_double_data= 6578.001;
- /* now, execute the prepared statement to insert 10 records.. */
- for (o_tiny_data= 0; o_tiny_data < 100; o_tiny_data++)
- {
- len= sprintf(data, "MySQL%d", o_int_data);
- rc= mysql_stmt_fetch(stmt);
- check_execute(stmt, rc);
- if (!opt_silent)
- {
- fprintf(stdout, "\n");
- fprintf(stdout, "\n\t tiny : %d (%lu)", tiny_data, length[0]);
- fprintf(stdout, "\n\t short : %d (%lu)", small_data, length[3]);
- fprintf(stdout, "\n\t int : %d (%lu)", int_data, length[2]);
- fprintf(stdout, "\n\t big : %s (%lu)", llstr(big_data, llbuf),
- length[4]);
- fprintf(stdout, "\n\t float : %f (%lu)", real_data, length[5]);
- fprintf(stdout, "\n\t double : %f (%lu)", double_data, length[6]);
- fprintf(stdout, "\n\t str : %s (%lu)", str_data, length[1]);
- }
- DIE_UNLESS(tiny_data == o_tiny_data);
- DIE_UNLESS(is_null[0] == 0);
- DIE_UNLESS(length[0] == 1);
- DIE_UNLESS(int_data == o_int_data);
- DIE_UNLESS(length[2] == 4);
- DIE_UNLESS(small_data == o_small_data);
- DIE_UNLESS(length[3] == 2);
- DIE_UNLESS(big_data == o_big_data);
- DIE_UNLESS(length[4] == 8);
- DIE_UNLESS(real_data == o_real_data);
- DIE_UNLESS(length[5] == 4);
- DIE_UNLESS(cmp_double(&double_data, &o_double_data));
- DIE_UNLESS(length[6] == 8);
- DIE_UNLESS(strcmp(data, str_data) == 0);
- DIE_UNLESS(length[1] == len);
- o_int_data += 25;
- o_small_data += 10;
- o_big_data += 100;
- o_real_data += 1;
- o_double_data += 10.09;
- }
- rc= mysql_stmt_fetch(stmt);
- DIE_UNLESS(rc == MYSQL_NO_DATA);
- mysql_stmt_close(stmt);
- }
- /* Test double comparision */
- static void test_double_compare()
- {
- MYSQL_STMT *stmt;
- int rc;
- char real_data[10], tiny_data;
- double double_data;
- MYSQL_RES *result;
- MYSQL_BIND my_bind[3];
- ulong length[3];
- char query[MAX_TEST_QUERY_LENGTH];
- myheader("test_double_compare");
- rc= mysql_autocommit(mysql, TRUE);
- myquery(rc);
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_double_compare");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE test_double_compare(col1 tinyint, "
- " col2 float, col3 double )");
- myquery(rc);
- rc= mysql_query(mysql, "INSERT INTO test_double_compare "
- "VALUES (1, 10.2, 34.5)");
- myquery(rc);
- strmov(query, "UPDATE test_double_compare SET col1=100 "
- "WHERE col1 = ? AND col2 = ? AND COL3 = ?");
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt(stmt);
- verify_param_count(stmt, 3);
- /* Always bzero bind array because there can be internal members */
- bzero((char*) my_bind, sizeof(my_bind));
- /* tinyint */
- my_bind[0].buffer_type= MYSQL_TYPE_TINY;
- my_bind[0].buffer= (void *)&tiny_data;
- /* string->float */
- my_bind[1].buffer_type= MYSQL_TYPE_STRING;
- my_bind[1].buffer= (void *)&real_data;
- my_bind[1].buffer_length= sizeof(real_data);
- my_bind[1].length= &length[1];
- length[1]= 10;
- /* double */
- my_bind[2].buffer_type= MYSQL_TYPE_DOUBLE;
- my_bind[2].buffer= (void *)&double_data;
- tiny_data= 1;
- strmov(real_data, "10.2");
- double_data= 34.5;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- verify_affected_rows(0);
- mysql_stmt_close(stmt);
- /* now fetch the results ..*/
- rc= mysql_commit(mysql);
- myquery(rc);
- /* test the results now, only one row should exist */
- rc= mysql_query(mysql, "SELECT * FROM test_double_compare");
- myquery(rc);
- /* get the result */
- result= mysql_store_result(mysql);
- mytest(result);
- rc= my_process_result_set(result);
- DIE_UNLESS((int)tiny_data == rc);
- mysql_free_result(result);
- }
- /* Test simple null */
- static void test_null()
- {
- MYSQL_STMT *stmt;
- int rc;
- uint nData;
- MYSQL_BIND my_bind[2];
- my_bool is_null[2];
- char query[MAX_TEST_QUERY_LENGTH];
- myheader("test_null");
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_null");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE test_null(col1 int, col2 varchar(50))");
- myquery(rc);
- /* insert by prepare, wrong column name */
- strmov(query, "INSERT INTO test_null(col3, col2) VALUES(?, ?)");
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt_r(stmt);
- strmov(query, "INSERT INTO test_null(col1, col2) VALUES(?, ?)");
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt(stmt);
- verify_param_count(stmt, 2);
- /* Always bzero all members of bind parameter */
- bzero((char*) my_bind, sizeof(my_bind));
- my_bind[0].buffer_type= MYSQL_TYPE_LONG;
- my_bind[0].is_null= &is_null[0];
- is_null[0]= 1;
- my_bind[1]= my_bind[0];
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- /* now, execute the prepared statement to insert 10 records.. */
- for (nData= 0; nData<10; nData++)
- {
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- }
- /* Re-bind with MYSQL_TYPE_NULL */
- my_bind[0].buffer_type= MYSQL_TYPE_NULL;
- is_null[0]= 0; /* reset */
- my_bind[1]= my_bind[0];
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- for (nData= 0; nData<10; nData++)
- {
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- }
- mysql_stmt_close(stmt);
- /* now fetch the results ..*/
- rc= mysql_commit(mysql);
- myquery(rc);
- nData*= 2;
- rc= my_stmt_result("SELECT * FROM test_null");;
- DIE_UNLESS((int) nData == rc);
- /* Fetch results */
- my_bind[0].buffer_type= MYSQL_TYPE_LONG;
- my_bind[0].buffer= (void *)&nData; /* this buffer won't be altered */
- my_bind[0].length= 0;
- my_bind[1]= my_bind[0];
- my_bind[0].is_null= &is_null[0];
- my_bind[1].is_null= &is_null[1];
- stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_null");
- check_stmt(stmt);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= mysql_stmt_bind_result(stmt, my_bind);
- check_execute(stmt, rc);
- rc= 0;
- is_null[0]= is_null[1]= 0;
- while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
- {
- DIE_UNLESS(is_null[0]);
- DIE_UNLESS(is_null[1]);
- rc++;
- is_null[0]= is_null[1]= 0;
- }
- DIE_UNLESS(rc == (int) nData);
- mysql_stmt_close(stmt);
- }
- /* Test for NULL as PS parameter (BUG#3367, BUG#3371) */
- static void test_ps_null_param()
- {
- MYSQL_STMT *stmt;
- int rc;
- MYSQL_BIND in_bind;
- my_bool in_is_null;
- long int in_long;
- MYSQL_BIND out_bind;
- ulong out_length;
- my_bool out_is_null;
- char out_str_data[20];
- const char *queries[]= {"select ?", "select ?+1",
- "select col1 from test_ps_nulls where col1 <=> ?",
- NULL
- };
- const char **cur_query= queries;
- myheader("test_null_ps_param_in_result");
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ps_nulls");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE test_ps_nulls(col1 int)");
- myquery(rc);
- rc= mysql_query(mysql, "INSERT INTO test_ps_nulls values (1), (null)");
- myquery(rc);
- /* Always bzero all members of bind parameter */
- bzero((char*) &in_bind, sizeof(in_bind));
- bzero((char*) &out_bind, sizeof(out_bind));
- in_bind.buffer_type= MYSQL_TYPE_LONG;
- in_bind.is_null= &in_is_null;
- in_bind.length= 0;
- in_bind.buffer= (void *)&in_long;
- in_is_null= 1;
- in_long= 1;
- out_bind.buffer_type= MYSQL_TYPE_STRING;
- out_bind.is_null= &out_is_null;
- out_bind.length= &out_length;
- out_bind.buffer= out_str_data;
- out_bind.buffer_length= array_elements(out_str_data);
- /* Execute several queries, all returning NULL in result. */
- for(cur_query= queries; *cur_query; cur_query++)
- {
- char query[MAX_TEST_QUERY_LENGTH];
- strmov(query, *cur_query);
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt(stmt);
- verify_param_count(stmt, 1);
- rc= mysql_stmt_bind_param(stmt, &in_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_bind_result(stmt, &out_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= mysql_stmt_fetch(stmt);
- DIE_UNLESS(rc != MYSQL_NO_DATA);
- DIE_UNLESS(out_is_null);
- rc= mysql_stmt_fetch(stmt);
- DIE_UNLESS(rc == MYSQL_NO_DATA);
- mysql_stmt_close(stmt);
- }
- }
- /* Test fetch null */
- static void test_fetch_null()
- {
- MYSQL_STMT *stmt;
- int rc;
- int i, nData;
- MYSQL_BIND my_bind[11];
- ulong length[11];
- my_bool is_null[11];
- char query[MAX_TEST_QUERY_LENGTH];
- myheader("test_fetch_null");
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_fetch_null");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE test_fetch_null("
- " col1 tinyint, col2 smallint, "
- " col3 int, col4 bigint, "
- " col5 float, col6 double, "
- " col7 date, col8 time, "
- " col9 varbinary(10), "
- " col10 varchar(50), "
- " col11 char(20))");
- myquery(rc);
- rc= mysql_query(mysql, "INSERT INTO test_fetch_null (col11) "
- "VALUES (1000), (88), (389789)");
- myquery(rc);
- rc= mysql_commit(mysql);
- myquery(rc);
- /* fetch */
- bzero((char*) my_bind, sizeof(my_bind));
- for (i= 0; i < (int) array_elements(my_bind); i++)
- {
- my_bind[i].buffer_type= MYSQL_TYPE_LONG;
- my_bind[i].is_null= &is_null[i];
- my_bind[i].length= &length[i];
- }
- my_bind[i-1].buffer= (void *)&nData; /* Last column is not null */
- strmov((char *)query , "SELECT * FROM test_fetch_null");
- rc= my_stmt_result(query);
- DIE_UNLESS(rc == 3);
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt(stmt);
- rc= mysql_stmt_bind_result(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= 0;
- while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
- {
- rc++;
- for (i= 0; i < 10; i++)
- {
- if (!opt_silent)
- fprintf(stdout, "\n data[%d] : %s", i,
- is_null[i] ? "NULL" : "NOT NULL");
- DIE_UNLESS(is_null[i]);
- }
- if (!opt_silent)
- fprintf(stdout, "\n data[%d]: %d", i, nData);
- DIE_UNLESS(nData == 1000 || nData == 88 || nData == 389789);
- DIE_UNLESS(is_null[i] == 0);
- DIE_UNLESS(length[i] == 4);
- }
- DIE_UNLESS(rc == 3);
- mysql_stmt_close(stmt);
- }
- /* Test simple select */
- static void test_select_version()
- {
- MYSQL_STMT *stmt;
- int rc;
- myheader("test_select_version");
- stmt= mysql_simple_prepare(mysql, "SELECT @@version");
- check_stmt(stmt);
- verify_param_count(stmt, 0);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- my_process_stmt_result(stmt);
- mysql_stmt_close(stmt);
- }
- /* Test simple show */
- static void test_select_show_table()
- {
- MYSQL_STMT *stmt;
- int rc, i;
- myheader("test_select_show_table");
- stmt= mysql_simple_prepare(mysql, "SHOW TABLES FROM mysql");
- check_stmt(stmt);
- verify_param_count(stmt, 0);
- for (i= 1; i < 3; i++)
- {
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- }
- my_process_stmt_result(stmt);
- mysql_stmt_close(stmt);
- }
- /* Test simple select to debug */
- static void test_select_direct()
- {
- int rc;
- MYSQL_RES *result;
- myheader("test_select_direct");
- rc= mysql_autocommit(mysql, TRUE);
- myquery(rc);
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE test_select(id int, id1 tinyint, "
- " id2 float, "
- " id3 double, "
- " name varchar(50))");
- myquery(rc);
- /* insert a row and commit the transaction */
- rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 5, 2.3, 4.5, 'venu')");
- myquery(rc);
- rc= mysql_commit(mysql);
- myquery(rc);
- rc= mysql_query(mysql, "SELECT * FROM test_select");
- myquery(rc);
- /* get the result */
- result= mysql_store_result(mysql);
- mytest(result);
- (void) my_process_result_set(result);
- mysql_free_result(result);
- }
- /* Test simple select with prepare */
- static void test_select_prepare()
- {
- int rc;
- MYSQL_STMT *stmt;
- myheader("test_select_prepare");
- rc= mysql_autocommit(mysql, TRUE);
- myquery(rc);
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
- myquery(rc);
- /* insert a row and commit the transaction */
- rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
- myquery(rc);
- rc= mysql_commit(mysql);
- myquery(rc);
- stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
- check_stmt(stmt);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= my_process_stmt_result(stmt);
- DIE_UNLESS(rc == 1);
- mysql_stmt_close(stmt);
- rc= mysql_query(mysql, "DROP TABLE test_select");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE test_select(id tinyint, id1 int, "
- " id2 float, id3 float, "
- " name varchar(50))");
- myquery(rc);
- /* insert a row and commit the transaction */
- rc= mysql_query(mysql, "INSERT INTO test_select(id, id1, id2, name) VALUES(10, 5, 2.3, 'venu')");
- myquery(rc);
- rc= mysql_commit(mysql);
- myquery(rc);
- stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
- check_stmt(stmt);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= my_process_stmt_result(stmt);
- DIE_UNLESS(rc == 1);
- mysql_stmt_close(stmt);
- }
- /* Test simple select */
- static void test_select()
- {
- MYSQL_STMT *stmt;
- int rc;
- char szData[25];
- int nData= 1;
- MYSQL_BIND my_bind[2];
- ulong length[2];
- char query[MAX_TEST_QUERY_LENGTH];
- myheader("test_select");
- rc= mysql_autocommit(mysql, TRUE);
- myquery(rc);
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
- myquery(rc);
- /* insert a row and commit the transaction */
- rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
- myquery(rc);
- /* now insert the second row, and roll back the transaction */
- rc= mysql_query(mysql, "INSERT INTO test_select VALUES(20, 'mysql')");
- myquery(rc);
- rc= mysql_commit(mysql);
- myquery(rc);
- strmov(query, "SELECT * FROM test_select WHERE id= ? "
- "AND CONVERT(name USING utf8) =?");
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt(stmt);
- verify_param_count(stmt, 2);
- /* Always bzero all members of bind parameter */
- bzero((char*) my_bind, sizeof(my_bind));
- /* string data */
- nData= 10;
- strmov(szData, (char *)"venu");
- my_bind[1].buffer_type= MYSQL_TYPE_STRING;
- my_bind[1].buffer= (void *)szData;
- my_bind[1].buffer_length= 4;
- my_bind[1].length= &length[1];
- length[1]= 4;
- my_bind[0].buffer= (void *)&nData;
- my_bind[0].buffer_type= MYSQL_TYPE_LONG;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= my_process_stmt_result(stmt);
- DIE_UNLESS(rc == 1);
- mysql_stmt_close(stmt);
- }
- /*
- Test for BUG#3420 ("select id1, value1 from t where id= ? or value= ?"
- returns all rows in the table)
- */
- static void test_ps_conj_select()
- {
- MYSQL_STMT *stmt;
- int rc;
- MYSQL_BIND my_bind[2];
- int32 int_data;
- char str_data[32];
- unsigned long str_length;
- char query[MAX_TEST_QUERY_LENGTH];
- myheader("test_ps_conj_select");
- rc= mysql_query(mysql, "drop table if exists t1");
- myquery(rc);
- rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
- "value2 varchar(100), value1 varchar(100))");
- myquery(rc);
- rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
- "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
- myquery(rc);
- strmov(query, "select id1, value1 from t1 where id1= ? or "
- "CONVERT(value1 USING utf8)= ?");
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt(stmt);
- verify_param_count(stmt, 2);
- /* Always bzero all members of bind parameter */
- bzero((char*) my_bind, sizeof(my_bind));
- my_bind[0].buffer_type= MYSQL_TYPE_LONG;
- my_bind[0].buffer= (void *)&int_data;
- my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
- my_bind[1].buffer= (void *)str_data;
- my_bind[1].buffer_length= array_elements(str_data);
- my_bind[1].length= &str_length;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- int_data= 1;
- strmov(str_data, "hh");
- str_length= strlen(str_data);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= my_process_stmt_result(stmt);
- DIE_UNLESS(rc == 3);
- mysql_stmt_close(stmt);
- }
- /* reads Qcache_hits from server and returns its value */
- static uint query_cache_hits(MYSQL *conn)
- {
- MYSQL_RES *res;
- MYSQL_ROW row;
- int rc;
- uint result;
- rc= mysql_query(conn, "show status like 'qcache_hits'");
- myquery(rc);
- res= mysql_use_result(conn);
- DIE_UNLESS(res);
- row= mysql_fetch_row(res);
- DIE_UNLESS(row);
- result= atoi(row[1]);
- mysql_free_result(res);
- return result;
- }
- /*
- utility for the next test; expects 3 rows in the result from a SELECT,
- compares each row/field with an expected value.
- */
- #define test_ps_query_cache_result(i1,s1,l1,i2,s2,l2,i3,s3,l3) \
- r_metadata= mysql_stmt_result_metadata(stmt); \
- DIE_UNLESS(r_metadata != NULL); \
- rc= mysql_stmt_fetch(stmt); \
- check_execute(stmt, rc); \
- if (!opt_silent) \
- fprintf(stdout, "\n row 1: %d, %s(%lu)", r_int_data, \
- r_str_data, r_str_length); \
- DIE_UNLESS((r_int_data == i1) && (r_str_length == l1) && \
- (strcmp(r_str_data, s1) == 0)); \
- rc= mysql_stmt_fetch(stmt); \
- check_execute(stmt, rc); \
- if (!opt_silent) \
- fprintf(stdout, "\n row 2: %d, %s(%lu)", r_int_data, \
- r_str_data, r_str_length); \
- DIE_UNLESS((r_int_data == i2) && (r_str_length == l2) && \
- (strcmp(r_str_data, s2) == 0)); \
- rc= mysql_stmt_fetch(stmt); \
- check_execute(stmt, rc); \
- if (!opt_silent) \
- fprintf(stdout, "\n row 3: %d, %s(%lu)", r_int_data, \
- r_str_data, r_str_length); \
- DIE_UNLESS((r_int_data == i3) && (r_str_length == l3) && \
- (strcmp(r_str_data, s3) == 0)); \
- rc= mysql_stmt_fetch(stmt); \
- DIE_UNLESS(rc == MYSQL_NO_DATA); \
- mysql_free_result(r_metadata);
- /*
- Test that prepared statements make use of the query cache just as normal
- statements (BUG#735).
- */
- static void test_ps_query_cache()
- {
- MYSQL *lmysql= mysql;
- MYSQL_STMT *stmt;
- int rc;
- MYSQL_BIND p_bind[2],r_bind[2]; /* p: param bind; r: result bind */
- int32 p_int_data, r_int_data;
- char p_str_data[32], r_str_data[32];
- unsigned long p_str_length, r_str_length;
- MYSQL_RES *r_metadata;
- char query[MAX_TEST_QUERY_LENGTH];
- uint hits1, hits2;
- enum enum_test_ps_query_cache
- {
- /*
- We iterate the same prepare/executes block, but have iterations where
- we vary the query cache conditions.
- */
- /* the query cache is enabled for the duration of prep&execs: */
- TEST_QCACHE_ON= 0,
- /*
- same but using a new connection (to see if qcache serves results from
- the previous connection as it should):
- */
- TEST_QCACHE_ON_WITH_OTHER_CONN,
- /*
- First border case: disables the query cache before prepare and
- re-enables it before execution (to test if we have no bug then):
- */
- TEST_QCACHE_OFF_ON,
- /*
- Second border case: enables the query cache before prepare and
- disables it before execution:
- */
- TEST_QCACHE_ON_OFF
- };
- enum enum_test_ps_query_cache iteration;
- myheader("test_ps_query_cache");
- rc= mysql_query(mysql, "SET SQL_MODE=''");
- myquery(rc);
- /* prepare the table */
- rc= mysql_query(mysql, "drop table if exists t1");
- myquery(rc);
- rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
- "value2 varchar(100), value1 varchar(100))");
- myquery(rc);
- rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
- "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
- myquery(rc);
- for (iteration= TEST_QCACHE_ON; iteration <= TEST_QCACHE_ON_OFF; iteration++)
- {
- switch (iteration) {
- case TEST_QCACHE_ON:
- case TEST_QCACHE_ON_OFF:
- rc= mysql_query(lmysql, "set global query_cache_size=1000000");
- myquery(rc);
- break;
- case TEST_QCACHE_OFF_ON:
- rc= mysql_query(lmysql, "set global query_cache_size=0");
- myquery(rc);
- break;
- case TEST_QCACHE_ON_WITH_OTHER_CONN:
- if (!opt_silent)
- fprintf(stdout, "\n Establishing a test connection ...");
- if (!(lmysql= mysql_client_init(NULL)))
- {
- printf("mysql_client_init() failed");
- DIE_UNLESS(0);
- }
- if (!(mysql_real_connect(lmysql, opt_host, opt_user,
- opt_password, current_db, opt_port,
- opt_unix_socket, 0)))
- {
- printf("connection failed");
- mysql_close(lmysql);
- DIE_UNLESS(0);
- }
- rc= mysql_query(lmysql, "SET SQL_MODE=''");
- myquery(rc);
- if (!opt_silent)
- fprintf(stdout, "OK");
- }
- strmov(query, "select id1, value1 from t1 where id1= ? or "
- "CONVERT(value1 USING utf8)= ?");
- stmt= mysql_simple_prepare(lmysql, query);
- check_stmt(stmt);
- verify_param_count(stmt, 2);
- switch (iteration) {
- case TEST_QCACHE_OFF_ON:
- rc= mysql_query(lmysql, "set global query_cache_size=1000000");
- myquery(rc);
- break;
- case TEST_QCACHE_ON_OFF:
- rc= mysql_query(lmysql, "set global query_cache_size=0");
- myquery(rc);
- default:
- break;
- }
- bzero((char*) p_bind, sizeof(p_bind));
- p_bind[0].buffer_type= MYSQL_TYPE_LONG;
- p_bind[0].buffer= (void *)&p_int_data;
- p_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
- p_bind[1].buffer= (void *)p_str_data;
- p_bind[1].buffer_length= array_elements(p_str_data);
- p_bind[1].length= &p_str_length;
- rc= mysql_stmt_bind_param(stmt, p_bind);
- check_execute(stmt, rc);
- p_int_data= 1;
- strmov(p_str_data, "hh");
- p_str_length= strlen(p_str_data);
- bzero((char*) r_bind, sizeof(r_bind));
- r_bind[0].buffer_type= MYSQL_TYPE_LONG;
- r_bind[0].buffer= (void *)&r_int_data;
- r_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
- r_bind[1].buffer= (void *)r_str_data;
- r_bind[1].buffer_length= array_elements(r_str_data);
- r_bind[1].length= &r_str_length;
- rc= mysql_stmt_bind_result(stmt, r_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
- /* now retry with the same parameter values and see qcache hits */
- hits1= query_cache_hits(lmysql);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
- hits2= query_cache_hits(lmysql);
- switch(iteration) {
- case TEST_QCACHE_ON_WITH_OTHER_CONN:
- case TEST_QCACHE_ON: /* should have hit */
- DIE_UNLESS(hits2-hits1 == 1);
- break;
- case TEST_QCACHE_OFF_ON:
- case TEST_QCACHE_ON_OFF: /* should not have hit */
- DIE_UNLESS(hits2-hits1 == 0);
- break;
- }
- /* now modify parameter values and see qcache hits */
- strmov(p_str_data, "ii");
- p_str_length= strlen(p_str_data);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
- hits1= query_cache_hits(lmysql);
- switch(iteration) {
- case TEST_QCACHE_ON:
- case TEST_QCACHE_OFF_ON:
- case TEST_QCACHE_ON_OFF: /* should not have hit */
- DIE_UNLESS(hits2-hits1 == 0);
- break;
- case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
- DIE_UNLESS(hits1-hits2 == 1);
- break;
- }
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
- hits2= query_cache_hits(lmysql);
- mysql_stmt_close(stmt);
- switch(iteration) {
- case TEST_QCACHE_ON: /* should have hit */
- DIE_UNLESS(hits2-hits1 == 1);
- break;
- case TEST_QCACHE_OFF_ON:
- case TEST_QCACHE_ON_OFF: /* should not have hit */
- DIE_UNLESS(hits2-hits1 == 0);
- break;
- case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
- DIE_UNLESS(hits2-hits1 == 1);
- break;
- }
- } /* for(iteration=...) */
- if (lmysql != mysql)
- mysql_close(lmysql);
- rc= mysql_query(mysql, "set global query_cache_size=0");
- myquery(rc);
- }
- /* Test BUG#1115 (incorrect string parameter value allocation) */
- static void test_bug1115()
- {
- MYSQL_STMT *stmt;
- int rc;
- MYSQL_BIND my_bind[1];
- ulong length[1];
- char szData[11];
- char query[MAX_TEST_QUERY_LENGTH];
- myheader("test_bug1115");
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE test_select(\
- session_id char(9) NOT NULL, \
- a int(8) unsigned NOT NULL, \
- b int(5) NOT NULL, \
- c int(5) NOT NULL, \
- d datetime NOT NULL)");
- myquery(rc);
- rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
- "(\"abc\", 1, 2, 3, 2003-08-30), "
- "(\"abd\", 1, 2, 3, 2003-08-30), "
- "(\"abf\", 1, 2, 3, 2003-08-30), "
- "(\"abg\", 1, 2, 3, 2003-08-30), "
- "(\"abh\", 1, 2, 3, 2003-08-30), "
- "(\"abj\", 1, 2, 3, 2003-08-30), "
- "(\"abk\", 1, 2, 3, 2003-08-30), "
- "(\"abl\", 1, 2, 3, 2003-08-30), "
- "(\"abq\", 1, 2, 3, 2003-08-30) ");
- myquery(rc);
- rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
- "(\"abw\", 1, 2, 3, 2003-08-30), "
- "(\"abe\", 1, 2, 3, 2003-08-30), "
- "(\"abr\", 1, 2, 3, 2003-08-30), "
- "(\"abt\", 1, 2, 3, 2003-08-30), "
- "(\"aby\", 1, 2, 3, 2003-08-30), "
- "(\"abu\", 1, 2, 3, 2003-08-30), "
- "(\"abi\", 1, 2, 3, 2003-08-30), "
- "(\"abo\", 1, 2, 3, 2003-08-30), "
- "(\"abp\", 1, 2, 3, 2003-08-30), "
- "(\"abz\", 1, 2, 3, 2003-08-30), "
- "(\"abx\", 1, 2, 3, 2003-08-30)");
- myquery(rc);
- strmov(query, "SELECT * FROM test_select WHERE "
- "CONVERT(session_id USING utf8)= ?");
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt(stmt);
- verify_param_count(stmt, 1);
- /* Always bzero all members of bind parameter */
- bzero((char*) my_bind, sizeof(my_bind));
- strmov(szData, (char *)"abc");
- my_bind[0].buffer_type= MYSQL_TYPE_STRING;
- my_bind[0].buffer= (void *)szData;
- my_bind[0].buffer_length= 10;
- my_bind[0].length= &length[0];
- length[0]= 3;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= my_process_stmt_result(stmt);
- DIE_UNLESS(rc == 1);
- strmov(szData, (char *)"venu");
- my_bind[0].buffer_type= MYSQL_TYPE_STRING;
- my_bind[0].buffer= (void *)szData;
- my_bind[0].buffer_length= 10;
- my_bind[0].length= &length[0];
- length[0]= 4;
- my_bind[0].is_null= 0;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= my_process_stmt_result(stmt);
- DIE_UNLESS(rc == 0);
- strmov(szData, (char *)"abc");
- my_bind[0].buffer_type= MYSQL_TYPE_STRING;
- my_bind[0].buffer= (void *)szData;
- my_bind[0].buffer_length= 10;
- my_bind[0].length= &length[0];
- length[0]= 3;
- my_bind[0].is_null= 0;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= my_process_stmt_result(stmt);
- DIE_UNLESS(rc == 1);
- mysql_stmt_close(stmt);
- }
- /* Test BUG#1180 (optimized away part of WHERE clause) */
- static void test_bug1180()
- {
- MYSQL_STMT *stmt;
- int rc;
- MYSQL_BIND my_bind[1];
- ulong length[1];
- char szData[11];
- char query[MAX_TEST_QUERY_LENGTH];
- myheader("test_select_bug");
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
- myquery(rc);
- rc= mysql_query(mysql, "CREATE TABLE test_select(session_id char(9) NOT NULL)");
- myquery(rc);
- rc= mysql_query(mysql, "INSERT INTO test_select VALUES (\"abc\")");
- myquery(rc);
- strmov(query, "SELECT * FROM test_select WHERE ?= \"1111\" and "
- "session_id= \"abc\"");
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt(stmt);
- verify_param_count(stmt, 1);
- /* Always bzero all members of bind parameter */
- bzero((char*) my_bind, sizeof(my_bind));
- strmov(szData, (char *)"abc");
- my_bind[0].buffer_type= MYSQL_TYPE_STRING;
- my_bind[0].buffer= (void *)szData;
- my_bind[0].buffer_length= 10;
- my_bind[0].length= &length[0];
- length[0]= 3;
- my_bind[0].is_null= 0;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= my_process_stmt_result(stmt);
- DIE_UNLESS(rc == 0);
- strmov(szData, (char *)"1111");
- my_bind[0].buffer_type= MYSQL_TYPE_STRING;
- my_bind[0].buffer= (void *)szData;
- my_bind[0].buffer_length= 10;
- my_bind[0].length= &length[0];
- length[0]= 4;
- my_bind[0].is_null= 0;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= my_process_stmt_result(stmt);
- DIE_UNLESS(rc == 1);
- strmov(szData, (char *)"abc");
- my_bind[0].buffer_type= MYSQL_TYPE_STRING;
- my_bind[0].buffer= (void *)szData;
- my_bind[0].buffer_length= 10;
- my_bind[0].length= &length[0];
- length[0]= 3;
- my_bind[0].is_null= 0;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- rc= my_process_stmt_result(stmt);
- DIE_UNLESS(rc == 0);
- mysql_stmt_close(stmt);
- }
- /*
- Test BUG#1644 (Insertion of more than 3 NULL columns with parameter
- binding fails)
- */
- static void test_bug1644()
- {
- MYSQL_STMT *stmt;
- MYSQL_RES *result;
- MYSQL_ROW row;
- MYSQL_BIND my_bind[4];
- int num;
- my_bool isnull;
- int rc, i;
- char query[MAX_TEST_QUERY_LENGTH];
- myheader("test_bug1644");
- rc= mysql_query(mysql, "DROP TABLE IF EXISTS foo_dfr");
- myquery(rc);
- rc= mysql_query(mysql,
- "CREATE TABLE foo_dfr(col1 int, col2 int, col3 int, col4 int);");
- myquery(rc);
- strmov(query, "INSERT INTO foo_dfr VALUES (?, ?, ?, ? )");
- stmt= mysql_simple_prepare(mysql, query);
- check_stmt(stmt);
- verify_param_count(stmt, 4);
- /* Always bzero all members of bind parameter */
- bzero((char*) my_bind, sizeof(my_bind));
- num= 22;
- isnull= 0;
- for (i= 0 ; i < 4 ; i++)
- {
- my_bind[i].buffer_type= MYSQL_TYPE_LONG;
- my_bind[i].buffer= (void *)#
- my_bind[i].is_null= &isnull;
- }
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- isnull= 1;
- for (i= 0 ; i < 4 ; i++)
- my_bind[i].is_null= &isnull;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- isnull= 0;
- num= 88;
- for (i= 0 ; i < 4 ; i++)
- my_bind[i].is_null= &isnull;
- rc= mysql_stmt_bind_param(stmt, my_bind);
- check_execute(stmt, rc);
- rc= mysql_stmt_execute(stmt);
- check_execute(stmt, rc);
- mysql_stmt_close(stmt);
- rc= mysql_query(mysql, "SELECT * FROM foo_dfr");
- myquery(rc);
- result= mysql_store_result(mysql);
- mytest(result);
- rc= my_process_result_set(result);
- DIE_UNLESS(rc == 3);
- mysql_data_seek(result, 0);
- row= mysql_fetch_row(result);
- mytest(row);
- for (i= 0 ; i < 4 ; i++)
- {
- DIE_UNLESS(strcmp(row[i], "22") == 0);
- }
- row= mysql_fetch_row(result);
- mytest(row);
- for (i= 0 ; i < 4 ; i++)
- {
- DIE_UNLESS(row[i] == 0);
- }
- row= mysql_fetch_row(result);
- mytest(row);
- for (i= 0 ; i < 4 ; i++)
- {
- DIE_UNLESS(strcmp(row[i], "88") == 0);
- }
- row= mysql_fetch_row(result);
- mytest_r(row);
- mysql_free_result(result);
- }
- /* Test simple select show */
- static void test_select_show()
- {
- MYSQL_STMT *st…
Large files files are truncated, but you can click here to view the full file