PageRenderTime 82ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 2ms

/release/src-rt-6.x.4708/router/mysql/tests/mysql_client_test.c

https://bitbucket.org/edrikk/freshtomato-arm-at-gui
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

  1. /* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
  12. /***************************************************************************
  13. This is a test sample to test the new features in MySQL client-server
  14. protocol
  15. Main author: venu ( venu@mysql.com )
  16. ***************************************************************************/
  17. /*
  18. XXX: PLEASE RUN THIS PROGRAM UNDER VALGRIND AND VERIFY THAT YOUR TEST
  19. DOESN'T CONTAIN WARNINGS/ERRORS BEFORE YOU PUSH.
  20. */
  21. /*
  22. The fw.c file includes all the mysql_client_test framework; this file
  23. contains only the actual tests, plus the list of test functions to call.
  24. */
  25. #include "mysql_client_fw.c"
  26. /* Query processing */
  27. static void client_query()
  28. {
  29. int rc;
  30. myheader("client_query");
  31. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  32. myquery(rc);
  33. rc= mysql_query(mysql, "CREATE TABLE t1("
  34. "id int primary key auto_increment, "
  35. "name varchar(20))");
  36. myquery(rc);
  37. rc= mysql_query(mysql, "CREATE TABLE t1(id int, name varchar(20))");
  38. myquery_r(rc);
  39. rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('mysql')");
  40. myquery(rc);
  41. rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('monty')");
  42. myquery(rc);
  43. rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('venu')");
  44. myquery(rc);
  45. rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
  46. myquery(rc);
  47. rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
  48. myquery(rc);
  49. rc= mysql_query(mysql, "UPDATE t1 SET name= 'updated' "
  50. "WHERE name= 'deleted'");
  51. myquery(rc);
  52. rc= mysql_query(mysql, "UPDATE t1 SET id= 3 WHERE name= 'updated'");
  53. myquery_r(rc);
  54. myquery(mysql_query(mysql, "drop table t1"));
  55. }
  56. /* Store result processing */
  57. static void client_store_result()
  58. {
  59. MYSQL_RES *result;
  60. int rc;
  61. myheader("client_store_result");
  62. rc= mysql_query(mysql, "SELECT * FROM t1");
  63. myquery(rc);
  64. /* get the result */
  65. result= mysql_store_result(mysql);
  66. mytest(result);
  67. (void) my_process_result_set(result);
  68. mysql_free_result(result);
  69. }
  70. /* Fetch the results */
  71. static void client_use_result()
  72. {
  73. MYSQL_RES *result;
  74. int rc;
  75. myheader("client_use_result");
  76. rc= mysql_query(mysql, "SELECT * FROM t1");
  77. myquery(rc);
  78. /* get the result */
  79. result= mysql_use_result(mysql);
  80. mytest(result);
  81. (void) my_process_result_set(result);
  82. mysql_free_result(result);
  83. }
  84. /* Query processing */
  85. static void test_debug_example()
  86. {
  87. int rc;
  88. MYSQL_RES *result;
  89. myheader("test_debug_example");
  90. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_debug_example");
  91. myquery(rc);
  92. rc= mysql_query(mysql, "CREATE TABLE test_debug_example("
  93. "id INT PRIMARY KEY AUTO_INCREMENT, "
  94. "name VARCHAR(20), xxx INT)");
  95. myquery(rc);
  96. rc= mysql_query(mysql, "INSERT INTO test_debug_example (name) "
  97. "VALUES ('mysql')");
  98. myquery(rc);
  99. rc= mysql_query(mysql, "UPDATE test_debug_example SET name='updated' "
  100. "WHERE name='deleted'");
  101. myquery(rc);
  102. rc= mysql_query(mysql, "SELECT * FROM test_debug_example where name='mysql'");
  103. myquery(rc);
  104. result= mysql_use_result(mysql);
  105. mytest(result);
  106. (void) my_process_result_set(result);
  107. mysql_free_result(result);
  108. rc= mysql_query(mysql, "DROP TABLE test_debug_example");
  109. myquery(rc);
  110. }
  111. /* Test autocommit feature for BDB tables */
  112. static void test_tran_bdb()
  113. {
  114. MYSQL_RES *result;
  115. MYSQL_ROW row;
  116. int rc;
  117. myheader("test_tran_bdb");
  118. /* set AUTOCOMMIT to OFF */
  119. rc= mysql_autocommit(mysql, FALSE);
  120. myquery(rc);
  121. rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction");
  122. myquery(rc);
  123. /* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
  124. rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction( "
  125. "col1 int , col2 varchar(30)) ENGINE= BDB");
  126. myquery(rc);
  127. /* insert a row and commit the transaction */
  128. rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')");
  129. myquery(rc);
  130. rc= mysql_commit(mysql);
  131. myquery(rc);
  132. /* now insert the second row, and roll back the transaction */
  133. rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')");
  134. myquery(rc);
  135. rc= mysql_rollback(mysql);
  136. myquery(rc);
  137. /* delete first row, and roll it back */
  138. rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10");
  139. myquery(rc);
  140. rc= mysql_rollback(mysql);
  141. myquery(rc);
  142. /* test the results now, only one row should exist */
  143. rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
  144. myquery(rc);
  145. /* get the result */
  146. result= mysql_store_result(mysql);
  147. mytest(result);
  148. (void) my_process_result_set(result);
  149. mysql_free_result(result);
  150. /* test the results now, only one row should exist */
  151. rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
  152. myquery(rc);
  153. /* get the result */
  154. result= mysql_use_result(mysql);
  155. mytest(result);
  156. row= mysql_fetch_row(result);
  157. mytest(row);
  158. row= mysql_fetch_row(result);
  159. mytest_r(row);
  160. mysql_free_result(result);
  161. mysql_autocommit(mysql, TRUE);
  162. }
  163. /* Test autocommit feature for InnoDB tables */
  164. static void test_tran_innodb()
  165. {
  166. MYSQL_RES *result;
  167. MYSQL_ROW row;
  168. int rc;
  169. myheader("test_tran_innodb");
  170. /* set AUTOCOMMIT to OFF */
  171. rc= mysql_autocommit(mysql, FALSE);
  172. myquery(rc);
  173. rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction");
  174. myquery(rc);
  175. /* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
  176. rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction(col1 int, "
  177. "col2 varchar(30)) ENGINE= InnoDB");
  178. myquery(rc);
  179. /* insert a row and commit the transaction */
  180. rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')");
  181. myquery(rc);
  182. rc= mysql_commit(mysql);
  183. myquery(rc);
  184. /* now insert the second row, and roll back the transaction */
  185. rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')");
  186. myquery(rc);
  187. rc= mysql_rollback(mysql);
  188. myquery(rc);
  189. /* delete first row, and roll it back */
  190. rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10");
  191. myquery(rc);
  192. rc= mysql_rollback(mysql);
  193. myquery(rc);
  194. /* test the results now, only one row should exist */
  195. rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
  196. myquery(rc);
  197. /* get the result */
  198. result= mysql_store_result(mysql);
  199. mytest(result);
  200. (void) my_process_result_set(result);
  201. mysql_free_result(result);
  202. /* test the results now, only one row should exist */
  203. rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
  204. myquery(rc);
  205. /* get the result */
  206. result= mysql_use_result(mysql);
  207. mytest(result);
  208. row= mysql_fetch_row(result);
  209. mytest(row);
  210. row= mysql_fetch_row(result);
  211. mytest_r(row);
  212. mysql_free_result(result);
  213. mysql_autocommit(mysql, TRUE);
  214. }
  215. /* Test for BUG#7242 */
  216. static void test_prepare_insert_update()
  217. {
  218. MYSQL_STMT *stmt;
  219. int rc;
  220. int i;
  221. const char *testcase[]= {
  222. "CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B))",
  223. "INSERT t1 VALUES (1,2,10), (3,4,20)",
  224. "INSERT t1 VALUES (5,6,30), (7,4,40), (8,9,60) ON DUPLICATE KEY UPDATE c=c+100",
  225. "SELECT * FROM t1",
  226. "INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0",
  227. "SELECT * FROM t1",
  228. "INSERT t1 VALUES (2,1,11), (7,4,40) ON DUPLICATE KEY UPDATE c=c+VALUES(a)",
  229. NULL};
  230. const char **cur_query;
  231. myheader("test_prepare_insert_update");
  232. for (cur_query= testcase; *cur_query; cur_query++)
  233. {
  234. char query[MAX_TEST_QUERY_LENGTH];
  235. printf("\nRunning query: %s", *cur_query);
  236. strmov(query, *cur_query);
  237. stmt= mysql_simple_prepare(mysql, query);
  238. check_stmt(stmt);
  239. verify_param_count(stmt, 0);
  240. rc= mysql_stmt_execute(stmt);
  241. check_execute(stmt, rc);
  242. /* try the last query several times */
  243. if (!cur_query[1])
  244. {
  245. for (i=0; i < 3;i++)
  246. {
  247. printf("\nExecuting last statement again");
  248. rc= mysql_stmt_execute(stmt);
  249. check_execute(stmt, rc);
  250. rc= mysql_stmt_execute(stmt);
  251. check_execute(stmt, rc);
  252. }
  253. }
  254. mysql_stmt_close(stmt);
  255. }
  256. rc= mysql_commit(mysql);
  257. myquery(rc);
  258. }
  259. /* Test simple prepares of all DML statements */
  260. static void test_prepare_simple()
  261. {
  262. MYSQL_STMT *stmt;
  263. int rc;
  264. char query[MAX_TEST_QUERY_LENGTH];
  265. myheader("test_prepare_simple");
  266. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_simple");
  267. myquery(rc);
  268. rc= mysql_query(mysql, "CREATE TABLE test_prepare_simple("
  269. "id int, name varchar(50))");
  270. myquery(rc);
  271. /* insert */
  272. strmov(query, "INSERT INTO test_prepare_simple VALUES(?, ?)");
  273. stmt= mysql_simple_prepare(mysql, query);
  274. check_stmt(stmt);
  275. verify_param_count(stmt, 2);
  276. mysql_stmt_close(stmt);
  277. /* update */
  278. strmov(query, "UPDATE test_prepare_simple SET id=? "
  279. "WHERE id=? AND CONVERT(name USING utf8)= ?");
  280. stmt= mysql_simple_prepare(mysql, query);
  281. check_stmt(stmt);
  282. verify_param_count(stmt, 3);
  283. mysql_stmt_close(stmt);
  284. /* delete */
  285. strmov(query, "DELETE FROM test_prepare_simple WHERE id=10");
  286. stmt= mysql_simple_prepare(mysql, query);
  287. check_stmt(stmt);
  288. verify_param_count(stmt, 0);
  289. rc= mysql_stmt_execute(stmt);
  290. check_execute(stmt, rc);
  291. mysql_stmt_close(stmt);
  292. /* delete */
  293. strmov(query, "DELETE FROM test_prepare_simple WHERE id=?");
  294. stmt= mysql_simple_prepare(mysql, query);
  295. check_stmt(stmt);
  296. verify_param_count(stmt, 1);
  297. mysql_stmt_close(stmt);
  298. /* select */
  299. strmov(query, "SELECT * FROM test_prepare_simple WHERE id=? "
  300. "AND CONVERT(name USING utf8)= ?");
  301. stmt= mysql_simple_prepare(mysql, query);
  302. check_stmt(stmt);
  303. verify_param_count(stmt, 2);
  304. mysql_stmt_close(stmt);
  305. /* now fetch the results ..*/
  306. rc= mysql_commit(mysql);
  307. myquery(rc);
  308. }
  309. /* Test simple prepare field results */
  310. static void test_prepare_field_result()
  311. {
  312. MYSQL_STMT *stmt;
  313. MYSQL_RES *result;
  314. int rc;
  315. char query[MAX_TEST_QUERY_LENGTH];
  316. myheader("test_prepare_field_result");
  317. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_field_result");
  318. myquery(rc);
  319. rc= mysql_query(mysql, "CREATE TABLE test_prepare_field_result(int_c int, "
  320. "var_c varchar(50), ts_c timestamp(14), "
  321. "char_c char(4), date_c date, extra tinyint)");
  322. myquery(rc);
  323. /* insert */
  324. strmov(query, "SELECT int_c, var_c, date_c as date, ts_c, char_c FROM "
  325. " test_prepare_field_result as t1 WHERE int_c=?");
  326. stmt= mysql_simple_prepare(mysql, query);
  327. check_stmt(stmt);
  328. verify_param_count(stmt, 1);
  329. result= mysql_stmt_result_metadata(stmt);
  330. mytest(result);
  331. my_print_result_metadata(result);
  332. if (!opt_silent)
  333. fprintf(stdout, "\n\n field attributes:\n");
  334. verify_prepare_field(result, 0, "int_c", "int_c", MYSQL_TYPE_LONG,
  335. "t1", "test_prepare_field_result", current_db, 11, 0);
  336. verify_prepare_field(result, 1, "var_c", "var_c", MYSQL_TYPE_VAR_STRING,
  337. "t1", "test_prepare_field_result", current_db, 50, 0);
  338. verify_prepare_field(result, 2, "date", "date_c", MYSQL_TYPE_DATE,
  339. "t1", "test_prepare_field_result", current_db, 10, 0);
  340. verify_prepare_field(result, 3, "ts_c", "ts_c", MYSQL_TYPE_TIMESTAMP,
  341. "t1", "test_prepare_field_result", current_db, 19, 0);
  342. verify_prepare_field(result, 4, "char_c", "char_c",
  343. (mysql_get_server_version(mysql) <= 50000 ?
  344. MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING),
  345. "t1", "test_prepare_field_result", current_db, 4, 0);
  346. verify_field_count(result, 5);
  347. mysql_free_result(result);
  348. mysql_stmt_close(stmt);
  349. }
  350. /* Test simple prepare field results */
  351. static void test_prepare_syntax()
  352. {
  353. MYSQL_STMT *stmt;
  354. int rc;
  355. char query[MAX_TEST_QUERY_LENGTH];
  356. myheader("test_prepare_syntax");
  357. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_syntax");
  358. myquery(rc);
  359. rc= mysql_query(mysql, "CREATE TABLE test_prepare_syntax("
  360. "id int, name varchar(50), extra int)");
  361. myquery(rc);
  362. strmov(query, "INSERT INTO test_prepare_syntax VALUES(?");
  363. stmt= mysql_simple_prepare(mysql, query);
  364. check_stmt_r(stmt);
  365. strmov(query, "SELECT id, name FROM test_prepare_syntax WHERE id=? AND WHERE");
  366. stmt= mysql_simple_prepare(mysql, query);
  367. check_stmt_r(stmt);
  368. /* now fetch the results ..*/
  369. rc= mysql_commit(mysql);
  370. myquery(rc);
  371. }
  372. /* Test a simple prepare */
  373. static void test_prepare()
  374. {
  375. MYSQL_STMT *stmt;
  376. int rc, i;
  377. int int_data, o_int_data;
  378. char str_data[50], data[50];
  379. char tiny_data, o_tiny_data;
  380. short small_data, o_small_data;
  381. longlong big_data, o_big_data;
  382. float real_data, o_real_data;
  383. double double_data, o_double_data;
  384. ulong length[7], len;
  385. my_bool is_null[7];
  386. char llbuf[22];
  387. MYSQL_BIND my_bind[7];
  388. char query[MAX_TEST_QUERY_LENGTH];
  389. myheader("test_prepare");
  390. rc= mysql_autocommit(mysql, TRUE);
  391. myquery(rc);
  392. rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
  393. myquery(rc);
  394. rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 tinyint, "
  395. "col2 varchar(15), col3 int, "
  396. "col4 smallint, col5 bigint, "
  397. "col6 float, col7 double )");
  398. myquery(rc);
  399. /* insert by prepare */
  400. strxmov(query, "INSERT INTO my_prepare VALUES(?, ?, ?, ?, ?, ?, ?)", NullS);
  401. stmt= mysql_simple_prepare(mysql, query);
  402. check_stmt(stmt);
  403. verify_param_count(stmt, 7);
  404. bzero((char*) my_bind, sizeof(my_bind));
  405. /* tinyint */
  406. my_bind[0].buffer_type= MYSQL_TYPE_TINY;
  407. my_bind[0].buffer= (void *)&tiny_data;
  408. /* string */
  409. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  410. my_bind[1].buffer= (void *)str_data;
  411. my_bind[1].buffer_length= 1000; /* Max string length */
  412. /* integer */
  413. my_bind[2].buffer_type= MYSQL_TYPE_LONG;
  414. my_bind[2].buffer= (void *)&int_data;
  415. /* short */
  416. my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
  417. my_bind[3].buffer= (void *)&small_data;
  418. /* bigint */
  419. my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
  420. my_bind[4].buffer= (void *)&big_data;
  421. /* float */
  422. my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
  423. my_bind[5].buffer= (void *)&real_data;
  424. /* double */
  425. my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
  426. my_bind[6].buffer= (void *)&double_data;
  427. for (i= 0; i < (int) array_elements(my_bind); i++)
  428. {
  429. my_bind[i].length= &length[i];
  430. my_bind[i].is_null= &is_null[i];
  431. is_null[i]= 0;
  432. }
  433. rc= mysql_stmt_bind_param(stmt, my_bind);
  434. check_execute(stmt, rc);
  435. int_data= 320;
  436. small_data= 1867;
  437. big_data= 1000;
  438. real_data= 2;
  439. double_data= 6578.001;
  440. /* now, execute the prepared statement to insert 10 records.. */
  441. for (tiny_data= 0; tiny_data < 100; tiny_data++)
  442. {
  443. length[1]= sprintf(str_data, "MySQL%d", int_data);
  444. rc= mysql_stmt_execute(stmt);
  445. check_execute(stmt, rc);
  446. int_data += 25;
  447. small_data += 10;
  448. big_data += 100;
  449. real_data += 1;
  450. double_data += 10.09;
  451. }
  452. mysql_stmt_close(stmt);
  453. /* now fetch the results ..*/
  454. rc= mysql_commit(mysql);
  455. myquery(rc);
  456. /* test the results now, only one row should exist */
  457. rc= my_stmt_result("SELECT * FROM my_prepare");
  458. DIE_UNLESS(tiny_data == (char) rc);
  459. stmt= mysql_simple_prepare(mysql, "SELECT * FROM my_prepare");
  460. check_stmt(stmt);
  461. rc= mysql_stmt_bind_result(stmt, my_bind);
  462. check_execute(stmt, rc);
  463. /* get the result */
  464. rc= mysql_stmt_execute(stmt);
  465. check_execute(stmt, rc);
  466. o_int_data= 320;
  467. o_small_data= 1867;
  468. o_big_data= 1000;
  469. o_real_data= 2;
  470. o_double_data= 6578.001;
  471. /* now, execute the prepared statement to insert 10 records.. */
  472. for (o_tiny_data= 0; o_tiny_data < 100; o_tiny_data++)
  473. {
  474. len= sprintf(data, "MySQL%d", o_int_data);
  475. rc= mysql_stmt_fetch(stmt);
  476. check_execute(stmt, rc);
  477. if (!opt_silent)
  478. {
  479. fprintf(stdout, "\n");
  480. fprintf(stdout, "\n\t tiny : %d (%lu)", tiny_data, length[0]);
  481. fprintf(stdout, "\n\t short : %d (%lu)", small_data, length[3]);
  482. fprintf(stdout, "\n\t int : %d (%lu)", int_data, length[2]);
  483. fprintf(stdout, "\n\t big : %s (%lu)", llstr(big_data, llbuf),
  484. length[4]);
  485. fprintf(stdout, "\n\t float : %f (%lu)", real_data, length[5]);
  486. fprintf(stdout, "\n\t double : %f (%lu)", double_data, length[6]);
  487. fprintf(stdout, "\n\t str : %s (%lu)", str_data, length[1]);
  488. }
  489. DIE_UNLESS(tiny_data == o_tiny_data);
  490. DIE_UNLESS(is_null[0] == 0);
  491. DIE_UNLESS(length[0] == 1);
  492. DIE_UNLESS(int_data == o_int_data);
  493. DIE_UNLESS(length[2] == 4);
  494. DIE_UNLESS(small_data == o_small_data);
  495. DIE_UNLESS(length[3] == 2);
  496. DIE_UNLESS(big_data == o_big_data);
  497. DIE_UNLESS(length[4] == 8);
  498. DIE_UNLESS(real_data == o_real_data);
  499. DIE_UNLESS(length[5] == 4);
  500. DIE_UNLESS(cmp_double(&double_data, &o_double_data));
  501. DIE_UNLESS(length[6] == 8);
  502. DIE_UNLESS(strcmp(data, str_data) == 0);
  503. DIE_UNLESS(length[1] == len);
  504. o_int_data += 25;
  505. o_small_data += 10;
  506. o_big_data += 100;
  507. o_real_data += 1;
  508. o_double_data += 10.09;
  509. }
  510. rc= mysql_stmt_fetch(stmt);
  511. DIE_UNLESS(rc == MYSQL_NO_DATA);
  512. mysql_stmt_close(stmt);
  513. }
  514. /* Test double comparision */
  515. static void test_double_compare()
  516. {
  517. MYSQL_STMT *stmt;
  518. int rc;
  519. char real_data[10], tiny_data;
  520. double double_data;
  521. MYSQL_RES *result;
  522. MYSQL_BIND my_bind[3];
  523. ulong length[3];
  524. char query[MAX_TEST_QUERY_LENGTH];
  525. myheader("test_double_compare");
  526. rc= mysql_autocommit(mysql, TRUE);
  527. myquery(rc);
  528. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_double_compare");
  529. myquery(rc);
  530. rc= mysql_query(mysql, "CREATE TABLE test_double_compare(col1 tinyint, "
  531. " col2 float, col3 double )");
  532. myquery(rc);
  533. rc= mysql_query(mysql, "INSERT INTO test_double_compare "
  534. "VALUES (1, 10.2, 34.5)");
  535. myquery(rc);
  536. strmov(query, "UPDATE test_double_compare SET col1=100 "
  537. "WHERE col1 = ? AND col2 = ? AND COL3 = ?");
  538. stmt= mysql_simple_prepare(mysql, query);
  539. check_stmt(stmt);
  540. verify_param_count(stmt, 3);
  541. /* Always bzero bind array because there can be internal members */
  542. bzero((char*) my_bind, sizeof(my_bind));
  543. /* tinyint */
  544. my_bind[0].buffer_type= MYSQL_TYPE_TINY;
  545. my_bind[0].buffer= (void *)&tiny_data;
  546. /* string->float */
  547. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  548. my_bind[1].buffer= (void *)&real_data;
  549. my_bind[1].buffer_length= sizeof(real_data);
  550. my_bind[1].length= &length[1];
  551. length[1]= 10;
  552. /* double */
  553. my_bind[2].buffer_type= MYSQL_TYPE_DOUBLE;
  554. my_bind[2].buffer= (void *)&double_data;
  555. tiny_data= 1;
  556. strmov(real_data, "10.2");
  557. double_data= 34.5;
  558. rc= mysql_stmt_bind_param(stmt, my_bind);
  559. check_execute(stmt, rc);
  560. rc= mysql_stmt_execute(stmt);
  561. check_execute(stmt, rc);
  562. verify_affected_rows(0);
  563. mysql_stmt_close(stmt);
  564. /* now fetch the results ..*/
  565. rc= mysql_commit(mysql);
  566. myquery(rc);
  567. /* test the results now, only one row should exist */
  568. rc= mysql_query(mysql, "SELECT * FROM test_double_compare");
  569. myquery(rc);
  570. /* get the result */
  571. result= mysql_store_result(mysql);
  572. mytest(result);
  573. rc= my_process_result_set(result);
  574. DIE_UNLESS((int)tiny_data == rc);
  575. mysql_free_result(result);
  576. }
  577. /* Test simple null */
  578. static void test_null()
  579. {
  580. MYSQL_STMT *stmt;
  581. int rc;
  582. uint nData;
  583. MYSQL_BIND my_bind[2];
  584. my_bool is_null[2];
  585. char query[MAX_TEST_QUERY_LENGTH];
  586. myheader("test_null");
  587. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_null");
  588. myquery(rc);
  589. rc= mysql_query(mysql, "CREATE TABLE test_null(col1 int, col2 varchar(50))");
  590. myquery(rc);
  591. /* insert by prepare, wrong column name */
  592. strmov(query, "INSERT INTO test_null(col3, col2) VALUES(?, ?)");
  593. stmt= mysql_simple_prepare(mysql, query);
  594. check_stmt_r(stmt);
  595. strmov(query, "INSERT INTO test_null(col1, col2) VALUES(?, ?)");
  596. stmt= mysql_simple_prepare(mysql, query);
  597. check_stmt(stmt);
  598. verify_param_count(stmt, 2);
  599. /* Always bzero all members of bind parameter */
  600. bzero((char*) my_bind, sizeof(my_bind));
  601. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  602. my_bind[0].is_null= &is_null[0];
  603. is_null[0]= 1;
  604. my_bind[1]= my_bind[0];
  605. rc= mysql_stmt_bind_param(stmt, my_bind);
  606. check_execute(stmt, rc);
  607. /* now, execute the prepared statement to insert 10 records.. */
  608. for (nData= 0; nData<10; nData++)
  609. {
  610. rc= mysql_stmt_execute(stmt);
  611. check_execute(stmt, rc);
  612. }
  613. /* Re-bind with MYSQL_TYPE_NULL */
  614. my_bind[0].buffer_type= MYSQL_TYPE_NULL;
  615. is_null[0]= 0; /* reset */
  616. my_bind[1]= my_bind[0];
  617. rc= mysql_stmt_bind_param(stmt, my_bind);
  618. check_execute(stmt, rc);
  619. for (nData= 0; nData<10; nData++)
  620. {
  621. rc= mysql_stmt_execute(stmt);
  622. check_execute(stmt, rc);
  623. }
  624. mysql_stmt_close(stmt);
  625. /* now fetch the results ..*/
  626. rc= mysql_commit(mysql);
  627. myquery(rc);
  628. nData*= 2;
  629. rc= my_stmt_result("SELECT * FROM test_null");;
  630. DIE_UNLESS((int) nData == rc);
  631. /* Fetch results */
  632. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  633. my_bind[0].buffer= (void *)&nData; /* this buffer won't be altered */
  634. my_bind[0].length= 0;
  635. my_bind[1]= my_bind[0];
  636. my_bind[0].is_null= &is_null[0];
  637. my_bind[1].is_null= &is_null[1];
  638. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_null");
  639. check_stmt(stmt);
  640. rc= mysql_stmt_execute(stmt);
  641. check_execute(stmt, rc);
  642. rc= mysql_stmt_bind_result(stmt, my_bind);
  643. check_execute(stmt, rc);
  644. rc= 0;
  645. is_null[0]= is_null[1]= 0;
  646. while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
  647. {
  648. DIE_UNLESS(is_null[0]);
  649. DIE_UNLESS(is_null[1]);
  650. rc++;
  651. is_null[0]= is_null[1]= 0;
  652. }
  653. DIE_UNLESS(rc == (int) nData);
  654. mysql_stmt_close(stmt);
  655. }
  656. /* Test for NULL as PS parameter (BUG#3367, BUG#3371) */
  657. static void test_ps_null_param()
  658. {
  659. MYSQL_STMT *stmt;
  660. int rc;
  661. MYSQL_BIND in_bind;
  662. my_bool in_is_null;
  663. long int in_long;
  664. MYSQL_BIND out_bind;
  665. ulong out_length;
  666. my_bool out_is_null;
  667. char out_str_data[20];
  668. const char *queries[]= {"select ?", "select ?+1",
  669. "select col1 from test_ps_nulls where col1 <=> ?",
  670. NULL
  671. };
  672. const char **cur_query= queries;
  673. myheader("test_null_ps_param_in_result");
  674. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ps_nulls");
  675. myquery(rc);
  676. rc= mysql_query(mysql, "CREATE TABLE test_ps_nulls(col1 int)");
  677. myquery(rc);
  678. rc= mysql_query(mysql, "INSERT INTO test_ps_nulls values (1), (null)");
  679. myquery(rc);
  680. /* Always bzero all members of bind parameter */
  681. bzero((char*) &in_bind, sizeof(in_bind));
  682. bzero((char*) &out_bind, sizeof(out_bind));
  683. in_bind.buffer_type= MYSQL_TYPE_LONG;
  684. in_bind.is_null= &in_is_null;
  685. in_bind.length= 0;
  686. in_bind.buffer= (void *)&in_long;
  687. in_is_null= 1;
  688. in_long= 1;
  689. out_bind.buffer_type= MYSQL_TYPE_STRING;
  690. out_bind.is_null= &out_is_null;
  691. out_bind.length= &out_length;
  692. out_bind.buffer= out_str_data;
  693. out_bind.buffer_length= array_elements(out_str_data);
  694. /* Execute several queries, all returning NULL in result. */
  695. for(cur_query= queries; *cur_query; cur_query++)
  696. {
  697. char query[MAX_TEST_QUERY_LENGTH];
  698. strmov(query, *cur_query);
  699. stmt= mysql_simple_prepare(mysql, query);
  700. check_stmt(stmt);
  701. verify_param_count(stmt, 1);
  702. rc= mysql_stmt_bind_param(stmt, &in_bind);
  703. check_execute(stmt, rc);
  704. rc= mysql_stmt_bind_result(stmt, &out_bind);
  705. check_execute(stmt, rc);
  706. rc= mysql_stmt_execute(stmt);
  707. check_execute(stmt, rc);
  708. rc= mysql_stmt_fetch(stmt);
  709. DIE_UNLESS(rc != MYSQL_NO_DATA);
  710. DIE_UNLESS(out_is_null);
  711. rc= mysql_stmt_fetch(stmt);
  712. DIE_UNLESS(rc == MYSQL_NO_DATA);
  713. mysql_stmt_close(stmt);
  714. }
  715. }
  716. /* Test fetch null */
  717. static void test_fetch_null()
  718. {
  719. MYSQL_STMT *stmt;
  720. int rc;
  721. int i, nData;
  722. MYSQL_BIND my_bind[11];
  723. ulong length[11];
  724. my_bool is_null[11];
  725. char query[MAX_TEST_QUERY_LENGTH];
  726. myheader("test_fetch_null");
  727. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_fetch_null");
  728. myquery(rc);
  729. rc= mysql_query(mysql, "CREATE TABLE test_fetch_null("
  730. " col1 tinyint, col2 smallint, "
  731. " col3 int, col4 bigint, "
  732. " col5 float, col6 double, "
  733. " col7 date, col8 time, "
  734. " col9 varbinary(10), "
  735. " col10 varchar(50), "
  736. " col11 char(20))");
  737. myquery(rc);
  738. rc= mysql_query(mysql, "INSERT INTO test_fetch_null (col11) "
  739. "VALUES (1000), (88), (389789)");
  740. myquery(rc);
  741. rc= mysql_commit(mysql);
  742. myquery(rc);
  743. /* fetch */
  744. bzero((char*) my_bind, sizeof(my_bind));
  745. for (i= 0; i < (int) array_elements(my_bind); i++)
  746. {
  747. my_bind[i].buffer_type= MYSQL_TYPE_LONG;
  748. my_bind[i].is_null= &is_null[i];
  749. my_bind[i].length= &length[i];
  750. }
  751. my_bind[i-1].buffer= (void *)&nData; /* Last column is not null */
  752. strmov((char *)query , "SELECT * FROM test_fetch_null");
  753. rc= my_stmt_result(query);
  754. DIE_UNLESS(rc == 3);
  755. stmt= mysql_simple_prepare(mysql, query);
  756. check_stmt(stmt);
  757. rc= mysql_stmt_bind_result(stmt, my_bind);
  758. check_execute(stmt, rc);
  759. rc= mysql_stmt_execute(stmt);
  760. check_execute(stmt, rc);
  761. rc= 0;
  762. while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
  763. {
  764. rc++;
  765. for (i= 0; i < 10; i++)
  766. {
  767. if (!opt_silent)
  768. fprintf(stdout, "\n data[%d] : %s", i,
  769. is_null[i] ? "NULL" : "NOT NULL");
  770. DIE_UNLESS(is_null[i]);
  771. }
  772. if (!opt_silent)
  773. fprintf(stdout, "\n data[%d]: %d", i, nData);
  774. DIE_UNLESS(nData == 1000 || nData == 88 || nData == 389789);
  775. DIE_UNLESS(is_null[i] == 0);
  776. DIE_UNLESS(length[i] == 4);
  777. }
  778. DIE_UNLESS(rc == 3);
  779. mysql_stmt_close(stmt);
  780. }
  781. /* Test simple select */
  782. static void test_select_version()
  783. {
  784. MYSQL_STMT *stmt;
  785. int rc;
  786. myheader("test_select_version");
  787. stmt= mysql_simple_prepare(mysql, "SELECT @@version");
  788. check_stmt(stmt);
  789. verify_param_count(stmt, 0);
  790. rc= mysql_stmt_execute(stmt);
  791. check_execute(stmt, rc);
  792. my_process_stmt_result(stmt);
  793. mysql_stmt_close(stmt);
  794. }
  795. /* Test simple show */
  796. static void test_select_show_table()
  797. {
  798. MYSQL_STMT *stmt;
  799. int rc, i;
  800. myheader("test_select_show_table");
  801. stmt= mysql_simple_prepare(mysql, "SHOW TABLES FROM mysql");
  802. check_stmt(stmt);
  803. verify_param_count(stmt, 0);
  804. for (i= 1; i < 3; i++)
  805. {
  806. rc= mysql_stmt_execute(stmt);
  807. check_execute(stmt, rc);
  808. }
  809. my_process_stmt_result(stmt);
  810. mysql_stmt_close(stmt);
  811. }
  812. /* Test simple select to debug */
  813. static void test_select_direct()
  814. {
  815. int rc;
  816. MYSQL_RES *result;
  817. myheader("test_select_direct");
  818. rc= mysql_autocommit(mysql, TRUE);
  819. myquery(rc);
  820. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
  821. myquery(rc);
  822. rc= mysql_query(mysql, "CREATE TABLE test_select(id int, id1 tinyint, "
  823. " id2 float, "
  824. " id3 double, "
  825. " name varchar(50))");
  826. myquery(rc);
  827. /* insert a row and commit the transaction */
  828. rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 5, 2.3, 4.5, 'venu')");
  829. myquery(rc);
  830. rc= mysql_commit(mysql);
  831. myquery(rc);
  832. rc= mysql_query(mysql, "SELECT * FROM test_select");
  833. myquery(rc);
  834. /* get the result */
  835. result= mysql_store_result(mysql);
  836. mytest(result);
  837. (void) my_process_result_set(result);
  838. mysql_free_result(result);
  839. }
  840. /* Test simple select with prepare */
  841. static void test_select_prepare()
  842. {
  843. int rc;
  844. MYSQL_STMT *stmt;
  845. myheader("test_select_prepare");
  846. rc= mysql_autocommit(mysql, TRUE);
  847. myquery(rc);
  848. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
  849. myquery(rc);
  850. rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
  851. myquery(rc);
  852. /* insert a row and commit the transaction */
  853. rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
  854. myquery(rc);
  855. rc= mysql_commit(mysql);
  856. myquery(rc);
  857. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
  858. check_stmt(stmt);
  859. rc= mysql_stmt_execute(stmt);
  860. check_execute(stmt, rc);
  861. rc= my_process_stmt_result(stmt);
  862. DIE_UNLESS(rc == 1);
  863. mysql_stmt_close(stmt);
  864. rc= mysql_query(mysql, "DROP TABLE test_select");
  865. myquery(rc);
  866. rc= mysql_query(mysql, "CREATE TABLE test_select(id tinyint, id1 int, "
  867. " id2 float, id3 float, "
  868. " name varchar(50))");
  869. myquery(rc);
  870. /* insert a row and commit the transaction */
  871. rc= mysql_query(mysql, "INSERT INTO test_select(id, id1, id2, name) VALUES(10, 5, 2.3, 'venu')");
  872. myquery(rc);
  873. rc= mysql_commit(mysql);
  874. myquery(rc);
  875. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
  876. check_stmt(stmt);
  877. rc= mysql_stmt_execute(stmt);
  878. check_execute(stmt, rc);
  879. rc= my_process_stmt_result(stmt);
  880. DIE_UNLESS(rc == 1);
  881. mysql_stmt_close(stmt);
  882. }
  883. /* Test simple select */
  884. static void test_select()
  885. {
  886. MYSQL_STMT *stmt;
  887. int rc;
  888. char szData[25];
  889. int nData= 1;
  890. MYSQL_BIND my_bind[2];
  891. ulong length[2];
  892. char query[MAX_TEST_QUERY_LENGTH];
  893. myheader("test_select");
  894. rc= mysql_autocommit(mysql, TRUE);
  895. myquery(rc);
  896. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
  897. myquery(rc);
  898. rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
  899. myquery(rc);
  900. /* insert a row and commit the transaction */
  901. rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
  902. myquery(rc);
  903. /* now insert the second row, and roll back the transaction */
  904. rc= mysql_query(mysql, "INSERT INTO test_select VALUES(20, 'mysql')");
  905. myquery(rc);
  906. rc= mysql_commit(mysql);
  907. myquery(rc);
  908. strmov(query, "SELECT * FROM test_select WHERE id= ? "
  909. "AND CONVERT(name USING utf8) =?");
  910. stmt= mysql_simple_prepare(mysql, query);
  911. check_stmt(stmt);
  912. verify_param_count(stmt, 2);
  913. /* Always bzero all members of bind parameter */
  914. bzero((char*) my_bind, sizeof(my_bind));
  915. /* string data */
  916. nData= 10;
  917. strmov(szData, (char *)"venu");
  918. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  919. my_bind[1].buffer= (void *)szData;
  920. my_bind[1].buffer_length= 4;
  921. my_bind[1].length= &length[1];
  922. length[1]= 4;
  923. my_bind[0].buffer= (void *)&nData;
  924. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  925. rc= mysql_stmt_bind_param(stmt, my_bind);
  926. check_execute(stmt, rc);
  927. rc= mysql_stmt_execute(stmt);
  928. check_execute(stmt, rc);
  929. rc= my_process_stmt_result(stmt);
  930. DIE_UNLESS(rc == 1);
  931. mysql_stmt_close(stmt);
  932. }
  933. /*
  934. Test for BUG#3420 ("select id1, value1 from t where id= ? or value= ?"
  935. returns all rows in the table)
  936. */
  937. static void test_ps_conj_select()
  938. {
  939. MYSQL_STMT *stmt;
  940. int rc;
  941. MYSQL_BIND my_bind[2];
  942. int32 int_data;
  943. char str_data[32];
  944. unsigned long str_length;
  945. char query[MAX_TEST_QUERY_LENGTH];
  946. myheader("test_ps_conj_select");
  947. rc= mysql_query(mysql, "drop table if exists t1");
  948. myquery(rc);
  949. rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
  950. "value2 varchar(100), value1 varchar(100))");
  951. myquery(rc);
  952. rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
  953. "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
  954. myquery(rc);
  955. strmov(query, "select id1, value1 from t1 where id1= ? or "
  956. "CONVERT(value1 USING utf8)= ?");
  957. stmt= mysql_simple_prepare(mysql, query);
  958. check_stmt(stmt);
  959. verify_param_count(stmt, 2);
  960. /* Always bzero all members of bind parameter */
  961. bzero((char*) my_bind, sizeof(my_bind));
  962. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  963. my_bind[0].buffer= (void *)&int_data;
  964. my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
  965. my_bind[1].buffer= (void *)str_data;
  966. my_bind[1].buffer_length= array_elements(str_data);
  967. my_bind[1].length= &str_length;
  968. rc= mysql_stmt_bind_param(stmt, my_bind);
  969. check_execute(stmt, rc);
  970. int_data= 1;
  971. strmov(str_data, "hh");
  972. str_length= strlen(str_data);
  973. rc= mysql_stmt_execute(stmt);
  974. check_execute(stmt, rc);
  975. rc= my_process_stmt_result(stmt);
  976. DIE_UNLESS(rc == 3);
  977. mysql_stmt_close(stmt);
  978. }
  979. /* reads Qcache_hits from server and returns its value */
  980. static uint query_cache_hits(MYSQL *conn)
  981. {
  982. MYSQL_RES *res;
  983. MYSQL_ROW row;
  984. int rc;
  985. uint result;
  986. rc= mysql_query(conn, "show status like 'qcache_hits'");
  987. myquery(rc);
  988. res= mysql_use_result(conn);
  989. DIE_UNLESS(res);
  990. row= mysql_fetch_row(res);
  991. DIE_UNLESS(row);
  992. result= atoi(row[1]);
  993. mysql_free_result(res);
  994. return result;
  995. }
  996. /*
  997. utility for the next test; expects 3 rows in the result from a SELECT,
  998. compares each row/field with an expected value.
  999. */
  1000. #define test_ps_query_cache_result(i1,s1,l1,i2,s2,l2,i3,s3,l3) \
  1001. r_metadata= mysql_stmt_result_metadata(stmt); \
  1002. DIE_UNLESS(r_metadata != NULL); \
  1003. rc= mysql_stmt_fetch(stmt); \
  1004. check_execute(stmt, rc); \
  1005. if (!opt_silent) \
  1006. fprintf(stdout, "\n row 1: %d, %s(%lu)", r_int_data, \
  1007. r_str_data, r_str_length); \
  1008. DIE_UNLESS((r_int_data == i1) && (r_str_length == l1) && \
  1009. (strcmp(r_str_data, s1) == 0)); \
  1010. rc= mysql_stmt_fetch(stmt); \
  1011. check_execute(stmt, rc); \
  1012. if (!opt_silent) \
  1013. fprintf(stdout, "\n row 2: %d, %s(%lu)", r_int_data, \
  1014. r_str_data, r_str_length); \
  1015. DIE_UNLESS((r_int_data == i2) && (r_str_length == l2) && \
  1016. (strcmp(r_str_data, s2) == 0)); \
  1017. rc= mysql_stmt_fetch(stmt); \
  1018. check_execute(stmt, rc); \
  1019. if (!opt_silent) \
  1020. fprintf(stdout, "\n row 3: %d, %s(%lu)", r_int_data, \
  1021. r_str_data, r_str_length); \
  1022. DIE_UNLESS((r_int_data == i3) && (r_str_length == l3) && \
  1023. (strcmp(r_str_data, s3) == 0)); \
  1024. rc= mysql_stmt_fetch(stmt); \
  1025. DIE_UNLESS(rc == MYSQL_NO_DATA); \
  1026. mysql_free_result(r_metadata);
  1027. /*
  1028. Test that prepared statements make use of the query cache just as normal
  1029. statements (BUG#735).
  1030. */
  1031. static void test_ps_query_cache()
  1032. {
  1033. MYSQL *lmysql= mysql;
  1034. MYSQL_STMT *stmt;
  1035. int rc;
  1036. MYSQL_BIND p_bind[2],r_bind[2]; /* p: param bind; r: result bind */
  1037. int32 p_int_data, r_int_data;
  1038. char p_str_data[32], r_str_data[32];
  1039. unsigned long p_str_length, r_str_length;
  1040. MYSQL_RES *r_metadata;
  1041. char query[MAX_TEST_QUERY_LENGTH];
  1042. uint hits1, hits2;
  1043. enum enum_test_ps_query_cache
  1044. {
  1045. /*
  1046. We iterate the same prepare/executes block, but have iterations where
  1047. we vary the query cache conditions.
  1048. */
  1049. /* the query cache is enabled for the duration of prep&execs: */
  1050. TEST_QCACHE_ON= 0,
  1051. /*
  1052. same but using a new connection (to see if qcache serves results from
  1053. the previous connection as it should):
  1054. */
  1055. TEST_QCACHE_ON_WITH_OTHER_CONN,
  1056. /*
  1057. First border case: disables the query cache before prepare and
  1058. re-enables it before execution (to test if we have no bug then):
  1059. */
  1060. TEST_QCACHE_OFF_ON,
  1061. /*
  1062. Second border case: enables the query cache before prepare and
  1063. disables it before execution:
  1064. */
  1065. TEST_QCACHE_ON_OFF
  1066. };
  1067. enum enum_test_ps_query_cache iteration;
  1068. myheader("test_ps_query_cache");
  1069. rc= mysql_query(mysql, "SET SQL_MODE=''");
  1070. myquery(rc);
  1071. /* prepare the table */
  1072. rc= mysql_query(mysql, "drop table if exists t1");
  1073. myquery(rc);
  1074. rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
  1075. "value2 varchar(100), value1 varchar(100))");
  1076. myquery(rc);
  1077. rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
  1078. "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
  1079. myquery(rc);
  1080. for (iteration= TEST_QCACHE_ON; iteration <= TEST_QCACHE_ON_OFF; iteration++)
  1081. {
  1082. switch (iteration) {
  1083. case TEST_QCACHE_ON:
  1084. case TEST_QCACHE_ON_OFF:
  1085. rc= mysql_query(lmysql, "set global query_cache_size=1000000");
  1086. myquery(rc);
  1087. break;
  1088. case TEST_QCACHE_OFF_ON:
  1089. rc= mysql_query(lmysql, "set global query_cache_size=0");
  1090. myquery(rc);
  1091. break;
  1092. case TEST_QCACHE_ON_WITH_OTHER_CONN:
  1093. if (!opt_silent)
  1094. fprintf(stdout, "\n Establishing a test connection ...");
  1095. if (!(lmysql= mysql_client_init(NULL)))
  1096. {
  1097. printf("mysql_client_init() failed");
  1098. DIE_UNLESS(0);
  1099. }
  1100. if (!(mysql_real_connect(lmysql, opt_host, opt_user,
  1101. opt_password, current_db, opt_port,
  1102. opt_unix_socket, 0)))
  1103. {
  1104. printf("connection failed");
  1105. mysql_close(lmysql);
  1106. DIE_UNLESS(0);
  1107. }
  1108. rc= mysql_query(lmysql, "SET SQL_MODE=''");
  1109. myquery(rc);
  1110. if (!opt_silent)
  1111. fprintf(stdout, "OK");
  1112. }
  1113. strmov(query, "select id1, value1 from t1 where id1= ? or "
  1114. "CONVERT(value1 USING utf8)= ?");
  1115. stmt= mysql_simple_prepare(lmysql, query);
  1116. check_stmt(stmt);
  1117. verify_param_count(stmt, 2);
  1118. switch (iteration) {
  1119. case TEST_QCACHE_OFF_ON:
  1120. rc= mysql_query(lmysql, "set global query_cache_size=1000000");
  1121. myquery(rc);
  1122. break;
  1123. case TEST_QCACHE_ON_OFF:
  1124. rc= mysql_query(lmysql, "set global query_cache_size=0");
  1125. myquery(rc);
  1126. default:
  1127. break;
  1128. }
  1129. bzero((char*) p_bind, sizeof(p_bind));
  1130. p_bind[0].buffer_type= MYSQL_TYPE_LONG;
  1131. p_bind[0].buffer= (void *)&p_int_data;
  1132. p_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
  1133. p_bind[1].buffer= (void *)p_str_data;
  1134. p_bind[1].buffer_length= array_elements(p_str_data);
  1135. p_bind[1].length= &p_str_length;
  1136. rc= mysql_stmt_bind_param(stmt, p_bind);
  1137. check_execute(stmt, rc);
  1138. p_int_data= 1;
  1139. strmov(p_str_data, "hh");
  1140. p_str_length= strlen(p_str_data);
  1141. bzero((char*) r_bind, sizeof(r_bind));
  1142. r_bind[0].buffer_type= MYSQL_TYPE_LONG;
  1143. r_bind[0].buffer= (void *)&r_int_data;
  1144. r_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
  1145. r_bind[1].buffer= (void *)r_str_data;
  1146. r_bind[1].buffer_length= array_elements(r_str_data);
  1147. r_bind[1].length= &r_str_length;
  1148. rc= mysql_stmt_bind_result(stmt, r_bind);
  1149. check_execute(stmt, rc);
  1150. rc= mysql_stmt_execute(stmt);
  1151. check_execute(stmt, rc);
  1152. test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
  1153. /* now retry with the same parameter values and see qcache hits */
  1154. hits1= query_cache_hits(lmysql);
  1155. rc= mysql_stmt_execute(stmt);
  1156. check_execute(stmt, rc);
  1157. test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
  1158. hits2= query_cache_hits(lmysql);
  1159. switch(iteration) {
  1160. case TEST_QCACHE_ON_WITH_OTHER_CONN:
  1161. case TEST_QCACHE_ON: /* should have hit */
  1162. DIE_UNLESS(hits2-hits1 == 1);
  1163. break;
  1164. case TEST_QCACHE_OFF_ON:
  1165. case TEST_QCACHE_ON_OFF: /* should not have hit */
  1166. DIE_UNLESS(hits2-hits1 == 0);
  1167. break;
  1168. }
  1169. /* now modify parameter values and see qcache hits */
  1170. strmov(p_str_data, "ii");
  1171. p_str_length= strlen(p_str_data);
  1172. rc= mysql_stmt_execute(stmt);
  1173. check_execute(stmt, rc);
  1174. test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
  1175. hits1= query_cache_hits(lmysql);
  1176. switch(iteration) {
  1177. case TEST_QCACHE_ON:
  1178. case TEST_QCACHE_OFF_ON:
  1179. case TEST_QCACHE_ON_OFF: /* should not have hit */
  1180. DIE_UNLESS(hits2-hits1 == 0);
  1181. break;
  1182. case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
  1183. DIE_UNLESS(hits1-hits2 == 1);
  1184. break;
  1185. }
  1186. rc= mysql_stmt_execute(stmt);
  1187. check_execute(stmt, rc);
  1188. test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
  1189. hits2= query_cache_hits(lmysql);
  1190. mysql_stmt_close(stmt);
  1191. switch(iteration) {
  1192. case TEST_QCACHE_ON: /* should have hit */
  1193. DIE_UNLESS(hits2-hits1 == 1);
  1194. break;
  1195. case TEST_QCACHE_OFF_ON:
  1196. case TEST_QCACHE_ON_OFF: /* should not have hit */
  1197. DIE_UNLESS(hits2-hits1 == 0);
  1198. break;
  1199. case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
  1200. DIE_UNLESS(hits2-hits1 == 1);
  1201. break;
  1202. }
  1203. } /* for(iteration=...) */
  1204. if (lmysql != mysql)
  1205. mysql_close(lmysql);
  1206. rc= mysql_query(mysql, "set global query_cache_size=0");
  1207. myquery(rc);
  1208. }
  1209. /* Test BUG#1115 (incorrect string parameter value allocation) */
  1210. static void test_bug1115()
  1211. {
  1212. MYSQL_STMT *stmt;
  1213. int rc;
  1214. MYSQL_BIND my_bind[1];
  1215. ulong length[1];
  1216. char szData[11];
  1217. char query[MAX_TEST_QUERY_LENGTH];
  1218. myheader("test_bug1115");
  1219. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
  1220. myquery(rc);
  1221. rc= mysql_query(mysql, "CREATE TABLE test_select(\
  1222. session_id char(9) NOT NULL, \
  1223. a int(8) unsigned NOT NULL, \
  1224. b int(5) NOT NULL, \
  1225. c int(5) NOT NULL, \
  1226. d datetime NOT NULL)");
  1227. myquery(rc);
  1228. rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
  1229. "(\"abc\", 1, 2, 3, 2003-08-30), "
  1230. "(\"abd\", 1, 2, 3, 2003-08-30), "
  1231. "(\"abf\", 1, 2, 3, 2003-08-30), "
  1232. "(\"abg\", 1, 2, 3, 2003-08-30), "
  1233. "(\"abh\", 1, 2, 3, 2003-08-30), "
  1234. "(\"abj\", 1, 2, 3, 2003-08-30), "
  1235. "(\"abk\", 1, 2, 3, 2003-08-30), "
  1236. "(\"abl\", 1, 2, 3, 2003-08-30), "
  1237. "(\"abq\", 1, 2, 3, 2003-08-30) ");
  1238. myquery(rc);
  1239. rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
  1240. "(\"abw\", 1, 2, 3, 2003-08-30), "
  1241. "(\"abe\", 1, 2, 3, 2003-08-30), "
  1242. "(\"abr\", 1, 2, 3, 2003-08-30), "
  1243. "(\"abt\", 1, 2, 3, 2003-08-30), "
  1244. "(\"aby\", 1, 2, 3, 2003-08-30), "
  1245. "(\"abu\", 1, 2, 3, 2003-08-30), "
  1246. "(\"abi\", 1, 2, 3, 2003-08-30), "
  1247. "(\"abo\", 1, 2, 3, 2003-08-30), "
  1248. "(\"abp\", 1, 2, 3, 2003-08-30), "
  1249. "(\"abz\", 1, 2, 3, 2003-08-30), "
  1250. "(\"abx\", 1, 2, 3, 2003-08-30)");
  1251. myquery(rc);
  1252. strmov(query, "SELECT * FROM test_select WHERE "
  1253. "CONVERT(session_id USING utf8)= ?");
  1254. stmt= mysql_simple_prepare(mysql, query);
  1255. check_stmt(stmt);
  1256. verify_param_count(stmt, 1);
  1257. /* Always bzero all members of bind parameter */
  1258. bzero((char*) my_bind, sizeof(my_bind));
  1259. strmov(szData, (char *)"abc");
  1260. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  1261. my_bind[0].buffer= (void *)szData;
  1262. my_bind[0].buffer_length= 10;
  1263. my_bind[0].length= &length[0];
  1264. length[0]= 3;
  1265. rc= mysql_stmt_bind_param(stmt, my_bind);
  1266. check_execute(stmt, rc);
  1267. rc= mysql_stmt_execute(stmt);
  1268. check_execute(stmt, rc);
  1269. rc= my_process_stmt_result(stmt);
  1270. DIE_UNLESS(rc == 1);
  1271. strmov(szData, (char *)"venu");
  1272. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  1273. my_bind[0].buffer= (void *)szData;
  1274. my_bind[0].buffer_length= 10;
  1275. my_bind[0].length= &length[0];
  1276. length[0]= 4;
  1277. my_bind[0].is_null= 0;
  1278. rc= mysql_stmt_bind_param(stmt, my_bind);
  1279. check_execute(stmt, rc);
  1280. rc= mysql_stmt_execute(stmt);
  1281. check_execute(stmt, rc);
  1282. rc= my_process_stmt_result(stmt);
  1283. DIE_UNLESS(rc == 0);
  1284. strmov(szData, (char *)"abc");
  1285. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  1286. my_bind[0].buffer= (void *)szData;
  1287. my_bind[0].buffer_length= 10;
  1288. my_bind[0].length= &length[0];
  1289. length[0]= 3;
  1290. my_bind[0].is_null= 0;
  1291. rc= mysql_stmt_bind_param(stmt, my_bind);
  1292. check_execute(stmt, rc);
  1293. rc= mysql_stmt_execute(stmt);
  1294. check_execute(stmt, rc);
  1295. rc= my_process_stmt_result(stmt);
  1296. DIE_UNLESS(rc == 1);
  1297. mysql_stmt_close(stmt);
  1298. }
  1299. /* Test BUG#1180 (optimized away part of WHERE clause) */
  1300. static void test_bug1180()
  1301. {
  1302. MYSQL_STMT *stmt;
  1303. int rc;
  1304. MYSQL_BIND my_bind[1];
  1305. ulong length[1];
  1306. char szData[11];
  1307. char query[MAX_TEST_QUERY_LENGTH];
  1308. myheader("test_select_bug");
  1309. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
  1310. myquery(rc);
  1311. rc= mysql_query(mysql, "CREATE TABLE test_select(session_id char(9) NOT NULL)");
  1312. myquery(rc);
  1313. rc= mysql_query(mysql, "INSERT INTO test_select VALUES (\"abc\")");
  1314. myquery(rc);
  1315. strmov(query, "SELECT * FROM test_select WHERE ?= \"1111\" and "
  1316. "session_id= \"abc\"");
  1317. stmt= mysql_simple_prepare(mysql, query);
  1318. check_stmt(stmt);
  1319. verify_param_count(stmt, 1);
  1320. /* Always bzero all members of bind parameter */
  1321. bzero((char*) my_bind, sizeof(my_bind));
  1322. strmov(szData, (char *)"abc");
  1323. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  1324. my_bind[0].buffer= (void *)szData;
  1325. my_bind[0].buffer_length= 10;
  1326. my_bind[0].length= &length[0];
  1327. length[0]= 3;
  1328. my_bind[0].is_null= 0;
  1329. rc= mysql_stmt_bind_param(stmt, my_bind);
  1330. check_execute(stmt, rc);
  1331. rc= mysql_stmt_execute(stmt);
  1332. check_execute(stmt, rc);
  1333. rc= my_process_stmt_result(stmt);
  1334. DIE_UNLESS(rc == 0);
  1335. strmov(szData, (char *)"1111");
  1336. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  1337. my_bind[0].buffer= (void *)szData;
  1338. my_bind[0].buffer_length= 10;
  1339. my_bind[0].length= &length[0];
  1340. length[0]= 4;
  1341. my_bind[0].is_null= 0;
  1342. rc= mysql_stmt_bind_param(stmt, my_bind);
  1343. check_execute(stmt, rc);
  1344. rc= mysql_stmt_execute(stmt);
  1345. check_execute(stmt, rc);
  1346. rc= my_process_stmt_result(stmt);
  1347. DIE_UNLESS(rc == 1);
  1348. strmov(szData, (char *)"abc");
  1349. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  1350. my_bind[0].buffer= (void *)szData;
  1351. my_bind[0].buffer_length= 10;
  1352. my_bind[0].length= &length[0];
  1353. length[0]= 3;
  1354. my_bind[0].is_null= 0;
  1355. rc= mysql_stmt_bind_param(stmt, my_bind);
  1356. check_execute(stmt, rc);
  1357. rc= mysql_stmt_execute(stmt);
  1358. check_execute(stmt, rc);
  1359. rc= my_process_stmt_result(stmt);
  1360. DIE_UNLESS(rc == 0);
  1361. mysql_stmt_close(stmt);
  1362. }
  1363. /*
  1364. Test BUG#1644 (Insertion of more than 3 NULL columns with parameter
  1365. binding fails)
  1366. */
  1367. static void test_bug1644()
  1368. {
  1369. MYSQL_STMT *stmt;
  1370. MYSQL_RES *result;
  1371. MYSQL_ROW row;
  1372. MYSQL_BIND my_bind[4];
  1373. int num;
  1374. my_bool isnull;
  1375. int rc, i;
  1376. char query[MAX_TEST_QUERY_LENGTH];
  1377. myheader("test_bug1644");
  1378. rc= mysql_query(mysql, "DROP TABLE IF EXISTS foo_dfr");
  1379. myquery(rc);
  1380. rc= mysql_query(mysql,
  1381. "CREATE TABLE foo_dfr(col1 int, col2 int, col3 int, col4 int);");
  1382. myquery(rc);
  1383. strmov(query, "INSERT INTO foo_dfr VALUES (?, ?, ?, ? )");
  1384. stmt= mysql_simple_prepare(mysql, query);
  1385. check_stmt(stmt);
  1386. verify_param_count(stmt, 4);
  1387. /* Always bzero all members of bind parameter */
  1388. bzero((char*) my_bind, sizeof(my_bind));
  1389. num= 22;
  1390. isnull= 0;
  1391. for (i= 0 ; i < 4 ; i++)
  1392. {
  1393. my_bind[i].buffer_type= MYSQL_TYPE_LONG;
  1394. my_bind[i].buffer= (void *)&num;
  1395. my_bind[i].is_null= &isnull;
  1396. }
  1397. rc= mysql_stmt_bind_param(stmt, my_bind);
  1398. check_execute(stmt, rc);
  1399. rc= mysql_stmt_execute(stmt);
  1400. check_execute(stmt, rc);
  1401. isnull= 1;
  1402. for (i= 0 ; i < 4 ; i++)
  1403. my_bind[i].is_null= &isnull;
  1404. rc= mysql_stmt_bind_param(stmt, my_bind);
  1405. check_execute(stmt, rc);
  1406. rc= mysql_stmt_execute(stmt);
  1407. check_execute(stmt, rc);
  1408. isnull= 0;
  1409. num= 88;
  1410. for (i= 0 ; i < 4 ; i++)
  1411. my_bind[i].is_null= &isnull;
  1412. rc= mysql_stmt_bind_param(stmt, my_bind);
  1413. check_execute(stmt, rc);
  1414. rc= mysql_stmt_execute(stmt);
  1415. check_execute(stmt, rc);
  1416. mysql_stmt_close(stmt);
  1417. rc= mysql_query(mysql, "SELECT * FROM foo_dfr");
  1418. myquery(rc);
  1419. result= mysql_store_result(mysql);
  1420. mytest(result);
  1421. rc= my_process_result_set(result);
  1422. DIE_UNLESS(rc == 3);
  1423. mysql_data_seek(result, 0);
  1424. row= mysql_fetch_row(result);
  1425. mytest(row);
  1426. for (i= 0 ; i < 4 ; i++)
  1427. {
  1428. DIE_UNLESS(strcmp(row[i], "22") == 0);
  1429. }
  1430. row= mysql_fetch_row(result);
  1431. mytest(row);
  1432. for (i= 0 ; i < 4 ; i++)
  1433. {
  1434. DIE_UNLESS(row[i] == 0);
  1435. }
  1436. row= mysql_fetch_row(result);
  1437. mytest(row);
  1438. for (i= 0 ; i < 4 ; i++)
  1439. {
  1440. DIE_UNLESS(strcmp(row[i], "88") == 0);
  1441. }
  1442. row= mysql_fetch_row(result);
  1443. mytest_r(row);
  1444. mysql_free_result(result);
  1445. }
  1446. /* Test simple select show */
  1447. static void test_select_show()
  1448. {
  1449. MYSQL_STMT *st

Large files files are truncated, but you can click here to view the full file