PageRenderTime 133ms CodeModel.GetById 20ms RepoModel.GetById 1ms 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
  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 *stmt;
  1450. int rc;
  1451. char query[MAX_TEST_QUERY_LENGTH];
  1452. myheader("test_select_show");
  1453. mysql_autocommit(mysql, TRUE);
  1454. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_show");
  1455. myquery(rc);
  1456. rc= mysql_query(mysql, "CREATE TABLE test_show(id int(4) NOT NULL primary "
  1457. " key, name char(2))");
  1458. myquery(rc);
  1459. stmt= mysql_simple_prepare(mysql, "show columns from test_show");
  1460. check_stmt(stmt);
  1461. verify_param_count(stmt, 0);
  1462. rc= mysql_stmt_execute(stmt);
  1463. check_execute(stmt, rc);
  1464. my_process_stmt_result(stmt);
  1465. mysql_stmt_close(stmt);
  1466. stmt= mysql_simple_prepare(mysql, "show tables from mysql like ?");
  1467. check_stmt_r(stmt);
  1468. strxmov(query, "show tables from ", current_db, " like \'test_show\'", NullS);
  1469. stmt= mysql_simple_prepare(mysql, query);
  1470. check_stmt(stmt);
  1471. rc= mysql_stmt_execute(stmt);
  1472. check_execute(stmt, rc);
  1473. my_process_stmt_result(stmt);
  1474. mysql_stmt_close(stmt);
  1475. stmt= mysql_simple_prepare(mysql, "describe test_show");
  1476. check_stmt(stmt);
  1477. rc= mysql_stmt_execute(stmt);
  1478. check_execute(stmt, rc);
  1479. my_process_stmt_result(stmt);
  1480. mysql_stmt_close(stmt);
  1481. stmt= mysql_simple_prepare(mysql, "show keys from test_show");
  1482. check_stmt(stmt);
  1483. rc= mysql_stmt_execute(stmt);
  1484. check_execute(stmt, rc);
  1485. rc= my_process_stmt_result(stmt);
  1486. DIE_UNLESS(rc == 1);
  1487. mysql_stmt_close(stmt);
  1488. }
  1489. /* Test simple update */
  1490. static void test_simple_update()
  1491. {
  1492. MYSQL_STMT *stmt;
  1493. int rc;
  1494. char szData[25];
  1495. int nData= 1;
  1496. MYSQL_RES *result;
  1497. MYSQL_BIND my_bind[2];
  1498. ulong length[2];
  1499. char query[MAX_TEST_QUERY_LENGTH];
  1500. myheader("test_simple_update");
  1501. rc= mysql_autocommit(mysql, TRUE);
  1502. myquery(rc);
  1503. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
  1504. myquery(rc);
  1505. rc= mysql_query(mysql, "CREATE TABLE test_update(col1 int, "
  1506. " col2 varchar(50), col3 int )");
  1507. myquery(rc);
  1508. rc= mysql_query(mysql, "INSERT INTO test_update VALUES(1, 'MySQL', 100)");
  1509. myquery(rc);
  1510. verify_affected_rows(1);
  1511. rc= mysql_commit(mysql);
  1512. myquery(rc);
  1513. /* insert by prepare */
  1514. strmov(query, "UPDATE test_update SET col2= ? WHERE col1= ?");
  1515. stmt= mysql_simple_prepare(mysql, query);
  1516. check_stmt(stmt);
  1517. verify_param_count(stmt, 2);
  1518. /* Always bzero all members of bind parameter */
  1519. bzero((char*) my_bind, sizeof(my_bind));
  1520. nData= 1;
  1521. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  1522. my_bind[0].buffer= szData; /* string data */
  1523. my_bind[0].buffer_length= sizeof(szData);
  1524. my_bind[0].length= &length[0];
  1525. length[0]= sprintf(szData, "updated-data");
  1526. my_bind[1].buffer= (void *) &nData;
  1527. my_bind[1].buffer_type= MYSQL_TYPE_LONG;
  1528. rc= mysql_stmt_bind_param(stmt, my_bind);
  1529. check_execute(stmt, rc);
  1530. rc= mysql_stmt_execute(stmt);
  1531. check_execute(stmt, rc);
  1532. verify_affected_rows(1);
  1533. mysql_stmt_close(stmt);
  1534. /* now fetch the results ..*/
  1535. rc= mysql_commit(mysql);
  1536. myquery(rc);
  1537. /* test the results now, only one row should exist */
  1538. rc= mysql_query(mysql, "SELECT * FROM test_update");
  1539. myquery(rc);
  1540. /* get the result */
  1541. result= mysql_store_result(mysql);
  1542. mytest(result);
  1543. rc= my_process_result_set(result);
  1544. DIE_UNLESS(rc == 1);
  1545. mysql_free_result(result);
  1546. }
  1547. /* Test simple long data handling */
  1548. static void test_long_data()
  1549. {
  1550. MYSQL_STMT *stmt;
  1551. int rc, int_data;
  1552. char *data= NullS;
  1553. MYSQL_RES *result;
  1554. MYSQL_BIND my_bind[3];
  1555. char query[MAX_TEST_QUERY_LENGTH];
  1556. myheader("test_long_data");
  1557. rc= mysql_autocommit(mysql, TRUE);
  1558. myquery(rc);
  1559. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
  1560. myquery(rc);
  1561. rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, "
  1562. " col2 long varchar, col3 long varbinary)");
  1563. myquery(rc);
  1564. strmov(query, "INSERT INTO test_long_data(col1, col2) VALUES(?)");
  1565. stmt= mysql_simple_prepare(mysql, query);
  1566. check_stmt_r(stmt);
  1567. strmov(query, "INSERT INTO test_long_data(col1, col2, col3) VALUES(?, ?, ?)");
  1568. stmt= mysql_simple_prepare(mysql, query);
  1569. check_stmt(stmt);
  1570. verify_param_count(stmt, 3);
  1571. /* Always bzero all members of bind parameter */
  1572. bzero((char*) my_bind, sizeof(my_bind));
  1573. my_bind[0].buffer= (void *)&int_data;
  1574. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  1575. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  1576. my_bind[2]= my_bind[1];
  1577. rc= mysql_stmt_bind_param(stmt, my_bind);
  1578. check_execute(stmt, rc);
  1579. int_data= 999;
  1580. data= (char *)"Michael";
  1581. /* supply data in pieces */
  1582. rc= mysql_stmt_send_long_data(stmt, 1, data, strlen(data));
  1583. data= (char *)" 'Monty' Widenius";
  1584. rc= mysql_stmt_send_long_data(stmt, 1, data, strlen(data));
  1585. check_execute(stmt, rc);
  1586. rc= mysql_stmt_send_long_data(stmt, 2, "Venu (venu@mysql.com)", 4);
  1587. check_execute(stmt, rc);
  1588. /* execute */
  1589. rc= mysql_stmt_execute(stmt);
  1590. if (!opt_silent)
  1591. fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
  1592. check_execute(stmt, rc);
  1593. rc= mysql_commit(mysql);
  1594. myquery(rc);
  1595. /* now fetch the results ..*/
  1596. rc= mysql_query(mysql, "SELECT * FROM test_long_data");
  1597. myquery(rc);
  1598. /* get the result */
  1599. result= mysql_store_result(mysql);
  1600. mytest(result);
  1601. rc= my_process_result_set(result);
  1602. DIE_UNLESS(rc == 1);
  1603. mysql_free_result(result);
  1604. verify_col_data("test_long_data", "col1", "999");
  1605. verify_col_data("test_long_data", "col2", "Michael 'Monty' Widenius");
  1606. verify_col_data("test_long_data", "col3", "Venu");
  1607. mysql_stmt_close(stmt);
  1608. }
  1609. /* Test long data (string) handling */
  1610. static void test_long_data_str()
  1611. {
  1612. MYSQL_STMT *stmt;
  1613. int rc, i;
  1614. char data[255];
  1615. long length;
  1616. ulong length1;
  1617. MYSQL_RES *result;
  1618. MYSQL_BIND my_bind[2];
  1619. my_bool is_null[2];
  1620. char query[MAX_TEST_QUERY_LENGTH];
  1621. myheader("test_long_data_str");
  1622. rc= mysql_autocommit(mysql, TRUE);
  1623. myquery(rc);
  1624. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
  1625. myquery(rc);
  1626. rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(id int, longstr long varchar)");
  1627. myquery(rc);
  1628. strmov(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
  1629. stmt= mysql_simple_prepare(mysql, query);
  1630. check_stmt(stmt);
  1631. verify_param_count(stmt, 2);
  1632. /* Always bzero all members of bind parameter */
  1633. bzero((char*) my_bind, sizeof(my_bind));
  1634. my_bind[0].buffer= (void *)&length;
  1635. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  1636. my_bind[0].is_null= &is_null[0];
  1637. is_null[0]= 0;
  1638. length= 0;
  1639. my_bind[1].buffer= data; /* string data */
  1640. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  1641. my_bind[1].length= &length1;
  1642. my_bind[1].is_null= &is_null[1];
  1643. is_null[1]= 0;
  1644. rc= mysql_stmt_bind_param(stmt, my_bind);
  1645. check_execute(stmt, rc);
  1646. length= 40;
  1647. strmov(data, "MySQL AB");
  1648. /* supply data in pieces */
  1649. for(i= 0; i < 4; i++)
  1650. {
  1651. rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 5);
  1652. check_execute(stmt, rc);
  1653. }
  1654. /* execute */
  1655. rc= mysql_stmt_execute(stmt);
  1656. if (!opt_silent)
  1657. fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
  1658. check_execute(stmt, rc);
  1659. mysql_stmt_close(stmt);
  1660. rc= mysql_commit(mysql);
  1661. myquery(rc);
  1662. /* now fetch the results ..*/
  1663. rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr FROM test_long_data_str");
  1664. myquery(rc);
  1665. /* get the result */
  1666. result= mysql_store_result(mysql);
  1667. mytest(result);
  1668. rc= my_process_result_set(result);
  1669. DIE_UNLESS(rc == 1);
  1670. mysql_free_result(result);
  1671. sprintf(data, "%d", i*5);
  1672. verify_col_data("test_long_data_str", "LENGTH(longstr)", data);
  1673. data[0]= '\0';
  1674. while (i--)
  1675. strxmov(data, data, "MySQL", NullS);
  1676. verify_col_data("test_long_data_str", "longstr", data);
  1677. rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
  1678. myquery(rc);
  1679. }
  1680. /* Test long data (string) handling */
  1681. static void test_long_data_str1()
  1682. {
  1683. MYSQL_STMT *stmt;
  1684. int rc, i;
  1685. char data[255];
  1686. long length;
  1687. ulong max_blob_length, blob_length, length1;
  1688. my_bool true_value;
  1689. MYSQL_RES *result;
  1690. MYSQL_BIND my_bind[2];
  1691. MYSQL_FIELD *field;
  1692. char query[MAX_TEST_QUERY_LENGTH];
  1693. myheader("test_long_data_str1");
  1694. rc= mysql_autocommit(mysql, TRUE);
  1695. myquery(rc);
  1696. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
  1697. myquery(rc);
  1698. rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(longstr long varchar, blb long varbinary)");
  1699. myquery(rc);
  1700. strmov(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
  1701. stmt= mysql_simple_prepare(mysql, query);
  1702. check_stmt(stmt);
  1703. verify_param_count(stmt, 2);
  1704. /* Always bzero all members of bind parameter */
  1705. bzero((char*) my_bind, sizeof(my_bind));
  1706. my_bind[0].buffer= data; /* string data */
  1707. my_bind[0].buffer_length= sizeof(data);
  1708. my_bind[0].length= &length1;
  1709. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  1710. length1= 0;
  1711. my_bind[1]= my_bind[0];
  1712. my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
  1713. rc= mysql_stmt_bind_param(stmt, my_bind);
  1714. check_execute(stmt, rc);
  1715. length= sprintf(data, "MySQL AB");
  1716. /* supply data in pieces */
  1717. for (i= 0; i < 3; i++)
  1718. {
  1719. rc= mysql_stmt_send_long_data(stmt, 0, data, length);
  1720. check_execute(stmt, rc);
  1721. rc= mysql_stmt_send_long_data(stmt, 1, data, 2);
  1722. check_execute(stmt, rc);
  1723. }
  1724. /* execute */
  1725. rc= mysql_stmt_execute(stmt);
  1726. if (!opt_silent)
  1727. fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
  1728. check_execute(stmt, rc);
  1729. mysql_stmt_close(stmt);
  1730. rc= mysql_commit(mysql);
  1731. myquery(rc);
  1732. /* now fetch the results ..*/
  1733. rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr, LENGTH(blb), blb FROM test_long_data_str");
  1734. myquery(rc);
  1735. /* get the result */
  1736. result= mysql_store_result(mysql);
  1737. mysql_field_seek(result, 1);
  1738. field= mysql_fetch_field(result);
  1739. max_blob_length= field->max_length;
  1740. mytest(result);
  1741. rc= my_process_result_set(result);
  1742. DIE_UNLESS(rc == 1);
  1743. mysql_free_result(result);
  1744. sprintf(data, "%ld", (long)i*length);
  1745. verify_col_data("test_long_data_str", "length(longstr)", data);
  1746. sprintf(data, "%d", i*2);
  1747. verify_col_data("test_long_data_str", "length(blb)", data);
  1748. /* Test length of field->max_length */
  1749. stmt= mysql_simple_prepare(mysql, "SELECT * from test_long_data_str");
  1750. check_stmt(stmt);
  1751. verify_param_count(stmt, 0);
  1752. rc= mysql_stmt_execute(stmt);
  1753. check_execute(stmt, rc);
  1754. rc= mysql_stmt_store_result(stmt);
  1755. check_execute(stmt, rc);
  1756. result= mysql_stmt_result_metadata(stmt);
  1757. field= mysql_fetch_fields(result);
  1758. /* First test what happens if STMT_ATTR_UPDATE_MAX_LENGTH is not used */
  1759. DIE_UNLESS(field->max_length == 0);
  1760. mysql_free_result(result);
  1761. /* Enable updating of field->max_length */
  1762. true_value= 1;
  1763. mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &true_value);
  1764. rc= mysql_stmt_execute(stmt);
  1765. check_execute(stmt, rc);
  1766. rc= mysql_stmt_store_result(stmt);
  1767. check_execute(stmt, rc);
  1768. result= mysql_stmt_result_metadata(stmt);
  1769. field= mysql_fetch_fields(result);
  1770. DIE_UNLESS(field->max_length == max_blob_length);
  1771. /* Fetch results into a data buffer that is smaller than data */
  1772. bzero((char*) my_bind, sizeof(*my_bind));
  1773. my_bind[0].buffer_type= MYSQL_TYPE_BLOB;
  1774. my_bind[0].buffer= (void *) &data; /* this buffer won't be altered */
  1775. my_bind[0].buffer_length= 16;
  1776. my_bind[0].length= &blob_length;
  1777. my_bind[0].error= &my_bind[0].error_value;
  1778. rc= mysql_stmt_bind_result(stmt, my_bind);
  1779. data[16]= 0;
  1780. rc= mysql_stmt_fetch(stmt);
  1781. DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
  1782. DIE_UNLESS(my_bind[0].error_value);
  1783. DIE_UNLESS(strlen(data) == 16);
  1784. DIE_UNLESS(blob_length == max_blob_length);
  1785. /* Fetch all data */
  1786. bzero((char*) (my_bind+1), sizeof(*my_bind));
  1787. my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
  1788. my_bind[1].buffer= (void *) &data; /* this buffer won't be altered */
  1789. my_bind[1].buffer_length= sizeof(data);
  1790. my_bind[1].length= &blob_length;
  1791. bzero(data, sizeof(data));
  1792. mysql_stmt_fetch_column(stmt, my_bind+1, 0, 0);
  1793. DIE_UNLESS(strlen(data) == max_blob_length);
  1794. mysql_free_result(result);
  1795. mysql_stmt_close(stmt);
  1796. /* Drop created table */
  1797. rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
  1798. myquery(rc);
  1799. }
  1800. /* Test long data (binary) handling */
  1801. static void test_long_data_bin()
  1802. {
  1803. MYSQL_STMT *stmt;
  1804. int rc;
  1805. char data[255];
  1806. long length;
  1807. MYSQL_RES *result;
  1808. MYSQL_BIND my_bind[2];
  1809. char query[MAX_TEST_QUERY_LENGTH];
  1810. myheader("test_long_data_bin");
  1811. rc= mysql_autocommit(mysql, TRUE);
  1812. myquery(rc);
  1813. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_bin");
  1814. myquery(rc);
  1815. rc= mysql_query(mysql, "CREATE TABLE test_long_data_bin(id int, longbin long varbinary)");
  1816. myquery(rc);
  1817. strmov(query, "INSERT INTO test_long_data_bin VALUES(?, ?)");
  1818. stmt= mysql_simple_prepare(mysql, query);
  1819. check_stmt(stmt);
  1820. verify_param_count(stmt, 2);
  1821. /* Always bzero all members of bind parameter */
  1822. bzero((char*) my_bind, sizeof(my_bind));
  1823. my_bind[0].buffer= (void *)&length;
  1824. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  1825. length= 0;
  1826. my_bind[1].buffer= data; /* string data */
  1827. my_bind[1].buffer_type= MYSQL_TYPE_LONG_BLOB;
  1828. rc= mysql_stmt_bind_param(stmt, my_bind);
  1829. check_execute(stmt, rc);
  1830. length= 10;
  1831. strmov(data, "MySQL AB");
  1832. /* supply data in pieces */
  1833. {
  1834. int i;
  1835. for (i= 0; i < 100; i++)
  1836. {
  1837. rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 4);
  1838. check_execute(stmt, rc);
  1839. }
  1840. }
  1841. /* execute */
  1842. rc= mysql_stmt_execute(stmt);
  1843. if (!opt_silent)
  1844. fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
  1845. check_execute(stmt, rc);
  1846. mysql_stmt_close(stmt);
  1847. rc= mysql_commit(mysql);
  1848. myquery(rc);
  1849. /* now fetch the results ..*/
  1850. rc= mysql_query(mysql, "SELECT LENGTH(longbin), longbin FROM test_long_data_bin");
  1851. myquery(rc);
  1852. /* get the result */
  1853. result= mysql_store_result(mysql);
  1854. mytest(result);
  1855. rc= my_process_result_set(result);
  1856. DIE_UNLESS(rc == 1);
  1857. mysql_free_result(result);
  1858. }
  1859. /* Test simple delete */
  1860. static void test_simple_delete()
  1861. {
  1862. MYSQL_STMT *stmt;
  1863. int rc;
  1864. char szData[30]= {0};
  1865. int nData= 1;
  1866. MYSQL_RES *result;
  1867. MYSQL_BIND my_bind[2];
  1868. ulong length[2];
  1869. char query[MAX_TEST_QUERY_LENGTH];
  1870. myheader("test_simple_delete");
  1871. rc= mysql_autocommit(mysql, TRUE);
  1872. myquery(rc);
  1873. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_simple_delete");
  1874. myquery(rc);
  1875. rc= mysql_query(mysql, "CREATE TABLE test_simple_delete(col1 int, \
  1876. col2 varchar(50), col3 int )");
  1877. myquery(rc);
  1878. rc= mysql_query(mysql, "INSERT INTO test_simple_delete VALUES(1, 'MySQL', 100)");
  1879. myquery(rc);
  1880. verify_affected_rows(1);
  1881. rc= mysql_commit(mysql);
  1882. myquery(rc);
  1883. /* insert by prepare */
  1884. strmov(query, "DELETE FROM test_simple_delete WHERE col1= ? AND "
  1885. "CONVERT(col2 USING utf8)= ? AND col3= 100");
  1886. stmt= mysql_simple_prepare(mysql, query);
  1887. check_stmt(stmt);
  1888. verify_param_count(stmt, 2);
  1889. /* Always bzero all members of bind parameter */
  1890. bzero((char*) my_bind, sizeof(my_bind));
  1891. nData= 1;
  1892. strmov(szData, "MySQL");
  1893. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  1894. my_bind[1].buffer= szData; /* string data */
  1895. my_bind[1].buffer_length= sizeof(szData);
  1896. my_bind[1].length= &length[1];
  1897. length[1]= 5;
  1898. my_bind[0].buffer= (void *)&nData;
  1899. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  1900. rc= mysql_stmt_bind_param(stmt, my_bind);
  1901. check_execute(stmt, rc);
  1902. rc= mysql_stmt_execute(stmt);
  1903. check_execute(stmt, rc);
  1904. verify_affected_rows(1);
  1905. mysql_stmt_close(stmt);
  1906. /* now fetch the results ..*/
  1907. rc= mysql_commit(mysql);
  1908. myquery(rc);
  1909. /* test the results now, only one row should exist */
  1910. rc= mysql_query(mysql, "SELECT * FROM test_simple_delete");
  1911. myquery(rc);
  1912. /* get the result */
  1913. result= mysql_store_result(mysql);
  1914. mytest(result);
  1915. rc= my_process_result_set(result);
  1916. DIE_UNLESS(rc == 0);
  1917. mysql_free_result(result);
  1918. }
  1919. /* Test simple update */
  1920. static void test_update()
  1921. {
  1922. MYSQL_STMT *stmt;
  1923. int rc;
  1924. char szData[25];
  1925. int nData= 1;
  1926. MYSQL_RES *result;
  1927. MYSQL_BIND my_bind[2];
  1928. ulong length[2];
  1929. char query[MAX_TEST_QUERY_LENGTH];
  1930. myheader("test_update");
  1931. rc= mysql_autocommit(mysql, TRUE);
  1932. myquery(rc);
  1933. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
  1934. myquery(rc);
  1935. rc= mysql_query(mysql, "CREATE TABLE test_update("
  1936. "col1 int primary key auto_increment, "
  1937. "col2 varchar(50), col3 int )");
  1938. myquery(rc);
  1939. strmov(query, "INSERT INTO test_update(col2, col3) VALUES(?, ?)");
  1940. stmt= mysql_simple_prepare(mysql, query);
  1941. check_stmt(stmt);
  1942. verify_param_count(stmt, 2);
  1943. /* Always bzero all members of bind parameter */
  1944. bzero((char*) my_bind, sizeof(my_bind));
  1945. /* string data */
  1946. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  1947. my_bind[0].buffer= szData;
  1948. my_bind[0].buffer_length= sizeof(szData);
  1949. my_bind[0].length= &length[0];
  1950. length[0]= sprintf(szData, "inserted-data");
  1951. my_bind[1].buffer= (void *)&nData;
  1952. my_bind[1].buffer_type= MYSQL_TYPE_LONG;
  1953. rc= mysql_stmt_bind_param(stmt, my_bind);
  1954. check_execute(stmt, rc);
  1955. nData= 100;
  1956. rc= mysql_stmt_execute(stmt);
  1957. check_execute(stmt, rc);
  1958. verify_affected_rows(1);
  1959. mysql_stmt_close(stmt);
  1960. strmov(query, "UPDATE test_update SET col2= ? WHERE col3= ?");
  1961. stmt= mysql_simple_prepare(mysql, query);
  1962. check_stmt(stmt);
  1963. verify_param_count(stmt, 2);
  1964. nData= 100;
  1965. /* Always bzero all members of bind parameter */
  1966. bzero((char*) my_bind, sizeof(my_bind));
  1967. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  1968. my_bind[0].buffer= szData;
  1969. my_bind[0].buffer_length= sizeof(szData);
  1970. my_bind[0].length= &length[0];
  1971. length[0]= sprintf(szData, "updated-data");
  1972. my_bind[1].buffer= (void *)&nData;
  1973. my_bind[1].buffer_type= MYSQL_TYPE_LONG;
  1974. rc= mysql_stmt_bind_param(stmt, my_bind);
  1975. check_execute(stmt, rc);
  1976. rc= mysql_stmt_execute(stmt);
  1977. check_execute(stmt, rc);
  1978. verify_affected_rows(1);
  1979. mysql_stmt_close(stmt);
  1980. /* now fetch the results ..*/
  1981. rc= mysql_commit(mysql);
  1982. myquery(rc);
  1983. /* test the results now, only one row should exist */
  1984. rc= mysql_query(mysql, "SELECT * FROM test_update");
  1985. myquery(rc);
  1986. /* get the result */
  1987. result= mysql_store_result(mysql);
  1988. mytest(result);
  1989. rc= my_process_result_set(result);
  1990. DIE_UNLESS(rc == 1);
  1991. mysql_free_result(result);
  1992. }
  1993. /* Test prepare without parameters */
  1994. static void test_prepare_noparam()
  1995. {
  1996. MYSQL_STMT *stmt;
  1997. int rc;
  1998. MYSQL_RES *result;
  1999. char query[MAX_TEST_QUERY_LENGTH];
  2000. myheader("test_prepare_noparam");
  2001. rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
  2002. myquery(rc);
  2003. rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 int, col2 varchar(50))");
  2004. myquery(rc);
  2005. /* insert by prepare */
  2006. strmov(query, "INSERT INTO my_prepare VALUES(10, 'venu')");
  2007. stmt= mysql_simple_prepare(mysql, query);
  2008. check_stmt(stmt);
  2009. verify_param_count(stmt, 0);
  2010. rc= mysql_stmt_execute(stmt);
  2011. check_execute(stmt, rc);
  2012. mysql_stmt_close(stmt);
  2013. /* now fetch the results ..*/
  2014. rc= mysql_commit(mysql);
  2015. myquery(rc);
  2016. /* test the results now, only one row should exist */
  2017. rc= mysql_query(mysql, "SELECT * FROM my_prepare");
  2018. myquery(rc);
  2019. /* get the result */
  2020. result= mysql_store_result(mysql);
  2021. mytest(result);
  2022. rc= my_process_result_set(result);
  2023. DIE_UNLESS(rc == 1);
  2024. mysql_free_result(result);
  2025. }
  2026. /* Test simple bind result */
  2027. static void test_bind_result()
  2028. {
  2029. MYSQL_STMT *stmt;
  2030. int rc;
  2031. int nData;
  2032. ulong length1;
  2033. char szData[100];
  2034. MYSQL_BIND my_bind[2];
  2035. my_bool is_null[2];
  2036. myheader("test_bind_result");
  2037. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
  2038. myquery(rc);
  2039. rc= mysql_query(mysql, "CREATE TABLE test_bind_result(col1 int , col2 varchar(50))");
  2040. myquery(rc);
  2041. rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(10, 'venu')");
  2042. myquery(rc);
  2043. rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(20, 'MySQL')");
  2044. myquery(rc);
  2045. rc= mysql_query(mysql, "INSERT INTO test_bind_result(col2) VALUES('monty')");
  2046. myquery(rc);
  2047. rc= mysql_commit(mysql);
  2048. myquery(rc);
  2049. /* fetch */
  2050. bzero((char*) my_bind, sizeof(my_bind));
  2051. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  2052. my_bind[0].buffer= (void *) &nData; /* integer data */
  2053. my_bind[0].is_null= &is_null[0];
  2054. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  2055. my_bind[1].buffer= szData; /* string data */
  2056. my_bind[1].buffer_length= sizeof(szData);
  2057. my_bind[1].length= &length1;
  2058. my_bind[1].is_null= &is_null[1];
  2059. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_result");
  2060. check_stmt(stmt);
  2061. rc= mysql_stmt_bind_result(stmt, my_bind);
  2062. check_execute(stmt, rc);
  2063. rc= mysql_stmt_execute(stmt);
  2064. check_execute(stmt, rc);
  2065. rc= mysql_stmt_fetch(stmt);
  2066. check_execute(stmt, rc);
  2067. if (!opt_silent)
  2068. fprintf(stdout, "\n row 1: %d, %s(%lu)", nData, szData, length1);
  2069. DIE_UNLESS(nData == 10);
  2070. DIE_UNLESS(strcmp(szData, "venu") == 0);
  2071. DIE_UNLESS(length1 == 4);
  2072. rc= mysql_stmt_fetch(stmt);
  2073. check_execute(stmt, rc);
  2074. if (!opt_silent)
  2075. fprintf(stdout, "\n row 2: %d, %s(%lu)", nData, szData, length1);
  2076. DIE_UNLESS(nData == 20);
  2077. DIE_UNLESS(strcmp(szData, "MySQL") == 0);
  2078. DIE_UNLESS(length1 == 5);
  2079. rc= mysql_stmt_fetch(stmt);
  2080. check_execute(stmt, rc);
  2081. if (!opt_silent && is_null[0])
  2082. fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
  2083. DIE_UNLESS(is_null[0]);
  2084. DIE_UNLESS(strcmp(szData, "monty") == 0);
  2085. DIE_UNLESS(length1 == 5);
  2086. rc= mysql_stmt_fetch(stmt);
  2087. DIE_UNLESS(rc == MYSQL_NO_DATA);
  2088. mysql_stmt_close(stmt);
  2089. }
  2090. /* Test ext bind result */
  2091. static void test_bind_result_ext()
  2092. {
  2093. MYSQL_STMT *stmt;
  2094. int rc, i;
  2095. uchar t_data;
  2096. short s_data;
  2097. int i_data;
  2098. longlong b_data;
  2099. float f_data;
  2100. double d_data;
  2101. char szData[20], bData[20];
  2102. ulong szLength, bLength;
  2103. MYSQL_BIND my_bind[8];
  2104. ulong length[8];
  2105. my_bool is_null[8];
  2106. char llbuf[22];
  2107. myheader("test_bind_result_ext");
  2108. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
  2109. myquery(rc);
  2110. rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, "
  2111. " c2 smallint, "
  2112. " c3 int, c4 bigint, "
  2113. " c5 float, c6 double, "
  2114. " c7 varbinary(10), "
  2115. " c8 varchar(50))");
  2116. myquery(rc);
  2117. rc= mysql_query(mysql, "INSERT INTO test_bind_result "
  2118. "VALUES (19, 2999, 3999, 4999999, "
  2119. " 2345.6, 5678.89563, 'venu', 'mysql')");
  2120. myquery(rc);
  2121. rc= mysql_commit(mysql);
  2122. myquery(rc);
  2123. bzero((char*) my_bind, sizeof(my_bind));
  2124. for (i= 0; i < (int) array_elements(my_bind); i++)
  2125. {
  2126. my_bind[i].length= &length[i];
  2127. my_bind[i].is_null= &is_null[i];
  2128. }
  2129. my_bind[0].buffer_type= MYSQL_TYPE_TINY;
  2130. my_bind[0].buffer= (void *)&t_data;
  2131. my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
  2132. my_bind[2].buffer_type= MYSQL_TYPE_LONG;
  2133. my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
  2134. my_bind[1].buffer= (void *)&s_data;
  2135. my_bind[2].buffer= (void *)&i_data;
  2136. my_bind[3].buffer= (void *)&b_data;
  2137. my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
  2138. my_bind[4].buffer= (void *)&f_data;
  2139. my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
  2140. my_bind[5].buffer= (void *)&d_data;
  2141. my_bind[6].buffer_type= MYSQL_TYPE_STRING;
  2142. my_bind[6].buffer= (void *)szData;
  2143. my_bind[6].buffer_length= sizeof(szData);
  2144. my_bind[6].length= &szLength;
  2145. my_bind[7].buffer_type= MYSQL_TYPE_TINY_BLOB;
  2146. my_bind[7].buffer= (void *)&bData;
  2147. my_bind[7].length= &bLength;
  2148. my_bind[7].buffer_length= sizeof(bData);
  2149. stmt= mysql_simple_prepare(mysql, "select * from test_bind_result");
  2150. check_stmt(stmt);
  2151. rc= mysql_stmt_bind_result(stmt, my_bind);
  2152. check_execute(stmt, rc);
  2153. rc= mysql_stmt_execute(stmt);
  2154. check_execute(stmt, rc);
  2155. rc= mysql_stmt_fetch(stmt);
  2156. check_execute(stmt, rc);
  2157. if (!opt_silent)
  2158. {
  2159. fprintf(stdout, "\n data (tiny) : %d", t_data);
  2160. fprintf(stdout, "\n data (short) : %d", s_data);
  2161. fprintf(stdout, "\n data (int) : %d", i_data);
  2162. fprintf(stdout, "\n data (big) : %s", llstr(b_data, llbuf));
  2163. fprintf(stdout, "\n data (float) : %f", f_data);
  2164. fprintf(stdout, "\n data (double) : %f", d_data);
  2165. fprintf(stdout, "\n data (str) : %s(%lu)", szData, szLength);
  2166. bData[bLength]= '\0'; /* bData is binary */
  2167. fprintf(stdout, "\n data (bin) : %s(%lu)", bData, bLength);
  2168. }
  2169. DIE_UNLESS(t_data == 19);
  2170. DIE_UNLESS(s_data == 2999);
  2171. DIE_UNLESS(i_data == 3999);
  2172. DIE_UNLESS(b_data == 4999999);
  2173. /*DIE_UNLESS(f_data == 2345.60);*/
  2174. /*DIE_UNLESS(d_data == 5678.89563);*/
  2175. DIE_UNLESS(strcmp(szData, "venu") == 0);
  2176. DIE_UNLESS(strncmp(bData, "mysql", 5) == 0);
  2177. DIE_UNLESS(szLength == 4);
  2178. DIE_UNLESS(bLength == 5);
  2179. rc= mysql_stmt_fetch(stmt);
  2180. DIE_UNLESS(rc == MYSQL_NO_DATA);
  2181. mysql_stmt_close(stmt);
  2182. }
  2183. /* Test ext bind result */
  2184. static void test_bind_result_ext1()
  2185. {
  2186. MYSQL_STMT *stmt;
  2187. uint i;
  2188. int rc;
  2189. char t_data[20];
  2190. float s_data;
  2191. short i_data;
  2192. uchar b_data;
  2193. int f_data;
  2194. long bData;
  2195. char d_data[20];
  2196. double szData;
  2197. MYSQL_BIND my_bind[8];
  2198. ulong length[8];
  2199. my_bool is_null[8];
  2200. myheader("test_bind_result_ext1");
  2201. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
  2202. myquery(rc);
  2203. rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, c2 smallint, \
  2204. c3 int, c4 bigint, \
  2205. c5 float, c6 double, \
  2206. c7 varbinary(10), \
  2207. c8 varchar(10))");
  2208. myquery(rc);
  2209. rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(120, 2999, 3999, 54, \
  2210. 2.6, 58.89, \
  2211. '206', '6.7')");
  2212. myquery(rc);
  2213. rc= mysql_commit(mysql);
  2214. myquery(rc);
  2215. bzero((char*) my_bind, sizeof(my_bind));
  2216. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  2217. my_bind[0].buffer= (void *) t_data;
  2218. my_bind[0].buffer_length= sizeof(t_data);
  2219. my_bind[0].error= &my_bind[0].error_value;
  2220. my_bind[1].buffer_type= MYSQL_TYPE_FLOAT;
  2221. my_bind[1].buffer= (void *)&s_data;
  2222. my_bind[1].buffer_length= 0;
  2223. my_bind[1].error= &my_bind[1].error_value;
  2224. my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
  2225. my_bind[2].buffer= (void *)&i_data;
  2226. my_bind[2].buffer_length= 0;
  2227. my_bind[2].error= &my_bind[2].error_value;
  2228. my_bind[3].buffer_type= MYSQL_TYPE_TINY;
  2229. my_bind[3].buffer= (void *)&b_data;
  2230. my_bind[3].buffer_length= 0;
  2231. my_bind[3].error= &my_bind[3].error_value;
  2232. my_bind[4].buffer_type= MYSQL_TYPE_LONG;
  2233. my_bind[4].buffer= (void *)&f_data;
  2234. my_bind[4].buffer_length= 0;
  2235. my_bind[4].error= &my_bind[4].error_value;
  2236. my_bind[5].buffer_type= MYSQL_TYPE_STRING;
  2237. my_bind[5].buffer= (void *)d_data;
  2238. my_bind[5].buffer_length= sizeof(d_data);
  2239. my_bind[5].error= &my_bind[5].error_value;
  2240. my_bind[6].buffer_type= MYSQL_TYPE_LONG;
  2241. my_bind[6].buffer= (void *)&bData;
  2242. my_bind[6].buffer_length= 0;
  2243. my_bind[6].error= &my_bind[6].error_value;
  2244. my_bind[7].buffer_type= MYSQL_TYPE_DOUBLE;
  2245. my_bind[7].buffer= (void *)&szData;
  2246. my_bind[7].buffer_length= 0;
  2247. my_bind[7].error= &my_bind[7].error_value;
  2248. for (i= 0; i < array_elements(my_bind); i++)
  2249. {
  2250. my_bind[i].is_null= &is_null[i];
  2251. my_bind[i].length= &length[i];
  2252. }
  2253. stmt= mysql_simple_prepare(mysql, "select * from test_bind_result");
  2254. check_stmt(stmt);
  2255. rc= mysql_stmt_bind_result(stmt, my_bind);
  2256. check_execute(stmt, rc);
  2257. rc= mysql_stmt_execute(stmt);
  2258. check_execute(stmt, rc);
  2259. rc= mysql_stmt_fetch(stmt);
  2260. printf("rc=%d\n", rc);
  2261. DIE_UNLESS(rc == 0);
  2262. if (!opt_silent)
  2263. {
  2264. fprintf(stdout, "\n data (tiny) : %s(%lu)", t_data, length[0]);
  2265. fprintf(stdout, "\n data (short) : %f(%lu)", s_data, length[1]);
  2266. fprintf(stdout, "\n data (int) : %d(%lu)", i_data, length[2]);
  2267. fprintf(stdout, "\n data (big) : %d(%lu)", b_data, length[3]);
  2268. fprintf(stdout, "\n data (float) : %d(%lu)", f_data, length[4]);
  2269. fprintf(stdout, "\n data (double) : %s(%lu)", d_data, length[5]);
  2270. fprintf(stdout, "\n data (bin) : %ld(%lu)", bData, length[6]);
  2271. fprintf(stdout, "\n data (str) : %g(%lu)", szData, length[7]);
  2272. }
  2273. DIE_UNLESS(strcmp(t_data, "120") == 0);
  2274. DIE_UNLESS(i_data == 3999);
  2275. DIE_UNLESS(f_data == 2);
  2276. DIE_UNLESS(strcmp(d_data, "58.89") == 0);
  2277. DIE_UNLESS(b_data == 54);
  2278. DIE_UNLESS(length[0] == 3);
  2279. DIE_UNLESS(length[1] == 4);
  2280. DIE_UNLESS(length[2] == 2);
  2281. DIE_UNLESS(length[3] == 1);
  2282. DIE_UNLESS(length[4] == 4);
  2283. DIE_UNLESS(length[5] == 5);
  2284. DIE_UNLESS(length[6] == 4);
  2285. DIE_UNLESS(length[7] == 8);
  2286. rc= mysql_stmt_fetch(stmt);
  2287. DIE_UNLESS(rc == MYSQL_NO_DATA);
  2288. mysql_stmt_close(stmt);
  2289. }
  2290. /* Generalized fetch conversion routine for all basic types */
  2291. static void bind_fetch(int row_count)
  2292. {
  2293. MYSQL_STMT *stmt;
  2294. int rc, i, count= row_count;
  2295. int32 data[10];
  2296. int8 i8_data;
  2297. int16 i16_data;
  2298. int32 i32_data;
  2299. longlong i64_data;
  2300. float f_data;
  2301. double d_data;
  2302. char s_data[10];
  2303. ulong length[10];
  2304. MYSQL_BIND my_bind[7];
  2305. my_bool is_null[7];
  2306. stmt= mysql_simple_prepare(mysql, "INSERT INTO test_bind_fetch VALUES "
  2307. "(?, ?, ?, ?, ?, ?, ?)");
  2308. check_stmt(stmt);
  2309. verify_param_count(stmt, 7);
  2310. /* Always bzero all members of bind parameter */
  2311. bzero((char*) my_bind, sizeof(my_bind));
  2312. for (i= 0; i < (int) array_elements(my_bind); i++)
  2313. {
  2314. my_bind[i].buffer_type= MYSQL_TYPE_LONG;
  2315. my_bind[i].buffer= (void *) &data[i];
  2316. }
  2317. rc= mysql_stmt_bind_param(stmt, my_bind);
  2318. check_execute(stmt, rc);
  2319. while (count--)
  2320. {
  2321. rc= 10+count;
  2322. for (i= 0; i < (int) array_elements(my_bind); i++)
  2323. {
  2324. data[i]= rc+i;
  2325. rc+= 12;
  2326. }
  2327. rc= mysql_stmt_execute(stmt);
  2328. check_execute(stmt, rc);
  2329. }
  2330. rc= mysql_commit(mysql);
  2331. myquery(rc);
  2332. mysql_stmt_close(stmt);
  2333. rc= my_stmt_result("SELECT * FROM test_bind_fetch");
  2334. DIE_UNLESS(row_count == rc);
  2335. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_fetch");
  2336. check_stmt(stmt);
  2337. for (i= 0; i < (int) array_elements(my_bind); i++)
  2338. {
  2339. my_bind[i].buffer= (void *) &data[i];
  2340. my_bind[i].length= &length[i];
  2341. my_bind[i].is_null= &is_null[i];
  2342. }
  2343. my_bind[0].buffer_type= MYSQL_TYPE_TINY;
  2344. my_bind[0].buffer= (void *)&i8_data;
  2345. my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
  2346. my_bind[1].buffer= (void *)&i16_data;
  2347. my_bind[2].buffer_type= MYSQL_TYPE_LONG;
  2348. my_bind[2].buffer= (void *)&i32_data;
  2349. my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
  2350. my_bind[3].buffer= (void *)&i64_data;
  2351. my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
  2352. my_bind[4].buffer= (void *)&f_data;
  2353. my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
  2354. my_bind[5].buffer= (void *)&d_data;
  2355. my_bind[6].buffer_type= MYSQL_TYPE_STRING;
  2356. my_bind[6].buffer= (void *)&s_data;
  2357. my_bind[6].buffer_length= sizeof(s_data);
  2358. rc= mysql_stmt_bind_result(stmt, my_bind);
  2359. check_execute(stmt, rc);
  2360. rc= mysql_stmt_execute(stmt);
  2361. check_execute(stmt, rc);
  2362. rc= mysql_stmt_store_result(stmt);
  2363. check_execute(stmt, rc);
  2364. while (row_count--)
  2365. {
  2366. rc= mysql_stmt_fetch(stmt);
  2367. check_execute(stmt, rc);
  2368. if (!opt_silent)
  2369. {
  2370. fprintf(stdout, "\n");
  2371. fprintf(stdout, "\n tiny : %ld(%lu)", (ulong) i8_data, length[0]);
  2372. fprintf(stdout, "\n short : %ld(%lu)", (ulong) i16_data, length[1]);
  2373. fprintf(stdout, "\n int : %ld(%lu)", (ulong) i32_data, length[2]);
  2374. fprintf(stdout, "\n longlong : %ld(%lu)", (ulong) i64_data, length[3]);
  2375. fprintf(stdout, "\n float : %f(%lu)", f_data, length[4]);
  2376. fprintf(stdout, "\n double : %g(%lu)", d_data, length[5]);
  2377. fprintf(stdout, "\n char : %s(%lu)", s_data, length[6]);
  2378. }
  2379. rc= 10+row_count;
  2380. /* TINY */
  2381. DIE_UNLESS((int) i8_data == rc);
  2382. DIE_UNLESS(length[0] == 1);
  2383. rc+= 13;
  2384. /* SHORT */
  2385. DIE_UNLESS((int) i16_data == rc);
  2386. DIE_UNLESS(length[1] == 2);
  2387. rc+= 13;
  2388. /* LONG */
  2389. DIE_UNLESS((int) i32_data == rc);
  2390. DIE_UNLESS(length[2] == 4);
  2391. rc+= 13;
  2392. /* LONGLONG */
  2393. DIE_UNLESS((int) i64_data == rc);
  2394. DIE_UNLESS(length[3] == 8);
  2395. rc+= 13;
  2396. /* FLOAT */
  2397. DIE_UNLESS((int)f_data == rc);
  2398. DIE_UNLESS(length[4] == 4);
  2399. rc+= 13;
  2400. /* DOUBLE */
  2401. DIE_UNLESS((int)d_data == rc);
  2402. DIE_UNLESS(length[5] == 8);
  2403. rc+= 13;
  2404. /* CHAR */
  2405. {
  2406. char buff[20];
  2407. long len= sprintf(buff, "%d", rc);
  2408. DIE_UNLESS(strcmp(s_data, buff) == 0);
  2409. DIE_UNLESS(length[6] == (ulong) len);
  2410. }
  2411. }
  2412. rc= mysql_stmt_fetch(stmt);
  2413. DIE_UNLESS(rc == MYSQL_NO_DATA);
  2414. mysql_stmt_close(stmt);
  2415. }
  2416. /* Test fetching of date, time and ts */
  2417. static void test_fetch_date()
  2418. {
  2419. MYSQL_STMT *stmt;
  2420. uint i;
  2421. int rc, year;
  2422. char date[25], my_time[25], ts[25], ts_4[25], ts_6[20], dt[20];
  2423. ulong d_length, t_length, ts_length, ts4_length, ts6_length,
  2424. dt_length, y_length;
  2425. MYSQL_BIND my_bind[8];
  2426. my_bool is_null[8];
  2427. ulong length[8];
  2428. myheader("test_fetch_date");
  2429. /* Will not work if sql_mode is NO_ZERO_DATE (implicit if TRADITIONAL) */
  2430. rc= mysql_query(mysql, "SET SQL_MODE=''");
  2431. myquery(rc);
  2432. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
  2433. myquery(rc);
  2434. rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 date, c2 time, \
  2435. c3 timestamp(14), \
  2436. c4 year, \
  2437. c5 datetime, \
  2438. c6 timestamp(4), \
  2439. c7 timestamp(6))");
  2440. myquery(rc);
  2441. rc= mysql_query(mysql, "SET SQL_MODE=''");
  2442. rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES('2002-01-02', \
  2443. '12:49:00', \
  2444. '2002-01-02 17:46:59', \
  2445. 2010, \
  2446. '2010-07-10', \
  2447. '2020', '1999-12-29')");
  2448. myquery(rc);
  2449. rc= mysql_commit(mysql);
  2450. myquery(rc);
  2451. bzero((char*) my_bind, sizeof(my_bind));
  2452. for (i= 0; i < array_elements(my_bind); i++)
  2453. {
  2454. my_bind[i].is_null= &is_null[i];
  2455. my_bind[i].length= &length[i];
  2456. }
  2457. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  2458. my_bind[1]= my_bind[2]= my_bind[0];
  2459. my_bind[0].buffer= (void *)&date;
  2460. my_bind[0].buffer_length= sizeof(date);
  2461. my_bind[0].length= &d_length;
  2462. my_bind[1].buffer= (void *)&my_time;
  2463. my_bind[1].buffer_length= sizeof(my_time);
  2464. my_bind[1].length= &t_length;
  2465. my_bind[2].buffer= (void *)&ts;
  2466. my_bind[2].buffer_length= sizeof(ts);
  2467. my_bind[2].length= &ts_length;
  2468. my_bind[3].buffer_type= MYSQL_TYPE_LONG;
  2469. my_bind[3].buffer= (void *)&year;
  2470. my_bind[3].length= &y_length;
  2471. my_bind[4].buffer_type= MYSQL_TYPE_STRING;
  2472. my_bind[4].buffer= (void *)&dt;
  2473. my_bind[4].buffer_length= sizeof(dt);
  2474. my_bind[4].length= &dt_length;
  2475. my_bind[5].buffer_type= MYSQL_TYPE_STRING;
  2476. my_bind[5].buffer= (void *)&ts_4;
  2477. my_bind[5].buffer_length= sizeof(ts_4);
  2478. my_bind[5].length= &ts4_length;
  2479. my_bind[6].buffer_type= MYSQL_TYPE_STRING;
  2480. my_bind[6].buffer= (void *)&ts_6;
  2481. my_bind[6].buffer_length= sizeof(ts_6);
  2482. my_bind[6].length= &ts6_length;
  2483. rc= my_stmt_result("SELECT * FROM test_bind_result");
  2484. DIE_UNLESS(rc == 1);
  2485. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_result");
  2486. check_stmt(stmt);
  2487. rc= mysql_stmt_bind_result(stmt, my_bind);
  2488. check_execute(stmt, rc);
  2489. rc= mysql_stmt_execute(stmt);
  2490. check_execute(stmt, rc);
  2491. ts_4[0]= '\0';
  2492. rc= mysql_stmt_fetch(stmt);
  2493. check_execute(stmt, rc);
  2494. if (!opt_silent)
  2495. {
  2496. fprintf(stdout, "\n date : %s(%lu)", date, d_length);
  2497. fprintf(stdout, "\n time : %s(%lu)", my_time, t_length);
  2498. fprintf(stdout, "\n ts : %s(%lu)", ts, ts_length);
  2499. fprintf(stdout, "\n year : %d(%lu)", year, y_length);
  2500. fprintf(stdout, "\n dt : %s(%lu)", dt, dt_length);
  2501. fprintf(stdout, "\n ts(4) : %s(%lu)", ts_4, ts4_length);
  2502. fprintf(stdout, "\n ts(6) : %s(%lu)", ts_6, ts6_length);
  2503. }
  2504. DIE_UNLESS(strcmp(date, "2002-01-02") == 0);
  2505. DIE_UNLESS(d_length == 10);
  2506. DIE_UNLESS(strcmp(my_time, "12:49:00") == 0);
  2507. DIE_UNLESS(t_length == 8);
  2508. DIE_UNLESS(strcmp(ts, "2002-01-02 17:46:59") == 0);
  2509. DIE_UNLESS(ts_length == 19);
  2510. DIE_UNLESS(year == 2010);
  2511. DIE_UNLESS(y_length == 4);
  2512. DIE_UNLESS(strcmp(dt, "2010-07-10 00:00:00") == 0);
  2513. DIE_UNLESS(dt_length == 19);
  2514. DIE_UNLESS(strcmp(ts_4, "0000-00-00 00:00:00") == 0);
  2515. DIE_UNLESS(ts4_length == strlen("0000-00-00 00:00:00"));
  2516. DIE_UNLESS(strcmp(ts_6, "1999-12-29 00:00:00") == 0);
  2517. DIE_UNLESS(ts6_length == 19);
  2518. rc= mysql_stmt_fetch(stmt);
  2519. DIE_UNLESS(rc == MYSQL_NO_DATA);
  2520. mysql_stmt_close(stmt);
  2521. }
  2522. /* Test fetching of str to all types */
  2523. static void test_fetch_str()
  2524. {
  2525. int rc;
  2526. myheader("test_fetch_str");
  2527. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
  2528. myquery(rc);
  2529. rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 char(10), \
  2530. c2 char(10), \
  2531. c3 char(20), \
  2532. c4 char(20), \
  2533. c5 char(30), \
  2534. c6 char(40), \
  2535. c7 char(20))");
  2536. myquery(rc);
  2537. bind_fetch(3);
  2538. }
  2539. /* Test fetching of long to all types */
  2540. static void test_fetch_long()
  2541. {
  2542. int rc;
  2543. myheader("test_fetch_long");
  2544. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
  2545. myquery(rc);
  2546. rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 int unsigned, \
  2547. c2 int unsigned, \
  2548. c3 int, \
  2549. c4 int, \
  2550. c5 int, \
  2551. c6 int unsigned, \
  2552. c7 int)");
  2553. myquery(rc);
  2554. bind_fetch(4);
  2555. }
  2556. /* Test fetching of short to all types */
  2557. static void test_fetch_short()
  2558. {
  2559. int rc;
  2560. myheader("test_fetch_short");
  2561. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
  2562. myquery(rc);
  2563. rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 smallint unsigned, \
  2564. c2 smallint, \
  2565. c3 smallint unsigned, \
  2566. c4 smallint, \
  2567. c5 smallint, \
  2568. c6 smallint, \
  2569. c7 smallint unsigned)");
  2570. myquery(rc);
  2571. bind_fetch(5);
  2572. }
  2573. /* Test fetching of tiny to all types */
  2574. static void test_fetch_tiny()
  2575. {
  2576. int rc;
  2577. myheader("test_fetch_tiny");
  2578. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
  2579. myquery(rc);
  2580. rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 tinyint unsigned, \
  2581. c2 tinyint, \
  2582. c3 tinyint unsigned, \
  2583. c4 tinyint, \
  2584. c5 tinyint, \
  2585. c6 tinyint, \
  2586. c7 tinyint unsigned)");
  2587. myquery(rc);
  2588. bind_fetch(3);
  2589. }
  2590. /* Test fetching of longlong to all types */
  2591. static void test_fetch_bigint()
  2592. {
  2593. int rc;
  2594. myheader("test_fetch_bigint");
  2595. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
  2596. myquery(rc);
  2597. rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 bigint, \
  2598. c2 bigint, \
  2599. c3 bigint unsigned, \
  2600. c4 bigint unsigned, \
  2601. c5 bigint unsigned, \
  2602. c6 bigint unsigned, \
  2603. c7 bigint unsigned)");
  2604. myquery(rc);
  2605. bind_fetch(2);
  2606. }
  2607. /* Test fetching of float to all types */
  2608. static void test_fetch_float()
  2609. {
  2610. int rc;
  2611. myheader("test_fetch_float");
  2612. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
  2613. myquery(rc);
  2614. rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 float(3), \
  2615. c2 float, \
  2616. c3 float unsigned, \
  2617. c4 float, \
  2618. c5 float, \
  2619. c6 float, \
  2620. c7 float(10) unsigned)");
  2621. myquery(rc);
  2622. bind_fetch(2);
  2623. }
  2624. /* Test fetching of double to all types */
  2625. static void test_fetch_double()
  2626. {
  2627. int rc;
  2628. myheader("test_fetch_double");
  2629. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
  2630. myquery(rc);
  2631. rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 double(5, 2), "
  2632. "c2 double unsigned, c3 double unsigned, "
  2633. "c4 double unsigned, c5 double unsigned, "
  2634. "c6 double unsigned, c7 double unsigned)");
  2635. myquery(rc);
  2636. bind_fetch(3);
  2637. }
  2638. /* Test simple prepare with all possible types */
  2639. static void test_prepare_ext()
  2640. {
  2641. MYSQL_STMT *stmt;
  2642. int rc;
  2643. char *sql;
  2644. int nData= 1;
  2645. char tData= 1;
  2646. short sData= 10;
  2647. longlong bData= 20;
  2648. MYSQL_BIND my_bind[6];
  2649. char query[MAX_TEST_QUERY_LENGTH];
  2650. myheader("test_prepare_ext");
  2651. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_ext");
  2652. myquery(rc);
  2653. sql= (char *)"CREATE TABLE test_prepare_ext"
  2654. "("
  2655. " c1 tinyint,"
  2656. " c2 smallint,"
  2657. " c3 mediumint,"
  2658. " c4 int,"
  2659. " c5 integer,"
  2660. " c6 bigint,"
  2661. " c7 float,"
  2662. " c8 double,"
  2663. " c9 double precision,"
  2664. " c10 real,"
  2665. " c11 decimal(7, 4),"
  2666. " c12 numeric(8, 4),"
  2667. " c13 date,"
  2668. " c14 datetime,"
  2669. " c15 timestamp(14),"
  2670. " c16 time,"
  2671. " c17 year,"
  2672. " c18 bit,"
  2673. " c19 bool,"
  2674. " c20 char,"
  2675. " c21 char(10),"
  2676. " c22 varchar(30),"
  2677. " c23 tinyblob,"
  2678. " c24 tinytext,"
  2679. " c25 blob,"
  2680. " c26 text,"
  2681. " c27 mediumblob,"
  2682. " c28 mediumtext,"
  2683. " c29 longblob,"
  2684. " c30 longtext,"
  2685. " c31 enum('one', 'two', 'three'),"
  2686. " c32 set('monday', 'tuesday', 'wednesday'))";
  2687. rc= mysql_query(mysql, sql);
  2688. myquery(rc);
  2689. /* insert by prepare - all integers */
  2690. strmov(query, (char *)"INSERT INTO test_prepare_ext(c1, c2, c3, c4, c5, c6) VALUES(?, ?, ?, ?, ?, ?)");
  2691. stmt= mysql_simple_prepare(mysql, query);
  2692. check_stmt(stmt);
  2693. verify_param_count(stmt, 6);
  2694. /* Always bzero all members of bind parameter */
  2695. bzero((char*) my_bind, sizeof(my_bind));
  2696. /*tinyint*/
  2697. my_bind[0].buffer_type= MYSQL_TYPE_TINY;
  2698. my_bind[0].buffer= (void *)&tData;
  2699. /*smallint*/
  2700. my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
  2701. my_bind[1].buffer= (void *)&sData;
  2702. /*mediumint*/
  2703. my_bind[2].buffer_type= MYSQL_TYPE_LONG;
  2704. my_bind[2].buffer= (void *)&nData;
  2705. /*int*/
  2706. my_bind[3].buffer_type= MYSQL_TYPE_LONG;
  2707. my_bind[3].buffer= (void *)&nData;
  2708. /*integer*/
  2709. my_bind[4].buffer_type= MYSQL_TYPE_LONG;
  2710. my_bind[4].buffer= (void *)&nData;
  2711. /*bigint*/
  2712. my_bind[5].buffer_type= MYSQL_TYPE_LONGLONG;
  2713. my_bind[5].buffer= (void *)&bData;
  2714. rc= mysql_stmt_bind_param(stmt, my_bind);
  2715. check_execute(stmt, rc);
  2716. /*
  2717. * integer to integer
  2718. */
  2719. for (nData= 0; nData<10; nData++, tData++, sData++, bData++)
  2720. {
  2721. rc= mysql_stmt_execute(stmt);
  2722. check_execute(stmt, rc);
  2723. }
  2724. mysql_stmt_close(stmt);
  2725. /* now fetch the results ..*/
  2726. stmt= mysql_simple_prepare(mysql, "SELECT c1, c2, c3, c4, c5, c6 "
  2727. "FROM test_prepare_ext");
  2728. check_stmt(stmt);
  2729. /* get the result */
  2730. rc= mysql_stmt_execute(stmt);
  2731. check_execute(stmt, rc);
  2732. rc= my_process_stmt_result(stmt);
  2733. DIE_UNLESS(nData == rc);
  2734. mysql_stmt_close(stmt);
  2735. }
  2736. /* Test real and alias names */
  2737. static void test_field_names()
  2738. {
  2739. int rc;
  2740. MYSQL_RES *result;
  2741. myheader("test_field_names");
  2742. if (!opt_silent)
  2743. fprintf(stdout, "\n %d, %d, %d", MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NEWDATE, MYSQL_TYPE_ENUM);
  2744. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names1");
  2745. myquery(rc);
  2746. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names2");
  2747. myquery(rc);
  2748. rc= mysql_query(mysql, "CREATE TABLE test_field_names1(id int, name varchar(50))");
  2749. myquery(rc);
  2750. rc= mysql_query(mysql, "CREATE TABLE test_field_names2(id int, name varchar(50))");
  2751. myquery(rc);
  2752. /* with table name included with TRUE column name */
  2753. rc= mysql_query(mysql, "SELECT id as 'id-alias' FROM test_field_names1");
  2754. myquery(rc);
  2755. result= mysql_use_result(mysql);
  2756. mytest(result);
  2757. rc= my_process_result_set(result);
  2758. DIE_UNLESS(rc == 0);
  2759. mysql_free_result(result);
  2760. /* with table name included with TRUE column name */
  2761. rc= mysql_query(mysql, "SELECT t1.id as 'id-alias', test_field_names2.name FROM test_field_names1 t1, test_field_names2");
  2762. myquery(rc);
  2763. result= mysql_use_result(mysql);
  2764. mytest(result);
  2765. rc= my_process_result_set(result);
  2766. DIE_UNLESS(rc == 0);
  2767. mysql_free_result(result);
  2768. }
  2769. /* Test warnings */
  2770. static void test_warnings()
  2771. {
  2772. int rc;
  2773. MYSQL_RES *result;
  2774. myheader("test_warnings");
  2775. mysql_query(mysql, "DROP TABLE if exists test_non_exists");
  2776. rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
  2777. myquery(rc);
  2778. if (!opt_silent)
  2779. fprintf(stdout, "\n total warnings: %d", mysql_warning_count(mysql));
  2780. rc= mysql_query(mysql, "SHOW WARNINGS");
  2781. myquery(rc);
  2782. result= mysql_store_result(mysql);
  2783. mytest(result);
  2784. rc= my_process_result_set(result);
  2785. DIE_UNLESS(rc == 1);
  2786. mysql_free_result(result);
  2787. }
  2788. /* Test errors */
  2789. static void test_errors()
  2790. {
  2791. int rc;
  2792. MYSQL_RES *result;
  2793. myheader("test_errors");
  2794. mysql_query(mysql, "DROP TABLE if exists test_non_exists");
  2795. rc= mysql_query(mysql, "DROP TABLE test_non_exists");
  2796. myquery_r(rc);
  2797. rc= mysql_query(mysql, "SHOW ERRORS");
  2798. myquery(rc);
  2799. result= mysql_store_result(mysql);
  2800. mytest(result);
  2801. (void) my_process_result_set(result);
  2802. mysql_free_result(result);
  2803. }
  2804. /* Test simple prepare-insert */
  2805. static void test_insert()
  2806. {
  2807. MYSQL_STMT *stmt;
  2808. int rc;
  2809. char str_data[50];
  2810. char tiny_data;
  2811. MYSQL_RES *result;
  2812. MYSQL_BIND my_bind[2];
  2813. ulong length;
  2814. myheader("test_insert");
  2815. rc= mysql_autocommit(mysql, TRUE);
  2816. myquery(rc);
  2817. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
  2818. myquery(rc);
  2819. rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
  2820. col2 varchar(50))");
  2821. myquery(rc);
  2822. /* insert by prepare */
  2823. stmt= mysql_simple_prepare(mysql,
  2824. "INSERT INTO test_prep_insert VALUES(?, ?)");
  2825. check_stmt(stmt);
  2826. verify_param_count(stmt, 2);
  2827. /*
  2828. We need to bzero bind structure because mysql_stmt_bind_param checks all
  2829. its members.
  2830. */
  2831. bzero((char*) my_bind, sizeof(my_bind));
  2832. /* tinyint */
  2833. my_bind[0].buffer_type= MYSQL_TYPE_TINY;
  2834. my_bind[0].buffer= (void *)&tiny_data;
  2835. /* string */
  2836. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  2837. my_bind[1].buffer= str_data;
  2838. my_bind[1].buffer_length= sizeof(str_data);;
  2839. my_bind[1].length= &length;
  2840. rc= mysql_stmt_bind_param(stmt, my_bind);
  2841. check_execute(stmt, rc);
  2842. /* now, execute the prepared statement to insert 10 records.. */
  2843. for (tiny_data= 0; tiny_data < 3; tiny_data++)
  2844. {
  2845. length= sprintf(str_data, "MySQL%d", tiny_data);
  2846. rc= mysql_stmt_execute(stmt);
  2847. check_execute(stmt, rc);
  2848. }
  2849. mysql_stmt_close(stmt);
  2850. /* now fetch the results ..*/
  2851. rc= mysql_commit(mysql);
  2852. myquery(rc);
  2853. /* test the results now, only one row should exist */
  2854. rc= mysql_query(mysql, "SELECT * FROM test_prep_insert");
  2855. myquery(rc);
  2856. /* get the result */
  2857. result= mysql_store_result(mysql);
  2858. mytest(result);
  2859. rc= my_process_result_set(result);
  2860. DIE_UNLESS((int) tiny_data == rc);
  2861. mysql_free_result(result);
  2862. }
  2863. /* Test simple prepare-resultset info */
  2864. static void test_prepare_resultset()
  2865. {
  2866. MYSQL_STMT *stmt;
  2867. int rc;
  2868. MYSQL_RES *result;
  2869. myheader("test_prepare_resultset");
  2870. rc= mysql_autocommit(mysql, TRUE);
  2871. myquery(rc);
  2872. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_resultset");
  2873. myquery(rc);
  2874. rc= mysql_query(mysql, "CREATE TABLE test_prepare_resultset(id int, \
  2875. name varchar(50), extra double)");
  2876. myquery(rc);
  2877. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_prepare_resultset");
  2878. check_stmt(stmt);
  2879. verify_param_count(stmt, 0);
  2880. result= mysql_stmt_result_metadata(stmt);
  2881. mytest(result);
  2882. my_print_result_metadata(result);
  2883. mysql_free_result(result);
  2884. mysql_stmt_close(stmt);
  2885. }
  2886. /* Test field flags (verify .NET provider) */
  2887. static void test_field_flags()
  2888. {
  2889. int rc;
  2890. MYSQL_RES *result;
  2891. MYSQL_FIELD *field;
  2892. unsigned int i;
  2893. myheader("test_field_flags");
  2894. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_flags");
  2895. myquery(rc);
  2896. rc= mysql_query(mysql, "CREATE TABLE test_field_flags(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, \
  2897. id1 int NOT NULL, \
  2898. id2 int UNIQUE, \
  2899. id3 int, \
  2900. id4 int NOT NULL, \
  2901. id5 int, \
  2902. KEY(id3, id4))");
  2903. myquery(rc);
  2904. /* with table name included with TRUE column name */
  2905. rc= mysql_query(mysql, "SELECT * FROM test_field_flags");
  2906. myquery(rc);
  2907. result= mysql_use_result(mysql);
  2908. mytest(result);
  2909. mysql_field_seek(result, 0);
  2910. if (!opt_silent)
  2911. fputc('\n', stdout);
  2912. for(i= 0; i< mysql_num_fields(result); i++)
  2913. {
  2914. field= mysql_fetch_field(result);
  2915. if (!opt_silent)
  2916. {
  2917. fprintf(stdout, "\n field:%d", i);
  2918. if (field->flags & NOT_NULL_FLAG)
  2919. fprintf(stdout, "\n NOT_NULL_FLAG");
  2920. if (field->flags & PRI_KEY_FLAG)
  2921. fprintf(stdout, "\n PRI_KEY_FLAG");
  2922. if (field->flags & UNIQUE_KEY_FLAG)
  2923. fprintf(stdout, "\n UNIQUE_KEY_FLAG");
  2924. if (field->flags & MULTIPLE_KEY_FLAG)
  2925. fprintf(stdout, "\n MULTIPLE_KEY_FLAG");
  2926. if (field->flags & AUTO_INCREMENT_FLAG)
  2927. fprintf(stdout, "\n AUTO_INCREMENT_FLAG");
  2928. }
  2929. }
  2930. mysql_free_result(result);
  2931. }
  2932. /* Test mysql_stmt_close for open stmts */
  2933. static void test_stmt_close()
  2934. {
  2935. MYSQL *lmysql;
  2936. MYSQL_STMT *stmt1, *stmt2, *stmt3, *stmt_x;
  2937. MYSQL_BIND my_bind[1];
  2938. MYSQL_RES *result;
  2939. unsigned int count;
  2940. int rc;
  2941. char query[MAX_TEST_QUERY_LENGTH];
  2942. myheader("test_stmt_close");
  2943. if (!opt_silent)
  2944. fprintf(stdout, "\n Establishing a test connection ...");
  2945. if (!(lmysql= mysql_client_init(NULL)))
  2946. {
  2947. myerror("mysql_client_init() failed");
  2948. exit(1);
  2949. }
  2950. if (!(mysql_real_connect(lmysql, opt_host, opt_user,
  2951. opt_password, current_db, opt_port,
  2952. opt_unix_socket, 0)))
  2953. {
  2954. myerror("connection failed");
  2955. exit(1);
  2956. }
  2957. lmysql->reconnect= 1;
  2958. if (!opt_silent)
  2959. fprintf(stdout, "OK");
  2960. /* set AUTOCOMMIT to ON*/
  2961. mysql_autocommit(lmysql, TRUE);
  2962. rc= mysql_query(lmysql, "SET SQL_MODE = ''");
  2963. myquery(rc);
  2964. rc= mysql_query(lmysql, "DROP TABLE IF EXISTS test_stmt_close");
  2965. myquery(rc);
  2966. rc= mysql_query(lmysql, "CREATE TABLE test_stmt_close(id int)");
  2967. myquery(rc);
  2968. strmov(query, "DO \"nothing\"");
  2969. stmt1= mysql_simple_prepare(lmysql, query);
  2970. check_stmt(stmt1);
  2971. verify_param_count(stmt1, 0);
  2972. strmov(query, "INSERT INTO test_stmt_close(id) VALUES(?)");
  2973. stmt_x= mysql_simple_prepare(mysql, query);
  2974. check_stmt(stmt_x);
  2975. verify_param_count(stmt_x, 1);
  2976. strmov(query, "UPDATE test_stmt_close SET id= ? WHERE id= ?");
  2977. stmt3= mysql_simple_prepare(lmysql, query);
  2978. check_stmt(stmt3);
  2979. verify_param_count(stmt3, 2);
  2980. strmov(query, "SELECT * FROM test_stmt_close WHERE id= ?");
  2981. stmt2= mysql_simple_prepare(lmysql, query);
  2982. check_stmt(stmt2);
  2983. verify_param_count(stmt2, 1);
  2984. rc= mysql_stmt_close(stmt1);
  2985. if (!opt_silent)
  2986. fprintf(stdout, "\n mysql_close_stmt(1) returned: %d", rc);
  2987. DIE_UNLESS(rc == 0);
  2988. /*
  2989. Originally we were going to close all statements automatically in
  2990. mysql_close(). This proved to not work well - users weren't able to
  2991. close statements by hand once mysql_close() had been called.
  2992. Now mysql_close() doesn't free any statements, so this test doesn't
  2993. serve its original designation any more.
  2994. Here we free stmt2 and stmt3 by hand to avoid memory leaks.
  2995. */
  2996. mysql_stmt_close(stmt2);
  2997. mysql_stmt_close(stmt3);
  2998. mysql_close(lmysql);
  2999. /*
  3000. We need to bzero bind structure because mysql_stmt_bind_param checks all
  3001. its members.
  3002. */
  3003. bzero((char*) my_bind, sizeof(my_bind));
  3004. my_bind[0].buffer= (void *)&count;
  3005. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  3006. count= 100;
  3007. rc= mysql_stmt_bind_param(stmt_x, my_bind);
  3008. check_execute(stmt_x, rc);
  3009. rc= mysql_stmt_execute(stmt_x);
  3010. check_execute(stmt_x, rc);
  3011. verify_st_affected_rows(stmt_x, 1);
  3012. rc= mysql_stmt_close(stmt_x);
  3013. if (!opt_silent)
  3014. fprintf(stdout, "\n mysql_close_stmt(x) returned: %d", rc);
  3015. DIE_UNLESS( rc == 0);
  3016. rc= mysql_query(mysql, "SELECT id FROM test_stmt_close");
  3017. myquery(rc);
  3018. result= mysql_store_result(mysql);
  3019. mytest(result);
  3020. rc= my_process_result_set(result);
  3021. DIE_UNLESS(rc == 1);
  3022. mysql_free_result(result);
  3023. }
  3024. /* Test simple set-variable prepare */
  3025. static void test_set_variable()
  3026. {
  3027. MYSQL_STMT *stmt, *stmt1;
  3028. int rc;
  3029. int set_count, def_count, get_count;
  3030. ulong length;
  3031. char var[NAME_LEN+1];
  3032. MYSQL_BIND set_bind[1], get_bind[2];
  3033. myheader("test_set_variable");
  3034. mysql_autocommit(mysql, TRUE);
  3035. stmt1= mysql_simple_prepare(mysql, "show variables like 'max_error_count'");
  3036. check_stmt(stmt1);
  3037. /*
  3038. We need to bzero bind structure because mysql_stmt_bind_param checks all
  3039. its members.
  3040. */
  3041. bzero((char*) get_bind, sizeof(get_bind));
  3042. get_bind[0].buffer_type= MYSQL_TYPE_STRING;
  3043. get_bind[0].buffer= (void *)var;
  3044. get_bind[0].length= &length;
  3045. get_bind[0].buffer_length= (int)NAME_LEN;
  3046. length= NAME_LEN;
  3047. get_bind[1].buffer_type= MYSQL_TYPE_LONG;
  3048. get_bind[1].buffer= (void *)&get_count;
  3049. rc= mysql_stmt_execute(stmt1);
  3050. check_execute(stmt1, rc);
  3051. rc= mysql_stmt_bind_result(stmt1, get_bind);
  3052. check_execute(stmt1, rc);
  3053. rc= mysql_stmt_fetch(stmt1);
  3054. check_execute(stmt1, rc);
  3055. if (!opt_silent)
  3056. fprintf(stdout, "\n max_error_count(default): %d", get_count);
  3057. def_count= get_count;
  3058. DIE_UNLESS(strcmp(var, "max_error_count") == 0);
  3059. rc= mysql_stmt_fetch(stmt1);
  3060. DIE_UNLESS(rc == MYSQL_NO_DATA);
  3061. stmt= mysql_simple_prepare(mysql, "set max_error_count= ?");
  3062. check_stmt(stmt);
  3063. bzero((char*) set_bind, sizeof(set_bind));
  3064. set_bind[0].buffer_type= MYSQL_TYPE_LONG;
  3065. set_bind[0].buffer= (void *)&set_count;
  3066. rc= mysql_stmt_bind_param(stmt, set_bind);
  3067. check_execute(stmt, rc);
  3068. set_count= 31;
  3069. rc= mysql_stmt_execute(stmt);
  3070. check_execute(stmt, rc);
  3071. mysql_commit(mysql);
  3072. rc= mysql_stmt_execute(stmt1);
  3073. check_execute(stmt1, rc);
  3074. rc= mysql_stmt_fetch(stmt1);
  3075. check_execute(stmt1, rc);
  3076. if (!opt_silent)
  3077. fprintf(stdout, "\n max_error_count : %d", get_count);
  3078. DIE_UNLESS(get_count == set_count);
  3079. rc= mysql_stmt_fetch(stmt1);
  3080. DIE_UNLESS(rc == MYSQL_NO_DATA);
  3081. /* restore back to default */
  3082. set_count= def_count;
  3083. rc= mysql_stmt_execute(stmt);
  3084. check_execute(stmt, rc);
  3085. rc= mysql_stmt_execute(stmt1);
  3086. check_execute(stmt1, rc);
  3087. rc= mysql_stmt_fetch(stmt1);
  3088. check_execute(stmt1, rc);
  3089. if (!opt_silent)
  3090. fprintf(stdout, "\n max_error_count(default): %d", get_count);
  3091. DIE_UNLESS(get_count == set_count);
  3092. rc= mysql_stmt_fetch(stmt1);
  3093. DIE_UNLESS(rc == MYSQL_NO_DATA);
  3094. mysql_stmt_close(stmt);
  3095. mysql_stmt_close(stmt1);
  3096. }
  3097. #if NOT_USED
  3098. /* Insert meta info .. */
  3099. static void test_insert_meta()
  3100. {
  3101. MYSQL_STMT *stmt;
  3102. int rc;
  3103. MYSQL_RES *result;
  3104. MYSQL_FIELD *field;
  3105. myheader("test_insert_meta");
  3106. rc= mysql_autocommit(mysql, TRUE);
  3107. myquery(rc);
  3108. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
  3109. myquery(rc);
  3110. rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
  3111. col2 varchar(50), col3 varchar(30))");
  3112. myquery(rc);
  3113. strmov(query, "INSERT INTO test_prep_insert VALUES(10, 'venu1', 'test')");
  3114. stmt= mysql_simple_prepare(mysql, query);
  3115. check_stmt(stmt);
  3116. verify_param_count(stmt, 0);
  3117. result= mysql_param_result(stmt);
  3118. mytest_r(result);
  3119. mysql_stmt_close(stmt);
  3120. strmov(query, "INSERT INTO test_prep_insert VALUES(?, 'venu', ?)");
  3121. stmt= mysql_simple_prepare(mysql, query);
  3122. check_stmt(stmt);
  3123. verify_param_count(stmt, 2);
  3124. result= mysql_param_result(stmt);
  3125. mytest(result);
  3126. my_print_result_metadata(result);
  3127. mysql_field_seek(result, 0);
  3128. field= mysql_fetch_field(result);
  3129. mytest(field);
  3130. if (!opt_silent)
  3131. fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col1");
  3132. DIE_UNLESS(strcmp(field->name, "col1") == 0);
  3133. field= mysql_fetch_field(result);
  3134. mytest(field);
  3135. if (!opt_silent)
  3136. fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col3");
  3137. DIE_UNLESS(strcmp(field->name, "col3") == 0);
  3138. field= mysql_fetch_field(result);
  3139. mytest_r(field);
  3140. mysql_free_result(result);
  3141. mysql_stmt_close(stmt);
  3142. }
  3143. /* Update meta info .. */
  3144. static void test_update_meta()
  3145. {
  3146. MYSQL_STMT *stmt;
  3147. int rc;
  3148. MYSQL_RES *result;
  3149. MYSQL_FIELD *field;
  3150. myheader("test_update_meta");
  3151. rc= mysql_autocommit(mysql, TRUE);
  3152. myquery(rc);
  3153. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_update");
  3154. myquery(rc);
  3155. rc= mysql_query(mysql, "CREATE TABLE test_prep_update(col1 tinyint, \
  3156. col2 varchar(50), col3 varchar(30))");
  3157. myquery(rc);
  3158. strmov(query, "UPDATE test_prep_update SET col1=10, col2='venu1' WHERE col3='test'");
  3159. stmt= mysql_simple_prepare(mysql, query);
  3160. check_stmt(stmt);
  3161. verify_param_count(stmt, 0);
  3162. result= mysql_param_result(stmt);
  3163. mytest_r(result);
  3164. mysql_stmt_close(stmt);
  3165. strmov(query, "UPDATE test_prep_update SET col1=?, col2='venu' WHERE col3=?");
  3166. stmt= mysql_simple_prepare(mysql, query);
  3167. check_stmt(stmt);
  3168. verify_param_count(stmt, 2);
  3169. result= mysql_param_result(stmt);
  3170. mytest(result);
  3171. my_print_result_metadata(result);
  3172. mysql_field_seek(result, 0);
  3173. field= mysql_fetch_field(result);
  3174. mytest(field);
  3175. if (!opt_silent)
  3176. {
  3177. fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
  3178. fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
  3179. }
  3180. DIE_UNLESS(strcmp(field->name, "col1") == 0);
  3181. DIE_UNLESS(strcmp(field->table, "test_prep_update") == 0);
  3182. field= mysql_fetch_field(result);
  3183. mytest(field);
  3184. if (!opt_silent)
  3185. {
  3186. fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col3");
  3187. fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
  3188. }
  3189. DIE_UNLESS(strcmp(field->name, "col3") == 0);
  3190. DIE_UNLESS(strcmp(field->table, "test_prep_update") == 0);
  3191. field= mysql_fetch_field(result);
  3192. mytest_r(field);
  3193. mysql_free_result(result);
  3194. mysql_stmt_close(stmt);
  3195. }
  3196. /* Select meta info .. */
  3197. static void test_select_meta()
  3198. {
  3199. MYSQL_STMT *stmt;
  3200. int rc;
  3201. MYSQL_RES *result;
  3202. MYSQL_FIELD *field;
  3203. myheader("test_select_meta");
  3204. rc= mysql_autocommit(mysql, TRUE);
  3205. myquery(rc);
  3206. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_select");
  3207. myquery(rc);
  3208. rc= mysql_query(mysql, "CREATE TABLE test_prep_select(col1 tinyint, \
  3209. col2 varchar(50), col3 varchar(30))");
  3210. myquery(rc);
  3211. strmov(query, "SELECT * FROM test_prep_select WHERE col1=10");
  3212. stmt= mysql_simple_prepare(mysql, query);
  3213. check_stmt(stmt);
  3214. verify_param_count(stmt, 0);
  3215. result= mysql_param_result(stmt);
  3216. mytest_r(result);
  3217. strmov(query, "SELECT col1, col3 from test_prep_select WHERE col1=? AND col3='test' AND col2= ?");
  3218. stmt= mysql_simple_prepare(mysql, query);
  3219. check_stmt(stmt);
  3220. verify_param_count(stmt, 2);
  3221. result= mysql_param_result(stmt);
  3222. mytest(result);
  3223. my_print_result_metadata(result);
  3224. mysql_field_seek(result, 0);
  3225. field= mysql_fetch_field(result);
  3226. mytest(field);
  3227. if (!opt_silent)
  3228. {
  3229. fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
  3230. fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
  3231. }
  3232. DIE_UNLESS(strcmp(field->name, "col1") == 0);
  3233. DIE_UNLESS(strcmp(field->table, "test_prep_select") == 0);
  3234. field= mysql_fetch_field(result);
  3235. mytest(field);
  3236. if (!opt_silent)
  3237. {
  3238. fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col2");
  3239. fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
  3240. }
  3241. DIE_UNLESS(strcmp(field->name, "col2") == 0);
  3242. DIE_UNLESS(strcmp(field->table, "test_prep_select") == 0);
  3243. field= mysql_fetch_field(result);
  3244. mytest_r(field);
  3245. mysql_free_result(result);
  3246. mysql_stmt_close(stmt);
  3247. }
  3248. #endif
  3249. /* Test FUNCTION field info / DATE_FORMAT() table_name . */
  3250. static void test_func_fields()
  3251. {
  3252. int rc;
  3253. MYSQL_RES *result;
  3254. MYSQL_FIELD *field;
  3255. myheader("test_func_fields");
  3256. rc= mysql_autocommit(mysql, TRUE);
  3257. myquery(rc);
  3258. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_dateformat");
  3259. myquery(rc);
  3260. rc= mysql_query(mysql, "CREATE TABLE test_dateformat(id int, \
  3261. ts timestamp)");
  3262. myquery(rc);
  3263. rc= mysql_query(mysql, "INSERT INTO test_dateformat(id) values(10)");
  3264. myquery(rc);
  3265. rc= mysql_query(mysql, "SELECT ts FROM test_dateformat");
  3266. myquery(rc);
  3267. result= mysql_store_result(mysql);
  3268. mytest(result);
  3269. field= mysql_fetch_field(result);
  3270. mytest(field);
  3271. if (!opt_silent)
  3272. fprintf(stdout, "\n table name: `%s` (expected: `%s`)", field->table,
  3273. "test_dateformat");
  3274. DIE_UNLESS(strcmp(field->table, "test_dateformat") == 0);
  3275. field= mysql_fetch_field(result);
  3276. mytest_r(field); /* no more fields */
  3277. mysql_free_result(result);
  3278. /* DATE_FORMAT */
  3279. rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'venu' FROM test_dateformat");
  3280. myquery(rc);
  3281. result= mysql_store_result(mysql);
  3282. mytest(result);
  3283. field= mysql_fetch_field(result);
  3284. mytest(field);
  3285. if (!opt_silent)
  3286. fprintf(stdout, "\n table name: `%s` (expected: `%s`)", field->table, "");
  3287. DIE_UNLESS(field->table[0] == '\0');
  3288. field= mysql_fetch_field(result);
  3289. mytest_r(field); /* no more fields */
  3290. mysql_free_result(result);
  3291. /* FIELD ALIAS TEST */
  3292. rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'YEAR' FROM test_dateformat");
  3293. myquery(rc);
  3294. result= mysql_store_result(mysql);
  3295. mytest(result);
  3296. field= mysql_fetch_field(result);
  3297. mytest(field);
  3298. if (!opt_silent)
  3299. {
  3300. printf("\n field name: `%s` (expected: `%s`)", field->name, "YEAR");
  3301. printf("\n field org name: `%s` (expected: `%s`)", field->org_name, "");
  3302. }
  3303. DIE_UNLESS(strcmp(field->name, "YEAR") == 0);
  3304. DIE_UNLESS(field->org_name[0] == '\0');
  3305. field= mysql_fetch_field(result);
  3306. mytest_r(field); /* no more fields */
  3307. mysql_free_result(result);
  3308. }
  3309. /* Multiple stmts .. */
  3310. static void test_multi_stmt()
  3311. {
  3312. MYSQL_STMT *stmt, *stmt1, *stmt2;
  3313. int rc;
  3314. uint32 id;
  3315. char name[50];
  3316. MYSQL_BIND my_bind[2];
  3317. ulong length[2];
  3318. my_bool is_null[2];
  3319. myheader("test_multi_stmt");
  3320. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_multi_table");
  3321. myquery(rc);
  3322. rc= mysql_query(mysql, "CREATE TABLE test_multi_table(id int, name char(20))");
  3323. myquery(rc);
  3324. rc= mysql_query(mysql, "INSERT INTO test_multi_table values(10, 'mysql')");
  3325. myquery(rc);
  3326. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_multi_table "
  3327. "WHERE id= ?");
  3328. check_stmt(stmt);
  3329. stmt2= mysql_simple_prepare(mysql, "UPDATE test_multi_table "
  3330. "SET name='updated' WHERE id=10");
  3331. check_stmt(stmt2);
  3332. verify_param_count(stmt, 1);
  3333. /*
  3334. We need to bzero bind structure because mysql_stmt_bind_param checks all
  3335. its members.
  3336. */
  3337. bzero((char*) my_bind, sizeof(my_bind));
  3338. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  3339. my_bind[0].buffer= (void *)&id;
  3340. my_bind[0].is_null= &is_null[0];
  3341. my_bind[0].length= &length[0];
  3342. is_null[0]= 0;
  3343. length[0]= 0;
  3344. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  3345. my_bind[1].buffer= (void *)name;
  3346. my_bind[1].buffer_length= sizeof(name);
  3347. my_bind[1].length= &length[1];
  3348. my_bind[1].is_null= &is_null[1];
  3349. rc= mysql_stmt_bind_param(stmt, my_bind);
  3350. check_execute(stmt, rc);
  3351. rc= mysql_stmt_bind_result(stmt, my_bind);
  3352. check_execute(stmt, rc);
  3353. id= 10;
  3354. rc= mysql_stmt_execute(stmt);
  3355. check_execute(stmt, rc);
  3356. id= 999;
  3357. rc= mysql_stmt_fetch(stmt);
  3358. check_execute(stmt, rc);
  3359. if (!opt_silent)
  3360. {
  3361. fprintf(stdout, "\n int_data: %lu(%lu)", (ulong) id, length[0]);
  3362. fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
  3363. }
  3364. DIE_UNLESS(id == 10);
  3365. DIE_UNLESS(strcmp(name, "mysql") == 0);
  3366. rc= mysql_stmt_fetch(stmt);
  3367. DIE_UNLESS(rc == MYSQL_NO_DATA);
  3368. /* alter the table schema now */
  3369. stmt1= mysql_simple_prepare(mysql, "DELETE FROM test_multi_table "
  3370. "WHERE id= ? AND "
  3371. "CONVERT(name USING utf8)=?");
  3372. check_stmt(stmt1);
  3373. verify_param_count(stmt1, 2);
  3374. rc= mysql_stmt_bind_param(stmt1, my_bind);
  3375. check_execute(stmt1, rc);
  3376. rc= mysql_stmt_execute(stmt2);
  3377. check_execute(stmt2, rc);
  3378. verify_st_affected_rows(stmt2, 1);
  3379. rc= mysql_stmt_execute(stmt);
  3380. check_execute(stmt, rc);
  3381. rc= mysql_stmt_fetch(stmt);
  3382. check_execute(stmt, rc);
  3383. if (!opt_silent)
  3384. {
  3385. fprintf(stdout, "\n int_data: %lu(%lu)", (ulong) id, length[0]);
  3386. fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
  3387. }
  3388. DIE_UNLESS(id == 10);
  3389. DIE_UNLESS(strcmp(name, "updated") == 0);
  3390. rc= mysql_stmt_fetch(stmt);
  3391. DIE_UNLESS(rc == MYSQL_NO_DATA);
  3392. rc= mysql_stmt_execute(stmt1);
  3393. check_execute(stmt1, rc);
  3394. verify_st_affected_rows(stmt1, 1);
  3395. mysql_stmt_close(stmt1);
  3396. rc= mysql_stmt_execute(stmt);
  3397. check_execute(stmt, rc);
  3398. rc= mysql_stmt_fetch(stmt);
  3399. DIE_UNLESS(rc == MYSQL_NO_DATA);
  3400. rc= my_stmt_result("SELECT * FROM test_multi_table");
  3401. DIE_UNLESS(rc == 0);
  3402. mysql_stmt_close(stmt);
  3403. mysql_stmt_close(stmt2);
  3404. }
  3405. /* Test simple sample - manual */
  3406. static void test_manual_sample()
  3407. {
  3408. unsigned int param_count;
  3409. MYSQL_STMT *stmt;
  3410. short small_data;
  3411. int int_data;
  3412. int rc;
  3413. char str_data[50];
  3414. ulonglong affected_rows;
  3415. MYSQL_BIND my_bind[3];
  3416. my_bool is_null;
  3417. char query[MAX_TEST_QUERY_LENGTH];
  3418. myheader("test_manual_sample");
  3419. /*
  3420. Sample which is incorporated directly in the manual under Prepared
  3421. statements section (Example from mysql_stmt_execute()
  3422. */
  3423. mysql_autocommit(mysql, 1);
  3424. if (mysql_query(mysql, "DROP TABLE IF EXISTS test_table"))
  3425. {
  3426. fprintf(stderr, "\n drop table failed");
  3427. fprintf(stderr, "\n %s", mysql_error(mysql));
  3428. exit(1);
  3429. }
  3430. if (mysql_query(mysql, "CREATE TABLE test_table(col1 int, col2 varchar(50), \
  3431. col3 smallint, \
  3432. col4 timestamp(14))"))
  3433. {
  3434. fprintf(stderr, "\n create table failed");
  3435. fprintf(stderr, "\n %s", mysql_error(mysql));
  3436. exit(1);
  3437. }
  3438. /* Prepare a insert query with 3 parameters */
  3439. strmov(query, "INSERT INTO test_table(col1, col2, col3) values(?, ?, ?)");
  3440. if (!(stmt= mysql_simple_prepare(mysql, query)))
  3441. {
  3442. fprintf(stderr, "\n prepare, insert failed");
  3443. fprintf(stderr, "\n %s", mysql_error(mysql));
  3444. exit(1);
  3445. }
  3446. if (!opt_silent)
  3447. fprintf(stdout, "\n prepare, insert successful");
  3448. /* Get the parameter count from the statement */
  3449. param_count= mysql_stmt_param_count(stmt);
  3450. if (!opt_silent)
  3451. fprintf(stdout, "\n total parameters in insert: %d", param_count);
  3452. if (param_count != 3) /* validate parameter count */
  3453. {
  3454. fprintf(stderr, "\n invalid parameter count returned by MySQL");
  3455. exit(1);
  3456. }
  3457. /* Bind the data for the parameters */
  3458. /*
  3459. We need to bzero bind structure because mysql_stmt_bind_param checks all
  3460. its members.
  3461. */
  3462. bzero((char*) my_bind, sizeof(my_bind));
  3463. /* INTEGER PART */
  3464. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  3465. my_bind[0].buffer= (void *)&int_data;
  3466. /* STRING PART */
  3467. my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
  3468. my_bind[1].buffer= (void *)str_data;
  3469. my_bind[1].buffer_length= sizeof(str_data);
  3470. /* SMALLINT PART */
  3471. my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
  3472. my_bind[2].buffer= (void *)&small_data;
  3473. my_bind[2].is_null= &is_null;
  3474. is_null= 0;
  3475. /* Bind the buffers */
  3476. if (mysql_stmt_bind_param(stmt, my_bind))
  3477. {
  3478. fprintf(stderr, "\n param bind failed");
  3479. fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
  3480. exit(1);
  3481. }
  3482. /* Specify the data */
  3483. int_data= 10; /* integer */
  3484. strmov(str_data, "MySQL"); /* string */
  3485. /* INSERT SMALLINT data as NULL */
  3486. is_null= 1;
  3487. /* Execute the insert statement - 1*/
  3488. if (mysql_stmt_execute(stmt))
  3489. {
  3490. fprintf(stderr, "\n execute 1 failed");
  3491. fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
  3492. exit(1);
  3493. }
  3494. /* Get the total rows affected */
  3495. affected_rows= mysql_stmt_affected_rows(stmt);
  3496. if (!opt_silent)
  3497. fprintf(stdout, "\n total affected rows: %ld", (ulong) affected_rows);
  3498. if (affected_rows != 1) /* validate affected rows */
  3499. {
  3500. fprintf(stderr, "\n invalid affected rows by MySQL");
  3501. exit(1);
  3502. }
  3503. /* Re-execute the insert, by changing the values */
  3504. int_data= 1000;
  3505. strmov(str_data, "The most popular open source database");
  3506. small_data= 1000; /* smallint */
  3507. is_null= 0; /* reset */
  3508. /* Execute the insert statement - 2*/
  3509. if (mysql_stmt_execute(stmt))
  3510. {
  3511. fprintf(stderr, "\n execute 2 failed");
  3512. fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
  3513. exit(1);
  3514. }
  3515. /* Get the total rows affected */
  3516. affected_rows= mysql_stmt_affected_rows(stmt);
  3517. if (!opt_silent)
  3518. fprintf(stdout, "\n total affected rows: %ld", (ulong) affected_rows);
  3519. if (affected_rows != 1) /* validate affected rows */
  3520. {
  3521. fprintf(stderr, "\n invalid affected rows by MySQL");
  3522. exit(1);
  3523. }
  3524. /* Close the statement */
  3525. if (mysql_stmt_close(stmt))
  3526. {
  3527. fprintf(stderr, "\n failed while closing the statement");
  3528. fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
  3529. exit(1);
  3530. }
  3531. rc= my_stmt_result("SELECT * FROM test_table");
  3532. DIE_UNLESS(rc == 2);
  3533. /* DROP THE TABLE */
  3534. if (mysql_query(mysql, "DROP TABLE test_table"))
  3535. {
  3536. fprintf(stderr, "\n drop table failed");
  3537. fprintf(stderr, "\n %s", mysql_error(mysql));
  3538. exit(1);
  3539. }
  3540. if (!opt_silent)
  3541. fprintf(stdout, "Success !!!");
  3542. }
  3543. /* Test alter table scenario in the middle of prepare */
  3544. static void test_prepare_alter()
  3545. {
  3546. MYSQL_STMT *stmt;
  3547. int rc, id;
  3548. MYSQL_BIND my_bind[1];
  3549. my_bool is_null;
  3550. myheader("test_prepare_alter");
  3551. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_alter");
  3552. myquery(rc);
  3553. rc= mysql_query(mysql, "CREATE TABLE test_prep_alter(id int, name char(20))");
  3554. myquery(rc);
  3555. rc= mysql_query(mysql, "INSERT INTO test_prep_alter values(10, 'venu'), (20, 'mysql')");
  3556. myquery(rc);
  3557. stmt= mysql_simple_prepare(mysql, "INSERT INTO test_prep_alter VALUES(?, 'monty')");
  3558. check_stmt(stmt);
  3559. verify_param_count(stmt, 1);
  3560. /*
  3561. We need to bzero bind structure because mysql_stmt_bind_param checks all
  3562. its members.
  3563. */
  3564. bzero((char*) my_bind, sizeof(my_bind));
  3565. is_null= 0;
  3566. my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
  3567. my_bind[0].buffer= (void *)&id;
  3568. my_bind[0].is_null= &is_null;
  3569. rc= mysql_stmt_bind_param(stmt, my_bind);
  3570. check_execute(stmt, rc);
  3571. id= 30;
  3572. rc= mysql_stmt_execute(stmt);
  3573. check_execute(stmt, rc);
  3574. if (thread_query("ALTER TABLE test_prep_alter change id id_new varchar(20)"))
  3575. exit(1);
  3576. is_null= 1;
  3577. rc= mysql_stmt_execute(stmt);
  3578. check_execute(stmt, rc);
  3579. rc= my_stmt_result("SELECT * FROM test_prep_alter");
  3580. DIE_UNLESS(rc == 4);
  3581. mysql_stmt_close(stmt);
  3582. }
  3583. /* Test the support of multi-statement executions */
  3584. static void test_multi_statements()
  3585. {
  3586. MYSQL *mysql_local;
  3587. MYSQL_RES *result;
  3588. int rc;
  3589. const char *query= "\
  3590. DROP TABLE IF EXISTS test_multi_tab;\
  3591. CREATE TABLE test_multi_tab(id int, name char(20));\
  3592. INSERT INTO test_multi_tab(id) VALUES(10), (20);\
  3593. INSERT INTO test_multi_tab VALUES(20, 'insert;comma');\
  3594. SELECT * FROM test_multi_tab;\
  3595. UPDATE test_multi_tab SET name='new;name' WHERE id=20;\
  3596. DELETE FROM test_multi_tab WHERE name='new;name';\
  3597. SELECT * FROM test_multi_tab;\
  3598. DELETE FROM test_multi_tab WHERE id=10;\
  3599. SELECT * FROM test_multi_tab;\
  3600. DROP TABLE test_multi_tab;\
  3601. select 1;\
  3602. DROP TABLE IF EXISTS test_multi_tab";
  3603. uint count, exp_value;
  3604. uint rows[]= {0, 0, 2, 1, 3, 2, 2, 1, 1, 0, 0, 1, 0};
  3605. myheader("test_multi_statements");
  3606. /*
  3607. First test that we get an error for multi statements
  3608. (Because default connection is not opened with CLIENT_MULTI_STATEMENTS)
  3609. */
  3610. rc= mysql_query(mysql, query); /* syntax error */
  3611. myquery_r(rc);
  3612. rc= mysql_next_result(mysql);
  3613. DIE_UNLESS(rc == -1);
  3614. rc= mysql_more_results(mysql);
  3615. DIE_UNLESS(rc == 0);
  3616. if (!(mysql_local= mysql_client_init(NULL)))
  3617. {
  3618. fprintf(stdout, "\n mysql_client_init() failed");
  3619. exit(1);
  3620. }
  3621. /* Create connection that supports multi statements */
  3622. if (!(mysql_real_connect(mysql_local, opt_host, opt_user,
  3623. opt_password, current_db, opt_port,
  3624. opt_unix_socket, CLIENT_MULTI_STATEMENTS)))
  3625. {
  3626. fprintf(stdout, "\n connection failed(%s)", mysql_error(mysql_local));
  3627. exit(1);
  3628. }
  3629. mysql_local->reconnect= 1;
  3630. rc= mysql_query(mysql_local, query);
  3631. myquery(rc);
  3632. for (count= 0 ; count < array_elements(rows) ; count++)
  3633. {
  3634. if (!opt_silent)
  3635. fprintf(stdout, "\n Query %d: ", count);
  3636. if ((result= mysql_store_result(mysql_local)))
  3637. {
  3638. (void) my_process_result_set(result);
  3639. mysql_free_result(result);
  3640. }
  3641. else if (!opt_silent)
  3642. fprintf(stdout, "OK, %ld row(s) affected, %ld warning(s)\n",
  3643. (ulong) mysql_affected_rows(mysql_local),
  3644. (ulong) mysql_warning_count(mysql_local));
  3645. exp_value= (uint) mysql_affected_rows(mysql_local);
  3646. if (rows[count] != exp_value)
  3647. {
  3648. fprintf(stderr, "row %d had affected rows: %d, should be %d\n",
  3649. count, exp_value, rows[count]);
  3650. exit(1);
  3651. }
  3652. if (count != array_elements(rows) -1)
  3653. {
  3654. if (!(rc= mysql_more_results(mysql_local)))
  3655. {
  3656. fprintf(stdout,
  3657. "mysql_more_result returned wrong value: %d for row %d\n",
  3658. rc, count);
  3659. exit(1);
  3660. }
  3661. if ((rc= mysql_next_result(mysql_local)))
  3662. {
  3663. exp_value= mysql_errno(mysql_local);
  3664. exit(1);
  3665. }
  3666. }
  3667. else
  3668. {
  3669. rc= mysql_more_results(mysql_local);
  3670. DIE_UNLESS(rc == 0);
  3671. rc= mysql_next_result(mysql_local);
  3672. DIE_UNLESS(rc == -1);
  3673. }
  3674. }
  3675. /* check that errors abort multi statements */
  3676. rc= mysql_query(mysql_local, "select 1+1+a;select 1+1");
  3677. myquery_r(rc);
  3678. rc= mysql_more_results(mysql_local);
  3679. DIE_UNLESS(rc == 0);
  3680. rc= mysql_next_result(mysql_local);
  3681. DIE_UNLESS(rc == -1);
  3682. rc= mysql_query(mysql_local, "select 1+1;select 1+1+a;select 1");
  3683. myquery(rc);
  3684. result= mysql_store_result(mysql_local);
  3685. mytest(result);
  3686. mysql_free_result(result);
  3687. rc= mysql_more_results(mysql_local);
  3688. DIE_UNLESS(rc == 1);
  3689. rc= mysql_next_result(mysql_local);
  3690. DIE_UNLESS(rc > 0);
  3691. /*
  3692. Ensure that we can now do a simple query (this checks that the server is
  3693. not trying to send us the results for the last 'select 1'
  3694. */
  3695. rc= mysql_query(mysql_local, "select 1+1+1");
  3696. myquery(rc);
  3697. result= mysql_store_result(mysql_local);
  3698. mytest(result);
  3699. (void) my_process_result_set(result);
  3700. mysql_free_result(result);
  3701. /*
  3702. Check if errors in one of the queries handled properly.
  3703. */
  3704. rc= mysql_query(mysql_local, "select 1; select * from not_existing_table");
  3705. myquery(rc);
  3706. result= mysql_store_result(mysql_local);
  3707. mysql_free_result(result);
  3708. rc= mysql_next_result(mysql_local);
  3709. DIE_UNLESS(rc > 0);
  3710. rc= mysql_next_result(mysql_local);
  3711. DIE_UNLESS(rc < 0);
  3712. mysql_close(mysql_local);
  3713. }
  3714. /*
  3715. Check that Prepared statement cannot contain several
  3716. SQL statements
  3717. */
  3718. static void test_prepare_multi_statements()
  3719. {
  3720. MYSQL *mysql_local;
  3721. MYSQL_STMT *stmt;
  3722. char query[MAX_TEST_QUERY_LENGTH];
  3723. myheader("test_prepare_multi_statements");
  3724. if (!(mysql_local= mysql_client_init(NULL)))
  3725. {
  3726. fprintf(stderr, "\n mysql_client_init() failed");
  3727. exit(1);
  3728. }
  3729. if (!(mysql_real_connect(mysql_local, opt_host, opt_user,
  3730. opt_password, current_db, opt_port,
  3731. opt_unix_socket, CLIENT_MULTI_STATEMENTS)))
  3732. {
  3733. fprintf(stderr, "\n connection failed(%s)", mysql_error(mysql_local));
  3734. exit(1);
  3735. }
  3736. mysql_local->reconnect= 1;
  3737. strmov(query, "select 1; select 'another value'");
  3738. stmt= mysql_simple_prepare(mysql_local, query);
  3739. check_stmt_r(stmt);
  3740. mysql_close(mysql_local);
  3741. }
  3742. /* Test simple bind store result */
  3743. static void test_store_result()
  3744. {
  3745. MYSQL_STMT *stmt;
  3746. int rc;
  3747. int32 nData;
  3748. char szData[100];
  3749. MYSQL_BIND my_bind[2];
  3750. ulong length, length1;
  3751. my_bool is_null[2];
  3752. myheader("test_store_result");
  3753. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
  3754. myquery(rc);
  3755. rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
  3756. myquery(rc);
  3757. rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
  3758. myquery(rc);
  3759. rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
  3760. myquery(rc);
  3761. rc= mysql_commit(mysql);
  3762. myquery(rc);
  3763. /* fetch */
  3764. bzero((char*) my_bind, sizeof(my_bind));
  3765. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  3766. my_bind[0].buffer= (void *) &nData; /* integer data */
  3767. my_bind[0].length= &length;
  3768. my_bind[0].is_null= &is_null[0];
  3769. length= 0;
  3770. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  3771. my_bind[1].buffer= szData; /* string data */
  3772. my_bind[1].buffer_length= sizeof(szData);
  3773. my_bind[1].length= &length1;
  3774. my_bind[1].is_null= &is_null[1];
  3775. length1= 0;
  3776. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_store_result");
  3777. check_stmt(stmt);
  3778. rc= mysql_stmt_bind_result(stmt, my_bind);
  3779. check_execute(stmt, rc);
  3780. rc= mysql_stmt_execute(stmt);
  3781. check_execute(stmt, rc);
  3782. rc= mysql_stmt_store_result(stmt);
  3783. check_execute(stmt, rc);
  3784. rc= mysql_stmt_fetch(stmt);
  3785. check_execute(stmt, rc);
  3786. if (!opt_silent)
  3787. fprintf(stdout, "\n row 1: %ld, %s(%lu)", (long) nData, szData, length1);
  3788. DIE_UNLESS(nData == 10);
  3789. DIE_UNLESS(strcmp(szData, "venu") == 0);
  3790. DIE_UNLESS(length1 == 4);
  3791. rc= mysql_stmt_fetch(stmt);
  3792. check_execute(stmt, rc);
  3793. if (!opt_silent)
  3794. fprintf(stdout, "\n row 2: %ld, %s(%lu)", (long) nData, szData, length1);
  3795. DIE_UNLESS(nData == 20);
  3796. DIE_UNLESS(strcmp(szData, "mysql") == 0);
  3797. DIE_UNLESS(length1 == 5);
  3798. length= 99;
  3799. rc= mysql_stmt_fetch(stmt);
  3800. check_execute(stmt, rc);
  3801. if (!opt_silent && is_null[0])
  3802. fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
  3803. DIE_UNLESS(is_null[0]);
  3804. DIE_UNLESS(strcmp(szData, "monty") == 0);
  3805. DIE_UNLESS(length1 == 5);
  3806. rc= mysql_stmt_fetch(stmt);
  3807. DIE_UNLESS(rc == MYSQL_NO_DATA);
  3808. rc= mysql_stmt_execute(stmt);
  3809. check_execute(stmt, rc);
  3810. rc= mysql_stmt_store_result(stmt);
  3811. check_execute(stmt, rc);
  3812. rc= mysql_stmt_fetch(stmt);
  3813. check_execute(stmt, rc);
  3814. if (!opt_silent)
  3815. fprintf(stdout, "\n row 1: %ld, %s(%lu)", (long) nData, szData, length1);
  3816. DIE_UNLESS(nData == 10);
  3817. DIE_UNLESS(strcmp(szData, "venu") == 0);
  3818. DIE_UNLESS(length1 == 4);
  3819. rc= mysql_stmt_fetch(stmt);
  3820. check_execute(stmt, rc);
  3821. if (!opt_silent)
  3822. fprintf(stdout, "\n row 2: %ld, %s(%lu)", (long) nData, szData, length1);
  3823. DIE_UNLESS(nData == 20);
  3824. DIE_UNLESS(strcmp(szData, "mysql") == 0);
  3825. DIE_UNLESS(length1 == 5);
  3826. length= 99;
  3827. rc= mysql_stmt_fetch(stmt);
  3828. check_execute(stmt, rc);
  3829. if (!opt_silent && is_null[0])
  3830. fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
  3831. DIE_UNLESS(is_null[0]);
  3832. DIE_UNLESS(strcmp(szData, "monty") == 0);
  3833. DIE_UNLESS(length1 == 5);
  3834. rc= mysql_stmt_fetch(stmt);
  3835. DIE_UNLESS(rc == MYSQL_NO_DATA);
  3836. mysql_stmt_close(stmt);
  3837. }
  3838. /* Test simple bind store result */
  3839. static void test_store_result1()
  3840. {
  3841. MYSQL_STMT *stmt;
  3842. int rc;
  3843. myheader("test_store_result1");
  3844. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
  3845. myquery(rc);
  3846. rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
  3847. myquery(rc);
  3848. rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
  3849. myquery(rc);
  3850. rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
  3851. myquery(rc);
  3852. rc= mysql_commit(mysql);
  3853. myquery(rc);
  3854. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_store_result");
  3855. check_stmt(stmt);
  3856. rc= mysql_stmt_execute(stmt);
  3857. check_execute(stmt, rc);
  3858. rc= mysql_stmt_store_result(stmt);
  3859. check_execute(stmt, rc);
  3860. rc= 0;
  3861. while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
  3862. rc++;
  3863. if (!opt_silent)
  3864. fprintf(stdout, "\n total rows: %d", rc);
  3865. DIE_UNLESS(rc == 3);
  3866. rc= mysql_stmt_execute(stmt);
  3867. check_execute(stmt, rc);
  3868. rc= mysql_stmt_store_result(stmt);
  3869. check_execute(stmt, rc);
  3870. rc= 0;
  3871. while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
  3872. rc++;
  3873. if (!opt_silent)
  3874. fprintf(stdout, "\n total rows: %d", rc);
  3875. DIE_UNLESS(rc == 3);
  3876. mysql_stmt_close(stmt);
  3877. }
  3878. /* Another test for bind and store result */
  3879. static void test_store_result2()
  3880. {
  3881. MYSQL_STMT *stmt;
  3882. int rc;
  3883. int nData;
  3884. ulong length;
  3885. MYSQL_BIND my_bind[1];
  3886. char query[MAX_TEST_QUERY_LENGTH];
  3887. myheader("test_store_result2");
  3888. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
  3889. myquery(rc);
  3890. rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
  3891. myquery(rc);
  3892. rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
  3893. myquery(rc);
  3894. rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
  3895. myquery(rc);
  3896. rc= mysql_commit(mysql);
  3897. myquery(rc);
  3898. /*
  3899. We need to bzero bind structure because mysql_stmt_bind_param checks all
  3900. its members.
  3901. */
  3902. bzero((char*) my_bind, sizeof(my_bind));
  3903. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  3904. my_bind[0].buffer= (void *) &nData; /* integer data */
  3905. my_bind[0].length= &length;
  3906. my_bind[0].is_null= 0;
  3907. strmov((char *)query , "SELECT col1 FROM test_store_result where col1= ?");
  3908. stmt= mysql_simple_prepare(mysql, query);
  3909. check_stmt(stmt);
  3910. rc= mysql_stmt_bind_param(stmt, my_bind);
  3911. check_execute(stmt, rc);
  3912. rc= mysql_stmt_bind_result(stmt, my_bind);
  3913. check_execute(stmt, rc);
  3914. nData= 10; length= 0;
  3915. rc= mysql_stmt_execute(stmt);
  3916. check_execute(stmt, rc);
  3917. nData= 0;
  3918. rc= mysql_stmt_store_result(stmt);
  3919. check_execute(stmt, rc);
  3920. rc= mysql_stmt_fetch(stmt);
  3921. check_execute(stmt, rc);
  3922. if (!opt_silent)
  3923. fprintf(stdout, "\n row 1: %d", nData);
  3924. DIE_UNLESS(nData == 10);
  3925. rc= mysql_stmt_fetch(stmt);
  3926. DIE_UNLESS(rc == MYSQL_NO_DATA);
  3927. nData= 20;
  3928. rc= mysql_stmt_execute(stmt);
  3929. check_execute(stmt, rc);
  3930. nData= 0;
  3931. rc= mysql_stmt_store_result(stmt);
  3932. check_execute(stmt, rc);
  3933. rc= mysql_stmt_fetch(stmt);
  3934. check_execute(stmt, rc);
  3935. if (!opt_silent)
  3936. fprintf(stdout, "\n row 1: %d", nData);
  3937. DIE_UNLESS(nData == 20);
  3938. rc= mysql_stmt_fetch(stmt);
  3939. DIE_UNLESS(rc == MYSQL_NO_DATA);
  3940. mysql_stmt_close(stmt);
  3941. }
  3942. /* Test simple subselect prepare */
  3943. static void test_subselect()
  3944. {
  3945. MYSQL_STMT *stmt;
  3946. int rc, id;
  3947. MYSQL_BIND my_bind[1];
  3948. DBUG_ENTER("test_subselect");
  3949. myheader("test_subselect");
  3950. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sub1");
  3951. myquery(rc);
  3952. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sub2");
  3953. myquery(rc);
  3954. rc= mysql_query(mysql, "CREATE TABLE test_sub1(id int)");
  3955. myquery(rc);
  3956. rc= mysql_query(mysql, "CREATE TABLE test_sub2(id int, id1 int)");
  3957. myquery(rc);
  3958. rc= mysql_query(mysql, "INSERT INTO test_sub1 values(2)");
  3959. myquery(rc);
  3960. rc= mysql_query(mysql, "INSERT INTO test_sub2 VALUES(1, 7), (2, 7)");
  3961. myquery(rc);
  3962. rc= mysql_commit(mysql);
  3963. myquery(rc);
  3964. /* fetch */
  3965. /*
  3966. We need to bzero bind structure because mysql_stmt_bind_param checks all
  3967. its members.
  3968. */
  3969. bzero((char*) my_bind, sizeof(my_bind));
  3970. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  3971. my_bind[0].buffer= (void *) &id;
  3972. my_bind[0].length= 0;
  3973. my_bind[0].is_null= 0;
  3974. stmt= mysql_simple_prepare(mysql, "INSERT INTO test_sub2(id) SELECT * FROM test_sub1 WHERE id= ?");
  3975. check_stmt(stmt);
  3976. rc= mysql_stmt_bind_param(stmt, my_bind);
  3977. check_execute(stmt, rc);
  3978. id= 2;
  3979. rc= mysql_stmt_execute(stmt);
  3980. check_execute(stmt, rc);
  3981. verify_st_affected_rows(stmt, 1);
  3982. id= 9;
  3983. rc= mysql_stmt_execute(stmt);
  3984. check_execute(stmt, rc);
  3985. verify_st_affected_rows(stmt, 0);
  3986. mysql_stmt_close(stmt);
  3987. rc= my_stmt_result("SELECT * FROM test_sub2");
  3988. DIE_UNLESS(rc == 3);
  3989. rc= my_stmt_result("SELECT ROW(1, 7) IN (select id, id1 "
  3990. "from test_sub2 WHERE id1= 8)");
  3991. DIE_UNLESS(rc == 1);
  3992. rc= my_stmt_result("SELECT ROW(1, 7) IN (select id, id1 "
  3993. "from test_sub2 WHERE id1= 7)");
  3994. DIE_UNLESS(rc == 1);
  3995. stmt= mysql_simple_prepare(mysql, ("SELECT ROW(1, 7) IN (select id, id1 "
  3996. "from test_sub2 WHERE id1= ?)"));
  3997. check_stmt(stmt);
  3998. rc= mysql_stmt_bind_param(stmt, my_bind);
  3999. check_execute(stmt, rc);
  4000. rc= mysql_stmt_bind_result(stmt, my_bind);
  4001. check_execute(stmt, rc);
  4002. id= 7;
  4003. rc= mysql_stmt_execute(stmt);
  4004. check_execute(stmt, rc);
  4005. rc= mysql_stmt_fetch(stmt);
  4006. check_execute(stmt, rc);
  4007. if (!opt_silent)
  4008. fprintf(stdout, "\n row 1: %d", id);
  4009. DIE_UNLESS(id == 1);
  4010. rc= mysql_stmt_fetch(stmt);
  4011. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4012. id= 8;
  4013. rc= mysql_stmt_execute(stmt);
  4014. check_execute(stmt, rc);
  4015. rc= mysql_stmt_fetch(stmt);
  4016. check_execute(stmt, rc);
  4017. if (!opt_silent)
  4018. fprintf(stdout, "\n row 1: %d", id);
  4019. DIE_UNLESS(id == 0);
  4020. rc= mysql_stmt_fetch(stmt);
  4021. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4022. mysql_stmt_close(stmt);
  4023. DBUG_VOID_RETURN;
  4024. }
  4025. /*
  4026. Generalized conversion routine to handle DATE, TIME and DATETIME
  4027. conversion using MYSQL_TIME structure
  4028. */
  4029. static void test_bind_date_conv(uint row_count)
  4030. {
  4031. MYSQL_STMT *stmt= 0;
  4032. uint rc, i, count= row_count;
  4033. ulong length[4];
  4034. MYSQL_BIND my_bind[4];
  4035. my_bool is_null[4]= {0};
  4036. MYSQL_TIME tm[4];
  4037. ulong second_part;
  4038. uint year, month, day, hour, minute, sec;
  4039. stmt= mysql_simple_prepare(mysql, "INSERT INTO test_date VALUES(?, ?, ?, ?)");
  4040. check_stmt(stmt);
  4041. verify_param_count(stmt, 4);
  4042. /*
  4043. We need to bzero bind structure because mysql_stmt_bind_param checks all
  4044. its members.
  4045. */
  4046. bzero((char*) my_bind, sizeof(my_bind));
  4047. my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
  4048. my_bind[1].buffer_type= MYSQL_TYPE_TIME;
  4049. my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
  4050. my_bind[3].buffer_type= MYSQL_TYPE_DATE;
  4051. for (i= 0; i < (int) array_elements(my_bind); i++)
  4052. {
  4053. my_bind[i].buffer= (void *) &tm[i];
  4054. my_bind[i].is_null= &is_null[i];
  4055. my_bind[i].length= &length[i];
  4056. my_bind[i].buffer_length= 30;
  4057. length[i]= 20;
  4058. }
  4059. second_part= 0;
  4060. year= 2000;
  4061. month= 01;
  4062. day= 10;
  4063. hour= 11;
  4064. minute= 16;
  4065. sec= 20;
  4066. rc= mysql_stmt_bind_param(stmt, my_bind);
  4067. check_execute(stmt, rc);
  4068. for (count= 0; count < row_count; count++)
  4069. {
  4070. for (i= 0; i < (int) array_elements(my_bind); i++)
  4071. {
  4072. tm[i].neg= 0;
  4073. tm[i].second_part= second_part+count;
  4074. if (my_bind[i].buffer_type != MYSQL_TYPE_TIME)
  4075. {
  4076. tm[i].year= year+count;
  4077. tm[i].month= month+count;
  4078. tm[i].day= day+count;
  4079. }
  4080. else
  4081. tm[i].year= tm[i].month= tm[i].day= 0;
  4082. if (my_bind[i].buffer_type != MYSQL_TYPE_DATE)
  4083. {
  4084. tm[i].hour= hour+count;
  4085. tm[i].minute= minute+count;
  4086. tm[i].second= sec+count;
  4087. }
  4088. else
  4089. tm[i].hour= tm[i].minute= tm[i].second= 0;
  4090. }
  4091. rc= mysql_stmt_execute(stmt);
  4092. check_execute(stmt, rc);
  4093. }
  4094. rc= mysql_commit(mysql);
  4095. myquery(rc);
  4096. mysql_stmt_close(stmt);
  4097. rc= my_stmt_result("SELECT * FROM test_date");
  4098. DIE_UNLESS(row_count == rc);
  4099. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_date");
  4100. check_stmt(stmt);
  4101. rc= mysql_stmt_bind_result(stmt, my_bind);
  4102. check_execute(stmt, rc);
  4103. rc= mysql_stmt_execute(stmt);
  4104. check_execute(stmt, rc);
  4105. rc= mysql_stmt_store_result(stmt);
  4106. check_execute(stmt, rc);
  4107. for (count= 0; count < row_count; count++)
  4108. {
  4109. rc= mysql_stmt_fetch(stmt);
  4110. DIE_UNLESS(rc == 0 || rc == MYSQL_DATA_TRUNCATED);
  4111. if (!opt_silent)
  4112. fprintf(stdout, "\n");
  4113. for (i= 0; i < array_elements(my_bind); i++)
  4114. {
  4115. if (!opt_silent)
  4116. fprintf(stdout, "\ntime[%d]: %02d-%02d-%02d %02d:%02d:%02d.%02lu",
  4117. i, tm[i].year, tm[i].month, tm[i].day,
  4118. tm[i].hour, tm[i].minute, tm[i].second,
  4119. tm[i].second_part);
  4120. DIE_UNLESS(tm[i].year == 0 || tm[i].year == year+count);
  4121. DIE_UNLESS(tm[i].month == 0 || tm[i].month == month+count);
  4122. DIE_UNLESS(tm[i].day == 0 || tm[i].day == day+count);
  4123. DIE_UNLESS(tm[i].hour == 0 || tm[i].hour == hour+count);
  4124. DIE_UNLESS(tm[i].minute == 0 || tm[i].minute == minute+count);
  4125. DIE_UNLESS(tm[i].second == 0 || tm[i].second == sec+count);
  4126. DIE_UNLESS(tm[i].second_part == 0 ||
  4127. tm[i].second_part == second_part+count);
  4128. }
  4129. }
  4130. rc= mysql_stmt_fetch(stmt);
  4131. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4132. mysql_stmt_close(stmt);
  4133. }
  4134. /* Test DATE, TIME, DATETIME and TS with MYSQL_TIME conversion */
  4135. static void test_date()
  4136. {
  4137. int rc;
  4138. myheader("test_date");
  4139. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
  4140. myquery(rc);
  4141. rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP(14), \
  4142. c2 TIME, \
  4143. c3 DATETIME, \
  4144. c4 DATE)");
  4145. myquery(rc);
  4146. test_bind_date_conv(5);
  4147. }
  4148. /* Test all time types to DATE and DATE to all types */
  4149. static void test_date_date()
  4150. {
  4151. int rc;
  4152. myheader("test_date_date");
  4153. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
  4154. myquery(rc);
  4155. rc= mysql_query(mysql, "CREATE TABLE test_date(c1 DATE, \
  4156. c2 DATE, \
  4157. c3 DATE, \
  4158. c4 DATE)");
  4159. myquery(rc);
  4160. test_bind_date_conv(3);
  4161. }
  4162. /* Test all time types to TIME and TIME to all types */
  4163. static void test_date_time()
  4164. {
  4165. int rc;
  4166. myheader("test_date_time");
  4167. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
  4168. myquery(rc);
  4169. rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIME, \
  4170. c2 TIME, \
  4171. c3 TIME, \
  4172. c4 TIME)");
  4173. myquery(rc);
  4174. test_bind_date_conv(3);
  4175. }
  4176. /* Test all time types to TIMESTAMP and TIMESTAMP to all types */
  4177. static void test_date_ts()
  4178. {
  4179. int rc;
  4180. myheader("test_date_ts");
  4181. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
  4182. myquery(rc);
  4183. rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP(10), \
  4184. c2 TIMESTAMP(14), \
  4185. c3 TIMESTAMP, \
  4186. c4 TIMESTAMP(6))");
  4187. myquery(rc);
  4188. test_bind_date_conv(2);
  4189. }
  4190. /* Test all time types to DATETIME and DATETIME to all types */
  4191. static void test_date_dt()
  4192. {
  4193. int rc;
  4194. myheader("test_date_dt");
  4195. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
  4196. myquery(rc);
  4197. rc= mysql_query(mysql, "CREATE TABLE test_date(c1 datetime, "
  4198. " c2 datetime, c3 datetime, c4 date)");
  4199. myquery(rc);
  4200. test_bind_date_conv(2);
  4201. }
  4202. /* Misc tests to keep pure coverage happy */
  4203. static void test_pure_coverage()
  4204. {
  4205. MYSQL_STMT *stmt;
  4206. MYSQL_BIND my_bind[1];
  4207. int rc;
  4208. ulong length;
  4209. myheader("test_pure_coverage");
  4210. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_pure");
  4211. myquery(rc);
  4212. rc= mysql_query(mysql, "CREATE TABLE test_pure(c1 int, c2 varchar(20))");
  4213. myquery(rc);
  4214. stmt= mysql_simple_prepare(mysql, "insert into test_pure(c67788) values(10)");
  4215. check_stmt_r(stmt);
  4216. /* Query without params and result should allow to bind 0 arrays */
  4217. stmt= mysql_simple_prepare(mysql, "insert into test_pure(c2) values(10)");
  4218. check_stmt(stmt);
  4219. rc= mysql_stmt_bind_param(stmt, (MYSQL_BIND*)0);
  4220. check_execute(stmt, rc);
  4221. rc= mysql_stmt_execute(stmt);
  4222. check_execute(stmt, rc);
  4223. rc= mysql_stmt_bind_result(stmt, (MYSQL_BIND*)0);
  4224. DIE_UNLESS(rc == 1);
  4225. mysql_stmt_close(stmt);
  4226. stmt= mysql_simple_prepare(mysql, "insert into test_pure(c2) values(?)");
  4227. check_stmt(stmt);
  4228. /*
  4229. We need to bzero bind structure because mysql_stmt_bind_param checks all
  4230. its members.
  4231. */
  4232. bzero((char*) my_bind, sizeof(my_bind));
  4233. my_bind[0].length= &length;
  4234. my_bind[0].is_null= 0;
  4235. my_bind[0].buffer_length= 0;
  4236. my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
  4237. rc= mysql_stmt_bind_param(stmt, my_bind);
  4238. check_execute_r(stmt, rc); /* unsupported buffer type */
  4239. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  4240. rc= mysql_stmt_bind_param(stmt, my_bind);
  4241. check_execute(stmt, rc);
  4242. rc= mysql_stmt_store_result(stmt);
  4243. check_execute(stmt, rc);
  4244. mysql_stmt_close(stmt);
  4245. stmt= mysql_simple_prepare(mysql, "select * from test_pure");
  4246. check_execute(stmt, rc);
  4247. rc= mysql_stmt_execute(stmt);
  4248. check_execute(stmt, rc);
  4249. my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
  4250. rc= mysql_stmt_bind_result(stmt, my_bind);
  4251. check_execute_r(stmt, rc); /* unsupported buffer type */
  4252. rc= mysql_stmt_store_result(stmt);
  4253. DIE_UNLESS(rc);
  4254. rc= mysql_stmt_store_result(stmt);
  4255. DIE_UNLESS(rc); /* Old error must be reset first */
  4256. mysql_stmt_close(stmt);
  4257. mysql_query(mysql, "DROP TABLE test_pure");
  4258. }
  4259. /* Test for string buffer fetch */
  4260. static void test_buffers()
  4261. {
  4262. MYSQL_STMT *stmt;
  4263. MYSQL_BIND my_bind[1];
  4264. int rc;
  4265. ulong length;
  4266. my_bool is_null;
  4267. char buffer[20];
  4268. myheader("test_buffers");
  4269. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_buffer");
  4270. myquery(rc);
  4271. rc= mysql_query(mysql, "CREATE TABLE test_buffer(str varchar(20))");
  4272. myquery(rc);
  4273. rc= mysql_query(mysql, "insert into test_buffer values('MySQL')\
  4274. , ('Database'), ('Open-Source'), ('Popular')");
  4275. myquery(rc);
  4276. stmt= mysql_simple_prepare(mysql, "select str from test_buffer");
  4277. check_stmt(stmt);
  4278. rc= mysql_stmt_execute(stmt);
  4279. check_execute(stmt, rc);
  4280. bzero(buffer, sizeof(buffer)); /* Avoid overruns in printf() */
  4281. bzero((char*) my_bind, sizeof(my_bind));
  4282. my_bind[0].length= &length;
  4283. my_bind[0].is_null= &is_null;
  4284. my_bind[0].buffer_length= 1;
  4285. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  4286. my_bind[0].buffer= (void *)buffer;
  4287. my_bind[0].error= &my_bind[0].error_value;
  4288. rc= mysql_stmt_bind_result(stmt, my_bind);
  4289. check_execute(stmt, rc);
  4290. rc= mysql_stmt_store_result(stmt);
  4291. check_execute(stmt, rc);
  4292. buffer[1]= 'X';
  4293. rc= mysql_stmt_fetch(stmt);
  4294. DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
  4295. DIE_UNLESS(my_bind[0].error_value);
  4296. if (!opt_silent)
  4297. fprintf(stdout, "\n data: %s (%lu)", buffer, length);
  4298. DIE_UNLESS(buffer[0] == 'M');
  4299. DIE_UNLESS(buffer[1] == 'X');
  4300. DIE_UNLESS(length == 5);
  4301. my_bind[0].buffer_length= 8;
  4302. rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
  4303. check_execute(stmt, rc);
  4304. rc= mysql_stmt_fetch(stmt);
  4305. check_execute(stmt, rc);
  4306. if (!opt_silent)
  4307. fprintf(stdout, "\n data: %s (%lu)", buffer, length);
  4308. DIE_UNLESS(strncmp(buffer, "Database", 8) == 0);
  4309. DIE_UNLESS(length == 8);
  4310. my_bind[0].buffer_length= 12;
  4311. rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
  4312. check_execute(stmt, rc);
  4313. rc= mysql_stmt_fetch(stmt);
  4314. check_execute(stmt, rc);
  4315. if (!opt_silent)
  4316. fprintf(stdout, "\n data: %s (%lu)", buffer, length);
  4317. DIE_UNLESS(strcmp(buffer, "Open-Source") == 0);
  4318. DIE_UNLESS(length == 11);
  4319. my_bind[0].buffer_length= 6;
  4320. rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
  4321. check_execute(stmt, rc);
  4322. rc= mysql_stmt_fetch(stmt);
  4323. DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
  4324. DIE_UNLESS(my_bind[0].error_value);
  4325. if (!opt_silent)
  4326. fprintf(stdout, "\n data: %s (%lu)", buffer, length);
  4327. DIE_UNLESS(strncmp(buffer, "Popula", 6) == 0);
  4328. DIE_UNLESS(length == 7);
  4329. mysql_stmt_close(stmt);
  4330. }
  4331. /* Test the direct query execution in the middle of open stmts */
  4332. static void test_open_direct()
  4333. {
  4334. MYSQL_STMT *stmt;
  4335. MYSQL_RES *result;
  4336. int rc;
  4337. myheader("test_open_direct");
  4338. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_open_direct");
  4339. myquery(rc);
  4340. rc= mysql_query(mysql, "CREATE TABLE test_open_direct(id int, name char(6))");
  4341. myquery(rc);
  4342. stmt= mysql_simple_prepare(mysql, "INSERT INTO test_open_direct values(10, 'mysql')");
  4343. check_stmt(stmt);
  4344. rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
  4345. myquery(rc);
  4346. result= mysql_store_result(mysql);
  4347. mytest(result);
  4348. rc= my_process_result_set(result);
  4349. DIE_UNLESS(rc == 0);
  4350. mysql_free_result(result);
  4351. rc= mysql_stmt_execute(stmt);
  4352. check_execute(stmt, rc);
  4353. verify_st_affected_rows(stmt, 1);
  4354. rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
  4355. myquery(rc);
  4356. result= mysql_store_result(mysql);
  4357. mytest(result);
  4358. rc= my_process_result_set(result);
  4359. DIE_UNLESS(rc == 1);
  4360. mysql_free_result(result);
  4361. rc= mysql_stmt_execute(stmt);
  4362. check_execute(stmt, rc);
  4363. verify_st_affected_rows(stmt, 1);
  4364. rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
  4365. myquery(rc);
  4366. result= mysql_store_result(mysql);
  4367. mytest(result);
  4368. rc= my_process_result_set(result);
  4369. DIE_UNLESS(rc == 2);
  4370. mysql_free_result(result);
  4371. mysql_stmt_close(stmt);
  4372. /* run a direct query in the middle of a fetch */
  4373. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_open_direct");
  4374. check_stmt(stmt);
  4375. rc= mysql_stmt_execute(stmt);
  4376. check_execute(stmt, rc);
  4377. rc= mysql_stmt_fetch(stmt);
  4378. check_execute(stmt, rc);
  4379. rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
  4380. myquery_r(rc);
  4381. rc= mysql_stmt_close(stmt);
  4382. check_execute(stmt, rc);
  4383. rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
  4384. myquery(rc);
  4385. /* run a direct query with store result */
  4386. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_open_direct");
  4387. check_stmt(stmt);
  4388. rc= mysql_stmt_execute(stmt);
  4389. check_execute(stmt, rc);
  4390. rc= mysql_stmt_store_result(stmt);
  4391. check_execute(stmt, rc);
  4392. rc= mysql_stmt_fetch(stmt);
  4393. check_execute(stmt, rc);
  4394. rc= mysql_query(mysql, "drop table test_open_direct");
  4395. myquery(rc);
  4396. rc= mysql_stmt_close(stmt);
  4397. check_execute(stmt, rc);
  4398. }
  4399. /* Test fetch without prior bound buffers */
  4400. static void test_fetch_nobuffs()
  4401. {
  4402. MYSQL_STMT *stmt;
  4403. MYSQL_BIND my_bind[4];
  4404. char str[4][50];
  4405. int rc;
  4406. myheader("test_fetch_nobuffs");
  4407. stmt= mysql_simple_prepare(mysql, "SELECT DATABASE(), CURRENT_USER(), \
  4408. CURRENT_DATE(), CURRENT_TIME()");
  4409. check_stmt(stmt);
  4410. rc= mysql_stmt_execute(stmt);
  4411. check_execute(stmt, rc);
  4412. rc= 0;
  4413. while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
  4414. rc++;
  4415. if (!opt_silent)
  4416. fprintf(stdout, "\n total rows : %d", rc);
  4417. DIE_UNLESS(rc == 1);
  4418. bzero((char*) my_bind, sizeof(MYSQL_BIND));
  4419. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  4420. my_bind[0].buffer= (void *)str[0];
  4421. my_bind[0].buffer_length= sizeof(str[0]);
  4422. my_bind[1]= my_bind[2]= my_bind[3]= my_bind[0];
  4423. my_bind[1].buffer= (void *)str[1];
  4424. my_bind[2].buffer= (void *)str[2];
  4425. my_bind[3].buffer= (void *)str[3];
  4426. rc= mysql_stmt_bind_result(stmt, my_bind);
  4427. check_execute(stmt, rc);
  4428. rc= mysql_stmt_execute(stmt);
  4429. check_execute(stmt, rc);
  4430. rc= 0;
  4431. while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
  4432. {
  4433. rc++;
  4434. if (!opt_silent)
  4435. {
  4436. fprintf(stdout, "\n CURRENT_DATABASE(): %s", str[0]);
  4437. fprintf(stdout, "\n CURRENT_USER() : %s", str[1]);
  4438. fprintf(stdout, "\n CURRENT_DATE() : %s", str[2]);
  4439. fprintf(stdout, "\n CURRENT_TIME() : %s", str[3]);
  4440. }
  4441. }
  4442. if (!opt_silent)
  4443. fprintf(stdout, "\n total rows : %d", rc);
  4444. DIE_UNLESS(rc == 1);
  4445. mysql_stmt_close(stmt);
  4446. }
  4447. /* Test a misc bug */
  4448. static void test_ushort_bug()
  4449. {
  4450. MYSQL_STMT *stmt;
  4451. MYSQL_BIND my_bind[4];
  4452. ushort short_value;
  4453. uint32 long_value;
  4454. ulong s_length, l_length, ll_length, t_length;
  4455. ulonglong longlong_value;
  4456. int rc;
  4457. uchar tiny_value;
  4458. char llbuf[22];
  4459. myheader("test_ushort_bug");
  4460. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ushort");
  4461. myquery(rc);
  4462. rc= mysql_query(mysql, "CREATE TABLE test_ushort(a smallint unsigned, \
  4463. b smallint unsigned, \
  4464. c smallint unsigned, \
  4465. d smallint unsigned)");
  4466. myquery(rc);
  4467. rc= mysql_query(mysql,
  4468. "INSERT INTO test_ushort VALUES(35999, 35999, 35999, 200)");
  4469. myquery(rc);
  4470. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_ushort");
  4471. check_stmt(stmt);
  4472. rc= mysql_stmt_execute(stmt);
  4473. check_execute(stmt, rc);
  4474. bzero((char*) my_bind, sizeof(my_bind));
  4475. my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
  4476. my_bind[0].buffer= (void *)&short_value;
  4477. my_bind[0].is_unsigned= TRUE;
  4478. my_bind[0].length= &s_length;
  4479. my_bind[1].buffer_type= MYSQL_TYPE_LONG;
  4480. my_bind[1].buffer= (void *)&long_value;
  4481. my_bind[1].length= &l_length;
  4482. my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
  4483. my_bind[2].buffer= (void *)&longlong_value;
  4484. my_bind[2].length= &ll_length;
  4485. my_bind[3].buffer_type= MYSQL_TYPE_TINY;
  4486. my_bind[3].buffer= (void *)&tiny_value;
  4487. my_bind[3].is_unsigned= TRUE;
  4488. my_bind[3].length= &t_length;
  4489. rc= mysql_stmt_bind_result(stmt, my_bind);
  4490. check_execute(stmt, rc);
  4491. rc= mysql_stmt_fetch(stmt);
  4492. check_execute(stmt, rc);
  4493. if (!opt_silent)
  4494. {
  4495. fprintf(stdout, "\n ushort : %d (%ld)", short_value, s_length);
  4496. fprintf(stdout, "\n ulong : %lu (%ld)", (ulong) long_value, l_length);
  4497. fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
  4498. ll_length);
  4499. fprintf(stdout, "\n tinyint : %d (%ld)", tiny_value, t_length);
  4500. }
  4501. DIE_UNLESS(short_value == 35999);
  4502. DIE_UNLESS(s_length == 2);
  4503. DIE_UNLESS(long_value == 35999);
  4504. DIE_UNLESS(l_length == 4);
  4505. DIE_UNLESS(longlong_value == 35999);
  4506. DIE_UNLESS(ll_length == 8);
  4507. DIE_UNLESS(tiny_value == 200);
  4508. DIE_UNLESS(t_length == 1);
  4509. rc= mysql_stmt_fetch(stmt);
  4510. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4511. mysql_stmt_close(stmt);
  4512. }
  4513. /* Test a misc smallint-signed conversion bug */
  4514. static void test_sshort_bug()
  4515. {
  4516. MYSQL_STMT *stmt;
  4517. MYSQL_BIND my_bind[4];
  4518. short short_value;
  4519. int32 long_value;
  4520. ulong s_length, l_length, ll_length, t_length;
  4521. ulonglong longlong_value;
  4522. int rc;
  4523. uchar tiny_value;
  4524. char llbuf[22];
  4525. myheader("test_sshort_bug");
  4526. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sshort");
  4527. myquery(rc);
  4528. rc= mysql_query(mysql, "CREATE TABLE test_sshort(a smallint signed, \
  4529. b smallint signed, \
  4530. c smallint unsigned, \
  4531. d smallint unsigned)");
  4532. myquery(rc);
  4533. rc= mysql_query(mysql, "INSERT INTO test_sshort VALUES(-5999, -5999, 35999, 200)");
  4534. myquery(rc);
  4535. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_sshort");
  4536. check_stmt(stmt);
  4537. rc= mysql_stmt_execute(stmt);
  4538. check_execute(stmt, rc);
  4539. bzero((char*) my_bind, sizeof(my_bind));
  4540. my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
  4541. my_bind[0].buffer= (void *)&short_value;
  4542. my_bind[0].length= &s_length;
  4543. my_bind[1].buffer_type= MYSQL_TYPE_LONG;
  4544. my_bind[1].buffer= (void *)&long_value;
  4545. my_bind[1].length= &l_length;
  4546. my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
  4547. my_bind[2].buffer= (void *)&longlong_value;
  4548. my_bind[2].length= &ll_length;
  4549. my_bind[3].buffer_type= MYSQL_TYPE_TINY;
  4550. my_bind[3].buffer= (void *)&tiny_value;
  4551. my_bind[3].is_unsigned= TRUE;
  4552. my_bind[3].length= &t_length;
  4553. rc= mysql_stmt_bind_result(stmt, my_bind);
  4554. check_execute(stmt, rc);
  4555. rc= mysql_stmt_fetch(stmt);
  4556. check_execute(stmt, rc);
  4557. if (!opt_silent)
  4558. {
  4559. fprintf(stdout, "\n sshort : %d (%ld)", short_value, s_length);
  4560. fprintf(stdout, "\n slong : %ld (%ld)", (long) long_value, l_length);
  4561. fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
  4562. ll_length);
  4563. fprintf(stdout, "\n tinyint : %d (%ld)", tiny_value, t_length);
  4564. }
  4565. DIE_UNLESS(short_value == -5999);
  4566. DIE_UNLESS(s_length == 2);
  4567. DIE_UNLESS(long_value == -5999);
  4568. DIE_UNLESS(l_length == 4);
  4569. DIE_UNLESS(longlong_value == 35999);
  4570. DIE_UNLESS(ll_length == 8);
  4571. DIE_UNLESS(tiny_value == 200);
  4572. DIE_UNLESS(t_length == 1);
  4573. rc= mysql_stmt_fetch(stmt);
  4574. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4575. mysql_stmt_close(stmt);
  4576. }
  4577. /* Test a misc tinyint-signed conversion bug */
  4578. static void test_stiny_bug()
  4579. {
  4580. MYSQL_STMT *stmt;
  4581. MYSQL_BIND my_bind[4];
  4582. short short_value;
  4583. int32 long_value;
  4584. ulong s_length, l_length, ll_length, t_length;
  4585. ulonglong longlong_value;
  4586. int rc;
  4587. uchar tiny_value;
  4588. char llbuf[22];
  4589. myheader("test_stiny_bug");
  4590. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stiny");
  4591. myquery(rc);
  4592. rc= mysql_query(mysql, "CREATE TABLE test_stiny(a tinyint signed, \
  4593. b tinyint signed, \
  4594. c tinyint unsigned, \
  4595. d tinyint unsigned)");
  4596. myquery(rc);
  4597. rc= mysql_query(mysql, "INSERT INTO test_stiny VALUES(-128, -127, 255, 0)");
  4598. myquery(rc);
  4599. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_stiny");
  4600. check_stmt(stmt);
  4601. rc= mysql_stmt_execute(stmt);
  4602. check_execute(stmt, rc);
  4603. bzero((char*) my_bind, sizeof(my_bind));
  4604. my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
  4605. my_bind[0].buffer= (void *)&short_value;
  4606. my_bind[0].length= &s_length;
  4607. my_bind[1].buffer_type= MYSQL_TYPE_LONG;
  4608. my_bind[1].buffer= (void *)&long_value;
  4609. my_bind[1].length= &l_length;
  4610. my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
  4611. my_bind[2].buffer= (void *)&longlong_value;
  4612. my_bind[2].length= &ll_length;
  4613. my_bind[3].buffer_type= MYSQL_TYPE_TINY;
  4614. my_bind[3].buffer= (void *)&tiny_value;
  4615. my_bind[3].length= &t_length;
  4616. rc= mysql_stmt_bind_result(stmt, my_bind);
  4617. check_execute(stmt, rc);
  4618. rc= mysql_stmt_fetch(stmt);
  4619. check_execute(stmt, rc);
  4620. if (!opt_silent)
  4621. {
  4622. fprintf(stdout, "\n sshort : %d (%ld)", short_value, s_length);
  4623. fprintf(stdout, "\n slong : %ld (%ld)", (long) long_value, l_length);
  4624. fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
  4625. ll_length);
  4626. fprintf(stdout, "\n tinyint : %d (%ld)", tiny_value, t_length);
  4627. }
  4628. DIE_UNLESS(short_value == -128);
  4629. DIE_UNLESS(s_length == 2);
  4630. DIE_UNLESS(long_value == -127);
  4631. DIE_UNLESS(l_length == 4);
  4632. DIE_UNLESS(longlong_value == 255);
  4633. DIE_UNLESS(ll_length == 8);
  4634. DIE_UNLESS(tiny_value == 0);
  4635. DIE_UNLESS(t_length == 1);
  4636. rc= mysql_stmt_fetch(stmt);
  4637. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4638. mysql_stmt_close(stmt);
  4639. }
  4640. /* Test misc field information, bug: #74 */
  4641. static void test_field_misc()
  4642. {
  4643. MYSQL_STMT *stmt;
  4644. MYSQL_RES *result;
  4645. int rc;
  4646. myheader("test_field_misc");
  4647. rc= mysql_query(mysql, "SELECT @@autocommit");
  4648. myquery(rc);
  4649. result= mysql_store_result(mysql);
  4650. mytest(result);
  4651. rc= my_process_result_set(result);
  4652. DIE_UNLESS(rc == 1);
  4653. verify_prepare_field(result, 0,
  4654. "@@autocommit", "", /* field and its org name */
  4655. MYSQL_TYPE_LONGLONG, /* field type */
  4656. "", "", /* table and its org name */
  4657. "", 1, 0); /* db name, length(its bool flag)*/
  4658. mysql_free_result(result);
  4659. stmt= mysql_simple_prepare(mysql, "SELECT @@autocommit");
  4660. check_stmt(stmt);
  4661. rc= mysql_stmt_execute(stmt);
  4662. check_execute(stmt, rc);
  4663. result= mysql_stmt_result_metadata(stmt);
  4664. mytest(result);
  4665. rc= my_process_stmt_result(stmt);
  4666. DIE_UNLESS(rc == 1);
  4667. verify_prepare_field(result, 0,
  4668. "@@autocommit", "", /* field and its org name */
  4669. MYSQL_TYPE_LONGLONG, /* field type */
  4670. "", "", /* table and its org name */
  4671. "", 1, 0); /* db name, length(its bool flag)*/
  4672. mysql_free_result(result);
  4673. mysql_stmt_close(stmt);
  4674. stmt= mysql_simple_prepare(mysql, "SELECT @@max_error_count");
  4675. check_stmt(stmt);
  4676. result= mysql_stmt_result_metadata(stmt);
  4677. mytest(result);
  4678. rc= mysql_stmt_execute(stmt);
  4679. check_execute(stmt, rc);
  4680. rc= my_process_stmt_result(stmt);
  4681. DIE_UNLESS(rc == 1);
  4682. verify_prepare_field(result, 0,
  4683. "@@max_error_count", "", /* field and its org name */
  4684. MYSQL_TYPE_LONGLONG, /* field type */
  4685. "", "", /* table and its org name */
  4686. /* db name, length */
  4687. "", MY_INT64_NUM_DECIMAL_DIGITS , 0);
  4688. mysql_free_result(result);
  4689. mysql_stmt_close(stmt);
  4690. stmt= mysql_simple_prepare(mysql, "SELECT @@max_allowed_packet");
  4691. check_stmt(stmt);
  4692. result= mysql_stmt_result_metadata(stmt);
  4693. mytest(result);
  4694. rc= mysql_stmt_execute(stmt);
  4695. check_execute(stmt, rc);
  4696. DIE_UNLESS(1 == my_process_stmt_result(stmt));
  4697. verify_prepare_field(result, 0,
  4698. "@@max_allowed_packet", "", /* field and its org name */
  4699. MYSQL_TYPE_LONGLONG, /* field type */
  4700. "", "", /* table and its org name */
  4701. /* db name, length */
  4702. "", MY_INT64_NUM_DECIMAL_DIGITS, 0);
  4703. mysql_free_result(result);
  4704. mysql_stmt_close(stmt);
  4705. stmt= mysql_simple_prepare(mysql, "SELECT @@sql_warnings");
  4706. check_stmt(stmt);
  4707. result= mysql_stmt_result_metadata(stmt);
  4708. mytest(result);
  4709. rc= mysql_stmt_execute(stmt);
  4710. check_execute(stmt, rc);
  4711. rc= my_process_stmt_result(stmt);
  4712. DIE_UNLESS(rc == 1);
  4713. verify_prepare_field(result, 0,
  4714. "@@sql_warnings", "", /* field and its org name */
  4715. MYSQL_TYPE_LONGLONG, /* field type */
  4716. "", "", /* table and its org name */
  4717. "", 1, 0); /* db name, length */
  4718. mysql_free_result(result);
  4719. mysql_stmt_close(stmt);
  4720. }
  4721. /*
  4722. Test SET OPTION feature with prepare stmts
  4723. bug #85 (reported by mark@mysql.com)
  4724. */
  4725. static void test_set_option()
  4726. {
  4727. MYSQL_STMT *stmt;
  4728. MYSQL_RES *result;
  4729. int rc;
  4730. myheader("test_set_option");
  4731. mysql_autocommit(mysql, TRUE);
  4732. /* LIMIT the rows count to 2 */
  4733. rc= mysql_query(mysql, "SET OPTION SQL_SELECT_LIMIT= 2");
  4734. myquery(rc);
  4735. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_limit");
  4736. myquery(rc);
  4737. rc= mysql_query(mysql, "CREATE TABLE test_limit(a tinyint)");
  4738. myquery(rc);
  4739. rc= mysql_query(mysql, "INSERT INTO test_limit VALUES(10), (20), (30), (40)");
  4740. myquery(rc);
  4741. if (!opt_silent)
  4742. fprintf(stdout, "\n with SQL_SELECT_LIMIT= 2 (direct)");
  4743. rc= mysql_query(mysql, "SELECT * FROM test_limit");
  4744. myquery(rc);
  4745. result= mysql_store_result(mysql);
  4746. mytest(result);
  4747. rc= my_process_result_set(result);
  4748. DIE_UNLESS(rc == 2);
  4749. mysql_free_result(result);
  4750. if (!opt_silent)
  4751. fprintf(stdout, "\n with SQL_SELECT_LIMIT=2 (prepare)");
  4752. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_limit");
  4753. check_stmt(stmt);
  4754. rc= mysql_stmt_execute(stmt);
  4755. check_execute(stmt, rc);
  4756. rc= my_process_stmt_result(stmt);
  4757. DIE_UNLESS(rc == 2);
  4758. mysql_stmt_close(stmt);
  4759. /* RESET the LIMIT the rows count to 0 */
  4760. if (!opt_silent)
  4761. fprintf(stdout, "\n with SQL_SELECT_LIMIT=DEFAULT (prepare)");
  4762. rc= mysql_query(mysql, "SET OPTION SQL_SELECT_LIMIT=DEFAULT");
  4763. myquery(rc);
  4764. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_limit");
  4765. check_stmt(stmt);
  4766. rc= mysql_stmt_execute(stmt);
  4767. check_execute(stmt, rc);
  4768. rc= my_process_stmt_result(stmt);
  4769. DIE_UNLESS(rc == 4);
  4770. mysql_stmt_close(stmt);
  4771. }
  4772. /*
  4773. Test a misc GRANT option
  4774. bug #89 (reported by mark@mysql.com)
  4775. */
  4776. #ifndef EMBEDDED_LIBRARY
  4777. static void test_prepare_grant()
  4778. {
  4779. int rc;
  4780. char query[MAX_TEST_QUERY_LENGTH];
  4781. myheader("test_prepare_grant");
  4782. mysql_autocommit(mysql, TRUE);
  4783. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_grant");
  4784. myquery(rc);
  4785. rc= mysql_query(mysql, "CREATE TABLE test_grant(a tinyint primary key auto_increment)");
  4786. myquery(rc);
  4787. strxmov(query, "GRANT INSERT, UPDATE, SELECT ON ", current_db,
  4788. ".test_grant TO 'test_grant'@",
  4789. opt_host ? opt_host : "'localhost'", NullS);
  4790. if (mysql_query(mysql, query))
  4791. {
  4792. myerror("GRANT failed");
  4793. /*
  4794. If server started with --skip-grant-tables, skip this test, else
  4795. exit to indicate an error
  4796. ER_UNKNOWN_COM_ERROR= 1047
  4797. */
  4798. if (mysql_errno(mysql) != 1047)
  4799. exit(1);
  4800. }
  4801. else
  4802. {
  4803. MYSQL *org_mysql= mysql, *lmysql;
  4804. MYSQL_STMT *stmt;
  4805. if (!opt_silent)
  4806. fprintf(stdout, "\n Establishing a test connection ...");
  4807. if (!(lmysql= mysql_client_init(NULL)))
  4808. {
  4809. myerror("mysql_client_init() failed");
  4810. exit(1);
  4811. }
  4812. if (!(mysql_real_connect(lmysql, opt_host, "test_grant",
  4813. "", current_db, opt_port,
  4814. opt_unix_socket, 0)))
  4815. {
  4816. myerror("connection failed");
  4817. mysql_close(lmysql);
  4818. exit(1);
  4819. }
  4820. lmysql->reconnect= 1;
  4821. if (!opt_silent)
  4822. fprintf(stdout, "OK");
  4823. mysql= lmysql;
  4824. rc= mysql_query(mysql, "INSERT INTO test_grant VALUES(NULL)");
  4825. myquery(rc);
  4826. rc= mysql_query(mysql, "INSERT INTO test_grant(a) VALUES(NULL)");
  4827. myquery(rc);
  4828. execute_prepare_query("INSERT INTO test_grant(a) VALUES(NULL)", 1);
  4829. execute_prepare_query("INSERT INTO test_grant VALUES(NULL)", 1);
  4830. execute_prepare_query("UPDATE test_grant SET a=9 WHERE a=1", 1);
  4831. rc= my_stmt_result("SELECT a FROM test_grant");
  4832. DIE_UNLESS(rc == 4);
  4833. /* Both DELETE expected to fail as user does not have DELETE privs */
  4834. rc= mysql_query(mysql, "DELETE FROM test_grant");
  4835. myquery_r(rc);
  4836. stmt= mysql_simple_prepare(mysql, "DELETE FROM test_grant");
  4837. check_stmt_r(stmt);
  4838. rc= my_stmt_result("SELECT * FROM test_grant");
  4839. DIE_UNLESS(rc == 4);
  4840. mysql_close(lmysql);
  4841. mysql= org_mysql;
  4842. rc= mysql_query(mysql, "delete from mysql.user where User='test_grant'");
  4843. myquery(rc);
  4844. DIE_UNLESS(1 == mysql_affected_rows(mysql));
  4845. rc= mysql_query(mysql, "delete from mysql.tables_priv where User='test_grant'");
  4846. myquery(rc);
  4847. DIE_UNLESS(1 == mysql_affected_rows(mysql));
  4848. }
  4849. }
  4850. #endif /* EMBEDDED_LIBRARY */
  4851. /*
  4852. Test a crash when invalid/corrupted .frm is used in the
  4853. SHOW TABLE STATUS
  4854. bug #93 (reported by serg@mysql.com).
  4855. */
  4856. static void test_frm_bug()
  4857. {
  4858. MYSQL_STMT *stmt;
  4859. MYSQL_BIND my_bind[2];
  4860. MYSQL_RES *result;
  4861. MYSQL_ROW row;
  4862. FILE *test_file;
  4863. char data_dir[FN_REFLEN];
  4864. char test_frm[FN_REFLEN];
  4865. int rc;
  4866. myheader("test_frm_bug");
  4867. mysql_autocommit(mysql, TRUE);
  4868. rc= mysql_query(mysql, "drop table if exists test_frm_bug");
  4869. myquery(rc);
  4870. rc= mysql_query(mysql, "flush tables");
  4871. myquery(rc);
  4872. stmt= mysql_simple_prepare(mysql, "show variables like 'datadir'");
  4873. check_stmt(stmt);
  4874. rc= mysql_stmt_execute(stmt);
  4875. check_execute(stmt, rc);
  4876. bzero((char*) my_bind, sizeof(my_bind));
  4877. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  4878. my_bind[0].buffer= data_dir;
  4879. my_bind[0].buffer_length= FN_REFLEN;
  4880. my_bind[1]= my_bind[0];
  4881. rc= mysql_stmt_bind_result(stmt, my_bind);
  4882. check_execute(stmt, rc);
  4883. rc= mysql_stmt_fetch(stmt);
  4884. check_execute(stmt, rc);
  4885. if (!opt_silent)
  4886. fprintf(stdout, "\n data directory: %s", data_dir);
  4887. rc= mysql_stmt_fetch(stmt);
  4888. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4889. strxmov(test_frm, data_dir, "/", current_db, "/", "test_frm_bug.frm", NullS);
  4890. if (!opt_silent)
  4891. fprintf(stdout, "\n test_frm: %s", test_frm);
  4892. if (!(test_file= my_fopen(test_frm, (int) (O_RDWR | O_CREAT), MYF(MY_WME))))
  4893. {
  4894. fprintf(stdout, "\n ERROR: my_fopen failed for '%s'", test_frm);
  4895. fprintf(stdout, "\n test cancelled");
  4896. exit(1);
  4897. }
  4898. if (!opt_silent)
  4899. fprintf(test_file, "this is a junk file for test");
  4900. rc= mysql_query(mysql, "SHOW TABLE STATUS like 'test_frm_bug'");
  4901. myquery(rc);
  4902. result= mysql_store_result(mysql);
  4903. mytest(result);/* It can't be NULL */
  4904. rc= my_process_result_set(result);
  4905. DIE_UNLESS(rc == 1);
  4906. mysql_data_seek(result, 0);
  4907. row= mysql_fetch_row(result);
  4908. mytest(row);
  4909. if (!opt_silent)
  4910. fprintf(stdout, "\n Comment: %s", row[17]);
  4911. DIE_UNLESS(row[17] != 0);
  4912. mysql_free_result(result);
  4913. mysql_stmt_close(stmt);
  4914. my_fclose(test_file, MYF(0));
  4915. mysql_query(mysql, "drop table if exists test_frm_bug");
  4916. }
  4917. /* Test DECIMAL conversion */
  4918. static void test_decimal_bug()
  4919. {
  4920. MYSQL_STMT *stmt;
  4921. MYSQL_BIND my_bind[1];
  4922. char data[30];
  4923. int rc;
  4924. my_bool is_null;
  4925. myheader("test_decimal_bug");
  4926. mysql_autocommit(mysql, TRUE);
  4927. rc= mysql_query(mysql, "drop table if exists test_decimal_bug");
  4928. myquery(rc);
  4929. rc= mysql_query(mysql, "create table test_decimal_bug(c1 decimal(10, 2))");
  4930. myquery(rc);
  4931. rc= mysql_query(mysql, "insert into test_decimal_bug value(8), (10.22), (5.61)");
  4932. myquery(rc);
  4933. stmt= mysql_simple_prepare(mysql, "select c1 from test_decimal_bug where c1= ?");
  4934. check_stmt(stmt);
  4935. /*
  4936. We need to bzero bind structure because mysql_stmt_bind_param checks all
  4937. its members.
  4938. */
  4939. bzero((char*) my_bind, sizeof(my_bind));
  4940. my_bind[0].buffer_type= MYSQL_TYPE_NEWDECIMAL;
  4941. my_bind[0].buffer= (void *)data;
  4942. my_bind[0].buffer_length= 25;
  4943. my_bind[0].is_null= &is_null;
  4944. is_null= 0;
  4945. rc= mysql_stmt_bind_param(stmt, my_bind);
  4946. check_execute(stmt, rc);
  4947. strmov(data, "8.0");
  4948. rc= mysql_stmt_execute(stmt);
  4949. check_execute(stmt, rc);
  4950. data[0]= 0;
  4951. rc= mysql_stmt_bind_result(stmt, my_bind);
  4952. check_execute(stmt, rc);
  4953. rc= mysql_stmt_fetch(stmt);
  4954. check_execute(stmt, rc);
  4955. if (!opt_silent)
  4956. fprintf(stdout, "\n data: %s", data);
  4957. DIE_UNLESS(strcmp(data, "8.00") == 0);
  4958. rc= mysql_stmt_fetch(stmt);
  4959. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4960. strmov(data, "5.61");
  4961. rc= mysql_stmt_execute(stmt);
  4962. check_execute(stmt, rc);
  4963. data[0]= 0;
  4964. rc= mysql_stmt_bind_result(stmt, my_bind);
  4965. check_execute(stmt, rc);
  4966. rc= mysql_stmt_fetch(stmt);
  4967. check_execute(stmt, rc);
  4968. if (!opt_silent)
  4969. fprintf(stdout, "\n data: %s", data);
  4970. DIE_UNLESS(strcmp(data, "5.61") == 0);
  4971. rc= mysql_stmt_fetch(stmt);
  4972. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4973. is_null= 1;
  4974. rc= mysql_stmt_execute(stmt);
  4975. check_execute(stmt, rc);
  4976. rc= mysql_stmt_fetch(stmt);
  4977. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4978. strmov(data, "10.22"); is_null= 0;
  4979. rc= mysql_stmt_execute(stmt);
  4980. check_execute(stmt, rc);
  4981. data[0]= 0;
  4982. rc= mysql_stmt_bind_result(stmt, my_bind);
  4983. check_execute(stmt, rc);
  4984. rc= mysql_stmt_fetch(stmt);
  4985. check_execute(stmt, rc);
  4986. if (!opt_silent)
  4987. fprintf(stdout, "\n data: %s", data);
  4988. DIE_UNLESS(strcmp(data, "10.22") == 0);
  4989. rc= mysql_stmt_fetch(stmt);
  4990. DIE_UNLESS(rc == MYSQL_NO_DATA);
  4991. mysql_stmt_close(stmt);
  4992. }
  4993. /* Test EXPLAIN bug (#115, reported by mark@mysql.com & georg@php.net). */
  4994. static void test_explain_bug()
  4995. {
  4996. MYSQL_STMT *stmt;
  4997. MYSQL_RES *result;
  4998. int rc;
  4999. myheader("test_explain_bug");
  5000. mysql_autocommit(mysql, TRUE);
  5001. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_explain");
  5002. myquery(rc);
  5003. rc= mysql_query(mysql, "CREATE TABLE test_explain(id int, name char(2))");
  5004. myquery(rc);
  5005. stmt= mysql_simple_prepare(mysql, "explain test_explain");
  5006. check_stmt(stmt);
  5007. rc= mysql_stmt_execute(stmt);
  5008. check_execute(stmt, rc);
  5009. rc= my_process_stmt_result(stmt);
  5010. DIE_UNLESS(rc == 2);
  5011. result= mysql_stmt_result_metadata(stmt);
  5012. mytest(result);
  5013. if (!opt_silent)
  5014. fprintf(stdout, "\n total fields in the result: %d",
  5015. mysql_num_fields(result));
  5016. DIE_UNLESS(6 == mysql_num_fields(result));
  5017. verify_prepare_field(result, 0, "Field", "COLUMN_NAME",
  5018. mysql_get_server_version(mysql) <= 50000 ?
  5019. MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
  5020. 0, 0, "", 64, 0);
  5021. verify_prepare_field(result, 1, "Type", "COLUMN_TYPE", MYSQL_TYPE_BLOB,
  5022. 0, 0, "", 0, 0);
  5023. verify_prepare_field(result, 2, "Null", "IS_NULLABLE",
  5024. mysql_get_server_version(mysql) <= 50000 ?
  5025. MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
  5026. 0, 0, "", 3, 0);
  5027. verify_prepare_field(result, 3, "Key", "COLUMN_KEY",
  5028. mysql_get_server_version(mysql) <= 50000 ?
  5029. MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
  5030. 0, 0, "", 3, 0);
  5031. if ( mysql_get_server_version(mysql) >= 50027 )
  5032. {
  5033. /* The patch for bug#23037 changes column type of DEAULT to blob */
  5034. verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
  5035. MYSQL_TYPE_BLOB, 0, 0, "", 0, 0);
  5036. }
  5037. else
  5038. {
  5039. verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
  5040. mysql_get_server_version(mysql) >= 50027 ?
  5041. MYSQL_TYPE_BLOB :
  5042. mysql_get_server_version(mysql) <= 50000 ?
  5043. MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
  5044. 0, 0, "",
  5045. mysql_get_server_version(mysql) >= 50027 ? 0 :64, 0);
  5046. }
  5047. verify_prepare_field(result, 5, "Extra", "EXTRA",
  5048. mysql_get_server_version(mysql) <= 50000 ?
  5049. MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
  5050. 0, 0, "", 27, 0);
  5051. mysql_free_result(result);
  5052. mysql_stmt_close(stmt);
  5053. stmt= mysql_simple_prepare(mysql, "explain select id, name FROM test_explain");
  5054. check_stmt(stmt);
  5055. rc= mysql_stmt_execute(stmt);
  5056. check_execute(stmt, rc);
  5057. rc= my_process_stmt_result(stmt);
  5058. DIE_UNLESS(rc == 1);
  5059. result= mysql_stmt_result_metadata(stmt);
  5060. mytest(result);
  5061. if (!opt_silent)
  5062. fprintf(stdout, "\n total fields in the result: %d",
  5063. mysql_num_fields(result));
  5064. DIE_UNLESS(10 == mysql_num_fields(result));
  5065. verify_prepare_field(result, 0, "id", "", MYSQL_TYPE_LONGLONG,
  5066. "", "", "", 3, 0);
  5067. verify_prepare_field(result, 1, "select_type", "", MYSQL_TYPE_VAR_STRING,
  5068. "", "", "", 19, 0);
  5069. verify_prepare_field(result, 2, "table", "", MYSQL_TYPE_VAR_STRING,
  5070. "", "", "", NAME_CHAR_LEN, 0);
  5071. verify_prepare_field(result, 3, "type", "", MYSQL_TYPE_VAR_STRING,
  5072. "", "", "", 10, 0);
  5073. verify_prepare_field(result, 4, "possible_keys", "", MYSQL_TYPE_VAR_STRING,
  5074. "", "", "", NAME_CHAR_LEN*MAX_KEY, 0);
  5075. verify_prepare_field(result, 5, "key", "", MYSQL_TYPE_VAR_STRING,
  5076. "", "", "", NAME_CHAR_LEN, 0);
  5077. if (mysql_get_server_version(mysql) <= 50000)
  5078. {
  5079. verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_LONGLONG, "",
  5080. "", "", 3, 0);
  5081. }
  5082. else
  5083. {
  5084. verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_VAR_STRING, "",
  5085. "", "", NAME_CHAR_LEN*MAX_KEY, 0);
  5086. }
  5087. verify_prepare_field(result, 7, "ref", "", MYSQL_TYPE_VAR_STRING,
  5088. "", "", "", NAME_CHAR_LEN*16, 0);
  5089. verify_prepare_field(result, 8, "rows", "", MYSQL_TYPE_LONGLONG,
  5090. "", "", "", 10, 0);
  5091. verify_prepare_field(result, 9, "Extra", "", MYSQL_TYPE_VAR_STRING,
  5092. "", "", "", 255, 0);
  5093. mysql_free_result(result);
  5094. mysql_stmt_close(stmt);
  5095. }
  5096. #ifdef NOT_YET_WORKING
  5097. /*
  5098. Test math functions.
  5099. Bug #148 (reported by salle@mysql.com).
  5100. */
  5101. #define myerrno(n) check_errcode(n)
  5102. static void check_errcode(const unsigned int err)
  5103. {
  5104. if (!opt_silent || mysql_errno(mysql) != err)
  5105. {
  5106. if (mysql->server_version)
  5107. fprintf(stdout, "\n [MySQL-%s]", mysql->server_version);
  5108. else
  5109. fprintf(stdout, "\n [MySQL]");
  5110. fprintf(stdout, "[%d] %s\n", mysql_errno(mysql), mysql_error(mysql));
  5111. }
  5112. DIE_UNLESS(mysql_errno(mysql) == err);
  5113. }
  5114. static void test_drop_temp()
  5115. {
  5116. int rc;
  5117. myheader("test_drop_temp");
  5118. rc= mysql_query(mysql, "DROP DATABASE IF EXISTS test_drop_temp_db");
  5119. myquery(rc);
  5120. rc= mysql_query(mysql, "CREATE DATABASE test_drop_temp_db");
  5121. myquery(rc);
  5122. rc= mysql_query(mysql, "CREATE TABLE test_drop_temp_db.t1(c1 int, c2 char(1))");
  5123. myquery(rc);
  5124. rc= mysql_query(mysql, "delete from mysql.db where Db='test_drop_temp_db'");
  5125. myquery(rc);
  5126. rc= mysql_query(mysql, "delete from mysql.db where Db='test_drop_temp_db'");
  5127. myquery(rc);
  5128. strxmov(query, "GRANT SELECT, USAGE, DROP ON test_drop_temp_db.* TO test_temp@",
  5129. opt_host ? opt_host : "localhost", NullS);
  5130. if (mysql_query(mysql, query))
  5131. {
  5132. myerror("GRANT failed");
  5133. /*
  5134. If server started with --skip-grant-tables, skip this test, else
  5135. exit to indicate an error
  5136. ER_UNKNOWN_COM_ERROR= 1047
  5137. */
  5138. if (mysql_errno(mysql) != 1047)
  5139. exit(1);
  5140. }
  5141. else
  5142. {
  5143. MYSQL *org_mysql= mysql, *lmysql;
  5144. if (!opt_silent)
  5145. fprintf(stdout, "\n Establishing a test connection ...");
  5146. if (!(lmysql= mysql_client_init(NULL)))
  5147. {
  5148. myerror("mysql_client_init() failed");
  5149. exit(1);
  5150. }
  5151. rc= mysql_query(mysql, "flush privileges");
  5152. myquery(rc);
  5153. if (!(mysql_real_connect(lmysql, opt_host ? opt_host : "localhost", "test_temp",
  5154. "", "test_drop_temp_db", opt_port,
  5155. opt_unix_socket, 0)))
  5156. {
  5157. mysql= lmysql;
  5158. myerror("connection failed");
  5159. mysql_close(lmysql);
  5160. exit(1);
  5161. }
  5162. lmysql->reconnect= 1;
  5163. if (!opt_silent)
  5164. fprintf(stdout, "OK");
  5165. mysql= lmysql;
  5166. rc= mysql_query(mysql, "INSERT INTO t1 VALUES(10, 'C')");
  5167. myerrno((uint)1142);
  5168. rc= mysql_query(mysql, "DROP TABLE t1");
  5169. myerrno((uint)1142);
  5170. mysql= org_mysql;
  5171. rc= mysql_query(mysql, "CREATE TEMPORARY TABLE test_drop_temp_db.t1(c1 int)");
  5172. myquery(rc);
  5173. rc= mysql_query(mysql, "CREATE TEMPORARY TABLE test_drop_temp_db.t2 LIKE test_drop_temp_db.t1");
  5174. myquery(rc);
  5175. mysql= lmysql;
  5176. rc= mysql_query(mysql, "DROP TABLE t1, t2");
  5177. myquery_r(rc);
  5178. rc= mysql_query(mysql, "DROP TEMPORARY TABLE t1");
  5179. myquery_r(rc);
  5180. rc= mysql_query(mysql, "DROP TEMPORARY TABLE t2");
  5181. myquery_r(rc);
  5182. mysql_close(lmysql);
  5183. mysql= org_mysql;
  5184. rc= mysql_query(mysql, "drop database test_drop_temp_db");
  5185. myquery(rc);
  5186. DIE_UNLESS(1 == mysql_affected_rows(mysql));
  5187. rc= mysql_query(mysql, "delete from mysql.user where User='test_temp'");
  5188. myquery(rc);
  5189. DIE_UNLESS(1 == mysql_affected_rows(mysql));
  5190. rc= mysql_query(mysql, "delete from mysql.tables_priv where User='test_temp'");
  5191. myquery(rc);
  5192. DIE_UNLESS(1 == mysql_affected_rows(mysql));
  5193. }
  5194. }
  5195. #endif
  5196. /* Test warnings for cuted rows */
  5197. static void test_cuted_rows()
  5198. {
  5199. int rc, count;
  5200. MYSQL_RES *result;
  5201. myheader("test_cuted_rows");
  5202. mysql_query(mysql, "DROP TABLE if exists t1");
  5203. mysql_query(mysql, "DROP TABLE if exists t2");
  5204. rc= mysql_query(mysql, "CREATE TABLE t1(c1 tinyint)");
  5205. myquery(rc);
  5206. rc= mysql_query(mysql, "CREATE TABLE t2(c1 int not null)");
  5207. myquery(rc);
  5208. rc= mysql_query(mysql, "INSERT INTO t1 values(10), (NULL), (NULL)");
  5209. myquery(rc);
  5210. count= mysql_warning_count(mysql);
  5211. if (!opt_silent)
  5212. fprintf(stdout, "\n total warnings: %d", count);
  5213. DIE_UNLESS(count == 0);
  5214. rc= mysql_query(mysql, "INSERT INTO t2 SELECT * FROM t1");
  5215. myquery(rc);
  5216. count= mysql_warning_count(mysql);
  5217. if (!opt_silent)
  5218. fprintf(stdout, "\n total warnings: %d", count);
  5219. DIE_UNLESS(count == 2);
  5220. rc= mysql_query(mysql, "SHOW WARNINGS");
  5221. myquery(rc);
  5222. result= mysql_store_result(mysql);
  5223. mytest(result);
  5224. rc= my_process_result_set(result);
  5225. DIE_UNLESS(rc == 2);
  5226. mysql_free_result(result);
  5227. rc= mysql_query(mysql, "INSERT INTO t1 VALUES('junk'), (876789)");
  5228. myquery(rc);
  5229. count= mysql_warning_count(mysql);
  5230. if (!opt_silent)
  5231. fprintf(stdout, "\n total warnings: %d", count);
  5232. DIE_UNLESS(count == 2);
  5233. rc= mysql_query(mysql, "SHOW WARNINGS");
  5234. myquery(rc);
  5235. result= mysql_store_result(mysql);
  5236. mytest(result);
  5237. rc= my_process_result_set(result);
  5238. DIE_UNLESS(rc == 2);
  5239. mysql_free_result(result);
  5240. }
  5241. /* Test update/binary logs */
  5242. static void test_logs()
  5243. {
  5244. MYSQL_STMT *stmt;
  5245. MYSQL_BIND my_bind[2];
  5246. char data[255];
  5247. ulong length;
  5248. int rc;
  5249. short id;
  5250. myheader("test_logs");
  5251. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_logs");
  5252. myquery(rc);
  5253. rc= mysql_query(mysql, "CREATE TABLE test_logs(id smallint, name varchar(20))");
  5254. myquery(rc);
  5255. strmov((char *)data, "INSERT INTO test_logs VALUES(?, ?)");
  5256. stmt= mysql_simple_prepare(mysql, data);
  5257. check_stmt(stmt);
  5258. /*
  5259. We need to bzero bind structure because mysql_stmt_bind_param checks all
  5260. its members.
  5261. */
  5262. bzero((char*) my_bind, sizeof(my_bind));
  5263. my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
  5264. my_bind[0].buffer= (void *)&id;
  5265. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  5266. my_bind[1].buffer= (void *)&data;
  5267. my_bind[1].buffer_length= 255;
  5268. my_bind[1].length= &length;
  5269. id= 9876;
  5270. length= (ulong)(strmov((char *)data, "MySQL - Open Source Database")- data);
  5271. rc= mysql_stmt_bind_param(stmt, my_bind);
  5272. check_execute(stmt, rc);
  5273. rc= mysql_stmt_execute(stmt);
  5274. check_execute(stmt, rc);
  5275. strmov((char *)data, "'");
  5276. length= 1;
  5277. rc= mysql_stmt_execute(stmt);
  5278. check_execute(stmt, rc);
  5279. strmov((char *)data, "\"");
  5280. length= 1;
  5281. rc= mysql_stmt_execute(stmt);
  5282. check_execute(stmt, rc);
  5283. length= (ulong)(strmov((char *)data, "my\'sql\'")-data);
  5284. rc= mysql_stmt_execute(stmt);
  5285. check_execute(stmt, rc);
  5286. length= (ulong)(strmov((char *)data, "my\"sql\"")-data);
  5287. rc= mysql_stmt_execute(stmt);
  5288. check_execute(stmt, rc);
  5289. mysql_stmt_close(stmt);
  5290. strmov((char *)data, "INSERT INTO test_logs VALUES(20, 'mysql')");
  5291. stmt= mysql_simple_prepare(mysql, data);
  5292. check_stmt(stmt);
  5293. rc= mysql_stmt_execute(stmt);
  5294. check_execute(stmt, rc);
  5295. rc= mysql_stmt_execute(stmt);
  5296. check_execute(stmt, rc);
  5297. mysql_stmt_close(stmt);
  5298. strmov((char *)data, "SELECT * FROM test_logs WHERE id=?");
  5299. stmt= mysql_simple_prepare(mysql, data);
  5300. check_stmt(stmt);
  5301. rc= mysql_stmt_bind_param(stmt, my_bind);
  5302. check_execute(stmt, rc);
  5303. rc= mysql_stmt_execute(stmt);
  5304. check_execute(stmt, rc);
  5305. my_bind[1].buffer_length= 255;
  5306. rc= mysql_stmt_bind_result(stmt, my_bind);
  5307. check_execute(stmt, rc);
  5308. rc= mysql_stmt_fetch(stmt);
  5309. check_execute(stmt, rc);
  5310. if (!opt_silent)
  5311. {
  5312. fprintf(stdout, "id : %d\n", id);
  5313. fprintf(stdout, "name : %s(%ld)\n", data, length);
  5314. }
  5315. DIE_UNLESS(id == 9876);
  5316. DIE_UNLESS(length == 19 || length == 20); /* Due to VARCHAR(20) */
  5317. DIE_UNLESS(is_prefix(data, "MySQL - Open Source") == 1);
  5318. rc= mysql_stmt_fetch(stmt);
  5319. check_execute(stmt, rc);
  5320. if (!opt_silent)
  5321. fprintf(stdout, "\n name : %s(%ld)", data, length);
  5322. DIE_UNLESS(length == 1);
  5323. DIE_UNLESS(strcmp(data, "'") == 0);
  5324. rc= mysql_stmt_fetch(stmt);
  5325. check_execute(stmt, rc);
  5326. if (!opt_silent)
  5327. fprintf(stdout, "\n name : %s(%ld)", data, length);
  5328. DIE_UNLESS(length == 1);
  5329. DIE_UNLESS(strcmp(data, "\"") == 0);
  5330. rc= mysql_stmt_fetch(stmt);
  5331. check_execute(stmt, rc);
  5332. if (!opt_silent)
  5333. fprintf(stdout, "\n name : %s(%ld)", data, length);
  5334. DIE_UNLESS(length == 7);
  5335. DIE_UNLESS(strcmp(data, "my\'sql\'") == 0);
  5336. rc= mysql_stmt_fetch(stmt);
  5337. check_execute(stmt, rc);
  5338. if (!opt_silent)
  5339. fprintf(stdout, "\n name : %s(%ld)", data, length);
  5340. DIE_UNLESS(length == 7);
  5341. /*DIE_UNLESS(strcmp(data, "my\"sql\"") == 0); */
  5342. rc= mysql_stmt_fetch(stmt);
  5343. DIE_UNLESS(rc == MYSQL_NO_DATA);
  5344. mysql_stmt_close(stmt);
  5345. rc= mysql_query(mysql, "DROP TABLE test_logs");
  5346. myquery(rc);
  5347. }
  5348. /* Test 'n' statements create and close */
  5349. static void test_nstmts()
  5350. {
  5351. MYSQL_STMT *stmt;
  5352. char query[255];
  5353. int rc;
  5354. static uint i, total_stmts= 2000;
  5355. MYSQL_BIND my_bind[1];
  5356. myheader("test_nstmts");
  5357. mysql_autocommit(mysql, TRUE);
  5358. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_nstmts");
  5359. myquery(rc);
  5360. rc= mysql_query(mysql, "CREATE TABLE test_nstmts(id int)");
  5361. myquery(rc);
  5362. /*
  5363. We need to bzero bind structure because mysql_stmt_bind_param checks all
  5364. its members.
  5365. */
  5366. bzero((char*) my_bind, sizeof(my_bind));
  5367. my_bind[0].buffer= (void *)&i;
  5368. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  5369. for (i= 0; i < total_stmts; i++)
  5370. {
  5371. if (!opt_silent)
  5372. fprintf(stdout, "\r stmt: %d", i);
  5373. strmov(query, "insert into test_nstmts values(?)");
  5374. stmt= mysql_simple_prepare(mysql, query);
  5375. check_stmt(stmt);
  5376. rc= mysql_stmt_bind_param(stmt, my_bind);
  5377. check_execute(stmt, rc);
  5378. rc= mysql_stmt_execute(stmt);
  5379. check_execute(stmt, rc);
  5380. mysql_stmt_close(stmt);
  5381. }
  5382. stmt= mysql_simple_prepare(mysql, " select count(*) from test_nstmts");
  5383. check_stmt(stmt);
  5384. rc= mysql_stmt_execute(stmt);
  5385. check_execute(stmt, rc);
  5386. i= 0;
  5387. rc= mysql_stmt_bind_result(stmt, my_bind);
  5388. check_execute(stmt, rc);
  5389. rc= mysql_stmt_fetch(stmt);
  5390. check_execute(stmt, rc);
  5391. if (!opt_silent)
  5392. fprintf(stdout, "\n total rows: %d", i);
  5393. DIE_UNLESS( i == total_stmts);
  5394. rc= mysql_stmt_fetch(stmt);
  5395. DIE_UNLESS(rc == MYSQL_NO_DATA);
  5396. mysql_stmt_close(stmt);
  5397. rc= mysql_query(mysql, "DROP TABLE test_nstmts");
  5398. myquery(rc);
  5399. }
  5400. /* Test stmt seek() functions */
  5401. static void test_fetch_seek()
  5402. {
  5403. MYSQL_STMT *stmt;
  5404. MYSQL_BIND my_bind[3];
  5405. MYSQL_ROW_OFFSET row;
  5406. int rc;
  5407. int32 c1;
  5408. char c2[11], c3[20];
  5409. myheader("test_fetch_seek");
  5410. rc= mysql_query(mysql, "drop table if exists t1");
  5411. myquery(rc);
  5412. rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10), c3 timestamp(14))");
  5413. myquery(rc);
  5414. rc= mysql_query(mysql, "insert into t1(c2) values('venu'), ('mysql'), ('open'), ('source')");
  5415. myquery(rc);
  5416. stmt= mysql_simple_prepare(mysql, "select * from t1");
  5417. check_stmt(stmt);
  5418. bzero((char*) my_bind, sizeof(my_bind));
  5419. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  5420. my_bind[0].buffer= (void *)&c1;
  5421. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  5422. my_bind[1].buffer= (void *)c2;
  5423. my_bind[1].buffer_length= sizeof(c2);
  5424. my_bind[2]= my_bind[1];
  5425. my_bind[2].buffer= (void *)c3;
  5426. my_bind[2].buffer_length= sizeof(c3);
  5427. rc= mysql_stmt_execute(stmt);
  5428. check_execute(stmt, rc);
  5429. rc= mysql_stmt_bind_result(stmt, my_bind);
  5430. check_execute(stmt, rc);
  5431. rc= mysql_stmt_store_result(stmt);
  5432. check_execute(stmt, rc);
  5433. rc= mysql_stmt_fetch(stmt);
  5434. check_execute(stmt, rc);
  5435. if (!opt_silent)
  5436. fprintf(stdout, "\n row 0: %ld, %s, %s", (long) c1, c2, c3);
  5437. row= mysql_stmt_row_tell(stmt);
  5438. row= mysql_stmt_row_seek(stmt, row);
  5439. rc= mysql_stmt_fetch(stmt);
  5440. check_execute(stmt, rc);
  5441. if (!opt_silent)
  5442. fprintf(stdout, "\n row 2: %ld, %s, %s", (long) c1, c2, c3);
  5443. row= mysql_stmt_row_seek(stmt, row);
  5444. rc= mysql_stmt_fetch(stmt);
  5445. check_execute(stmt, rc);
  5446. if (!opt_silent)
  5447. fprintf(stdout, "\n row 2: %ld, %s, %s", (long) c1, c2, c3);
  5448. mysql_stmt_data_seek(stmt, 0);
  5449. rc= mysql_stmt_fetch(stmt);
  5450. check_execute(stmt, rc);
  5451. if (!opt_silent)
  5452. fprintf(stdout, "\n row 0: %ld, %s, %s", (long) c1, c2, c3);
  5453. rc= mysql_stmt_fetch(stmt);
  5454. check_execute(stmt, rc);
  5455. rc= mysql_stmt_fetch(stmt);
  5456. check_execute(stmt, rc);
  5457. rc= mysql_stmt_fetch(stmt);
  5458. check_execute(stmt, rc);
  5459. rc= mysql_stmt_fetch(stmt);
  5460. DIE_UNLESS(rc == MYSQL_NO_DATA);
  5461. mysql_stmt_close(stmt);
  5462. myquery(mysql_query(mysql, "drop table t1"));
  5463. }
  5464. /* Test mysql_stmt_fetch_column() with offset */
  5465. static void test_fetch_offset()
  5466. {
  5467. MYSQL_STMT *stmt;
  5468. MYSQL_BIND my_bind[1];
  5469. char data[11];
  5470. ulong length;
  5471. int rc;
  5472. my_bool is_null;
  5473. myheader("test_fetch_offset");
  5474. rc= mysql_query(mysql, "drop table if exists t1");
  5475. myquery(rc);
  5476. rc= mysql_query(mysql, "create table t1(a char(10))");
  5477. myquery(rc);
  5478. rc= mysql_query(mysql, "insert into t1 values('abcdefghij'), (null)");
  5479. myquery(rc);
  5480. stmt= mysql_simple_prepare(mysql, "select * from t1");
  5481. check_stmt(stmt);
  5482. bzero((char*) my_bind, sizeof(my_bind));
  5483. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  5484. my_bind[0].buffer= (void *)data;
  5485. my_bind[0].buffer_length= 11;
  5486. my_bind[0].is_null= &is_null;
  5487. my_bind[0].length= &length;
  5488. rc= mysql_stmt_execute(stmt);
  5489. check_execute(stmt, rc);
  5490. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
  5491. check_execute_r(stmt, rc);
  5492. rc= mysql_stmt_execute(stmt);
  5493. check_execute(stmt, rc);
  5494. rc= mysql_stmt_bind_result(stmt, my_bind);
  5495. check_execute(stmt, rc);
  5496. rc= mysql_stmt_store_result(stmt);
  5497. check_execute(stmt, rc);
  5498. rc= mysql_stmt_fetch(stmt);
  5499. check_execute(stmt, rc);
  5500. data[0]= '\0';
  5501. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
  5502. check_execute(stmt, rc);
  5503. if (!opt_silent)
  5504. fprintf(stdout, "\n col 1: %s (%ld)", data, length);
  5505. DIE_UNLESS(strncmp(data, "abcd", 4) == 0 && length == 10);
  5506. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 5);
  5507. check_execute(stmt, rc);
  5508. if (!opt_silent)
  5509. fprintf(stdout, "\n col 1: %s (%ld)", data, length);
  5510. DIE_UNLESS(strncmp(data, "fg", 2) == 0 && length == 10);
  5511. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 9);
  5512. check_execute(stmt, rc);
  5513. if (!opt_silent)
  5514. fprintf(stdout, "\n col 0: %s (%ld)", data, length);
  5515. DIE_UNLESS(strncmp(data, "j", 1) == 0 && length == 10);
  5516. rc= mysql_stmt_fetch(stmt);
  5517. check_execute(stmt, rc);
  5518. is_null= 0;
  5519. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
  5520. check_execute(stmt, rc);
  5521. DIE_UNLESS(is_null == 1);
  5522. rc= mysql_stmt_fetch(stmt);
  5523. DIE_UNLESS(rc == MYSQL_NO_DATA);
  5524. rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
  5525. check_execute_r(stmt, rc);
  5526. mysql_stmt_close(stmt);
  5527. myquery(mysql_query(mysql, "drop table t1"));
  5528. }
  5529. /* Test mysql_stmt_fetch_column() */
  5530. static void test_fetch_column()
  5531. {
  5532. MYSQL_STMT *stmt;
  5533. MYSQL_BIND my_bind[2];
  5534. char c2[20], bc2[20];
  5535. ulong l1, l2, bl1, bl2;
  5536. int rc, c1, bc1;
  5537. myheader("test_fetch_column");
  5538. rc= mysql_query(mysql, "drop table if exists t1");
  5539. myquery(rc);
  5540. rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10))");
  5541. myquery(rc);
  5542. rc= mysql_query(mysql, "insert into t1(c2) values('venu'), ('mysql')");
  5543. myquery(rc);
  5544. stmt= mysql_simple_prepare(mysql, "select * from t1 order by c2 desc");
  5545. check_stmt(stmt);
  5546. bzero((char*) my_bind, sizeof(my_bind));
  5547. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  5548. my_bind[0].buffer= (void *)&bc1;
  5549. my_bind[0].buffer_length= 0;
  5550. my_bind[0].is_null= 0;
  5551. my_bind[0].length= &bl1;
  5552. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  5553. my_bind[1].buffer= (void *)bc2;
  5554. my_bind[1].buffer_length= 7;
  5555. my_bind[1].is_null= 0;
  5556. my_bind[1].length= &bl2;
  5557. rc= mysql_stmt_execute(stmt);
  5558. check_execute(stmt, rc);
  5559. rc= mysql_stmt_bind_result(stmt, my_bind);
  5560. check_execute(stmt, rc);
  5561. rc= mysql_stmt_store_result(stmt);
  5562. check_execute(stmt, rc);
  5563. rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0); /* No-op at this point */
  5564. check_execute_r(stmt, rc);
  5565. rc= mysql_stmt_fetch(stmt);
  5566. check_execute(stmt, rc);
  5567. if (!opt_silent)
  5568. fprintf(stdout, "\n row 0: %d, %s", bc1, bc2);
  5569. c2[0]= '\0'; l2= 0;
  5570. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  5571. my_bind[0].buffer= (void *)c2;
  5572. my_bind[0].buffer_length= 7;
  5573. my_bind[0].is_null= 0;
  5574. my_bind[0].length= &l2;
  5575. rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
  5576. check_execute(stmt, rc);
  5577. if (!opt_silent)
  5578. fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
  5579. DIE_UNLESS(strncmp(c2, "venu", 4) == 0 && l2 == 4);
  5580. c2[0]= '\0'; l2= 0;
  5581. rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
  5582. check_execute(stmt, rc);
  5583. if (!opt_silent)
  5584. fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
  5585. DIE_UNLESS(strcmp(c2, "venu") == 0 && l2 == 4);
  5586. c1= 0;
  5587. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  5588. my_bind[0].buffer= (void *)&c1;
  5589. my_bind[0].buffer_length= 0;
  5590. my_bind[0].is_null= 0;
  5591. my_bind[0].length= &l1;
  5592. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
  5593. check_execute(stmt, rc);
  5594. if (!opt_silent)
  5595. fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
  5596. DIE_UNLESS(c1 == 1 && l1 == 4);
  5597. rc= mysql_stmt_fetch(stmt);
  5598. check_execute(stmt, rc);
  5599. if (!opt_silent)
  5600. fprintf(stdout, "\n row 1: %d, %s", bc1, bc2);
  5601. c2[0]= '\0'; l2= 0;
  5602. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  5603. my_bind[0].buffer= (void *)c2;
  5604. my_bind[0].buffer_length= 7;
  5605. my_bind[0].is_null= 0;
  5606. my_bind[0].length= &l2;
  5607. rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
  5608. check_execute(stmt, rc);
  5609. if (!opt_silent)
  5610. fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
  5611. DIE_UNLESS(strncmp(c2, "mysq", 4) == 0 && l2 == 5);
  5612. c2[0]= '\0'; l2= 0;
  5613. rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
  5614. check_execute(stmt, rc);
  5615. if (!opt_silent)
  5616. fprintf(stdout, "\n col 1: %si(%ld)", c2, l2);
  5617. DIE_UNLESS(strcmp(c2, "mysql") == 0 && l2 == 5);
  5618. c1= 0;
  5619. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  5620. my_bind[0].buffer= (void *)&c1;
  5621. my_bind[0].buffer_length= 0;
  5622. my_bind[0].is_null= 0;
  5623. my_bind[0].length= &l1;
  5624. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
  5625. check_execute(stmt, rc);
  5626. if (!opt_silent)
  5627. fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
  5628. DIE_UNLESS(c1 == 2 && l1 == 4);
  5629. rc= mysql_stmt_fetch(stmt);
  5630. DIE_UNLESS(rc == MYSQL_NO_DATA);
  5631. rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
  5632. check_execute_r(stmt, rc);
  5633. mysql_stmt_close(stmt);
  5634. myquery(mysql_query(mysql, "drop table t1"));
  5635. }
  5636. /* Test mysql_list_fields() */
  5637. static void test_list_fields()
  5638. {
  5639. MYSQL_RES *result;
  5640. int rc;
  5641. myheader("test_list_fields");
  5642. rc= mysql_query(mysql, "drop table if exists t1");
  5643. myquery(rc);
  5644. rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10) default 'mysql')");
  5645. myquery(rc);
  5646. result= mysql_list_fields(mysql, "t1", NULL);
  5647. mytest(result);
  5648. rc= my_process_result_set(result);
  5649. DIE_UNLESS(rc == 0);
  5650. verify_prepare_field(result, 0, "c1", "c1", MYSQL_TYPE_LONG,
  5651. "t1", "t1",
  5652. current_db, 11, "0");
  5653. verify_prepare_field(result, 1, "c2", "c2", MYSQL_TYPE_STRING,
  5654. "t1", "t1",
  5655. current_db, 10, "mysql");
  5656. mysql_free_result(result);
  5657. myquery(mysql_query(mysql, "drop table t1"));
  5658. }
  5659. static void test_bug19671()
  5660. {
  5661. MYSQL_RES *result;
  5662. int rc;
  5663. myheader("test_bug19671");
  5664. mysql_query(mysql, "set sql_mode=''");
  5665. rc= mysql_query(mysql, "drop table if exists t1");
  5666. myquery(rc);
  5667. rc= mysql_query(mysql, "drop view if exists v1");
  5668. myquery(rc);
  5669. rc= mysql_query(mysql, "create table t1(f1 int)");
  5670. myquery(rc);
  5671. rc= mysql_query(mysql, "create view v1 as select va.* from t1 va");
  5672. myquery(rc);
  5673. result= mysql_list_fields(mysql, "v1", NULL);
  5674. mytest(result);
  5675. rc= my_process_result_set(result);
  5676. DIE_UNLESS(rc == 0);
  5677. verify_prepare_field(result, 0, "f1", "f1", MYSQL_TYPE_LONG,
  5678. "v1", "v1", current_db, 11, "0");
  5679. mysql_free_result(result);
  5680. myquery(mysql_query(mysql, "drop view v1"));
  5681. myquery(mysql_query(mysql, "drop table t1"));
  5682. }
  5683. /* Test a memory ovverun bug */
  5684. static void test_mem_overun()
  5685. {
  5686. char buffer[10000], field[10];
  5687. MYSQL_STMT *stmt;
  5688. MYSQL_RES *field_res;
  5689. int rc, i, length;
  5690. myheader("test_mem_overun");
  5691. /*
  5692. Test a memory ovverun bug when a table had 1000 fields with
  5693. a row of data
  5694. */
  5695. rc= mysql_query(mysql, "drop table if exists t_mem_overun");
  5696. myquery(rc);
  5697. strxmov(buffer, "create table t_mem_overun(", NullS);
  5698. for (i= 0; i < 1000; i++)
  5699. {
  5700. sprintf(field, "c%d int", i);
  5701. strxmov(buffer, buffer, field, ", ", NullS);
  5702. }
  5703. length= strlen(buffer);
  5704. buffer[length-2]= ')';
  5705. buffer[--length]= '\0';
  5706. rc= mysql_real_query(mysql, buffer, length);
  5707. myquery(rc);
  5708. strxmov(buffer, "insert into t_mem_overun values(", NullS);
  5709. for (i= 0; i < 1000; i++)
  5710. {
  5711. strxmov(buffer, buffer, "1, ", NullS);
  5712. }
  5713. length= strlen(buffer);
  5714. buffer[length-2]= ')';
  5715. buffer[--length]= '\0';
  5716. rc= mysql_real_query(mysql, buffer, length);
  5717. myquery(rc);
  5718. rc= mysql_query(mysql, "select * from t_mem_overun");
  5719. myquery(rc);
  5720. rc= my_process_result(mysql);
  5721. DIE_UNLESS(rc == 1);
  5722. stmt= mysql_simple_prepare(mysql, "select * from t_mem_overun");
  5723. check_stmt(stmt);
  5724. rc= mysql_stmt_execute(stmt);
  5725. check_execute(stmt, rc);
  5726. field_res= mysql_stmt_result_metadata(stmt);
  5727. mytest(field_res);
  5728. if (!opt_silent)
  5729. fprintf(stdout, "\n total fields : %d", mysql_num_fields(field_res));
  5730. DIE_UNLESS( 1000 == mysql_num_fields(field_res));
  5731. rc= mysql_stmt_store_result(stmt);
  5732. check_execute(stmt, rc);
  5733. rc= mysql_stmt_fetch(stmt);
  5734. check_execute(stmt, rc);
  5735. rc= mysql_stmt_fetch(stmt);
  5736. DIE_UNLESS(rc == MYSQL_NO_DATA);
  5737. mysql_free_result(field_res);
  5738. mysql_stmt_close(stmt);
  5739. }
  5740. /* Test mysql_stmt_free_result() */
  5741. static void test_free_result()
  5742. {
  5743. MYSQL_STMT *stmt;
  5744. MYSQL_BIND my_bind[1];
  5745. char c2[5];
  5746. ulong bl1, l2;
  5747. int rc, c1, bc1;
  5748. myheader("test_free_result");
  5749. rc= mysql_query(mysql, "drop table if exists test_free_result");
  5750. myquery(rc);
  5751. rc= mysql_query(mysql, "create table test_free_result("
  5752. "c1 int primary key auto_increment)");
  5753. myquery(rc);
  5754. rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
  5755. myquery(rc);
  5756. stmt= mysql_simple_prepare(mysql, "select * from test_free_result");
  5757. check_stmt(stmt);
  5758. bzero((char*) my_bind, sizeof(my_bind));
  5759. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  5760. my_bind[0].buffer= (void *)&bc1;
  5761. my_bind[0].length= &bl1;
  5762. rc= mysql_stmt_execute(stmt);
  5763. check_execute(stmt, rc);
  5764. rc= mysql_stmt_bind_result(stmt, my_bind);
  5765. check_execute(stmt, rc);
  5766. rc= mysql_stmt_fetch(stmt);
  5767. check_execute(stmt, rc);
  5768. c2[0]= '\0'; l2= 0;
  5769. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  5770. my_bind[0].buffer= (void *)c2;
  5771. my_bind[0].buffer_length= 7;
  5772. my_bind[0].is_null= 0;
  5773. my_bind[0].length= &l2;
  5774. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
  5775. check_execute(stmt, rc);
  5776. if (!opt_silent)
  5777. fprintf(stdout, "\n col 0: %s(%ld)", c2, l2);
  5778. DIE_UNLESS(strncmp(c2, "1", 1) == 0 && l2 == 1);
  5779. rc= mysql_stmt_fetch(stmt);
  5780. check_execute(stmt, rc);
  5781. c1= 0, l2= 0;
  5782. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  5783. my_bind[0].buffer= (void *)&c1;
  5784. my_bind[0].buffer_length= 0;
  5785. my_bind[0].is_null= 0;
  5786. my_bind[0].length= &l2;
  5787. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
  5788. check_execute(stmt, rc);
  5789. if (!opt_silent)
  5790. fprintf(stdout, "\n col 0: %d(%ld)", c1, l2);
  5791. DIE_UNLESS(c1 == 2 && l2 == 4);
  5792. rc= mysql_query(mysql, "drop table test_free_result");
  5793. myquery_r(rc); /* error should be, COMMANDS OUT OF SYNC */
  5794. rc= mysql_stmt_free_result(stmt);
  5795. check_execute(stmt, rc);
  5796. rc= mysql_query(mysql, "drop table test_free_result");
  5797. myquery(rc); /* should be successful */
  5798. mysql_stmt_close(stmt);
  5799. }
  5800. /* Test mysql_stmt_store_result() */
  5801. static void test_free_store_result()
  5802. {
  5803. MYSQL_STMT *stmt;
  5804. MYSQL_BIND my_bind[1];
  5805. char c2[5];
  5806. ulong bl1, l2;
  5807. int rc, c1, bc1;
  5808. myheader("test_free_store_result");
  5809. rc= mysql_query(mysql, "drop table if exists test_free_result");
  5810. myquery(rc);
  5811. rc= mysql_query(mysql, "create table test_free_result(c1 int primary key auto_increment)");
  5812. myquery(rc);
  5813. rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
  5814. myquery(rc);
  5815. stmt= mysql_simple_prepare(mysql, "select * from test_free_result");
  5816. check_stmt(stmt);
  5817. bzero((char*) my_bind, sizeof(my_bind));
  5818. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  5819. my_bind[0].buffer= (void *)&bc1;
  5820. my_bind[0].buffer_length= 0;
  5821. my_bind[0].is_null= 0;
  5822. my_bind[0].length= &bl1;
  5823. rc= mysql_stmt_execute(stmt);
  5824. check_execute(stmt, rc);
  5825. rc= mysql_stmt_bind_result(stmt, my_bind);
  5826. check_execute(stmt, rc);
  5827. rc= mysql_stmt_store_result(stmt);
  5828. check_execute(stmt, rc);
  5829. rc= mysql_stmt_fetch(stmt);
  5830. check_execute(stmt, rc);
  5831. c2[0]= '\0'; l2= 0;
  5832. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  5833. my_bind[0].buffer= (void *)c2;
  5834. my_bind[0].buffer_length= 7;
  5835. my_bind[0].is_null= 0;
  5836. my_bind[0].length= &l2;
  5837. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
  5838. check_execute(stmt, rc);
  5839. if (!opt_silent)
  5840. fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
  5841. DIE_UNLESS(strncmp(c2, "1", 1) == 0 && l2 == 1);
  5842. rc= mysql_stmt_fetch(stmt);
  5843. check_execute(stmt, rc);
  5844. c1= 0, l2= 0;
  5845. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  5846. my_bind[0].buffer= (void *)&c1;
  5847. my_bind[0].buffer_length= 0;
  5848. my_bind[0].is_null= 0;
  5849. my_bind[0].length= &l2;
  5850. rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
  5851. check_execute(stmt, rc);
  5852. if (!opt_silent)
  5853. fprintf(stdout, "\n col 0: %d(%ld)", c1, l2);
  5854. DIE_UNLESS(c1 == 2 && l2 == 4);
  5855. rc= mysql_stmt_free_result(stmt);
  5856. check_execute(stmt, rc);
  5857. rc= mysql_query(mysql, "drop table test_free_result");
  5858. myquery(rc);
  5859. mysql_stmt_close(stmt);
  5860. }
  5861. /* Test SQLmode */
  5862. static void test_sqlmode()
  5863. {
  5864. MYSQL_STMT *stmt;
  5865. MYSQL_BIND my_bind[2];
  5866. char c1[5], c2[5];
  5867. int rc;
  5868. char query[MAX_TEST_QUERY_LENGTH];
  5869. myheader("test_sqlmode");
  5870. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_piping");
  5871. myquery(rc);
  5872. rc= mysql_query(mysql, "CREATE TABLE test_piping(name varchar(10))");
  5873. myquery(rc);
  5874. /* PIPES_AS_CONCAT */
  5875. strmov(query, "SET SQL_MODE= \"PIPES_AS_CONCAT\"");
  5876. if (!opt_silent)
  5877. fprintf(stdout, "\n With %s", query);
  5878. rc= mysql_query(mysql, query);
  5879. myquery(rc);
  5880. strmov(query, "INSERT INTO test_piping VALUES(?||?)");
  5881. if (!opt_silent)
  5882. fprintf(stdout, "\n query: %s", query);
  5883. stmt= mysql_simple_prepare(mysql, query);
  5884. check_stmt(stmt);
  5885. if (!opt_silent)
  5886. fprintf(stdout, "\n total parameters: %ld", mysql_stmt_param_count(stmt));
  5887. /*
  5888. We need to bzero bind structure because mysql_stmt_bind_param checks all
  5889. its members.
  5890. */
  5891. bzero((char*) my_bind, sizeof(my_bind));
  5892. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  5893. my_bind[0].buffer= (void *)c1;
  5894. my_bind[0].buffer_length= 2;
  5895. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  5896. my_bind[1].buffer= (void *)c2;
  5897. my_bind[1].buffer_length= 3;
  5898. rc= mysql_stmt_bind_param(stmt, my_bind);
  5899. check_execute(stmt, rc);
  5900. strmov(c1, "My"); strmov(c2, "SQL");
  5901. rc= mysql_stmt_execute(stmt);
  5902. check_execute(stmt, rc);
  5903. mysql_stmt_close(stmt);
  5904. verify_col_data("test_piping", "name", "MySQL");
  5905. rc= mysql_query(mysql, "DELETE FROM test_piping");
  5906. myquery(rc);
  5907. strmov(query, "SELECT connection_id ()");
  5908. if (!opt_silent)
  5909. fprintf(stdout, "\n query: %s", query);
  5910. stmt= mysql_simple_prepare(mysql, query);
  5911. check_stmt(stmt);
  5912. mysql_stmt_close(stmt);
  5913. /* ANSI */
  5914. strmov(query, "SET SQL_MODE= \"ANSI\"");
  5915. if (!opt_silent)
  5916. fprintf(stdout, "\n With %s", query);
  5917. rc= mysql_query(mysql, query);
  5918. myquery(rc);
  5919. strmov(query, "INSERT INTO test_piping VALUES(?||?)");
  5920. if (!opt_silent)
  5921. fprintf(stdout, "\n query: %s", query);
  5922. stmt= mysql_simple_prepare(mysql, query);
  5923. check_stmt(stmt);
  5924. if (!opt_silent)
  5925. fprintf(stdout, "\n total parameters: %ld", mysql_stmt_param_count(stmt));
  5926. rc= mysql_stmt_bind_param(stmt, my_bind);
  5927. check_execute(stmt, rc);
  5928. strmov(c1, "My"); strmov(c2, "SQL");
  5929. rc= mysql_stmt_execute(stmt);
  5930. check_execute(stmt, rc);
  5931. mysql_stmt_close(stmt);
  5932. verify_col_data("test_piping", "name", "MySQL");
  5933. /* ANSI mode spaces ... */
  5934. strmov(query, "SELECT connection_id ()");
  5935. if (!opt_silent)
  5936. fprintf(stdout, "\n query: %s", query);
  5937. stmt= mysql_simple_prepare(mysql, query);
  5938. check_stmt(stmt);
  5939. rc= mysql_stmt_execute(stmt);
  5940. check_execute(stmt, rc);
  5941. rc= mysql_stmt_fetch(stmt);
  5942. check_execute(stmt, rc);
  5943. rc= mysql_stmt_fetch(stmt);
  5944. DIE_UNLESS(rc == MYSQL_NO_DATA);
  5945. if (!opt_silent)
  5946. fprintf(stdout, "\n returned 1 row\n");
  5947. mysql_stmt_close(stmt);
  5948. /* IGNORE SPACE MODE */
  5949. strmov(query, "SET SQL_MODE= \"IGNORE_SPACE\"");
  5950. if (!opt_silent)
  5951. fprintf(stdout, "\n With %s", query);
  5952. rc= mysql_query(mysql, query);
  5953. myquery(rc);
  5954. strmov(query, "SELECT connection_id ()");
  5955. if (!opt_silent)
  5956. fprintf(stdout, "\n query: %s", query);
  5957. stmt= mysql_simple_prepare(mysql, query);
  5958. check_stmt(stmt);
  5959. rc= mysql_stmt_execute(stmt);
  5960. check_execute(stmt, rc);
  5961. rc= mysql_stmt_fetch(stmt);
  5962. check_execute(stmt, rc);
  5963. rc= mysql_stmt_fetch(stmt);
  5964. DIE_UNLESS(rc == MYSQL_NO_DATA);
  5965. if (!opt_silent)
  5966. fprintf(stdout, "\n returned 1 row");
  5967. mysql_stmt_close(stmt);
  5968. }
  5969. /* Test for timestamp handling */
  5970. static void test_ts()
  5971. {
  5972. MYSQL_STMT *stmt;
  5973. MYSQL_BIND my_bind[6];
  5974. MYSQL_TIME ts;
  5975. MYSQL_RES *prep_res;
  5976. char strts[30];
  5977. ulong length;
  5978. int rc, field_count;
  5979. char name;
  5980. char query[MAX_TEST_QUERY_LENGTH];
  5981. const char *queries [3]= {"SELECT a, b, c FROM test_ts WHERE %c=?",
  5982. "SELECT a, b, c FROM test_ts WHERE %c=?",
  5983. "SELECT a, b, c FROM test_ts WHERE %c=CAST(? AS DATE)"};
  5984. myheader("test_ts");
  5985. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ts");
  5986. myquery(rc);
  5987. rc= mysql_query(mysql, "CREATE TABLE test_ts(a DATE, b TIME, c TIMESTAMP)");
  5988. myquery(rc);
  5989. stmt= mysql_simple_prepare(mysql, "INSERT INTO test_ts VALUES(?, ?, ?), (?, ?, ?)");
  5990. check_stmt(stmt);
  5991. ts.year= 2003;
  5992. ts.month= 07;
  5993. ts.day= 12;
  5994. ts.hour= 21;
  5995. ts.minute= 07;
  5996. ts.second= 46;
  5997. ts.second_part= 0;
  5998. length= (long)(strmov(strts, "2003-07-12 21:07:46") - strts);
  5999. /*
  6000. We need to bzero bind structure because mysql_stmt_bind_param checks all
  6001. its members.
  6002. */
  6003. bzero((char*) my_bind, sizeof(my_bind));
  6004. my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
  6005. my_bind[0].buffer= (void *)&ts;
  6006. my_bind[0].buffer_length= sizeof(ts);
  6007. my_bind[2]= my_bind[1]= my_bind[0];
  6008. my_bind[3].buffer_type= MYSQL_TYPE_STRING;
  6009. my_bind[3].buffer= (void *)strts;
  6010. my_bind[3].buffer_length= sizeof(strts);
  6011. my_bind[3].length= &length;
  6012. my_bind[5]= my_bind[4]= my_bind[3];
  6013. rc= mysql_stmt_bind_param(stmt, my_bind);
  6014. check_execute(stmt, rc);
  6015. rc= mysql_stmt_execute(stmt);
  6016. check_execute(stmt, rc);
  6017. mysql_stmt_close(stmt);
  6018. verify_col_data("test_ts", "a", "2003-07-12");
  6019. verify_col_data("test_ts", "b", "21:07:46");
  6020. verify_col_data("test_ts", "c", "2003-07-12 21:07:46");
  6021. stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_ts");
  6022. check_stmt(stmt);
  6023. prep_res= mysql_stmt_result_metadata(stmt);
  6024. mytest(prep_res);
  6025. rc= mysql_stmt_execute(stmt);
  6026. check_execute(stmt, rc);
  6027. rc= my_process_stmt_result(stmt);
  6028. DIE_UNLESS(rc == 2);
  6029. field_count= mysql_num_fields(prep_res);
  6030. mysql_free_result(prep_res);
  6031. mysql_stmt_close(stmt);
  6032. for (name= 'a'; field_count--; name++)
  6033. {
  6034. int row_count= 0;
  6035. sprintf(query, queries[field_count], name);
  6036. if (!opt_silent)
  6037. fprintf(stdout, "\n %s", query);
  6038. stmt= mysql_simple_prepare(mysql, query);
  6039. check_stmt(stmt);
  6040. rc= mysql_stmt_bind_param(stmt, my_bind);
  6041. check_execute(stmt, rc);
  6042. rc= mysql_stmt_execute(stmt);
  6043. check_execute(stmt, rc);
  6044. while (mysql_stmt_fetch(stmt) == 0)
  6045. row_count++;
  6046. if (!opt_silent)
  6047. fprintf(stdout, "\n returned '%d' rows", row_count);
  6048. DIE_UNLESS(row_count == 2);
  6049. mysql_stmt_close(stmt);
  6050. }
  6051. }
  6052. /* Test for bug #1500. */
  6053. static void test_bug1500()
  6054. {
  6055. MYSQL_STMT *stmt;
  6056. MYSQL_BIND my_bind[3];
  6057. int rc;
  6058. int32 int_data[3]= {2, 3, 4};
  6059. const char *data;
  6060. myheader("test_bug1500");
  6061. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bg1500");
  6062. myquery(rc);
  6063. rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (i INT)");
  6064. myquery(rc);
  6065. rc= mysql_query(mysql, "INSERT INTO test_bg1500 VALUES (1), (2)");
  6066. myquery(rc);
  6067. rc= mysql_commit(mysql);
  6068. myquery(rc);
  6069. stmt= mysql_simple_prepare(mysql, "SELECT i FROM test_bg1500 WHERE i IN (?, ?, ?)");
  6070. check_stmt(stmt);
  6071. verify_param_count(stmt, 3);
  6072. /*
  6073. We need to bzero bind structure because mysql_stmt_bind_param checks all
  6074. its members.
  6075. */
  6076. bzero((char*) my_bind, sizeof(my_bind));
  6077. my_bind[0].buffer= (void *)int_data;
  6078. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  6079. my_bind[2]= my_bind[1]= my_bind[0];
  6080. my_bind[1].buffer= (void *)(int_data + 1);
  6081. my_bind[2].buffer= (void *)(int_data + 2);
  6082. rc= mysql_stmt_bind_param(stmt, my_bind);
  6083. check_execute(stmt, rc);
  6084. rc= mysql_stmt_execute(stmt);
  6085. check_execute(stmt, rc);
  6086. rc= my_process_stmt_result(stmt);
  6087. DIE_UNLESS(rc == 1);
  6088. mysql_stmt_close(stmt);
  6089. rc= mysql_query(mysql, "DROP TABLE test_bg1500");
  6090. myquery(rc);
  6091. rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (s VARCHAR(25), FULLTEXT(s)) engine=MyISAM");
  6092. myquery(rc);
  6093. rc= mysql_query(mysql,
  6094. "INSERT INTO test_bg1500 VALUES ('Gravedigger'), ('Greed'), ('Hollow Dogs')");
  6095. myquery(rc);
  6096. rc= mysql_commit(mysql);
  6097. myquery(rc);
  6098. stmt= mysql_simple_prepare(mysql,
  6099. "SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (?)");
  6100. check_stmt(stmt);
  6101. verify_param_count(stmt, 1);
  6102. data= "Dogs";
  6103. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  6104. my_bind[0].buffer= (void *) data;
  6105. my_bind[0].buffer_length= strlen(data);
  6106. my_bind[0].is_null= 0;
  6107. my_bind[0].length= 0;
  6108. rc= mysql_stmt_bind_param(stmt, my_bind);
  6109. check_execute(stmt, rc);
  6110. rc= mysql_stmt_execute(stmt);
  6111. check_execute(stmt, rc);
  6112. rc= my_process_stmt_result(stmt);
  6113. DIE_UNLESS(rc == 1);
  6114. mysql_stmt_close(stmt);
  6115. /* This should work too */
  6116. stmt= mysql_simple_prepare(mysql,
  6117. "SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (CONCAT(?, 'digger'))");
  6118. check_stmt(stmt);
  6119. verify_param_count(stmt, 1);
  6120. data= "Grave";
  6121. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  6122. my_bind[0].buffer= (void *) data;
  6123. my_bind[0].buffer_length= strlen(data);
  6124. rc= mysql_stmt_bind_param(stmt, my_bind);
  6125. check_execute(stmt, rc);
  6126. rc= mysql_stmt_execute(stmt);
  6127. check_execute(stmt, rc);
  6128. rc= my_process_stmt_result(stmt);
  6129. DIE_UNLESS(rc == 1);
  6130. mysql_stmt_close(stmt);
  6131. }
  6132. static void test_bug1946()
  6133. {
  6134. MYSQL_STMT *stmt;
  6135. int rc;
  6136. const char *query= "INSERT INTO prepare_command VALUES (?)";
  6137. myheader("test_bug1946");
  6138. rc= mysql_query(mysql, "DROP TABLE IF EXISTS prepare_command");
  6139. myquery(rc);
  6140. rc= mysql_query(mysql, "CREATE TABLE prepare_command(ID INT)");
  6141. myquery(rc);
  6142. stmt= mysql_simple_prepare(mysql, query);
  6143. check_stmt(stmt);
  6144. rc= mysql_real_query(mysql, query, strlen(query));
  6145. DIE_UNLESS(rc != 0);
  6146. if (!opt_silent)
  6147. fprintf(stdout, "Got error (as expected):\n");
  6148. myerror(NULL);
  6149. mysql_stmt_close(stmt);
  6150. rc= mysql_query(mysql, "DROP TABLE prepare_command");
  6151. }
  6152. static void test_parse_error_and_bad_length()
  6153. {
  6154. MYSQL_STMT *stmt;
  6155. int rc;
  6156. /* check that we get 4 syntax errors over the 4 calls */
  6157. myheader("test_parse_error_and_bad_length");
  6158. rc= mysql_query(mysql, "SHOW DATABAAAA");
  6159. DIE_UNLESS(rc);
  6160. if (!opt_silent)
  6161. fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
  6162. rc= mysql_real_query(mysql, "SHOW DATABASES", 100);
  6163. DIE_UNLESS(rc);
  6164. if (!opt_silent)
  6165. fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
  6166. stmt= mysql_simple_prepare(mysql, "SHOW DATABAAAA");
  6167. DIE_UNLESS(!stmt);
  6168. if (!opt_silent)
  6169. fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
  6170. stmt= mysql_stmt_init(mysql);
  6171. DIE_UNLESS(stmt);
  6172. rc= mysql_stmt_prepare(stmt, "SHOW DATABASES", 100);
  6173. DIE_UNLESS(rc != 0);
  6174. if (!opt_silent)
  6175. fprintf(stdout, "Got error (as expected): '%s'\n", mysql_stmt_error(stmt));
  6176. mysql_stmt_close(stmt);
  6177. }
  6178. static void test_bug2247()
  6179. {
  6180. MYSQL_STMT *stmt;
  6181. MYSQL_RES *res;
  6182. int rc;
  6183. int i;
  6184. const char *create= "CREATE TABLE bug2247(id INT UNIQUE AUTO_INCREMENT)";
  6185. const char *insert= "INSERT INTO bug2247 VALUES (NULL)";
  6186. const char *SELECT= "SELECT id FROM bug2247";
  6187. const char *update= "UPDATE bug2247 SET id=id+10";
  6188. const char *drop= "DROP TABLE IF EXISTS bug2247";
  6189. ulonglong exp_count;
  6190. enum { NUM_ROWS= 5 };
  6191. myheader("test_bug2247");
  6192. if (!opt_silent)
  6193. fprintf(stdout, "\nChecking if stmt_affected_rows is not affected by\n"
  6194. "mysql_query ... ");
  6195. /* create table and insert few rows */
  6196. rc= mysql_query(mysql, drop);
  6197. myquery(rc);
  6198. rc= mysql_query(mysql, create);
  6199. myquery(rc);
  6200. stmt= mysql_simple_prepare(mysql, insert);
  6201. check_stmt(stmt);
  6202. for (i= 0; i < NUM_ROWS; ++i)
  6203. {
  6204. rc= mysql_stmt_execute(stmt);
  6205. check_execute(stmt, rc);
  6206. }
  6207. exp_count= mysql_stmt_affected_rows(stmt);
  6208. DIE_UNLESS(exp_count == 1);
  6209. rc= mysql_query(mysql, SELECT);
  6210. myquery(rc);
  6211. /*
  6212. mysql_store_result overwrites mysql->affected_rows. Check that
  6213. mysql_stmt_affected_rows() returns the same value, whereas
  6214. mysql_affected_rows() value is correct.
  6215. */
  6216. res= mysql_store_result(mysql);
  6217. mytest(res);
  6218. DIE_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS);
  6219. DIE_UNLESS(exp_count == mysql_stmt_affected_rows(stmt));
  6220. rc= mysql_query(mysql, update);
  6221. myquery(rc);
  6222. DIE_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS);
  6223. DIE_UNLESS(exp_count == mysql_stmt_affected_rows(stmt));
  6224. mysql_free_result(res);
  6225. mysql_stmt_close(stmt);
  6226. /* check that mysql_stmt_store_result modifies mysql_stmt_affected_rows */
  6227. stmt= mysql_simple_prepare(mysql, SELECT);
  6228. check_stmt(stmt);
  6229. rc= mysql_stmt_execute(stmt);
  6230. check_execute(stmt, rc);
  6231. rc= mysql_stmt_store_result(stmt);
  6232. check_execute(stmt, rc);
  6233. exp_count= mysql_stmt_affected_rows(stmt);
  6234. DIE_UNLESS(exp_count == NUM_ROWS);
  6235. rc= mysql_query(mysql, insert);
  6236. myquery(rc);
  6237. DIE_UNLESS(mysql_affected_rows(mysql) == 1);
  6238. DIE_UNLESS(mysql_stmt_affected_rows(stmt) == exp_count);
  6239. mysql_stmt_close(stmt);
  6240. if (!opt_silent)
  6241. fprintf(stdout, "OK");
  6242. }
  6243. static void test_subqueries()
  6244. {
  6245. MYSQL_STMT *stmt;
  6246. int rc, i;
  6247. const char *query= "SELECT (SELECT SUM(a+b) FROM t2 where t1.b=t2.b GROUP BY t1.a LIMIT 1) as scalar_s, exists (select 1 from t2 where t2.a/2=t1.a) as exists_s, a in (select a+3 from t2) as in_s, (a-1, b-1) in (select a, b from t2) as in_row_s FROM t1, (select a x, b y from t2) tt WHERE x=a";
  6248. myheader("test_subqueries");
  6249. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
  6250. myquery(rc);
  6251. rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
  6252. myquery(rc);
  6253. rc= mysql_query(mysql,
  6254. "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
  6255. myquery(rc);
  6256. rc= mysql_query(mysql, "create table t2 select * from t1;");
  6257. myquery(rc);
  6258. stmt= mysql_simple_prepare(mysql, query);
  6259. check_stmt(stmt);
  6260. for (i= 0; i < 3; i++)
  6261. {
  6262. rc= mysql_stmt_execute(stmt);
  6263. check_execute(stmt, rc);
  6264. rc= my_process_stmt_result(stmt);
  6265. DIE_UNLESS(rc == 5);
  6266. }
  6267. mysql_stmt_close(stmt);
  6268. rc= mysql_query(mysql, "DROP TABLE t1, t2");
  6269. myquery(rc);
  6270. }
  6271. static void test_bad_union()
  6272. {
  6273. MYSQL_STMT *stmt;
  6274. const char *query= "SELECT 1, 2 union SELECT 1";
  6275. myheader("test_bad_union");
  6276. stmt= mysql_simple_prepare(mysql, query);
  6277. DIE_UNLESS(stmt == 0);
  6278. myerror(NULL);
  6279. }
  6280. static void test_distinct()
  6281. {
  6282. MYSQL_STMT *stmt;
  6283. int rc, i;
  6284. const char *query=
  6285. "SELECT 2+count(distinct b), group_concat(a) FROM t1 group by a";
  6286. myheader("test_distinct");
  6287. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  6288. myquery(rc);
  6289. rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
  6290. myquery(rc);
  6291. rc= mysql_query(mysql,
  6292. "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), \
  6293. (1, 10), (2, 20), (3, 30), (4, 40), (5, 50);");
  6294. myquery(rc);
  6295. for (i= 0; i < 3; i++)
  6296. {
  6297. stmt= mysql_simple_prepare(mysql, query);
  6298. check_stmt(stmt);
  6299. rc= mysql_stmt_execute(stmt);
  6300. check_execute(stmt, rc);
  6301. rc= my_process_stmt_result(stmt);
  6302. DIE_UNLESS(rc == 5);
  6303. mysql_stmt_close(stmt);
  6304. }
  6305. rc= mysql_query(mysql, "DROP TABLE t1");
  6306. myquery(rc);
  6307. }
  6308. /*
  6309. Test for bug#2248 "mysql_fetch without prior mysql_stmt_execute hangs"
  6310. */
  6311. static void test_bug2248()
  6312. {
  6313. MYSQL_STMT *stmt;
  6314. int rc;
  6315. const char *query1= "SELECT DATABASE()";
  6316. const char *query2= "INSERT INTO test_bug2248 VALUES (10)";
  6317. myheader("test_bug2248");
  6318. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bug2248");
  6319. myquery(rc);
  6320. rc= mysql_query(mysql, "CREATE TABLE test_bug2248 (id int)");
  6321. myquery(rc);
  6322. stmt= mysql_simple_prepare(mysql, query1);
  6323. check_stmt(stmt);
  6324. /* This should not hang */
  6325. rc= mysql_stmt_fetch(stmt);
  6326. check_execute_r(stmt, rc);
  6327. /* And this too */
  6328. rc= mysql_stmt_store_result(stmt);
  6329. check_execute_r(stmt, rc);
  6330. mysql_stmt_close(stmt);
  6331. stmt= mysql_simple_prepare(mysql, query2);
  6332. check_stmt(stmt);
  6333. rc= mysql_stmt_execute(stmt);
  6334. check_execute(stmt, rc);
  6335. /* This too should not hang but should return proper error */
  6336. rc= mysql_stmt_fetch(stmt);
  6337. DIE_UNLESS(rc == 1);
  6338. /* This too should not hang but should not bark */
  6339. rc= mysql_stmt_store_result(stmt);
  6340. check_execute(stmt, rc);
  6341. /* This should return proper error */
  6342. rc= mysql_stmt_fetch(stmt);
  6343. check_execute_r(stmt, rc);
  6344. DIE_UNLESS(rc == 1);
  6345. mysql_stmt_close(stmt);
  6346. rc= mysql_query(mysql, "DROP TABLE test_bug2248");
  6347. myquery(rc);
  6348. }
  6349. static void test_subqueries_ref()
  6350. {
  6351. MYSQL_STMT *stmt;
  6352. int rc, i;
  6353. const char *query= "SELECT a as ccc from t1 outr where a+1=(SELECT 1+outr.a from t1 where outr.a+1=a+1 and a=1)";
  6354. myheader("test_subqueries_ref");
  6355. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  6356. myquery(rc);
  6357. rc= mysql_query(mysql, "CREATE TABLE t1 (a int);");
  6358. myquery(rc);
  6359. rc= mysql_query(mysql,
  6360. "insert into t1 values (1), (2), (3), (4), (5);");
  6361. myquery(rc);
  6362. stmt= mysql_simple_prepare(mysql, query);
  6363. check_stmt(stmt);
  6364. for (i= 0; i < 3; i++)
  6365. {
  6366. rc= mysql_stmt_execute(stmt);
  6367. check_execute(stmt, rc);
  6368. rc= my_process_stmt_result(stmt);
  6369. DIE_UNLESS(rc == 1);
  6370. }
  6371. mysql_stmt_close(stmt);
  6372. rc= mysql_query(mysql, "DROP TABLE t1");
  6373. myquery(rc);
  6374. }
  6375. static void test_union()
  6376. {
  6377. MYSQL_STMT *stmt;
  6378. int rc;
  6379. myheader("test_union");
  6380. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
  6381. myquery(rc);
  6382. rc= mysql_query(mysql,
  6383. "CREATE TABLE t1 "
  6384. "(id INTEGER NOT NULL PRIMARY KEY, "
  6385. " name VARCHAR(20) NOT NULL)");
  6386. myquery(rc);
  6387. rc= mysql_query(mysql,
  6388. "INSERT INTO t1 (id, name) VALUES "
  6389. "(2, 'Ja'), (3, 'Ede'), "
  6390. "(4, 'Haag'), (5, 'Kabul'), "
  6391. "(6, 'Almere'), (7, 'Utrecht'), "
  6392. "(8, 'Qandahar'), (9, 'Amsterdam'), "
  6393. "(10, 'Amersfoort'), (11, 'Constantine')");
  6394. myquery(rc);
  6395. rc= mysql_query(mysql,
  6396. "CREATE TABLE t2 "
  6397. "(id INTEGER NOT NULL PRIMARY KEY, "
  6398. " name VARCHAR(20) NOT NULL)");
  6399. myquery(rc);
  6400. rc= mysql_query(mysql,
  6401. "INSERT INTO t2 (id, name) VALUES "
  6402. "(4, 'Guam'), (5, 'Aruba'), "
  6403. "(6, 'Angola'), (7, 'Albania'), "
  6404. "(8, 'Anguilla'), (9, 'Argentina'), "
  6405. "(10, 'Azerbaijan'), (11, 'Afghanistan'), "
  6406. "(12, 'Burkina Faso'), (13, 'Faroe Islands')");
  6407. myquery(rc);
  6408. stmt= mysql_simple_prepare(mysql,
  6409. "SELECT t1.name FROM t1 UNION "
  6410. "SELECT t2.name FROM t2");
  6411. check_stmt(stmt);
  6412. rc= mysql_stmt_execute(stmt);
  6413. check_execute(stmt, rc);
  6414. rc= my_process_stmt_result(stmt);
  6415. DIE_UNLESS(rc == 20);
  6416. mysql_stmt_close(stmt);
  6417. rc= mysql_query(mysql, "DROP TABLE t1, t2");
  6418. myquery(rc);
  6419. }
  6420. static void test_bug3117()
  6421. {
  6422. MYSQL_STMT *stmt;
  6423. MYSQL_BIND buffer;
  6424. longlong lii;
  6425. ulong length;
  6426. my_bool is_null;
  6427. int rc;
  6428. myheader("test_bug3117");
  6429. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  6430. myquery(rc);
  6431. rc= mysql_query(mysql, "CREATE TABLE t1 (id int auto_increment primary key)");
  6432. myquery(rc);
  6433. stmt= mysql_simple_prepare(mysql, "SELECT LAST_INSERT_ID()");
  6434. check_stmt(stmt);
  6435. rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
  6436. myquery(rc);
  6437. rc= mysql_stmt_execute(stmt);
  6438. check_execute(stmt, rc);
  6439. bzero((char*) &buffer, sizeof(buffer));
  6440. buffer.buffer_type= MYSQL_TYPE_LONGLONG;
  6441. buffer.buffer_length= sizeof(lii);
  6442. buffer.buffer= (void *)&lii;
  6443. buffer.length= &length;
  6444. buffer.is_null= &is_null;
  6445. rc= mysql_stmt_bind_result(stmt, &buffer);
  6446. check_execute(stmt, rc);
  6447. rc= mysql_stmt_store_result(stmt);
  6448. check_execute(stmt, rc);
  6449. rc= mysql_stmt_fetch(stmt);
  6450. check_execute(stmt, rc);
  6451. DIE_UNLESS(is_null == 0 && lii == 1);
  6452. if (!opt_silent)
  6453. fprintf(stdout, "\n\tLAST_INSERT_ID()= 1 ok\n");
  6454. rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
  6455. myquery(rc);
  6456. rc= mysql_stmt_execute(stmt);
  6457. check_execute(stmt, rc);
  6458. rc= mysql_stmt_fetch(stmt);
  6459. check_execute(stmt, rc);
  6460. DIE_UNLESS(is_null == 0 && lii == 2);
  6461. if (!opt_silent)
  6462. fprintf(stdout, "\tLAST_INSERT_ID()= 2 ok\n");
  6463. mysql_stmt_close(stmt);
  6464. rc= mysql_query(mysql, "DROP TABLE t1");
  6465. myquery(rc);
  6466. }
  6467. static void test_join()
  6468. {
  6469. MYSQL_STMT *stmt;
  6470. int rc, i, j;
  6471. const char *query[]= {"SELECT * FROM t2 join t1 on (t1.a=t2.a)",
  6472. "SELECT * FROM t2 natural join t1",
  6473. "SELECT * FROM t2 join t1 using(a)",
  6474. "SELECT * FROM t2 left join t1 on(t1.a=t2.a)",
  6475. "SELECT * FROM t2 natural left join t1",
  6476. "SELECT * FROM t2 left join t1 using(a)",
  6477. "SELECT * FROM t2 right join t1 on(t1.a=t2.a)",
  6478. "SELECT * FROM t2 natural right join t1",
  6479. "SELECT * FROM t2 right join t1 using(a)"};
  6480. myheader("test_join");
  6481. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
  6482. myquery(rc);
  6483. rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
  6484. myquery(rc);
  6485. rc= mysql_query(mysql,
  6486. "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
  6487. myquery(rc);
  6488. rc= mysql_query(mysql, "CREATE TABLE t2 (a int , c int);");
  6489. myquery(rc);
  6490. rc= mysql_query(mysql,
  6491. "insert into t2 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
  6492. myquery(rc);
  6493. for (j= 0; j < 9; j++)
  6494. {
  6495. stmt= mysql_simple_prepare(mysql, query[j]);
  6496. check_stmt(stmt);
  6497. for (i= 0; i < 3; i++)
  6498. {
  6499. rc= mysql_stmt_execute(stmt);
  6500. check_execute(stmt, rc);
  6501. rc= my_process_stmt_result(stmt);
  6502. DIE_UNLESS(rc == 5);
  6503. }
  6504. mysql_stmt_close(stmt);
  6505. }
  6506. rc= mysql_query(mysql, "DROP TABLE t1, t2");
  6507. myquery(rc);
  6508. }
  6509. static void test_selecttmp()
  6510. {
  6511. MYSQL_STMT *stmt;
  6512. int rc, i;
  6513. const char *query= "select a, (select count(distinct t1.b) as sum from t1, t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3";
  6514. myheader("test_select_tmp");
  6515. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3");
  6516. myquery(rc);
  6517. rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
  6518. myquery(rc);
  6519. rc= mysql_query(mysql, "create table t2 (a int, b int);");
  6520. myquery(rc);
  6521. rc= mysql_query(mysql, "create table t3 (a int, b int);");
  6522. myquery(rc);
  6523. rc= mysql_query(mysql,
  6524. "insert into t1 values (0, 100), (1, 2), (1, 3), (2, 2), (2, 7), \
  6525. (2, -1), (3, 10);");
  6526. myquery(rc);
  6527. rc= mysql_query(mysql,
  6528. "insert into t2 values (0, 0), (1, 1), (2, 1), (3, 1), (4, 1);");
  6529. myquery(rc);
  6530. rc= mysql_query(mysql,
  6531. "insert into t3 values (3, 3), (2, 2), (1, 1);");
  6532. myquery(rc);
  6533. stmt= mysql_simple_prepare(mysql, query);
  6534. check_stmt(stmt);
  6535. for (i= 0; i < 3; i++)
  6536. {
  6537. rc= mysql_stmt_execute(stmt);
  6538. check_execute(stmt, rc);
  6539. rc= my_process_stmt_result(stmt);
  6540. DIE_UNLESS(rc == 3);
  6541. }
  6542. mysql_stmt_close(stmt);
  6543. rc= mysql_query(mysql, "DROP TABLE t1, t2, t3");
  6544. myquery(rc);
  6545. }
  6546. static void test_create_drop()
  6547. {
  6548. MYSQL_STMT *stmt_create, *stmt_drop, *stmt_select, *stmt_create_select;
  6549. char *query;
  6550. int rc, i;
  6551. myheader("test_table_manipulation");
  6552. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
  6553. myquery(rc);
  6554. rc= mysql_query(mysql, "create table t2 (a int);");
  6555. myquery(rc);
  6556. rc= mysql_query(mysql, "create table t1 (a int);");
  6557. myquery(rc);
  6558. rc= mysql_query(mysql, "insert into t2 values (3), (2), (1);");
  6559. myquery(rc);
  6560. query= (char*)"create table t1 (a int)";
  6561. stmt_create= mysql_simple_prepare(mysql, query);
  6562. check_stmt(stmt_create);
  6563. query= (char*)"drop table t1";
  6564. stmt_drop= mysql_simple_prepare(mysql, query);
  6565. check_stmt(stmt_drop);
  6566. query= (char*)"select a in (select a from t2) from t1";
  6567. stmt_select= mysql_simple_prepare(mysql, query);
  6568. check_stmt(stmt_select);
  6569. rc= mysql_query(mysql, "DROP TABLE t1");
  6570. myquery(rc);
  6571. query= (char*)"create table t1 select a from t2";
  6572. stmt_create_select= mysql_simple_prepare(mysql, query);
  6573. check_stmt(stmt_create_select);
  6574. for (i= 0; i < 3; i++)
  6575. {
  6576. rc= mysql_stmt_execute(stmt_create);
  6577. check_execute(stmt_create, rc);
  6578. if (!opt_silent)
  6579. fprintf(stdout, "created %i\n", i);
  6580. rc= mysql_stmt_execute(stmt_select);
  6581. check_execute(stmt_select, rc);
  6582. rc= my_process_stmt_result(stmt_select);
  6583. DIE_UNLESS(rc == 0);
  6584. rc= mysql_stmt_execute(stmt_drop);
  6585. check_execute(stmt_drop, rc);
  6586. if (!opt_silent)
  6587. fprintf(stdout, "dropped %i\n", i);
  6588. rc= mysql_stmt_execute(stmt_create_select);
  6589. check_execute(stmt_create, rc);
  6590. if (!opt_silent)
  6591. fprintf(stdout, "created select %i\n", i);
  6592. rc= mysql_stmt_execute(stmt_select);
  6593. check_execute(stmt_select, rc);
  6594. rc= my_process_stmt_result(stmt_select);
  6595. DIE_UNLESS(rc == 3);
  6596. rc= mysql_stmt_execute(stmt_drop);
  6597. check_execute(stmt_drop, rc);
  6598. if (!opt_silent)
  6599. fprintf(stdout, "dropped %i\n", i);
  6600. }
  6601. mysql_stmt_close(stmt_create);
  6602. mysql_stmt_close(stmt_drop);
  6603. mysql_stmt_close(stmt_select);
  6604. mysql_stmt_close(stmt_create_select);
  6605. rc= mysql_query(mysql, "DROP TABLE t2");
  6606. myquery(rc);
  6607. }
  6608. static void test_rename()
  6609. {
  6610. MYSQL_STMT *stmt;
  6611. const char *query= "rename table t1 to t2, t3 to t4";
  6612. int rc;
  6613. myheader("test_table_rename");
  6614. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
  6615. myquery(rc);
  6616. stmt= mysql_simple_prepare(mysql, query);
  6617. check_stmt(stmt);
  6618. rc= mysql_query(mysql, "create table t1 (a int)");
  6619. myquery(rc);
  6620. rc= mysql_stmt_execute(stmt);
  6621. check_execute_r(stmt, rc);
  6622. if (!opt_silent)
  6623. fprintf(stdout, "rename without t3\n");
  6624. rc= mysql_query(mysql, "create table t3 (a int)");
  6625. myquery(rc);
  6626. rc= mysql_stmt_execute(stmt);
  6627. check_execute(stmt, rc);
  6628. if (!opt_silent)
  6629. fprintf(stdout, "rename with t3\n");
  6630. rc= mysql_stmt_execute(stmt);
  6631. check_execute_r(stmt, rc);
  6632. if (!opt_silent)
  6633. fprintf(stdout, "rename renamed\n");
  6634. rc= mysql_query(mysql, "rename table t2 to t1, t4 to t3");
  6635. myquery(rc);
  6636. rc= mysql_stmt_execute(stmt);
  6637. check_execute(stmt, rc);
  6638. if (!opt_silent)
  6639. fprintf(stdout, "rename reverted\n");
  6640. mysql_stmt_close(stmt);
  6641. rc= mysql_query(mysql, "DROP TABLE t2, t4");
  6642. myquery(rc);
  6643. }
  6644. static void test_do_set()
  6645. {
  6646. MYSQL_STMT *stmt_do, *stmt_set;
  6647. char *query;
  6648. int rc, i;
  6649. myheader("test_do_set");
  6650. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  6651. myquery(rc);
  6652. rc= mysql_query(mysql, "create table t1 (a int)");
  6653. myquery(rc);
  6654. query= (char*)"do @var:=(1 in (select * from t1))";
  6655. stmt_do= mysql_simple_prepare(mysql, query);
  6656. check_stmt(stmt_do);
  6657. query= (char*)"set @var=(1 in (select * from t1))";
  6658. stmt_set= mysql_simple_prepare(mysql, query);
  6659. check_stmt(stmt_set);
  6660. for (i= 0; i < 3; i++)
  6661. {
  6662. rc= mysql_stmt_execute(stmt_do);
  6663. check_execute(stmt_do, rc);
  6664. if (!opt_silent)
  6665. fprintf(stdout, "do %i\n", i);
  6666. rc= mysql_stmt_execute(stmt_set);
  6667. check_execute(stmt_set, rc);
  6668. if (!opt_silent)
  6669. fprintf(stdout, "set %i\n", i);
  6670. }
  6671. mysql_stmt_close(stmt_do);
  6672. mysql_stmt_close(stmt_set);
  6673. }
  6674. static void test_multi()
  6675. {
  6676. MYSQL_STMT *stmt_delete, *stmt_update, *stmt_select1, *stmt_select2;
  6677. char *query;
  6678. MYSQL_BIND my_bind[1];
  6679. int rc, i;
  6680. int32 param= 1;
  6681. ulong length= 1;
  6682. myheader("test_multi");
  6683. /*
  6684. We need to bzero bind structure because mysql_stmt_bind_param checks all
  6685. its members.
  6686. */
  6687. bzero((char*) my_bind, sizeof(my_bind));
  6688. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  6689. my_bind[0].buffer= (void *)&param;
  6690. my_bind[0].length= &length;
  6691. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
  6692. myquery(rc);
  6693. rc= mysql_query(mysql, "create table t1 (a int, b int)");
  6694. myquery(rc);
  6695. rc= mysql_query(mysql, "create table t2 (a int, b int)");
  6696. myquery(rc);
  6697. rc= mysql_query(mysql, "insert into t1 values (3, 3), (2, 2), (1, 1)");
  6698. myquery(rc);
  6699. rc= mysql_query(mysql, "insert into t2 values (3, 3), (2, 2), (1, 1)");
  6700. myquery(rc);
  6701. query= (char*)"delete t1, t2 from t1, t2 where t1.a=t2.a and t1.b=10";
  6702. stmt_delete= mysql_simple_prepare(mysql, query);
  6703. check_stmt(stmt_delete);
  6704. query= (char*)"update t1, t2 set t1.b=10, t2.b=10 where t1.a=t2.a and t1.b=?";
  6705. stmt_update= mysql_simple_prepare(mysql, query);
  6706. check_stmt(stmt_update);
  6707. query= (char*)"select * from t1";
  6708. stmt_select1= mysql_simple_prepare(mysql, query);
  6709. check_stmt(stmt_select1);
  6710. query= (char*)"select * from t2";
  6711. stmt_select2= mysql_simple_prepare(mysql, query);
  6712. check_stmt(stmt_select2);
  6713. for(i= 0; i < 3; i++)
  6714. {
  6715. rc= mysql_stmt_bind_param(stmt_update, my_bind);
  6716. check_execute(stmt_update, rc);
  6717. rc= mysql_stmt_execute(stmt_update);
  6718. check_execute(stmt_update, rc);
  6719. if (!opt_silent)
  6720. fprintf(stdout, "update %ld\n", (long) param);
  6721. rc= mysql_stmt_execute(stmt_delete);
  6722. check_execute(stmt_delete, rc);
  6723. if (!opt_silent)
  6724. fprintf(stdout, "delete %ld\n", (long) param);
  6725. rc= mysql_stmt_execute(stmt_select1);
  6726. check_execute(stmt_select1, rc);
  6727. rc= my_process_stmt_result(stmt_select1);
  6728. DIE_UNLESS(rc == 3-param);
  6729. rc= mysql_stmt_execute(stmt_select2);
  6730. check_execute(stmt_select2, rc);
  6731. rc= my_process_stmt_result(stmt_select2);
  6732. DIE_UNLESS(rc == 3-param);
  6733. param++;
  6734. }
  6735. mysql_stmt_close(stmt_delete);
  6736. mysql_stmt_close(stmt_update);
  6737. mysql_stmt_close(stmt_select1);
  6738. mysql_stmt_close(stmt_select2);
  6739. rc= mysql_query(mysql, "drop table t1, t2");
  6740. myquery(rc);
  6741. }
  6742. static void test_insert_select()
  6743. {
  6744. MYSQL_STMT *stmt_insert, *stmt_select;
  6745. char *query;
  6746. int rc;
  6747. uint i;
  6748. myheader("test_insert_select");
  6749. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
  6750. myquery(rc);
  6751. rc= mysql_query(mysql, "create table t1 (a int)");
  6752. myquery(rc);
  6753. rc= mysql_query(mysql, "create table t2 (a int)");
  6754. myquery(rc);
  6755. rc= mysql_query(mysql, "insert into t2 values (1)");
  6756. myquery(rc);
  6757. query= (char*)"insert into t1 select a from t2";
  6758. stmt_insert= mysql_simple_prepare(mysql, query);
  6759. check_stmt(stmt_insert);
  6760. query= (char*)"select * from t1";
  6761. stmt_select= mysql_simple_prepare(mysql, query);
  6762. check_stmt(stmt_select);
  6763. for(i= 0; i < 3; i++)
  6764. {
  6765. rc= mysql_stmt_execute(stmt_insert);
  6766. check_execute(stmt_insert, rc);
  6767. if (!opt_silent)
  6768. fprintf(stdout, "insert %u\n", i);
  6769. rc= mysql_stmt_execute(stmt_select);
  6770. check_execute(stmt_select, rc);
  6771. rc= my_process_stmt_result(stmt_select);
  6772. DIE_UNLESS(rc == (int)(i+1));
  6773. }
  6774. mysql_stmt_close(stmt_insert);
  6775. mysql_stmt_close(stmt_select);
  6776. rc= mysql_query(mysql, "drop table t1, t2");
  6777. myquery(rc);
  6778. }
  6779. static void test_bind_nagative()
  6780. {
  6781. MYSQL_STMT *stmt_insert;
  6782. char *query;
  6783. int rc;
  6784. MYSQL_BIND my_bind[1];
  6785. int32 my_val= 0;
  6786. ulong my_length= 0L;
  6787. my_bool my_null= FALSE;
  6788. myheader("test_insert_select");
  6789. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  6790. myquery(rc);
  6791. rc= mysql_query(mysql, "create temporary table t1 (c1 int unsigned)");
  6792. myquery(rc);
  6793. rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1), (-1)");
  6794. myquery(rc);
  6795. query= (char*)"INSERT INTO t1 VALUES (?)";
  6796. stmt_insert= mysql_simple_prepare(mysql, query);
  6797. check_stmt(stmt_insert);
  6798. /* bind parameters */
  6799. bzero((char*) my_bind, sizeof(my_bind));
  6800. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  6801. my_bind[0].buffer= (void *)&my_val;
  6802. my_bind[0].length= &my_length;
  6803. my_bind[0].is_null= (char*)&my_null;
  6804. rc= mysql_stmt_bind_param(stmt_insert, my_bind);
  6805. check_execute(stmt_insert, rc);
  6806. my_val= -1;
  6807. rc= mysql_stmt_execute(stmt_insert);
  6808. check_execute(stmt_insert, rc);
  6809. mysql_stmt_close(stmt_insert);
  6810. rc= mysql_query(mysql, "drop table t1");
  6811. myquery(rc);
  6812. }
  6813. static void test_derived()
  6814. {
  6815. MYSQL_STMT *stmt;
  6816. int rc, i;
  6817. MYSQL_BIND my_bind[1];
  6818. int32 my_val= 0;
  6819. ulong my_length= 0L;
  6820. my_bool my_null= FALSE;
  6821. const char *query=
  6822. "select count(1) from (select f.id from t1 f where f.id=?) as x";
  6823. myheader("test_derived");
  6824. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  6825. myquery(rc);
  6826. rc= mysql_query(mysql, "create table t1 (id int(8), primary key (id)) \
  6827. ENGINE=InnoDB DEFAULT CHARSET=utf8");
  6828. myquery(rc);
  6829. rc= mysql_query(mysql, "insert into t1 values (1)");
  6830. myquery(rc);
  6831. stmt= mysql_simple_prepare(mysql, query);
  6832. check_stmt(stmt);
  6833. /*
  6834. We need to bzero bind structure because mysql_stmt_bind_param checks all
  6835. its members.
  6836. */
  6837. bzero((char*) my_bind, sizeof(my_bind));
  6838. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  6839. my_bind[0].buffer= (void *)&my_val;
  6840. my_bind[0].length= &my_length;
  6841. my_bind[0].is_null= (char*)&my_null;
  6842. my_val= 1;
  6843. rc= mysql_stmt_bind_param(stmt, my_bind);
  6844. check_execute(stmt, rc);
  6845. for (i= 0; i < 3; i++)
  6846. {
  6847. rc= mysql_stmt_execute(stmt);
  6848. check_execute(stmt, rc);
  6849. rc= my_process_stmt_result(stmt);
  6850. DIE_UNLESS(rc == 1);
  6851. }
  6852. mysql_stmt_close(stmt);
  6853. rc= mysql_query(mysql, "DROP TABLE t1");
  6854. myquery(rc);
  6855. }
  6856. static void test_xjoin()
  6857. {
  6858. MYSQL_STMT *stmt;
  6859. int rc, i;
  6860. const char *query=
  6861. "select t.id, p1.value, n1.value, p2.value, n2.value from t3 t LEFT JOIN t1 p1 ON (p1.id=t.param1_id) LEFT JOIN t2 p2 ON (p2.id=t.param2_id) LEFT JOIN t4 n1 ON (n1.id=p1.name_id) LEFT JOIN t4 n2 ON (n2.id=p2.name_id) where t.id=1";
  6862. myheader("test_xjoin");
  6863. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
  6864. myquery(rc);
  6865. rc= mysql_query(mysql, "create table t3 (id int(8), param1_id int(8), param2_id int(8)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
  6866. myquery(rc);
  6867. rc= mysql_query(mysql, "create table t1 ( id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
  6868. myquery(rc);
  6869. rc= mysql_query(mysql, "create table t2 (id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  6870. myquery(rc);
  6871. rc= mysql_query(mysql, "create table t4(id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
  6872. myquery(rc);
  6873. rc= mysql_query(mysql, "insert into t3 values (1, 1, 1), (2, 2, null)");
  6874. myquery(rc);
  6875. rc= mysql_query(mysql, "insert into t1 values (1, 1, 'aaa'), (2, null, 'bbb')");
  6876. myquery(rc);
  6877. rc= mysql_query(mysql, "insert into t2 values (1, 2, 'ccc')");
  6878. myquery(rc);
  6879. rc= mysql_query(mysql, "insert into t4 values (1, 'Name1'), (2, null)");
  6880. myquery(rc);
  6881. stmt= mysql_simple_prepare(mysql, query);
  6882. check_stmt(stmt);
  6883. for (i= 0; i < 3; i++)
  6884. {
  6885. rc= mysql_stmt_execute(stmt);
  6886. check_execute(stmt, rc);
  6887. rc= my_process_stmt_result(stmt);
  6888. DIE_UNLESS(rc == 1);
  6889. }
  6890. mysql_stmt_close(stmt);
  6891. rc= mysql_query(mysql, "DROP TABLE t1, t2, t3, t4");
  6892. myquery(rc);
  6893. }
  6894. static void test_bug3035()
  6895. {
  6896. MYSQL_STMT *stmt;
  6897. int rc;
  6898. MYSQL_BIND bind_array[12], *my_bind= bind_array, *bind_end= my_bind + 12;
  6899. int8 int8_val;
  6900. uint8 uint8_val;
  6901. int16 int16_val;
  6902. uint16 uint16_val;
  6903. int32 int32_val;
  6904. uint32 uint32_val;
  6905. longlong int64_val;
  6906. ulonglong uint64_val;
  6907. double double_val, udouble_val, double_tmp;
  6908. char longlong_as_string[22], ulonglong_as_string[22];
  6909. /* mins and maxes */
  6910. const int8 int8_min= -128;
  6911. const int8 int8_max= 127;
  6912. const uint8 uint8_min= 0;
  6913. const uint8 uint8_max= 255;
  6914. const int16 int16_min= -32768;
  6915. const int16 int16_max= 32767;
  6916. const uint16 uint16_min= 0;
  6917. const uint16 uint16_max= 65535;
  6918. const int32 int32_max= 2147483647L;
  6919. const int32 int32_min= -int32_max - 1;
  6920. const uint32 uint32_min= 0;
  6921. const uint32 uint32_max= 4294967295U;
  6922. /* it might not work okay everyplace */
  6923. const longlong int64_max= LL(9223372036854775807);
  6924. const longlong int64_min= -int64_max - 1;
  6925. const ulonglong uint64_min= 0U;
  6926. const ulonglong uint64_max= ULL(18446744073709551615);
  6927. const char *stmt_text;
  6928. myheader("test_bug3035");
  6929. stmt_text= "DROP TABLE IF EXISTS t1";
  6930. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  6931. myquery(rc);
  6932. stmt_text= "CREATE TABLE t1 (i8 TINYINT, ui8 TINYINT UNSIGNED, "
  6933. "i16 SMALLINT, ui16 SMALLINT UNSIGNED, "
  6934. "i32 INT, ui32 INT UNSIGNED, "
  6935. "i64 BIGINT, ui64 BIGINT UNSIGNED, "
  6936. "id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT)";
  6937. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  6938. myquery(rc);
  6939. bzero((char*) bind_array, sizeof(bind_array));
  6940. for (my_bind= bind_array; my_bind < bind_end; my_bind++)
  6941. my_bind->error= &my_bind->error_value;
  6942. bind_array[0].buffer_type= MYSQL_TYPE_TINY;
  6943. bind_array[0].buffer= (void *) &int8_val;
  6944. bind_array[1].buffer_type= MYSQL_TYPE_TINY;
  6945. bind_array[1].buffer= (void *) &uint8_val;
  6946. bind_array[1].is_unsigned= 1;
  6947. bind_array[2].buffer_type= MYSQL_TYPE_SHORT;
  6948. bind_array[2].buffer= (void *) &int16_val;
  6949. bind_array[3].buffer_type= MYSQL_TYPE_SHORT;
  6950. bind_array[3].buffer= (void *) &uint16_val;
  6951. bind_array[3].is_unsigned= 1;
  6952. bind_array[4].buffer_type= MYSQL_TYPE_LONG;
  6953. bind_array[4].buffer= (void *) &int32_val;
  6954. bind_array[5].buffer_type= MYSQL_TYPE_LONG;
  6955. bind_array[5].buffer= (void *) &uint32_val;
  6956. bind_array[5].is_unsigned= 1;
  6957. bind_array[6].buffer_type= MYSQL_TYPE_LONGLONG;
  6958. bind_array[6].buffer= (void *) &int64_val;
  6959. bind_array[7].buffer_type= MYSQL_TYPE_LONGLONG;
  6960. bind_array[7].buffer= (void *) &uint64_val;
  6961. bind_array[7].is_unsigned= 1;
  6962. stmt= mysql_stmt_init(mysql);
  6963. check_stmt(stmt);
  6964. stmt_text= "INSERT INTO t1 (i8, ui8, i16, ui16, i32, ui32, i64, ui64) "
  6965. "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
  6966. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  6967. check_execute(stmt, rc);
  6968. mysql_stmt_bind_param(stmt, bind_array);
  6969. int8_val= int8_min;
  6970. uint8_val= uint8_min;
  6971. int16_val= int16_min;
  6972. uint16_val= uint16_min;
  6973. int32_val= int32_min;
  6974. uint32_val= uint32_min;
  6975. int64_val= int64_min;
  6976. uint64_val= uint64_min;
  6977. rc= mysql_stmt_execute(stmt);
  6978. check_execute(stmt, rc);
  6979. int8_val= int8_max;
  6980. uint8_val= uint8_max;
  6981. int16_val= int16_max;
  6982. uint16_val= uint16_max;
  6983. int32_val= int32_max;
  6984. uint32_val= uint32_max;
  6985. int64_val= int64_max;
  6986. uint64_val= uint64_max;
  6987. rc= mysql_stmt_execute(stmt);
  6988. check_execute(stmt, rc);
  6989. stmt_text= "SELECT i8, ui8, i16, ui16, i32, ui32, i64, ui64, ui64, "
  6990. "cast(ui64 as signed), ui64, cast(ui64 as signed)"
  6991. "FROM t1 ORDER BY id ASC";
  6992. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  6993. check_execute(stmt, rc);
  6994. rc= mysql_stmt_execute(stmt);
  6995. check_execute(stmt, rc);
  6996. bind_array[8].buffer_type= MYSQL_TYPE_DOUBLE;
  6997. bind_array[8].buffer= (void *) &udouble_val;
  6998. bind_array[9].buffer_type= MYSQL_TYPE_DOUBLE;
  6999. bind_array[9].buffer= (void *) &double_val;
  7000. bind_array[10].buffer_type= MYSQL_TYPE_STRING;
  7001. bind_array[10].buffer= (void *) &ulonglong_as_string;
  7002. bind_array[10].buffer_length= sizeof(ulonglong_as_string);
  7003. bind_array[11].buffer_type= MYSQL_TYPE_STRING;
  7004. bind_array[11].buffer= (void *) &longlong_as_string;
  7005. bind_array[11].buffer_length= sizeof(longlong_as_string);
  7006. mysql_stmt_bind_result(stmt, bind_array);
  7007. rc= mysql_stmt_fetch(stmt);
  7008. check_execute(stmt, rc);
  7009. DIE_UNLESS(int8_val == int8_min);
  7010. DIE_UNLESS(uint8_val == uint8_min);
  7011. DIE_UNLESS(int16_val == int16_min);
  7012. DIE_UNLESS(uint16_val == uint16_min);
  7013. DIE_UNLESS(int32_val == int32_min);
  7014. DIE_UNLESS(uint32_val == uint32_min);
  7015. DIE_UNLESS(int64_val == int64_min);
  7016. DIE_UNLESS(uint64_val == uint64_min);
  7017. DIE_UNLESS(double_val == (longlong) uint64_min);
  7018. double_tmp= ulonglong2double(uint64_val);
  7019. DIE_UNLESS(cmp_double(&udouble_val, &double_tmp));
  7020. DIE_UNLESS(!strcmp(longlong_as_string, "0"));
  7021. DIE_UNLESS(!strcmp(ulonglong_as_string, "0"));
  7022. rc= mysql_stmt_fetch(stmt);
  7023. if (!opt_silent)
  7024. {
  7025. printf("Truncation mask: ");
  7026. for (my_bind= bind_array; my_bind < bind_end; my_bind++)
  7027. printf("%d", (int) my_bind->error_value);
  7028. printf("\n");
  7029. }
  7030. DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED || rc == 0);
  7031. DIE_UNLESS(int8_val == int8_max);
  7032. DIE_UNLESS(uint8_val == uint8_max);
  7033. DIE_UNLESS(int16_val == int16_max);
  7034. DIE_UNLESS(uint16_val == uint16_max);
  7035. DIE_UNLESS(int32_val == int32_max);
  7036. DIE_UNLESS(uint32_val == uint32_max);
  7037. DIE_UNLESS(int64_val == int64_max);
  7038. DIE_UNLESS(uint64_val == uint64_max);
  7039. DIE_UNLESS(double_val == (longlong) uint64_val);
  7040. double_tmp= ulonglong2double(uint64_val);
  7041. DIE_UNLESS(cmp_double(&udouble_val, &double_tmp));
  7042. DIE_UNLESS(!strcmp(longlong_as_string, "-1"));
  7043. DIE_UNLESS(!strcmp(ulonglong_as_string, "18446744073709551615"));
  7044. rc= mysql_stmt_fetch(stmt);
  7045. DIE_UNLESS(rc == MYSQL_NO_DATA);
  7046. mysql_stmt_close(stmt);
  7047. stmt_text= "DROP TABLE t1";
  7048. mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7049. }
  7050. static void test_union2()
  7051. {
  7052. MYSQL_STMT *stmt;
  7053. int rc, i;
  7054. myheader("test_union2");
  7055. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  7056. myquery(rc);
  7057. rc= mysql_query(mysql, "CREATE TABLE t1(col1 INT, \
  7058. col2 VARCHAR(40), \
  7059. col3 SMALLINT, \
  7060. col4 TIMESTAMP)");
  7061. myquery(rc);
  7062. stmt= mysql_simple_prepare(mysql,
  7063. "select col1 FROM t1 where col1=1 union distinct "
  7064. "select col1 FROM t1 where col1=2");
  7065. check_stmt(stmt);
  7066. for (i= 0; i < 3; i++)
  7067. {
  7068. rc= mysql_stmt_execute(stmt);
  7069. check_execute(stmt, rc);
  7070. rc= my_process_stmt_result(stmt);
  7071. DIE_UNLESS(rc == 0);
  7072. }
  7073. mysql_stmt_close(stmt);
  7074. rc= mysql_query(mysql, "DROP TABLE t1");
  7075. myquery(rc);
  7076. }
  7077. /*
  7078. This tests for various mysql_stmt_send_long_data bugs described in #1664
  7079. */
  7080. static void test_bug1664()
  7081. {
  7082. MYSQL_STMT *stmt;
  7083. int rc, int_data;
  7084. const char *data;
  7085. const char *str_data= "Simple string";
  7086. MYSQL_BIND my_bind[2];
  7087. const char *query= "INSERT INTO test_long_data(col2, col1) VALUES(?, ?)";
  7088. myheader("test_bug1664");
  7089. rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
  7090. myquery(rc);
  7091. rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, col2 long varchar)");
  7092. myquery(rc);
  7093. stmt= mysql_stmt_init(mysql);
  7094. check_stmt(stmt);
  7095. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  7096. check_execute(stmt, rc);
  7097. verify_param_count(stmt, 2);
  7098. bzero((char*) my_bind, sizeof(my_bind));
  7099. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  7100. my_bind[0].buffer= (void *)str_data;
  7101. my_bind[0].buffer_length= strlen(str_data);
  7102. my_bind[1].buffer= (void *)&int_data;
  7103. my_bind[1].buffer_type= MYSQL_TYPE_LONG;
  7104. rc= mysql_stmt_bind_param(stmt, my_bind);
  7105. check_execute(stmt, rc);
  7106. int_data= 1;
  7107. /*
  7108. Let us supply empty long_data. This should work and should
  7109. not break following execution.
  7110. */
  7111. data= "";
  7112. rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
  7113. check_execute(stmt, rc);
  7114. rc= mysql_stmt_execute(stmt);
  7115. check_execute(stmt, rc);
  7116. verify_col_data("test_long_data", "col1", "1");
  7117. verify_col_data("test_long_data", "col2", "");
  7118. rc= mysql_query(mysql, "DELETE FROM test_long_data");
  7119. myquery(rc);
  7120. /* This should pass OK */
  7121. data= (char *)"Data";
  7122. rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
  7123. check_execute(stmt, rc);
  7124. rc= mysql_stmt_execute(stmt);
  7125. check_execute(stmt, rc);
  7126. verify_col_data("test_long_data", "col1", "1");
  7127. verify_col_data("test_long_data", "col2", "Data");
  7128. /* clean up */
  7129. rc= mysql_query(mysql, "DELETE FROM test_long_data");
  7130. myquery(rc);
  7131. /*
  7132. Now we are changing int parameter and don't do anything
  7133. with first parameter. Second mysql_stmt_execute() should run
  7134. OK treating this first parameter as string parameter.
  7135. */
  7136. int_data= 2;
  7137. /* execute */
  7138. rc= mysql_stmt_execute(stmt);
  7139. check_execute(stmt, rc);
  7140. verify_col_data("test_long_data", "col1", "2");
  7141. verify_col_data("test_long_data", "col2", str_data);
  7142. /* clean up */
  7143. rc= mysql_query(mysql, "DELETE FROM test_long_data");
  7144. myquery(rc);
  7145. /*
  7146. Now we are sending other long data. It should not be
  7147. concatened to previous.
  7148. */
  7149. data= (char *)"SomeOtherData";
  7150. rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
  7151. check_execute(stmt, rc);
  7152. rc= mysql_stmt_execute(stmt);
  7153. check_execute(stmt, rc);
  7154. verify_col_data("test_long_data", "col1", "2");
  7155. verify_col_data("test_long_data", "col2", "SomeOtherData");
  7156. mysql_stmt_close(stmt);
  7157. /* clean up */
  7158. rc= mysql_query(mysql, "DELETE FROM test_long_data");
  7159. myquery(rc);
  7160. /* Now let us test how mysql_stmt_reset works. */
  7161. stmt= mysql_stmt_init(mysql);
  7162. check_stmt(stmt);
  7163. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  7164. check_execute(stmt, rc);
  7165. rc= mysql_stmt_bind_param(stmt, my_bind);
  7166. check_execute(stmt, rc);
  7167. data= (char *)"SomeData";
  7168. rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
  7169. check_execute(stmt, rc);
  7170. rc= mysql_stmt_reset(stmt);
  7171. check_execute(stmt, rc);
  7172. rc= mysql_stmt_execute(stmt);
  7173. check_execute(stmt, rc);
  7174. verify_col_data("test_long_data", "col1", "2");
  7175. verify_col_data("test_long_data", "col2", str_data);
  7176. mysql_stmt_close(stmt);
  7177. /* Final clean up */
  7178. rc= mysql_query(mysql, "DROP TABLE test_long_data");
  7179. myquery(rc);
  7180. }
  7181. static void test_order_param()
  7182. {
  7183. MYSQL_STMT *stmt;
  7184. int rc;
  7185. myheader("test_order_param");
  7186. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  7187. myquery(rc);
  7188. rc= mysql_query(mysql, "CREATE TABLE t1(a INT, b char(10))");
  7189. myquery(rc);
  7190. stmt= mysql_simple_prepare(mysql,
  7191. "select sum(a) + 200, 1 from t1 "
  7192. " union distinct "
  7193. "select sum(a) + 200, 1 from t1 group by b ");
  7194. check_stmt(stmt);
  7195. mysql_stmt_close(stmt);
  7196. stmt= mysql_simple_prepare(mysql,
  7197. "select sum(a) + 200, ? from t1 group by b "
  7198. " union distinct "
  7199. "select sum(a) + 200, 1 from t1 group by b ");
  7200. check_stmt(stmt);
  7201. mysql_stmt_close(stmt);
  7202. stmt= mysql_simple_prepare(mysql,
  7203. "select sum(a) + 200, ? from t1 "
  7204. " union distinct "
  7205. "select sum(a) + 200, 1 from t1 group by b ");
  7206. check_stmt(stmt);
  7207. mysql_stmt_close(stmt);
  7208. rc= mysql_query(mysql, "DROP TABLE t1");
  7209. myquery(rc);
  7210. }
  7211. static void test_union_param()
  7212. {
  7213. MYSQL_STMT *stmt;
  7214. char *query;
  7215. int rc, i;
  7216. MYSQL_BIND my_bind[2];
  7217. char my_val[4];
  7218. ulong my_length= 3L;
  7219. my_bool my_null= FALSE;
  7220. myheader("test_union_param");
  7221. strmov(my_val, "abc");
  7222. query= (char*)"select ? as my_col union distinct select ?";
  7223. stmt= mysql_simple_prepare(mysql, query);
  7224. check_stmt(stmt);
  7225. /*
  7226. We need to bzero bind structure because mysql_stmt_bind_param checks all
  7227. its members.
  7228. */
  7229. bzero((char*) my_bind, sizeof(my_bind));
  7230. /* bind parameters */
  7231. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  7232. my_bind[0].buffer= (char*) &my_val;
  7233. my_bind[0].buffer_length= 4;
  7234. my_bind[0].length= &my_length;
  7235. my_bind[0].is_null= (char*)&my_null;
  7236. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  7237. my_bind[1].buffer= (char*) &my_val;
  7238. my_bind[1].buffer_length= 4;
  7239. my_bind[1].length= &my_length;
  7240. my_bind[1].is_null= (char*)&my_null;
  7241. rc= mysql_stmt_bind_param(stmt, my_bind);
  7242. check_execute(stmt, rc);
  7243. for (i= 0; i < 3; i++)
  7244. {
  7245. rc= mysql_stmt_execute(stmt);
  7246. check_execute(stmt, rc);
  7247. rc= my_process_stmt_result(stmt);
  7248. DIE_UNLESS(rc == 1);
  7249. }
  7250. mysql_stmt_close(stmt);
  7251. }
  7252. static void test_ps_i18n()
  7253. {
  7254. MYSQL_STMT *stmt;
  7255. int rc;
  7256. const char *stmt_text;
  7257. MYSQL_BIND bind_array[2];
  7258. /* Represented as numbers to keep UTF8 tools from clobbering them. */
  7259. const char *koi8= "\xee\xd5\x2c\x20\xda\xc1\x20\xd2\xd9\xc2\xc1\xcc\xcb\xd5";
  7260. const char *cp1251= "\xcd\xf3\x2c\x20\xe7\xe0\x20\xf0\xfb\xe1\xe0\xeb\xea\xf3";
  7261. char buf1[16], buf2[16];
  7262. ulong buf1_len, buf2_len;
  7263. myheader("test_ps_i18n");
  7264. stmt_text= "DROP TABLE IF EXISTS t1";
  7265. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7266. myquery(rc);
  7267. /*
  7268. Create table with binary columns, set session character set to cp1251,
  7269. client character set to koi8, and make sure that there is conversion
  7270. on insert and no conversion on select
  7271. */
  7272. stmt_text= "CREATE TABLE t1 (c1 VARBINARY(255), c2 VARBINARY(255))";
  7273. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7274. myquery(rc);
  7275. stmt_text= "SET CHARACTER_SET_CLIENT=koi8r, "
  7276. "CHARACTER_SET_CONNECTION=cp1251, "
  7277. "CHARACTER_SET_RESULTS=koi8r";
  7278. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7279. myquery(rc);
  7280. bzero((char*) bind_array, sizeof(bind_array));
  7281. bind_array[0].buffer_type= MYSQL_TYPE_STRING;
  7282. bind_array[0].buffer= (void *) koi8;
  7283. bind_array[0].buffer_length= strlen(koi8);
  7284. bind_array[1].buffer_type= MYSQL_TYPE_STRING;
  7285. bind_array[1].buffer= (void *) koi8;
  7286. bind_array[1].buffer_length= strlen(koi8);
  7287. stmt= mysql_stmt_init(mysql);
  7288. check_stmt(stmt);
  7289. stmt_text= "INSERT INTO t1 (c1, c2) VALUES (?, ?)";
  7290. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  7291. check_execute(stmt, rc);
  7292. mysql_stmt_bind_param(stmt, bind_array);
  7293. mysql_stmt_send_long_data(stmt, 0, koi8, strlen(koi8));
  7294. rc= mysql_stmt_execute(stmt);
  7295. check_execute(stmt, rc);
  7296. stmt_text= "SELECT c1, c2 FROM t1";
  7297. /* c1 and c2 are binary so no conversion will be done on select */
  7298. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  7299. check_execute(stmt, rc);
  7300. rc= mysql_stmt_execute(stmt);
  7301. check_execute(stmt, rc);
  7302. bind_array[0].buffer= buf1;
  7303. bind_array[0].buffer_length= sizeof(buf1);
  7304. bind_array[0].length= &buf1_len;
  7305. bind_array[1].buffer= buf2;
  7306. bind_array[1].buffer_length= sizeof(buf2);
  7307. bind_array[1].length= &buf2_len;
  7308. mysql_stmt_bind_result(stmt, bind_array);
  7309. rc= mysql_stmt_fetch(stmt);
  7310. check_execute(stmt, rc);
  7311. DIE_UNLESS(buf1_len == strlen(cp1251));
  7312. DIE_UNLESS(buf2_len == strlen(cp1251));
  7313. DIE_UNLESS(!memcmp(buf1, cp1251, buf1_len));
  7314. DIE_UNLESS(!memcmp(buf2, cp1251, buf1_len));
  7315. rc= mysql_stmt_fetch(stmt);
  7316. DIE_UNLESS(rc == MYSQL_NO_DATA);
  7317. stmt_text= "DROP TABLE IF EXISTS t1";
  7318. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7319. myquery(rc);
  7320. /*
  7321. Now create table with two cp1251 columns, set client character
  7322. set to koi8 and supply columns of one row as string and another as
  7323. binary data. Binary data must not be converted on insert, and both
  7324. columns must be converted to client character set on select.
  7325. */
  7326. stmt_text= "CREATE TABLE t1 (c1 VARCHAR(255) CHARACTER SET cp1251, "
  7327. "c2 VARCHAR(255) CHARACTER SET cp1251)";
  7328. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7329. myquery(rc);
  7330. stmt_text= "INSERT INTO t1 (c1, c2) VALUES (?, ?)";
  7331. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  7332. check_execute(stmt, rc);
  7333. /* this data must be converted */
  7334. bind_array[0].buffer_type= MYSQL_TYPE_STRING;
  7335. bind_array[0].buffer= (void *) koi8;
  7336. bind_array[0].buffer_length= strlen(koi8);
  7337. bind_array[1].buffer_type= MYSQL_TYPE_STRING;
  7338. bind_array[1].buffer= (void *) koi8;
  7339. bind_array[1].buffer_length= strlen(koi8);
  7340. mysql_stmt_bind_param(stmt, bind_array);
  7341. mysql_stmt_send_long_data(stmt, 0, koi8, strlen(koi8));
  7342. rc= mysql_stmt_execute(stmt);
  7343. check_execute(stmt, rc);
  7344. /* this data must not be converted */
  7345. bind_array[0].buffer_type= MYSQL_TYPE_BLOB;
  7346. bind_array[0].buffer= (void *) cp1251;
  7347. bind_array[0].buffer_length= strlen(cp1251);
  7348. bind_array[1].buffer_type= MYSQL_TYPE_BLOB;
  7349. bind_array[1].buffer= (void *) cp1251;
  7350. bind_array[1].buffer_length= strlen(cp1251);
  7351. mysql_stmt_bind_param(stmt, bind_array);
  7352. mysql_stmt_send_long_data(stmt, 0, cp1251, strlen(cp1251));
  7353. rc= mysql_stmt_execute(stmt);
  7354. check_execute(stmt, rc);
  7355. /* Fetch data and verify that rows are in koi8 */
  7356. stmt_text= "SELECT c1, c2 FROM t1";
  7357. /* c1 and c2 are binary so no conversion will be done on select */
  7358. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  7359. check_execute(stmt, rc);
  7360. rc= mysql_stmt_execute(stmt);
  7361. check_execute(stmt, rc);
  7362. bind_array[0].buffer= buf1;
  7363. bind_array[0].buffer_length= sizeof(buf1);
  7364. bind_array[0].length= &buf1_len;
  7365. bind_array[1].buffer= buf2;
  7366. bind_array[1].buffer_length= sizeof(buf2);
  7367. bind_array[1].length= &buf2_len;
  7368. mysql_stmt_bind_result(stmt, bind_array);
  7369. while ((rc= mysql_stmt_fetch(stmt)) == 0)
  7370. {
  7371. DIE_UNLESS(buf1_len == strlen(koi8));
  7372. DIE_UNLESS(buf2_len == strlen(koi8));
  7373. DIE_UNLESS(!memcmp(buf1, koi8, buf1_len));
  7374. DIE_UNLESS(!memcmp(buf2, koi8, buf1_len));
  7375. }
  7376. DIE_UNLESS(rc == MYSQL_NO_DATA);
  7377. mysql_stmt_close(stmt);
  7378. stmt_text= "DROP TABLE t1";
  7379. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7380. myquery(rc);
  7381. stmt_text= "SET NAMES DEFAULT";
  7382. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7383. myquery(rc);
  7384. }
  7385. static void test_bug3796()
  7386. {
  7387. MYSQL_STMT *stmt;
  7388. MYSQL_BIND my_bind[1];
  7389. const char *concat_arg0= "concat_with_";
  7390. enum { OUT_BUFF_SIZE= 30 };
  7391. char out_buff[OUT_BUFF_SIZE];
  7392. char canonical_buff[OUT_BUFF_SIZE];
  7393. ulong out_length;
  7394. const char *stmt_text;
  7395. int rc;
  7396. myheader("test_bug3796");
  7397. /* Create and fill test table */
  7398. stmt_text= "DROP TABLE IF EXISTS t1";
  7399. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7400. myquery(rc);
  7401. stmt_text= "CREATE TABLE t1 (a INT, b VARCHAR(30))";
  7402. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7403. myquery(rc);
  7404. stmt_text= "INSERT INTO t1 VALUES(1, 'ONE'), (2, 'TWO')";
  7405. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7406. myquery(rc);
  7407. /* Create statement handle and prepare it with select */
  7408. stmt= mysql_stmt_init(mysql);
  7409. stmt_text= "SELECT concat(?, b) FROM t1";
  7410. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  7411. check_execute(stmt, rc);
  7412. /* Bind input buffers */
  7413. bzero((char*) my_bind, sizeof(my_bind));
  7414. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  7415. my_bind[0].buffer= (void *) concat_arg0;
  7416. my_bind[0].buffer_length= strlen(concat_arg0);
  7417. mysql_stmt_bind_param(stmt, my_bind);
  7418. /* Execute the select statement */
  7419. rc= mysql_stmt_execute(stmt);
  7420. check_execute(stmt, rc);
  7421. my_bind[0].buffer= (void *) out_buff;
  7422. my_bind[0].buffer_length= OUT_BUFF_SIZE;
  7423. my_bind[0].length= &out_length;
  7424. mysql_stmt_bind_result(stmt, my_bind);
  7425. rc= mysql_stmt_fetch(stmt);
  7426. if (!opt_silent)
  7427. printf("Concat result: '%s'\n", out_buff);
  7428. check_execute(stmt, rc);
  7429. strmov(canonical_buff, concat_arg0);
  7430. strcat(canonical_buff, "ONE");
  7431. DIE_UNLESS(strlen(canonical_buff) == out_length &&
  7432. strncmp(out_buff, canonical_buff, out_length) == 0);
  7433. rc= mysql_stmt_fetch(stmt);
  7434. check_execute(stmt, rc);
  7435. strmov(canonical_buff + strlen(concat_arg0), "TWO");
  7436. DIE_UNLESS(strlen(canonical_buff) == out_length &&
  7437. strncmp(out_buff, canonical_buff, out_length) == 0);
  7438. if (!opt_silent)
  7439. printf("Concat result: '%s'\n", out_buff);
  7440. rc= mysql_stmt_fetch(stmt);
  7441. DIE_UNLESS(rc == MYSQL_NO_DATA);
  7442. mysql_stmt_close(stmt);
  7443. stmt_text= "DROP TABLE IF EXISTS t1";
  7444. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  7445. myquery(rc);
  7446. }
  7447. static void test_bug4026()
  7448. {
  7449. MYSQL_STMT *stmt;
  7450. MYSQL_BIND my_bind[2];
  7451. MYSQL_TIME time_in, time_out;
  7452. MYSQL_TIME datetime_in, datetime_out;
  7453. const char *stmt_text;
  7454. int rc;
  7455. myheader("test_bug4026");
  7456. /* Check that microseconds are inserted and selected successfully */
  7457. /* Create a statement handle and prepare it with select */
  7458. stmt= mysql_stmt_init(mysql);
  7459. stmt_text= "SELECT ?, ?";
  7460. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  7461. check_execute(stmt, rc);
  7462. /* Bind input buffers */
  7463. bzero((char*) my_bind, sizeof(my_bind));
  7464. bzero((char*) &time_in, sizeof(time_in));
  7465. bzero((char*) &time_out, sizeof(time_out));
  7466. bzero((char*) &datetime_in, sizeof(datetime_in));
  7467. bzero((char*) &datetime_out, sizeof(datetime_out));
  7468. my_bind[0].buffer_type= MYSQL_TYPE_TIME;
  7469. my_bind[0].buffer= (void *) &time_in;
  7470. my_bind[1].buffer_type= MYSQL_TYPE_DATETIME;
  7471. my_bind[1].buffer= (void *) &datetime_in;
  7472. time_in.hour= 23;
  7473. time_in.minute= 59;
  7474. time_in.second= 59;
  7475. time_in.second_part= 123456;
  7476. /*
  7477. This is not necessary, just to make DIE_UNLESS below work: this field
  7478. is filled in when time is received from server
  7479. */
  7480. time_in.time_type= MYSQL_TIMESTAMP_TIME;
  7481. datetime_in= time_in;
  7482. datetime_in.year= 2003;
  7483. datetime_in.month= 12;
  7484. datetime_in.day= 31;
  7485. datetime_in.time_type= MYSQL_TIMESTAMP_DATETIME;
  7486. mysql_stmt_bind_param(stmt, my_bind);
  7487. /* Execute the select statement */
  7488. rc= mysql_stmt_execute(stmt);
  7489. check_execute(stmt, rc);
  7490. my_bind[0].buffer= (void *) &time_out;
  7491. my_bind[1].buffer= (void *) &datetime_out;
  7492. mysql_stmt_bind_result(stmt, my_bind);
  7493. rc= mysql_stmt_fetch(stmt);
  7494. DIE_UNLESS(rc == 0);
  7495. if (!opt_silent)
  7496. {
  7497. printf("%d:%d:%d.%lu\n", time_out.hour, time_out.minute, time_out.second,
  7498. time_out.second_part);
  7499. printf("%d-%d-%d %d:%d:%d.%lu\n", datetime_out.year, datetime_out.month,
  7500. datetime_out.day, datetime_out.hour,
  7501. datetime_out.minute, datetime_out.second,
  7502. datetime_out.second_part);
  7503. }
  7504. DIE_UNLESS(memcmp(&time_in, &time_out, sizeof(time_in)) == 0);
  7505. DIE_UNLESS(memcmp(&datetime_in, &datetime_out, sizeof(datetime_in)) == 0);
  7506. mysql_stmt_close(stmt);
  7507. }
  7508. static void test_bug4079()
  7509. {
  7510. MYSQL_STMT *stmt;
  7511. MYSQL_BIND my_bind[1];
  7512. const char *stmt_text;
  7513. uint32 res;
  7514. int rc;
  7515. myheader("test_bug4079");
  7516. /* Create and fill table */
  7517. mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  7518. mysql_query(mysql, "CREATE TABLE t1 (a int)");
  7519. mysql_query(mysql, "INSERT INTO t1 VALUES (1), (2)");
  7520. /* Prepare erroneous statement */
  7521. stmt= mysql_stmt_init(mysql);
  7522. stmt_text= "SELECT 1 < (SELECT a FROM t1)";
  7523. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  7524. check_execute(stmt, rc);
  7525. /* Execute the select statement */
  7526. rc= mysql_stmt_execute(stmt);
  7527. check_execute(stmt, rc);
  7528. /* Bind input buffers */
  7529. bzero((char*) my_bind, sizeof(my_bind));
  7530. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  7531. my_bind[0].buffer= (void *) &res;
  7532. mysql_stmt_bind_result(stmt, my_bind);
  7533. rc= mysql_stmt_fetch(stmt);
  7534. DIE_UNLESS(rc != 0 && rc != MYSQL_NO_DATA);
  7535. if (!opt_silent)
  7536. printf("Got error from mysql_stmt_fetch (as expected):\n%s\n",
  7537. mysql_stmt_error(stmt));
  7538. /* buggy version of libmysql hanged up here */
  7539. mysql_stmt_close(stmt);
  7540. }
  7541. static void test_bug4236()
  7542. {
  7543. MYSQL_STMT *stmt;
  7544. const char *stmt_text;
  7545. int rc;
  7546. MYSQL_STMT backup;
  7547. myheader("test_bug4236");
  7548. stmt= mysql_stmt_init(mysql);
  7549. /* mysql_stmt_execute() of statement with statement id= 0 crashed server */
  7550. stmt_text= "SELECT 1";
  7551. /* We need to prepare statement to pass by possible check in libmysql */
  7552. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  7553. check_execute(stmt, rc);
  7554. /* Hack to check that server works OK if statement wasn't found */
  7555. backup.stmt_id= stmt->stmt_id;
  7556. stmt->stmt_id= 0;
  7557. rc= mysql_stmt_execute(stmt);
  7558. DIE_UNLESS(rc);
  7559. /* Restore original statement id to be able to reprepare it */
  7560. stmt->stmt_id= backup.stmt_id;
  7561. mysql_stmt_close(stmt);
  7562. }
  7563. static void test_bug4030()
  7564. {
  7565. MYSQL_STMT *stmt;
  7566. MYSQL_BIND my_bind[3];
  7567. MYSQL_TIME time_canonical, time_out;
  7568. MYSQL_TIME date_canonical, date_out;
  7569. MYSQL_TIME datetime_canonical, datetime_out;
  7570. const char *stmt_text;
  7571. int rc;
  7572. myheader("test_bug4030");
  7573. /* Check that microseconds are inserted and selected successfully */
  7574. /* Execute a query with time values in prepared mode */
  7575. stmt= mysql_stmt_init(mysql);
  7576. stmt_text= "SELECT '23:59:59.123456', '2003-12-31', "
  7577. "'2003-12-31 23:59:59.123456'";
  7578. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  7579. check_execute(stmt, rc);
  7580. rc= mysql_stmt_execute(stmt);
  7581. check_execute(stmt, rc);
  7582. /* Bind output buffers */
  7583. bzero((char*) my_bind, sizeof(my_bind));
  7584. bzero((char*) &time_canonical, sizeof(time_canonical));
  7585. bzero((char*) &time_out, sizeof(time_out));
  7586. bzero((char*) &date_canonical, sizeof(date_canonical));
  7587. bzero((char*) &date_out, sizeof(date_out));
  7588. bzero((char*) &datetime_canonical, sizeof(datetime_canonical));
  7589. bzero((char*) &datetime_out, sizeof(datetime_out));
  7590. my_bind[0].buffer_type= MYSQL_TYPE_TIME;
  7591. my_bind[0].buffer= (void *) &time_out;
  7592. my_bind[1].buffer_type= MYSQL_TYPE_DATE;
  7593. my_bind[1].buffer= (void *) &date_out;
  7594. my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
  7595. my_bind[2].buffer= (void *) &datetime_out;
  7596. time_canonical.hour= 23;
  7597. time_canonical.minute= 59;
  7598. time_canonical.second= 59;
  7599. time_canonical.second_part= 123456;
  7600. time_canonical.time_type= MYSQL_TIMESTAMP_TIME;
  7601. date_canonical.year= 2003;
  7602. date_canonical.month= 12;
  7603. date_canonical.day= 31;
  7604. date_canonical.time_type= MYSQL_TIMESTAMP_DATE;
  7605. datetime_canonical= time_canonical;
  7606. datetime_canonical.year= 2003;
  7607. datetime_canonical.month= 12;
  7608. datetime_canonical.day= 31;
  7609. datetime_canonical.time_type= MYSQL_TIMESTAMP_DATETIME;
  7610. mysql_stmt_bind_result(stmt, my_bind);
  7611. rc= mysql_stmt_fetch(stmt);
  7612. DIE_UNLESS(rc == 0);
  7613. if (!opt_silent)
  7614. {
  7615. printf("%d:%d:%d.%lu\n", time_out.hour, time_out.minute, time_out.second,
  7616. time_out.second_part);
  7617. printf("%d-%d-%d\n", date_out.year, date_out.month, date_out.day);
  7618. printf("%d-%d-%d %d:%d:%d.%lu\n", datetime_out.year, datetime_out.month,
  7619. datetime_out.day, datetime_out.hour,
  7620. datetime_out.minute, datetime_out.second,
  7621. datetime_out.second_part);
  7622. }
  7623. DIE_UNLESS(memcmp(&time_canonical, &time_out, sizeof(time_out)) == 0);
  7624. DIE_UNLESS(memcmp(&date_canonical, &date_out, sizeof(date_out)) == 0);
  7625. DIE_UNLESS(memcmp(&datetime_canonical, &datetime_out, sizeof(datetime_out)) == 0);
  7626. mysql_stmt_close(stmt);
  7627. }
  7628. static void test_view()
  7629. {
  7630. MYSQL_STMT *stmt;
  7631. int rc, i;
  7632. MYSQL_BIND my_bind[1];
  7633. char str_data[50];
  7634. ulong length = 0L;
  7635. long is_null = 0L;
  7636. const char *query=
  7637. "SELECT COUNT(*) FROM v1 WHERE SERVERNAME=?";
  7638. myheader("test_view");
  7639. rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,t2,t3,v1");
  7640. myquery(rc);
  7641. rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1,t2,t3");
  7642. myquery(rc);
  7643. rc= mysql_query(mysql,"CREATE TABLE t1 ("
  7644. " SERVERGRP varchar(20) NOT NULL default '', "
  7645. " DBINSTANCE varchar(20) NOT NULL default '', "
  7646. " PRIMARY KEY (SERVERGRP)) "
  7647. " CHARSET=latin1 collate=latin1_bin");
  7648. myquery(rc);
  7649. rc= mysql_query(mysql,"CREATE TABLE t2 ("
  7650. " SERVERNAME varchar(20) NOT NULL, "
  7651. " SERVERGRP varchar(20) NOT NULL, "
  7652. " PRIMARY KEY (SERVERNAME)) "
  7653. " CHARSET=latin1 COLLATE latin1_bin");
  7654. myquery(rc);
  7655. rc= mysql_query(mysql,
  7656. "CREATE TABLE t3 ("
  7657. " SERVERGRP varchar(20) BINARY NOT NULL, "
  7658. " TABNAME varchar(30) NOT NULL, MAPSTATE char(1) NOT NULL, "
  7659. " ACTSTATE char(1) NOT NULL , "
  7660. " LOCAL_NAME varchar(30) NOT NULL, "
  7661. " CHG_DATE varchar(8) NOT NULL default '00000000', "
  7662. " CHG_TIME varchar(6) NOT NULL default '000000', "
  7663. " MXUSER varchar(12) NOT NULL default '', "
  7664. " PRIMARY KEY (SERVERGRP, TABNAME, MAPSTATE, ACTSTATE, "
  7665. " LOCAL_NAME)) CHARSET=latin1 COLLATE latin1_bin");
  7666. myquery(rc);
  7667. rc= mysql_query(mysql,"CREATE VIEW v1 AS select sql_no_cache"
  7668. " T0001.SERVERNAME AS SERVERNAME, T0003.TABNAME AS"
  7669. " TABNAME,T0003.LOCAL_NAME AS LOCAL_NAME,T0002.DBINSTANCE AS"
  7670. " DBINSTANCE from t2 T0001 join t1 T0002 join t3 T0003 where"
  7671. " ((T0002.SERVERGRP = T0001.SERVERGRP) and"
  7672. " (T0002.SERVERGRP = T0003.SERVERGRP)"
  7673. " and (T0003.MAPSTATE = _latin1'A') and"
  7674. " (T0003.ACTSTATE = _latin1' '))");
  7675. myquery(rc);
  7676. stmt= mysql_stmt_init(mysql);
  7677. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  7678. check_execute(stmt, rc);
  7679. strmov(str_data, "TEST");
  7680. bzero((char*) my_bind, sizeof(my_bind));
  7681. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  7682. my_bind[0].buffer= (char *)&str_data;
  7683. my_bind[0].buffer_length= 50;
  7684. my_bind[0].length= &length;
  7685. length= 4;
  7686. my_bind[0].is_null= (char*)&is_null;
  7687. rc= mysql_stmt_bind_param(stmt, my_bind);
  7688. check_execute(stmt,rc);
  7689. for (i= 0; i < 3; i++)
  7690. {
  7691. rc= mysql_stmt_execute(stmt);
  7692. check_execute(stmt, rc);
  7693. rc= my_process_stmt_result(stmt);
  7694. DIE_UNLESS(1 == rc);
  7695. }
  7696. mysql_stmt_close(stmt);
  7697. rc= mysql_query(mysql, "DROP TABLE t1,t2,t3");
  7698. myquery(rc);
  7699. rc= mysql_query(mysql, "DROP VIEW v1");
  7700. myquery(rc);
  7701. }
  7702. static void test_view_where()
  7703. {
  7704. MYSQL_STMT *stmt;
  7705. int rc, i;
  7706. const char *query=
  7707. "select v1.c,v2.c from v1, v2";
  7708. myheader("test_view_where");
  7709. rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1,v2");
  7710. myquery(rc);
  7711. rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,v2,t1");
  7712. myquery(rc);
  7713. rc= mysql_query(mysql,"CREATE TABLE t1 (a int, b int)");
  7714. myquery(rc);
  7715. rc= mysql_query(mysql,"insert into t1 values (1,2), (1,3), (2,4), (2,5), (3,10)");
  7716. myquery(rc);
  7717. rc= mysql_query(mysql,"create view v1 (c) as select b from t1 where a<3");
  7718. myquery(rc);
  7719. rc= mysql_query(mysql,"create view v2 (c) as select b from t1 where a>=3");
  7720. myquery(rc);
  7721. stmt= mysql_stmt_init(mysql);
  7722. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  7723. check_execute(stmt, rc);
  7724. for (i= 0; i < 3; i++)
  7725. {
  7726. rc= mysql_stmt_execute(stmt);
  7727. check_execute(stmt, rc);
  7728. rc= my_process_stmt_result(stmt);
  7729. DIE_UNLESS(4 == rc);
  7730. }
  7731. mysql_stmt_close(stmt);
  7732. rc= mysql_query(mysql, "DROP TABLE t1");
  7733. myquery(rc);
  7734. rc= mysql_query(mysql, "DROP VIEW v1, v2");
  7735. myquery(rc);
  7736. }
  7737. static void test_view_2where()
  7738. {
  7739. MYSQL_STMT *stmt;
  7740. int rc, i;
  7741. MYSQL_BIND my_bind[8];
  7742. char parms[8][100];
  7743. ulong length[8];
  7744. const char *query=
  7745. "select relid, report, handle, log_group, username, variant, type, "
  7746. "version, erfdat, erftime, erfname, aedat, aetime, aename, dependvars, "
  7747. "inactive from V_LTDX where mandt = ? and relid = ? and report = ? and "
  7748. "handle = ? and log_group = ? and username in ( ? , ? ) and type = ?";
  7749. myheader("test_view_2where");
  7750. rc= mysql_query(mysql, "DROP TABLE IF EXISTS LTDX");
  7751. myquery(rc);
  7752. rc= mysql_query(mysql, "DROP VIEW IF EXISTS V_LTDX");
  7753. myquery(rc);
  7754. rc= mysql_query(mysql,
  7755. "CREATE TABLE LTDX (MANDT char(3) NOT NULL default '000', "
  7756. " RELID char(2) NOT NULL, REPORT varchar(40) NOT NULL,"
  7757. " HANDLE varchar(4) NOT NULL, LOG_GROUP varchar(4) NOT NULL,"
  7758. " USERNAME varchar(12) NOT NULL,"
  7759. " VARIANT varchar(12) NOT NULL,"
  7760. " TYPE char(1) NOT NULL, SRTF2 int(11) NOT NULL,"
  7761. " VERSION varchar(6) NOT NULL default '000000',"
  7762. " ERFDAT varchar(8) NOT NULL default '00000000',"
  7763. " ERFTIME varchar(6) NOT NULL default '000000',"
  7764. " ERFNAME varchar(12) NOT NULL,"
  7765. " AEDAT varchar(8) NOT NULL default '00000000',"
  7766. " AETIME varchar(6) NOT NULL default '000000',"
  7767. " AENAME varchar(12) NOT NULL,"
  7768. " DEPENDVARS varchar(10) NOT NULL,"
  7769. " INACTIVE char(1) NOT NULL, CLUSTR smallint(6) NOT NULL,"
  7770. " CLUSTD blob,"
  7771. " PRIMARY KEY (MANDT, RELID, REPORT, HANDLE, LOG_GROUP, "
  7772. "USERNAME, VARIANT, TYPE, SRTF2))"
  7773. " CHARSET=latin1 COLLATE latin1_bin");
  7774. myquery(rc);
  7775. rc= mysql_query(mysql,
  7776. "CREATE VIEW V_LTDX AS select T0001.MANDT AS "
  7777. " MANDT,T0001.RELID AS RELID,T0001.REPORT AS "
  7778. " REPORT,T0001.HANDLE AS HANDLE,T0001.LOG_GROUP AS "
  7779. " LOG_GROUP,T0001.USERNAME AS USERNAME,T0001.VARIANT AS "
  7780. " VARIANT,T0001.TYPE AS TYPE,T0001.VERSION AS "
  7781. " VERSION,T0001.ERFDAT AS ERFDAT,T0001.ERFTIME AS "
  7782. " ERFTIME,T0001.ERFNAME AS ERFNAME,T0001.AEDAT AS "
  7783. " AEDAT,T0001.AETIME AS AETIME,T0001.AENAME AS "
  7784. " AENAME,T0001.DEPENDVARS AS DEPENDVARS,T0001.INACTIVE AS "
  7785. " INACTIVE from LTDX T0001 where (T0001.SRTF2 = 0)");
  7786. myquery(rc);
  7787. bzero((char*) my_bind, sizeof(my_bind));
  7788. for (i=0; i < 8; i++) {
  7789. strmov(parms[i], "1");
  7790. my_bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
  7791. my_bind[i].buffer = (char *)&parms[i];
  7792. my_bind[i].buffer_length = 100;
  7793. my_bind[i].is_null = 0;
  7794. my_bind[i].length = &length[i];
  7795. length[i] = 1;
  7796. }
  7797. stmt= mysql_stmt_init(mysql);
  7798. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  7799. check_execute(stmt, rc);
  7800. rc= mysql_stmt_bind_param(stmt, my_bind);
  7801. check_execute(stmt,rc);
  7802. rc= mysql_stmt_execute(stmt);
  7803. check_execute(stmt, rc);
  7804. rc= my_process_stmt_result(stmt);
  7805. DIE_UNLESS(0 == rc);
  7806. mysql_stmt_close(stmt);
  7807. rc= mysql_query(mysql, "DROP VIEW V_LTDX");
  7808. myquery(rc);
  7809. rc= mysql_query(mysql, "DROP TABLE LTDX");
  7810. myquery(rc);
  7811. }
  7812. static void test_view_star()
  7813. {
  7814. MYSQL_STMT *stmt;
  7815. int rc, i;
  7816. MYSQL_BIND my_bind[8];
  7817. char parms[8][100];
  7818. ulong length[8];
  7819. const char *query= "SELECT * FROM vt1 WHERE a IN (?,?)";
  7820. myheader("test_view_star");
  7821. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, vt1");
  7822. myquery(rc);
  7823. rc= mysql_query(mysql, "DROP VIEW IF EXISTS t1, vt1");
  7824. myquery(rc);
  7825. rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
  7826. myquery(rc);
  7827. rc= mysql_query(mysql, "CREATE VIEW vt1 AS SELECT a FROM t1");
  7828. myquery(rc);
  7829. bzero((char*) my_bind, sizeof(my_bind));
  7830. for (i= 0; i < 2; i++) {
  7831. sprintf((char *)&parms[i], "%d", i);
  7832. my_bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
  7833. my_bind[i].buffer = (char *)&parms[i];
  7834. my_bind[i].buffer_length = 100;
  7835. my_bind[i].is_null = 0;
  7836. my_bind[i].length = &length[i];
  7837. length[i] = 1;
  7838. }
  7839. stmt= mysql_stmt_init(mysql);
  7840. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  7841. check_execute(stmt, rc);
  7842. rc= mysql_stmt_bind_param(stmt, my_bind);
  7843. check_execute(stmt,rc);
  7844. for (i= 0; i < 3; i++)
  7845. {
  7846. rc= mysql_stmt_execute(stmt);
  7847. check_execute(stmt, rc);
  7848. rc= my_process_stmt_result(stmt);
  7849. DIE_UNLESS(0 == rc);
  7850. }
  7851. mysql_stmt_close(stmt);
  7852. rc= mysql_query(mysql, "DROP TABLE t1");
  7853. myquery(rc);
  7854. rc= mysql_query(mysql, "DROP VIEW vt1");
  7855. myquery(rc);
  7856. }
  7857. static void test_view_insert()
  7858. {
  7859. MYSQL_STMT *insert_stmt, *select_stmt;
  7860. int rc, i;
  7861. MYSQL_BIND my_bind[1];
  7862. int my_val = 0;
  7863. ulong my_length = 0L;
  7864. long my_null = 0L;
  7865. const char *query=
  7866. "insert into v1 values (?)";
  7867. myheader("test_view_insert");
  7868. rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
  7869. myquery(rc);
  7870. rc = mysql_query(mysql, "DROP VIEW IF EXISTS t1,v1");
  7871. myquery(rc);
  7872. rc= mysql_query(mysql,"create table t1 (a int, primary key (a))");
  7873. myquery(rc);
  7874. rc= mysql_query(mysql, "create view v1 as select a from t1 where a>=1");
  7875. myquery(rc);
  7876. insert_stmt= mysql_stmt_init(mysql);
  7877. rc= mysql_stmt_prepare(insert_stmt, query, strlen(query));
  7878. check_execute(insert_stmt, rc);
  7879. query= "select * from t1";
  7880. select_stmt= mysql_stmt_init(mysql);
  7881. rc= mysql_stmt_prepare(select_stmt, query, strlen(query));
  7882. check_execute(select_stmt, rc);
  7883. bzero((char*) my_bind, sizeof(my_bind));
  7884. my_bind[0].buffer_type = MYSQL_TYPE_LONG;
  7885. my_bind[0].buffer = (char *)&my_val;
  7886. my_bind[0].length = &my_length;
  7887. my_bind[0].is_null = (char*)&my_null;
  7888. rc= mysql_stmt_bind_param(insert_stmt, my_bind);
  7889. check_execute(insert_stmt, rc);
  7890. for (i= 0; i < 3; i++)
  7891. {
  7892. int rowcount= 0;
  7893. my_val= i;
  7894. rc= mysql_stmt_execute(insert_stmt);
  7895. check_execute(insert_stmt, rc);
  7896. rc= mysql_stmt_execute(select_stmt);
  7897. check_execute(select_stmt, rc);
  7898. rowcount= (int)my_process_stmt_result(select_stmt);
  7899. DIE_UNLESS((i+1) == rowcount);
  7900. }
  7901. mysql_stmt_close(insert_stmt);
  7902. mysql_stmt_close(select_stmt);
  7903. rc= mysql_query(mysql, "DROP VIEW v1");
  7904. myquery(rc);
  7905. rc= mysql_query(mysql, "DROP TABLE t1");
  7906. myquery(rc);
  7907. }
  7908. static void test_left_join_view()
  7909. {
  7910. MYSQL_STMT *stmt;
  7911. int rc, i;
  7912. const char *query=
  7913. "select t1.a, v1.x from t1 left join v1 on (t1.a= v1.x);";
  7914. myheader("test_left_join_view");
  7915. rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
  7916. myquery(rc);
  7917. rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1");
  7918. myquery(rc);
  7919. rc= mysql_query(mysql,"CREATE TABLE t1 (a int)");
  7920. myquery(rc);
  7921. rc= mysql_query(mysql,"insert into t1 values (1), (2), (3)");
  7922. myquery(rc);
  7923. rc= mysql_query(mysql,"create view v1 (x) as select a from t1 where a > 1");
  7924. myquery(rc);
  7925. stmt= mysql_stmt_init(mysql);
  7926. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  7927. check_execute(stmt, rc);
  7928. for (i= 0; i < 3; i++)
  7929. {
  7930. rc= mysql_stmt_execute(stmt);
  7931. check_execute(stmt, rc);
  7932. rc= my_process_stmt_result(stmt);
  7933. DIE_UNLESS(3 == rc);
  7934. }
  7935. mysql_stmt_close(stmt);
  7936. rc= mysql_query(mysql, "DROP VIEW v1");
  7937. myquery(rc);
  7938. rc= mysql_query(mysql, "DROP TABLE t1");
  7939. myquery(rc);
  7940. }
  7941. static void test_view_insert_fields()
  7942. {
  7943. MYSQL_STMT *stmt;
  7944. char parm[11][1000];
  7945. ulong l[11];
  7946. int rc, i;
  7947. MYSQL_BIND my_bind[11];
  7948. const char *query= "INSERT INTO `v1` ( `K1C4` ,`K2C4` ,`K3C4` ,`K4N4` ,`F1C4` ,`F2I4` ,`F3N5` ,`F7F8` ,`F6N4` ,`F5C8` ,`F9D8` ) VALUES( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )";
  7949. myheader("test_view_insert_fields");
  7950. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, v1");
  7951. myquery(rc);
  7952. rc= mysql_query(mysql, "DROP VIEW IF EXISTS t1, v1");
  7953. myquery(rc);
  7954. rc= mysql_query(mysql,
  7955. "CREATE TABLE t1 (K1C4 varchar(4) NOT NULL,"
  7956. "K2C4 varchar(4) NOT NULL, K3C4 varchar(4) NOT NULL,"
  7957. "K4N4 varchar(4) NOT NULL default '0000',"
  7958. "F1C4 varchar(4) NOT NULL, F2I4 int(11) NOT NULL,"
  7959. "F3N5 varchar(5) NOT NULL default '00000',"
  7960. "F4I4 int(11) NOT NULL default '0', F5C8 varchar(8) NOT NULL,"
  7961. "F6N4 varchar(4) NOT NULL default '0000',"
  7962. "F7F8 double NOT NULL default '0',"
  7963. "F8F8 double NOT NULL default '0',"
  7964. "F9D8 decimal(8,2) NOT NULL default '0.00',"
  7965. "PRIMARY KEY (K1C4,K2C4,K3C4,K4N4)) "
  7966. "CHARSET=latin1 COLLATE latin1_bin");
  7967. myquery(rc);
  7968. rc= mysql_query(mysql,
  7969. "CREATE VIEW v1 AS select sql_no_cache "
  7970. " K1C4 AS K1C4, K2C4 AS K2C4, K3C4 AS K3C4, K4N4 AS K4N4, "
  7971. " F1C4 AS F1C4, F2I4 AS F2I4, F3N5 AS F3N5,"
  7972. " F7F8 AS F7F8, F6N4 AS F6N4, F5C8 AS F5C8, F9D8 AS F9D8"
  7973. " from t1 T0001");
  7974. bzero((char*) my_bind, sizeof(my_bind));
  7975. for (i= 0; i < 11; i++)
  7976. {
  7977. l[i]= 20;
  7978. my_bind[i].buffer_type= MYSQL_TYPE_STRING;
  7979. my_bind[i].is_null= 0;
  7980. my_bind[i].buffer= (char *)&parm[i];
  7981. strmov(parm[i], "1");
  7982. my_bind[i].buffer_length= 2;
  7983. my_bind[i].length= &l[i];
  7984. }
  7985. stmt= mysql_stmt_init(mysql);
  7986. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  7987. check_execute(stmt, rc);
  7988. rc= mysql_stmt_bind_param(stmt, my_bind);
  7989. check_execute(stmt, rc);
  7990. rc= mysql_stmt_execute(stmt);
  7991. check_execute(stmt, rc);
  7992. mysql_stmt_close(stmt);
  7993. query= "select * from t1";
  7994. stmt= mysql_stmt_init(mysql);
  7995. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  7996. check_execute(stmt, rc);
  7997. rc= mysql_stmt_execute(stmt);
  7998. check_execute(stmt, rc);
  7999. rc= my_process_stmt_result(stmt);
  8000. DIE_UNLESS(1 == rc);
  8001. mysql_stmt_close(stmt);
  8002. rc= mysql_query(mysql, "DROP VIEW v1");
  8003. myquery(rc);
  8004. rc= mysql_query(mysql, "DROP TABLE t1");
  8005. myquery(rc);
  8006. }
  8007. static void test_bug5126()
  8008. {
  8009. MYSQL_STMT *stmt;
  8010. MYSQL_BIND my_bind[2];
  8011. int32 c1, c2;
  8012. const char *stmt_text;
  8013. int rc;
  8014. myheader("test_bug5126");
  8015. stmt_text= "DROP TABLE IF EXISTS t1";
  8016. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8017. myquery(rc);
  8018. stmt_text= "CREATE TABLE t1 (a mediumint, b int)";
  8019. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8020. myquery(rc);
  8021. stmt_text= "INSERT INTO t1 VALUES (8386608, 1)";
  8022. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8023. myquery(rc);
  8024. stmt= mysql_stmt_init(mysql);
  8025. stmt_text= "SELECT a, b FROM t1";
  8026. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8027. check_execute(stmt, rc);
  8028. rc= mysql_stmt_execute(stmt);
  8029. check_execute(stmt, rc);
  8030. /* Bind output buffers */
  8031. bzero((char*) my_bind, sizeof(my_bind));
  8032. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  8033. my_bind[0].buffer= &c1;
  8034. my_bind[1].buffer_type= MYSQL_TYPE_LONG;
  8035. my_bind[1].buffer= &c2;
  8036. mysql_stmt_bind_result(stmt, my_bind);
  8037. rc= mysql_stmt_fetch(stmt);
  8038. DIE_UNLESS(rc == 0);
  8039. DIE_UNLESS(c1 == 8386608 && c2 == 1);
  8040. if (!opt_silent)
  8041. printf("%ld, %ld\n", (long) c1, (long) c2);
  8042. mysql_stmt_close(stmt);
  8043. }
  8044. static void test_bug4231()
  8045. {
  8046. MYSQL_STMT *stmt;
  8047. MYSQL_BIND my_bind[2];
  8048. MYSQL_TIME tm[2];
  8049. const char *stmt_text;
  8050. int rc;
  8051. myheader("test_bug4231");
  8052. stmt_text= "DROP TABLE IF EXISTS t1";
  8053. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8054. myquery(rc);
  8055. stmt_text= "CREATE TABLE t1 (a int)";
  8056. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8057. myquery(rc);
  8058. stmt_text= "INSERT INTO t1 VALUES (1)";
  8059. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8060. myquery(rc);
  8061. stmt= mysql_stmt_init(mysql);
  8062. stmt_text= "SELECT a FROM t1 WHERE ? = ?";
  8063. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8064. check_execute(stmt, rc);
  8065. /* Bind input buffers */
  8066. bzero((char*) my_bind, sizeof(my_bind));
  8067. bzero((char*) tm, sizeof(tm));
  8068. my_bind[0].buffer_type= MYSQL_TYPE_DATE;
  8069. my_bind[0].buffer= &tm[0];
  8070. my_bind[1].buffer_type= MYSQL_TYPE_DATE;
  8071. my_bind[1].buffer= &tm[1];
  8072. mysql_stmt_bind_param(stmt, my_bind);
  8073. check_execute(stmt, rc);
  8074. /*
  8075. First set server-side params to some non-zero non-equal values:
  8076. then we will check that they are not used when client sends
  8077. new (zero) times.
  8078. */
  8079. tm[0].time_type = MYSQL_TIMESTAMP_DATE;
  8080. tm[0].year = 2000;
  8081. tm[0].month = 1;
  8082. tm[0].day = 1;
  8083. tm[1]= tm[0];
  8084. --tm[1].year; /* tm[0] != tm[1] */
  8085. rc= mysql_stmt_execute(stmt);
  8086. check_execute(stmt, rc);
  8087. rc= mysql_stmt_fetch(stmt);
  8088. /* binds are unequal, no rows should be returned */
  8089. DIE_UNLESS(rc == MYSQL_NO_DATA);
  8090. /* Set one of the dates to zero */
  8091. tm[0].year= tm[0].month= tm[0].day= 0;
  8092. tm[1]= tm[0];
  8093. mysql_stmt_execute(stmt);
  8094. rc= mysql_stmt_fetch(stmt);
  8095. DIE_UNLESS(rc == 0);
  8096. mysql_stmt_close(stmt);
  8097. stmt_text= "DROP TABLE t1";
  8098. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8099. myquery(rc);
  8100. }
  8101. static void test_bug5399()
  8102. {
  8103. /*
  8104. Ascii 97 is 'a', which gets mapped to Ascii 65 'A' unless internal
  8105. statement id hash in the server uses binary collation.
  8106. */
  8107. #define NUM_OF_USED_STMT 97
  8108. MYSQL_STMT *stmt_list[NUM_OF_USED_STMT];
  8109. MYSQL_STMT **stmt;
  8110. MYSQL_BIND my_bind[1];
  8111. char buff[600];
  8112. int rc;
  8113. int32 no;
  8114. myheader("test_bug5399");
  8115. bzero((char*) my_bind, sizeof(my_bind));
  8116. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  8117. my_bind[0].buffer= &no;
  8118. for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
  8119. {
  8120. sprintf(buff, "select %d", (int) (stmt - stmt_list));
  8121. *stmt= mysql_stmt_init(mysql);
  8122. rc= mysql_stmt_prepare(*stmt, buff, strlen(buff));
  8123. check_execute(*stmt, rc);
  8124. mysql_stmt_bind_result(*stmt, my_bind);
  8125. }
  8126. if (!opt_silent)
  8127. printf("%d statements prepared.\n", NUM_OF_USED_STMT);
  8128. for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
  8129. {
  8130. rc= mysql_stmt_execute(*stmt);
  8131. check_execute(*stmt, rc);
  8132. rc= mysql_stmt_store_result(*stmt);
  8133. check_execute(*stmt, rc);
  8134. rc= mysql_stmt_fetch(*stmt);
  8135. DIE_UNLESS(rc == 0);
  8136. DIE_UNLESS((int32) (stmt - stmt_list) == no);
  8137. }
  8138. for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
  8139. mysql_stmt_close(*stmt);
  8140. #undef NUM_OF_USED_STMT
  8141. }
  8142. static void test_bug5194()
  8143. {
  8144. MYSQL_STMT *stmt;
  8145. MYSQL_BIND *my_bind;
  8146. char *query;
  8147. char *param_str;
  8148. int param_str_length;
  8149. const char *stmt_text;
  8150. int rc;
  8151. float float_array[250] =
  8152. {
  8153. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8154. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8155. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8156. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8157. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8158. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8159. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8160. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8161. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8162. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8163. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8164. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8165. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  8166. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8167. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8168. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8169. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8170. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8171. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8172. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8173. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8174. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8175. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8176. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  8177. 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25
  8178. };
  8179. float *fa_ptr= float_array;
  8180. /* Number of columns per row */
  8181. const int COLUMN_COUNT= sizeof(float_array)/sizeof(*float_array);
  8182. /* Number of rows per bulk insert to start with */
  8183. const int MIN_ROWS_PER_INSERT= 262;
  8184. /* Max number of rows per bulk insert to end with */
  8185. const int MAX_ROWS_PER_INSERT= 300;
  8186. const int MAX_PARAM_COUNT= COLUMN_COUNT*MAX_ROWS_PER_INSERT;
  8187. const char *query_template= "insert into t1 values %s";
  8188. const int CHARS_PER_PARAM= 5; /* space needed to place ", ?" in the query */
  8189. const int uint16_max= 65535;
  8190. int nrows, i;
  8191. myheader("test_bug5194");
  8192. stmt_text= "drop table if exists t1";
  8193. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8194. stmt_text= "create table if not exists t1"
  8195. "(c1 float, c2 float, c3 float, c4 float, c5 float, c6 float, "
  8196. "c7 float, c8 float, c9 float, c10 float, c11 float, c12 float, "
  8197. "c13 float, c14 float, c15 float, c16 float, c17 float, c18 float, "
  8198. "c19 float, c20 float, c21 float, c22 float, c23 float, c24 float, "
  8199. "c25 float, c26 float, c27 float, c28 float, c29 float, c30 float, "
  8200. "c31 float, c32 float, c33 float, c34 float, c35 float, c36 float, "
  8201. "c37 float, c38 float, c39 float, c40 float, c41 float, c42 float, "
  8202. "c43 float, c44 float, c45 float, c46 float, c47 float, c48 float, "
  8203. "c49 float, c50 float, c51 float, c52 float, c53 float, c54 float, "
  8204. "c55 float, c56 float, c57 float, c58 float, c59 float, c60 float, "
  8205. "c61 float, c62 float, c63 float, c64 float, c65 float, c66 float, "
  8206. "c67 float, c68 float, c69 float, c70 float, c71 float, c72 float, "
  8207. "c73 float, c74 float, c75 float, c76 float, c77 float, c78 float, "
  8208. "c79 float, c80 float, c81 float, c82 float, c83 float, c84 float, "
  8209. "c85 float, c86 float, c87 float, c88 float, c89 float, c90 float, "
  8210. "c91 float, c92 float, c93 float, c94 float, c95 float, c96 float, "
  8211. "c97 float, c98 float, c99 float, c100 float, c101 float, c102 float, "
  8212. "c103 float, c104 float, c105 float, c106 float, c107 float, c108 float, "
  8213. "c109 float, c110 float, c111 float, c112 float, c113 float, c114 float, "
  8214. "c115 float, c116 float, c117 float, c118 float, c119 float, c120 float, "
  8215. "c121 float, c122 float, c123 float, c124 float, c125 float, c126 float, "
  8216. "c127 float, c128 float, c129 float, c130 float, c131 float, c132 float, "
  8217. "c133 float, c134 float, c135 float, c136 float, c137 float, c138 float, "
  8218. "c139 float, c140 float, c141 float, c142 float, c143 float, c144 float, "
  8219. "c145 float, c146 float, c147 float, c148 float, c149 float, c150 float, "
  8220. "c151 float, c152 float, c153 float, c154 float, c155 float, c156 float, "
  8221. "c157 float, c158 float, c159 float, c160 float, c161 float, c162 float, "
  8222. "c163 float, c164 float, c165 float, c166 float, c167 float, c168 float, "
  8223. "c169 float, c170 float, c171 float, c172 float, c173 float, c174 float, "
  8224. "c175 float, c176 float, c177 float, c178 float, c179 float, c180 float, "
  8225. "c181 float, c182 float, c183 float, c184 float, c185 float, c186 float, "
  8226. "c187 float, c188 float, c189 float, c190 float, c191 float, c192 float, "
  8227. "c193 float, c194 float, c195 float, c196 float, c197 float, c198 float, "
  8228. "c199 float, c200 float, c201 float, c202 float, c203 float, c204 float, "
  8229. "c205 float, c206 float, c207 float, c208 float, c209 float, c210 float, "
  8230. "c211 float, c212 float, c213 float, c214 float, c215 float, c216 float, "
  8231. "c217 float, c218 float, c219 float, c220 float, c221 float, c222 float, "
  8232. "c223 float, c224 float, c225 float, c226 float, c227 float, c228 float, "
  8233. "c229 float, c230 float, c231 float, c232 float, c233 float, c234 float, "
  8234. "c235 float, c236 float, c237 float, c238 float, c239 float, c240 float, "
  8235. "c241 float, c242 float, c243 float, c244 float, c245 float, c246 float, "
  8236. "c247 float, c248 float, c249 float, c250 float)";
  8237. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8238. myquery(rc);
  8239. my_bind= (MYSQL_BIND*) malloc(MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
  8240. query= (char*) malloc(strlen(query_template) +
  8241. MAX_PARAM_COUNT * CHARS_PER_PARAM + 1);
  8242. param_str= (char*) malloc(COLUMN_COUNT * CHARS_PER_PARAM);
  8243. if (my_bind == 0 || query == 0 || param_str == 0)
  8244. {
  8245. fprintf(stderr, "Can't allocate enough memory for query structs\n");
  8246. if (my_bind)
  8247. free(my_bind);
  8248. if (query)
  8249. free(query);
  8250. if (param_str)
  8251. free(param_str);
  8252. return;
  8253. }
  8254. stmt= mysql_stmt_init(mysql);
  8255. /* setup a template for one row of parameters */
  8256. sprintf(param_str, "(");
  8257. for (i= 1; i < COLUMN_COUNT; ++i)
  8258. strcat(param_str, "?, ");
  8259. strcat(param_str, "?)");
  8260. param_str_length= strlen(param_str);
  8261. /* setup bind array */
  8262. bzero((char*) my_bind, MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
  8263. for (i= 0; i < MAX_PARAM_COUNT; ++i)
  8264. {
  8265. my_bind[i].buffer_type= MYSQL_TYPE_FLOAT;
  8266. my_bind[i].buffer= fa_ptr;
  8267. if (++fa_ptr == float_array + COLUMN_COUNT)
  8268. fa_ptr= float_array;
  8269. }
  8270. /*
  8271. Test each number of rows per bulk insert, so that we can see where
  8272. MySQL fails.
  8273. */
  8274. for (nrows= MIN_ROWS_PER_INSERT; nrows <= MAX_ROWS_PER_INSERT; ++nrows)
  8275. {
  8276. char *query_ptr;
  8277. /* Create statement text for current number of rows */
  8278. sprintf(query, query_template, param_str);
  8279. query_ptr= query + strlen(query);
  8280. for (i= 1; i < nrows; ++i)
  8281. {
  8282. memcpy(query_ptr, ", ", 2);
  8283. query_ptr+= 2;
  8284. memcpy(query_ptr, param_str, param_str_length);
  8285. query_ptr+= param_str_length;
  8286. }
  8287. *query_ptr= '\0';
  8288. rc= mysql_stmt_prepare(stmt, query, query_ptr - query);
  8289. if (rc && nrows * COLUMN_COUNT > uint16_max)
  8290. {
  8291. if (!opt_silent)
  8292. printf("Failed to prepare a statement with %d placeholders "
  8293. "(as expected).\n", nrows * COLUMN_COUNT);
  8294. break;
  8295. }
  8296. else
  8297. check_execute(stmt, rc);
  8298. if (!opt_silent)
  8299. printf("Insert: query length= %d, row count= %d, param count= %lu\n",
  8300. (int) strlen(query), nrows, mysql_stmt_param_count(stmt));
  8301. /* bind the parameter array and execute the query */
  8302. rc= mysql_stmt_bind_param(stmt, my_bind);
  8303. check_execute(stmt, rc);
  8304. rc= mysql_stmt_execute(stmt);
  8305. check_execute(stmt, rc);
  8306. mysql_stmt_reset(stmt);
  8307. }
  8308. mysql_stmt_close(stmt);
  8309. free(my_bind);
  8310. free(query);
  8311. free(param_str);
  8312. stmt_text= "drop table t1";
  8313. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8314. myquery(rc);
  8315. }
  8316. static void test_bug5315()
  8317. {
  8318. MYSQL_STMT *stmt;
  8319. const char *stmt_text;
  8320. int rc;
  8321. myheader("test_bug5315");
  8322. stmt_text= "SELECT 1";
  8323. stmt= mysql_stmt_init(mysql);
  8324. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8325. DIE_UNLESS(rc == 0);
  8326. if (!opt_silent)
  8327. printf("Excuting mysql_change_user\n");
  8328. mysql_change_user(mysql, opt_user, opt_password, current_db);
  8329. if (!opt_silent)
  8330. printf("Excuting mysql_stmt_execute\n");
  8331. rc= mysql_stmt_execute(stmt);
  8332. DIE_UNLESS(rc != 0);
  8333. if (rc)
  8334. {
  8335. if (!opt_silent)
  8336. printf("Got error (as expected): '%s'\n", mysql_stmt_error(stmt));
  8337. }
  8338. /* check that connection is OK */
  8339. if (!opt_silent)
  8340. printf("Excuting mysql_stmt_close\n");
  8341. mysql_stmt_close(stmt);
  8342. if (!opt_silent)
  8343. printf("Excuting mysql_stmt_init\n");
  8344. stmt= mysql_stmt_init(mysql);
  8345. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8346. DIE_UNLESS(rc == 0);
  8347. rc= mysql_stmt_execute(stmt);
  8348. DIE_UNLESS(rc == 0);
  8349. mysql_stmt_close(stmt);
  8350. }
  8351. static void test_bug6049()
  8352. {
  8353. MYSQL_STMT *stmt;
  8354. MYSQL_BIND my_bind[1];
  8355. MYSQL_RES *res;
  8356. MYSQL_ROW row;
  8357. const char *stmt_text;
  8358. char buffer[30];
  8359. ulong length;
  8360. int rc;
  8361. myheader("test_bug6049");
  8362. stmt_text= "SELECT MAKETIME(-25, 12, 12)";
  8363. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8364. myquery(rc);
  8365. res= mysql_store_result(mysql);
  8366. row= mysql_fetch_row(res);
  8367. stmt= mysql_stmt_init(mysql);
  8368. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8369. check_execute(stmt, rc);
  8370. rc= mysql_stmt_execute(stmt);
  8371. check_execute(stmt, rc);
  8372. bzero((char*) my_bind, sizeof(my_bind));
  8373. my_bind[0].buffer_type = MYSQL_TYPE_STRING;
  8374. my_bind[0].buffer = &buffer;
  8375. my_bind[0].buffer_length = sizeof(buffer);
  8376. my_bind[0].length = &length;
  8377. mysql_stmt_bind_result(stmt, my_bind);
  8378. rc= mysql_stmt_fetch(stmt);
  8379. DIE_UNLESS(rc == 0);
  8380. if (!opt_silent)
  8381. {
  8382. printf("Result from query: %s\n", row[0]);
  8383. printf("Result from prepared statement: %s\n", (char*) buffer);
  8384. }
  8385. DIE_UNLESS(strcmp(row[0], (char*) buffer) == 0);
  8386. mysql_free_result(res);
  8387. mysql_stmt_close(stmt);
  8388. }
  8389. static void test_bug6058()
  8390. {
  8391. MYSQL_STMT *stmt;
  8392. MYSQL_BIND my_bind[1];
  8393. MYSQL_RES *res;
  8394. MYSQL_ROW row;
  8395. const char *stmt_text;
  8396. char buffer[30];
  8397. ulong length;
  8398. int rc;
  8399. myheader("test_bug6058");
  8400. rc= mysql_query(mysql, "SET SQL_MODE=''");
  8401. myquery(rc);
  8402. stmt_text= "SELECT CAST('0000-00-00' AS DATE)";
  8403. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8404. myquery(rc);
  8405. res= mysql_store_result(mysql);
  8406. row= mysql_fetch_row(res);
  8407. stmt= mysql_stmt_init(mysql);
  8408. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8409. check_execute(stmt, rc);
  8410. rc= mysql_stmt_execute(stmt);
  8411. check_execute(stmt, rc);
  8412. bzero((char*) my_bind, sizeof(my_bind));
  8413. my_bind[0].buffer_type = MYSQL_TYPE_STRING;
  8414. my_bind[0].buffer = &buffer;
  8415. my_bind[0].buffer_length = sizeof(buffer);
  8416. my_bind[0].length = &length;
  8417. mysql_stmt_bind_result(stmt, my_bind);
  8418. rc= mysql_stmt_fetch(stmt);
  8419. DIE_UNLESS(rc == 0);
  8420. if (!opt_silent)
  8421. {
  8422. printf("Result from query: %s\n", row[0]);
  8423. printf("Result from prepared statement: %s\n", buffer);
  8424. }
  8425. DIE_UNLESS(strcmp(row[0], buffer) == 0);
  8426. mysql_free_result(res);
  8427. mysql_stmt_close(stmt);
  8428. }
  8429. static void test_bug6059()
  8430. {
  8431. MYSQL_STMT *stmt;
  8432. const char *stmt_text;
  8433. myheader("test_bug6059");
  8434. stmt_text= "SELECT 'foo' INTO OUTFILE 'x.3'";
  8435. stmt= mysql_stmt_init(mysql);
  8436. (void) mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8437. DIE_UNLESS(mysql_stmt_field_count(stmt) == 0);
  8438. mysql_stmt_close(stmt);
  8439. }
  8440. static void test_bug6046()
  8441. {
  8442. MYSQL_STMT *stmt;
  8443. const char *stmt_text;
  8444. int rc;
  8445. short b= 1;
  8446. MYSQL_BIND my_bind[1];
  8447. myheader("test_bug6046");
  8448. stmt_text= "DROP TABLE IF EXISTS t1";
  8449. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8450. myquery(rc);
  8451. stmt_text= "CREATE TABLE t1 (a int, b int)";
  8452. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8453. myquery(rc);
  8454. stmt_text= "INSERT INTO t1 VALUES (1,1),(2,2),(3,1),(4,2)";
  8455. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8456. myquery(rc);
  8457. stmt= mysql_stmt_init(mysql);
  8458. stmt_text= "SELECT t1.a FROM t1 NATURAL JOIN t1 as X1 "
  8459. "WHERE t1.b > ? ORDER BY t1.a";
  8460. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8461. check_execute(stmt, rc);
  8462. b= 1;
  8463. bzero((char*) my_bind, sizeof(my_bind));
  8464. my_bind[0].buffer= &b;
  8465. my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
  8466. mysql_stmt_bind_param(stmt, my_bind);
  8467. rc= mysql_stmt_execute(stmt);
  8468. check_execute(stmt, rc);
  8469. mysql_stmt_store_result(stmt);
  8470. rc= mysql_stmt_execute(stmt);
  8471. check_execute(stmt, rc);
  8472. mysql_stmt_close(stmt);
  8473. }
  8474. static void test_basic_cursors()
  8475. {
  8476. const char *basic_tables[]=
  8477. {
  8478. "DROP TABLE IF EXISTS t1, t2",
  8479. "CREATE TABLE t1 "
  8480. "(id INTEGER NOT NULL PRIMARY KEY, "
  8481. " name VARCHAR(20) NOT NULL)",
  8482. "INSERT INTO t1 (id, name) VALUES "
  8483. " (2, 'Ja'), (3, 'Ede'), "
  8484. " (4, 'Haag'), (5, 'Kabul'), "
  8485. " (6, 'Almere'), (7, 'Utrecht'), "
  8486. " (8, 'Qandahar'), (9, 'Amsterdam'), "
  8487. " (10, 'Amersfoort'), (11, 'Constantine')",
  8488. "CREATE TABLE t2 "
  8489. "(id INTEGER NOT NULL PRIMARY KEY, "
  8490. " name VARCHAR(20) NOT NULL)",
  8491. "INSERT INTO t2 (id, name) VALUES "
  8492. " (4, 'Guam'), (5, 'Aruba'), "
  8493. " (6, 'Angola'), (7, 'Albania'), "
  8494. " (8, 'Anguilla'), (9, 'Argentina'), "
  8495. " (10, 'Azerbaijan'), (11, 'Afghanistan'), "
  8496. " (12, 'Burkina Faso'), (13, 'Faroe Islands')"
  8497. };
  8498. const char *queries[]=
  8499. {
  8500. "SELECT * FROM t1",
  8501. "SELECT * FROM t2"
  8502. };
  8503. DBUG_ENTER("test_basic_cursors");
  8504. myheader("test_basic_cursors");
  8505. fill_tables(basic_tables, sizeof(basic_tables)/sizeof(*basic_tables));
  8506. fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
  8507. fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
  8508. DBUG_VOID_RETURN;
  8509. }
  8510. static void test_cursors_with_union()
  8511. {
  8512. const char *queries[]=
  8513. {
  8514. "SELECT t1.name FROM t1 UNION SELECT t2.name FROM t2",
  8515. "SELECT t1.id FROM t1 WHERE t1.id < 5"
  8516. };
  8517. myheader("test_cursors_with_union");
  8518. fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
  8519. fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
  8520. }
  8521. static void test_cursors_with_procedure()
  8522. {
  8523. const char *queries[]=
  8524. {
  8525. "SELECT * FROM t1 procedure analyse()"
  8526. };
  8527. myheader("test_cursors_with_procedure");
  8528. fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
  8529. fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
  8530. }
  8531. /*
  8532. Altough mysql_create_db(), mysql_rm_db() are deprecated since 4.0 they
  8533. should not crash server and should not hang in case of errors.
  8534. Since those functions can't be seen in modern API (unless client library
  8535. was compiled with USE_OLD_FUNCTIONS define) we use simple_command() macro.
  8536. */
  8537. static void test_bug6081()
  8538. {
  8539. int rc;
  8540. myheader("test_bug6081");
  8541. rc= simple_command(mysql, COM_DROP_DB, (uchar*) current_db,
  8542. (ulong)strlen(current_db), 0);
  8543. if (rc == 0 && mysql_errno(mysql) != ER_UNKNOWN_COM_ERROR)
  8544. {
  8545. myerror(NULL); /* purecov: inspected */
  8546. die(__FILE__, __LINE__, "COM_DROP_DB failed"); /* purecov: inspected */
  8547. }
  8548. rc= simple_command(mysql, COM_DROP_DB, (uchar*) current_db,
  8549. (ulong)strlen(current_db), 0);
  8550. myquery_r(rc);
  8551. rc= simple_command(mysql, COM_CREATE_DB, (uchar*) current_db,
  8552. (ulong)strlen(current_db), 0);
  8553. if (rc == 0 && mysql_errno(mysql) != ER_UNKNOWN_COM_ERROR)
  8554. {
  8555. myerror(NULL); /* purecov: inspected */
  8556. die(__FILE__, __LINE__, "COM_CREATE_DB failed"); /* purecov: inspected */
  8557. }
  8558. rc= simple_command(mysql, COM_CREATE_DB, (uchar*) current_db,
  8559. (ulong)strlen(current_db), 0);
  8560. myquery_r(rc);
  8561. rc= mysql_select_db(mysql, current_db);
  8562. myquery(rc);
  8563. }
  8564. static void test_bug6096()
  8565. {
  8566. MYSQL_STMT *stmt;
  8567. MYSQL_RES *query_result, *stmt_metadata;
  8568. const char *stmt_text;
  8569. MYSQL_BIND my_bind[12];
  8570. MYSQL_FIELD *query_field_list, *stmt_field_list;
  8571. ulong query_field_count, stmt_field_count;
  8572. int rc;
  8573. my_bool update_max_length= TRUE;
  8574. uint i;
  8575. myheader("test_bug6096");
  8576. stmt_text= "drop table if exists t1";
  8577. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8578. myquery(rc);
  8579. mysql_query(mysql, "set sql_mode=''");
  8580. stmt_text= "create table t1 (c_tinyint tinyint, c_smallint smallint, "
  8581. " c_mediumint mediumint, c_int int, "
  8582. " c_bigint bigint, c_float float, "
  8583. " c_double double, c_varchar varchar(20), "
  8584. " c_char char(20), c_time time, c_date date, "
  8585. " c_datetime datetime)";
  8586. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8587. myquery(rc);
  8588. stmt_text= "insert into t1 values (-100, -20000, 30000000, 4, 8, 1.0, "
  8589. "2.0, 'abc', 'def', now(), now(), now())";
  8590. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8591. myquery(rc);
  8592. stmt_text= "select * from t1";
  8593. /* Run select in prepared and non-prepared mode and compare metadata */
  8594. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8595. myquery(rc);
  8596. query_result= mysql_store_result(mysql);
  8597. query_field_list= mysql_fetch_fields(query_result);
  8598. query_field_count= mysql_num_fields(query_result);
  8599. stmt= mysql_stmt_init(mysql);
  8600. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8601. check_execute(stmt, rc);
  8602. rc= mysql_stmt_execute(stmt);
  8603. check_execute(stmt, rc);
  8604. mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH,
  8605. (void*) &update_max_length);
  8606. mysql_stmt_store_result(stmt);
  8607. stmt_metadata= mysql_stmt_result_metadata(stmt);
  8608. stmt_field_list= mysql_fetch_fields(stmt_metadata);
  8609. stmt_field_count= mysql_num_fields(stmt_metadata);
  8610. DIE_UNLESS(stmt_field_count == query_field_count);
  8611. /* Print out and check the metadata */
  8612. if (!opt_silent)
  8613. {
  8614. printf(" ------------------------------------------------------------\n");
  8615. printf(" | Metadata \n");
  8616. printf(" ------------------------------------------------------------\n");
  8617. printf(" | Query | Prepared statement \n");
  8618. printf(" ------------------------------------------------------------\n");
  8619. printf(" field name | length | max_length | length | max_length\n");
  8620. printf(" ------------------------------------------------------------\n");
  8621. for (i= 0; i < query_field_count; ++i)
  8622. {
  8623. MYSQL_FIELD *f1= &query_field_list[i], *f2= &stmt_field_list[i];
  8624. printf(" %-11s | %9lu | %10lu | %9lu | %10lu \n",
  8625. f1->name, f1->length, f1->max_length, f2->length, f2->max_length);
  8626. DIE_UNLESS(f1->length == f2->length);
  8627. }
  8628. printf(" ---------------------------------------------------------------\n");
  8629. }
  8630. /* Bind and fetch the data */
  8631. bzero((char*) my_bind, sizeof(my_bind));
  8632. for (i= 0; i < stmt_field_count; ++i)
  8633. {
  8634. my_bind[i].buffer_type= MYSQL_TYPE_STRING;
  8635. my_bind[i].buffer_length= stmt_field_list[i].max_length + 1;
  8636. my_bind[i].buffer= malloc(my_bind[i].buffer_length);
  8637. }
  8638. mysql_stmt_bind_result(stmt, my_bind);
  8639. rc= mysql_stmt_fetch(stmt);
  8640. check_execute(stmt, rc);
  8641. rc= mysql_stmt_fetch(stmt);
  8642. DIE_UNLESS(rc == MYSQL_NO_DATA);
  8643. /* Clean up */
  8644. for (i= 0; i < stmt_field_count; ++i)
  8645. free(my_bind[i].buffer);
  8646. mysql_stmt_close(stmt);
  8647. mysql_free_result(query_result);
  8648. mysql_free_result(stmt_metadata);
  8649. stmt_text= "drop table t1";
  8650. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8651. myquery(rc);
  8652. }
  8653. /*
  8654. Test of basic checks that are performed in server for components
  8655. of MYSQL_TIME parameters.
  8656. */
  8657. static void test_datetime_ranges()
  8658. {
  8659. const char *stmt_text;
  8660. int rc, i;
  8661. MYSQL_STMT *stmt;
  8662. MYSQL_BIND my_bind[6];
  8663. MYSQL_TIME tm[6];
  8664. myheader("test_datetime_ranges");
  8665. stmt_text= "drop table if exists t1";
  8666. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8667. myquery(rc);
  8668. stmt_text= "create table t1 (year datetime, month datetime, day datetime, "
  8669. "hour datetime, min datetime, sec datetime)";
  8670. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8671. myquery(rc);
  8672. stmt= mysql_simple_prepare(mysql,
  8673. "INSERT INTO t1 VALUES (?, ?, ?, ?, ?, ?)");
  8674. check_stmt(stmt);
  8675. verify_param_count(stmt, 6);
  8676. bzero((char*) my_bind, sizeof(my_bind));
  8677. for (i= 0; i < 6; i++)
  8678. {
  8679. my_bind[i].buffer_type= MYSQL_TYPE_DATETIME;
  8680. my_bind[i].buffer= &tm[i];
  8681. }
  8682. rc= mysql_stmt_bind_param(stmt, my_bind);
  8683. check_execute(stmt, rc);
  8684. tm[0].year= 2004; tm[0].month= 11; tm[0].day= 10;
  8685. tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
  8686. tm[0].second_part= 0; tm[0].neg= 0;
  8687. tm[5]= tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
  8688. tm[0].year= 10000; tm[1].month= 13; tm[2].day= 32;
  8689. tm[3].hour= 24; tm[4].minute= 60; tm[5].second= 60;
  8690. rc= mysql_stmt_execute(stmt);
  8691. check_execute(stmt, rc);
  8692. DIE_UNLESS(mysql_warning_count(mysql) != 6);
  8693. verify_col_data("t1", "year", "0000-00-00 00:00:00");
  8694. verify_col_data("t1", "month", "0000-00-00 00:00:00");
  8695. verify_col_data("t1", "day", "0000-00-00 00:00:00");
  8696. verify_col_data("t1", "hour", "0000-00-00 00:00:00");
  8697. verify_col_data("t1", "min", "0000-00-00 00:00:00");
  8698. verify_col_data("t1", "sec", "0000-00-00 00:00:00");
  8699. mysql_stmt_close(stmt);
  8700. stmt_text= "delete from t1";
  8701. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8702. myquery(rc);
  8703. stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 (year, month, day) "
  8704. "VALUES (?, ?, ?)");
  8705. check_stmt(stmt);
  8706. verify_param_count(stmt, 3);
  8707. /*
  8708. We reuse contents of bind and tm arrays left from previous part of test.
  8709. */
  8710. for (i= 0; i < 3; i++)
  8711. my_bind[i].buffer_type= MYSQL_TYPE_DATE;
  8712. rc= mysql_stmt_bind_param(stmt, my_bind);
  8713. check_execute(stmt, rc);
  8714. rc= mysql_stmt_execute(stmt);
  8715. check_execute(stmt, rc);
  8716. DIE_UNLESS(mysql_warning_count(mysql) != 3);
  8717. verify_col_data("t1", "year", "0000-00-00 00:00:00");
  8718. verify_col_data("t1", "month", "0000-00-00 00:00:00");
  8719. verify_col_data("t1", "day", "0000-00-00 00:00:00");
  8720. mysql_stmt_close(stmt);
  8721. stmt_text= "drop table t1";
  8722. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8723. myquery(rc);
  8724. stmt_text= "create table t1 (day_ovfl time, day time, hour time, min time, sec time)";
  8725. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8726. myquery(rc);
  8727. stmt= mysql_simple_prepare(mysql,
  8728. "INSERT INTO t1 VALUES (?, ?, ?, ?, ?)");
  8729. check_stmt(stmt);
  8730. verify_param_count(stmt, 5);
  8731. /*
  8732. Again we reuse what we can from previous part of test.
  8733. */
  8734. for (i= 0; i < 5; i++)
  8735. my_bind[i].buffer_type= MYSQL_TYPE_TIME;
  8736. rc= mysql_stmt_bind_param(stmt, my_bind);
  8737. check_execute(stmt, rc);
  8738. tm[0].year= 0; tm[0].month= 0; tm[0].day= 10;
  8739. tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
  8740. tm[0].second_part= 0; tm[0].neg= 0;
  8741. tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
  8742. tm[0].day= 35; tm[1].day= 34; tm[2].hour= 30; tm[3].minute= 60; tm[4].second= 60;
  8743. rc= mysql_stmt_execute(stmt);
  8744. check_execute(stmt, rc);
  8745. DIE_UNLESS(mysql_warning_count(mysql) == 2);
  8746. verify_col_data("t1", "day_ovfl", "838:59:59");
  8747. verify_col_data("t1", "day", "828:30:30");
  8748. verify_col_data("t1", "hour", "270:30:30");
  8749. verify_col_data("t1", "min", "00:00:00");
  8750. verify_col_data("t1", "sec", "00:00:00");
  8751. mysql_stmt_close(stmt);
  8752. stmt_text= "drop table t1";
  8753. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8754. myquery(rc);
  8755. }
  8756. static void test_bug4172()
  8757. {
  8758. MYSQL_STMT *stmt;
  8759. MYSQL_BIND my_bind[3];
  8760. const char *stmt_text;
  8761. MYSQL_RES *res;
  8762. MYSQL_ROW row;
  8763. int rc;
  8764. char f[100], d[100], e[100];
  8765. ulong f_len, d_len, e_len;
  8766. myheader("test_bug4172");
  8767. mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  8768. mysql_query(mysql, "CREATE TABLE t1 (f float, d double, e decimal(10,4))");
  8769. mysql_query(mysql, "INSERT INTO t1 VALUES (12345.1234, 123456.123456, "
  8770. "123456.1234)");
  8771. stmt= mysql_stmt_init(mysql);
  8772. stmt_text= "SELECT f, d, e FROM t1";
  8773. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8774. check_execute(stmt, rc);
  8775. rc= mysql_stmt_execute(stmt);
  8776. check_execute(stmt, rc);
  8777. bzero((char*) my_bind, sizeof(my_bind));
  8778. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  8779. my_bind[0].buffer= f;
  8780. my_bind[0].buffer_length= sizeof(f);
  8781. my_bind[0].length= &f_len;
  8782. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  8783. my_bind[1].buffer= d;
  8784. my_bind[1].buffer_length= sizeof(d);
  8785. my_bind[1].length= &d_len;
  8786. my_bind[2].buffer_type= MYSQL_TYPE_STRING;
  8787. my_bind[2].buffer= e;
  8788. my_bind[2].buffer_length= sizeof(e);
  8789. my_bind[2].length= &e_len;
  8790. mysql_stmt_bind_result(stmt, my_bind);
  8791. mysql_stmt_store_result(stmt);
  8792. rc= mysql_stmt_fetch(stmt);
  8793. check_execute(stmt, rc);
  8794. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8795. myquery(rc);
  8796. res= mysql_store_result(mysql);
  8797. row= mysql_fetch_row(res);
  8798. if (!opt_silent)
  8799. {
  8800. printf("Binary protocol: float=%s, double=%s, decimal(10,4)=%s\n",
  8801. f, d, e);
  8802. printf("Text protocol: float=%s, double=%s, decimal(10,4)=%s\n",
  8803. row[0], row[1], row[2]);
  8804. }
  8805. DIE_UNLESS(!strcmp(f, row[0]) && !strcmp(d, row[1]) && !strcmp(e, row[2]));
  8806. mysql_free_result(res);
  8807. mysql_stmt_close(stmt);
  8808. }
  8809. static void test_conversion()
  8810. {
  8811. MYSQL_STMT *stmt;
  8812. const char *stmt_text;
  8813. int rc;
  8814. MYSQL_BIND my_bind[1];
  8815. char buff[4];
  8816. ulong length;
  8817. myheader("test_conversion");
  8818. stmt_text= "DROP TABLE IF EXISTS t1";
  8819. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8820. myquery(rc);
  8821. stmt_text= "CREATE TABLE t1 (a TEXT) DEFAULT CHARSET latin1";
  8822. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8823. myquery(rc);
  8824. stmt_text= "SET character_set_connection=utf8, character_set_client=utf8, "
  8825. " character_set_results=latin1";
  8826. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8827. myquery(rc);
  8828. stmt= mysql_stmt_init(mysql);
  8829. stmt_text= "INSERT INTO t1 (a) VALUES (?)";
  8830. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8831. check_execute(stmt, rc);
  8832. bzero((char*) my_bind, sizeof(my_bind));
  8833. my_bind[0].buffer= buff;
  8834. my_bind[0].length= &length;
  8835. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  8836. mysql_stmt_bind_param(stmt, my_bind);
  8837. buff[0]= (uchar) 0xC3;
  8838. buff[1]= (uchar) 0xA0;
  8839. length= 2;
  8840. rc= mysql_stmt_execute(stmt);
  8841. check_execute(stmt, rc);
  8842. stmt_text= "SELECT a FROM t1";
  8843. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8844. check_execute(stmt, rc);
  8845. rc= mysql_stmt_execute(stmt);
  8846. check_execute(stmt, rc);
  8847. my_bind[0].buffer_length= sizeof(buff);
  8848. mysql_stmt_bind_result(stmt, my_bind);
  8849. rc= mysql_stmt_fetch(stmt);
  8850. DIE_UNLESS(rc == 0);
  8851. DIE_UNLESS(length == 1);
  8852. DIE_UNLESS((uchar) buff[0] == 0xE0);
  8853. rc= mysql_stmt_fetch(stmt);
  8854. DIE_UNLESS(rc == MYSQL_NO_DATA);
  8855. mysql_stmt_close(stmt);
  8856. stmt_text= "DROP TABLE t1";
  8857. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8858. myquery(rc);
  8859. stmt_text= "SET NAMES DEFAULT";
  8860. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8861. myquery(rc);
  8862. }
  8863. static void test_rewind(void)
  8864. {
  8865. MYSQL_STMT *stmt;
  8866. MYSQL_BIND my_bind;
  8867. int rc = 0;
  8868. const char *stmt_text;
  8869. long unsigned int length=4, Data=0;
  8870. my_bool isnull=0;
  8871. myheader("test_rewind");
  8872. stmt_text= "CREATE TABLE t1 (a int)";
  8873. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8874. myquery(rc);
  8875. stmt_text= "INSERT INTO t1 VALUES(2),(3),(4)";
  8876. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8877. myquery(rc);
  8878. stmt= mysql_stmt_init(mysql);
  8879. stmt_text= "SELECT * FROM t1";
  8880. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8881. check_execute(stmt, rc);
  8882. bzero((char*) &my_bind, sizeof(MYSQL_BIND));
  8883. my_bind.buffer_type= MYSQL_TYPE_LONG;
  8884. my_bind.buffer= (void *)&Data; /* this buffer won't be altered */
  8885. my_bind.length= &length;
  8886. my_bind.is_null= &isnull;
  8887. rc= mysql_stmt_execute(stmt);
  8888. check_execute(stmt, rc);
  8889. rc= mysql_stmt_store_result(stmt);
  8890. DIE_UNLESS(rc == 0);
  8891. rc= mysql_stmt_bind_result(stmt, &my_bind);
  8892. DIE_UNLESS(rc == 0);
  8893. /* retreive all result sets till we are at the end */
  8894. while(!mysql_stmt_fetch(stmt))
  8895. if (!opt_silent)
  8896. printf("fetched result:%ld\n", Data);
  8897. DIE_UNLESS(rc != MYSQL_NO_DATA);
  8898. /* seek to the first row */
  8899. mysql_stmt_data_seek(stmt, 0);
  8900. /* now we should be able to fetch the results again */
  8901. /* but mysql_stmt_fetch returns MYSQL_NO_DATA */
  8902. while(!(rc= mysql_stmt_fetch(stmt)))
  8903. if (!opt_silent)
  8904. printf("fetched result after seek:%ld\n", Data);
  8905. DIE_UNLESS(rc == MYSQL_NO_DATA);
  8906. stmt_text= "DROP TABLE t1";
  8907. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8908. myquery(rc);
  8909. rc= mysql_stmt_free_result(stmt);
  8910. rc= mysql_stmt_close(stmt);
  8911. }
  8912. static void test_truncation()
  8913. {
  8914. MYSQL_STMT *stmt;
  8915. const char *stmt_text;
  8916. int rc;
  8917. uint bind_count;
  8918. MYSQL_BIND *bind_array, *my_bind;
  8919. myheader("test_truncation");
  8920. /* Prepare the test table */
  8921. rc= mysql_query(mysql, "drop table if exists t1");
  8922. myquery(rc);
  8923. stmt_text= "create table t1 ("
  8924. "i8 tinyint, ui8 tinyint unsigned, "
  8925. "i16 smallint, i16_1 smallint, "
  8926. "ui16 smallint unsigned, i32 int, i32_1 int, "
  8927. "d double, d_1 double, ch char(30), ch_1 char(30), "
  8928. "tx text, tx_1 text, ch_2 char(30) "
  8929. ")";
  8930. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8931. myquery(rc);
  8932. stmt_text= "insert into t1 VALUES ("
  8933. "-10, " /* i8 */
  8934. "200, " /* ui8 */
  8935. "32000, " /* i16 */
  8936. "-32767, " /* i16_1 */
  8937. "64000, " /* ui16 */
  8938. "1073741824, " /* i32 */
  8939. "1073741825, " /* i32_1 */
  8940. "123.456, " /* d */
  8941. "-12345678910, " /* d_1 */
  8942. "'111111111111111111111111111111',"/* ch */
  8943. "'abcdef', " /* ch_1 */
  8944. "'12345 ', " /* tx */
  8945. "'12345.67 ', " /* tx_1 */
  8946. "'12345.67abc'" /* ch_2 */
  8947. ")";
  8948. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  8949. myquery(rc);
  8950. stmt_text= "select i8 c1, i8 c2, ui8 c3, i16_1 c4, ui16 c5, "
  8951. " i16 c6, ui16 c7, i32 c8, i32_1 c9, i32_1 c10, "
  8952. " d c11, d_1 c12, d_1 c13, ch c14, ch_1 c15, tx c16, "
  8953. " tx_1 c17, ch_2 c18 "
  8954. "from t1";
  8955. stmt= mysql_stmt_init(mysql);
  8956. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  8957. check_execute(stmt, rc);
  8958. rc= mysql_stmt_execute(stmt);
  8959. check_execute(stmt, rc);
  8960. bind_count= (uint) mysql_stmt_field_count(stmt);
  8961. /*************** Fill in the bind structure and bind it **************/
  8962. bind_array= malloc(sizeof(MYSQL_BIND) * bind_count);
  8963. bzero((char*) bind_array, sizeof(MYSQL_BIND) * bind_count);
  8964. for (my_bind= bind_array; my_bind < bind_array + bind_count; my_bind++)
  8965. my_bind->error= &my_bind->error_value;
  8966. my_bind= bind_array;
  8967. my_bind->buffer= malloc(sizeof(uint8));
  8968. my_bind->buffer_type= MYSQL_TYPE_TINY;
  8969. my_bind->is_unsigned= TRUE;
  8970. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  8971. my_bind->buffer= malloc(sizeof(uint32));
  8972. my_bind->buffer_type= MYSQL_TYPE_LONG;
  8973. my_bind->is_unsigned= TRUE;
  8974. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  8975. my_bind->buffer= malloc(sizeof(int8));
  8976. my_bind->buffer_type= MYSQL_TYPE_TINY;
  8977. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  8978. my_bind->buffer= malloc(sizeof(uint16));
  8979. my_bind->buffer_type= MYSQL_TYPE_SHORT;
  8980. my_bind->is_unsigned= TRUE;
  8981. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  8982. my_bind->buffer= malloc(sizeof(int16));
  8983. my_bind->buffer_type= MYSQL_TYPE_SHORT;
  8984. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  8985. my_bind->buffer= malloc(sizeof(uint16));
  8986. my_bind->buffer_type= MYSQL_TYPE_SHORT;
  8987. my_bind->is_unsigned= TRUE;
  8988. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  8989. my_bind->buffer= malloc(sizeof(int8));
  8990. my_bind->buffer_type= MYSQL_TYPE_TINY;
  8991. my_bind->is_unsigned= TRUE;
  8992. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  8993. my_bind->buffer= malloc(sizeof(float));
  8994. my_bind->buffer_type= MYSQL_TYPE_FLOAT;
  8995. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  8996. my_bind->buffer= malloc(sizeof(float));
  8997. my_bind->buffer_type= MYSQL_TYPE_FLOAT;
  8998. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  8999. my_bind->buffer= malloc(sizeof(double));
  9000. my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
  9001. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9002. my_bind->buffer= malloc(sizeof(longlong));
  9003. my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
  9004. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9005. my_bind->buffer= malloc(sizeof(ulonglong));
  9006. my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
  9007. my_bind->is_unsigned= TRUE;
  9008. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9009. my_bind->buffer= malloc(sizeof(longlong));
  9010. my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
  9011. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9012. my_bind->buffer= malloc(sizeof(longlong));
  9013. my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
  9014. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9015. my_bind->buffer= malloc(sizeof(longlong));
  9016. my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
  9017. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9018. my_bind->buffer= malloc(sizeof(longlong));
  9019. my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
  9020. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9021. my_bind->buffer= malloc(sizeof(double));
  9022. my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
  9023. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9024. my_bind->buffer= malloc(sizeof(double));
  9025. my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
  9026. rc= mysql_stmt_bind_result(stmt, bind_array);
  9027. check_execute(stmt, rc);
  9028. rc= mysql_stmt_fetch(stmt);
  9029. DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
  9030. /*************** Verify truncation results ***************************/
  9031. my_bind= bind_array;
  9032. /* signed tiny -> tiny */
  9033. DIE_UNLESS(*my_bind->error && * (int8*) my_bind->buffer == -10);
  9034. /* signed tiny -> uint32 */
  9035. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9036. DIE_UNLESS(*my_bind->error && * (int32*) my_bind->buffer == -10);
  9037. /* unsigned tiny -> tiny */
  9038. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9039. DIE_UNLESS(*my_bind->error && * (uint8*) my_bind->buffer == 200);
  9040. /* short -> ushort */
  9041. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9042. DIE_UNLESS(*my_bind->error && * (int16*) my_bind->buffer == -32767);
  9043. /* ushort -> short */
  9044. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9045. DIE_UNLESS(*my_bind->error && * (uint16*) my_bind->buffer == 64000);
  9046. /* short -> ushort (no truncation, data is in the range of target type) */
  9047. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9048. DIE_UNLESS(! *my_bind->error && * (uint16*) my_bind->buffer == 32000);
  9049. /* ushort -> utiny */
  9050. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9051. DIE_UNLESS(*my_bind->error && * (int8*) my_bind->buffer == 0);
  9052. /* int -> float: no truncation, the number is a power of two */
  9053. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9054. DIE_UNLESS(! *my_bind->error && * (float*) my_bind->buffer == 1073741824);
  9055. /* int -> float: truncation, not enough bits in float */
  9056. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9057. DIE_UNLESS(*my_bind->error);
  9058. /* int -> double: no truncation */
  9059. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9060. DIE_UNLESS(! *my_bind->error && * (double*) my_bind->buffer == 1073741825);
  9061. /* double -> longlong: fractional part is lost */
  9062. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9063. /* double -> ulonglong, negative fp number to unsigned integer */
  9064. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9065. /* Value in the buffer is not defined: don't test it */
  9066. DIE_UNLESS(*my_bind->error);
  9067. /* double -> longlong, negative fp number to signed integer: no loss */
  9068. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9069. DIE_UNLESS(! *my_bind->error && * (longlong*) my_bind->buffer == LL(-12345678910));
  9070. /* big numeric string -> number */
  9071. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9072. DIE_UNLESS(*my_bind->error);
  9073. /* junk string -> number */
  9074. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9075. DIE_UNLESS(*my_bind->error && *(longlong*) my_bind->buffer == 0);
  9076. /* string with trailing spaces -> number */
  9077. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9078. DIE_UNLESS(! *my_bind->error && *(longlong*) my_bind->buffer == 12345);
  9079. /* string with trailing spaces -> double */
  9080. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9081. DIE_UNLESS(! *my_bind->error && *(double*) my_bind->buffer == 12345.67);
  9082. /* string with trailing junk -> double */
  9083. DIE_UNLESS(my_bind++ < bind_array + bind_count);
  9084. /*
  9085. XXX: There must be a truncation error: but it's not the way the server
  9086. behaves, so let's leave it for now.
  9087. */
  9088. DIE_UNLESS(*(double*) my_bind->buffer == 12345.67);
  9089. /*
  9090. TODO: string -> double, double -> time, double -> string (truncation
  9091. errors are not supported here yet)
  9092. longlong -> time/date/datetime
  9093. date -> time, date -> timestamp, date -> number
  9094. time -> string, time -> date, time -> timestamp,
  9095. number -> date string -> date
  9096. */
  9097. /*************** Cleanup *********************************************/
  9098. mysql_stmt_close(stmt);
  9099. for (my_bind= bind_array; my_bind < bind_array + bind_count; my_bind++)
  9100. free(my_bind->buffer);
  9101. free(bind_array);
  9102. rc= mysql_query(mysql, "drop table t1");
  9103. myquery(rc);
  9104. }
  9105. static void test_truncation_option()
  9106. {
  9107. MYSQL_STMT *stmt;
  9108. const char *stmt_text;
  9109. int rc;
  9110. uint8 buf;
  9111. my_bool option= 0;
  9112. my_bool error;
  9113. MYSQL_BIND my_bind;
  9114. myheader("test_truncation_option");
  9115. /* Prepare the test table */
  9116. stmt_text= "select -1";
  9117. stmt= mysql_stmt_init(mysql);
  9118. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  9119. check_execute(stmt, rc);
  9120. rc= mysql_stmt_execute(stmt);
  9121. check_execute(stmt, rc);
  9122. bzero((char*) &my_bind, sizeof(my_bind));
  9123. my_bind.buffer= (void*) &buf;
  9124. my_bind.buffer_type= MYSQL_TYPE_TINY;
  9125. my_bind.is_unsigned= TRUE;
  9126. my_bind.error= &error;
  9127. rc= mysql_stmt_bind_result(stmt, &my_bind);
  9128. check_execute(stmt, rc);
  9129. rc= mysql_stmt_fetch(stmt);
  9130. DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
  9131. DIE_UNLESS(error);
  9132. rc= mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option);
  9133. myquery(rc);
  9134. /* need to rebind for the new setting to take effect */
  9135. rc= mysql_stmt_bind_result(stmt, &my_bind);
  9136. check_execute(stmt, rc);
  9137. rc= mysql_stmt_execute(stmt);
  9138. check_execute(stmt, rc);
  9139. rc= mysql_stmt_fetch(stmt);
  9140. check_execute(stmt, rc);
  9141. /* The only change is rc - error pointers are still filled in */
  9142. DIE_UNLESS(error == 1);
  9143. /* restore back the defaults */
  9144. option= 1;
  9145. mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option);
  9146. mysql_stmt_close(stmt);
  9147. }
  9148. /* Bug#6761 - mysql_list_fields doesn't work */
  9149. static void test_bug6761(void)
  9150. {
  9151. const char *stmt_text;
  9152. MYSQL_RES *res;
  9153. int rc;
  9154. myheader("test_bug6761");
  9155. stmt_text= "CREATE TABLE t1 (a int, b char(255), c decimal)";
  9156. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  9157. myquery(rc);
  9158. res= mysql_list_fields(mysql, "t1", "%");
  9159. DIE_UNLESS(res && mysql_num_fields(res) == 3);
  9160. mysql_free_result(res);
  9161. stmt_text= "DROP TABLE t1";
  9162. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  9163. myquery(rc);
  9164. }
  9165. /* Bug#8330 - mysql_stmt_execute crashes (libmysql) */
  9166. static void test_bug8330()
  9167. {
  9168. const char *stmt_text;
  9169. MYSQL_STMT *stmt[2];
  9170. int i, rc;
  9171. const char *query= "select a,b from t1 where a=?";
  9172. MYSQL_BIND my_bind[2];
  9173. long lval[2];
  9174. myheader("test_bug8330");
  9175. stmt_text= "drop table if exists t1";
  9176. /* in case some previos test failed */
  9177. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  9178. myquery(rc);
  9179. stmt_text= "create table t1 (a int, b int)";
  9180. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  9181. myquery(rc);
  9182. bzero((char*) my_bind, sizeof(my_bind));
  9183. for (i=0; i < 2; i++)
  9184. {
  9185. stmt[i]= mysql_stmt_init(mysql);
  9186. rc= mysql_stmt_prepare(stmt[i], query, strlen(query));
  9187. check_execute(stmt[i], rc);
  9188. my_bind[i].buffer_type= MYSQL_TYPE_LONG;
  9189. my_bind[i].buffer= (void*) &lval[i];
  9190. my_bind[i].is_null= 0;
  9191. mysql_stmt_bind_param(stmt[i], &my_bind[i]);
  9192. }
  9193. rc= mysql_stmt_execute(stmt[0]);
  9194. check_execute(stmt[0], rc);
  9195. rc= mysql_stmt_execute(stmt[1]);
  9196. DIE_UNLESS(rc && mysql_stmt_errno(stmt[1]) == CR_COMMANDS_OUT_OF_SYNC);
  9197. rc= mysql_stmt_execute(stmt[0]);
  9198. check_execute(stmt[0], rc);
  9199. mysql_stmt_close(stmt[0]);
  9200. mysql_stmt_close(stmt[1]);
  9201. stmt_text= "drop table t1";
  9202. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  9203. myquery(rc);
  9204. }
  9205. /* Bug#7990 - mysql_stmt_close doesn't reset mysql->net.last_error */
  9206. static void test_bug7990()
  9207. {
  9208. MYSQL_STMT *stmt;
  9209. int rc;
  9210. myheader("test_bug7990");
  9211. stmt= mysql_stmt_init(mysql);
  9212. rc= mysql_stmt_prepare(stmt, "foo", 3);
  9213. /*
  9214. XXX: the fact that we store errno both in STMT and in
  9215. MYSQL is not documented and is subject to change in 5.0
  9216. */
  9217. DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql));
  9218. mysql_stmt_close(stmt);
  9219. DIE_UNLESS(!mysql_errno(mysql));
  9220. }
  9221. /*
  9222. Bug #15518 - Reusing a stmt that has failed during prepare
  9223. does not clear error
  9224. */
  9225. static void test_bug15518()
  9226. {
  9227. MYSQL_STMT *stmt;
  9228. MYSQL* mysql1;
  9229. int rc;
  9230. myheader("test_bug15518");
  9231. mysql1= mysql_client_init(NULL);
  9232. if (!mysql_real_connect(mysql1, opt_host, opt_user, opt_password,
  9233. opt_db ? opt_db : "test", opt_port, opt_unix_socket,
  9234. CLIENT_MULTI_STATEMENTS))
  9235. {
  9236. fprintf(stderr, "Failed to connect to the database\n");
  9237. DIE_UNLESS(0);
  9238. }
  9239. stmt= mysql_stmt_init(mysql1);
  9240. /*
  9241. The prepare of foo should fail with errno 1064 since
  9242. it's not a valid query
  9243. */
  9244. rc= mysql_stmt_prepare(stmt, "foo", 3);
  9245. if (!opt_silent)
  9246. fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
  9247. rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
  9248. DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql1));
  9249. /*
  9250. Use the same stmt and reprepare with another query that
  9251. suceeds
  9252. */
  9253. rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
  9254. if (!opt_silent)
  9255. fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
  9256. rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
  9257. DIE_UNLESS(!rc || mysql_stmt_errno(stmt) || mysql_errno(mysql1));
  9258. mysql_stmt_close(stmt);
  9259. DIE_UNLESS(!mysql_errno(mysql1));
  9260. /*
  9261. part2, when connection to server has been closed
  9262. after first prepare
  9263. */
  9264. stmt= mysql_stmt_init(mysql1);
  9265. rc= mysql_stmt_prepare(stmt, "foo", 3);
  9266. if (!opt_silent)
  9267. fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
  9268. rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
  9269. DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql1));
  9270. /* Close connection to server */
  9271. mysql_close(mysql1);
  9272. /*
  9273. Use the same stmt and reprepare with another query that
  9274. suceeds. The prepare should fail with error 2013 since
  9275. connection to server has been closed.
  9276. */
  9277. rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
  9278. if (!opt_silent)
  9279. fprintf(stdout, "rc: %d, mysql_stmt_errno: %d\n",
  9280. rc, mysql_stmt_errno(stmt));
  9281. DIE_UNLESS(rc && mysql_stmt_errno(stmt));
  9282. mysql_stmt_close(stmt);
  9283. }
  9284. static void disable_query_logs()
  9285. {
  9286. int rc;
  9287. rc= mysql_query(mysql, "set @@global.general_log=off");
  9288. myquery(rc);
  9289. rc= mysql_query(mysql, "set @@global.slow_query_log=off");
  9290. myquery(rc);
  9291. }
  9292. static void enable_query_logs(int truncate)
  9293. {
  9294. int rc;
  9295. rc= mysql_query(mysql, "set @save_global_general_log=@@global.general_log");
  9296. myquery(rc);
  9297. rc= mysql_query(mysql, "set @save_global_slow_query_log=@@global.slow_query_log");
  9298. myquery(rc);
  9299. rc= mysql_query(mysql, "set @@global.general_log=on");
  9300. myquery(rc);
  9301. rc= mysql_query(mysql, "set @@global.slow_query_log=on");
  9302. myquery(rc);
  9303. if (truncate)
  9304. {
  9305. rc= mysql_query(mysql, "truncate mysql.general_log");
  9306. myquery(rc);
  9307. rc= mysql_query(mysql, "truncate mysql.slow_log");
  9308. myquery(rc);
  9309. }
  9310. }
  9311. static void restore_query_logs()
  9312. {
  9313. int rc;
  9314. rc= mysql_query(mysql, "set @@global.general_log=@save_global_general_log");
  9315. myquery(rc);
  9316. rc= mysql_query(mysql, "set @@global.slow_query_log=@save_global_slow_query_log");
  9317. myquery(rc);
  9318. }
  9319. static void test_view_sp_list_fields()
  9320. {
  9321. int rc;
  9322. MYSQL_RES *res;
  9323. myheader("test_view_sp_list_fields");
  9324. rc= mysql_query(mysql, "DROP FUNCTION IF EXISTS f1");
  9325. myquery(rc);
  9326. rc= mysql_query(mysql, "DROP TABLE IF EXISTS v1, t1, t2");
  9327. myquery(rc);
  9328. rc= mysql_query(mysql, "DROP VIEW IF EXISTS v1, t1, t2");
  9329. myquery(rc);
  9330. rc= mysql_query(mysql, "create function f1 () returns int return 5");
  9331. myquery(rc);
  9332. rc= mysql_query(mysql, "create table t1 (s1 char,s2 char)");
  9333. myquery(rc);
  9334. rc= mysql_query(mysql, "create table t2 (s1 int);");
  9335. myquery(rc);
  9336. rc= mysql_query(mysql, "create view v1 as select s2,sum(s1) - \
  9337. count(s2) as vx from t1 group by s2 having sum(s1) - count(s2) < (select f1() \
  9338. from t2);");
  9339. myquery(rc);
  9340. res= mysql_list_fields(mysql, "v1", NullS);
  9341. DIE_UNLESS(res != 0 && mysql_num_fields(res) != 0);
  9342. rc= mysql_query(mysql, "DROP FUNCTION f1");
  9343. myquery(rc);
  9344. rc= mysql_query(mysql, "DROP VIEW v1");
  9345. myquery(rc);
  9346. rc= mysql_query(mysql, "DROP TABLE t1, t2");
  9347. mysql_free_result(res);
  9348. myquery(rc);
  9349. }
  9350. /*
  9351. Test mysql_real_escape_string() with gbk charset
  9352. The important part is that 0x27 (') is the second-byte in a invalid
  9353. two-byte GBK character here. But 0xbf5c is a valid GBK character, so
  9354. it needs to be escaped as 0x5cbf27
  9355. */
  9356. #define TEST_BUG8378_IN "\xef\xbb\xbf\x27\xbf\x10"
  9357. #define TEST_BUG8378_OUT "\xef\xbb\x5c\xbf\x5c\x27\x5c\xbf\x10"
  9358. static void test_bug8378()
  9359. {
  9360. #if defined(HAVE_CHARSET_gbk) && !defined(EMBEDDED_LIBRARY)
  9361. MYSQL *lmysql;
  9362. char out[9]; /* strlen(TEST_BUG8378)*2+1 */
  9363. char buf[256];
  9364. int len, rc;
  9365. myheader("test_bug8378");
  9366. if (!opt_silent)
  9367. fprintf(stdout, "\n Establishing a test connection ...");
  9368. if (!(lmysql= mysql_client_init(NULL)))
  9369. {
  9370. myerror("mysql_client_init() failed");
  9371. exit(1);
  9372. }
  9373. if (mysql_options(lmysql, MYSQL_SET_CHARSET_NAME, "gbk"))
  9374. {
  9375. myerror("mysql_options() failed");
  9376. exit(1);
  9377. }
  9378. if (!(mysql_real_connect(lmysql, opt_host, opt_user,
  9379. opt_password, current_db, opt_port,
  9380. opt_unix_socket, 0)))
  9381. {
  9382. myerror("connection failed");
  9383. exit(1);
  9384. }
  9385. if (!opt_silent)
  9386. fprintf(stdout, "OK");
  9387. rc= mysql_query(lmysql, "SET SQL_MODE=''");
  9388. myquery(rc);
  9389. len= mysql_real_escape_string(lmysql, out, TEST_BUG8378_IN, 4);
  9390. /* No escaping should have actually happened. */
  9391. DIE_UNLESS(memcmp(out, TEST_BUG8378_OUT, len) == 0);
  9392. sprintf(buf, "SELECT '%s'", out);
  9393. rc=mysql_real_query(lmysql, buf, strlen(buf));
  9394. myquery(rc);
  9395. mysql_close(lmysql);
  9396. #endif
  9397. }
  9398. static void test_bug8722()
  9399. {
  9400. MYSQL_STMT *stmt;
  9401. int rc;
  9402. const char *stmt_text;
  9403. myheader("test_bug8722");
  9404. /* Prepare test data */
  9405. stmt_text= "drop table if exists t1, v1";
  9406. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  9407. myquery(rc);
  9408. stmt_text= "CREATE TABLE t1 (c1 varchar(10), c2 varchar(10), c3 varchar(10),"
  9409. " c4 varchar(10), c5 varchar(10), c6 varchar(10),"
  9410. " c7 varchar(10), c8 varchar(10), c9 varchar(10),"
  9411. "c10 varchar(10))";
  9412. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  9413. myquery(rc);
  9414. stmt_text= "INSERT INTO t1 VALUES (1,2,3,4,5,6,7,8,9,10)";
  9415. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  9416. myquery(rc);
  9417. stmt_text= "CREATE VIEW v1 AS SELECT * FROM t1";
  9418. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  9419. myquery(rc);
  9420. /* Note: if you uncomment following block everything works fine */
  9421. /*
  9422. rc= mysql_query(mysql, "sellect * from v1");
  9423. myquery(rc);
  9424. mysql_free_result(mysql_store_result(mysql));
  9425. */
  9426. stmt= mysql_stmt_init(mysql);
  9427. stmt_text= "select * from v1";
  9428. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  9429. check_execute(stmt, rc);
  9430. mysql_stmt_close(stmt);
  9431. stmt_text= "drop table if exists t1, v1";
  9432. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  9433. myquery(rc);
  9434. }
  9435. MYSQL_STMT *open_cursor(const char *query)
  9436. {
  9437. int rc;
  9438. const ulong type= (ulong)CURSOR_TYPE_READ_ONLY;
  9439. MYSQL_STMT *stmt= mysql_stmt_init(mysql);
  9440. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  9441. check_execute(stmt, rc);
  9442. mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
  9443. return stmt;
  9444. }
  9445. static void test_bug8880()
  9446. {
  9447. MYSQL_STMT *stmt_list[2], **stmt;
  9448. MYSQL_STMT **stmt_list_end= (MYSQL_STMT**) stmt_list + 2;
  9449. int rc;
  9450. myheader("test_bug8880");
  9451. mysql_query(mysql, "drop table if exists t1");
  9452. mysql_query(mysql, "create table t1 (a int not null primary key, b int)");
  9453. rc= mysql_query(mysql, "insert into t1 values (1,1)");
  9454. myquery(rc); /* one check is enough */
  9455. /*
  9456. when inserting 2 rows everything works well
  9457. mysql_query(mysql, "INSERT INTO t1 VALUES (1,1),(2,2)");
  9458. */
  9459. for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
  9460. *stmt= open_cursor("select a from t1");
  9461. for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
  9462. {
  9463. rc= mysql_stmt_execute(*stmt);
  9464. check_execute(*stmt, rc);
  9465. }
  9466. for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
  9467. mysql_stmt_close(*stmt);
  9468. }
  9469. static void test_bug9159()
  9470. {
  9471. MYSQL_STMT *stmt;
  9472. int rc;
  9473. const char *stmt_text= "select a, b from t1";
  9474. const unsigned long type= CURSOR_TYPE_READ_ONLY;
  9475. myheader("test_bug9159");
  9476. mysql_query(mysql, "drop table if exists t1");
  9477. mysql_query(mysql, "create table t1 (a int not null primary key, b int)");
  9478. rc= mysql_query(mysql, "insert into t1 values (1,1)");
  9479. myquery(rc);
  9480. stmt= mysql_stmt_init(mysql);
  9481. mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  9482. mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void *)&type);
  9483. mysql_stmt_execute(stmt);
  9484. mysql_stmt_close(stmt);
  9485. rc= mysql_query(mysql, "drop table if exists t1");
  9486. myquery(rc);
  9487. }
  9488. /* Crash when opening a cursor to a query with DISTICNT and no key */
  9489. static void test_bug9520()
  9490. {
  9491. MYSQL_STMT *stmt;
  9492. MYSQL_BIND my_bind[1];
  9493. char a[6];
  9494. ulong a_len;
  9495. int rc, row_count= 0;
  9496. myheader("test_bug9520");
  9497. mysql_query(mysql, "drop table if exists t1");
  9498. mysql_query(mysql, "create table t1 (a char(5), b char(5), c char(5),"
  9499. " primary key (a, b, c))");
  9500. rc= mysql_query(mysql, "insert into t1 values ('x', 'y', 'z'), "
  9501. " ('a', 'b', 'c'), ('k', 'l', 'm')");
  9502. myquery(rc);
  9503. stmt= open_cursor("select distinct b from t1");
  9504. /*
  9505. Not crashes with:
  9506. stmt= open_cursor("select distinct a from t1");
  9507. */
  9508. rc= mysql_stmt_execute(stmt);
  9509. check_execute(stmt, rc);
  9510. bzero((char*) my_bind, sizeof(my_bind));
  9511. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  9512. my_bind[0].buffer= (char*) a;
  9513. my_bind[0].buffer_length= sizeof(a);
  9514. my_bind[0].length= &a_len;
  9515. mysql_stmt_bind_result(stmt, my_bind);
  9516. while (!(rc= mysql_stmt_fetch(stmt)))
  9517. row_count++;
  9518. DIE_UNLESS(rc == MYSQL_NO_DATA);
  9519. if (!opt_silent)
  9520. printf("Fetched %d rows\n", row_count);
  9521. DBUG_ASSERT(row_count == 3);
  9522. mysql_stmt_close(stmt);
  9523. rc= mysql_query(mysql, "drop table t1");
  9524. myquery(rc);
  9525. }
  9526. /*
  9527. We can't have more than one cursor open for a prepared statement.
  9528. Test re-executions of a PS with cursor; mysql_stmt_reset must close
  9529. the cursor attached to the statement, if there is one.
  9530. */
  9531. static void test_bug9478()
  9532. {
  9533. MYSQL_STMT *stmt;
  9534. MYSQL_BIND my_bind[1];
  9535. char a[6];
  9536. ulong a_len;
  9537. int rc, i;
  9538. DBUG_ENTER("test_bug9478");
  9539. myheader("test_bug9478");
  9540. mysql_query(mysql, "drop table if exists t1");
  9541. mysql_query(mysql, "create table t1 (id integer not null primary key, "
  9542. " name varchar(20) not null)");
  9543. rc= mysql_query(mysql, "insert into t1 (id, name) values "
  9544. " (1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
  9545. myquery(rc);
  9546. stmt= open_cursor("select name from t1 where id=2");
  9547. bzero((char*) my_bind, sizeof(my_bind));
  9548. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  9549. my_bind[0].buffer= (char*) a;
  9550. my_bind[0].buffer_length= sizeof(a);
  9551. my_bind[0].length= &a_len;
  9552. mysql_stmt_bind_result(stmt, my_bind);
  9553. for (i= 0; i < 5; i++)
  9554. {
  9555. rc= mysql_stmt_execute(stmt);
  9556. check_execute(stmt, rc);
  9557. rc= mysql_stmt_fetch(stmt);
  9558. check_execute(stmt, rc);
  9559. if (!opt_silent && i == 0)
  9560. printf("Fetched row: %s\n", a);
  9561. /*
  9562. The query above is a one-row result set. Therefore, there is no
  9563. cursor associated with it, as the server won't bother with opening
  9564. a cursor for a one-row result set. The first row was read from the
  9565. server in the fetch above. But there is eof packet pending in the
  9566. network. mysql_stmt_execute will flush the packet and successfully
  9567. execute the statement.
  9568. */
  9569. rc= mysql_stmt_execute(stmt);
  9570. check_execute(stmt, rc);
  9571. rc= mysql_stmt_fetch(stmt);
  9572. check_execute(stmt, rc);
  9573. if (!opt_silent && i == 0)
  9574. printf("Fetched row: %s\n", a);
  9575. rc= mysql_stmt_fetch(stmt);
  9576. DIE_UNLESS(rc == MYSQL_NO_DATA);
  9577. {
  9578. char buff[8];
  9579. /* Fill in the fetch packet */
  9580. int4store(buff, stmt->stmt_id);
  9581. buff[4]= 1; /* prefetch rows */
  9582. rc= ((*mysql->methods->advanced_command)(mysql, COM_STMT_FETCH,
  9583. (uchar*) buff,
  9584. sizeof(buff), 0,0,1,NULL) ||
  9585. (*mysql->methods->read_query_result)(mysql));
  9586. DIE_UNLESS(rc);
  9587. if (!opt_silent && i == 0)
  9588. printf("Got error (as expected): %s\n", mysql_error(mysql));
  9589. }
  9590. rc= mysql_stmt_execute(stmt);
  9591. check_execute(stmt, rc);
  9592. rc= mysql_stmt_fetch(stmt);
  9593. check_execute(stmt, rc);
  9594. if (!opt_silent && i == 0)
  9595. printf("Fetched row: %s\n", a);
  9596. rc= mysql_stmt_reset(stmt);
  9597. check_execute(stmt, rc);
  9598. rc= mysql_stmt_fetch(stmt);
  9599. DIE_UNLESS(rc && mysql_stmt_errno(stmt));
  9600. if (!opt_silent && i == 0)
  9601. printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
  9602. }
  9603. rc= mysql_stmt_close(stmt);
  9604. DIE_UNLESS(rc == 0);
  9605. /* Test the case with a server side cursor */
  9606. stmt= open_cursor("select name from t1");
  9607. mysql_stmt_bind_result(stmt, my_bind);
  9608. for (i= 0; i < 5; i++)
  9609. {
  9610. DBUG_PRINT("loop",("i: %d", i));
  9611. rc= mysql_stmt_execute(stmt);
  9612. check_execute(stmt, rc);
  9613. rc= mysql_stmt_fetch(stmt);
  9614. check_execute(stmt, rc);
  9615. if (!opt_silent && i == 0)
  9616. printf("Fetched row: %s\n", a);
  9617. rc= mysql_stmt_execute(stmt);
  9618. check_execute(stmt, rc);
  9619. while (! (rc= mysql_stmt_fetch(stmt)))
  9620. {
  9621. if (!opt_silent && i == 0)
  9622. printf("Fetched row: %s\n", a);
  9623. }
  9624. DIE_UNLESS(rc == MYSQL_NO_DATA);
  9625. rc= mysql_stmt_execute(stmt);
  9626. check_execute(stmt, rc);
  9627. rc= mysql_stmt_fetch(stmt);
  9628. check_execute(stmt, rc);
  9629. if (!opt_silent && i == 0)
  9630. printf("Fetched row: %s\n", a);
  9631. rc= mysql_stmt_reset(stmt);
  9632. check_execute(stmt, rc);
  9633. rc= mysql_stmt_fetch(stmt);
  9634. DIE_UNLESS(rc && mysql_stmt_errno(stmt));
  9635. if (!opt_silent && i == 0)
  9636. printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
  9637. }
  9638. rc= mysql_stmt_close(stmt);
  9639. DIE_UNLESS(rc == 0);
  9640. rc= mysql_query(mysql, "drop table t1");
  9641. myquery(rc);
  9642. DBUG_VOID_RETURN;
  9643. }
  9644. /*
  9645. Error message is returned for unsupported features.
  9646. Test also cursors with non-default PREFETCH_ROWS
  9647. */
  9648. static void test_bug9643()
  9649. {
  9650. MYSQL_STMT *stmt;
  9651. MYSQL_BIND my_bind[1];
  9652. int32 a;
  9653. int rc;
  9654. const char *stmt_text;
  9655. int num_rows= 0;
  9656. ulong type;
  9657. ulong prefetch_rows= 5;
  9658. myheader("test_bug9643");
  9659. mysql_query(mysql, "drop table if exists t1");
  9660. mysql_query(mysql, "create table t1 (id integer not null primary key)");
  9661. rc= mysql_query(mysql, "insert into t1 (id) values "
  9662. " (1), (2), (3), (4), (5), (6), (7), (8), (9)");
  9663. myquery(rc);
  9664. stmt= mysql_stmt_init(mysql);
  9665. /* Not implemented in 5.0 */
  9666. type= (ulong) CURSOR_TYPE_SCROLLABLE;
  9667. rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
  9668. DIE_UNLESS(rc);
  9669. if (! opt_silent)
  9670. printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
  9671. type= (ulong) CURSOR_TYPE_READ_ONLY;
  9672. rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
  9673. check_execute(stmt, rc);
  9674. rc= mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS,
  9675. (void*) &prefetch_rows);
  9676. check_execute(stmt, rc);
  9677. stmt_text= "select * from t1";
  9678. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  9679. check_execute(stmt, rc);
  9680. bzero((char*) my_bind, sizeof(my_bind));
  9681. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  9682. my_bind[0].buffer= (void*) &a;
  9683. my_bind[0].buffer_length= sizeof(a);
  9684. mysql_stmt_bind_result(stmt, my_bind);
  9685. rc= mysql_stmt_execute(stmt);
  9686. check_execute(stmt, rc);
  9687. while ((rc= mysql_stmt_fetch(stmt)) == 0)
  9688. ++num_rows;
  9689. DIE_UNLESS(num_rows == 9);
  9690. rc= mysql_stmt_close(stmt);
  9691. DIE_UNLESS(rc == 0);
  9692. rc= mysql_query(mysql, "drop table t1");
  9693. myquery(rc);
  9694. }
  9695. /*
  9696. Bug#11111: fetch from view returns wrong data
  9697. */
  9698. static void test_bug11111()
  9699. {
  9700. MYSQL_STMT *stmt;
  9701. MYSQL_BIND my_bind[2];
  9702. char buf[2][20];
  9703. ulong len[2];
  9704. int i;
  9705. int rc;
  9706. const char *query= "SELECT DISTINCT f1,ff2 FROM v1";
  9707. myheader("test_bug11111");
  9708. rc= mysql_query(mysql, "drop table if exists t1, t2, v1");
  9709. myquery(rc);
  9710. rc= mysql_query(mysql, "drop view if exists t1, t2, v1");
  9711. myquery(rc);
  9712. rc= mysql_query(mysql, "create table t1 (f1 int, f2 int)");
  9713. myquery(rc);
  9714. rc= mysql_query(mysql, "create table t2 (ff1 int, ff2 int)");
  9715. myquery(rc);
  9716. rc= mysql_query(mysql, "create view v1 as select * from t1, t2 where f1=ff1");
  9717. myquery(rc);
  9718. rc= mysql_query(mysql, "insert into t1 values (1,1), (2,2), (3,3)");
  9719. myquery(rc);
  9720. rc= mysql_query(mysql, "insert into t2 values (1,1), (2,2), (3,3)");
  9721. myquery(rc);
  9722. stmt= mysql_stmt_init(mysql);
  9723. mysql_stmt_prepare(stmt, query, strlen(query));
  9724. mysql_stmt_execute(stmt);
  9725. bzero((char*) my_bind, sizeof(my_bind));
  9726. for (i=0; i < 2; i++)
  9727. {
  9728. my_bind[i].buffer_type= MYSQL_TYPE_STRING;
  9729. my_bind[i].buffer= (uchar* *)&buf[i];
  9730. my_bind[i].buffer_length= 20;
  9731. my_bind[i].length= &len[i];
  9732. }
  9733. rc= mysql_stmt_bind_result(stmt, my_bind);
  9734. check_execute(stmt, rc);
  9735. rc= mysql_stmt_fetch(stmt);
  9736. check_execute(stmt, rc);
  9737. if (!opt_silent)
  9738. printf("return: %s", buf[1]);
  9739. DIE_UNLESS(!strcmp(buf[1],"1"));
  9740. mysql_stmt_close(stmt);
  9741. rc= mysql_query(mysql, "drop view v1");
  9742. myquery(rc);
  9743. rc= mysql_query(mysql, "drop table t1, t2");
  9744. myquery(rc);
  9745. }
  9746. /*
  9747. Check that proper cleanups are done for prepared statement when
  9748. fetching thorugh a cursor.
  9749. */
  9750. static void test_bug10729()
  9751. {
  9752. MYSQL_STMT *stmt;
  9753. MYSQL_BIND my_bind[1];
  9754. char a[21];
  9755. int rc;
  9756. const char *stmt_text;
  9757. int i= 0;
  9758. const char *name_array[3]= { "aaa", "bbb", "ccc" };
  9759. ulong type;
  9760. myheader("test_bug10729");
  9761. mysql_query(mysql, "drop table if exists t1");
  9762. mysql_query(mysql, "create table t1 (id integer not null primary key,"
  9763. "name VARCHAR(20) NOT NULL)");
  9764. rc= mysql_query(mysql, "insert into t1 (id, name) values "
  9765. "(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
  9766. myquery(rc);
  9767. stmt= mysql_stmt_init(mysql);
  9768. type= (ulong) CURSOR_TYPE_READ_ONLY;
  9769. rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
  9770. check_execute(stmt, rc);
  9771. stmt_text= "select name from t1";
  9772. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  9773. check_execute(stmt, rc);
  9774. bzero((char*) my_bind, sizeof(my_bind));
  9775. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  9776. my_bind[0].buffer= (void*) a;
  9777. my_bind[0].buffer_length= sizeof(a);
  9778. mysql_stmt_bind_result(stmt, my_bind);
  9779. for (i= 0; i < 3; i++)
  9780. {
  9781. int row_no= 0;
  9782. rc= mysql_stmt_execute(stmt);
  9783. check_execute(stmt, rc);
  9784. while ((rc= mysql_stmt_fetch(stmt)) == 0)
  9785. {
  9786. DIE_UNLESS(strcmp(a, name_array[row_no]) == 0);
  9787. if (!opt_silent)
  9788. printf("%d: %s\n", row_no, a);
  9789. ++row_no;
  9790. }
  9791. DIE_UNLESS(rc == MYSQL_NO_DATA);
  9792. }
  9793. rc= mysql_stmt_close(stmt);
  9794. DIE_UNLESS(rc == 0);
  9795. rc= mysql_query(mysql, "drop table t1");
  9796. myquery(rc);
  9797. }
  9798. /*
  9799. Check that mysql_next_result works properly in case when one of
  9800. the statements used in a multi-statement query is erroneous
  9801. */
  9802. static void test_bug9992()
  9803. {
  9804. MYSQL *mysql1;
  9805. MYSQL_RES* res ;
  9806. int rc;
  9807. myheader("test_bug9992");
  9808. if (!opt_silent)
  9809. printf("Establishing a connection with option CLIENT_MULTI_STATEMENTS..\n");
  9810. mysql1= mysql_client_init(NULL);
  9811. if (!mysql_real_connect(mysql1, opt_host, opt_user, opt_password,
  9812. opt_db ? opt_db : "test", opt_port, opt_unix_socket,
  9813. CLIENT_MULTI_STATEMENTS))
  9814. {
  9815. fprintf(stderr, "Failed to connect to the database\n");
  9816. DIE_UNLESS(0);
  9817. }
  9818. /* Sic: SHOW DATABASE is incorrect syntax. */
  9819. rc= mysql_query(mysql1, "SHOW TABLES; SHOW DATABASE; SELECT 1;");
  9820. if (rc)
  9821. {
  9822. fprintf(stderr, "[%d] %s\n", mysql_errno(mysql1), mysql_error(mysql1));
  9823. DIE_UNLESS(0);
  9824. }
  9825. if (!opt_silent)
  9826. printf("Testing mysql_store_result/mysql_next_result..\n");
  9827. res= mysql_store_result(mysql1);
  9828. DIE_UNLESS(res);
  9829. mysql_free_result(res);
  9830. rc= mysql_next_result(mysql1);
  9831. DIE_UNLESS(rc == 1); /* Got errors, as expected */
  9832. if (!opt_silent)
  9833. fprintf(stdout, "Got error, as expected:\n [%d] %s\n",
  9834. mysql_errno(mysql1), mysql_error(mysql1));
  9835. mysql_close(mysql1);
  9836. }
  9837. /* Bug#10736: cursors and subqueries, memroot management */
  9838. static void test_bug10736()
  9839. {
  9840. MYSQL_STMT *stmt;
  9841. MYSQL_BIND my_bind[1];
  9842. char a[21];
  9843. int rc;
  9844. const char *stmt_text;
  9845. int i= 0;
  9846. ulong type;
  9847. myheader("test_bug10736");
  9848. mysql_query(mysql, "drop table if exists t1");
  9849. mysql_query(mysql, "create table t1 (id integer not null primary key,"
  9850. "name VARCHAR(20) NOT NULL)");
  9851. rc= mysql_query(mysql, "insert into t1 (id, name) values "
  9852. "(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
  9853. myquery(rc);
  9854. stmt= mysql_stmt_init(mysql);
  9855. type= (ulong) CURSOR_TYPE_READ_ONLY;
  9856. rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
  9857. check_execute(stmt, rc);
  9858. stmt_text= "select name from t1 where name=(select name from t1 where id=2)";
  9859. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  9860. check_execute(stmt, rc);
  9861. bzero((char*) my_bind, sizeof(my_bind));
  9862. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  9863. my_bind[0].buffer= (void*) a;
  9864. my_bind[0].buffer_length= sizeof(a);
  9865. mysql_stmt_bind_result(stmt, my_bind);
  9866. for (i= 0; i < 3; i++)
  9867. {
  9868. int row_no= 0;
  9869. rc= mysql_stmt_execute(stmt);
  9870. check_execute(stmt, rc);
  9871. while ((rc= mysql_stmt_fetch(stmt)) == 0)
  9872. {
  9873. if (!opt_silent)
  9874. printf("%d: %s\n", row_no, a);
  9875. ++row_no;
  9876. }
  9877. DIE_UNLESS(rc == MYSQL_NO_DATA);
  9878. }
  9879. rc= mysql_stmt_close(stmt);
  9880. DIE_UNLESS(rc == 0);
  9881. rc= mysql_query(mysql, "drop table t1");
  9882. myquery(rc);
  9883. }
  9884. /* Bug#10794: cursors, packets out of order */
  9885. static void test_bug10794()
  9886. {
  9887. MYSQL_STMT *stmt, *stmt1;
  9888. MYSQL_BIND my_bind[2];
  9889. char a[21];
  9890. int id_val;
  9891. ulong a_len;
  9892. int rc;
  9893. const char *stmt_text;
  9894. int i= 0;
  9895. ulong type;
  9896. myheader("test_bug10794");
  9897. mysql_query(mysql, "drop table if exists t1");
  9898. mysql_query(mysql, "create table t1 (id integer not null primary key,"
  9899. "name varchar(20) not null)");
  9900. stmt= mysql_stmt_init(mysql);
  9901. stmt_text= "insert into t1 (id, name) values (?, ?)";
  9902. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  9903. check_execute(stmt, rc);
  9904. bzero((char*) my_bind, sizeof(my_bind));
  9905. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  9906. my_bind[0].buffer= (void*) &id_val;
  9907. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  9908. my_bind[1].buffer= (void*) a;
  9909. my_bind[1].length= &a_len;
  9910. rc= mysql_stmt_bind_param(stmt, my_bind);
  9911. check_execute(stmt, rc);
  9912. for (i= 0; i < 42; i++)
  9913. {
  9914. id_val= (i+1)*10;
  9915. sprintf(a, "a%d", i);
  9916. a_len= strlen(a); /* safety against broken sprintf */
  9917. rc= mysql_stmt_execute(stmt);
  9918. check_execute(stmt, rc);
  9919. }
  9920. stmt_text= "select name from t1";
  9921. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  9922. type= (ulong) CURSOR_TYPE_READ_ONLY;
  9923. mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  9924. stmt1= mysql_stmt_init(mysql);
  9925. mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  9926. bzero((char*) my_bind, sizeof(my_bind));
  9927. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  9928. my_bind[0].buffer= (void*) a;
  9929. my_bind[0].buffer_length= sizeof(a);
  9930. my_bind[0].length= &a_len;
  9931. rc= mysql_stmt_bind_result(stmt, my_bind);
  9932. check_execute(stmt, rc);
  9933. rc= mysql_stmt_execute(stmt);
  9934. check_execute(stmt, rc);
  9935. rc= mysql_stmt_fetch(stmt);
  9936. check_execute(stmt, rc);
  9937. if (!opt_silent)
  9938. printf("Fetched row from stmt: %s\n", a);
  9939. /* Don't optimize: an attribute of the original test case */
  9940. mysql_stmt_free_result(stmt);
  9941. mysql_stmt_reset(stmt);
  9942. stmt_text= "select name from t1 where id=10";
  9943. rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
  9944. check_execute(stmt1, rc);
  9945. rc= mysql_stmt_bind_result(stmt1, my_bind);
  9946. check_execute(stmt1, rc);
  9947. rc= mysql_stmt_execute(stmt1);
  9948. while (1)
  9949. {
  9950. rc= mysql_stmt_fetch(stmt1);
  9951. if (rc == MYSQL_NO_DATA)
  9952. {
  9953. if (!opt_silent)
  9954. printf("End of data in stmt1\n");
  9955. break;
  9956. }
  9957. check_execute(stmt1, rc);
  9958. if (!opt_silent)
  9959. printf("Fetched row from stmt1: %s\n", a);
  9960. }
  9961. mysql_stmt_close(stmt);
  9962. mysql_stmt_close(stmt1);
  9963. rc= mysql_query(mysql, "drop table t1");
  9964. myquery(rc);
  9965. }
  9966. /* Bug#11172: cursors, crash on a fetch from a datetime column */
  9967. static void test_bug11172()
  9968. {
  9969. MYSQL_STMT *stmt;
  9970. MYSQL_BIND bind_in[1], bind_out[2];
  9971. MYSQL_TIME hired;
  9972. int rc;
  9973. const char *stmt_text;
  9974. int i= 0, id;
  9975. ulong type;
  9976. myheader("test_bug11172");
  9977. mysql_query(mysql, "drop table if exists t1");
  9978. mysql_query(mysql, "create table t1 (id integer not null primary key,"
  9979. "hired date not null)");
  9980. rc= mysql_query(mysql,
  9981. "insert into t1 (id, hired) values (1, '1933-08-24'), "
  9982. "(2, '1965-01-01'), (3, '1949-08-17'), (4, '1945-07-07'), "
  9983. "(5, '1941-05-15'), (6, '1978-09-15'), (7, '1936-03-28')");
  9984. myquery(rc);
  9985. stmt= mysql_stmt_init(mysql);
  9986. stmt_text= "SELECT id, hired FROM t1 WHERE hired=?";
  9987. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  9988. check_execute(stmt, rc);
  9989. type= (ulong) CURSOR_TYPE_READ_ONLY;
  9990. mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  9991. bzero((char*) bind_in, sizeof(bind_in));
  9992. bzero((char*) bind_out, sizeof(bind_out));
  9993. bzero((char*) &hired, sizeof(hired));
  9994. hired.year= 1965;
  9995. hired.month= 1;
  9996. hired.day= 1;
  9997. bind_in[0].buffer_type= MYSQL_TYPE_DATE;
  9998. bind_in[0].buffer= (void*) &hired;
  9999. bind_in[0].buffer_length= sizeof(hired);
  10000. bind_out[0].buffer_type= MYSQL_TYPE_LONG;
  10001. bind_out[0].buffer= (void*) &id;
  10002. bind_out[1]= bind_in[0];
  10003. for (i= 0; i < 3; i++)
  10004. {
  10005. rc= mysql_stmt_bind_param(stmt, bind_in);
  10006. check_execute(stmt, rc);
  10007. rc= mysql_stmt_bind_result(stmt, bind_out);
  10008. check_execute(stmt, rc);
  10009. rc= mysql_stmt_execute(stmt);
  10010. check_execute(stmt, rc);
  10011. while ((rc= mysql_stmt_fetch(stmt)) == 0)
  10012. {
  10013. if (!opt_silent)
  10014. printf("fetched data %d:%d-%d-%d\n", id,
  10015. hired.year, hired.month, hired.day);
  10016. }
  10017. DIE_UNLESS(rc == MYSQL_NO_DATA);
  10018. if (!mysql_stmt_free_result(stmt))
  10019. mysql_stmt_reset(stmt);
  10020. }
  10021. mysql_stmt_close(stmt);
  10022. mysql_rollback(mysql);
  10023. mysql_rollback(mysql);
  10024. rc= mysql_query(mysql, "drop table t1");
  10025. myquery(rc);
  10026. }
  10027. /* Bug#11656: cursors, crash on a fetch from a query with distinct. */
  10028. static void test_bug11656()
  10029. {
  10030. MYSQL_STMT *stmt;
  10031. MYSQL_BIND my_bind[2];
  10032. int rc;
  10033. const char *stmt_text;
  10034. char buf[2][20];
  10035. int i= 0;
  10036. ulong type;
  10037. myheader("test_bug11656");
  10038. mysql_query(mysql, "drop table if exists t1");
  10039. rc= mysql_query(mysql, "create table t1 ("
  10040. "server varchar(40) not null, "
  10041. "test_kind varchar(1) not null, "
  10042. "test_id varchar(30) not null , "
  10043. "primary key (server,test_kind,test_id))");
  10044. myquery(rc);
  10045. stmt_text= "select distinct test_kind, test_id from t1 "
  10046. "where server in (?, ?)";
  10047. stmt= mysql_stmt_init(mysql);
  10048. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  10049. check_execute(stmt, rc);
  10050. type= (ulong) CURSOR_TYPE_READ_ONLY;
  10051. mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  10052. bzero((char*) my_bind, sizeof(my_bind));
  10053. strmov(buf[0], "pcint502_MY2");
  10054. strmov(buf[1], "*");
  10055. for (i=0; i < 2; i++)
  10056. {
  10057. my_bind[i].buffer_type= MYSQL_TYPE_STRING;
  10058. my_bind[i].buffer= (uchar* *)&buf[i];
  10059. my_bind[i].buffer_length= strlen(buf[i]);
  10060. }
  10061. mysql_stmt_bind_param(stmt, my_bind);
  10062. rc= mysql_stmt_execute(stmt);
  10063. check_execute(stmt, rc);
  10064. rc= mysql_stmt_fetch(stmt);
  10065. DIE_UNLESS(rc == MYSQL_NO_DATA);
  10066. mysql_stmt_close(stmt);
  10067. rc= mysql_query(mysql, "drop table t1");
  10068. myquery(rc);
  10069. }
  10070. /*
  10071. Check that the server signals when NO_BACKSLASH_ESCAPES mode is in effect,
  10072. and mysql_real_escape_string() does the right thing as a result.
  10073. */
  10074. static void test_bug10214()
  10075. {
  10076. int len;
  10077. char out[8];
  10078. myheader("test_bug10214");
  10079. DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES));
  10080. len= mysql_real_escape_string(mysql, out, "a'b\\c", 5);
  10081. DIE_UNLESS(memcmp(out, "a\\'b\\\\c", len) == 0);
  10082. mysql_query(mysql, "set sql_mode='NO_BACKSLASH_ESCAPES'");
  10083. DIE_UNLESS(mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES);
  10084. len= mysql_real_escape_string(mysql, out, "a'b\\c", 5);
  10085. DIE_UNLESS(memcmp(out, "a''b\\c", len) == 0);
  10086. mysql_query(mysql, "set sql_mode=''");
  10087. }
  10088. static void test_client_character_set()
  10089. {
  10090. MY_CHARSET_INFO cs;
  10091. char *csname= (char*) "utf8";
  10092. char *csdefault= (char*)mysql_character_set_name(mysql);
  10093. int rc;
  10094. myheader("test_client_character_set");
  10095. rc= mysql_set_character_set(mysql, csname);
  10096. DIE_UNLESS(rc == 0);
  10097. mysql_get_character_set_info(mysql, &cs);
  10098. DIE_UNLESS(!strcmp(cs.csname, "utf8"));
  10099. DIE_UNLESS(!strcmp(cs.name, "utf8_general_ci"));
  10100. /* Restore the default character set */
  10101. rc= mysql_set_character_set(mysql, csdefault);
  10102. myquery(rc);
  10103. }
  10104. /* Test correct max length for MEDIUMTEXT and LONGTEXT columns */
  10105. static void test_bug9735()
  10106. {
  10107. MYSQL_RES *res;
  10108. int rc;
  10109. myheader("test_bug9735");
  10110. rc= mysql_query(mysql, "drop table if exists t1");
  10111. myquery(rc);
  10112. rc= mysql_query(mysql, "create table t1 (a mediumtext, b longtext) "
  10113. "character set latin1");
  10114. myquery(rc);
  10115. rc= mysql_query(mysql, "select * from t1");
  10116. myquery(rc);
  10117. res= mysql_store_result(mysql);
  10118. verify_prepare_field(res, 0, "a", "a", MYSQL_TYPE_BLOB,
  10119. "t1", "t1", current_db, (1U << 24)-1, 0);
  10120. verify_prepare_field(res, 1, "b", "b", MYSQL_TYPE_BLOB,
  10121. "t1", "t1", current_db, ~0U, 0);
  10122. mysql_free_result(res);
  10123. rc= mysql_query(mysql, "drop table t1");
  10124. myquery(rc);
  10125. }
  10126. /* Bug#11183 "mysql_stmt_reset() doesn't reset information about error" */
  10127. static void test_bug11183()
  10128. {
  10129. int rc;
  10130. MYSQL_STMT *stmt;
  10131. char bug_statement[]= "insert into t1 values (1)";
  10132. myheader("test_bug11183");
  10133. mysql_query(mysql, "drop table t1 if exists");
  10134. mysql_query(mysql, "create table t1 (a int)");
  10135. stmt= mysql_stmt_init(mysql);
  10136. DIE_UNLESS(stmt != 0);
  10137. rc= mysql_stmt_prepare(stmt, bug_statement, strlen(bug_statement));
  10138. check_execute(stmt, rc);
  10139. rc= mysql_query(mysql, "drop table t1");
  10140. myquery(rc);
  10141. /* Trying to execute statement that should fail on execute stage */
  10142. rc= mysql_stmt_execute(stmt);
  10143. DIE_UNLESS(rc);
  10144. mysql_stmt_reset(stmt);
  10145. DIE_UNLESS(mysql_stmt_errno(stmt) == 0);
  10146. mysql_query(mysql, "create table t1 (a int)");
  10147. /* Trying to execute statement that should pass ok */
  10148. if (mysql_stmt_execute(stmt))
  10149. {
  10150. mysql_stmt_reset(stmt);
  10151. DIE_UNLESS(mysql_stmt_errno(stmt) == 0);
  10152. }
  10153. mysql_stmt_close(stmt);
  10154. rc= mysql_query(mysql, "drop table t1");
  10155. myquery(rc);
  10156. }
  10157. static void test_bug11037()
  10158. {
  10159. MYSQL_STMT *stmt;
  10160. int rc;
  10161. const char *stmt_text;
  10162. myheader("test_bug11037");
  10163. mysql_query(mysql, "drop table if exists t1");
  10164. rc= mysql_query(mysql, "create table t1 (id int not null)");
  10165. myquery(rc);
  10166. rc= mysql_query(mysql, "insert into t1 values (1)");
  10167. myquery(rc);
  10168. stmt_text= "select id FROM t1";
  10169. stmt= mysql_stmt_init(mysql);
  10170. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  10171. /* expected error */
  10172. rc = mysql_stmt_fetch(stmt);
  10173. DIE_UNLESS(rc==1);
  10174. if (!opt_silent)
  10175. fprintf(stdout, "Got error, as expected:\n [%d] %s\n",
  10176. mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
  10177. rc= mysql_stmt_execute(stmt);
  10178. check_execute(stmt, rc);
  10179. rc= mysql_stmt_fetch(stmt);
  10180. DIE_UNLESS(rc==0);
  10181. rc= mysql_stmt_fetch(stmt);
  10182. DIE_UNLESS(rc==MYSQL_NO_DATA);
  10183. rc= mysql_stmt_fetch(stmt);
  10184. DIE_UNLESS(rc==MYSQL_NO_DATA);
  10185. mysql_stmt_close(stmt);
  10186. rc= mysql_query(mysql, "drop table t1");
  10187. myquery(rc);
  10188. }
  10189. /* Bug#10760: cursors, crash in a fetch after rollback. */
  10190. static void test_bug10760()
  10191. {
  10192. MYSQL_STMT *stmt;
  10193. MYSQL_BIND my_bind[1];
  10194. int rc;
  10195. const char *stmt_text;
  10196. char id_buf[20];
  10197. ulong id_len;
  10198. int i= 0;
  10199. ulong type;
  10200. myheader("test_bug10760");
  10201. mysql_query(mysql, "drop table if exists t1, t2");
  10202. /* create tables */
  10203. rc= mysql_query(mysql, "create table t1 (id integer not null primary key)"
  10204. " engine=MyISAM");
  10205. myquery(rc);
  10206. for (; i < 42; ++i)
  10207. {
  10208. char buf[100];
  10209. sprintf(buf, "insert into t1 (id) values (%d)", i+1);
  10210. rc= mysql_query(mysql, buf);
  10211. myquery(rc);
  10212. }
  10213. mysql_autocommit(mysql, FALSE);
  10214. /* create statement */
  10215. stmt= mysql_stmt_init(mysql);
  10216. type= (ulong) CURSOR_TYPE_READ_ONLY;
  10217. mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  10218. /*
  10219. 1: check that a deadlock within the same connection
  10220. is resolved and an error is returned. The deadlock is modelled
  10221. as follows:
  10222. con1: open cursor for select * from t1;
  10223. con1: insert into t1 (id) values (1)
  10224. */
  10225. stmt_text= "select id from t1 order by 1";
  10226. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  10227. check_execute(stmt, rc);
  10228. rc= mysql_stmt_execute(stmt);
  10229. check_execute(stmt, rc);
  10230. rc= mysql_query(mysql, "update t1 set id=id+100");
  10231. /*
  10232. If cursors are not materialized, the update will return an error;
  10233. we mainly test that it won't deadlock.
  10234. */
  10235. if (rc && !opt_silent)
  10236. printf("Got error (as expected): %s\n", mysql_error(mysql));
  10237. /*
  10238. 2: check that MyISAM tables used in cursors survive
  10239. COMMIT/ROLLBACK.
  10240. */
  10241. rc= mysql_rollback(mysql); /* should not close the cursor */
  10242. myquery(rc);
  10243. rc= mysql_stmt_fetch(stmt);
  10244. check_execute(stmt, rc);
  10245. /*
  10246. 3: check that cursors to InnoDB tables are closed (for now) by
  10247. COMMIT/ROLLBACK.
  10248. */
  10249. if (! have_innodb)
  10250. {
  10251. if (!opt_silent)
  10252. printf("Testing that cursors are closed at COMMIT/ROLLBACK requires "
  10253. "InnoDB.\n");
  10254. }
  10255. else
  10256. {
  10257. stmt_text= "select id from t1 order by 1";
  10258. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  10259. check_execute(stmt, rc);
  10260. rc= mysql_query(mysql, "alter table t1 engine=InnoDB");
  10261. myquery(rc);
  10262. bzero(my_bind, sizeof(my_bind));
  10263. my_bind[0].buffer_type= MYSQL_TYPE_STRING;
  10264. my_bind[0].buffer= (void*) id_buf;
  10265. my_bind[0].buffer_length= sizeof(id_buf);
  10266. my_bind[0].length= &id_len;
  10267. check_execute(stmt, rc);
  10268. mysql_stmt_bind_result(stmt, my_bind);
  10269. rc= mysql_stmt_execute(stmt);
  10270. rc= mysql_stmt_fetch(stmt);
  10271. DIE_UNLESS(rc == 0);
  10272. if (!opt_silent)
  10273. printf("Fetched row %s\n", id_buf);
  10274. rc= mysql_rollback(mysql); /* should close the cursor */
  10275. myquery(rc);
  10276. #if 0
  10277. rc= mysql_stmt_fetch(stmt);
  10278. DIE_UNLESS(rc);
  10279. if (!opt_silent)
  10280. printf("Got error (as expected): %s\n", mysql_error(mysql));
  10281. #endif
  10282. }
  10283. mysql_stmt_close(stmt);
  10284. rc= mysql_query(mysql, "drop table t1");
  10285. myquery(rc);
  10286. mysql_autocommit(mysql, TRUE); /* restore default */
  10287. }
  10288. static void test_bug12001()
  10289. {
  10290. MYSQL *mysql_local;
  10291. MYSQL_RES *result;
  10292. const char *query= "DROP TABLE IF EXISTS test_table;"
  10293. "CREATE TABLE test_table(id INT);"
  10294. "INSERT INTO test_table VALUES(10);"
  10295. "UPDATE test_table SET id=20 WHERE id=10;"
  10296. "SELECT * FROM test_table;"
  10297. "INSERT INTO non_existent_table VALUES(11);";
  10298. int rc, res;
  10299. myheader("test_bug12001");
  10300. if (!(mysql_local= mysql_client_init(NULL)))
  10301. {
  10302. fprintf(stdout, "\n mysql_client_init() failed");
  10303. exit(1);
  10304. }
  10305. /* Create connection that supports multi statements */
  10306. if (!mysql_real_connect(mysql_local, opt_host, opt_user,
  10307. opt_password, current_db, opt_port,
  10308. opt_unix_socket, CLIENT_MULTI_STATEMENTS |
  10309. CLIENT_MULTI_RESULTS))
  10310. {
  10311. fprintf(stdout, "\n mysql_real_connect() failed");
  10312. exit(1);
  10313. }
  10314. rc= mysql_query(mysql_local, query);
  10315. myquery(rc);
  10316. do
  10317. {
  10318. if (mysql_field_count(mysql_local) &&
  10319. (result= mysql_use_result(mysql_local)))
  10320. {
  10321. mysql_free_result(result);
  10322. }
  10323. }
  10324. while (!(res= mysql_next_result(mysql_local)));
  10325. rc= mysql_query(mysql_local, "DROP TABLE IF EXISTS test_table");
  10326. myquery(rc);
  10327. mysql_close(mysql_local);
  10328. DIE_UNLESS(res==1);
  10329. }
  10330. /* Bug#11909: wrong metadata if fetching from two cursors */
  10331. static void test_bug11909()
  10332. {
  10333. MYSQL_STMT *stmt1, *stmt2;
  10334. MYSQL_BIND my_bind[7];
  10335. int rc;
  10336. char firstname[20], midinit[20], lastname[20], workdept[20];
  10337. ulong firstname_len, midinit_len, lastname_len, workdept_len;
  10338. uint32 empno;
  10339. double salary;
  10340. float bonus;
  10341. const char *stmt_text;
  10342. myheader("test_bug11909");
  10343. stmt_text= "drop table if exists t1";
  10344. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  10345. myquery(rc);
  10346. stmt_text= "create table t1 ("
  10347. " empno int(11) not null, firstname varchar(20) not null,"
  10348. " midinit varchar(20) not null, lastname varchar(20) not null,"
  10349. " workdept varchar(6) not null, salary double not null,"
  10350. " bonus float not null, primary key (empno)"
  10351. ") default charset=latin1 collate=latin1_bin";
  10352. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  10353. myquery(rc);
  10354. stmt_text= "insert into t1 values "
  10355. "(10, 'CHRISTINE', 'I', 'HAAS', 'A00', 52750, 1000), "
  10356. "(20, 'MICHAEL', 'L', 'THOMPSON', 'B01', 41250, 800),"
  10357. "(30, 'SALLY', 'A', 'KWAN', 'C01', 38250, 800),"
  10358. "(50, 'JOHN', 'B', 'GEYER', 'E01', 40175, 800), "
  10359. "(60, 'IRVING', 'F', 'STERN', 'D11', 32250, 500)";
  10360. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  10361. myquery(rc);
  10362. /* ****** Begin of trace ****** */
  10363. stmt1= open_cursor("SELECT empno, firstname, midinit, lastname,"
  10364. "workdept, salary, bonus FROM t1");
  10365. bzero(my_bind, sizeof(my_bind));
  10366. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  10367. my_bind[0].buffer= (void*) &empno;
  10368. my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
  10369. my_bind[1].buffer= (void*) firstname;
  10370. my_bind[1].buffer_length= sizeof(firstname);
  10371. my_bind[1].length= &firstname_len;
  10372. my_bind[2].buffer_type= MYSQL_TYPE_VAR_STRING;
  10373. my_bind[2].buffer= (void*) midinit;
  10374. my_bind[2].buffer_length= sizeof(midinit);
  10375. my_bind[2].length= &midinit_len;
  10376. my_bind[3].buffer_type= MYSQL_TYPE_VAR_STRING;
  10377. my_bind[3].buffer= (void*) lastname;
  10378. my_bind[3].buffer_length= sizeof(lastname);
  10379. my_bind[3].length= &lastname_len;
  10380. my_bind[4].buffer_type= MYSQL_TYPE_VAR_STRING;
  10381. my_bind[4].buffer= (void*) workdept;
  10382. my_bind[4].buffer_length= sizeof(workdept);
  10383. my_bind[4].length= &workdept_len;
  10384. my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
  10385. my_bind[5].buffer= (void*) &salary;
  10386. my_bind[6].buffer_type= MYSQL_TYPE_FLOAT;
  10387. my_bind[6].buffer= (void*) &bonus;
  10388. rc= mysql_stmt_bind_result(stmt1, my_bind);
  10389. check_execute(stmt1, rc);
  10390. rc= mysql_stmt_execute(stmt1);
  10391. check_execute(stmt1, rc);
  10392. rc= mysql_stmt_fetch(stmt1);
  10393. DIE_UNLESS(rc == 0);
  10394. DIE_UNLESS(empno == 10);
  10395. DIE_UNLESS(strcmp(firstname, "CHRISTINE") == 0);
  10396. DIE_UNLESS(strcmp(midinit, "I") == 0);
  10397. DIE_UNLESS(strcmp(lastname, "HAAS") == 0);
  10398. DIE_UNLESS(strcmp(workdept, "A00") == 0);
  10399. DIE_UNLESS(salary == (double) 52750.0);
  10400. DIE_UNLESS(bonus == (float) 1000.0);
  10401. stmt2= open_cursor("SELECT empno, firstname FROM t1");
  10402. rc= mysql_stmt_bind_result(stmt2, my_bind);
  10403. check_execute(stmt2, rc);
  10404. rc= mysql_stmt_execute(stmt2);
  10405. check_execute(stmt2, rc);
  10406. rc= mysql_stmt_fetch(stmt2);
  10407. DIE_UNLESS(rc == 0);
  10408. DIE_UNLESS(empno == 10);
  10409. DIE_UNLESS(strcmp(firstname, "CHRISTINE") == 0);
  10410. rc= mysql_stmt_reset(stmt2);
  10411. check_execute(stmt2, rc);
  10412. /* ERROR: next statement should return 0 */
  10413. rc= mysql_stmt_fetch(stmt1);
  10414. DIE_UNLESS(rc == 0);
  10415. mysql_stmt_close(stmt1);
  10416. mysql_stmt_close(stmt2);
  10417. rc= mysql_rollback(mysql);
  10418. myquery(rc);
  10419. rc= mysql_query(mysql, "drop table t1");
  10420. myquery(rc);
  10421. }
  10422. /* Cursors: opening a cursor to a compilicated query with ORDER BY */
  10423. static void test_bug11901()
  10424. {
  10425. MYSQL_STMT *stmt;
  10426. MYSQL_BIND my_bind[2];
  10427. int rc;
  10428. char workdept[20];
  10429. ulong workdept_len;
  10430. uint32 empno;
  10431. const char *stmt_text;
  10432. myheader("test_bug11901");
  10433. stmt_text= "drop table if exists t1, t2";
  10434. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  10435. myquery(rc);
  10436. stmt_text= "create table t1 ("
  10437. " empno int(11) not null, firstname varchar(20) not null,"
  10438. " midinit varchar(20) not null, lastname varchar(20) not null,"
  10439. " workdept varchar(6) not null, salary double not null,"
  10440. " bonus float not null, primary key (empno), "
  10441. " unique key (workdept, empno) "
  10442. ") default charset=latin1 collate=latin1_bin";
  10443. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  10444. myquery(rc);
  10445. stmt_text= "insert into t1 values "
  10446. "(10, 'CHRISTINE', 'I', 'HAAS', 'A00', 52750, 1000),"
  10447. "(20, 'MICHAEL', 'L', 'THOMPSON', 'B01', 41250, 800), "
  10448. "(30, 'SALLY', 'A', 'KWAN', 'C01', 38250, 800), "
  10449. "(50, 'JOHN', 'B', 'GEYER', 'E01', 40175, 800), "
  10450. "(60, 'IRVING', 'F', 'STERN', 'D11', 32250, 500), "
  10451. "(70, 'EVA', 'D', 'PULASKI', 'D21', 36170, 700), "
  10452. "(90, 'EILEEN', 'W', 'HENDERSON', 'E11', 29750, 600), "
  10453. "(100, 'THEODORE', 'Q', 'SPENSER', 'E21', 26150, 500), "
  10454. "(110, 'VINCENZO', 'G', 'LUCCHESSI', 'A00', 46500, 900), "
  10455. "(120, 'SEAN', '', 'O\\'CONNELL', 'A00', 29250, 600), "
  10456. "(130, 'DOLORES', 'M', 'QUINTANA', 'C01', 23800, 500), "
  10457. "(140, 'HEATHER', 'A', 'NICHOLLS', 'C01', 28420, 600), "
  10458. "(150, 'BRUCE', '', 'ADAMSON', 'D11', 25280, 500), "
  10459. "(160, 'ELIZABETH', 'R', 'PIANKA', 'D11', 22250, 400), "
  10460. "(170, 'MASATOSHI', 'J', 'YOSHIMURA', 'D11', 24680, 500), "
  10461. "(180, 'MARILYN', 'S', 'SCOUTTEN', 'D11', 21340, 500), "
  10462. "(190, 'JAMES', 'H', 'WALKER', 'D11', 20450, 400), "
  10463. "(200, 'DAVID', '', 'BROWN', 'D11', 27740, 600), "
  10464. "(210, 'WILLIAM', 'T', 'JONES', 'D11', 18270, 400), "
  10465. "(220, 'JENNIFER', 'K', 'LUTZ', 'D11', 29840, 600), "
  10466. "(230, 'JAMES', 'J', 'JEFFERSON', 'D21', 22180, 400), "
  10467. "(240, 'SALVATORE', 'M', 'MARINO', 'D21', 28760, 600), "
  10468. "(250, 'DANIEL', 'S', 'SMITH', 'D21', 19180, 400), "
  10469. "(260, 'SYBIL', 'P', 'JOHNSON', 'D21', 17250, 300), "
  10470. "(270, 'MARIA', 'L', 'PEREZ', 'D21', 27380, 500), "
  10471. "(280, 'ETHEL', 'R', 'SCHNEIDER', 'E11', 26250, 500), "
  10472. "(290, 'JOHN', 'R', 'PARKER', 'E11', 15340, 300), "
  10473. "(300, 'PHILIP', 'X', 'SMITH', 'E11', 17750, 400), "
  10474. "(310, 'MAUDE', 'F', 'SETRIGHT', 'E11', 15900, 300), "
  10475. "(320, 'RAMLAL', 'V', 'MEHTA', 'E21', 19950, 400), "
  10476. "(330, 'WING', '', 'LEE', 'E21', 25370, 500), "
  10477. "(340, 'JASON', 'R', 'GOUNOT', 'E21', 23840, 500)";
  10478. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  10479. myquery(rc);
  10480. stmt_text= "create table t2 ("
  10481. " deptno varchar(6) not null, deptname varchar(20) not null,"
  10482. " mgrno int(11) not null, location varchar(20) not null,"
  10483. " admrdept varchar(6) not null, refcntd int(11) not null,"
  10484. " refcntu int(11) not null, primary key (deptno)"
  10485. ") default charset=latin1 collate=latin1_bin";
  10486. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  10487. myquery(rc);
  10488. stmt_text= "insert into t2 values "
  10489. "('A00', 'SPIFFY COMPUTER SERV', 10, '', 'A00', 0, 0), "
  10490. "('B01', 'PLANNING', 20, '', 'A00', 0, 0), "
  10491. "('C01', 'INFORMATION CENTER', 30, '', 'A00', 0, 0), "
  10492. "('D01', 'DEVELOPMENT CENTER', 0, '', 'A00', 0, 0),"
  10493. "('D11', 'MANUFACTURING SYSTEM', 60, '', 'D01', 0, 0), "
  10494. "('D21', 'ADMINISTRATION SYSTE', 70, '', 'D01', 0, 0), "
  10495. "('E01', 'SUPPORT SERVICES', 50, '', 'A00', 0, 0), "
  10496. "('E11', 'OPERATIONS', 90, '', 'E01', 0, 0), "
  10497. "('E21', 'SOFTWARE SUPPORT', 100,'', 'E01', 0, 0)";
  10498. rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
  10499. myquery(rc);
  10500. /* ****** Begin of trace ****** */
  10501. stmt= open_cursor("select t1.empno, t1.workdept "
  10502. "from (t1 left join t2 on t2.deptno = t1.workdept) "
  10503. "where t2.deptno in "
  10504. " (select t2.deptno "
  10505. " from (t1 left join t2 on t2.deptno = t1.workdept) "
  10506. " where t1.empno = ?) "
  10507. "order by 1");
  10508. bzero(my_bind, sizeof(my_bind));
  10509. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  10510. my_bind[0].buffer= &empno;
  10511. rc= mysql_stmt_bind_param(stmt, my_bind);
  10512. check_execute(stmt, rc);
  10513. my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
  10514. my_bind[1].buffer= (void*) workdept;
  10515. my_bind[1].buffer_length= sizeof(workdept);
  10516. my_bind[1].length= &workdept_len;
  10517. rc= mysql_stmt_bind_result(stmt, my_bind);
  10518. check_execute(stmt, rc);
  10519. empno= 10;
  10520. /* ERROR: next statement causes a server crash */
  10521. rc= mysql_stmt_execute(stmt);
  10522. check_execute(stmt, rc);
  10523. mysql_stmt_close(stmt);
  10524. rc= mysql_query(mysql, "drop table t1, t2");
  10525. myquery(rc);
  10526. }
  10527. /* Bug#11904: mysql_stmt_attr_set CURSOR_TYPE_READ_ONLY grouping wrong result */
  10528. static void test_bug11904()
  10529. {
  10530. MYSQL_STMT *stmt1;
  10531. int rc;
  10532. const char *stmt_text;
  10533. const ulong type= (ulong)CURSOR_TYPE_READ_ONLY;
  10534. MYSQL_BIND my_bind[2];
  10535. int country_id=0;
  10536. char row_data[11]= {0};
  10537. myheader("test_bug11904");
  10538. /* create tables */
  10539. rc= mysql_query(mysql, "DROP TABLE IF EXISTS bug11904b");
  10540. myquery(rc);
  10541. rc= mysql_query(mysql, "CREATE TABLE bug11904b (id int, name char(10), primary key(id, name))");
  10542. myquery(rc);
  10543. rc= mysql_query(mysql, "INSERT INTO bug11904b VALUES (1, 'sofia'), (1,'plovdiv'),"
  10544. " (1,'varna'), (2,'LA'), (2,'new york'), (3,'heidelberg'),"
  10545. " (3,'berlin'), (3, 'frankfurt')");
  10546. myquery(rc);
  10547. mysql_commit(mysql);
  10548. /* create statement */
  10549. stmt1= mysql_stmt_init(mysql);
  10550. mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  10551. stmt_text= "SELECT id, MIN(name) FROM bug11904b GROUP BY id";
  10552. rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
  10553. check_execute(stmt1, rc);
  10554. memset(my_bind, 0, sizeof(my_bind));
  10555. my_bind[0].buffer_type= MYSQL_TYPE_LONG;
  10556. my_bind[0].buffer=& country_id;
  10557. my_bind[0].buffer_length= 0;
  10558. my_bind[0].length= 0;
  10559. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  10560. my_bind[1].buffer=& row_data;
  10561. my_bind[1].buffer_length= sizeof(row_data) - 1;
  10562. my_bind[1].length= 0;
  10563. rc= mysql_stmt_bind_result(stmt1, my_bind);
  10564. check_execute(stmt1, rc);
  10565. rc= mysql_stmt_execute(stmt1);
  10566. check_execute(stmt1, rc);
  10567. rc= mysql_stmt_fetch(stmt1);
  10568. check_execute(stmt1, rc);
  10569. DIE_UNLESS(country_id == 1);
  10570. DIE_UNLESS(memcmp(row_data, "plovdiv", 7) == 0);
  10571. rc= mysql_stmt_fetch(stmt1);
  10572. check_execute(stmt1, rc);
  10573. DIE_UNLESS(country_id == 2);
  10574. DIE_UNLESS(memcmp(row_data, "LA", 2) == 0);
  10575. rc= mysql_stmt_fetch(stmt1);
  10576. check_execute(stmt1, rc);
  10577. DIE_UNLESS(country_id == 3);
  10578. DIE_UNLESS(memcmp(row_data, "berlin", 6) == 0);
  10579. rc= mysql_stmt_close(stmt1);
  10580. check_execute(stmt1, rc);
  10581. rc= mysql_query(mysql, "drop table bug11904b");
  10582. myquery(rc);
  10583. }
  10584. /* Bug#12243: multiple cursors, crash in a fetch after commit. */
  10585. static void test_bug12243()
  10586. {
  10587. MYSQL_STMT *stmt1, *stmt2;
  10588. int rc;
  10589. const char *stmt_text;
  10590. ulong type;
  10591. myheader("test_bug12243");
  10592. if (! have_innodb)
  10593. {
  10594. if (!opt_silent)
  10595. printf("This test requires InnoDB.\n");
  10596. return;
  10597. }
  10598. /* create tables */
  10599. mysql_query(mysql, "drop table if exists t1");
  10600. mysql_query(mysql, "create table t1 (a int) engine=InnoDB");
  10601. rc= mysql_query(mysql, "insert into t1 (a) values (1), (2)");
  10602. myquery(rc);
  10603. mysql_autocommit(mysql, FALSE);
  10604. /* create statement */
  10605. stmt1= mysql_stmt_init(mysql);
  10606. stmt2= mysql_stmt_init(mysql);
  10607. type= (ulong) CURSOR_TYPE_READ_ONLY;
  10608. mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  10609. mysql_stmt_attr_set(stmt2, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  10610. stmt_text= "select a from t1";
  10611. rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
  10612. check_execute(stmt1, rc);
  10613. rc= mysql_stmt_execute(stmt1);
  10614. check_execute(stmt1, rc);
  10615. rc= mysql_stmt_fetch(stmt1);
  10616. check_execute(stmt1, rc);
  10617. rc= mysql_stmt_prepare(stmt2, stmt_text, strlen(stmt_text));
  10618. check_execute(stmt2, rc);
  10619. rc= mysql_stmt_execute(stmt2);
  10620. check_execute(stmt2, rc);
  10621. rc= mysql_stmt_fetch(stmt2);
  10622. check_execute(stmt2, rc);
  10623. rc= mysql_stmt_close(stmt1);
  10624. check_execute(stmt1, rc);
  10625. rc= mysql_commit(mysql);
  10626. myquery(rc);
  10627. rc= mysql_stmt_fetch(stmt2);
  10628. check_execute(stmt2, rc);
  10629. mysql_stmt_close(stmt2);
  10630. rc= mysql_query(mysql, "drop table t1");
  10631. myquery(rc);
  10632. mysql_autocommit(mysql, TRUE); /* restore default */
  10633. }
  10634. /*
  10635. Bug#11718: query with function, join and order by returns wrong type
  10636. */
  10637. static void test_bug11718()
  10638. {
  10639. MYSQL_RES *res;
  10640. int rc;
  10641. const char *query= "select str_to_date(concat(f3),'%Y%m%d') from t1,t2 "
  10642. "where f1=f2 order by f1";
  10643. myheader("test_bug11718");
  10644. rc= mysql_query(mysql, "drop table if exists t1, t2");
  10645. myquery(rc);
  10646. rc= mysql_query(mysql, "create table t1 (f1 int)");
  10647. myquery(rc);
  10648. rc= mysql_query(mysql, "create table t2 (f2 int, f3 numeric(8))");
  10649. myquery(rc);
  10650. rc= mysql_query(mysql, "insert into t1 values (1), (2)");
  10651. myquery(rc);
  10652. rc= mysql_query(mysql, "insert into t2 values (1,20050101), (2,20050202)");
  10653. myquery(rc);
  10654. rc= mysql_query(mysql, query);
  10655. myquery(rc);
  10656. res = mysql_store_result(mysql);
  10657. if (!opt_silent)
  10658. printf("return type: %s", (res->fields[0].type == MYSQL_TYPE_DATE)?"DATE":
  10659. "not DATE");
  10660. DIE_UNLESS(res->fields[0].type == MYSQL_TYPE_DATE);
  10661. mysql_free_result(res);
  10662. rc= mysql_query(mysql, "drop table t1, t2");
  10663. myquery(rc);
  10664. }
  10665. /*
  10666. Bug #12925: Bad handling of maximum values in getopt
  10667. */
  10668. static void test_bug12925()
  10669. {
  10670. myheader("test_bug12925");
  10671. if (opt_getopt_ll_test)
  10672. DIE_UNLESS(opt_getopt_ll_test == LL(25600*1024*1024));
  10673. }
  10674. /*
  10675. Bug#14210 "Simple query with > operator on large table gives server
  10676. crash"
  10677. */
  10678. static void test_bug14210()
  10679. {
  10680. MYSQL_STMT *stmt;
  10681. int rc, i;
  10682. const char *stmt_text;
  10683. ulong type;
  10684. myheader("test_bug14210");
  10685. mysql_query(mysql, "drop table if exists t1");
  10686. /*
  10687. To trigger the problem the table must be InnoDB, although the problem
  10688. itself is not InnoDB related. In case the table is MyISAM this test
  10689. is harmless.
  10690. */
  10691. mysql_query(mysql, "create table t1 (a varchar(255)) engine=InnoDB");
  10692. rc= mysql_query(mysql, "insert into t1 (a) values (repeat('a', 256))");
  10693. myquery(rc);
  10694. rc= mysql_query(mysql, "set @@session.max_heap_table_size=16384");
  10695. /* Create a big enough table (more than max_heap_table_size) */
  10696. for (i= 0; i < 8; i++)
  10697. {
  10698. rc= mysql_query(mysql, "insert into t1 (a) select a from t1");
  10699. myquery(rc);
  10700. }
  10701. /* create statement */
  10702. stmt= mysql_stmt_init(mysql);
  10703. type= (ulong) CURSOR_TYPE_READ_ONLY;
  10704. mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  10705. stmt_text= "select a from t1";
  10706. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  10707. check_execute(stmt, rc);
  10708. rc= mysql_stmt_execute(stmt);
  10709. while ((rc= mysql_stmt_fetch(stmt)) == 0)
  10710. ;
  10711. DIE_UNLESS(rc == MYSQL_NO_DATA);
  10712. rc= mysql_stmt_close(stmt);
  10713. rc= mysql_query(mysql, "drop table t1");
  10714. myquery(rc);
  10715. rc= mysql_query(mysql, "set @@session.max_heap_table_size=default");
  10716. myquery(rc);
  10717. }
  10718. /* Bug#13488: wrong column metadata when fetching from cursor */
  10719. static void test_bug13488()
  10720. {
  10721. MYSQL_BIND my_bind[3];
  10722. MYSQL_STMT *stmt1;
  10723. int rc, f1, f2, f3, i;
  10724. const ulong type= CURSOR_TYPE_READ_ONLY;
  10725. const char *query= "select * from t1 left join t2 on f1=f2 where f1=1";
  10726. myheader("test_bug13488");
  10727. rc= mysql_query(mysql, "drop table if exists t1, t2");
  10728. myquery(rc);
  10729. rc= mysql_query(mysql, "create table t1 (f1 int not null primary key)");
  10730. myquery(rc);
  10731. rc= mysql_query(mysql, "create table t2 (f2 int not null primary key, "
  10732. "f3 int not null)");
  10733. myquery(rc);
  10734. rc= mysql_query(mysql, "insert into t1 values (1), (2)");
  10735. myquery(rc);
  10736. rc= mysql_query(mysql, "insert into t2 values (1,2), (2,4)");
  10737. myquery(rc);
  10738. memset(my_bind, 0, sizeof(my_bind));
  10739. for (i= 0; i < 3; i++)
  10740. {
  10741. my_bind[i].buffer_type= MYSQL_TYPE_LONG;
  10742. my_bind[i].buffer_length= 4;
  10743. my_bind[i].length= 0;
  10744. }
  10745. my_bind[0].buffer=&f1;
  10746. my_bind[1].buffer=&f2;
  10747. my_bind[2].buffer=&f3;
  10748. stmt1= mysql_stmt_init(mysql);
  10749. rc= mysql_stmt_attr_set(stmt1,STMT_ATTR_CURSOR_TYPE, (const void *)&type);
  10750. check_execute(stmt1, rc);
  10751. rc= mysql_stmt_prepare(stmt1, query, strlen(query));
  10752. check_execute(stmt1, rc);
  10753. rc= mysql_stmt_execute(stmt1);
  10754. check_execute(stmt1, rc);
  10755. rc= mysql_stmt_bind_result(stmt1, my_bind);
  10756. check_execute(stmt1, rc);
  10757. rc= mysql_stmt_fetch(stmt1);
  10758. check_execute(stmt1, rc);
  10759. rc= mysql_stmt_free_result(stmt1);
  10760. check_execute(stmt1, rc);
  10761. rc= mysql_stmt_reset(stmt1);
  10762. check_execute(stmt1, rc);
  10763. rc= mysql_stmt_close(stmt1);
  10764. check_execute(stmt1, rc);
  10765. if (!opt_silent)
  10766. printf("data is: %s", (f1 == 1 && f2 == 1 && f3 == 2)?"OK":
  10767. "wrong");
  10768. DIE_UNLESS(f1 == 1 && f2 == 1 && f3 == 2);
  10769. rc= mysql_query(mysql, "drop table t1, t2");
  10770. myquery(rc);
  10771. }
  10772. /*
  10773. Bug#13524: warnings of a previous command are not reset when fetching
  10774. from a cursor.
  10775. */
  10776. static void test_bug13524()
  10777. {
  10778. MYSQL_STMT *stmt;
  10779. int rc;
  10780. unsigned int warning_count;
  10781. const ulong type= CURSOR_TYPE_READ_ONLY;
  10782. const char *query= "select * from t1";
  10783. myheader("test_bug13524");
  10784. rc= mysql_query(mysql, "drop table if exists t1, t2");
  10785. myquery(rc);
  10786. rc= mysql_query(mysql, "create table t1 (a int not null primary key)");
  10787. myquery(rc);
  10788. rc= mysql_query(mysql, "insert into t1 values (1), (2), (3), (4)");
  10789. myquery(rc);
  10790. stmt= mysql_stmt_init(mysql);
  10791. rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  10792. check_execute(stmt, rc);
  10793. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  10794. check_execute(stmt, rc);
  10795. rc= mysql_stmt_execute(stmt);
  10796. check_execute(stmt, rc);
  10797. rc= mysql_stmt_fetch(stmt);
  10798. check_execute(stmt, rc);
  10799. warning_count= mysql_warning_count(mysql);
  10800. DIE_UNLESS(warning_count == 0);
  10801. /* Check that DROP TABLE produced a warning (no such table) */
  10802. rc= mysql_query(mysql, "drop table if exists t2");
  10803. myquery(rc);
  10804. warning_count= mysql_warning_count(mysql);
  10805. DIE_UNLESS(warning_count == 1);
  10806. /*
  10807. Check that fetch from a cursor cleared the warning from the previous
  10808. command.
  10809. */
  10810. rc= mysql_stmt_fetch(stmt);
  10811. check_execute(stmt, rc);
  10812. warning_count= mysql_warning_count(mysql);
  10813. DIE_UNLESS(warning_count == 0);
  10814. /* Cleanup */
  10815. mysql_stmt_close(stmt);
  10816. rc= mysql_query(mysql, "drop table t1");
  10817. myquery(rc);
  10818. }
  10819. /*
  10820. Bug#14845 "mysql_stmt_fetch returns MYSQL_NO_DATA when COUNT(*) is 0"
  10821. */
  10822. static void test_bug14845()
  10823. {
  10824. MYSQL_STMT *stmt;
  10825. int rc;
  10826. const ulong type= CURSOR_TYPE_READ_ONLY;
  10827. const char *query= "select count(*) from t1 where 1 = 0";
  10828. myheader("test_bug14845");
  10829. rc= mysql_query(mysql, "drop table if exists t1");
  10830. myquery(rc);
  10831. rc= mysql_query(mysql, "create table t1 (id int(11) default null, "
  10832. "name varchar(20) default null)"
  10833. "engine=MyISAM DEFAULT CHARSET=utf8");
  10834. myquery(rc);
  10835. rc= mysql_query(mysql, "insert into t1 values (1,'abc'),(2,'def')");
  10836. myquery(rc);
  10837. stmt= mysql_stmt_init(mysql);
  10838. rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
  10839. check_execute(stmt, rc);
  10840. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  10841. check_execute(stmt, rc);
  10842. rc= mysql_stmt_execute(stmt);
  10843. check_execute(stmt, rc);
  10844. rc= mysql_stmt_fetch(stmt);
  10845. DIE_UNLESS(rc == 0);
  10846. rc= mysql_stmt_fetch(stmt);
  10847. DIE_UNLESS(rc == MYSQL_NO_DATA);
  10848. /* Cleanup */
  10849. mysql_stmt_close(stmt);
  10850. rc= mysql_query(mysql, "drop table t1");
  10851. myquery(rc);
  10852. }
  10853. /*
  10854. Bug #15510: mysql_warning_count returns 0 after mysql_stmt_fetch which
  10855. should warn
  10856. */
  10857. static void test_bug15510()
  10858. {
  10859. MYSQL_STMT *stmt;
  10860. int rc;
  10861. const char *query= "select 1 from dual where 1/0";
  10862. myheader("test_bug15510");
  10863. rc= mysql_query(mysql, "set @@sql_mode='ERROR_FOR_DIVISION_BY_ZERO'");
  10864. myquery(rc);
  10865. stmt= mysql_stmt_init(mysql);
  10866. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  10867. check_execute(stmt, rc);
  10868. rc= mysql_stmt_execute(stmt);
  10869. check_execute(stmt, rc);
  10870. rc= mysql_stmt_fetch(stmt);
  10871. DIE_UNLESS(mysql_warning_count(mysql));
  10872. /* Cleanup */
  10873. mysql_stmt_close(stmt);
  10874. rc= mysql_query(mysql, "set @@sql_mode=''");
  10875. myquery(rc);
  10876. }
  10877. /* Test MYSQL_OPT_RECONNECT, Bug#15719 */
  10878. static void test_opt_reconnect()
  10879. {
  10880. MYSQL *lmysql;
  10881. my_bool my_true= TRUE;
  10882. myheader("test_opt_reconnect");
  10883. if (!(lmysql= mysql_client_init(NULL)))
  10884. {
  10885. myerror("mysql_client_init() failed");
  10886. exit(1);
  10887. }
  10888. if (!opt_silent)
  10889. fprintf(stdout, "reconnect before mysql_options: %d\n", lmysql->reconnect);
  10890. DIE_UNLESS(lmysql->reconnect == 0);
  10891. if (mysql_options(lmysql, MYSQL_OPT_RECONNECT, &my_true))
  10892. {
  10893. myerror("mysql_options failed: unknown option MYSQL_OPT_RECONNECT\n");
  10894. DIE_UNLESS(0);
  10895. }
  10896. /* reconnect should be 1 */
  10897. if (!opt_silent)
  10898. fprintf(stdout, "reconnect after mysql_options: %d\n", lmysql->reconnect);
  10899. DIE_UNLESS(lmysql->reconnect == 1);
  10900. if (!(mysql_real_connect(lmysql, opt_host, opt_user,
  10901. opt_password, current_db, opt_port,
  10902. opt_unix_socket, 0)))
  10903. {
  10904. myerror("connection failed");
  10905. DIE_UNLESS(0);
  10906. }
  10907. /* reconnect should still be 1 */
  10908. if (!opt_silent)
  10909. fprintf(stdout, "reconnect after mysql_real_connect: %d\n",
  10910. lmysql->reconnect);
  10911. DIE_UNLESS(lmysql->reconnect == 1);
  10912. mysql_close(lmysql);
  10913. if (!(lmysql= mysql_client_init(NULL)))
  10914. {
  10915. myerror("mysql_client_init() failed");
  10916. DIE_UNLESS(0);
  10917. }
  10918. if (!opt_silent)
  10919. fprintf(stdout, "reconnect before mysql_real_connect: %d\n", lmysql->reconnect);
  10920. DIE_UNLESS(lmysql->reconnect == 0);
  10921. if (!(mysql_real_connect(lmysql, opt_host, opt_user,
  10922. opt_password, current_db, opt_port,
  10923. opt_unix_socket, 0)))
  10924. {
  10925. myerror("connection failed");
  10926. DIE_UNLESS(0);
  10927. }
  10928. /* reconnect should still be 0 */
  10929. if (!opt_silent)
  10930. fprintf(stdout, "reconnect after mysql_real_connect: %d\n",
  10931. lmysql->reconnect);
  10932. DIE_UNLESS(lmysql->reconnect == 0);
  10933. mysql_close(lmysql);
  10934. }
  10935. #ifndef EMBEDDED_LIBRARY
  10936. static void test_bug12744()
  10937. {
  10938. MYSQL_STMT *prep_stmt = NULL;
  10939. MYSQL *lmysql;
  10940. int rc;
  10941. myheader("test_bug12744");
  10942. lmysql= mysql_client_init(NULL);
  10943. DIE_UNLESS(lmysql);
  10944. if (!mysql_real_connect(lmysql, opt_host, opt_user, opt_password,
  10945. current_db, opt_port, opt_unix_socket, 0))
  10946. {
  10947. fprintf(stderr, "Failed to connect to the database\n");
  10948. DIE_UNLESS(0);
  10949. }
  10950. prep_stmt= mysql_stmt_init(lmysql);
  10951. rc= mysql_stmt_prepare(prep_stmt, "SELECT 1", 8);
  10952. DIE_UNLESS(rc == 0);
  10953. mysql_close(lmysql);
  10954. rc= mysql_stmt_execute(prep_stmt);
  10955. DIE_UNLESS(rc);
  10956. rc= mysql_stmt_reset(prep_stmt);
  10957. DIE_UNLESS(rc);
  10958. rc= mysql_stmt_close(prep_stmt);
  10959. DIE_UNLESS(rc == 0);
  10960. }
  10961. #endif /* EMBEDDED_LIBRARY */
  10962. /* Bug #16143: mysql_stmt_sqlstate returns an empty string instead of '00000' */
  10963. static void test_bug16143()
  10964. {
  10965. MYSQL_STMT *stmt;
  10966. myheader("test_bug16143");
  10967. stmt= mysql_stmt_init(mysql);
  10968. /* Check mysql_stmt_sqlstate return "no error" */
  10969. DIE_UNLESS(strcmp(mysql_stmt_sqlstate(stmt), "00000") == 0);
  10970. mysql_stmt_close(stmt);
  10971. }
  10972. /* Bug #16144: mysql_stmt_attr_get type error */
  10973. static void test_bug16144()
  10974. {
  10975. const my_bool flag_orig= (my_bool) 0xde;
  10976. my_bool flag= flag_orig;
  10977. MYSQL_STMT *stmt;
  10978. myheader("test_bug16144");
  10979. /* Check that attr_get returns correct data on little and big endian CPUs */
  10980. stmt= mysql_stmt_init(mysql);
  10981. mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (const void*) &flag);
  10982. mysql_stmt_attr_get(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &flag);
  10983. DIE_UNLESS(flag == flag_orig);
  10984. mysql_stmt_close(stmt);
  10985. }
  10986. /*
  10987. Bug #15613: "libmysqlclient API function mysql_stmt_prepare returns wrong
  10988. field length"
  10989. */
  10990. static void test_bug15613()
  10991. {
  10992. MYSQL_STMT *stmt;
  10993. const char *stmt_text;
  10994. MYSQL_RES *metadata;
  10995. MYSQL_FIELD *field;
  10996. int rc;
  10997. myheader("test_bug15613");
  10998. /* I. Prepare the table */
  10999. rc= mysql_query(mysql, "set names latin1");
  11000. myquery(rc);
  11001. mysql_query(mysql, "drop table if exists t1");
  11002. rc= mysql_query(mysql,
  11003. "create table t1 (t text character set utf8, "
  11004. "tt tinytext character set utf8, "
  11005. "mt mediumtext character set utf8, "
  11006. "lt longtext character set utf8, "
  11007. "vl varchar(255) character set latin1,"
  11008. "vb varchar(255) character set binary,"
  11009. "vu varchar(255) character set utf8)");
  11010. myquery(rc);
  11011. stmt= mysql_stmt_init(mysql);
  11012. /* II. Check SELECT metadata */
  11013. stmt_text= ("select t, tt, mt, lt, vl, vb, vu from t1");
  11014. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  11015. metadata= mysql_stmt_result_metadata(stmt);
  11016. field= mysql_fetch_fields(metadata);
  11017. if (!opt_silent)
  11018. {
  11019. printf("Field lengths (client character set is latin1):\n"
  11020. "text character set utf8:\t\t%lu\n"
  11021. "tinytext character set utf8:\t\t%lu\n"
  11022. "mediumtext character set utf8:\t\t%lu\n"
  11023. "longtext character set utf8:\t\t%lu\n"
  11024. "varchar(255) character set latin1:\t%lu\n"
  11025. "varchar(255) character set binary:\t%lu\n"
  11026. "varchar(255) character set utf8:\t%lu\n",
  11027. field[0].length, field[1].length, field[2].length, field[3].length,
  11028. field[4].length, field[5].length, field[6].length);
  11029. }
  11030. DIE_UNLESS(field[0].length == 65535);
  11031. DIE_UNLESS(field[1].length == 255);
  11032. DIE_UNLESS(field[2].length == 16777215);
  11033. DIE_UNLESS(field[3].length == 4294967295UL);
  11034. DIE_UNLESS(field[4].length == 255);
  11035. DIE_UNLESS(field[5].length == 255);
  11036. DIE_UNLESS(field[6].length == 255);
  11037. mysql_free_result(metadata);
  11038. mysql_stmt_free_result(stmt);
  11039. /* III. Cleanup */
  11040. rc= mysql_query(mysql, "drop table t1");
  11041. myquery(rc);
  11042. rc= mysql_query(mysql, "set names default");
  11043. myquery(rc);
  11044. mysql_stmt_close(stmt);
  11045. }
  11046. /*
  11047. Bug#17667: An attacker has the opportunity to bypass query logging.
  11048. Note! Also tests Bug#21813, where prepared statements are used to
  11049. run queries
  11050. */
  11051. static void test_bug17667()
  11052. {
  11053. int rc;
  11054. MYSQL_STMT *stmt;
  11055. enum query_type { QT_NORMAL, QT_PREPARED};
  11056. struct buffer_and_length {
  11057. enum query_type qt;
  11058. const char *buffer;
  11059. const uint length;
  11060. } statements[]= {
  11061. { QT_NORMAL, "drop table if exists bug17667", 29 },
  11062. { QT_NORMAL, "create table bug17667 (c varchar(20))", 37 },
  11063. { QT_NORMAL, "insert into bug17667 (c) values ('regular') /* NUL=\0 with comment */", 68 },
  11064. { QT_PREPARED,
  11065. "insert into bug17667 (c) values ('prepared') /* NUL=\0 with comment */", 69, },
  11066. { QT_NORMAL, "insert into bug17667 (c) values ('NUL=\0 in value')", 50 },
  11067. { QT_NORMAL, "insert into bug17667 (c) values ('5 NULs=\0\0\0\0\0')", 48 },
  11068. { QT_PREPARED, "insert into bug17667 (c) values ('6 NULs=\0\0\0\0\0\0')", 50 },
  11069. { QT_NORMAL, "/* NUL=\0 with comment */ insert into bug17667 (c) values ('encore')", 67 },
  11070. { QT_NORMAL, "drop table bug17667", 19 },
  11071. { QT_NORMAL, NULL, 0 } };
  11072. struct buffer_and_length *statement_cursor;
  11073. FILE *log_file;
  11074. char *master_log_filename;
  11075. myheader("test_bug17667");
  11076. master_log_filename = (char *) malloc(strlen(opt_vardir) + strlen("/log/master.log") + 1);
  11077. strxmov(master_log_filename, opt_vardir, "/log/master.log", NullS);
  11078. if (!opt_silent)
  11079. printf("Opening '%s'\n", master_log_filename);
  11080. log_file= my_fopen(master_log_filename, (int) (O_RDONLY | O_BINARY), MYF(0));
  11081. free(master_log_filename);
  11082. if (log_file == NULL)
  11083. {
  11084. if (!opt_silent)
  11085. {
  11086. printf("Could not find the log file, VARDIR/log/master.log, so "
  11087. "test_bug17667 is not run.\n"
  11088. "Run test from the mysql-test/mysql-test-run* program to set up "
  11089. "correct environment for this test.\n\n");
  11090. }
  11091. return;
  11092. }
  11093. enable_query_logs(1);
  11094. for (statement_cursor= statements; statement_cursor->buffer != NULL;
  11095. statement_cursor++)
  11096. {
  11097. if (statement_cursor->qt == QT_NORMAL)
  11098. {
  11099. /* Run statement as normal query */
  11100. rc= mysql_real_query(mysql, statement_cursor->buffer,
  11101. statement_cursor->length);
  11102. myquery(rc);
  11103. }
  11104. else if (statement_cursor->qt == QT_PREPARED)
  11105. {
  11106. /*
  11107. Run as prepared statement
  11108. NOTE! All these queries should be in the log twice,
  11109. one time for prepare and one time for execute
  11110. */
  11111. stmt= mysql_stmt_init(mysql);
  11112. rc= mysql_stmt_prepare(stmt, statement_cursor->buffer,
  11113. statement_cursor->length);
  11114. check_execute(stmt, rc);
  11115. rc= mysql_stmt_execute(stmt);
  11116. check_execute(stmt, rc);
  11117. mysql_stmt_close(stmt);
  11118. }
  11119. else
  11120. {
  11121. DIE_UNLESS(0==1);
  11122. }
  11123. }
  11124. /* Make sure the server has written the logs to disk before reading it */
  11125. rc= mysql_query(mysql, "flush logs");
  11126. myquery(rc);
  11127. for (statement_cursor= statements; statement_cursor->buffer != NULL;
  11128. statement_cursor++)
  11129. {
  11130. int expected_hits= 1, hits= 0;
  11131. char line_buffer[MAX_TEST_QUERY_LENGTH*2];
  11132. /* more than enough room for the query and some marginalia. */
  11133. /* Prepared statments always occurs twice in log */
  11134. if (statement_cursor->qt == QT_PREPARED)
  11135. expected_hits++;
  11136. /* Loop until we found expected number of log entries */
  11137. do {
  11138. /* Loop until statement is found in log */
  11139. do {
  11140. memset(line_buffer, '/', MAX_TEST_QUERY_LENGTH*2);
  11141. if(fgets(line_buffer, MAX_TEST_QUERY_LENGTH*2, log_file) == NULL)
  11142. {
  11143. /* If fgets returned NULL, it indicates either error or EOF */
  11144. if (feof(log_file))
  11145. DIE("Found EOF before all statements where found");
  11146. fprintf(stderr, "Got error %d while reading from file\n",
  11147. ferror(log_file));
  11148. DIE("Read error");
  11149. }
  11150. } while (my_memmem(line_buffer, MAX_TEST_QUERY_LENGTH*2,
  11151. statement_cursor->buffer,
  11152. statement_cursor->length) == NULL);
  11153. hits++;
  11154. } while (hits < expected_hits);
  11155. if (!opt_silent)
  11156. printf("Found statement starting with \"%s\"\n",
  11157. statement_cursor->buffer);
  11158. }
  11159. restore_query_logs();
  11160. if (!opt_silent)
  11161. printf("success. All queries found intact in the log.\n");
  11162. my_fclose(log_file, MYF(0));
  11163. }
  11164. /*
  11165. Bug#14169: type of group_concat() result changed to blob if tmp_table was
  11166. used
  11167. */
  11168. static void test_bug14169()
  11169. {
  11170. MYSQL_STMT *stmt;
  11171. const char *stmt_text;
  11172. MYSQL_RES *res;
  11173. MYSQL_FIELD *field;
  11174. int rc;
  11175. myheader("test_bug14169");
  11176. rc= mysql_query(mysql, "drop table if exists t1");
  11177. myquery(rc);
  11178. rc= mysql_query(mysql, "set session group_concat_max_len=1024");
  11179. myquery(rc);
  11180. rc= mysql_query(mysql, "create table t1 (f1 int unsigned, f2 varchar(255))");
  11181. myquery(rc);
  11182. rc= mysql_query(mysql, "insert into t1 values (1,repeat('a',255)),"
  11183. "(2,repeat('b',255))");
  11184. myquery(rc);
  11185. stmt= mysql_stmt_init(mysql);
  11186. stmt_text= "select f2,group_concat(f1) from t1 group by f2";
  11187. rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  11188. myquery(rc);
  11189. res= mysql_stmt_result_metadata(stmt);
  11190. field= mysql_fetch_fields(res);
  11191. if (!opt_silent)
  11192. printf("GROUP_CONCAT() result type %i", field[1].type);
  11193. DIE_UNLESS(field[1].type == MYSQL_TYPE_BLOB);
  11194. mysql_free_result(res);
  11195. mysql_stmt_free_result(stmt);
  11196. mysql_stmt_close(stmt);
  11197. rc= mysql_query(mysql, "drop table t1");
  11198. myquery(rc);
  11199. }
  11200. /*
  11201. Test that mysql_insert_id() behaves as documented in our manual
  11202. */
  11203. static void test_mysql_insert_id()
  11204. {
  11205. my_ulonglong res;
  11206. int rc;
  11207. myheader("test_mysql_insert_id");
  11208. rc= mysql_query(mysql, "drop table if exists t1");
  11209. myquery(rc);
  11210. /* table without auto_increment column */
  11211. rc= mysql_query(mysql, "create table t1 (f1 int, f2 varchar(255), key(f1))");
  11212. myquery(rc);
  11213. rc= mysql_query(mysql, "insert into t1 values (1,'a')");
  11214. myquery(rc);
  11215. res= mysql_insert_id(mysql);
  11216. DIE_UNLESS(res == 0);
  11217. rc= mysql_query(mysql, "insert into t1 values (null,'b')");
  11218. myquery(rc);
  11219. res= mysql_insert_id(mysql);
  11220. DIE_UNLESS(res == 0);
  11221. rc= mysql_query(mysql, "insert into t1 select 5,'c'");
  11222. myquery(rc);
  11223. res= mysql_insert_id(mysql);
  11224. DIE_UNLESS(res == 0);
  11225. /*
  11226. Test for bug #34889: mysql_client_test::test_mysql_insert_id test fails
  11227. sporadically
  11228. */
  11229. rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
  11230. myquery(rc);
  11231. rc= mysql_query(mysql, "insert into t2 values (null,'b')");
  11232. myquery(rc);
  11233. rc= mysql_query(mysql, "insert into t1 select 5,'c'");
  11234. myquery(rc);
  11235. res= mysql_insert_id(mysql);
  11236. DIE_UNLESS(res == 0);
  11237. rc= mysql_query(mysql, "drop table t2");
  11238. myquery(rc);
  11239. rc= mysql_query(mysql, "insert into t1 select null,'d'");
  11240. myquery(rc);
  11241. res= mysql_insert_id(mysql);
  11242. DIE_UNLESS(res == 0);
  11243. rc= mysql_query(mysql, "insert into t1 values (null,last_insert_id(300))");
  11244. myquery(rc);
  11245. res= mysql_insert_id(mysql);
  11246. DIE_UNLESS(res == 300);
  11247. rc= mysql_query(mysql, "insert into t1 select null,last_insert_id(400)");
  11248. myquery(rc);
  11249. res= mysql_insert_id(mysql);
  11250. /*
  11251. Behaviour change: old code used to return 0; but 400 is consistent
  11252. with INSERT VALUES, and the manual's section of mysql_insert_id() does not
  11253. say INSERT SELECT should be different.
  11254. */
  11255. DIE_UNLESS(res == 400);
  11256. /* table with auto_increment column */
  11257. rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
  11258. myquery(rc);
  11259. rc= mysql_query(mysql, "insert into t2 values (1,'a')");
  11260. myquery(rc);
  11261. res= mysql_insert_id(mysql);
  11262. DIE_UNLESS(res == 1);
  11263. /* this should not influence next INSERT if it doesn't have auto_inc */
  11264. rc= mysql_query(mysql, "insert into t1 values (10,'e')");
  11265. myquery(rc);
  11266. res= mysql_insert_id(mysql);
  11267. DIE_UNLESS(res == 0);
  11268. rc= mysql_query(mysql, "insert into t2 values (null,'b')");
  11269. myquery(rc);
  11270. res= mysql_insert_id(mysql);
  11271. DIE_UNLESS(res == 2);
  11272. rc= mysql_query(mysql, "insert into t2 select 5,'c'");
  11273. myquery(rc);
  11274. res= mysql_insert_id(mysql);
  11275. /*
  11276. Manual says that for multirow insert this should have been 5, but does not
  11277. say for INSERT SELECT. This is a behaviour change: old code used to return
  11278. 0. We try to be consistent with INSERT VALUES.
  11279. */
  11280. DIE_UNLESS(res == 5);
  11281. rc= mysql_query(mysql, "insert into t2 select null,'d'");
  11282. myquery(rc);
  11283. res= mysql_insert_id(mysql);
  11284. DIE_UNLESS(res == 6);
  11285. /* with more than one row */
  11286. rc= mysql_query(mysql, "insert into t2 values (10,'a'),(11,'b')");
  11287. myquery(rc);
  11288. res= mysql_insert_id(mysql);
  11289. DIE_UNLESS(res == 11);
  11290. rc= mysql_query(mysql, "insert into t2 select 12,'a' union select 13,'b'");
  11291. myquery(rc);
  11292. res= mysql_insert_id(mysql);
  11293. /*
  11294. Manual says that for multirow insert this should have been 13, but does
  11295. not say for INSERT SELECT. This is a behaviour change: old code used to
  11296. return 0. We try to be consistent with INSERT VALUES.
  11297. */
  11298. DIE_UNLESS(res == 13);
  11299. rc= mysql_query(mysql, "insert into t2 values (null,'a'),(null,'b')");
  11300. myquery(rc);
  11301. res= mysql_insert_id(mysql);
  11302. DIE_UNLESS(res == 14);
  11303. rc= mysql_query(mysql, "insert into t2 select null,'a' union select null,'b'");
  11304. myquery(rc);
  11305. res= mysql_insert_id(mysql);
  11306. DIE_UNLESS(res == 16);
  11307. rc= mysql_query(mysql, "insert into t2 select 12,'a' union select 13,'b'");
  11308. myquery_r(rc);
  11309. rc= mysql_query(mysql, "insert ignore into t2 select 12,'a' union select 13,'b'");
  11310. myquery(rc);
  11311. res= mysql_insert_id(mysql);
  11312. DIE_UNLESS(res == 0);
  11313. rc= mysql_query(mysql, "insert into t2 values (12,'a'),(13,'b')");
  11314. myquery_r(rc);
  11315. res= mysql_insert_id(mysql);
  11316. DIE_UNLESS(res == 0);
  11317. rc= mysql_query(mysql, "insert ignore into t2 values (12,'a'),(13,'b')");
  11318. myquery(rc);
  11319. res= mysql_insert_id(mysql);
  11320. DIE_UNLESS(res == 0);
  11321. /* mixing autogenerated and explicit values */
  11322. rc= mysql_query(mysql, "insert into t2 values (null,'e'),(12,'a'),(13,'b')");
  11323. myquery_r(rc);
  11324. rc= mysql_query(mysql, "insert into t2 values (null,'e'),(12,'a'),(13,'b'),(25,'g')");
  11325. myquery_r(rc);
  11326. rc= mysql_query(mysql, "insert into t2 values (null,last_insert_id(300))");
  11327. myquery(rc);
  11328. res= mysql_insert_id(mysql);
  11329. /*
  11330. according to the manual, this might be 20 or 300, but it looks like
  11331. auto_increment column takes priority over last_insert_id().
  11332. */
  11333. DIE_UNLESS(res == 20);
  11334. /* If first autogenerated number fails and 2nd works: */
  11335. rc= mysql_query(mysql, "drop table t2");
  11336. myquery(rc);
  11337. rc= mysql_query(mysql, "create table t2 (f1 int not null primary key "
  11338. "auto_increment, f2 varchar(255), unique (f2))");
  11339. myquery(rc);
  11340. rc= mysql_query(mysql, "insert into t2 values (null,'e')");
  11341. res= mysql_insert_id(mysql);
  11342. DIE_UNLESS(res == 1);
  11343. rc= mysql_query(mysql, "insert ignore into t2 values (null,'e'),(null,'a'),(null,'e')");
  11344. myquery(rc);
  11345. res= mysql_insert_id(mysql);
  11346. DIE_UNLESS(res == 2);
  11347. /* If autogenerated fails and explicit works: */
  11348. rc= mysql_query(mysql, "insert ignore into t2 values (null,'e'),(12,'c'),(null,'d')");
  11349. myquery(rc);
  11350. res= mysql_insert_id(mysql);
  11351. /*
  11352. Behaviour change: old code returned 3 (first autogenerated, even if it
  11353. fails); we now return first successful autogenerated.
  11354. */
  11355. DIE_UNLESS(res == 13);
  11356. /* UPDATE may update mysql_insert_id() if it uses LAST_INSERT_ID(#) */
  11357. rc= mysql_query(mysql, "update t2 set f1=14 where f1=12");
  11358. myquery(rc);
  11359. res= mysql_insert_id(mysql);
  11360. DIE_UNLESS(res == 0);
  11361. rc= mysql_query(mysql, "update t2 set f1=0 where f1=14");
  11362. myquery(rc);
  11363. res= mysql_insert_id(mysql);
  11364. DIE_UNLESS(res == 0);
  11365. rc= mysql_query(mysql, "update t2 set f2=last_insert_id(372) where f1=0");
  11366. myquery(rc);
  11367. res= mysql_insert_id(mysql);
  11368. DIE_UNLESS(res == 372);
  11369. /* check that LAST_INSERT_ID() does not update mysql_insert_id(): */
  11370. rc= mysql_query(mysql, "insert into t2 values (null,'g')");
  11371. myquery(rc);
  11372. res= mysql_insert_id(mysql);
  11373. DIE_UNLESS(res == 15);
  11374. rc= mysql_query(mysql, "update t2 set f2=(@li:=last_insert_id()) where f1=15");
  11375. myquery(rc);
  11376. res= mysql_insert_id(mysql);
  11377. DIE_UNLESS(res == 0);
  11378. /*
  11379. Behaviour change: now if ON DUPLICATE KEY UPDATE updates a row,
  11380. mysql_insert_id() returns the id of the row, instead of not being
  11381. affected.
  11382. */
  11383. rc= mysql_query(mysql, "insert into t2 values (null,@li) on duplicate key "
  11384. "update f2=concat('we updated ',f2)");
  11385. myquery(rc);
  11386. res= mysql_insert_id(mysql);
  11387. DIE_UNLESS(res == 15);
  11388. rc= mysql_query(mysql, "drop table t1,t2");
  11389. myquery(rc);
  11390. }
  11391. /*
  11392. Bug#20152: mysql_stmt_execute() writes to MYSQL_TYPE_DATE buffer
  11393. */
  11394. static void test_bug20152()
  11395. {
  11396. MYSQL_BIND my_bind[1];
  11397. MYSQL_STMT *stmt;
  11398. MYSQL_TIME tm;
  11399. int rc;
  11400. const char *query= "INSERT INTO t1 (f1) VALUES (?)";
  11401. myheader("test_bug20152");
  11402. memset(my_bind, 0, sizeof(my_bind));
  11403. my_bind[0].buffer_type= MYSQL_TYPE_DATE;
  11404. my_bind[0].buffer= (void*)&tm;
  11405. tm.year = 2006;
  11406. tm.month = 6;
  11407. tm.day = 18;
  11408. tm.hour = 14;
  11409. tm.minute = 9;
  11410. tm.second = 42;
  11411. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  11412. myquery(rc);
  11413. rc= mysql_query(mysql, "CREATE TABLE t1 (f1 DATE)");
  11414. myquery(rc);
  11415. stmt= mysql_stmt_init(mysql);
  11416. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  11417. check_execute(stmt, rc);
  11418. rc= mysql_stmt_bind_param(stmt, my_bind);
  11419. check_execute(stmt, rc);
  11420. rc= mysql_stmt_execute(stmt);
  11421. check_execute(stmt, rc);
  11422. rc= mysql_stmt_close(stmt);
  11423. check_execute(stmt, rc);
  11424. rc= mysql_query(mysql, "DROP TABLE t1");
  11425. myquery(rc);
  11426. if (tm.hour == 14 && tm.minute == 9 && tm.second == 42) {
  11427. if (!opt_silent)
  11428. printf("OK!");
  11429. } else {
  11430. printf("[14:09:42] != [%02d:%02d:%02d]\n", tm.hour, tm.minute, tm.second);
  11431. DIE_UNLESS(0==1);
  11432. }
  11433. }
  11434. /* Bug#15752 "Lost connection to MySQL server when calling a SP from C API" */
  11435. static void test_bug15752()
  11436. {
  11437. MYSQL mysql_local;
  11438. int rc, i;
  11439. const int ITERATION_COUNT= 100;
  11440. const char *query= "CALL p1()";
  11441. myheader("test_bug15752");
  11442. rc= mysql_query(mysql, "drop procedure if exists p1");
  11443. myquery(rc);
  11444. rc= mysql_query(mysql, "create procedure p1() select 1");
  11445. myquery(rc);
  11446. mysql_client_init(&mysql_local);
  11447. if (! mysql_real_connect(&mysql_local, opt_host, opt_user,
  11448. opt_password, current_db, opt_port,
  11449. opt_unix_socket,
  11450. CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS))
  11451. {
  11452. printf("Unable connect to MySQL server: %s\n", mysql_error(&mysql_local));
  11453. DIE_UNLESS(0);
  11454. }
  11455. rc= mysql_real_query(&mysql_local, query, strlen(query));
  11456. myquery(rc);
  11457. mysql_free_result(mysql_store_result(&mysql_local));
  11458. rc= mysql_real_query(&mysql_local, query, strlen(query));
  11459. DIE_UNLESS(rc && mysql_errno(&mysql_local) == CR_COMMANDS_OUT_OF_SYNC);
  11460. if (! opt_silent)
  11461. printf("Got error (as expected): %s\n", mysql_error(&mysql_local));
  11462. /* Check some other commands too */
  11463. DIE_UNLESS(mysql_next_result(&mysql_local) == 0);
  11464. mysql_free_result(mysql_store_result(&mysql_local));
  11465. DIE_UNLESS(mysql_next_result(&mysql_local) == -1);
  11466. /* The second problem is not reproducible: add the test case */
  11467. for (i = 0; i < ITERATION_COUNT; i++)
  11468. {
  11469. if (mysql_real_query(&mysql_local, query, strlen(query)))
  11470. {
  11471. printf("\ni=%d %s failed: %s\n", i, query, mysql_error(&mysql_local));
  11472. break;
  11473. }
  11474. mysql_free_result(mysql_store_result(&mysql_local));
  11475. DIE_UNLESS(mysql_next_result(&mysql_local) == 0);
  11476. mysql_free_result(mysql_store_result(&mysql_local));
  11477. DIE_UNLESS(mysql_next_result(&mysql_local) == -1);
  11478. }
  11479. mysql_close(&mysql_local);
  11480. rc= mysql_query(mysql, "drop procedure p1");
  11481. myquery(rc);
  11482. }
  11483. /*
  11484. Bug#21206: memory corruption when too many cursors are opened at once
  11485. Memory corruption happens when more than 1024 cursors are open
  11486. simultaneously.
  11487. */
  11488. static void test_bug21206()
  11489. {
  11490. const size_t cursor_count= 1025;
  11491. const char *create_table[]=
  11492. {
  11493. "DROP TABLE IF EXISTS t1",
  11494. "CREATE TABLE t1 (i INT)",
  11495. "INSERT INTO t1 VALUES (1), (2), (3)"
  11496. };
  11497. const char *query= "SELECT * FROM t1";
  11498. Stmt_fetch *fetch_array=
  11499. (Stmt_fetch*) calloc(cursor_count, sizeof(Stmt_fetch));
  11500. Stmt_fetch *fetch;
  11501. DBUG_ENTER("test_bug21206");
  11502. myheader("test_bug21206");
  11503. fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
  11504. for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
  11505. {
  11506. /* Init will exit(1) in case of error */
  11507. stmt_fetch_init(fetch, fetch - fetch_array, query);
  11508. }
  11509. for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
  11510. stmt_fetch_close(fetch);
  11511. free(fetch_array);
  11512. DBUG_VOID_RETURN;
  11513. }
  11514. /*
  11515. Ensure we execute the status code while testing
  11516. */
  11517. static void test_status()
  11518. {
  11519. const char *status;
  11520. DBUG_ENTER("test_status");
  11521. myheader("test_status");
  11522. if (!(status= mysql_stat(mysql)))
  11523. {
  11524. myerror("mysql_stat failed"); /* purecov: inspected */
  11525. die(__FILE__, __LINE__, "mysql_stat failed"); /* purecov: inspected */
  11526. }
  11527. DBUG_VOID_RETURN;
  11528. }
  11529. /*
  11530. Bug#21726: Incorrect result with multiple invocations of
  11531. LAST_INSERT_ID
  11532. Test that client gets updated value of insert_id on UPDATE that uses
  11533. LAST_INSERT_ID(expr).
  11534. select_query added to test for bug
  11535. #26921 Problem in mysql_insert_id() Embedded C API function
  11536. */
  11537. static void test_bug21726()
  11538. {
  11539. const char *create_table[]=
  11540. {
  11541. "DROP TABLE IF EXISTS t1",
  11542. "CREATE TABLE t1 (i INT)",
  11543. "INSERT INTO t1 VALUES (1)",
  11544. };
  11545. const char *update_query= "UPDATE t1 SET i= LAST_INSERT_ID(i + 1)";
  11546. int rc;
  11547. my_ulonglong insert_id;
  11548. const char *select_query= "SELECT * FROM t1";
  11549. MYSQL_RES *result;
  11550. DBUG_ENTER("test_bug21726");
  11551. myheader("test_bug21726");
  11552. fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
  11553. rc= mysql_query(mysql, update_query);
  11554. myquery(rc);
  11555. insert_id= mysql_insert_id(mysql);
  11556. DIE_UNLESS(insert_id == 2);
  11557. rc= mysql_query(mysql, update_query);
  11558. myquery(rc);
  11559. insert_id= mysql_insert_id(mysql);
  11560. DIE_UNLESS(insert_id == 3);
  11561. rc= mysql_query(mysql, select_query);
  11562. myquery(rc);
  11563. insert_id= mysql_insert_id(mysql);
  11564. DIE_UNLESS(insert_id == 3);
  11565. result= mysql_store_result(mysql);
  11566. mysql_free_result(result);
  11567. DBUG_VOID_RETURN;
  11568. }
  11569. /*
  11570. BUG#23383: mysql_affected_rows() returns different values than
  11571. mysql_stmt_affected_rows()
  11572. Test that both mysql_affected_rows() and mysql_stmt_affected_rows()
  11573. return -1 on error, 0 when no rows were affected, and (positive) row
  11574. count when some rows were affected.
  11575. */
  11576. static void test_bug23383()
  11577. {
  11578. const char *insert_query= "INSERT INTO t1 VALUES (1), (2)";
  11579. const char *update_query= "UPDATE t1 SET i= 4 WHERE i = 3";
  11580. MYSQL_STMT *stmt;
  11581. my_ulonglong row_count;
  11582. int rc;
  11583. DBUG_ENTER("test_bug23383");
  11584. myheader("test_bug23383");
  11585. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  11586. myquery(rc);
  11587. rc= mysql_query(mysql, "CREATE TABLE t1 (i INT UNIQUE)");
  11588. myquery(rc);
  11589. rc= mysql_query(mysql, insert_query);
  11590. myquery(rc);
  11591. row_count= mysql_affected_rows(mysql);
  11592. DIE_UNLESS(row_count == 2);
  11593. rc= mysql_query(mysql, insert_query);
  11594. DIE_UNLESS(rc != 0);
  11595. row_count= mysql_affected_rows(mysql);
  11596. DIE_UNLESS(row_count == (my_ulonglong)-1);
  11597. rc= mysql_query(mysql, update_query);
  11598. myquery(rc);
  11599. row_count= mysql_affected_rows(mysql);
  11600. DIE_UNLESS(row_count == 0);
  11601. rc= mysql_query(mysql, "DELETE FROM t1");
  11602. myquery(rc);
  11603. stmt= mysql_stmt_init(mysql);
  11604. DIE_UNLESS(stmt != 0);
  11605. rc= mysql_stmt_prepare(stmt, insert_query, strlen(insert_query));
  11606. check_execute(stmt, rc);
  11607. rc= mysql_stmt_execute(stmt);
  11608. check_execute(stmt, rc);
  11609. row_count= mysql_stmt_affected_rows(stmt);
  11610. DIE_UNLESS(row_count == 2);
  11611. rc= mysql_stmt_execute(stmt);
  11612. DIE_UNLESS(rc != 0);
  11613. row_count= mysql_stmt_affected_rows(stmt);
  11614. DIE_UNLESS(row_count == (my_ulonglong)-1);
  11615. rc= mysql_stmt_prepare(stmt, update_query, strlen(update_query));
  11616. check_execute(stmt, rc);
  11617. rc= mysql_stmt_execute(stmt);
  11618. check_execute(stmt, rc);
  11619. row_count= mysql_stmt_affected_rows(stmt);
  11620. DIE_UNLESS(row_count == 0);
  11621. rc= mysql_stmt_close(stmt);
  11622. check_execute(stmt, rc);
  11623. rc= mysql_query(mysql, "DROP TABLE t1");
  11624. myquery(rc);
  11625. DBUG_VOID_RETURN;
  11626. }
  11627. /*
  11628. BUG#21635: MYSQL_FIELD struct's member strings seem to misbehave for
  11629. expression cols
  11630. Check that for MIN(), MAX(), COUNT() only MYSQL_FIELD::name is set
  11631. to either expression or its alias, and db, org_table, table,
  11632. org_name fields are empty strings.
  11633. */
  11634. static void test_bug21635()
  11635. {
  11636. const char *expr[]=
  11637. {
  11638. "MIN(i)", "MIN(i)",
  11639. "MIN(i) AS A1", "A1",
  11640. "MAX(i)", "MAX(i)",
  11641. "MAX(i) AS A2", "A2",
  11642. "COUNT(i)", "COUNT(i)",
  11643. "COUNT(i) AS A3", "A3",
  11644. };
  11645. char query[MAX_TEST_QUERY_LENGTH];
  11646. char *query_end;
  11647. MYSQL_RES *result;
  11648. MYSQL_FIELD *field;
  11649. unsigned int field_count, i, j;
  11650. int rc;
  11651. DBUG_ENTER("test_bug21635");
  11652. myheader("test_bug21635");
  11653. query_end= strxmov(query, "SELECT ", NullS);
  11654. for (i= 0; i < sizeof(expr) / sizeof(*expr) / 2; ++i)
  11655. query_end= strxmov(query_end, expr[i * 2], ", ", NullS);
  11656. query_end= strxmov(query_end - 2, " FROM t1 GROUP BY i", NullS);
  11657. DIE_UNLESS(query_end - query < MAX_TEST_QUERY_LENGTH);
  11658. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  11659. myquery(rc);
  11660. rc= mysql_query(mysql, "CREATE TABLE t1 (i INT)");
  11661. myquery(rc);
  11662. /*
  11663. We need this loop to ensure correct behavior with both constant and
  11664. non-constant tables.
  11665. */
  11666. for (j= 0; j < 2 ; j++)
  11667. {
  11668. rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
  11669. myquery(rc);
  11670. rc= mysql_real_query(mysql, query, query_end - query);
  11671. myquery(rc);
  11672. result= mysql_use_result(mysql);
  11673. DIE_UNLESS(result);
  11674. field_count= mysql_field_count(mysql);
  11675. for (i= 0; i < field_count; ++i)
  11676. {
  11677. field= mysql_fetch_field_direct(result, i);
  11678. if (!opt_silent)
  11679. if (!opt_silent)
  11680. printf("%s -> %s ... ", expr[i * 2], field->name);
  11681. fflush(stdout);
  11682. DIE_UNLESS(field->db[0] == 0 && field->org_table[0] == 0 &&
  11683. field->table[0] == 0 && field->org_name[0] == 0);
  11684. DIE_UNLESS(strcmp(field->name, expr[i * 2 + 1]) == 0);
  11685. if (!opt_silent)
  11686. if (!opt_silent)
  11687. puts("OK");
  11688. }
  11689. mysql_free_result(result);
  11690. }
  11691. rc= mysql_query(mysql, "DROP TABLE t1");
  11692. myquery(rc);
  11693. DBUG_VOID_RETURN;
  11694. }
  11695. /*
  11696. Bug#24179 "select b into $var" fails with --cursor_protocol"
  11697. The failure is correct, check that the returned message is meaningful.
  11698. */
  11699. static void test_bug24179()
  11700. {
  11701. int rc;
  11702. MYSQL_STMT *stmt;
  11703. DBUG_ENTER("test_bug24179");
  11704. myheader("test_bug24179");
  11705. stmt= open_cursor("select 1 into @a");
  11706. rc= mysql_stmt_execute(stmt);
  11707. DIE_UNLESS(rc);
  11708. if (!opt_silent)
  11709. {
  11710. printf("Got error (as expected): %d %s\n",
  11711. mysql_stmt_errno(stmt),
  11712. mysql_stmt_error(stmt));
  11713. }
  11714. DIE_UNLESS(mysql_stmt_errno(stmt) == 1323);
  11715. mysql_stmt_close(stmt);
  11716. DBUG_VOID_RETURN;
  11717. }
  11718. /**
  11719. Bug#32265 Server returns different metadata if prepared statement is used
  11720. */
  11721. static void test_bug32265()
  11722. {
  11723. int rc;
  11724. MYSQL_STMT *stmt;
  11725. MYSQL_FIELD *field;
  11726. MYSQL_RES *metadata;
  11727. DBUG_ENTER("test_bug32265");
  11728. myheader("test_bug32265");
  11729. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  11730. myquery(rc);
  11731. rc= mysql_query(mysql, "CREATE TABLE t1 (a INTEGER)");
  11732. myquery(rc);
  11733. rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
  11734. myquery(rc);
  11735. rc= mysql_query(mysql, "CREATE VIEW v1 AS SELECT * FROM t1");
  11736. myquery(rc);
  11737. stmt= open_cursor("SELECT * FROM t1");
  11738. rc= mysql_stmt_execute(stmt);
  11739. check_execute(stmt, rc);
  11740. metadata= mysql_stmt_result_metadata(stmt);
  11741. field= mysql_fetch_field(metadata);
  11742. DIE_UNLESS(field);
  11743. DIE_UNLESS(strcmp(field->table, "t1") == 0);
  11744. DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
  11745. DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
  11746. mysql_free_result(metadata);
  11747. mysql_stmt_close(stmt);
  11748. stmt= open_cursor("SELECT a '' FROM t1 ``");
  11749. rc= mysql_stmt_execute(stmt);
  11750. check_execute(stmt, rc);
  11751. metadata= mysql_stmt_result_metadata(stmt);
  11752. field= mysql_fetch_field(metadata);
  11753. DIE_UNLESS(strcmp(field->table, "") == 0);
  11754. DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
  11755. DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
  11756. mysql_free_result(metadata);
  11757. mysql_stmt_close(stmt);
  11758. stmt= open_cursor("SELECT a '' FROM t1 ``");
  11759. rc= mysql_stmt_execute(stmt);
  11760. check_execute(stmt, rc);
  11761. metadata= mysql_stmt_result_metadata(stmt);
  11762. field= mysql_fetch_field(metadata);
  11763. DIE_UNLESS(strcmp(field->table, "") == 0);
  11764. DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
  11765. DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
  11766. mysql_free_result(metadata);
  11767. mysql_stmt_close(stmt);
  11768. stmt= open_cursor("SELECT * FROM v1");
  11769. rc= mysql_stmt_execute(stmt);
  11770. check_execute(stmt, rc);
  11771. metadata= mysql_stmt_result_metadata(stmt);
  11772. field= mysql_fetch_field(metadata);
  11773. DIE_UNLESS(strcmp(field->table, "v1") == 0);
  11774. DIE_UNLESS(strcmp(field->org_table, "v1") == 0);
  11775. DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
  11776. mysql_free_result(metadata);
  11777. mysql_stmt_close(stmt);
  11778. stmt= open_cursor("SELECT * FROM v1 /* SIC */ GROUP BY 1");
  11779. rc= mysql_stmt_execute(stmt);
  11780. check_execute(stmt, rc);
  11781. metadata= mysql_stmt_result_metadata(stmt);
  11782. field= mysql_fetch_field(metadata);
  11783. DIE_UNLESS(strcmp(field->table, "v1") == 0);
  11784. DIE_UNLESS(strcmp(field->org_table, "v1") == 0);
  11785. DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
  11786. mysql_free_result(metadata);
  11787. mysql_stmt_close(stmt);
  11788. rc= mysql_query(mysql, "DROP VIEW v1");
  11789. myquery(rc);
  11790. rc= mysql_query(mysql, "DROP TABLE t1");
  11791. myquery(rc);
  11792. DBUG_VOID_RETURN;
  11793. }
  11794. /*
  11795. Bug#28075 "COM_DEBUG crashes mysqld"
  11796. */
  11797. static void test_bug28075()
  11798. {
  11799. int rc;
  11800. DBUG_ENTER("test_bug28075");
  11801. myheader("test_bug28075");
  11802. rc= mysql_dump_debug_info(mysql);
  11803. DIE_UNLESS(rc == 0);
  11804. rc= mysql_ping(mysql);
  11805. DIE_UNLESS(rc == 0);
  11806. DBUG_VOID_RETURN;
  11807. }
  11808. /*
  11809. Bug#27876 (SF with cyrillic variable name fails during execution (regression))
  11810. */
  11811. static void test_bug27876()
  11812. {
  11813. int rc;
  11814. MYSQL_RES *result;
  11815. uchar utf8_func[] =
  11816. {
  11817. 0xd1, 0x84, 0xd1, 0x83, 0xd0, 0xbd, 0xd0, 0xba,
  11818. 0xd1, 0x86, 0xd0, 0xb8, 0xd0, 0xb9, 0xd0, 0xba,
  11819. 0xd0, 0xb0,
  11820. 0x00
  11821. };
  11822. uchar utf8_param[] =
  11823. {
  11824. 0xd0, 0xbf, 0xd0, 0xb0, 0xd1, 0x80, 0xd0, 0xb0,
  11825. 0xd0, 0xbc, 0xd0, 0xb5, 0xd1, 0x82, 0xd1, 0x8a,
  11826. 0xd1, 0x80, 0x5f, 0xd0, 0xb2, 0xd0, 0xb5, 0xd1,
  11827. 0x80, 0xd1, 0x81, 0xd0, 0xb8, 0xd1, 0x8f,
  11828. 0x00
  11829. };
  11830. char query[500];
  11831. DBUG_ENTER("test_bug27876");
  11832. myheader("test_bug27876");
  11833. rc= mysql_query(mysql, "set names utf8");
  11834. myquery(rc);
  11835. rc= mysql_query(mysql, "select version()");
  11836. myquery(rc);
  11837. result= mysql_store_result(mysql);
  11838. mytest(result);
  11839. mysql_free_result(result);
  11840. sprintf(query, "DROP FUNCTION IF EXISTS %s", (char*) utf8_func);
  11841. rc= mysql_query(mysql, query);
  11842. myquery(rc);
  11843. sprintf(query,
  11844. "CREATE FUNCTION %s( %s VARCHAR(25))"
  11845. " RETURNS VARCHAR(25) DETERMINISTIC RETURN %s",
  11846. (char*) utf8_func, (char*) utf8_param, (char*) utf8_param);
  11847. rc= mysql_query(mysql, query);
  11848. myquery(rc);
  11849. sprintf(query, "SELECT %s(VERSION())", (char*) utf8_func);
  11850. rc= mysql_query(mysql, query);
  11851. myquery(rc);
  11852. result= mysql_store_result(mysql);
  11853. mytest(result);
  11854. mysql_free_result(result);
  11855. sprintf(query, "DROP FUNCTION %s", (char*) utf8_func);
  11856. rc= mysql_query(mysql, query);
  11857. myquery(rc);
  11858. rc= mysql_query(mysql, "set names default");
  11859. myquery(rc);
  11860. DBUG_VOID_RETURN;
  11861. }
  11862. /*
  11863. Bug#28505: mysql_affected_rows() returns wrong value if CLIENT_FOUND_ROWS
  11864. flag is set.
  11865. */
  11866. static void test_bug28505()
  11867. {
  11868. my_ulonglong res;
  11869. myquery(mysql_query(mysql, "drop table if exists t1"));
  11870. myquery(mysql_query(mysql, "create table t1(f1 int primary key)"));
  11871. myquery(mysql_query(mysql, "insert into t1 values(1)"));
  11872. myquery(mysql_query(mysql,
  11873. "insert into t1 values(1) on duplicate key update f1=1"));
  11874. res= mysql_affected_rows(mysql);
  11875. DIE_UNLESS(!res);
  11876. myquery(mysql_query(mysql, "drop table t1"));
  11877. }
  11878. /*
  11879. Bug#28934: server crash when receiving malformed com_execute packets
  11880. */
  11881. static void test_bug28934()
  11882. {
  11883. my_bool error= 0;
  11884. MYSQL_BIND bind[5];
  11885. MYSQL_STMT *stmt;
  11886. int cnt;
  11887. myquery(mysql_query(mysql, "drop table if exists t1"));
  11888. myquery(mysql_query(mysql, "create table t1(id int)"));
  11889. myquery(mysql_query(mysql, "insert into t1 values(1),(2),(3),(4),(5)"));
  11890. stmt= mysql_simple_prepare(mysql,"select * from t1 where id in(?,?,?,?,?)");
  11891. check_stmt(stmt);
  11892. memset (&bind, 0, sizeof (bind));
  11893. for (cnt= 0; cnt < 5; cnt++)
  11894. {
  11895. bind[cnt].buffer_type= MYSQL_TYPE_LONG;
  11896. bind[cnt].buffer= (char*)&cnt;
  11897. bind[cnt].buffer_length= 0;
  11898. }
  11899. myquery(mysql_stmt_bind_param(stmt, bind));
  11900. stmt->param_count=2;
  11901. error= mysql_stmt_execute(stmt);
  11902. DIE_UNLESS(error != 0);
  11903. myerror(NULL);
  11904. mysql_stmt_close(stmt);
  11905. myquery(mysql_query(mysql, "drop table t1"));
  11906. }
  11907. /*
  11908. Test mysql_change_user() C API and COM_CHANGE_USER
  11909. */
  11910. static void test_change_user()
  11911. {
  11912. char buff[256];
  11913. const char *user_pw= "mysqltest_pw";
  11914. const char *user_no_pw= "mysqltest_no_pw";
  11915. const char *pw= "password";
  11916. const char *db= "mysqltest_user_test_database";
  11917. int rc;
  11918. DBUG_ENTER("test_change_user");
  11919. myheader("test_change_user");
  11920. /* Prepare environment */
  11921. sprintf(buff, "drop database if exists %s", db);
  11922. rc= mysql_query(mysql, buff);
  11923. myquery(rc);
  11924. sprintf(buff, "create database %s", db);
  11925. rc= mysql_query(mysql, buff);
  11926. myquery(rc);
  11927. sprintf(buff,
  11928. "grant select on %s.* to %s@'%%' identified by '%s'",
  11929. db,
  11930. user_pw,
  11931. pw);
  11932. rc= mysql_query(mysql, buff);
  11933. myquery(rc);
  11934. sprintf(buff,
  11935. "grant select on %s.* to %s@'localhost' identified by '%s'",
  11936. db,
  11937. user_pw,
  11938. pw);
  11939. rc= mysql_query(mysql, buff);
  11940. myquery(rc);
  11941. sprintf(buff,
  11942. "grant select on %s.* to %s@'%%'",
  11943. db,
  11944. user_no_pw);
  11945. rc= mysql_query(mysql, buff);
  11946. myquery(rc);
  11947. sprintf(buff,
  11948. "grant select on %s.* to %s@'localhost'",
  11949. db,
  11950. user_no_pw);
  11951. rc= mysql_query(mysql, buff);
  11952. myquery(rc);
  11953. /* Try some combinations */
  11954. rc= mysql_change_user(mysql, NULL, NULL, NULL);
  11955. DIE_UNLESS(rc);
  11956. if (! opt_silent)
  11957. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11958. rc= mysql_change_user(mysql, "", NULL, NULL);
  11959. DIE_UNLESS(rc);
  11960. if (! opt_silent)
  11961. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11962. rc= mysql_change_user(mysql, "", "", NULL);
  11963. DIE_UNLESS(rc);
  11964. if (! opt_silent)
  11965. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11966. rc= mysql_change_user(mysql, "", "", "");
  11967. DIE_UNLESS(rc);
  11968. if (! opt_silent)
  11969. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11970. rc= mysql_change_user(mysql, NULL, "", "");
  11971. DIE_UNLESS(rc);
  11972. if (! opt_silent)
  11973. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11974. rc= mysql_change_user(mysql, NULL, NULL, "");
  11975. DIE_UNLESS(rc);
  11976. if (! opt_silent)
  11977. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11978. rc= mysql_change_user(mysql, "", NULL, "");
  11979. DIE_UNLESS(rc);
  11980. if (! opt_silent)
  11981. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11982. rc= mysql_change_user(mysql, user_pw, NULL, "");
  11983. DIE_UNLESS(rc);
  11984. if (! opt_silent)
  11985. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11986. rc= mysql_change_user(mysql, user_pw, "", "");
  11987. DIE_UNLESS(rc);
  11988. if (! opt_silent)
  11989. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11990. rc= mysql_change_user(mysql, user_pw, "", NULL);
  11991. DIE_UNLESS(rc);
  11992. if (! opt_silent)
  11993. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11994. rc= mysql_change_user(mysql, user_pw, NULL, NULL);
  11995. DIE_UNLESS(rc);
  11996. if (! opt_silent)
  11997. printf("Got error (as expected): %s\n", mysql_error(mysql));
  11998. rc= mysql_change_user(mysql, user_pw, "", db);
  11999. DIE_UNLESS(rc);
  12000. if (! opt_silent)
  12001. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12002. rc= mysql_change_user(mysql, user_pw, NULL, db);
  12003. DIE_UNLESS(rc);
  12004. if (! opt_silent)
  12005. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12006. rc= mysql_change_user(mysql, user_pw, pw, db);
  12007. myquery(rc);
  12008. rc= mysql_change_user(mysql, user_pw, pw, NULL);
  12009. myquery(rc);
  12010. rc= mysql_change_user(mysql, user_pw, pw, "");
  12011. myquery(rc);
  12012. rc= mysql_change_user(mysql, user_no_pw, pw, db);
  12013. DIE_UNLESS(rc);
  12014. if (! opt_silent)
  12015. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12016. rc= mysql_change_user(mysql, user_no_pw, pw, "");
  12017. DIE_UNLESS(rc);
  12018. if (! opt_silent)
  12019. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12020. rc= mysql_change_user(mysql, user_no_pw, pw, NULL);
  12021. DIE_UNLESS(rc);
  12022. if (! opt_silent)
  12023. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12024. rc= mysql_change_user(mysql, user_no_pw, "", NULL);
  12025. myquery(rc);
  12026. rc= mysql_change_user(mysql, user_no_pw, "", "");
  12027. myquery(rc);
  12028. rc= mysql_change_user(mysql, user_no_pw, "", db);
  12029. myquery(rc);
  12030. rc= mysql_change_user(mysql, user_no_pw, NULL, db);
  12031. myquery(rc);
  12032. rc= mysql_change_user(mysql, "", pw, db);
  12033. DIE_UNLESS(rc);
  12034. if (! opt_silent)
  12035. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12036. rc= mysql_change_user(mysql, "", pw, "");
  12037. DIE_UNLESS(rc);
  12038. if (! opt_silent)
  12039. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12040. rc= mysql_change_user(mysql, "", pw, NULL);
  12041. DIE_UNLESS(rc);
  12042. if (! opt_silent)
  12043. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12044. rc= mysql_change_user(mysql, NULL, pw, NULL);
  12045. DIE_UNLESS(rc);
  12046. if (! opt_silent)
  12047. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12048. rc= mysql_change_user(mysql, NULL, NULL, db);
  12049. DIE_UNLESS(rc);
  12050. if (! opt_silent)
  12051. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12052. rc= mysql_change_user(mysql, NULL, "", db);
  12053. DIE_UNLESS(rc);
  12054. if (! opt_silent)
  12055. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12056. rc= mysql_change_user(mysql, "", "", db);
  12057. DIE_UNLESS(rc);
  12058. if (! opt_silent)
  12059. printf("Got error (as expected): %s\n", mysql_error(mysql));
  12060. /* Cleanup the environment */
  12061. mysql_change_user(mysql, opt_user, opt_password, current_db);
  12062. sprintf(buff, "drop database %s", db);
  12063. rc= mysql_query(mysql, buff);
  12064. myquery(rc);
  12065. sprintf(buff, "drop user %s@'%%'", user_pw);
  12066. rc= mysql_query(mysql, buff);
  12067. myquery(rc);
  12068. sprintf(buff, "drop user %s@'%%'", user_no_pw);
  12069. rc= mysql_query(mysql, buff);
  12070. myquery(rc);
  12071. sprintf(buff, "drop user %s@'localhost'", user_pw);
  12072. rc= mysql_query(mysql, buff);
  12073. myquery(rc);
  12074. sprintf(buff, "drop user %s@'localhost'", user_no_pw);
  12075. rc= mysql_query(mysql, buff);
  12076. myquery(rc);
  12077. DBUG_VOID_RETURN;
  12078. }
  12079. /*
  12080. Bug#27592 (stack overrun when storing datetime value using prepared statements)
  12081. */
  12082. static void test_bug27592()
  12083. {
  12084. const int NUM_ITERATIONS= 40;
  12085. int i;
  12086. int rc;
  12087. MYSQL_STMT *stmt= NULL;
  12088. MYSQL_BIND bind[1];
  12089. MYSQL_TIME time_val;
  12090. DBUG_ENTER("test_bug27592");
  12091. myheader("test_bug27592");
  12092. mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  12093. mysql_query(mysql, "CREATE TABLE t1(c2 DATETIME)");
  12094. stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 VALUES (?)");
  12095. DIE_UNLESS(stmt);
  12096. memset(bind, 0, sizeof(bind));
  12097. bind[0].buffer_type= MYSQL_TYPE_DATETIME;
  12098. bind[0].buffer= (char *) &time_val;
  12099. bind[0].length= NULL;
  12100. for (i= 0; i < NUM_ITERATIONS; i++)
  12101. {
  12102. time_val.year= 2007;
  12103. time_val.month= 6;
  12104. time_val.day= 7;
  12105. time_val.hour= 18;
  12106. time_val.minute= 41;
  12107. time_val.second= 3;
  12108. time_val.second_part=0;
  12109. time_val.neg=0;
  12110. rc= mysql_stmt_bind_param(stmt, bind);
  12111. check_execute(stmt, rc);
  12112. rc= mysql_stmt_execute(stmt);
  12113. check_execute(stmt, rc);
  12114. }
  12115. mysql_stmt_close(stmt);
  12116. DBUG_VOID_RETURN;
  12117. }
  12118. /*
  12119. Bug#29687 mysql_stmt_store_result memory leak in libmysqld
  12120. */
  12121. static void test_bug29687()
  12122. {
  12123. const int NUM_ITERATIONS= 40;
  12124. int i;
  12125. int rc;
  12126. MYSQL_STMT *stmt= NULL;
  12127. DBUG_ENTER("test_bug29687");
  12128. myheader("test_bug29687");
  12129. stmt= mysql_simple_prepare(mysql, "SELECT 1 FROM dual WHERE 0=2");
  12130. DIE_UNLESS(stmt);
  12131. for (i= 0; i < NUM_ITERATIONS; i++)
  12132. {
  12133. rc= mysql_stmt_execute(stmt);
  12134. check_execute(stmt, rc);
  12135. mysql_stmt_store_result(stmt);
  12136. while (mysql_stmt_fetch(stmt)==0);
  12137. mysql_stmt_free_result(stmt);
  12138. }
  12139. mysql_stmt_close(stmt);
  12140. DBUG_VOID_RETURN;
  12141. }
  12142. /*
  12143. Bug #29692 Single row inserts can incorrectly report a huge number of
  12144. row insertions
  12145. */
  12146. static void test_bug29692()
  12147. {
  12148. MYSQL* conn;
  12149. if (!(conn= mysql_client_init(NULL)))
  12150. {
  12151. myerror("test_bug29692 init failed");
  12152. exit(1);
  12153. }
  12154. if (!(mysql_real_connect(conn, opt_host, opt_user,
  12155. opt_password, opt_db ? opt_db:"test", opt_port,
  12156. opt_unix_socket, CLIENT_FOUND_ROWS)))
  12157. {
  12158. myerror("test_bug29692 connection failed");
  12159. mysql_close(mysql);
  12160. exit(1);
  12161. }
  12162. myquery(mysql_query(conn, "drop table if exists t1"));
  12163. myquery(mysql_query(conn, "create table t1(f1 int)"));
  12164. myquery(mysql_query(conn, "insert into t1 values(1)"));
  12165. DIE_UNLESS(1 == mysql_affected_rows(conn));
  12166. myquery(mysql_query(conn, "drop table t1"));
  12167. mysql_close(conn);
  12168. }
  12169. /**
  12170. Bug#29306 Truncated data in MS Access with decimal (3,1) columns in a VIEW
  12171. */
  12172. static void test_bug29306()
  12173. {
  12174. MYSQL_FIELD *field;
  12175. int rc;
  12176. MYSQL_RES *res;
  12177. DBUG_ENTER("test_bug29306");
  12178. myheader("test_bug29306");
  12179. rc= mysql_query(mysql, "DROP TABLE IF EXISTS tab17557");
  12180. myquery(rc);
  12181. rc= mysql_query(mysql, "DROP VIEW IF EXISTS view17557");
  12182. myquery(rc);
  12183. rc= mysql_query(mysql, "CREATE TABLE tab17557 (dd decimal (3,1))");
  12184. myquery(rc);
  12185. rc= mysql_query(mysql, "CREATE VIEW view17557 as SELECT dd FROM tab17557");
  12186. myquery(rc);
  12187. rc= mysql_query(mysql, "INSERT INTO tab17557 VALUES (7.6)");
  12188. myquery(rc);
  12189. /* Checking the view */
  12190. res= mysql_list_fields(mysql, "view17557", NULL);
  12191. while ((field= mysql_fetch_field(res)))
  12192. {
  12193. if (! opt_silent)
  12194. {
  12195. printf("field name %s\n", field->name);
  12196. printf("field table %s\n", field->table);
  12197. printf("field decimals %d\n", field->decimals);
  12198. if (field->decimals < 1)
  12199. printf("Error! No decimals! \n");
  12200. printf("\n\n");
  12201. }
  12202. DIE_UNLESS(field->decimals == 1);
  12203. }
  12204. mysql_free_result(res);
  12205. rc= mysql_query(mysql, "DROP TABLE tab17557");
  12206. myquery(rc);
  12207. rc= mysql_query(mysql, "DROP VIEW view17557");
  12208. myquery(rc);
  12209. DBUG_VOID_RETURN;
  12210. }
  12211. /*
  12212. Bug#30472: libmysql doesn't reset charset, insert_id after succ.
  12213. mysql_change_user() call row insertions.
  12214. */
  12215. static void bug30472_retrieve_charset_info(MYSQL *con,
  12216. char *character_set_name,
  12217. char *character_set_client,
  12218. char *character_set_results,
  12219. char *collation_connection)
  12220. {
  12221. MYSQL_RES *rs;
  12222. MYSQL_ROW row;
  12223. /* Get the cached client character set name. */
  12224. strcpy(character_set_name, mysql_character_set_name(con));
  12225. /* Retrieve server character set information. */
  12226. DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'character_set_client'"));
  12227. DIE_UNLESS(rs= mysql_store_result(con));
  12228. DIE_UNLESS(row= mysql_fetch_row(rs));
  12229. strcpy(character_set_client, row[1]);
  12230. mysql_free_result(rs);
  12231. DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'character_set_results'"));
  12232. DIE_UNLESS(rs= mysql_store_result(con));
  12233. DIE_UNLESS(row= mysql_fetch_row(rs));
  12234. strcpy(character_set_results, row[1]);
  12235. mysql_free_result(rs);
  12236. DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'collation_connection'"));
  12237. DIE_UNLESS(rs= mysql_store_result(con));
  12238. DIE_UNLESS(row= mysql_fetch_row(rs));
  12239. strcpy(collation_connection, row[1]);
  12240. mysql_free_result(rs);
  12241. }
  12242. static void test_bug30472()
  12243. {
  12244. MYSQL con;
  12245. char character_set_name_1[MY_CS_NAME_SIZE];
  12246. char character_set_client_1[MY_CS_NAME_SIZE];
  12247. char character_set_results_1[MY_CS_NAME_SIZE];
  12248. char collation_connnection_1[MY_CS_NAME_SIZE];
  12249. char character_set_name_2[MY_CS_NAME_SIZE];
  12250. char character_set_client_2[MY_CS_NAME_SIZE];
  12251. char character_set_results_2[MY_CS_NAME_SIZE];
  12252. char collation_connnection_2[MY_CS_NAME_SIZE];
  12253. char character_set_name_3[MY_CS_NAME_SIZE];
  12254. char character_set_client_3[MY_CS_NAME_SIZE];
  12255. char character_set_results_3[MY_CS_NAME_SIZE];
  12256. char collation_connnection_3[MY_CS_NAME_SIZE];
  12257. char character_set_name_4[MY_CS_NAME_SIZE];
  12258. char character_set_client_4[MY_CS_NAME_SIZE];
  12259. char character_set_results_4[MY_CS_NAME_SIZE];
  12260. char collation_connnection_4[MY_CS_NAME_SIZE];
  12261. /* Create a new connection. */
  12262. DIE_UNLESS(mysql_client_init(&con));
  12263. DIE_UNLESS(mysql_real_connect(&con,
  12264. opt_host,
  12265. opt_user,
  12266. opt_password,
  12267. opt_db ? opt_db : "test",
  12268. opt_port,
  12269. opt_unix_socket,
  12270. CLIENT_FOUND_ROWS));
  12271. /* Retrieve character set information. */
  12272. bug30472_retrieve_charset_info(&con,
  12273. character_set_name_1,
  12274. character_set_client_1,
  12275. character_set_results_1,
  12276. collation_connnection_1);
  12277. /* Switch client character set. */
  12278. DIE_IF(mysql_set_character_set(&con, "utf8"));
  12279. /* Retrieve character set information. */
  12280. bug30472_retrieve_charset_info(&con,
  12281. character_set_name_2,
  12282. character_set_client_2,
  12283. character_set_results_2,
  12284. collation_connnection_2);
  12285. /*
  12286. Check that
  12287. 1) character set has been switched and
  12288. 2) new character set is different from the original one.
  12289. */
  12290. DIE_UNLESS(strcmp(character_set_name_2, "utf8") == 0);
  12291. DIE_UNLESS(strcmp(character_set_client_2, "utf8") == 0);
  12292. DIE_UNLESS(strcmp(character_set_results_2, "utf8") == 0);
  12293. DIE_UNLESS(strcmp(collation_connnection_2, "utf8_general_ci") == 0);
  12294. DIE_UNLESS(strcmp(character_set_name_1, character_set_name_2) != 0);
  12295. DIE_UNLESS(strcmp(character_set_client_1, character_set_client_2) != 0);
  12296. DIE_UNLESS(strcmp(character_set_results_1, character_set_results_2) != 0);
  12297. DIE_UNLESS(strcmp(collation_connnection_1, collation_connnection_2) != 0);
  12298. /* Call mysql_change_user() with the same username, password, database. */
  12299. DIE_IF(mysql_change_user(&con,
  12300. opt_user,
  12301. opt_password,
  12302. opt_db ? opt_db : "test"));
  12303. /* Retrieve character set information. */
  12304. bug30472_retrieve_charset_info(&con,
  12305. character_set_name_3,
  12306. character_set_client_3,
  12307. character_set_results_3,
  12308. collation_connnection_3);
  12309. /* Check that character set information has been reset. */
  12310. DIE_UNLESS(strcmp(character_set_name_1, character_set_name_3) == 0);
  12311. DIE_UNLESS(strcmp(character_set_client_1, character_set_client_3) == 0);
  12312. DIE_UNLESS(strcmp(character_set_results_1, character_set_results_3) == 0);
  12313. DIE_UNLESS(strcmp(collation_connnection_1, collation_connnection_3) == 0);
  12314. /* Change connection-default character set in the client. */
  12315. mysql_options(&con, MYSQL_SET_CHARSET_NAME, "utf8");
  12316. /*
  12317. Call mysql_change_user() in order to check that new connection will
  12318. have UTF8 character set on the client and on the server.
  12319. */
  12320. DIE_IF(mysql_change_user(&con,
  12321. opt_user,
  12322. opt_password,
  12323. opt_db ? opt_db : "test"));
  12324. /* Retrieve character set information. */
  12325. bug30472_retrieve_charset_info(&con,
  12326. character_set_name_4,
  12327. character_set_client_4,
  12328. character_set_results_4,
  12329. collation_connnection_4);
  12330. /* Check that we have UTF8 on the server and on the client. */
  12331. DIE_UNLESS(strcmp(character_set_name_4, "utf8") == 0);
  12332. DIE_UNLESS(strcmp(character_set_client_4, "utf8") == 0);
  12333. DIE_UNLESS(strcmp(character_set_results_4, "utf8") == 0);
  12334. DIE_UNLESS(strcmp(collation_connnection_4, "utf8_general_ci") == 0);
  12335. /* That's it. Cleanup. */
  12336. mysql_close(&con);
  12337. }
  12338. static void bug20023_change_user(MYSQL *con)
  12339. {
  12340. DIE_IF(mysql_change_user(con,
  12341. opt_user,
  12342. opt_password,
  12343. opt_db ? opt_db : "test"));
  12344. }
  12345. static my_bool query_str_variable(MYSQL *con,
  12346. const char *var_name,
  12347. char *str,
  12348. size_t len)
  12349. {
  12350. MYSQL_RES *rs;
  12351. MYSQL_ROW row;
  12352. char query_buffer[MAX_TEST_QUERY_LENGTH];
  12353. my_bool is_null;
  12354. my_snprintf(query_buffer, sizeof (query_buffer),
  12355. "SELECT %s", var_name);
  12356. DIE_IF(mysql_query(con, query_buffer));
  12357. DIE_UNLESS(rs= mysql_store_result(con));
  12358. DIE_UNLESS(row= mysql_fetch_row(rs));
  12359. is_null= row[0] == NULL;
  12360. if (!is_null)
  12361. my_snprintf(str, len, "%s", row[0]);
  12362. mysql_free_result(rs);
  12363. return is_null;
  12364. }
  12365. static my_bool query_int_variable(MYSQL *con,
  12366. const char *var_name,
  12367. int *var_value)
  12368. {
  12369. char str[32];
  12370. my_bool is_null= query_str_variable(con, var_name, str, sizeof(str));
  12371. if (!is_null)
  12372. *var_value= atoi(str);
  12373. return is_null;
  12374. }
  12375. static void test_bug20023()
  12376. {
  12377. MYSQL con;
  12378. int sql_big_selects_orig= 0;
  12379. /*
  12380. Type of max_join_size is ha_rows, which might be ulong or off_t
  12381. depending on the platform or configure options. Preserve the string
  12382. to avoid type overflow pitfalls.
  12383. */
  12384. char max_join_size_orig[32];
  12385. int sql_big_selects_2= 0;
  12386. int sql_big_selects_3= 0;
  12387. int sql_big_selects_4= 0;
  12388. int sql_big_selects_5= 0;
  12389. char query_buffer[MAX_TEST_QUERY_LENGTH];
  12390. /* Create a new connection. */
  12391. DIE_UNLESS(mysql_client_init(&con));
  12392. DIE_UNLESS(mysql_real_connect(&con,
  12393. opt_host,
  12394. opt_user,
  12395. opt_password,
  12396. opt_db ? opt_db : "test",
  12397. opt_port,
  12398. opt_unix_socket,
  12399. CLIENT_FOUND_ROWS));
  12400. /***********************************************************************
  12401. Remember original SQL_BIG_SELECTS, MAX_JOIN_SIZE values.
  12402. ***********************************************************************/
  12403. query_int_variable(&con,
  12404. "@@session.sql_big_selects",
  12405. &sql_big_selects_orig);
  12406. query_str_variable(&con,
  12407. "@@global.max_join_size",
  12408. max_join_size_orig,
  12409. sizeof(max_join_size_orig));
  12410. /***********************************************************************
  12411. Test that COM_CHANGE_USER resets the SQL_BIG_SELECTS to the initial value.
  12412. ***********************************************************************/
  12413. /* Issue COM_CHANGE_USER. */
  12414. bug20023_change_user(&con);
  12415. /* Query SQL_BIG_SELECTS. */
  12416. query_int_variable(&con,
  12417. "@@session.sql_big_selects",
  12418. &sql_big_selects_2);
  12419. /* Check that SQL_BIG_SELECTS is reset properly. */
  12420. DIE_UNLESS(sql_big_selects_orig == sql_big_selects_2);
  12421. /***********************************************************************
  12422. Test that if MAX_JOIN_SIZE set to non-default value,
  12423. SQL_BIG_SELECTS will be 0.
  12424. ***********************************************************************/
  12425. /* Set MAX_JOIN_SIZE to some non-default value. */
  12426. DIE_IF(mysql_query(&con, "SET @@global.max_join_size = 10000"));
  12427. DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
  12428. /* Issue COM_CHANGE_USER. */
  12429. bug20023_change_user(&con);
  12430. /* Query SQL_BIG_SELECTS. */
  12431. query_int_variable(&con,
  12432. "@@session.sql_big_selects",
  12433. &sql_big_selects_3);
  12434. /* Check that SQL_BIG_SELECTS is 0. */
  12435. DIE_UNLESS(sql_big_selects_3 == 0);
  12436. /***********************************************************************
  12437. Test that if MAX_JOIN_SIZE set to default value,
  12438. SQL_BIG_SELECTS will be 1.
  12439. ***********************************************************************/
  12440. /* Set MAX_JOIN_SIZE to the default value (-1). */
  12441. DIE_IF(mysql_query(&con, "SET @@global.max_join_size = 18446744073709551615"));
  12442. DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
  12443. /* Issue COM_CHANGE_USER. */
  12444. bug20023_change_user(&con);
  12445. /* Query SQL_BIG_SELECTS. */
  12446. query_int_variable(&con,
  12447. "@@session.sql_big_selects",
  12448. &sql_big_selects_4);
  12449. /* Check that SQL_BIG_SELECTS is 1. */
  12450. DIE_UNLESS(sql_big_selects_4 == 1);
  12451. /***********************************************************************
  12452. Restore MAX_JOIN_SIZE.
  12453. Check that SQL_BIG_SELECTS will be the original one.
  12454. ***********************************************************************/
  12455. /* Restore MAX_JOIN_SIZE. */
  12456. my_snprintf(query_buffer,
  12457. sizeof (query_buffer),
  12458. "SET @@global.max_join_size = %s",
  12459. max_join_size_orig);
  12460. DIE_IF(mysql_query(&con, query_buffer));
  12461. DIE_IF(mysql_query(&con, "SET @@global.max_join_size = 18446744073709551615"));
  12462. DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
  12463. /* Issue COM_CHANGE_USER. */
  12464. bug20023_change_user(&con);
  12465. /* Query SQL_BIG_SELECTS. */
  12466. query_int_variable(&con,
  12467. "@@session.sql_big_selects",
  12468. &sql_big_selects_5);
  12469. /* Check that SQL_BIG_SELECTS is 1. */
  12470. DIE_UNLESS(sql_big_selects_5 == sql_big_selects_orig);
  12471. /***********************************************************************
  12472. That's it. Cleanup.
  12473. ***********************************************************************/
  12474. mysql_close(&con);
  12475. }
  12476. static void bug31418_impl()
  12477. {
  12478. MYSQL con;
  12479. my_bool is_null;
  12480. int rc= 0;
  12481. /* Create a new connection. */
  12482. DIE_UNLESS(mysql_client_init(&con));
  12483. DIE_UNLESS(mysql_real_connect(&con,
  12484. opt_host,
  12485. opt_user,
  12486. opt_password,
  12487. opt_db ? opt_db : "test",
  12488. opt_port,
  12489. opt_unix_socket,
  12490. CLIENT_FOUND_ROWS));
  12491. /***********************************************************************
  12492. Check that lock is free:
  12493. - IS_FREE_LOCK() should return 1;
  12494. - IS_USED_LOCK() should return NULL;
  12495. ***********************************************************************/
  12496. is_null= query_int_variable(&con,
  12497. "IS_FREE_LOCK('bug31418')",
  12498. &rc);
  12499. DIE_UNLESS(!is_null && rc);
  12500. is_null= query_int_variable(&con,
  12501. "IS_USED_LOCK('bug31418')",
  12502. &rc);
  12503. DIE_UNLESS(is_null);
  12504. /***********************************************************************
  12505. Acquire lock and check the lock status (the lock must be in use):
  12506. - IS_FREE_LOCK() should return 0;
  12507. - IS_USED_LOCK() should return non-zero thread id;
  12508. ***********************************************************************/
  12509. query_int_variable(&con, "GET_LOCK('bug31418', 1)", &rc);
  12510. DIE_UNLESS(rc);
  12511. is_null= query_int_variable(&con,
  12512. "IS_FREE_LOCK('bug31418')",
  12513. &rc);
  12514. DIE_UNLESS(!is_null && !rc);
  12515. is_null= query_int_variable(&con,
  12516. "IS_USED_LOCK('bug31418')",
  12517. &rc);
  12518. DIE_UNLESS(!is_null && rc);
  12519. /***********************************************************************
  12520. Issue COM_CHANGE_USER command and check the lock status
  12521. (the lock must be free):
  12522. - IS_FREE_LOCK() should return 1;
  12523. - IS_USED_LOCK() should return NULL;
  12524. **********************************************************************/
  12525. bug20023_change_user(&con);
  12526. is_null= query_int_variable(&con,
  12527. "IS_FREE_LOCK('bug31418')",
  12528. &rc);
  12529. DIE_UNLESS(!is_null && rc);
  12530. is_null= query_int_variable(&con,
  12531. "IS_USED_LOCK('bug31418')",
  12532. &rc);
  12533. DIE_UNLESS(is_null);
  12534. /***********************************************************************
  12535. That's it. Cleanup.
  12536. ***********************************************************************/
  12537. mysql_close(&con);
  12538. }
  12539. static void test_bug31418()
  12540. {
  12541. /* Run test case for BUG#31418 for three different connections. */
  12542. bug31418_impl();
  12543. bug31418_impl();
  12544. bug31418_impl();
  12545. }
  12546. /**
  12547. Bug#31669 Buffer overflow in mysql_change_user()
  12548. */
  12549. #define LARGE_BUFFER_SIZE 2048
  12550. static void test_bug31669()
  12551. {
  12552. int rc;
  12553. static char buff[LARGE_BUFFER_SIZE+1];
  12554. #ifndef EMBEDDED_LIBRARY
  12555. static char user[USERNAME_CHAR_LENGTH+1];
  12556. static char db[NAME_CHAR_LEN+1];
  12557. static char query[LARGE_BUFFER_SIZE*2];
  12558. #endif
  12559. DBUG_ENTER("test_bug31669");
  12560. myheader("test_bug31669");
  12561. rc= mysql_change_user(mysql, NULL, NULL, NULL);
  12562. DIE_UNLESS(rc);
  12563. rc= mysql_change_user(mysql, "", "", "");
  12564. DIE_UNLESS(rc);
  12565. memset(buff, 'a', sizeof(buff));
  12566. rc= mysql_change_user(mysql, buff, buff, buff);
  12567. DIE_UNLESS(rc);
  12568. rc = mysql_change_user(mysql, opt_user, opt_password, current_db);
  12569. DIE_UNLESS(!rc);
  12570. #ifndef EMBEDDED_LIBRARY
  12571. memset(db, 'a', sizeof(db));
  12572. db[NAME_CHAR_LEN]= 0;
  12573. strxmov(query, "CREATE DATABASE IF NOT EXISTS ", db, NullS);
  12574. rc= mysql_query(mysql, query);
  12575. myquery(rc);
  12576. memset(user, 'b', sizeof(user));
  12577. user[USERNAME_CHAR_LENGTH]= 0;
  12578. memset(buff, 'c', sizeof(buff));
  12579. buff[LARGE_BUFFER_SIZE]= 0;
  12580. strxmov(query, "GRANT ALL PRIVILEGES ON *.* TO '", user, "'@'%' IDENTIFIED BY "
  12581. "'", buff, "' WITH GRANT OPTION", NullS);
  12582. rc= mysql_query(mysql, query);
  12583. myquery(rc);
  12584. strxmov(query, "GRANT ALL PRIVILEGES ON *.* TO '", user, "'@'localhost' IDENTIFIED BY "
  12585. "'", buff, "' WITH GRANT OPTION", NullS);
  12586. rc= mysql_query(mysql, query);
  12587. myquery(rc);
  12588. rc= mysql_query(mysql, "FLUSH PRIVILEGES");
  12589. myquery(rc);
  12590. rc= mysql_change_user(mysql, user, buff, db);
  12591. DIE_UNLESS(!rc);
  12592. user[USERNAME_CHAR_LENGTH-1]= 'a';
  12593. rc= mysql_change_user(mysql, user, buff, db);
  12594. DIE_UNLESS(rc);
  12595. user[USERNAME_CHAR_LENGTH-1]= 'b';
  12596. buff[LARGE_BUFFER_SIZE-1]= 'd';
  12597. rc= mysql_change_user(mysql, user, buff, db);
  12598. DIE_UNLESS(rc);
  12599. buff[LARGE_BUFFER_SIZE-1]= 'c';
  12600. db[NAME_CHAR_LEN-1]= 'e';
  12601. rc= mysql_change_user(mysql, user, buff, db);
  12602. DIE_UNLESS(rc);
  12603. db[NAME_CHAR_LEN-1]= 'a';
  12604. rc= mysql_change_user(mysql, user, buff, db);
  12605. DIE_UNLESS(!rc);
  12606. rc= mysql_change_user(mysql, user + 1, buff + 1, db + 1);
  12607. DIE_UNLESS(rc);
  12608. rc = mysql_change_user(mysql, opt_user, opt_password, current_db);
  12609. DIE_UNLESS(!rc);
  12610. strxmov(query, "DROP DATABASE ", db, NullS);
  12611. rc= mysql_query(mysql, query);
  12612. myquery(rc);
  12613. strxmov(query, "DELETE FROM mysql.user WHERE User='", user, "'", NullS);
  12614. rc= mysql_query(mysql, query);
  12615. myquery(rc);
  12616. DIE_UNLESS(mysql_affected_rows(mysql) == 2);
  12617. #endif
  12618. DBUG_VOID_RETURN;
  12619. }
  12620. /**
  12621. Bug#28386 the general log is incomplete
  12622. */
  12623. static void test_bug28386()
  12624. {
  12625. int rc;
  12626. MYSQL_STMT *stmt;
  12627. MYSQL_RES *result;
  12628. MYSQL_ROW row;
  12629. MYSQL_BIND bind;
  12630. const char hello[]= "hello world!";
  12631. DBUG_ENTER("test_bug28386");
  12632. myheader("test_bug28386");
  12633. rc= mysql_query(mysql, "select @@global.log_output");
  12634. myquery(rc);
  12635. result= mysql_store_result(mysql);
  12636. DIE_UNLESS(result);
  12637. row= mysql_fetch_row(result);
  12638. if (! strstr(row[0], "TABLE"))
  12639. {
  12640. mysql_free_result(result);
  12641. if (! opt_silent)
  12642. printf("Skipping the test since logging to tables is not enabled\n");
  12643. /* Log output is not to tables */
  12644. return;
  12645. }
  12646. mysql_free_result(result);
  12647. enable_query_logs(1);
  12648. stmt= mysql_simple_prepare(mysql, "SELECT ?");
  12649. check_stmt(stmt);
  12650. memset(&bind, 0, sizeof(bind));
  12651. bind.buffer_type= MYSQL_TYPE_STRING;
  12652. bind.buffer= (void *) hello;
  12653. bind.buffer_length= sizeof(hello);
  12654. mysql_stmt_bind_param(stmt, &bind);
  12655. mysql_stmt_send_long_data(stmt, 0, hello, sizeof(hello));
  12656. rc= mysql_stmt_execute(stmt);
  12657. check_execute(stmt, rc);
  12658. rc= my_process_stmt_result(stmt);
  12659. DIE_UNLESS(rc == 1);
  12660. rc= mysql_stmt_reset(stmt);
  12661. check_execute(stmt, rc);
  12662. rc= mysql_stmt_close(stmt);
  12663. DIE_UNLESS(!rc);
  12664. rc= mysql_query(mysql, "select * from mysql.general_log where "
  12665. "command_type='Close stmt' or "
  12666. "command_type='Reset stmt' or "
  12667. "command_type='Long Data'");
  12668. myquery(rc);
  12669. result= mysql_store_result(mysql);
  12670. mytest(result);
  12671. DIE_UNLESS(mysql_num_rows(result) == 3);
  12672. mysql_free_result(result);
  12673. restore_query_logs();
  12674. DBUG_VOID_RETURN;
  12675. }
  12676. static void test_wl4166_1()
  12677. {
  12678. MYSQL_STMT *stmt;
  12679. int int_data;
  12680. char str_data[50];
  12681. char tiny_data;
  12682. short small_data;
  12683. longlong big_data;
  12684. float real_data;
  12685. double double_data;
  12686. ulong length[7];
  12687. my_bool is_null[7];
  12688. MYSQL_BIND my_bind[7];
  12689. int rc;
  12690. int i;
  12691. myheader("test_wl4166_1");
  12692. rc= mysql_query(mysql, "DROP TABLE IF EXISTS table_4166");
  12693. myquery(rc);
  12694. rc= mysql_query(mysql, "CREATE TABLE table_4166(col1 tinyint NOT NULL, "
  12695. "col2 varchar(15), col3 int, "
  12696. "col4 smallint, col5 bigint, "
  12697. "col6 float, col7 double, "
  12698. "colX varchar(10) default NULL)");
  12699. myquery(rc);
  12700. stmt= mysql_simple_prepare(mysql,
  12701. "INSERT INTO table_4166(col1, col2, col3, col4, col5, col6, col7) "
  12702. "VALUES(?, ?, ?, ?, ?, ?, ?)");
  12703. check_stmt(stmt);
  12704. verify_param_count(stmt, 7);
  12705. bzero(my_bind, sizeof(my_bind));
  12706. /* tinyint */
  12707. my_bind[0].buffer_type= MYSQL_TYPE_TINY;
  12708. my_bind[0].buffer= (void *)&tiny_data;
  12709. /* string */
  12710. my_bind[1].buffer_type= MYSQL_TYPE_STRING;
  12711. my_bind[1].buffer= (void *)str_data;
  12712. my_bind[1].buffer_length= 1000; /* Max string length */
  12713. /* integer */
  12714. my_bind[2].buffer_type= MYSQL_TYPE_LONG;
  12715. my_bind[2].buffer= (void *)&int_data;
  12716. /* short */
  12717. my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
  12718. my_bind[3].buffer= (void *)&small_data;
  12719. /* bigint */
  12720. my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
  12721. my_bind[4].buffer= (void *)&big_data;
  12722. /* float */
  12723. my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
  12724. my_bind[5].buffer= (void *)&real_data;
  12725. /* double */
  12726. my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
  12727. my_bind[6].buffer= (void *)&double_data;
  12728. for (i= 0; i < (int) array_elements(my_bind); i++)
  12729. {
  12730. my_bind[i].length= &length[i];
  12731. my_bind[i].is_null= &is_null[i];
  12732. is_null[i]= 0;
  12733. }
  12734. rc= mysql_stmt_bind_param(stmt, my_bind);
  12735. check_execute(stmt, rc);
  12736. int_data= 320;
  12737. small_data= 1867;
  12738. big_data= 1000;
  12739. real_data= 2;
  12740. double_data= 6578.001;
  12741. /* now, execute the prepared statement to insert 10 records.. */
  12742. for (tiny_data= 0; tiny_data < 10; tiny_data++)
  12743. {
  12744. length[1]= sprintf(str_data, "MySQL%d", int_data);
  12745. rc= mysql_stmt_execute(stmt);
  12746. check_execute(stmt, rc);
  12747. int_data += 25;
  12748. small_data += 10;
  12749. big_data += 100;
  12750. real_data += 1;
  12751. double_data += 10.09;
  12752. }
  12753. /* force a re-prepare with some DDL */
  12754. rc= mysql_query(mysql,
  12755. "ALTER TABLE table_4166 change colX colX varchar(20) default NULL");
  12756. myquery(rc);
  12757. /*
  12758. execute the prepared statement again,
  12759. without changing the types of parameters already bound.
  12760. */
  12761. for (tiny_data= 50; tiny_data < 60; tiny_data++)
  12762. {
  12763. length[1]= sprintf(str_data, "MySQL%d", int_data);
  12764. rc= mysql_stmt_execute(stmt);
  12765. check_execute(stmt, rc);
  12766. int_data += 25;
  12767. small_data += 10;
  12768. big_data += 100;
  12769. real_data += 1;
  12770. double_data += 10.09;
  12771. }
  12772. mysql_stmt_close(stmt);
  12773. rc= mysql_query(mysql, "DROP TABLE table_4166");
  12774. myquery(rc);
  12775. }
  12776. static void test_wl4166_2()
  12777. {
  12778. MYSQL_STMT *stmt;
  12779. int c_int;
  12780. MYSQL_TIME d_date;
  12781. MYSQL_BIND bind_out[2];
  12782. int rc;
  12783. myheader("test_wl4166_2");
  12784. rc= mysql_query(mysql, "SET SQL_MODE=''");
  12785. myquery(rc);
  12786. rc= mysql_query(mysql, "drop table if exists t1");
  12787. myquery(rc);
  12788. rc= mysql_query(mysql, "create table t1 (c_int int, d_date date)");
  12789. myquery(rc);
  12790. rc= mysql_query(mysql,
  12791. "insert into t1 (c_int, d_date) values (42, '1948-05-15')");
  12792. myquery(rc);
  12793. stmt= mysql_simple_prepare(mysql, "select * from t1");
  12794. check_stmt(stmt);
  12795. bzero(bind_out, sizeof(bind_out));
  12796. bind_out[0].buffer_type= MYSQL_TYPE_LONG;
  12797. bind_out[0].buffer= (void*) &c_int;
  12798. bind_out[1].buffer_type= MYSQL_TYPE_DATE;
  12799. bind_out[1].buffer= (void*) &d_date;
  12800. rc= mysql_stmt_bind_result(stmt, bind_out);
  12801. check_execute(stmt, rc);
  12802. /* int -> varchar transition */
  12803. rc= mysql_query(mysql,
  12804. "alter table t1 change column c_int c_int varchar(11)");
  12805. myquery(rc);
  12806. rc= mysql_stmt_execute(stmt);
  12807. check_execute(stmt, rc);
  12808. rc= mysql_stmt_fetch(stmt);
  12809. check_execute(stmt, rc);
  12810. DIE_UNLESS(c_int == 42);
  12811. DIE_UNLESS(d_date.year == 1948);
  12812. DIE_UNLESS(d_date.month == 5);
  12813. DIE_UNLESS(d_date.day == 15);
  12814. rc= mysql_stmt_fetch(stmt);
  12815. DIE_UNLESS(rc == MYSQL_NO_DATA);
  12816. /* varchar to int retrieval with truncation */
  12817. rc= mysql_query(mysql, "update t1 set c_int='abcde'");
  12818. myquery(rc);
  12819. rc= mysql_stmt_execute(stmt);
  12820. check_execute(stmt, rc);
  12821. rc= mysql_stmt_fetch(stmt);
  12822. check_execute_r(stmt, rc);
  12823. DIE_UNLESS(c_int == 0);
  12824. rc= mysql_stmt_fetch(stmt);
  12825. DIE_UNLESS(rc == MYSQL_NO_DATA);
  12826. /* alter table and increase the number of columns */
  12827. rc= mysql_query(mysql, "alter table t1 add column d_int int");
  12828. myquery(rc);
  12829. rc= mysql_stmt_execute(stmt);
  12830. check_execute_r(stmt, rc);
  12831. rc= mysql_stmt_reset(stmt);
  12832. check_execute(stmt, rc);
  12833. /* decrease the number of columns */
  12834. rc= mysql_query(mysql, "alter table t1 drop d_date, drop d_int");
  12835. myquery(rc);
  12836. rc= mysql_stmt_execute(stmt);
  12837. check_execute_r(stmt, rc);
  12838. mysql_stmt_close(stmt);
  12839. rc= mysql_query(mysql, "drop table t1");
  12840. myquery(rc);
  12841. }
  12842. /**
  12843. Bug#38486 Crash when using cursor protocol
  12844. */
  12845. static void test_bug38486(void)
  12846. {
  12847. MYSQL_STMT *stmt;
  12848. const char *stmt_text;
  12849. unsigned long type= CURSOR_TYPE_READ_ONLY;
  12850. DBUG_ENTER("test_bug38486");
  12851. myheader("test_bug38486");
  12852. stmt= mysql_stmt_init(mysql);
  12853. mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*)&type);
  12854. stmt_text= "CREATE TABLE t1 (a INT)";
  12855. mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  12856. mysql_stmt_execute(stmt);
  12857. mysql_stmt_close(stmt);
  12858. stmt= mysql_stmt_init(mysql);
  12859. mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*)&type);
  12860. stmt_text= "INSERT INTO t1 VALUES (1)";
  12861. mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
  12862. mysql_stmt_execute(stmt);
  12863. mysql_stmt_close(stmt);
  12864. DBUG_VOID_RETURN;
  12865. }
  12866. static void test_bug40365(void)
  12867. {
  12868. uint rc, i;
  12869. MYSQL_STMT *stmt= 0;
  12870. MYSQL_BIND my_bind[2];
  12871. my_bool is_null[2]= {0};
  12872. MYSQL_TIME tm[2];
  12873. DBUG_ENTER("test_bug40365");
  12874. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  12875. myquery(rc);
  12876. rc= mysql_query(mysql, "CREATE TABLE t1(c1 DATETIME, \
  12877. c2 DATE)");
  12878. myquery(rc);
  12879. stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 VALUES(?, ?)");
  12880. check_stmt(stmt);
  12881. verify_param_count(stmt, 2);
  12882. bzero((char*) my_bind, sizeof(my_bind));
  12883. my_bind[0].buffer_type= MYSQL_TYPE_DATETIME;
  12884. my_bind[1].buffer_type= MYSQL_TYPE_DATE;
  12885. for (i= 0; i < (int) array_elements(my_bind); i++)
  12886. {
  12887. my_bind[i].buffer= (void *) &tm[i];
  12888. my_bind[i].is_null= &is_null[i];
  12889. }
  12890. rc= mysql_stmt_bind_param(stmt, my_bind);
  12891. check_execute(stmt, rc);
  12892. for (i= 0; i < (int) array_elements(my_bind); i++)
  12893. {
  12894. tm[i].neg= 0;
  12895. tm[i].second_part= 0;
  12896. tm[i].year= 2009;
  12897. tm[i].month= 2;
  12898. tm[i].day= 29;
  12899. tm[i].hour= 0;
  12900. tm[i].minute= 0;
  12901. tm[i].second= 0;
  12902. }
  12903. rc= mysql_stmt_execute(stmt);
  12904. check_execute(stmt, rc);
  12905. rc= mysql_commit(mysql);
  12906. myquery(rc);
  12907. mysql_stmt_close(stmt);
  12908. stmt= mysql_simple_prepare(mysql, "SELECT * FROM t1");
  12909. check_stmt(stmt);
  12910. rc= mysql_stmt_bind_result(stmt, my_bind);
  12911. check_execute(stmt, rc);
  12912. rc= mysql_stmt_execute(stmt);
  12913. check_execute(stmt, rc);
  12914. rc= mysql_stmt_store_result(stmt);
  12915. check_execute(stmt, rc);
  12916. rc= mysql_stmt_fetch(stmt);
  12917. check_execute(stmt, rc);
  12918. if (!opt_silent)
  12919. fprintf(stdout, "\n");
  12920. for (i= 0; i < array_elements(my_bind); i++)
  12921. {
  12922. if (!opt_silent)
  12923. fprintf(stdout, "\ntime[%d]: %02d-%02d-%02d ",
  12924. i, tm[i].year, tm[i].month, tm[i].day);
  12925. DIE_UNLESS(tm[i].year == 0);
  12926. DIE_UNLESS(tm[i].month == 0);
  12927. DIE_UNLESS(tm[i].day == 0);
  12928. }
  12929. mysql_stmt_close(stmt);
  12930. DBUG_VOID_RETURN;
  12931. }
  12932. /**
  12933. Subtest for Bug#43560. Verifies that a loss of connection on the server side
  12934. is handled well by the mysql_stmt_execute() call, i.e., no SIGSEGV due to
  12935. a vio socket that is cleared upon closed connection.
  12936. Assumes the presence of the close_conn_after_stmt_execute debug feature in
  12937. the server. Verifies that it is connected to a debug server before proceeding
  12938. with the test.
  12939. */
  12940. static void test_bug43560(void)
  12941. {
  12942. MYSQL* conn;
  12943. uint rc;
  12944. MYSQL_STMT *stmt= 0;
  12945. MYSQL_BIND bind;
  12946. my_bool is_null= 0;
  12947. char buffer[256];
  12948. const uint BUFSIZE= sizeof(buffer);
  12949. const char* values[] = {"eins", "zwei", "drei", "viele", NULL};
  12950. const char insert_str[] = "INSERT INTO t1 (c2) VALUES (?)";
  12951. unsigned long length;
  12952. DBUG_ENTER("test_bug43560");
  12953. myheader("test_bug43560");
  12954. /* Make sure we only run against a debug server. */
  12955. if (!strstr(mysql->server_version, "debug"))
  12956. {
  12957. fprintf(stdout, "Skipping test_bug43560: server not DEBUG version\n");
  12958. DBUG_VOID_RETURN;
  12959. }
  12960. /*
  12961. Set up a separate connection for this test to avoid messing up the
  12962. general MYSQL object used in other subtests. Use TCP protocol to avoid
  12963. problems with the buffer semantics of AF_UNIX, and turn off auto reconnect.
  12964. */
  12965. conn= client_connect(0, MYSQL_PROTOCOL_TCP, 0);
  12966. rc= mysql_query(conn, "DROP TABLE IF EXISTS t1");
  12967. myquery(rc);
  12968. rc= mysql_query(conn,
  12969. "CREATE TABLE t1 (c1 INT PRIMARY KEY AUTO_INCREMENT, c2 CHAR(10))");
  12970. myquery(rc);
  12971. stmt= mysql_stmt_init(conn);
  12972. check_stmt(stmt);
  12973. rc= mysql_stmt_prepare(stmt, insert_str, strlen(insert_str));
  12974. check_execute(stmt, rc);
  12975. bind.buffer_type= MYSQL_TYPE_STRING;
  12976. bind.buffer_length= BUFSIZE;
  12977. bind.buffer= buffer;
  12978. bind.is_null= &is_null;
  12979. bind.length= &length;
  12980. rc= mysql_stmt_bind_param(stmt, &bind);
  12981. check_execute(stmt, rc);
  12982. /* First execute; should succeed. */
  12983. strncpy(buffer, values[0], BUFSIZE);
  12984. length= strlen(buffer);
  12985. rc= mysql_stmt_execute(stmt);
  12986. check_execute(stmt, rc);
  12987. /*
  12988. Set up the server to close this session's server-side socket after
  12989. next execution of prep statement.
  12990. */
  12991. rc= mysql_query(conn,"SET SESSION debug='+d,close_conn_after_stmt_execute'");
  12992. myquery(rc);
  12993. /* Second execute; should fail due to socket closed during execution. */
  12994. strncpy(buffer, values[1], BUFSIZE);
  12995. length= strlen(buffer);
  12996. rc= mysql_stmt_execute(stmt);
  12997. DIE_UNLESS(rc && mysql_stmt_errno(stmt) == CR_SERVER_LOST);
  12998. /*
  12999. Third execute; should fail (connection already closed), or SIGSEGV in
  13000. case of a Bug#43560 type regression in which case the whole test fails.
  13001. */
  13002. strncpy(buffer, values[2], BUFSIZE);
  13003. length= strlen(buffer);
  13004. rc= mysql_stmt_execute(stmt);
  13005. DIE_UNLESS(rc && mysql_stmt_errno(stmt) == CR_SERVER_LOST);
  13006. client_disconnect(conn, 0);
  13007. rc= mysql_query(mysql, "DROP TABLE t1");
  13008. myquery(rc);
  13009. DBUG_VOID_RETURN;
  13010. }
  13011. /**
  13012. Bug#36326: nested transaction and select
  13013. */
  13014. #ifdef HAVE_QUERY_CACHE
  13015. static void test_bug36326()
  13016. {
  13017. int rc;
  13018. DBUG_ENTER("test_bug36326");
  13019. myheader("test_bug36326");
  13020. rc= mysql_autocommit(mysql, TRUE);
  13021. myquery(rc);
  13022. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  13023. myquery(rc);
  13024. rc= mysql_query(mysql, "CREATE TABLE t1 (a INTEGER)");
  13025. myquery(rc);
  13026. rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
  13027. myquery(rc);
  13028. rc= mysql_query(mysql, "SET GLOBAL query_cache_type = 1");
  13029. myquery(rc);
  13030. rc= mysql_query(mysql, "SET GLOBAL query_cache_size = 1048576");
  13031. myquery(rc);
  13032. DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
  13033. DIE_UNLESS(mysql->server_status & SERVER_STATUS_AUTOCOMMIT);
  13034. rc= mysql_query(mysql, "BEGIN");
  13035. myquery(rc);
  13036. DIE_UNLESS(mysql->server_status & SERVER_STATUS_IN_TRANS);
  13037. rc= mysql_query(mysql, "SELECT * FROM t1");
  13038. myquery(rc);
  13039. rc= my_process_result(mysql);
  13040. DIE_UNLESS(rc == 1);
  13041. rc= mysql_rollback(mysql);
  13042. myquery(rc);
  13043. rc= mysql_query(mysql, "ROLLBACK");
  13044. myquery(rc);
  13045. DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
  13046. rc= mysql_query(mysql, "SELECT * FROM t1");
  13047. myquery(rc);
  13048. DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
  13049. rc= my_process_result(mysql);
  13050. DIE_UNLESS(rc == 1);
  13051. rc= mysql_query(mysql, "DROP TABLE t1");
  13052. myquery(rc);
  13053. rc= mysql_query(mysql, "SET GLOBAL query_cache_size = 0");
  13054. myquery(rc);
  13055. DBUG_VOID_RETURN;
  13056. }
  13057. #endif
  13058. /**
  13059. Bug#41078: With CURSOR_TYPE_READ_ONLY mysql_stmt_fetch() returns short
  13060. string value.
  13061. */
  13062. static void test_bug41078(void)
  13063. {
  13064. uint rc;
  13065. MYSQL_STMT *stmt= 0;
  13066. MYSQL_BIND param, result;
  13067. ulong cursor_type= CURSOR_TYPE_READ_ONLY;
  13068. ulong len;
  13069. char str[64];
  13070. const char param_str[]= "abcdefghijklmn";
  13071. my_bool is_null, error;
  13072. DBUG_ENTER("test_bug41078");
  13073. rc= mysql_query(mysql, "SET NAMES UTF8");
  13074. myquery(rc);
  13075. stmt= mysql_simple_prepare(mysql, "SELECT ?");
  13076. check_stmt(stmt);
  13077. verify_param_count(stmt, 1);
  13078. rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, &cursor_type);
  13079. check_execute(stmt, rc);
  13080. bzero(&param, sizeof(param));
  13081. param.buffer_type= MYSQL_TYPE_STRING;
  13082. param.buffer= (void *) param_str;
  13083. len= sizeof(param_str) - 1;
  13084. param.length= &len;
  13085. rc= mysql_stmt_bind_param(stmt, &param);
  13086. check_execute(stmt, rc);
  13087. rc= mysql_stmt_execute(stmt);
  13088. check_execute(stmt, rc);
  13089. bzero(&result, sizeof(result));
  13090. result.buffer_type= MYSQL_TYPE_STRING;
  13091. result.buffer= str;
  13092. result.buffer_length= sizeof(str);
  13093. result.is_null= &is_null;
  13094. result.length= &len;
  13095. result.error= &error;
  13096. rc= mysql_stmt_bind_result(stmt, &result);
  13097. check_execute(stmt, rc);
  13098. rc= mysql_stmt_store_result(stmt);
  13099. check_execute(stmt, rc);
  13100. rc= mysql_stmt_fetch(stmt);
  13101. check_execute(stmt, rc);
  13102. DIE_UNLESS(len == sizeof(param_str) - 1 && !strcmp(str, param_str));
  13103. mysql_stmt_close(stmt);
  13104. DBUG_VOID_RETURN;
  13105. }
  13106. /**
  13107. Bug#45010: invalid memory reads during parsing some strange statements
  13108. */
  13109. static void test_bug45010()
  13110. {
  13111. int rc;
  13112. const char query1[]= "select a.\x80",
  13113. query2[]= "describe `table\xef";
  13114. DBUG_ENTER("test_bug45010");
  13115. myheader("test_bug45010");
  13116. rc= mysql_query(mysql, "set names utf8");
  13117. myquery(rc);
  13118. /* \x80 (-128) could be used as a index of ident_map. */
  13119. rc= mysql_real_query(mysql, query1, sizeof(query1) - 1);
  13120. DIE_UNLESS(rc);
  13121. /* \xef (-17) could be used to skip 3 bytes past the buffer end. */
  13122. rc= mysql_real_query(mysql, query2, sizeof(query2) - 1);
  13123. DIE_UNLESS(rc);
  13124. rc= mysql_query(mysql, "set names default");
  13125. myquery(rc);
  13126. DBUG_VOID_RETURN;
  13127. }
  13128. /**
  13129. Bug#44495: Prepared Statement:
  13130. CALL p(<x>) - `thd->protocol == &thd->protocol_text' failed
  13131. */
  13132. static void test_bug44495()
  13133. {
  13134. int rc;
  13135. MYSQL con;
  13136. MYSQL_STMT *stmt;
  13137. DBUG_ENTER("test_bug44495");
  13138. myheader("test_44495");
  13139. rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
  13140. myquery(rc);
  13141. rc= mysql_query(mysql, "CREATE PROCEDURE p1(IN arg VARCHAR(25))"
  13142. " BEGIN SET @stmt = CONCAT('SELECT \"', arg, '\"');"
  13143. " PREPARE ps1 FROM @stmt;"
  13144. " EXECUTE ps1;"
  13145. " DROP PREPARE ps1;"
  13146. "END;");
  13147. myquery(rc);
  13148. DIE_UNLESS(mysql_client_init(&con));
  13149. DIE_UNLESS(mysql_real_connect(&con, opt_host, opt_user, opt_password,
  13150. current_db, opt_port, opt_unix_socket,
  13151. CLIENT_MULTI_RESULTS));
  13152. stmt= mysql_simple_prepare(&con, "CALL p1('abc')");
  13153. check_stmt(stmt);
  13154. rc= mysql_stmt_execute(stmt);
  13155. check_execute(stmt, rc);
  13156. rc= my_process_stmt_result(stmt);
  13157. DIE_UNLESS(rc == 1);
  13158. mysql_stmt_close(stmt);
  13159. mysql_close(&con);
  13160. rc= mysql_query(mysql, "DROP PROCEDURE p1");
  13161. myquery(rc);
  13162. DBUG_VOID_RETURN;
  13163. }
  13164. static void test_bug53371()
  13165. {
  13166. int rc;
  13167. MYSQL_RES *result;
  13168. myheader("test_bug53371");
  13169. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  13170. myquery(rc);
  13171. rc= mysql_query(mysql, "DROP DATABASE IF EXISTS bug53371");
  13172. myquery(rc);
  13173. rc= mysql_query(mysql, "DROP USER 'testbug'@localhost");
  13174. rc= mysql_query(mysql, "CREATE TABLE t1 (a INT)");
  13175. myquery(rc);
  13176. rc= mysql_query(mysql, "CREATE DATABASE bug53371");
  13177. myquery(rc);
  13178. rc= mysql_query(mysql, "GRANT SELECT ON bug53371.* to 'testbug'@localhost");
  13179. myquery(rc);
  13180. rc= mysql_change_user(mysql, "testbug", NULL, "bug53371");
  13181. myquery(rc);
  13182. rc= mysql_query(mysql, "SHOW COLUMNS FROM client_test_db.t1");
  13183. DIE_UNLESS(rc);
  13184. DIE_UNLESS(mysql_errno(mysql) == 1142);
  13185. result= mysql_list_fields(mysql, "../client_test_db/t1", NULL);
  13186. DIE_IF(result);
  13187. result= mysql_list_fields(mysql, "#mysql50#/../client_test_db/t1", NULL);
  13188. DIE_IF(result);
  13189. rc= mysql_change_user(mysql, opt_user, opt_password, current_db);
  13190. myquery(rc);
  13191. rc= mysql_query(mysql, "DROP TABLE t1");
  13192. myquery(rc);
  13193. rc= mysql_query(mysql, "DROP DATABASE bug53371");
  13194. myquery(rc);
  13195. rc= mysql_query(mysql, "DROP USER 'testbug'@localhost");
  13196. myquery(rc);
  13197. }
  13198. static void test_bug53907()
  13199. {
  13200. int rc;
  13201. uchar buf[] = "\x4test\x14../client_test_db/t1";
  13202. myheader("test_bug53907");
  13203. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  13204. myquery(rc);
  13205. rc= mysql_query(mysql, "DROP DATABASE IF EXISTS bug53907");
  13206. myquery(rc);
  13207. rc= mysql_query(mysql, "DROP USER 'testbug'@localhost");
  13208. rc= mysql_query(mysql, "CREATE TABLE t1 (a INT)");
  13209. myquery(rc);
  13210. rc= mysql_query(mysql, "CREATE DATABASE bug53907");
  13211. myquery(rc);
  13212. rc= mysql_query(mysql, "GRANT SELECT ON bug53907.* to 'testbug'@localhost");
  13213. myquery(rc);
  13214. rc= mysql_change_user(mysql, "testbug", NULL, "bug53907");
  13215. myquery(rc);
  13216. rc= simple_command(mysql, COM_TABLE_DUMP, buf, sizeof(buf), 0);
  13217. fprintf(stderr, ">>>>>>>>> %d\n", mysql_errno(mysql));
  13218. DIE_UNLESS(mysql_errno(mysql) == 1103); /* ER_WRONG_TABLE_NAME */
  13219. rc= mysql_change_user(mysql, opt_user, opt_password, current_db);
  13220. myquery(rc);
  13221. rc= mysql_query(mysql, "DROP TABLE t1");
  13222. myquery(rc);
  13223. rc= mysql_query(mysql, "DROP DATABASE bug53907");
  13224. myquery(rc);
  13225. rc= mysql_query(mysql, "DROP USER 'testbug'@localhost");
  13226. myquery(rc);
  13227. }
  13228. /**
  13229. Bug#42373: libmysql can mess a connection at connect
  13230. */
  13231. static void test_bug42373()
  13232. {
  13233. int rc;
  13234. MYSQL con;
  13235. MYSQL_STMT *stmt;
  13236. DBUG_ENTER("test_bug42373");
  13237. myheader("test_42373");
  13238. rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
  13239. myquery(rc);
  13240. rc= mysql_query(mysql, "CREATE PROCEDURE p1()"
  13241. " BEGIN"
  13242. " SELECT 1;"
  13243. " INSERT INTO t1 VALUES (2);"
  13244. "END;");
  13245. myquery(rc);
  13246. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  13247. myquery(rc);
  13248. rc= mysql_query(mysql, "CREATE TABLE t1 (a INT)");
  13249. myquery(rc);
  13250. /* Try with a stored procedure. */
  13251. DIE_UNLESS(mysql_client_init(&con));
  13252. mysql_options(&con, MYSQL_INIT_COMMAND, "CALL p1()");
  13253. DIE_UNLESS(mysql_real_connect(&con, opt_host, opt_user, opt_password,
  13254. current_db, opt_port, opt_unix_socket,
  13255. CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS));
  13256. stmt= mysql_simple_prepare(&con, "SELECT a FROM t1");
  13257. check_stmt(stmt);
  13258. rc= mysql_stmt_execute(stmt);
  13259. check_execute(stmt, rc);
  13260. rc= my_process_stmt_result(stmt);
  13261. DIE_UNLESS(rc == 1);
  13262. mysql_stmt_close(stmt);
  13263. /* Now try with a multi-statement. */
  13264. DIE_UNLESS(mysql_client_init(&con));
  13265. mysql_options(&con, MYSQL_INIT_COMMAND,
  13266. "SELECT 3; INSERT INTO t1 VALUES (4)");
  13267. DIE_UNLESS(mysql_real_connect(&con, opt_host, opt_user, opt_password,
  13268. current_db, opt_port, opt_unix_socket,
  13269. CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS));
  13270. stmt= mysql_simple_prepare(&con, "SELECT a FROM t1");
  13271. check_stmt(stmt);
  13272. rc= mysql_stmt_execute(stmt);
  13273. check_execute(stmt, rc);
  13274. rc= my_process_stmt_result(stmt);
  13275. DIE_UNLESS(rc == 2);
  13276. mysql_stmt_close(stmt);
  13277. mysql_close(&con);
  13278. rc= mysql_query(mysql, "DROP TABLE t1");
  13279. myquery(rc);
  13280. rc= mysql_query(mysql, "DROP PROCEDURE p1");
  13281. myquery(rc);
  13282. DBUG_VOID_RETURN;
  13283. }
  13284. /**
  13285. Bug#54041: MySQL 5.0.92 fails when tests from Connector/C suite run
  13286. */
  13287. static void test_bug54041_impl()
  13288. {
  13289. int rc;
  13290. MYSQL_STMT *stmt;
  13291. MYSQL_BIND bind;
  13292. DBUG_ENTER("test_bug54041");
  13293. myheader("test_bug54041");
  13294. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  13295. myquery(rc);
  13296. rc= mysql_query(mysql, "CREATE TABLE t1 (a INT)");
  13297. myquery(rc);
  13298. stmt= mysql_simple_prepare(mysql, "SELECT a FROM t1 WHERE a > ?");
  13299. check_stmt(stmt);
  13300. verify_param_count(stmt, 1);
  13301. memset(&bind, 0, sizeof(bind));
  13302. /* Any type that does not support long data handling. */
  13303. bind.buffer_type= MYSQL_TYPE_LONG;
  13304. rc= mysql_stmt_bind_param(stmt, &bind);
  13305. check_execute(stmt, rc);
  13306. /*
  13307. Trick the client API into sending a long data packet for
  13308. the parameter. Long data is only supported for string and
  13309. binary types.
  13310. */
  13311. stmt->params[0].buffer_type= MYSQL_TYPE_STRING;
  13312. rc= mysql_stmt_send_long_data(stmt, 0, "data", 5);
  13313. check_execute(stmt, rc);
  13314. /* Undo API violation. */
  13315. stmt->params[0].buffer_type= MYSQL_TYPE_LONG;
  13316. rc= mysql_stmt_execute(stmt);
  13317. /* Incorrect arguments. */
  13318. check_execute_r(stmt, rc);
  13319. mysql_stmt_close(stmt);
  13320. rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  13321. myquery(rc);
  13322. DBUG_VOID_RETURN;
  13323. }
  13324. /**
  13325. Bug#54041: MySQL 5.0.92 fails when tests from Connector/C suite run
  13326. */
  13327. static void test_bug54041()
  13328. {
  13329. enable_query_logs(0);
  13330. test_bug54041_impl();
  13331. disable_query_logs();
  13332. test_bug54041_impl();
  13333. restore_query_logs();
  13334. }
  13335. /**
  13336. Bug#47485: mysql_store_result returns a result set for a prepared statement
  13337. */
  13338. static void test_bug47485()
  13339. {
  13340. MYSQL_STMT *stmt;
  13341. MYSQL_RES *res;
  13342. MYSQL_BIND bind[2];
  13343. int rc;
  13344. const char* sql_select = "SELECT 1, 'a'";
  13345. int int_data;
  13346. char str_data[16];
  13347. my_bool is_null[2];
  13348. my_bool error[2];
  13349. unsigned long length[2];
  13350. DBUG_ENTER("test_bug47485");
  13351. myheader("test_bug47485");
  13352. stmt= mysql_stmt_init(mysql);
  13353. check_stmt(stmt);
  13354. rc= mysql_stmt_prepare(stmt, sql_select, strlen(sql_select));
  13355. check_execute(stmt, rc);
  13356. rc= mysql_stmt_execute(stmt);
  13357. check_execute(stmt, rc);
  13358. res = mysql_store_result(mysql);
  13359. DIE_UNLESS(res == NULL);
  13360. mysql_stmt_reset(stmt);
  13361. rc= mysql_stmt_execute(stmt);
  13362. check_execute(stmt, rc);
  13363. res = mysql_use_result(mysql);
  13364. DIE_UNLESS(res == NULL);
  13365. mysql_stmt_reset(stmt);
  13366. memset(bind, 0, sizeof(bind));
  13367. bind[0].buffer_type= MYSQL_TYPE_LONG;
  13368. bind[0].buffer= (char *)&int_data;
  13369. bind[0].is_null= &is_null[0];
  13370. bind[0].length= &length[0];
  13371. bind[0].error= &error[0];
  13372. bind[1].buffer_type= MYSQL_TYPE_STRING;
  13373. bind[1].buffer= (char *)str_data;
  13374. bind[1].buffer_length= sizeof(str_data);
  13375. bind[1].is_null= &is_null[1];
  13376. bind[1].length= &length[1];
  13377. bind[1].error= &error[1];
  13378. rc= mysql_stmt_bind_result(stmt, bind);
  13379. check_execute(stmt, rc);
  13380. rc= mysql_stmt_execute(stmt);
  13381. check_execute(stmt, rc);
  13382. rc= mysql_stmt_store_result(stmt);
  13383. check_execute(stmt, rc);
  13384. while (!(rc= mysql_stmt_fetch(stmt)))
  13385. ;
  13386. DIE_UNLESS(rc == MYSQL_NO_DATA);
  13387. mysql_stmt_reset(stmt);
  13388. memset(bind, 0, sizeof(bind));
  13389. bind[0].buffer_type= MYSQL_TYPE_LONG;
  13390. bind[0].buffer= (char *)&int_data;
  13391. bind[0].is_null= &is_null[0];
  13392. bind[0].length= &length[0];
  13393. bind[0].error= &error[0];
  13394. bind[1].buffer_type= MYSQL_TYPE_STRING;
  13395. bind[1].buffer= (char *)str_data;
  13396. bind[1].buffer_length= sizeof(str_data);
  13397. bind[1].is_null= &is_null[1];
  13398. bind[1].length= &length[1];
  13399. bind[1].error= &error[1];
  13400. rc= mysql_stmt_bind_result(stmt, bind);
  13401. check_execute(stmt, rc);
  13402. rc= mysql_stmt_execute(stmt);
  13403. check_execute(stmt, rc);
  13404. while (!(rc= mysql_stmt_fetch(stmt)))
  13405. ;
  13406. DIE_UNLESS(rc == MYSQL_NO_DATA);
  13407. mysql_stmt_close(stmt);
  13408. DBUG_VOID_RETURN;
  13409. }
  13410. /*
  13411. Bug#58036 client utf32, utf16, ucs2 should be disallowed, they crash server
  13412. */
  13413. static void test_bug58036()
  13414. {
  13415. MYSQL *conn;
  13416. DBUG_ENTER("test_bug47485");
  13417. myheader("test_bug58036");
  13418. /* Part1: try to connect with ucs2 client character set */
  13419. conn= mysql_client_init(NULL);
  13420. mysql_options(conn, MYSQL_SET_CHARSET_NAME, "ucs2");
  13421. if (mysql_real_connect(conn, opt_host, opt_user,
  13422. opt_password, opt_db ? opt_db : "test",
  13423. opt_port, opt_unix_socket, 0))
  13424. {
  13425. if (!opt_silent)
  13426. printf("mysql_real_connect() succeeded (failure expected)\n");
  13427. mysql_close(conn);
  13428. DIE("");
  13429. }
  13430. if (!opt_silent)
  13431. printf("Got mysql_real_connect() error (expected): %s (%d)\n",
  13432. mysql_error(conn), mysql_errno(conn));
  13433. DIE_UNLESS(mysql_errno(conn) == ER_WRONG_VALUE_FOR_VAR);
  13434. mysql_close(conn);
  13435. /*
  13436. Part2:
  13437. - connect with latin1
  13438. - then change client character set to ucs2
  13439. - then try mysql_change_user()
  13440. */
  13441. conn= mysql_client_init(NULL);
  13442. mysql_options(conn, MYSQL_SET_CHARSET_NAME, "latin1");
  13443. if (!mysql_real_connect(conn, opt_host, opt_user,
  13444. opt_password, opt_db ? opt_db : "test",
  13445. opt_port, opt_unix_socket, 0))
  13446. {
  13447. if (!opt_silent)
  13448. printf("mysql_real_connect() failed: %s (%d)\n",
  13449. mysql_error(conn), mysql_errno(conn));
  13450. mysql_close(conn);
  13451. DIE("");
  13452. }
  13453. mysql_options(conn, MYSQL_SET_CHARSET_NAME, "ucs2");
  13454. if (!mysql_change_user(conn, opt_user, opt_password, NULL))
  13455. {
  13456. if (!opt_silent)
  13457. printf("mysql_change_user() succedded, error expected!");
  13458. mysql_close(conn);
  13459. DIE("");
  13460. }
  13461. if (!opt_silent)
  13462. printf("Got mysql_change_user() error (expected): %s (%d)\n",
  13463. mysql_error(conn), mysql_errno(conn));
  13464. mysql_close(conn);
  13465. DBUG_VOID_RETURN;
  13466. }
  13467. /*
  13468. Bug #56976: Severe Denial Of Service in prepared statements
  13469. */
  13470. static void test_bug56976()
  13471. {
  13472. MYSQL_STMT *stmt;
  13473. MYSQL_BIND bind[1];
  13474. int rc;
  13475. const char* query = "SELECT LENGTH(?)";
  13476. char *long_buffer;
  13477. unsigned long i, packet_len = 256 * 1024L;
  13478. unsigned long dos_len = 2 * 1024 * 1024L;
  13479. DBUG_ENTER("test_bug56976");
  13480. myheader("test_bug56976");
  13481. stmt= mysql_stmt_init(mysql);
  13482. check_stmt(stmt);
  13483. rc= mysql_stmt_prepare(stmt, query, strlen(query));
  13484. check_execute(stmt, rc);
  13485. memset(bind, 0, sizeof(bind));
  13486. bind[0].buffer_type = MYSQL_TYPE_TINY_BLOB;
  13487. rc= mysql_stmt_bind_param(stmt, bind);
  13488. check_execute(stmt, rc);
  13489. long_buffer= (char*) my_malloc(packet_len, MYF(0));
  13490. DIE_UNLESS(long_buffer);
  13491. memset(long_buffer, 'a', packet_len);
  13492. for (i= 0; i < dos_len / packet_len; i++)
  13493. {
  13494. rc= mysql_stmt_send_long_data(stmt, 0, long_buffer, packet_len);
  13495. check_execute(stmt, rc);
  13496. }
  13497. my_free(long_buffer, MYF(0));
  13498. rc= mysql_stmt_execute(stmt);
  13499. DIE_UNLESS(rc && mysql_stmt_errno(stmt) == ER_UNKNOWN_ERROR);
  13500. mysql_stmt_close(stmt);
  13501. DBUG_VOID_RETURN;
  13502. }
  13503. /*
  13504. Bug#13001491: MYSQL_REFRESH CRASHES WHEN STORED ROUTINES ARE RUN CONCURRENTLY.
  13505. */
  13506. static void test_bug13001491()
  13507. {
  13508. int rc;
  13509. char query[MAX_TEST_QUERY_LENGTH];
  13510. MYSQL *c;
  13511. myheader("test_bug13001491");
  13512. my_snprintf(query, MAX_TEST_QUERY_LENGTH,
  13513. "GRANT ALL PRIVILEGES ON *.* TO mysqltest_u1@%s",
  13514. opt_host ? opt_host : "'localhost'");
  13515. rc= mysql_query(mysql, query);
  13516. myquery(rc);
  13517. my_snprintf(query, MAX_TEST_QUERY_LENGTH,
  13518. "GRANT RELOAD ON *.* TO mysqltest_u1@%s",
  13519. opt_host ? opt_host : "'localhost'");
  13520. rc= mysql_query(mysql, query);
  13521. myquery(rc);
  13522. c= mysql_client_init(NULL);
  13523. DIE_UNLESS(mysql_real_connect(c, opt_host, "mysqltest_u1", NULL,
  13524. current_db, opt_port, opt_unix_socket,
  13525. CLIENT_MULTI_STATEMENTS |
  13526. CLIENT_MULTI_RESULTS));
  13527. rc= mysql_query(c, "DROP PROCEDURE IF EXISTS p1");
  13528. myquery(rc);
  13529. rc= mysql_query(c,
  13530. "CREATE PROCEDURE p1() "
  13531. "BEGIN "
  13532. " DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END; "
  13533. " SELECT COUNT(*) "
  13534. " FROM INFORMATION_SCHEMA.PROCESSLIST "
  13535. " GROUP BY user "
  13536. " ORDER BY NULL "
  13537. " INTO @a; "
  13538. "END");
  13539. myquery(rc);
  13540. rc= mysql_query(c, "CALL p1()");
  13541. myquery(rc);
  13542. mysql_free_result(mysql_store_result(c));
  13543. /* Check that mysql_refresh() succeeds without REFRESH_LOG. */
  13544. rc= mysql_refresh(c, REFRESH_GRANT |
  13545. REFRESH_TABLES | REFRESH_HOSTS |
  13546. REFRESH_STATUS | REFRESH_THREADS);
  13547. myquery(rc);
  13548. /*
  13549. Check that mysql_refresh(REFRESH_LOG) does not crash the server even if it
  13550. fails. mysql_refresh(REFRESH_LOG) fails when error log points to unavailable
  13551. location.
  13552. */
  13553. mysql_refresh(c, REFRESH_LOG);
  13554. rc= mysql_query(c, "DROP PROCEDURE p1");
  13555. myquery(rc);
  13556. mysql_close(c);
  13557. c= NULL;
  13558. my_snprintf(query, MAX_TEST_QUERY_LENGTH,
  13559. "DROP USER mysqltest_u1@%s",
  13560. opt_host ? opt_host : "'localhost'");
  13561. rc= mysql_query(mysql, query);
  13562. myquery(rc);
  13563. }
  13564. static struct my_tests_st my_tests[]= {
  13565. { "disable_query_logs", disable_query_logs },
  13566. { "test_view_sp_list_fields", test_view_sp_list_fields },
  13567. { "client_query", client_query },
  13568. { "test_prepare_insert_update", test_prepare_insert_update},
  13569. #if NOT_YET_WORKING
  13570. { "test_drop_temp", test_drop_temp },
  13571. #endif
  13572. { "test_fetch_seek", test_fetch_seek },
  13573. { "test_fetch_nobuffs", test_fetch_nobuffs },
  13574. { "test_open_direct", test_open_direct },
  13575. { "test_fetch_null", test_fetch_null },
  13576. { "test_ps_null_param", test_ps_null_param },
  13577. { "test_fetch_date", test_fetch_date },
  13578. { "test_fetch_str", test_fetch_str },
  13579. { "test_fetch_long", test_fetch_long },
  13580. { "test_fetch_short", test_fetch_short },
  13581. { "test_fetch_tiny", test_fetch_tiny },
  13582. { "test_fetch_bigint", test_fetch_bigint },
  13583. { "test_fetch_float", test_fetch_float },
  13584. { "test_fetch_double", test_fetch_double },
  13585. { "test_bind_result_ext", test_bind_result_ext },
  13586. { "test_bind_result_ext1", test_bind_result_ext1 },
  13587. { "test_select_direct", test_select_direct },
  13588. { "test_select_prepare", test_select_prepare },
  13589. { "test_select", test_select },
  13590. { "test_select_version", test_select_version },
  13591. { "test_ps_conj_select", test_ps_conj_select },
  13592. { "test_select_show_table", test_select_show_table },
  13593. { "test_func_fields", test_func_fields },
  13594. { "test_long_data", test_long_data },
  13595. { "test_insert", test_insert },
  13596. { "test_set_variable", test_set_variable },
  13597. { "test_select_show", test_select_show },
  13598. { "test_prepare_noparam", test_prepare_noparam },
  13599. { "test_bind_result", test_bind_result },
  13600. { "test_prepare_simple", test_prepare_simple },
  13601. { "test_prepare", test_prepare },
  13602. { "test_null", test_null },
  13603. { "test_debug_example", test_debug_example },
  13604. { "test_update", test_update },
  13605. { "test_simple_update", test_simple_update },
  13606. { "test_simple_delete", test_simple_delete },
  13607. { "test_double_compare", test_double_compare },
  13608. { "client_store_result", client_store_result },
  13609. { "client_use_result", client_use_result },
  13610. { "test_tran_bdb", test_tran_bdb },
  13611. { "test_tran_innodb", test_tran_innodb },
  13612. { "test_prepare_ext", test_prepare_ext },
  13613. { "test_prepare_syntax", test_prepare_syntax },
  13614. { "test_field_names", test_field_names },
  13615. { "test_field_flags", test_field_flags },
  13616. { "test_long_data_str", test_long_data_str },
  13617. { "test_long_data_str1", test_long_data_str1 },
  13618. { "test_long_data_bin", test_long_data_bin },
  13619. { "test_warnings", test_warnings },
  13620. { "test_errors", test_errors },
  13621. { "test_prepare_resultset", test_prepare_resultset },
  13622. { "test_stmt_close", test_stmt_close },
  13623. { "test_prepare_field_result", test_prepare_field_result },
  13624. { "test_multi_stmt", test_multi_stmt },
  13625. { "test_multi_statements", test_multi_statements },
  13626. { "test_prepare_multi_statements", test_prepare_multi_statements },
  13627. { "test_store_result", test_store_result },
  13628. { "test_store_result1", test_store_result1 },
  13629. { "test_store_result2", test_store_result2 },
  13630. { "test_subselect", test_subselect },
  13631. { "test_date", test_date },
  13632. { "test_date_date", test_date_date },
  13633. { "test_date_time", test_date_time },
  13634. { "test_date_ts", test_date_ts },
  13635. { "test_date_dt", test_date_dt },
  13636. { "test_prepare_alter", test_prepare_alter },
  13637. { "test_manual_sample", test_manual_sample },
  13638. { "test_pure_coverage", test_pure_coverage },
  13639. { "test_buffers", test_buffers },
  13640. { "test_ushort_bug", test_ushort_bug },
  13641. { "test_sshort_bug", test_sshort_bug },
  13642. { "test_stiny_bug", test_stiny_bug },
  13643. { "test_field_misc", test_field_misc },
  13644. { "test_set_option", test_set_option },
  13645. #ifndef EMBEDDED_LIBRARY
  13646. { "test_prepare_grant", test_prepare_grant },
  13647. #endif
  13648. { "test_frm_bug", test_frm_bug },
  13649. { "test_explain_bug", test_explain_bug },
  13650. { "test_decimal_bug", test_decimal_bug },
  13651. { "test_nstmts", test_nstmts },
  13652. { "test_logs;", test_logs },
  13653. { "test_cuted_rows", test_cuted_rows },
  13654. { "test_fetch_offset", test_fetch_offset },
  13655. { "test_fetch_column", test_fetch_column },
  13656. { "test_mem_overun", test_mem_overun },
  13657. { "test_list_fields", test_list_fields },
  13658. { "test_free_result", test_free_result },
  13659. { "test_free_store_result", test_free_store_result },
  13660. { "test_sqlmode", test_sqlmode },
  13661. { "test_ts", test_ts },
  13662. { "test_bug1115", test_bug1115 },
  13663. { "test_bug1180", test_bug1180 },
  13664. { "test_bug1500", test_bug1500 },
  13665. { "test_bug1644", test_bug1644 },
  13666. { "test_bug1946", test_bug1946 },
  13667. { "test_bug2248", test_bug2248 },
  13668. { "test_parse_error_and_bad_length", test_parse_error_and_bad_length },
  13669. { "test_bug2247", test_bug2247 },
  13670. { "test_subqueries", test_subqueries },
  13671. { "test_bad_union", test_bad_union },
  13672. { "test_distinct", test_distinct },
  13673. { "test_subqueries_ref", test_subqueries_ref },
  13674. { "test_union", test_union },
  13675. { "test_bug3117", test_bug3117 },
  13676. { "test_join", test_join },
  13677. { "test_selecttmp", test_selecttmp },
  13678. { "test_create_drop", test_create_drop },
  13679. { "test_rename", test_rename },
  13680. { "test_do_set", test_do_set },
  13681. { "test_multi", test_multi },
  13682. { "test_insert_select", test_insert_select },
  13683. { "test_bind_nagative", test_bind_nagative },
  13684. { "test_derived", test_derived },
  13685. { "test_xjoin", test_xjoin },
  13686. { "test_bug3035", test_bug3035 },
  13687. { "test_union2", test_union2 },
  13688. { "test_bug1664", test_bug1664 },
  13689. { "test_union_param", test_union_param },
  13690. { "test_order_param", test_order_param },
  13691. { "test_ps_i18n", test_ps_i18n },
  13692. { "test_bug3796", test_bug3796 },
  13693. { "test_bug4026", test_bug4026 },
  13694. { "test_bug4079", test_bug4079 },
  13695. { "test_bug4236", test_bug4236 },
  13696. { "test_bug4030", test_bug4030 },
  13697. { "test_bug5126", test_bug5126 },
  13698. { "test_bug4231", test_bug4231 },
  13699. { "test_bug5399", test_bug5399 },
  13700. { "test_bug5194", test_bug5194 },
  13701. { "test_bug5315", test_bug5315 },
  13702. { "test_bug6049", test_bug6049 },
  13703. { "test_bug6058", test_bug6058 },
  13704. { "test_bug6059", test_bug6059 },
  13705. { "test_bug6046", test_bug6046 },
  13706. { "test_bug6081", test_bug6081 },
  13707. { "test_bug6096", test_bug6096 },
  13708. { "test_datetime_ranges", test_datetime_ranges },
  13709. { "test_bug4172", test_bug4172 },
  13710. { "test_conversion", test_conversion },
  13711. { "test_rewind", test_rewind },
  13712. { "test_bug6761", test_bug6761 },
  13713. { "test_view", test_view },
  13714. { "test_view_where", test_view_where },
  13715. { "test_view_2where", test_view_2where },
  13716. { "test_view_star", test_view_star },
  13717. { "test_view_insert", test_view_insert },
  13718. { "test_left_join_view", test_left_join_view },
  13719. { "test_view_insert_fields", test_view_insert_fields },
  13720. { "test_basic_cursors", test_basic_cursors },
  13721. { "test_cursors_with_union", test_cursors_with_union },
  13722. { "test_cursors_with_procedure", test_cursors_with_procedure },
  13723. { "test_truncation", test_truncation },
  13724. { "test_truncation_option", test_truncation_option },
  13725. { "test_client_character_set", test_client_character_set },
  13726. { "test_bug8330", test_bug8330 },
  13727. { "test_bug7990", test_bug7990 },
  13728. { "test_bug8378", test_bug8378 },
  13729. { "test_bug8722", test_bug8722 },
  13730. { "test_bug8880", test_bug8880 },
  13731. { "test_bug9159", test_bug9159 },
  13732. { "test_bug9520", test_bug9520 },
  13733. { "test_bug9478", test_bug9478 },
  13734. { "test_bug9643", test_bug9643 },
  13735. { "test_bug10729", test_bug10729 },
  13736. { "test_bug11111", test_bug11111 },
  13737. { "test_bug9992", test_bug9992 },
  13738. { "test_bug10736", test_bug10736 },
  13739. { "test_bug10794", test_bug10794 },
  13740. { "test_bug11172", test_bug11172 },
  13741. { "test_bug11656", test_bug11656 },
  13742. { "test_bug10214", test_bug10214 },
  13743. { "test_bug9735", test_bug9735 },
  13744. { "test_bug11183", test_bug11183 },
  13745. { "test_bug11037", test_bug11037 },
  13746. { "test_bug10760", test_bug10760 },
  13747. { "test_bug12001", test_bug12001 },
  13748. { "test_bug11718", test_bug11718 },
  13749. { "test_bug12925", test_bug12925 },
  13750. { "test_bug11909", test_bug11909 },
  13751. { "test_bug11901", test_bug11901 },
  13752. { "test_bug11904", test_bug11904 },
  13753. { "test_bug12243", test_bug12243 },
  13754. { "test_bug14210", test_bug14210 },
  13755. { "test_bug13488", test_bug13488 },
  13756. { "test_bug13524", test_bug13524 },
  13757. { "test_bug14845", test_bug14845 },
  13758. { "test_opt_reconnect", test_opt_reconnect },
  13759. { "test_bug15510", test_bug15510},
  13760. #ifndef EMBEDDED_LIBRARY
  13761. { "test_bug12744", test_bug12744 },
  13762. #endif
  13763. { "test_bug16143", test_bug16143 },
  13764. { "test_bug16144", test_bug16144 },
  13765. { "test_bug15613", test_bug15613 },
  13766. { "test_bug20152", test_bug20152 },
  13767. { "test_bug14169", test_bug14169 },
  13768. { "test_bug17667", test_bug17667 },
  13769. { "test_bug15752", test_bug15752 },
  13770. { "test_mysql_insert_id", test_mysql_insert_id },
  13771. { "test_bug19671", test_bug19671 },
  13772. { "test_bug21206", test_bug21206 },
  13773. { "test_bug21726", test_bug21726 },
  13774. { "test_bug15518", test_bug15518 },
  13775. { "test_bug23383", test_bug23383 },
  13776. { "test_bug32265", test_bug32265 },
  13777. { "test_bug21635", test_bug21635 },
  13778. { "test_status", test_status },
  13779. { "test_bug24179", test_bug24179 },
  13780. { "test_ps_query_cache", test_ps_query_cache },
  13781. { "test_bug28075", test_bug28075 },
  13782. { "test_bug27876", test_bug27876 },
  13783. { "test_bug28505", test_bug28505 },
  13784. { "test_bug28934", test_bug28934 },
  13785. { "test_bug27592", test_bug27592 },
  13786. { "test_bug29687", test_bug29687 },
  13787. { "test_bug29692", test_bug29692 },
  13788. { "test_bug29306", test_bug29306 },
  13789. { "test_change_user", test_change_user },
  13790. { "test_bug30472", test_bug30472 },
  13791. { "test_bug20023", test_bug20023 },
  13792. { "test_bug45010", test_bug45010 },
  13793. { "test_bug53371", test_bug53371 },
  13794. { "test_bug53907", test_bug53907 },
  13795. { "test_bug31418", test_bug31418 },
  13796. { "test_bug31669", test_bug31669 },
  13797. { "test_bug28386", test_bug28386 },
  13798. { "test_wl4166_1", test_wl4166_1 },
  13799. { "test_wl4166_2", test_wl4166_2 },
  13800. { "test_bug38486", test_bug38486 },
  13801. { "test_bug40365", test_bug40365 },
  13802. { "test_bug43560", test_bug43560 },
  13803. #ifdef HAVE_QUERY_CACHE
  13804. { "test_bug36326", test_bug36326 },
  13805. #endif
  13806. { "test_bug41078", test_bug41078 },
  13807. { "test_bug44495", test_bug44495 },
  13808. { "test_bug42373", test_bug42373 },
  13809. { "test_bug54041", test_bug54041 },
  13810. { "test_bug47485", test_bug47485 },
  13811. { "test_bug58036", test_bug58036 },
  13812. { "test_bug56976", test_bug56976 },
  13813. { "test_bug13001491", test_bug13001491 },
  13814. { 0, 0 }
  13815. };
  13816. static struct my_tests_st *get_my_tests() { return my_tests; }