1/*
2Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3
4The MySQL Connector/C is licensed under the terms of the GPLv2
5<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
6MySQL Connectors. There are special exceptions to the terms and
7conditions of the GPLv2 as it is applied to this software, see the
8FLOSS License Exception
9<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
10
11This program is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published
13by the Free Software Foundation; version 2 of the License.
14
15This program is distributed in the hope that it will be useful, but
16WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18for more details.
19
20You should have received a copy of the GNU General Public License along
21with this program; if not, write to the Free Software Foundation, Inc.,
2251 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24#include "my_test.h"
25
26#define MY_INT64_NUM_DECIMAL_DIGITS 21
27#define MAX_INDEXES 64
28
29/* A workaround for Sun Forte 5.6 on Solaris x86 */
30
31static int cmp_double(double *a, double *b)
32{
33 return *a == *b;
34 return OK;
35}
36
37/* Test BUG#1115 (incorrect string parameter value allocation) */
38
39static int test_conc67(MYSQL *mysql)
40{
41 MYSQL_STMT *stmt= mysql_stmt_init(mysql);
42 const char *query= "SELECT a,b FROM conc67 WHERE a=?";
43 int rc, i;
44 MYSQL_BIND bind[2];
45 char val[20];
46 MYSQL_BIND rbind;
47 MYSQL_RES *res;
48 ulong prefetch_rows= 1000;
49 ulong cursor_type= CURSOR_TYPE_READ_ONLY;
50
51 rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc67");
52 check_mysql_rc(rc, mysql);
53
54 rc= mysql_query(mysql, "CREATE TABLE conc67 (a int, b text)");
55 check_mysql_rc(rc, mysql);
56
57 rc= mysql_query(mysql, "INSERT INTO conc67 VALUES (1, 'foo')");
58 check_mysql_rc(rc, mysql);
59
60 rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, &cursor_type);
61 check_stmt_rc(rc, stmt);
62 rc= mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS, &prefetch_rows);
63 check_stmt_rc(rc, stmt);
64
65 rc= mysql_stmt_prepare(stmt, SL(query));
66 check_stmt_rc(rc, stmt);
67
68 memset(&rbind, 0, sizeof(MYSQL_BIND));
69 i= 1;
70 rbind.buffer_type= MYSQL_TYPE_LONG;
71 rbind.buffer= &i;
72 rbind.buffer_length= 4;
73 mysql_stmt_bind_param(stmt, &rbind);
74
75 rc= mysql_stmt_execute(stmt);
76 check_stmt_rc(rc, stmt);
77
78 res= mysql_stmt_result_metadata(stmt);
79 mysql_free_result(res);
80
81 memset(bind, 0, 2 * sizeof(MYSQL_BIND));
82
83 i= 0;
84 bind[0].buffer_type= MYSQL_TYPE_LONG;
85 bind[0].buffer= &i;
86 bind[0].buffer_length= 4;
87 bind[1].buffer_type= MYSQL_TYPE_STRING;
88 bind[1].buffer= &val;
89 bind[1].buffer_length= 20;
90
91 mysql_stmt_bind_result(stmt, bind);
92
93 rc= mysql_stmt_fetch(stmt);
94 check_stmt_rc(rc, stmt);
95
96 FAIL_IF(i != 1, "expected value 1 for first row");
97
98 rc= mysql_stmt_fetch(stmt);
99 FAIL_IF(rc != MYSQL_NO_DATA, "Eof expected");
100
101 mysql_stmt_close(stmt);
102 rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc67");
103 check_mysql_rc(rc, mysql);
104 return OK;
105}
106
107static int test_bug1115(MYSQL *mysql)
108{
109 MYSQL_STMT *stmt;
110 int rc, rowcount;
111 MYSQL_BIND my_bind[1];
112 ulong length[1];
113 char szData[11];
114 char query[MAX_TEST_QUERY_LENGTH];
115
116 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
117 check_mysql_rc(rc, mysql);
118
119 rc= mysql_query(mysql, "CREATE TABLE test_select(\
120session_id char(9) NOT NULL, \
121 a int(8) unsigned NOT NULL, \
122 b int(5) NOT NULL, \
123 c int(5) NOT NULL, \
124 d datetime NOT NULL)");
125 check_mysql_rc(rc, mysql);
126 rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
127 "(\"abc\", 1, 2, 3, 2003-08-30), "
128 "(\"abd\", 1, 2, 3, 2003-08-30), "
129 "(\"abf\", 1, 2, 3, 2003-08-30), "
130 "(\"abg\", 1, 2, 3, 2003-08-30), "
131 "(\"abh\", 1, 2, 3, 2003-08-30), "
132 "(\"abj\", 1, 2, 3, 2003-08-30), "
133 "(\"abk\", 1, 2, 3, 2003-08-30), "
134 "(\"abl\", 1, 2, 3, 2003-08-30), "
135 "(\"abq\", 1, 2, 3, 2003-08-30) ");
136 check_mysql_rc(rc, mysql);
137 rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
138 "(\"abw\", 1, 2, 3, 2003-08-30), "
139 "(\"abe\", 1, 2, 3, 2003-08-30), "
140 "(\"abr\", 1, 2, 3, 2003-08-30), "
141 "(\"abt\", 1, 2, 3, 2003-08-30), "
142 "(\"aby\", 1, 2, 3, 2003-08-30), "
143 "(\"abu\", 1, 2, 3, 2003-08-30), "
144 "(\"abi\", 1, 2, 3, 2003-08-30), "
145 "(\"abo\", 1, 2, 3, 2003-08-30), "
146 "(\"abp\", 1, 2, 3, 2003-08-30), "
147 "(\"abz\", 1, 2, 3, 2003-08-30), "
148 "(\"abx\", 1, 2, 3, 2003-08-30)");
149 check_mysql_rc(rc, mysql);
150
151 strcpy(query, "SELECT * FROM test_select WHERE "
152 "CONVERT(session_id USING utf8)= ?");
153 stmt= mysql_stmt_init(mysql);
154 FAIL_IF(!stmt, mysql_error(mysql));
155 rc= mysql_stmt_prepare(stmt, SL(query));
156 check_stmt_rc(rc, stmt);
157
158 FAIL_IF(mysql_stmt_param_count(stmt) != 1, "Paramcount != 1");
159
160 memset(my_bind, '\0', sizeof(MYSQL_BIND));
161
162 strcpy(szData, (char *)"abc");
163 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
164 my_bind[0].buffer= (void *)szData;
165 my_bind[0].buffer_length= 10;
166 my_bind[0].length= &length[0];
167 length[0]= 3;
168
169 rc= mysql_stmt_bind_param(stmt, my_bind);
170 check_stmt_rc(rc, stmt);
171
172 rc= mysql_stmt_execute(stmt);
173 check_stmt_rc(rc, stmt);
174
175 rowcount= 0;
176 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
177 rowcount++;
178 FAIL_IF(rowcount != 1, "rowcount=%d != 1");
179
180 strcpy(szData, (char *)"venu");
181 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
182 my_bind[0].buffer= (void *)szData;
183 my_bind[0].buffer_length= 10;
184 my_bind[0].length= &length[0];
185 length[0]= 4;
186 my_bind[0].is_null= 0;
187
188 rc= mysql_stmt_bind_param(stmt, my_bind);
189 check_stmt_rc(rc, stmt);
190
191 rc= mysql_stmt_execute(stmt);
192 check_stmt_rc(rc, stmt);
193
194 rowcount= 0;
195 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
196 rowcount++;
197 FAIL_IF(rowcount != 0, "rowcount != 0");
198
199 strcpy(szData, (char *)"abc");
200 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
201 my_bind[0].buffer= (void *)szData;
202 my_bind[0].buffer_length= 10;
203 my_bind[0].length= &length[0];
204 length[0]= 3;
205 my_bind[0].is_null= 0;
206
207 rc= mysql_stmt_bind_param(stmt, my_bind);
208 check_stmt_rc(rc, stmt);
209
210 rc= mysql_stmt_execute(stmt);
211 check_stmt_rc(rc, stmt);
212
213 rowcount= 0;
214 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
215 rowcount++;
216 FAIL_IF(rowcount != 1, "rowcount != 1");
217
218 mysql_stmt_close(stmt);
219 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
220 check_mysql_rc(rc, mysql);
221
222 return OK;
223}
224/* Test BUG#1180 (optimized away part of WHERE clause) */
225
226static int test_bug1180(MYSQL *mysql)
227{
228 MYSQL_STMT *stmt;
229 int rc, rowcount;
230 MYSQL_BIND my_bind[1];
231 ulong length[1];
232 char szData[11];
233 char query[MAX_TEST_QUERY_LENGTH];
234
235 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
236 check_mysql_rc(rc, mysql);
237
238 rc= mysql_query(mysql, "CREATE TABLE test_select(session_id char(9) NOT NULL)");
239 check_mysql_rc(rc, mysql);
240 rc= mysql_query(mysql, "INSERT INTO test_select VALUES (\"abc\")");
241 check_mysql_rc(rc, mysql);
242
243 strcpy(query, "SELECT * FROM test_select WHERE ?= \"1111\" and "
244 "session_id= \"abc\"");
245 stmt= mysql_stmt_init(mysql);
246 FAIL_IF(!stmt, mysql_error(mysql));
247 rc= mysql_stmt_prepare(stmt, SL(query));
248 check_stmt_rc(rc, stmt);
249
250 FAIL_IF(mysql_stmt_param_count(stmt) != 1, "Paramcount != 1");
251
252 memset(my_bind, '\0', sizeof(MYSQL_BIND));
253
254 strcpy(szData, (char *)"abc");
255 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
256 my_bind[0].buffer= (void *)szData;
257 my_bind[0].buffer_length= 10;
258 my_bind[0].length= &length[0];
259 length[0]= 3;
260 my_bind[0].is_null= 0;
261
262 rc= mysql_stmt_bind_param(stmt, my_bind);
263 check_stmt_rc(rc, stmt);
264
265 rc= mysql_stmt_execute(stmt);
266 check_stmt_rc(rc, stmt);
267
268
269 rowcount= 0;
270 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
271 rowcount++;
272 FAIL_IF(rowcount != 0, "rowcount != 0");
273
274 strcpy(szData, (char *)"1111");
275 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
276 my_bind[0].buffer= (void *)szData;
277 my_bind[0].buffer_length= 10;
278 my_bind[0].length= &length[0];
279 length[0]= 4;
280 my_bind[0].is_null= 0;
281
282 rc= mysql_stmt_bind_param(stmt, my_bind);
283 check_stmt_rc(rc, stmt);
284
285 rc= mysql_stmt_execute(stmt);
286 check_stmt_rc(rc, stmt);
287
288 rowcount= 0;
289 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
290 rowcount++;
291 FAIL_IF(rowcount != 1, "rowcount != 1");
292
293 strcpy(szData, (char *)"abc");
294 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
295 my_bind[0].buffer= (void *)szData;
296 my_bind[0].buffer_length= 10;
297 my_bind[0].length= &length[0];
298 length[0]= 3;
299 my_bind[0].is_null= 0;
300
301 rc= mysql_stmt_bind_param(stmt, my_bind);
302 check_stmt_rc(rc, stmt);
303
304 rc= mysql_stmt_execute(stmt);
305 check_stmt_rc(rc, stmt);
306
307 rowcount= 0;
308 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
309 rowcount++;
310 FAIL_IF(rowcount != 0, "rowcount != 0");
311
312 mysql_stmt_close(stmt);
313 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
314 check_mysql_rc(rc, mysql);
315
316 return OK;
317}
318
319
320/*
321 Test BUG#1644 (Insertion of more than 3 NULL columns with parameter
322 binding fails)
323*/
324
325static int test_bug1644(MYSQL *mysql)
326{
327 MYSQL_STMT *stmt;
328 MYSQL_RES *result;
329 MYSQL_ROW row;
330 MYSQL_BIND my_bind[4];
331 int num;
332 my_bool isnull;
333 int rc, i;
334 char query[MAX_TEST_QUERY_LENGTH];
335
336 rc= mysql_query(mysql, "DROP TABLE IF EXISTS foo_dfr");
337 check_mysql_rc(rc, mysql);
338
339 rc= mysql_query(mysql,
340 "CREATE TABLE foo_dfr(col1 int, col2 int, col3 int, col4 int);");
341 check_mysql_rc(rc, mysql);
342
343 strcpy(query, "INSERT INTO foo_dfr VALUES (?, ?, ?, ? )");
344 stmt= mysql_stmt_init(mysql);
345 FAIL_IF(!stmt, mysql_error(mysql));
346 rc= mysql_stmt_prepare(stmt, SL(query));
347 check_stmt_rc(rc, stmt);
348
349 FAIL_IF(mysql_stmt_param_count(stmt) != 4, "Paramcount != 4");
350
351 memset(my_bind, '\0', sizeof(MYSQL_BIND) * 4);
352
353 num= 22;
354 isnull= 0;
355 for (i= 0 ; i < 4 ; i++)
356 {
357 my_bind[i].buffer_type= MYSQL_TYPE_LONG;
358 my_bind[i].buffer= (void *)&num;
359 my_bind[i].is_null= &isnull;
360 }
361
362 rc= mysql_stmt_bind_param(stmt, my_bind);
363 check_stmt_rc(rc, stmt);
364
365 rc= mysql_stmt_execute(stmt);
366 check_stmt_rc(rc, stmt);
367
368 isnull= 1;
369 for (i= 0 ; i < 4 ; i++)
370 my_bind[i].is_null= &isnull;
371
372 rc= mysql_stmt_bind_param(stmt, my_bind);
373 check_stmt_rc(rc, stmt);
374
375 rc= mysql_stmt_execute(stmt);
376 check_stmt_rc(rc, stmt);
377
378 isnull= 0;
379 num= 88;
380 for (i= 0 ; i < 4 ; i++)
381 my_bind[i].is_null= &isnull;
382
383 rc= mysql_stmt_bind_param(stmt, my_bind);
384 check_stmt_rc(rc, stmt);
385
386 rc= mysql_stmt_execute(stmt);
387 check_stmt_rc(rc, stmt);
388
389 mysql_stmt_close(stmt);
390
391 rc= mysql_query(mysql, "SELECT * FROM foo_dfr");
392 check_mysql_rc(rc, mysql);
393
394 result= mysql_store_result(mysql);
395 FAIL_IF(!result, "Invalid resultset");
396
397 FAIL_IF(mysql_num_rows(result) != 3, "rowcount != 3");
398
399 mysql_data_seek(result, 0);
400
401 row= mysql_fetch_row(result);
402 FAIL_IF(!row, "row = NULL");
403 for (i= 0 ; i < 4 ; i++)
404 {
405 FAIL_UNLESS(strcmp(row[i], "22") == 0, "Wrong value");
406 }
407 row= mysql_fetch_row(result);
408 FAIL_IF(!row, "Invalid row");
409 for (i= 0 ; i < 4 ; i++)
410 {
411 FAIL_UNLESS(row[i] == 0, "row[i] != 0");
412 }
413 row= mysql_fetch_row(result);
414 FAIL_IF(!row, "Invalid row");
415 for (i= 0 ; i < 4 ; i++)
416 {
417 FAIL_UNLESS(strcmp(row[i], "88") == 0, "row[i] != 88");
418 }
419 row= mysql_fetch_row(result);
420 FAIL_IF(row, "row != NULL");
421
422 mysql_free_result(result);
423 rc= mysql_query(mysql, "DROP TABLE IF EXISTS foo_dfr");
424 check_mysql_rc(rc, mysql);
425
426 return OK;
427}
428
429static int test_bug11037(MYSQL *mysql)
430{
431 MYSQL_STMT *stmt;
432 int rc;
433 const char *stmt_text;
434
435 rc= mysql_query(mysql, "drop table if exists t1");
436 check_mysql_rc(rc, mysql);
437
438 rc= mysql_query(mysql, "create table t1 (id int not null)");
439 check_mysql_rc(rc, mysql);
440
441 rc= mysql_query(mysql, "insert into t1 values (1)");
442 check_mysql_rc(rc, mysql);
443
444 stmt_text= "select id FROM t1";
445 stmt= mysql_stmt_init(mysql);
446 FAIL_IF(!stmt, mysql_error(mysql));
447 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
448 check_stmt_rc(rc, stmt);
449
450 /* expected error */
451 rc = mysql_stmt_fetch(stmt);
452 FAIL_UNLESS(rc==1, "Error expedted");
453
454 rc= mysql_stmt_execute(stmt);
455 check_stmt_rc(rc, stmt);
456
457 rc= mysql_stmt_fetch(stmt);
458 check_stmt_rc(rc, stmt);
459
460 rc= mysql_stmt_fetch(stmt);
461 FAIL_UNLESS(rc==MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
462
463 rc= mysql_stmt_fetch(stmt);
464 FAIL_UNLESS(rc==MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
465
466 mysql_stmt_close(stmt);
467 rc= mysql_query(mysql, "drop table t1");
468 check_mysql_rc(rc, mysql);
469
470 return OK;
471}
472
473/* Bug#11183 "mysql_stmt_reset() doesn't reset information about error" */
474
475static int test_bug11183(MYSQL *mysql)
476{
477 int rc;
478 MYSQL_STMT *stmt;
479 char bug_statement[]= "insert into t1 values (1)";
480
481 rc= mysql_query(mysql, "drop table if exists t1");
482 check_mysql_rc(rc, mysql);
483 rc= mysql_query(mysql, "create table t1 (a int)");
484 check_mysql_rc(rc, mysql);
485
486 stmt= mysql_stmt_init(mysql);
487 FAIL_IF(!stmt, mysql_error(mysql));
488
489 rc= mysql_stmt_prepare(stmt, SL(bug_statement));
490 check_stmt_rc(rc, stmt);
491
492 rc= mysql_query(mysql, "drop table t1");
493 check_mysql_rc(rc, mysql);
494
495 /* Trying to execute statement that should fail on execute stage */
496 rc= mysql_stmt_execute(stmt);
497 FAIL_IF(!rc, "Error expected");
498
499 mysql_stmt_reset(stmt);
500 FAIL_IF(mysql_stmt_errno(stmt) != 0, "stmt->error != 0");
501
502 rc= mysql_query(mysql, "create table t1 (a int)");
503 check_mysql_rc(rc, mysql);
504
505 /* Trying to execute statement that should pass ok */
506 if (mysql_stmt_execute(stmt))
507 {
508 mysql_stmt_reset(stmt);
509 FAIL_IF(mysql_stmt_errno(stmt) == 0, "stmt->error != 0");
510 }
511
512 mysql_stmt_close(stmt);
513
514 rc= mysql_query(mysql, "drop table t1");
515 check_mysql_rc(rc, mysql);
516
517 return OK;
518}
519
520static int test_bug12744(MYSQL *mysql)
521{
522 MYSQL_STMT *stmt = NULL;
523 int rc;
524
525 stmt = mysql_stmt_init(mysql);
526 FAIL_IF(!stmt, mysql_error(mysql));
527 rc= mysql_stmt_prepare(stmt, "SET @a:=1", 9);
528 check_stmt_rc(rc, stmt);
529
530 rc= mysql_stmt_execute(stmt);
531 check_stmt_rc(rc, stmt);
532
533 /* set reconnect, kill and ping to reconnect */
534 rc= mysql_query(mysql, "SET @a:=1");
535 check_mysql_rc(rc, mysql);
536 rc= mysql_options(mysql, MYSQL_OPT_RECONNECT, "1");
537 check_mysql_rc(rc, mysql);
538 rc= mysql_kill(mysql, mysql_thread_id(mysql));
539
540 rc= mysql_ping(mysql);
541 check_mysql_rc(rc, mysql);
542
543 rc= mysql_stmt_close(stmt);
544 check_mysql_rc(rc, mysql);
545
546 return OK;
547}
548
549static int test_bug1500(MYSQL *mysql)
550{
551 MYSQL_STMT *stmt;
552 MYSQL_BIND my_bind[3];
553 int rc= 0;
554 int32 int_data[3]= {2, 3, 4};
555 const char *data;
556 const char *query;
557
558
559 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bg1500");
560 check_mysql_rc(rc, mysql);
561
562 rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (i INT)");
563 check_mysql_rc(rc, mysql);
564
565 rc= mysql_query(mysql, "INSERT INTO test_bg1500 VALUES (1), (2)");
566 check_mysql_rc(rc, mysql);
567
568 rc= mysql_commit(mysql);
569 check_mysql_rc(rc, mysql);
570
571 query= "SELECT i FROM test_bg1500 WHERE i IN (?, ?, ?)";
572 stmt= mysql_stmt_init(mysql);
573 FAIL_IF(!stmt, mysql_error(mysql));
574 rc= mysql_stmt_prepare(stmt, SL(query));
575 check_stmt_rc(rc, stmt);
576
577 FAIL_IF(mysql_stmt_param_count(stmt) != 3, "paramcount != 3");
578
579 memset(my_bind, '\0', sizeof(my_bind));
580
581 my_bind[0].buffer= (void *)int_data;
582 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
583 my_bind[2]= my_bind[1]= my_bind[0];
584 my_bind[1].buffer= (void *)(int_data + 1);
585 my_bind[2].buffer= (void *)(int_data + 2);
586
587 rc= mysql_stmt_bind_param(stmt, my_bind);
588 check_stmt_rc(rc, stmt);
589
590 rc= mysql_stmt_execute(stmt);
591 check_stmt_rc(rc, stmt);
592
593 rc= 0;
594 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
595 rc++;
596 FAIL_UNLESS(rc == 1, "rowcount != 1");
597
598 mysql_stmt_close(stmt);
599
600 rc= mysql_query(mysql, "DROP TABLE test_bg1500");
601 check_mysql_rc(rc, mysql);
602
603 rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (s VARCHAR(25), FULLTEXT(s)) engine=MyISAM");
604 check_mysql_rc(rc, mysql);
605
606 rc= mysql_query(mysql,
607 "INSERT INTO test_bg1500 VALUES ('Gravedigger'), ('Greed'), ('Hollow Dogs')");
608 check_mysql_rc(rc, mysql);
609
610 rc= mysql_commit(mysql);
611 check_mysql_rc(rc, mysql);
612
613 query= "SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (?)";
614 stmt= mysql_stmt_init(mysql);
615 FAIL_IF(!stmt, mysql_error(mysql));
616 rc= mysql_stmt_prepare(stmt, SL(query));
617 check_stmt_rc(rc, stmt);
618
619 FAIL_IF(mysql_stmt_param_count(stmt) != 1, "paramcount != 1");
620
621 data= "Dogs";
622 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
623 my_bind[0].buffer= (void *) data;
624 my_bind[0].buffer_length= (unsigned long)strlen(data);
625 my_bind[0].is_null= 0;
626 my_bind[0].length= 0;
627
628 rc= mysql_stmt_bind_param(stmt, my_bind);
629 check_stmt_rc(rc, stmt);
630
631 rc= mysql_stmt_execute(stmt);
632 check_stmt_rc(rc, stmt);
633
634 rc= 0;
635 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
636 rc++;
637 FAIL_UNLESS(rc == 1, "rowcount != 1");
638
639 mysql_stmt_close(stmt);
640
641 /* This should work too */
642 query= "SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (CONCAT(?, 'digger'))";
643 stmt= mysql_stmt_init(mysql);
644 FAIL_IF(!stmt, mysql_error(mysql));
645 rc= mysql_stmt_prepare(stmt, SL(query));
646 check_stmt_rc(rc, stmt);
647
648 FAIL_IF(mysql_stmt_param_count(stmt) != 1, "paramcount != 1");
649
650 data= "Grave";
651 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
652 my_bind[0].buffer= (void *) data;
653 my_bind[0].buffer_length= (unsigned long)strlen(data);
654
655 rc= mysql_stmt_bind_param(stmt, my_bind);
656 check_stmt_rc(rc, stmt);
657
658 rc= mysql_stmt_execute(stmt);
659 check_stmt_rc(rc, stmt);
660
661 rc= 0;
662 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
663 rc++;
664 FAIL_UNLESS(rc == 1, "rowcount != 1");
665
666 mysql_stmt_close(stmt);
667 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bg1500");
668 check_mysql_rc(rc, mysql);
669
670 return OK;
671}
672
673static int test_bug15510(MYSQL *mysql)
674{
675 MYSQL_STMT *stmt;
676 int rc;
677 const char *query= "select 1 from dual where 1/0";
678
679
680 rc= mysql_query(mysql, "set @@sql_mode='ERROR_FOR_DIVISION_BY_ZERO'");
681 check_mysql_rc(rc, mysql);
682
683 stmt= mysql_stmt_init(mysql);
684
685 rc= mysql_stmt_prepare(stmt, SL(query));
686 check_stmt_rc(rc, stmt);
687
688 rc= mysql_stmt_execute(stmt);
689 check_stmt_rc(rc, stmt);
690
691 rc= mysql_stmt_fetch(stmt);
692 FAIL_UNLESS(mysql_warning_count(mysql), "Warning expected");
693
694 /* Cleanup */
695 mysql_stmt_close(stmt);
696 rc= mysql_query(mysql, "set @@sql_mode=''");
697 check_mysql_rc(rc, mysql);
698
699 return OK;
700}
701
702/*
703 Bug #15518 - Reusing a stmt that has failed during prepare
704 does not clear error
705*/
706
707static int test_bug15518(MYSQL *mysql)
708{
709 MYSQL_STMT *stmt;
710 int rc;
711
712 stmt= mysql_stmt_init(mysql);
713
714 /*
715 The prepare of foo should fail with errno 1064 since
716 it's not a valid query
717 */
718 rc= mysql_stmt_prepare(stmt, "foo", 3);
719 FAIL_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql), "Error expected");
720
721 /*
722 Use the same stmt and reprepare with another query that
723 succeeds
724 */
725 rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
726 FAIL_UNLESS(!rc || mysql_stmt_errno(stmt) || mysql_errno(mysql), "Error expected");
727
728 rc= mysql_stmt_close(stmt);
729 check_mysql_rc(rc, mysql);
730 /*
731 part2, when connection to server has been closed
732 after first prepare
733 */
734 stmt= mysql_stmt_init(mysql);
735 rc= mysql_stmt_prepare(stmt, "foo", 3);
736 FAIL_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql), "Error expected");
737
738 /* Close connection to server */
739 mysql_close(mysql);
740
741 /*
742 Use the same stmt and reprepare with another query that
743 succeeds. The prepare should fail with error 2013 since
744 connection to server has been closed.
745 */
746 rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
747 FAIL_UNLESS(rc && mysql_stmt_errno(stmt), "Error expected");
748
749 mysql_stmt_close(stmt);
750
751 return OK;
752}
753
754/*
755 Bug #15613: "libmysqlclient API function mysql_stmt_prepare returns wrong
756 field length"
757*/
758
759static int test_bug15613(MYSQL *mysql)
760{
761 MYSQL_STMT *stmt;
762 const char *stmt_text;
763 MYSQL_RES *metadata;
764 MYSQL_FIELD *field;
765 int rc;
766
767 /* I. Prepare the table */
768 rc= mysql_query(mysql, "set names latin1");
769 check_mysql_rc(rc, mysql);
770 mysql_query(mysql, "drop table if exists t1");
771 rc= mysql_query(mysql,
772 "create table t1 (t text character set utf8, "
773 "tt tinytext character set utf8, "
774 "mt mediumtext character set utf8, "
775 "lt longtext character set utf8, "
776 "vl varchar(255) character set latin1,"
777 "vb varchar(255) character set binary,"
778 "vu varchar(255) character set utf8)");
779 check_mysql_rc(rc, mysql);
780
781 stmt= mysql_stmt_init(mysql);
782
783 /* II. Check SELECT metadata */
784 stmt_text= ("select t, tt, mt, lt, vl, vb, vu from t1");
785 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
786 metadata= mysql_stmt_result_metadata(stmt);
787 field= mysql_fetch_fields(metadata);
788 FAIL_UNLESS(field[0].length == 65535, "length != 65535");
789 FAIL_UNLESS(field[1].length == 255, "length != 244");
790 FAIL_UNLESS(field[2].length == 16777215, "length != 166777215");
791 FAIL_UNLESS(field[3].length == 4294967295UL, "length != 4294967295UL");
792 FAIL_UNLESS(field[4].length == 255, "length != 255");
793 FAIL_UNLESS(field[5].length == 255, "length != 255");
794 FAIL_UNLESS(field[6].length == 255, "length != 255");
795 mysql_free_result(metadata);
796 mysql_stmt_free_result(stmt);
797
798 /* III. Cleanup */
799 rc= mysql_query(mysql, "drop table t1");
800 check_mysql_rc(rc, mysql);
801 rc= mysql_query(mysql, "set names default");
802 check_mysql_rc(rc, mysql);
803 mysql_stmt_close(stmt);
804
805 return OK;
806}
807
808static int test_bug16144(MYSQL *mysql)
809{
810 const my_bool flag_orig= (my_bool) 0xde;
811 my_bool flag= flag_orig;
812 MYSQL_STMT *stmt;
813
814 /* Check that attr_get returns correct data on little and big endian CPUs */
815 stmt= mysql_stmt_init(mysql);
816 mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (const void*) &flag);
817 mysql_stmt_attr_get(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &flag);
818 FAIL_UNLESS(flag == flag_orig, "flag != flag_orig");
819
820 mysql_stmt_close(stmt);
821
822 return OK;
823}
824
825/*
826 This tests for various mysql_stmt_send_long_data bugs described in #1664
827*/
828
829static int test_bug1664(MYSQL *mysql)
830{
831 MYSQL_STMT *stmt;
832 int rc, int_data;
833 const char *data;
834 const char *str_data= "Simple string";
835 MYSQL_BIND my_bind[2];
836 const char *query= "INSERT INTO test_long_data(col2, col1) VALUES(?, ?)";
837
838
839 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
840 check_mysql_rc(rc, mysql);
841
842 rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, col2 long varchar)");
843 check_mysql_rc(rc, mysql);
844
845 stmt= mysql_stmt_init(mysql);
846 rc= mysql_stmt_prepare(stmt, SL(query));
847 check_stmt_rc(rc, stmt);
848
849 FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Param count != 2");
850
851 memset(my_bind, '\0', sizeof(my_bind));
852
853 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
854 my_bind[0].buffer= (void *)str_data;
855 my_bind[0].buffer_length= (unsigned long)strlen(str_data);
856
857 my_bind[1].buffer= (void *)&int_data;
858 my_bind[1].buffer_type= MYSQL_TYPE_LONG;
859
860 rc= mysql_stmt_bind_param(stmt, my_bind);
861 check_stmt_rc(rc, stmt);
862
863 int_data= 1;
864
865 /*
866 Let us supply empty long_data. This should work and should
867 not break following execution.
868 */
869 data= "";
870 rc= mysql_stmt_send_long_data(stmt, 0, SL(data));
871 check_stmt_rc(rc, stmt);
872
873 rc= mysql_stmt_execute(stmt);
874 check_stmt_rc(rc, stmt);
875 if (verify_col_data(mysql, "test_long_data", "col1", "1"))
876 goto error;
877 if (verify_col_data(mysql, "test_long_data", "col2", ""))
878 goto error;
879 rc= mysql_query(mysql, "DELETE FROM test_long_data");
880 check_mysql_rc(rc, mysql);
881
882 /* This should pass OK */
883 data= (char *)"Data";
884 rc= mysql_stmt_send_long_data(stmt, 0, SL(data));
885 check_stmt_rc(rc, stmt);
886
887 rc= mysql_stmt_execute(stmt);
888 check_stmt_rc(rc, stmt);
889
890 if (verify_col_data(mysql, "test_long_data", "col1", "1"))
891 goto error;
892 if (verify_col_data(mysql, "test_long_data", "col2", "Data"))
893 goto error;
894
895 /* clean up */
896 rc= mysql_query(mysql, "DELETE FROM test_long_data");
897 check_mysql_rc(rc, mysql);
898
899 /*
900 Now we are changing int parameter and don't do anything
901 with first parameter. Second mysql_stmt_execute() should run
902 OK treating this first parameter as string parameter.
903 */
904
905 int_data= 2;
906 /* execute */
907 rc= mysql_stmt_execute(stmt);
908 check_stmt_rc(rc, stmt);
909
910 if (verify_col_data(mysql, "test_long_data", "col1", "2"))
911 goto error;
912 if (verify_col_data(mysql, "test_long_data", "col2", str_data))
913 goto error;
914
915 /* clean up */
916 rc= mysql_query(mysql, "DELETE FROM test_long_data");
917 check_mysql_rc(rc, mysql);
918
919 /*
920 Now we are sending other long data. It should not be
921 concatened to previous.
922 */
923
924 data= (char *)"SomeOtherData";
925 rc= mysql_stmt_send_long_data(stmt, 0, SL(data));
926 check_stmt_rc(rc, stmt);
927
928 rc= mysql_stmt_execute(stmt);
929 check_stmt_rc(rc, stmt);
930
931 if (verify_col_data(mysql, "test_long_data", "col1", "2"))
932 goto error;
933 if (verify_col_data(mysql, "test_long_data", "col2", "SomeOtherData"))
934 goto error;
935
936 mysql_stmt_close(stmt);
937
938 /* clean up */
939 rc= mysql_query(mysql, "DELETE FROM test_long_data");
940 check_mysql_rc(rc, mysql);
941
942 /* Now let us test how mysql_stmt_reset works. */
943 stmt= mysql_stmt_init(mysql);
944 rc= mysql_stmt_prepare(stmt, SL(query));
945 check_stmt_rc(rc, stmt);
946 rc= mysql_stmt_bind_param(stmt, my_bind);
947 check_stmt_rc(rc, stmt);
948
949 data= (char *)"SomeData";
950 rc= mysql_stmt_send_long_data(stmt, 0, SL(data));
951 check_stmt_rc(rc, stmt);
952
953 rc= mysql_stmt_reset(stmt);
954 check_stmt_rc(rc, stmt);
955
956 rc= mysql_stmt_execute(stmt);
957 check_stmt_rc(rc, stmt);
958
959 if (verify_col_data(mysql, "test_long_data", "col1", "2"))
960 goto error;
961 if (verify_col_data(mysql, "test_long_data", "col2", str_data))
962 goto error;
963
964 mysql_stmt_close(stmt);
965
966 /* Final clean up */
967 rc= mysql_query(mysql, "DROP TABLE test_long_data");
968 check_mysql_rc(rc, mysql);
969
970 return OK;
971
972error:
973 mysql_stmt_close(stmt);
974 rc= mysql_query(mysql, "DROP TABLE test_long_data");
975 return FAIL;
976}
977/* Test a misc bug */
978
979static int test_ushort_bug(MYSQL *mysql)
980{
981 MYSQL_STMT *stmt;
982 MYSQL_BIND my_bind[4];
983 ushort short_value;
984 uint32 long_value;
985 ulong s_length, l_length, ll_length, t_length;
986 ulonglong longlong_value;
987 int rc;
988 uchar tiny_value;
989 const char *query= "SELECT * FROM test_ushort";
990
991 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ushort");
992 check_mysql_rc(rc, mysql);
993
994 rc= mysql_query(mysql, "CREATE TABLE test_ushort(a smallint unsigned, \
995 b smallint unsigned, \
996 c smallint unsigned, \
997 d smallint unsigned)");
998 check_mysql_rc(rc, mysql);
999
1000 rc= mysql_query(mysql,
1001 "INSERT INTO test_ushort VALUES(35999, 35999, 35999, 200)");
1002 check_mysql_rc(rc, mysql);
1003
1004 stmt= mysql_stmt_init(mysql);
1005 FAIL_IF(!stmt, mysql_error(mysql));
1006 rc= mysql_stmt_prepare(stmt, SL(query));
1007 check_stmt_rc(rc, stmt);
1008
1009 rc= mysql_stmt_execute(stmt);
1010 check_stmt_rc(rc, stmt);
1011
1012 memset(my_bind, '\0', sizeof(my_bind));
1013 my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
1014 my_bind[0].buffer= (void *)&short_value;
1015 my_bind[0].is_unsigned= TRUE;
1016 my_bind[0].length= &s_length;
1017
1018 my_bind[1].buffer_type= MYSQL_TYPE_LONG;
1019 my_bind[1].buffer= (void *)&long_value;
1020 my_bind[1].length= &l_length;
1021
1022 my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
1023 my_bind[2].buffer= (void *)&longlong_value;
1024 my_bind[2].length= &ll_length;
1025
1026 my_bind[3].buffer_type= MYSQL_TYPE_TINY;
1027 my_bind[3].buffer= (void *)&tiny_value;
1028 my_bind[3].is_unsigned= TRUE;
1029 my_bind[3].length= &t_length;
1030
1031 rc= mysql_stmt_bind_result(stmt, my_bind);
1032 check_stmt_rc(rc, stmt);
1033
1034 rc= mysql_stmt_fetch(stmt);
1035 check_stmt_rc(rc, stmt);
1036
1037 FAIL_UNLESS(short_value == 35999, "short_value != 35999");
1038 FAIL_UNLESS(s_length == 2, "length != 2");
1039
1040 FAIL_UNLESS(long_value == 35999, "long_value != 35999");
1041 FAIL_UNLESS(l_length == 4, "length != 4");
1042
1043 FAIL_UNLESS(longlong_value == 35999, "longlong_value != 35999");
1044 FAIL_UNLESS(ll_length == 8, "length != 8");
1045
1046 FAIL_UNLESS(tiny_value == 200, "tiny_value != 200");
1047 FAIL_UNLESS(t_length == 1, "length != 1");
1048
1049 rc= mysql_stmt_fetch(stmt);
1050 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
1051
1052 mysql_stmt_close(stmt);
1053 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ushort");
1054 check_mysql_rc(rc, mysql);
1055
1056 return OK;
1057}
1058
1059static int test_bug1946(MYSQL *mysql)
1060{
1061 MYSQL_STMT *stmt;
1062 int rc;
1063 const char *query= "INSERT INTO prepare_command VALUES (?)";
1064
1065
1066 rc= mysql_query(mysql, "DROP TABLE IF EXISTS prepare_command");
1067 check_mysql_rc(rc, mysql)
1068
1069 rc= mysql_query(mysql, "CREATE TABLE prepare_command(ID INT)");
1070 check_mysql_rc(rc, mysql)
1071
1072 stmt= mysql_stmt_init(mysql);
1073 FAIL_IF(!stmt, mysql_error(mysql));
1074 rc= mysql_stmt_prepare(stmt, SL(query));
1075 check_stmt_rc(rc, stmt);
1076
1077 rc= mysql_real_query(mysql, SL(query));
1078 FAIL_IF(!rc, "Error expected");
1079
1080 mysql_stmt_close(stmt);
1081 rc= mysql_query(mysql, "DROP TABLE prepare_command");
1082 check_mysql_rc(rc, mysql);
1083 return OK;
1084}
1085
1086static int test_bug20152(MYSQL *mysql)
1087{
1088 MYSQL_BIND my_bind[1];
1089 MYSQL_STMT *stmt;
1090 MYSQL_TIME tm;
1091 int rc;
1092 const char *query= "INSERT INTO t1 (f1) VALUES (?)";
1093
1094
1095 memset(my_bind, '\0', sizeof(my_bind));
1096 my_bind[0].buffer_type= MYSQL_TYPE_DATE;
1097 my_bind[0].buffer= (void*)&tm;
1098
1099 memset(&tm, 0, sizeof(MYSQL_TIME));
1100
1101 tm.year = 2006;
1102 tm.month = 6;
1103 tm.day = 18;
1104 tm.hour = 14;
1105 tm.minute = 9;
1106 tm.second = 42;
1107
1108 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1109 check_mysql_rc(rc, mysql)
1110 rc= mysql_query(mysql, "CREATE TABLE t1 (f1 DATE)");
1111 check_mysql_rc(rc, mysql)
1112
1113 stmt= mysql_stmt_init(mysql);
1114 rc= mysql_stmt_prepare(stmt, SL(query));
1115 check_stmt_rc(rc, stmt);
1116 rc= mysql_stmt_bind_param(stmt, my_bind);
1117 check_stmt_rc(rc, stmt);
1118 rc= mysql_stmt_execute(stmt);
1119 check_stmt_rc(rc, stmt);
1120 rc= mysql_stmt_close(stmt);
1121 check_stmt_rc(rc, stmt);
1122 rc= mysql_query(mysql, "DROP TABLE t1");
1123 check_mysql_rc(rc, mysql)
1124 FAIL_UNLESS(tm.hour == 14 && tm.minute == 9 && tm.second == 42, "time != 14:09:42");
1125 return OK;
1126}
1127
1128static int test_bug2247(MYSQL *mysql)
1129{
1130 MYSQL_STMT *stmt;
1131 MYSQL_RES *res;
1132 int rc;
1133 int i;
1134 const char *create= "CREATE TABLE bug2247(id INT UNIQUE AUTO_INCREMENT)";
1135 const char *insert= "INSERT INTO bug2247 VALUES (NULL)";
1136 const char *SELECT= "SELECT id FROM bug2247";
1137 const char *update= "UPDATE bug2247 SET id=id+10";
1138 const char *drop= "DROP TABLE IF EXISTS bug2247";
1139 ulonglong exp_count;
1140 enum { NUM_ROWS= 5 };
1141
1142
1143 /* create table and insert few rows */
1144 rc= mysql_query(mysql, drop);
1145 check_mysql_rc(rc, mysql)
1146
1147 rc= mysql_query(mysql, create);
1148 check_mysql_rc(rc, mysql)
1149
1150 stmt= mysql_stmt_init(mysql);
1151 FAIL_IF(!stmt, mysql_error(mysql));
1152 rc= mysql_stmt_prepare(stmt, SL(insert));
1153 check_stmt_rc(rc, stmt);
1154 for (i= 0; i < NUM_ROWS; ++i)
1155 {
1156 rc= mysql_stmt_execute(stmt);
1157 check_stmt_rc(rc, stmt);
1158 }
1159 exp_count= mysql_stmt_affected_rows(stmt);
1160 FAIL_UNLESS(exp_count == 1, "exp_count != 1");
1161
1162 rc= mysql_query(mysql, SELECT);
1163 check_mysql_rc(rc, mysql)
1164 /*
1165 mysql_store_result overwrites mysql->affected_rows. Check that
1166 mysql_stmt_affected_rows() returns the same value, whereas
1167 mysql_affected_rows() value is correct.
1168 */
1169 res= mysql_store_result(mysql);
1170 FAIL_IF(!res, "Invalid result set");
1171
1172 FAIL_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS, "affected_rows != NUM_ROWS");
1173 FAIL_UNLESS(exp_count == mysql_stmt_affected_rows(stmt), "affected_rows != exp_count");
1174
1175 rc= mysql_query(mysql, update);
1176 check_mysql_rc(rc, mysql)
1177 FAIL_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS, "affected_rows != NUM_ROWS");
1178 FAIL_UNLESS(exp_count == mysql_stmt_affected_rows(stmt), "affected_rows != exp_count");
1179
1180 mysql_free_result(res);
1181 mysql_stmt_close(stmt);
1182
1183 /* check that mysql_stmt_store_result modifies mysql_stmt_affected_rows */
1184 stmt= mysql_stmt_init(mysql);
1185 FAIL_IF(!stmt, mysql_error(mysql));
1186 rc= mysql_stmt_prepare(stmt, SL(SELECT));
1187 check_stmt_rc(rc, stmt);
1188
1189 rc= mysql_stmt_execute(stmt);
1190 check_stmt_rc(rc, stmt); rc= mysql_stmt_store_result(stmt);
1191 check_stmt_rc(rc, stmt); exp_count= mysql_stmt_affected_rows(stmt);
1192 FAIL_UNLESS(exp_count == NUM_ROWS, "exp_count != NUM_ROWS");
1193
1194 rc= mysql_query(mysql, insert);
1195 check_mysql_rc(rc, mysql)
1196 FAIL_UNLESS(mysql_affected_rows(mysql) == 1, "affected_rows != 1");
1197 FAIL_UNLESS(exp_count == mysql_stmt_affected_rows(stmt), "affected_rows != exp_count");
1198
1199 mysql_stmt_close(stmt);
1200 rc= mysql_query(mysql, drop);
1201 check_mysql_rc(rc, mysql)
1202 return OK;
1203}
1204
1205/*
1206 Test for bug#2248 "mysql_fetch without prior mysql_stmt_execute hangs"
1207*/
1208
1209static int test_bug2248(MYSQL *mysql)
1210{
1211 MYSQL_STMT *stmt;
1212 int rc;
1213 const char *query1= "SELECT DATABASE()";
1214 const char *query2= "INSERT INTO test_bug2248 VALUES (10)";
1215
1216
1217 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bug2248");
1218 check_mysql_rc(rc, mysql)
1219
1220 rc= mysql_query(mysql, "CREATE TABLE test_bug2248 (id int)");
1221 check_mysql_rc(rc, mysql)
1222
1223 stmt= mysql_stmt_init(mysql);
1224 FAIL_IF(!stmt, mysql_error(mysql));
1225 rc= mysql_stmt_prepare(stmt, SL(query1));
1226 check_stmt_rc(rc, stmt);
1227
1228 /* This should not hang */
1229 rc= mysql_stmt_fetch(stmt);
1230 FAIL_IF(!rc, "Error expected");
1231
1232 /* And this too */
1233 rc= mysql_stmt_store_result(stmt);
1234 FAIL_IF(!rc, "Error expected");
1235
1236 mysql_stmt_close(stmt);
1237
1238 stmt= mysql_stmt_init(mysql);
1239 FAIL_IF(!stmt, mysql_error(mysql));
1240 rc= mysql_stmt_prepare(stmt, SL(query2));
1241 check_stmt_rc(rc, stmt);
1242
1243 rc= mysql_stmt_execute(stmt);
1244 check_stmt_rc(rc, stmt);
1245 /* This too should not hang but should return proper error */
1246 rc= mysql_stmt_fetch(stmt);
1247 FAIL_UNLESS(rc == 1, "rc != 1");
1248
1249 /* This too should not hang but should not bark */
1250 rc= mysql_stmt_store_result(stmt);
1251 check_stmt_rc(rc, stmt);
1252 /* This should return proper error */
1253 rc= mysql_stmt_fetch(stmt);
1254 FAIL_UNLESS(rc == 1, "rc != 1");
1255
1256 mysql_stmt_close(stmt);
1257
1258 rc= mysql_query(mysql, "DROP TABLE test_bug2248");
1259 check_mysql_rc(rc, mysql)
1260 return OK;
1261}
1262
1263/*
1264 BUG#23383: mysql_affected_rows() returns different values than
1265 mysql_stmt_affected_rows()
1266
1267 Test that both mysql_affected_rows() and mysql_stmt_affected_rows()
1268 return -1 on error, 0 when no rows were affected, and (positive) row
1269 count when some rows were affected.
1270*/
1271static int test_bug23383(MYSQL *mysql)
1272{
1273 const char *insert_query= "INSERT INTO t1 VALUES (1), (2)";
1274 const char *update_query= "UPDATE t1 SET i= 4 WHERE i = 3";
1275 MYSQL_STMT *stmt;
1276 unsigned long long row_count;
1277 int rc;
1278
1279 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1280 check_mysql_rc(rc, mysql);
1281
1282 rc= mysql_query(mysql, "CREATE TABLE t1 (i INT UNIQUE)");
1283 check_mysql_rc(rc, mysql);
1284
1285 rc= mysql_query(mysql, insert_query);
1286 check_mysql_rc(rc, mysql);
1287 row_count= mysql_affected_rows(mysql);
1288 FAIL_UNLESS(row_count == 2, "row_count != 2");
1289
1290 rc= mysql_query(mysql, insert_query);
1291 FAIL_IF(!rc, "Error expected");
1292 row_count= mysql_affected_rows(mysql);
1293 FAIL_UNLESS(row_count == (unsigned long long)-1, "rowcount != -1");
1294
1295 rc= mysql_query(mysql, update_query);
1296 check_mysql_rc(rc, mysql);
1297 row_count= mysql_affected_rows(mysql);
1298 FAIL_UNLESS(row_count == 0, "");
1299
1300 rc= mysql_query(mysql, "DELETE FROM t1");
1301 check_mysql_rc(rc, mysql);
1302
1303 stmt= mysql_stmt_init(mysql);
1304 FAIL_IF(!stmt, mysql_error(mysql));
1305
1306 rc= mysql_stmt_prepare(stmt, SL(insert_query));
1307 check_stmt_rc(rc, stmt);
1308 rc= mysql_stmt_execute(stmt);
1309 check_stmt_rc(rc, stmt);
1310 row_count= mysql_stmt_affected_rows(stmt);
1311 FAIL_UNLESS(row_count == 2, "row_count != 2");
1312
1313 rc= mysql_stmt_execute(stmt);
1314 FAIL_UNLESS(rc != 0, "");
1315 row_count= mysql_stmt_affected_rows(stmt);
1316 FAIL_UNLESS(row_count == (unsigned long long)-1, "rowcount != -1");
1317
1318 rc= mysql_stmt_prepare(stmt, SL(update_query));
1319 check_stmt_rc(rc, stmt);
1320 rc= mysql_stmt_execute(stmt);
1321 check_stmt_rc(rc, stmt);
1322 row_count= mysql_stmt_affected_rows(stmt);
1323 FAIL_UNLESS(row_count == 0, "rowcount != 0");
1324
1325 rc= mysql_stmt_close(stmt);
1326 check_stmt_rc(rc, stmt);
1327 rc= mysql_query(mysql, "DROP TABLE t1");
1328 check_mysql_rc(rc, mysql);
1329
1330 return OK;
1331}
1332
1333/*
1334 Bug#27592 (stack overrun when storing datetime value using prepared statements)
1335*/
1336
1337static int test_bug27592(MYSQL *mysql)
1338{
1339 const int NUM_ITERATIONS= 40;
1340 int i;
1341 int rc;
1342 MYSQL_STMT *stmt= NULL;
1343 MYSQL_BIND bind[1];
1344 MYSQL_TIME time_val;
1345
1346 mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1347 mysql_query(mysql, "CREATE TABLE t1(c2 DATETIME)");
1348
1349 stmt= mysql_stmt_init(mysql);
1350 FAIL_IF(!stmt, mysql_error(mysql));
1351 rc= mysql_stmt_prepare(stmt, SL("INSERT INTO t1 VALUES (?)"));
1352 check_stmt_rc(rc, stmt);
1353
1354 memset(bind, '\0', sizeof(bind));
1355
1356 bind[0].buffer_type= MYSQL_TYPE_DATETIME;
1357 bind[0].buffer= (char *) &time_val;
1358 bind[0].length= NULL;
1359
1360 for (i= 0; i < NUM_ITERATIONS; i++)
1361 {
1362 time_val.year= 2007;
1363 time_val.month= 6;
1364 time_val.day= 7;
1365 time_val.hour= 18;
1366 time_val.minute= 41;
1367 time_val.second= 3;
1368
1369 time_val.second_part=0;
1370 time_val.neg=0;
1371
1372 rc= mysql_stmt_bind_param(stmt, bind);
1373 check_stmt_rc(rc, stmt);
1374 rc= mysql_stmt_execute(stmt);
1375 check_stmt_rc(rc, stmt);
1376 }
1377
1378 mysql_stmt_close(stmt);
1379 mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1380
1381 return OK;
1382}
1383
1384/*
1385 Bug#28934: server crash when receiving malformed com_execute packets
1386*/
1387
1388static int test_bug28934(MYSQL *mysql)
1389{
1390 my_bool error= 0;
1391 MYSQL_BIND bind[5];
1392 MYSQL_STMT *stmt;
1393 int rc, cnt;
1394
1395 rc= mysql_query(mysql, "drop table if exists t1");
1396 check_mysql_rc(rc, mysql);
1397 rc= mysql_query(mysql, "create table t1(id int)");
1398 check_mysql_rc(rc, mysql);
1399
1400 rc= mysql_query(mysql, "insert into t1 values(1),(2),(3),(4),(5)");
1401 check_mysql_rc(rc, mysql);
1402
1403 stmt= mysql_stmt_init(mysql);
1404 FAIL_IF(!stmt, mysql_error(mysql));
1405 rc= mysql_stmt_prepare(stmt, SL("select * from t1 where id in(?,?,?,?,?)"));
1406 check_stmt_rc(rc, stmt);
1407
1408 memset (&bind, '\0', sizeof (bind));
1409 for (cnt= 0; cnt < 5; cnt++)
1410 {
1411 bind[cnt].buffer_type= MYSQL_TYPE_LONG;
1412 bind[cnt].buffer= (char*)&cnt;
1413 bind[cnt].buffer_length= 0;
1414 }
1415 rc= mysql_stmt_bind_param(stmt, bind);
1416 check_stmt_rc(rc, stmt);
1417
1418 stmt->param_count=2;
1419 error= mysql_stmt_execute(stmt);
1420 FAIL_UNLESS(error != 0, "Error expected");
1421 mysql_stmt_close(stmt);
1422
1423 rc= mysql_query(mysql, "drop table t1");
1424 check_mysql_rc(rc, mysql);
1425 return OK;
1426}
1427
1428static int test_bug3035(MYSQL *mysql)
1429{
1430 MYSQL_STMT *stmt;
1431 int rc;
1432 MYSQL_BIND bind_array[12], *my_bind= bind_array, *bind_end= my_bind + 12;
1433 int8 int8_val;
1434 uint8 uint8_val;
1435 int16 int16_val;
1436 uint16 uint16_val;
1437 int32 int32_val;
1438 uint32 uint32_val;
1439 longlong int64_val;
1440 ulonglong uint64_val;
1441 double double_val, udouble_val, double_tmp;
1442 char longlong_as_string[22], ulonglong_as_string[22];
1443
1444 /* mins and maxes */
1445 const int8 int8_min= -128;
1446 const int8 int8_max= 127;
1447 const uint8 uint8_min= 0;
1448 const uint8 uint8_max= 255;
1449
1450 const int16 int16_min= -32768;
1451 const int16 int16_max= 32767;
1452 const uint16 uint16_min= 0;
1453 const uint16 uint16_max= 65535;
1454
1455 const int32 int32_max= 2147483647L;
1456 const int32 int32_min= -int32_max - 1;
1457 const uint32 uint32_min= 0;
1458 const uint32 uint32_max= 4294967295U;
1459
1460 /* it might not work okay everyplace */
1461 const longlong int64_max= 9223372036854775807LL;
1462 const longlong int64_min= -int64_max - 1;
1463
1464 const ulonglong uint64_min= 0U;
1465 const ulonglong uint64_max= 18446744073709551615ULL;
1466
1467 const char *stmt_text;
1468
1469
1470 stmt_text= "DROP TABLE IF EXISTS t1";
1471 rc= mysql_real_query(mysql, SL(stmt_text));
1472 check_mysql_rc(rc, mysql);
1473
1474 stmt_text= "CREATE TABLE t1 (i8 TINYINT, ui8 TINYINT UNSIGNED, "
1475 "i16 SMALLINT, ui16 SMALLINT UNSIGNED, "
1476 "i32 INT, ui32 INT UNSIGNED, "
1477 "i64 BIGINT, ui64 BIGINT UNSIGNED, "
1478 "id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT)";
1479 rc= mysql_real_query(mysql, SL(stmt_text));
1480 check_mysql_rc(rc, mysql);
1481
1482 memset(bind_array, '\0', sizeof(bind_array));
1483 for (my_bind= bind_array; my_bind < bind_end; my_bind++)
1484 my_bind->error= &my_bind->error_value;
1485
1486 bind_array[0].buffer_type= MYSQL_TYPE_TINY;
1487 bind_array[0].buffer= (void *) &int8_val;
1488
1489 bind_array[1].buffer_type= MYSQL_TYPE_TINY;
1490 bind_array[1].buffer= (void *) &uint8_val;
1491 bind_array[1].is_unsigned= 1;
1492
1493 bind_array[2].buffer_type= MYSQL_TYPE_SHORT;
1494 bind_array[2].buffer= (void *) &int16_val;
1495
1496 bind_array[3].buffer_type= MYSQL_TYPE_SHORT;
1497 bind_array[3].buffer= (void *) &uint16_val;
1498 bind_array[3].is_unsigned= 1;
1499
1500 bind_array[4].buffer_type= MYSQL_TYPE_LONG;
1501 bind_array[4].buffer= (void *) &int32_val;
1502
1503 bind_array[5].buffer_type= MYSQL_TYPE_LONG;
1504 bind_array[5].buffer= (void *) &uint32_val;
1505 bind_array[5].is_unsigned= 1;
1506
1507 bind_array[6].buffer_type= MYSQL_TYPE_LONGLONG;
1508 bind_array[6].buffer= (void *) &int64_val;
1509
1510 bind_array[7].buffer_type= MYSQL_TYPE_LONGLONG;
1511 bind_array[7].buffer= (void *) &uint64_val;
1512 bind_array[7].is_unsigned= 1;
1513
1514 stmt= mysql_stmt_init(mysql);
1515 check_stmt_rc(rc, stmt);
1516
1517 stmt_text= "INSERT INTO t1 (i8, ui8, i16, ui16, i32, ui32, i64, ui64) "
1518 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
1519 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
1520 check_stmt_rc(rc, stmt);
1521 mysql_stmt_bind_param(stmt, bind_array);
1522
1523 int8_val= int8_min;
1524 uint8_val= uint8_min;
1525 int16_val= int16_min;
1526 uint16_val= uint16_min;
1527 int32_val= int32_min;
1528 uint32_val= uint32_min;
1529 int64_val= int64_min;
1530 uint64_val= uint64_min;
1531
1532 rc= mysql_stmt_execute(stmt);
1533 check_stmt_rc(rc, stmt);
1534 int8_val= int8_max;
1535 uint8_val= uint8_max;
1536 int16_val= int16_max;
1537 uint16_val= uint16_max;
1538 int32_val= int32_max;
1539 uint32_val= uint32_max;
1540 int64_val= int64_max;
1541 uint64_val= uint64_max;
1542
1543 rc= mysql_stmt_execute(stmt);
1544 check_stmt_rc(rc, stmt);
1545 stmt_text= "SELECT i8, ui8, i16, ui16, i32, ui32, i64, ui64, ui64, "
1546 "cast(ui64 as signed), ui64, cast(ui64 as signed)"
1547 "FROM t1 ORDER BY id ASC";
1548
1549 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
1550 check_stmt_rc(rc, stmt);
1551 rc= mysql_stmt_execute(stmt);
1552 check_stmt_rc(rc, stmt);
1553 bind_array[8].buffer_type= MYSQL_TYPE_DOUBLE;
1554 bind_array[8].buffer= (void *) &udouble_val;
1555
1556 bind_array[9].buffer_type= MYSQL_TYPE_DOUBLE;
1557 bind_array[9].buffer= (void *) &double_val;
1558
1559 bind_array[10].buffer_type= MYSQL_TYPE_STRING;
1560 bind_array[10].buffer= (void *) &ulonglong_as_string;
1561 bind_array[10].buffer_length= sizeof(ulonglong_as_string);
1562
1563 bind_array[11].buffer_type= MYSQL_TYPE_STRING;
1564 bind_array[11].buffer= (void *) &longlong_as_string;
1565 bind_array[11].buffer_length= sizeof(longlong_as_string);
1566
1567 mysql_stmt_bind_result(stmt, bind_array);
1568
1569 rc= mysql_stmt_fetch(stmt);
1570 check_stmt_rc(rc, stmt);
1571 FAIL_UNLESS(int8_val == int8_min, "int8_val != int8_min");
1572 FAIL_UNLESS(uint8_val == uint8_min, "uint8_val != uint8_min");
1573 FAIL_UNLESS(int16_val == int16_min, "int16_val != int16_min");
1574 FAIL_UNLESS(uint16_val == uint16_min, "uint16_val != uint16_min");
1575 FAIL_UNLESS(int32_val == int32_min, "int32_val != int32_min");
1576 FAIL_UNLESS(uint32_val == uint32_min, "uint32_val != uint32_min");
1577 FAIL_UNLESS(int64_val == int64_min, "int64_val != int64_min");
1578 FAIL_UNLESS(uint64_val == uint64_min, "uint64_val != uint64_min");
1579 FAIL_UNLESS(double_val == (longlong) uint64_min, "double_val != uint64_min");
1580 double_tmp= ulonglong2double(uint64_val);
1581 FAIL_UNLESS(cmp_double(&udouble_val,&double_tmp), "udouble_val != double_tmp");
1582 FAIL_UNLESS(!strcmp(longlong_as_string, "0"), "longlong_as_string != '0'");
1583 FAIL_UNLESS(!strcmp(ulonglong_as_string, "0"), "ulonglong_as_string != '0'");
1584
1585 rc= mysql_stmt_fetch(stmt);
1586
1587 FAIL_UNLESS(rc == MYSQL_DATA_TRUNCATED || rc == 0, "rc != 0,MYSQL_DATA_TRUNCATED");
1588
1589 FAIL_UNLESS(int8_val == int8_max, "int8_val != int8_max");
1590 FAIL_UNLESS(uint8_val == uint8_max, "uint8_val != uint8_max");
1591 FAIL_UNLESS(int16_val == int16_max, "int16_val != int16_max");
1592 FAIL_UNLESS(uint16_val == uint16_max, "uint16_val != uint16_max");
1593 FAIL_UNLESS(int32_val == int32_max, "int32_val != int32_max");
1594 FAIL_UNLESS(uint32_val == uint32_max, "uint32_val != uint32_max");
1595 FAIL_UNLESS(int64_val == int64_max, "int64_val != int64_max");
1596 FAIL_UNLESS(uint64_val == uint64_max, "uint64_val != uint64_max");
1597 FAIL_UNLESS(double_val == (longlong) uint64_val, "double_val != uint64_val");
1598 double_tmp= ulonglong2double(uint64_val);
1599 FAIL_UNLESS(cmp_double(&udouble_val,&double_tmp), "udouble_val != double_tmp");
1600 FAIL_UNLESS(!strcmp(longlong_as_string, "-1"), "longlong_as_string != '-1'");
1601 FAIL_UNLESS(!strcmp(ulonglong_as_string, "18446744073709551615"), "ulonglong_as_string != '18446744073709551615'");
1602
1603 rc= mysql_stmt_fetch(stmt);
1604 FAIL_UNLESS(rc == MYSQL_NO_DATA, "");
1605
1606 mysql_stmt_close(stmt);
1607
1608 stmt_text= "DROP TABLE t1";
1609 mysql_real_query(mysql, SL(stmt_text));
1610 return OK;
1611}
1612
1613/*
1614 Test for BUG#3420 ("select id1, value1 from t where id= ? or value= ?"
1615 returns all rows in the table)
1616*/
1617
1618static int test_ps_conj_select(MYSQL *mysql)
1619{
1620 MYSQL_STMT *stmt;
1621 int rc;
1622 MYSQL_BIND my_bind[2];
1623 int32 int_data;
1624 char str_data[32];
1625 unsigned long str_length;
1626 char query[MAX_TEST_QUERY_LENGTH];
1627
1628 rc= mysql_query(mysql, "drop table if exists t1");
1629 check_mysql_rc(rc, mysql);
1630
1631 rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
1632 "value2 varchar(100), value1 varchar(100))");
1633 check_mysql_rc(rc, mysql);
1634
1635 rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
1636 "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
1637 check_mysql_rc(rc, mysql);
1638
1639 strcpy(query, "select id1, value1 from t1 where id1= ? or "
1640 "CONVERT(value1 USING utf8)= ?");
1641 stmt= mysql_stmt_init(mysql);
1642 FAIL_IF(!stmt, mysql_error(mysql));
1643 rc= mysql_stmt_prepare(stmt, SL(query));
1644 check_stmt_rc(rc, stmt);
1645
1646 FAIL_IF(mysql_stmt_param_count(stmt) != 2, "param_count != 2");
1647
1648 /* Always bzero all members of bind parameter */
1649 memset(my_bind, '\0', sizeof(my_bind));
1650 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1651 my_bind[0].buffer= (void *)&int_data;
1652
1653 my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
1654 my_bind[1].buffer= (void *)str_data;
1655 my_bind[1].buffer_length= array_elements(str_data);
1656 my_bind[1].length= &str_length;
1657
1658 rc= mysql_stmt_bind_param(stmt, my_bind);
1659 check_stmt_rc(rc, stmt);
1660 int_data= 1;
1661 strcpy(str_data, "hh");
1662 str_length= (unsigned long)strlen(str_data);
1663
1664 rc= mysql_stmt_execute(stmt);
1665 check_stmt_rc(rc, stmt);
1666
1667 rc=0;
1668 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
1669 rc++;
1670 FAIL_UNLESS(rc == 3, "rc != 3");
1671
1672 mysql_stmt_close(stmt);
1673 rc= mysql_query(mysql, "drop table if exists t1");
1674 check_mysql_rc(rc, mysql);
1675 return OK;
1676}
1677
1678/* Test for NULL as PS parameter (BUG#3367, BUG#3371) */
1679
1680static int test_ps_null_param(MYSQL *mysql)
1681{
1682 MYSQL_STMT *stmt;
1683 int rc;
1684
1685 MYSQL_BIND in_bind;
1686 my_bool in_is_null;
1687 long int in_long;
1688
1689 MYSQL_BIND out_bind;
1690 ulong out_length;
1691 my_bool out_is_null;
1692 char out_str_data[20];
1693
1694 const char *queries[]= {"select ?", "select ?+1",
1695 "select col1 from test_ps_nulls where col1 <=> ?",
1696 NULL
1697 };
1698 const char **cur_query= queries;
1699
1700
1701 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ps_nulls");
1702 check_mysql_rc(rc, mysql);
1703
1704 rc= mysql_query(mysql, "CREATE TABLE test_ps_nulls(col1 int)");
1705 check_mysql_rc(rc, mysql);
1706
1707 rc= mysql_query(mysql, "INSERT INTO test_ps_nulls values (1), (null)");
1708 check_mysql_rc(rc, mysql);
1709
1710 /* Always bzero all members of bind parameter */
1711 memset(&in_bind, '\0', sizeof(in_bind));
1712 memset(&out_bind, '\0', sizeof(out_bind));
1713 in_bind.buffer_type= MYSQL_TYPE_LONG;
1714 in_bind.is_null= &in_is_null;
1715 in_bind.length= 0;
1716 in_bind.buffer= (void *)&in_long;
1717 in_is_null= 1;
1718 in_long= 1;
1719
1720 out_bind.buffer_type= MYSQL_TYPE_STRING;
1721 out_bind.is_null= &out_is_null;
1722 out_bind.length= &out_length;
1723 out_bind.buffer= out_str_data;
1724 out_bind.buffer_length= array_elements(out_str_data);
1725
1726 /* Execute several queries, all returning NULL in result. */
1727 for(cur_query= queries; *cur_query; cur_query++)
1728 {
1729 char query[MAX_TEST_QUERY_LENGTH];
1730 strcpy(query, *cur_query);
1731 stmt= mysql_stmt_init(mysql);
1732 FAIL_IF(!stmt, mysql_error(mysql));
1733 rc= mysql_stmt_prepare(stmt, SL(query));
1734 diag("statement: %s", query);
1735 check_stmt_rc(rc, stmt);
1736 FAIL_IF(mysql_stmt_param_count(stmt) != 1, "param_count != 1");
1737
1738 rc= mysql_stmt_bind_param(stmt, &in_bind);
1739 check_stmt_rc(rc, stmt);
1740 rc= mysql_stmt_bind_result(stmt, &out_bind);
1741 check_stmt_rc(rc, stmt);
1742 rc= mysql_stmt_execute(stmt);
1743 check_stmt_rc(rc, stmt);
1744 rc= mysql_stmt_fetch(stmt);
1745 FAIL_UNLESS(rc != MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
1746 FAIL_UNLESS(out_is_null, "!out_is_null");
1747 rc= mysql_stmt_fetch(stmt);
1748 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
1749 mysql_stmt_close(stmt);
1750 }
1751 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ps_nulls");
1752 check_mysql_rc(rc, mysql);
1753 return OK;
1754}
1755
1756/*
1757 utility for the next test; expects 3 rows in the result from a SELECT,
1758 compares each row/field with an expected value.
1759 */
1760#define test_ps_query_cache_result(i1,s1,l1,i2,s2,l2,i3,s3,l3) \
1761 r_metadata= mysql_stmt_result_metadata(stmt); \
1762 FAIL_UNLESS(r_metadata != NULL, ""); \
1763 rc= mysql_stmt_fetch(stmt); \
1764 check_stmt_rc(rc,stmt); \
1765 FAIL_UNLESS((r_int_data == i1) && (r_str_length == l1) && \
1766 (strcmp(r_str_data, s1) == 0), "test_ps_query_cache_result failure"); \
1767 rc= mysql_stmt_fetch(stmt); \
1768 check_stmt_rc(rc,stmt); \
1769 FAIL_UNLESS((r_int_data == i2) && (r_str_length == l2) && \
1770 (strcmp(r_str_data, s2) == 0), "test_ps_query_cache_result failure"); \
1771 rc= mysql_stmt_fetch(stmt); \
1772 check_stmt_rc(rc,stmt); \
1773 FAIL_UNLESS((r_int_data == i3) && (r_str_length == l3) && \
1774 (strcmp(r_str_data, s3) == 0), "test_ps_query_cache_result failure"); \
1775 rc= mysql_stmt_fetch(stmt); \
1776 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA"); \
1777 mysql_free_result(r_metadata);
1778
1779/* reads Qcache_hits from server and returns its value */
1780static int query_cache_hits(MYSQL *mysql)
1781{
1782 MYSQL_RES *res;
1783 MYSQL_ROW row;
1784 int rc;
1785 uint result;
1786
1787 rc= mysql_query(mysql, "show status like 'qcache_hits'");
1788 check_mysql_rc(rc, mysql);
1789 res= mysql_use_result(mysql);
1790
1791 row= mysql_fetch_row(res);
1792
1793 result= atoi(row[1]);
1794 mysql_free_result(res);
1795 return result;
1796}
1797
1798
1799/*
1800 Test that prepared statements make use of the query cache just as normal
1801 statements (BUG#735).
1802*/
1803static int test_ps_query_cache(MYSQL *mysql)
1804{
1805 MYSQL *lmysql= mysql;
1806 MYSQL_STMT *stmt;
1807 int rc;
1808 MYSQL_BIND p_bind[2],r_bind[2]; /* p: param bind; r: result bind */
1809 int32 p_int_data, r_int_data;
1810 char p_str_data[32], r_str_data[32];
1811 unsigned long p_str_length, r_str_length;
1812 MYSQL_RES *r_metadata;
1813 char query[MAX_TEST_QUERY_LENGTH];
1814 uint hits1, hits2;
1815 enum enum_test_ps_query_cache
1816 {
1817 /*
1818 We iterate the same prepare/executes block, but have iterations where
1819 we vary the query cache conditions.
1820 */
1821 /* the query cache is enabled for the duration of prep&execs: */
1822 TEST_QCACHE_ON= 0,
1823 /*
1824 same but using a new connection (to see if qcache serves results from
1825 the previous connection as it should):
1826 */
1827 TEST_QCACHE_ON_WITH_OTHER_CONN,
1828 /*
1829 First border case: disables the query cache before prepare and
1830 re-enables it before execution (to test if we have no bug then):
1831 */
1832 TEST_QCACHE_OFF_ON,
1833 /*
1834 Second border case: enables the query cache before prepare and
1835 disables it before execution:
1836 */
1837 TEST_QCACHE_ON_OFF
1838 };
1839 enum enum_test_ps_query_cache iteration;
1840
1841 diag("test needs to be fixed");
1842 return SKIP;
1843 /* prepare the table */
1844
1845 rc= mysql_query(mysql, "drop table if exists t1");
1846 check_mysql_rc(rc, mysql);
1847
1848 rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
1849 "value2 varchar(100), value1 varchar(100))");
1850 check_mysql_rc(rc, mysql);
1851
1852 rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
1853 "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
1854 check_mysql_rc(rc, mysql);
1855
1856 for (iteration= TEST_QCACHE_ON; iteration <= TEST_QCACHE_ON_OFF; iteration++)
1857 {
1858 switch (iteration) {
1859 case TEST_QCACHE_ON:
1860 case TEST_QCACHE_ON_OFF:
1861 rc= mysql_query(lmysql, "set global query_cache_size=1000000");
1862 check_mysql_rc(rc, mysql);
1863 break;
1864 case TEST_QCACHE_OFF_ON:
1865 rc= mysql_query(lmysql, "set global query_cache_size=0");
1866 check_mysql_rc(rc, mysql);
1867 break;
1868 case TEST_QCACHE_ON_WITH_OTHER_CONN:
1869 lmysql= test_connect(NULL);
1870 FAIL_IF(!lmysql, "Opening new connection failed");
1871 break;
1872 }
1873
1874 strcpy(query, "select id1, value1 from t1 where id1= ? or "
1875 "CONVERT(value1 USING utf8)= ?");
1876 stmt= mysql_stmt_init(lmysql);
1877 FAIL_IF(!stmt, mysql_error(lmysql));
1878 rc= mysql_stmt_prepare(stmt, SL(query));
1879 check_stmt_rc(rc, stmt);
1880
1881 FAIL_IF(mysql_stmt_param_count(stmt) != 2, "param_count != 2");
1882
1883 switch (iteration) {
1884 case TEST_QCACHE_OFF_ON:
1885 rc= mysql_query(lmysql, "set global query_cache_size=1000000");
1886 check_mysql_rc(rc, mysql);
1887 break;
1888 case TEST_QCACHE_ON_OFF:
1889 rc= mysql_query(lmysql, "set global query_cache_size=0");
1890 check_mysql_rc(rc, mysql);
1891 default:
1892 break;
1893 }
1894
1895 memset(p_bind, '\0', sizeof(p_bind));
1896 p_bind[0].buffer_type= MYSQL_TYPE_LONG;
1897 p_bind[0].buffer= (void *)&p_int_data;
1898 p_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
1899 p_bind[1].buffer= (void *)p_str_data;
1900 p_bind[1].buffer_length= array_elements(p_str_data);
1901 p_bind[1].length= &p_str_length;
1902
1903 rc= mysql_stmt_bind_param(stmt, p_bind);
1904 check_stmt_rc(rc, stmt);
1905 p_int_data= 1;
1906 strcpy(p_str_data, "hh");
1907 p_str_length= (unsigned long)strlen(p_str_data);
1908
1909 memset(r_bind, '\0', sizeof(r_bind));
1910 r_bind[0].buffer_type= MYSQL_TYPE_LONG;
1911 r_bind[0].buffer= (void *)&r_int_data;
1912 r_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
1913 r_bind[1].buffer= (void *)r_str_data;
1914 r_bind[1].buffer_length= array_elements(r_str_data);
1915 r_bind[1].length= &r_str_length;
1916
1917 rc= mysql_stmt_bind_result(stmt, r_bind);
1918 check_stmt_rc(rc, stmt);
1919 rc= mysql_stmt_execute(stmt);
1920 check_stmt_rc(rc, stmt);
1921 test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
1922 r_metadata= mysql_stmt_result_metadata(stmt);
1923 FAIL_UNLESS(r_metadata != NULL, "");
1924 rc= mysql_stmt_fetch(stmt);
1925 check_stmt_rc(rc,stmt);
1926 FAIL_UNLESS((r_int_data == 1) && (r_str_length == 2) &&
1927 (strcmp(r_str_data, "hh") == 0), "test_ps_query_cache_result failure"); \
1928 rc= mysql_stmt_fetch(stmt);
1929 check_stmt_rc(rc,stmt);
1930 FAIL_UNLESS((r_int_data == 2) && (r_str_length == 2) &&
1931 (strcmp(r_str_data, "hh") == 0), "test_ps_query_cache_result failure"); \
1932 rc= mysql_stmt_fetch(stmt);
1933 check_stmt_rc(rc,stmt);
1934 FAIL_UNLESS((r_int_data == 1) && (r_str_length == 2) &&
1935 (strcmp(r_str_data, "ii") == 0), "test_ps_query_cache_result failure"); \
1936 rc= mysql_stmt_fetch(stmt);
1937 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
1938 mysql_free_result(r_metadata);
1939
1940
1941 /* now retry with the same parameter values and see qcache hits */
1942 hits1= query_cache_hits(lmysql);
1943 rc= mysql_stmt_execute(stmt);
1944 check_stmt_rc(rc, stmt); test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
1945 hits2= query_cache_hits(lmysql);
1946 switch(iteration) {
1947 case TEST_QCACHE_ON_WITH_OTHER_CONN:
1948 case TEST_QCACHE_ON: /* should have hit */
1949 FAIL_UNLESS(hits2-hits1 == 1, "hits2 != hits1 + 1");
1950 break;
1951 case TEST_QCACHE_OFF_ON:
1952 case TEST_QCACHE_ON_OFF: /* should not have hit */
1953 FAIL_UNLESS(hits2-hits1 == 0, "hits2 != hits1");
1954 break;
1955 }
1956
1957 /* now modify parameter values and see qcache hits */
1958 strcpy(p_str_data, "ii");
1959 p_str_length= (unsigned long)strlen(p_str_data);
1960 rc= mysql_stmt_execute(stmt);
1961 check_stmt_rc(rc, stmt);
1962 test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
1963 hits1= query_cache_hits(lmysql);
1964
1965 switch(iteration) {
1966 case TEST_QCACHE_ON:
1967 case TEST_QCACHE_OFF_ON:
1968 case TEST_QCACHE_ON_OFF: /* should not have hit */
1969 FAIL_UNLESS(hits2-hits1 == 0, "hits2 != hits1");
1970 break;
1971 case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
1972 FAIL_UNLESS(hits1-hits2 == 1, "hits2 != hits1+1");
1973 break;
1974 }
1975
1976 rc= mysql_stmt_execute(stmt);
1977 check_stmt_rc(rc, stmt);
1978 test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
1979 hits2= query_cache_hits(lmysql);
1980
1981 mysql_stmt_close(stmt);
1982
1983 switch(iteration) {
1984 case TEST_QCACHE_ON: /* should have hit */
1985 FAIL_UNLESS(hits2-hits1 == 1, "hits2 != hits1+1");
1986 break;
1987 case TEST_QCACHE_OFF_ON:
1988 case TEST_QCACHE_ON_OFF: /* should not have hit */
1989 FAIL_UNLESS(hits2-hits1 == 0, "hits2 != hits1");
1990 break;
1991 case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
1992 FAIL_UNLESS(hits2-hits1 == 1, "hits2 != hits1+1");
1993 break;
1994 }
1995
1996 } /* for(iteration=...) */
1997
1998 if (lmysql != mysql)
1999 mysql_close(lmysql);
2000
2001 rc= mysql_query(mysql, "set global query_cache_size=0");
2002 check_mysql_rc(rc, mysql);
2003 return OK;
2004}
2005
2006static int test_bug3117(MYSQL *mysql)
2007{
2008 MYSQL_STMT *stmt;
2009 MYSQL_BIND buffer;
2010 longlong lii;
2011 ulong length;
2012 my_bool is_null;
2013 int rc;
2014
2015 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
2016 check_mysql_rc(rc, mysql);
2017
2018 rc= mysql_query(mysql, "CREATE TABLE t1 (id int auto_increment primary key)");
2019 check_mysql_rc(rc, mysql);
2020
2021 stmt= mysql_stmt_init(mysql);
2022 FAIL_IF(!stmt, mysql_error(mysql));
2023 rc= mysql_stmt_prepare(stmt, SL("SELECT LAST_INSERT_ID()"));
2024 check_stmt_rc(rc, stmt);
2025
2026 rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
2027 check_mysql_rc(rc, mysql);
2028
2029 rc= mysql_stmt_execute(stmt);
2030 check_stmt_rc(rc, stmt);
2031 memset(&buffer, '\0', sizeof(buffer));
2032 buffer.buffer_type= MYSQL_TYPE_LONGLONG;
2033 buffer.buffer_length= sizeof(lii);
2034 buffer.buffer= (void *)&lii;
2035 buffer.length= &length;
2036 buffer.is_null= &is_null;
2037
2038 rc= mysql_stmt_bind_result(stmt, &buffer);
2039 check_stmt_rc(rc, stmt);
2040 rc= mysql_stmt_store_result(stmt);
2041 check_stmt_rc(rc, stmt);
2042 rc= mysql_stmt_fetch(stmt);
2043 check_stmt_rc(rc, stmt);
2044 FAIL_UNLESS(is_null == 0 && lii == 1, "is_null != 0 || lii != 1");
2045
2046 rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
2047 check_mysql_rc(rc, mysql);
2048
2049 rc= mysql_stmt_execute(stmt);
2050 check_stmt_rc(rc, stmt);
2051 rc= mysql_stmt_fetch(stmt);
2052 check_stmt_rc(rc, stmt);
2053 FAIL_UNLESS(is_null == 0 && lii == 2, "is_null != 0 || lii != 2");
2054
2055 mysql_stmt_close(stmt);
2056
2057 rc= mysql_query(mysql, "DROP TABLE t1");
2058 check_mysql_rc(rc, mysql);
2059 return OK;
2060}
2061
2062/**
2063 Bug#36004 mysql_stmt_prepare resets the list of warnings
2064*/
2065
2066static int test_bug36004(MYSQL *mysql)
2067{
2068 int rc, warning_count= 0;
2069 MYSQL_STMT *stmt;
2070
2071
2072 if (mysql_get_server_version(mysql) < 60000) {
2073 diag("Test requires MySQL Server version 6.0 or above");
2074 return SKIP;
2075 }
2076
2077 rc= mysql_query(mysql, "drop table if exists inexistant");
2078 check_mysql_rc(rc, mysql);
2079
2080 FAIL_UNLESS(mysql_warning_count(mysql) == 1, "");
2081 query_int_variable(mysql, "@@warning_count", &warning_count);
2082 FAIL_UNLESS(warning_count, "Warning expected");
2083
2084 stmt= mysql_stmt_init(mysql);
2085 FAIL_IF(!stmt, mysql_error(mysql));
2086 rc= mysql_stmt_prepare(stmt, SL("select 1"));
2087 check_stmt_rc(rc, stmt);
2088
2089 FAIL_UNLESS(mysql_warning_count(mysql) == 0, "No warning expected");
2090 query_int_variable(mysql, "@@warning_count", &warning_count);
2091 FAIL_UNLESS(warning_count, "warning expected");
2092
2093 rc= mysql_stmt_execute(stmt);
2094 check_stmt_rc(rc, stmt);
2095 FAIL_UNLESS(mysql_warning_count(mysql) == 0, "No warning expected");
2096 mysql_stmt_close(stmt);
2097
2098 query_int_variable(mysql, "@@warning_count", &warning_count);
2099 FAIL_UNLESS(warning_count, "Warning expected");
2100
2101 stmt= mysql_stmt_init(mysql);
2102 FAIL_IF(!stmt, mysql_error(mysql));
2103 rc= mysql_stmt_prepare(stmt, SL("drop table if exists inexistant"));
2104 check_stmt_rc(rc, stmt);
2105
2106 query_int_variable(mysql, "@@warning_count", &warning_count);
2107 FAIL_UNLESS(warning_count == 0, "No warning expected");
2108 mysql_stmt_close(stmt);
2109
2110 return OK;
2111}
2112
2113static int test_bug3796(MYSQL *mysql)
2114{
2115 MYSQL_STMT *stmt;
2116 MYSQL_BIND my_bind[1];
2117 const char *concat_arg0= "concat_with_";
2118 enum { OUT_BUFF_SIZE= 30 };
2119 char out_buff[OUT_BUFF_SIZE];
2120 char canonical_buff[OUT_BUFF_SIZE];
2121 ulong out_length;
2122 const char *stmt_text;
2123 int rc;
2124
2125
2126 /* Create and fill test table */
2127 stmt_text= "DROP TABLE IF EXISTS t1";
2128 rc= mysql_real_query(mysql, SL(stmt_text));
2129 check_mysql_rc(rc, mysql);
2130
2131 stmt_text= "CREATE TABLE t1 (a INT, b VARCHAR(30))";
2132 rc= mysql_real_query(mysql, SL(stmt_text));
2133 check_mysql_rc(rc, mysql);
2134
2135 stmt_text= "INSERT INTO t1 VALUES(1, 'ONE'), (2, 'TWO')";
2136 rc= mysql_real_query(mysql, SL(stmt_text));
2137 check_mysql_rc(rc, mysql);
2138
2139 /* Create statement handle and prepare it with select */
2140 stmt= mysql_stmt_init(mysql);
2141 stmt_text= "SELECT concat(?, b) FROM t1";
2142
2143 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2144 check_stmt_rc(rc, stmt);
2145 /* Bind input buffers */
2146 memset(my_bind, '\0', sizeof(my_bind));
2147 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2148 my_bind[0].buffer= (void *) concat_arg0;
2149 my_bind[0].buffer_length= (unsigned long)strlen(concat_arg0);
2150
2151 mysql_stmt_bind_param(stmt, my_bind);
2152
2153 /* Execute the select statement */
2154 rc= mysql_stmt_execute(stmt);
2155 check_stmt_rc(rc, stmt);
2156 my_bind[0].buffer= (void *) out_buff;
2157 my_bind[0].buffer_length= OUT_BUFF_SIZE;
2158 my_bind[0].length= &out_length;
2159
2160 mysql_stmt_bind_result(stmt, my_bind);
2161
2162 rc= mysql_stmt_fetch(stmt);
2163 check_stmt_rc(rc, stmt);
2164 strcpy(canonical_buff, concat_arg0);
2165 strcat(canonical_buff, "ONE");
2166 FAIL_UNLESS(strlen(canonical_buff) == out_length &&
2167 strncmp(out_buff, canonical_buff, out_length) == 0, "");
2168
2169 rc= mysql_stmt_fetch(stmt);
2170 check_stmt_rc(rc, stmt);
2171 strcpy(canonical_buff + strlen(concat_arg0), "TWO");
2172 FAIL_UNLESS(strlen(canonical_buff) == out_length &&
2173 strncmp(out_buff, canonical_buff, out_length) == 0, "");
2174
2175 rc= mysql_stmt_fetch(stmt);
2176 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
2177
2178 mysql_stmt_close(stmt);
2179
2180 stmt_text= "DROP TABLE IF EXISTS t1";
2181 rc= mysql_real_query(mysql, SL(stmt_text));
2182 check_mysql_rc(rc, mysql);
2183 return OK;
2184}
2185
2186static int test_bug4026(MYSQL *mysql)
2187{
2188 MYSQL_STMT *stmt;
2189 MYSQL_BIND my_bind[2];
2190 MYSQL_TIME time_in, time_out;
2191 MYSQL_TIME datetime_in, datetime_out;
2192 const char *stmt_text;
2193 int rc;
2194
2195
2196 /* Check that microseconds are inserted and selected successfully */
2197
2198 /* Create a statement handle and prepare it with select */
2199 stmt= mysql_stmt_init(mysql);
2200 stmt_text= "SELECT ?, ?";
2201
2202 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2203 check_stmt_rc(rc, stmt);
2204 /* Bind input buffers */
2205 memset(my_bind, '\0', sizeof(MYSQL_BIND) * 2);
2206 memset(&time_in, '\0', sizeof(MYSQL_TIME));
2207 memset(&time_out, '\0', sizeof(MYSQL_TIME));
2208 memset(&datetime_in, '\0', sizeof(MYSQL_TIME));
2209 memset(&datetime_out, '\0', sizeof(MYSQL_TIME));
2210 my_bind[0].buffer_type= MYSQL_TYPE_TIME;
2211 my_bind[0].buffer= (void *) &time_in;
2212 my_bind[1].buffer_type= MYSQL_TYPE_DATETIME;
2213 my_bind[1].buffer= (void *) &datetime_in;
2214
2215 time_in.hour= 23;
2216 time_in.minute= 59;
2217 time_in.second= 59;
2218 time_in.second_part= 123456;
2219 /*
2220 This is not necessary, just to make DIE_UNLESS below work: this field
2221 is filled in when time is received from server
2222 */
2223 time_in.time_type= MYSQL_TIMESTAMP_TIME;
2224
2225 datetime_in= time_in;
2226 datetime_in.year= 2003;
2227 datetime_in.month= 12;
2228 datetime_in.day= 31;
2229 datetime_in.time_type= MYSQL_TIMESTAMP_DATETIME;
2230
2231 mysql_stmt_bind_param(stmt, my_bind);
2232
2233 /* Execute the select statement */
2234 rc= mysql_stmt_execute(stmt);
2235 check_stmt_rc(rc, stmt);
2236 my_bind[0].buffer= (void *) &time_out;
2237 my_bind[1].buffer= (void *) &datetime_out;
2238
2239 mysql_stmt_bind_result(stmt, my_bind);
2240
2241 rc= mysql_stmt_fetch(stmt);
2242 FAIL_UNLESS(rc == 0, "rc != 0");
2243 FAIL_UNLESS(memcmp(&time_in, &time_out, sizeof(time_in)) == 0, "time_in != time_out");
2244 FAIL_UNLESS(memcmp(&datetime_in, &datetime_out, sizeof(datetime_in)) == 0, "datetime_in != datetime_out");
2245 mysql_stmt_close(stmt);
2246
2247 return OK;
2248}
2249
2250static int test_bug4030(MYSQL *mysql)
2251{
2252 MYSQL_STMT *stmt;
2253 MYSQL_BIND my_bind[3];
2254 MYSQL_TIME time_canonical, time_out;
2255 MYSQL_TIME date_canonical, date_out;
2256 MYSQL_TIME datetime_canonical, datetime_out;
2257 const char *stmt_text;
2258 int rc;
2259
2260
2261 /* Check that microseconds are inserted and selected successfully */
2262
2263 /* Execute a query with time values in prepared mode */
2264 stmt= mysql_stmt_init(mysql);
2265 stmt_text= "SELECT '23:59:59.123456', '2003-12-31', "
2266 "'2003-12-31 23:59:59.123456'";
2267 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2268 check_stmt_rc(rc, stmt);
2269 rc= mysql_stmt_execute(stmt);
2270 check_stmt_rc(rc, stmt);
2271 /* Bind output buffers */
2272 memset(my_bind, '\0', sizeof(my_bind));
2273 memset(&time_canonical, '\0', sizeof(time_canonical));
2274 memset(&time_out, '\0', sizeof(time_out));
2275 memset(&date_canonical, '\0', sizeof(date_canonical));
2276 memset(&date_out, '\0', sizeof(date_out));
2277 memset(&datetime_canonical, '\0', sizeof(datetime_canonical));
2278 memset(&datetime_out, '\0', sizeof(datetime_out));
2279 my_bind[0].buffer_type= MYSQL_TYPE_TIME;
2280 my_bind[0].buffer= (void *) &time_out;
2281 my_bind[1].buffer_type= MYSQL_TYPE_DATE;
2282 my_bind[1].buffer= (void *) &date_out;
2283 my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
2284 my_bind[2].buffer= (void *) &datetime_out;
2285
2286 time_canonical.hour= 23;
2287 time_canonical.minute= 59;
2288 time_canonical.second= 59;
2289 time_canonical.second_part= 123456;
2290 time_canonical.time_type= MYSQL_TIMESTAMP_TIME;
2291
2292 date_canonical.year= 2003;
2293 date_canonical.month= 12;
2294 date_canonical.day= 31;
2295 date_canonical.time_type= MYSQL_TIMESTAMP_DATE;
2296
2297 datetime_canonical= time_canonical;
2298 datetime_canonical.year= 2003;
2299 datetime_canonical.month= 12;
2300 datetime_canonical.day= 31;
2301 datetime_canonical.time_type= MYSQL_TIMESTAMP_DATETIME;
2302
2303 mysql_stmt_bind_result(stmt, my_bind);
2304
2305 rc= mysql_stmt_fetch(stmt);
2306 FAIL_UNLESS(rc == 0, "rc != 0");
2307 FAIL_UNLESS(memcmp(&time_canonical, &time_out, sizeof(time_out)) == 0, "time_canonical != time_out");
2308 FAIL_UNLESS(memcmp(&date_canonical, &date_out, sizeof(date_out)) == 0, "date_canoncical != date_out");
2309 FAIL_UNLESS(memcmp(&datetime_canonical, &datetime_out, sizeof(datetime_out)) == 0, "datetime_canonical != datetime_out");
2310 mysql_stmt_close(stmt);
2311 return OK;
2312}
2313
2314static int test_bug4079(MYSQL *mysql)
2315{
2316 MYSQL_STMT *stmt;
2317 MYSQL_BIND my_bind[1];
2318 const char *stmt_text;
2319 uint32 res;
2320 int rc;
2321
2322 /* Create and fill table */
2323 mysql_query(mysql, "DROP TABLE IF EXISTS t1");
2324 mysql_query(mysql, "CREATE TABLE t1 (a int)");
2325 mysql_query(mysql, "INSERT INTO t1 VALUES (1), (2)");
2326
2327 /* Prepare erroneous statement */
2328 stmt= mysql_stmt_init(mysql);
2329 stmt_text= "SELECT 1 < (SELECT a FROM t1)";
2330
2331 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2332 check_stmt_rc(rc, stmt);
2333 /* Execute the select statement */
2334 rc= mysql_stmt_execute(stmt);
2335 check_stmt_rc(rc, stmt);
2336 /* Bind input buffers */
2337 memset(my_bind, '\0', sizeof(my_bind));
2338 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2339 my_bind[0].buffer= (void *) &res;
2340
2341 mysql_stmt_bind_result(stmt, my_bind);
2342
2343 rc= mysql_stmt_fetch(stmt);
2344 FAIL_UNLESS(rc == 1, "rc != 1");
2345 /* buggy version of libmysql hanged up here */
2346 mysql_stmt_close(stmt);
2347 return OK;
2348}
2349
2350static int test_bug4172(MYSQL *mysql)
2351{
2352 MYSQL_STMT *stmt;
2353 MYSQL_BIND my_bind[3];
2354 const char *stmt_text;
2355 MYSQL_RES *res;
2356 MYSQL_ROW row;
2357 int rc;
2358 char f[100], d[100], e[100];
2359 ulong f_len, d_len, e_len;
2360
2361 mysql_query(mysql, "DROP TABLE IF EXISTS t1");
2362 mysql_query(mysql, "CREATE TABLE t1 (f float, d double, e decimal(10,4))");
2363 mysql_query(mysql, "INSERT INTO t1 VALUES (12345.1234, 123456.123456, "
2364 "123456.1234)");
2365
2366 stmt= mysql_stmt_init(mysql);
2367 stmt_text= "SELECT f, d, e FROM t1";
2368
2369 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2370 check_stmt_rc(rc, stmt); rc= mysql_stmt_execute(stmt);
2371 check_stmt_rc(rc, stmt);
2372 memset(my_bind, '\0', sizeof(my_bind)); my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2373 my_bind[0].buffer= f;
2374 my_bind[0].buffer_length= sizeof(f);
2375 my_bind[0].length= &f_len;
2376 my_bind[1].buffer_type= MYSQL_TYPE_STRING;
2377 my_bind[1].buffer= d;
2378 my_bind[1].buffer_length= sizeof(d);
2379 my_bind[1].length= &d_len;
2380 my_bind[2].buffer_type= MYSQL_TYPE_STRING;
2381 my_bind[2].buffer= e;
2382 my_bind[2].buffer_length= sizeof(e);
2383 my_bind[2].length= &e_len;
2384
2385 mysql_stmt_bind_result(stmt, my_bind);
2386
2387 mysql_stmt_store_result(stmt);
2388 rc= mysql_stmt_fetch(stmt);
2389 check_stmt_rc(rc, stmt);
2390 rc= mysql_real_query(mysql, SL(stmt_text));
2391 check_mysql_rc(rc, mysql);
2392 res= mysql_store_result(mysql);
2393 row= mysql_fetch_row(res);
2394
2395 diag("expected %s %s %s", row[0], row[1], row[2]);
2396 diag("fetched %s %s %s", f, d, e);
2397 FAIL_UNLESS(!strcmp(f, row[0]) && !strcmp(d, row[1]) && !strcmp(e, row[2]), "");
2398
2399 mysql_free_result(res);
2400 mysql_stmt_close(stmt);
2401 return OK;
2402}
2403
2404static int test_bug4231(MYSQL *mysql)
2405{
2406 MYSQL_STMT *stmt;
2407 MYSQL_BIND my_bind[2];
2408 MYSQL_TIME tm[2];
2409 const char *stmt_text;
2410 int rc;
2411
2412
2413 stmt_text= "DROP TABLE IF EXISTS t1";
2414 rc= mysql_real_query(mysql, SL(stmt_text));
2415 check_mysql_rc(rc, mysql);
2416
2417 stmt_text= "CREATE TABLE t1 (a int)";
2418 rc= mysql_real_query(mysql, SL(stmt_text));
2419 check_mysql_rc(rc, mysql);
2420
2421 stmt_text= "INSERT INTO t1 VALUES (1)";
2422 rc= mysql_real_query(mysql, SL(stmt_text));
2423 check_mysql_rc(rc, mysql);
2424
2425 stmt= mysql_stmt_init(mysql);
2426 stmt_text= "SELECT a FROM t1 WHERE ? = ?";
2427 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2428 check_stmt_rc(rc, stmt);
2429 /* Bind input buffers */
2430 memset(my_bind, '\0', sizeof(my_bind)); memset(tm, '\0', sizeof(tm));
2431 my_bind[0].buffer_type= MYSQL_TYPE_DATE;
2432 my_bind[0].buffer= &tm[0];
2433 my_bind[1].buffer_type= MYSQL_TYPE_DATE;
2434 my_bind[1].buffer= &tm[1];
2435
2436 mysql_stmt_bind_param(stmt, my_bind);
2437 check_stmt_rc(rc, stmt);
2438 /*
2439 First set server-side params to some non-zero non-equal values:
2440 then we will check that they are not used when client sends
2441 new (zero) times.
2442 */
2443 tm[0].time_type = MYSQL_TIMESTAMP_DATE;
2444 tm[0].year = 2000;
2445 tm[0].month = 1;
2446 tm[0].day = 1;
2447 tm[1]= tm[0];
2448 --tm[1].year; /* tm[0] != tm[1] */
2449
2450 rc= mysql_stmt_execute(stmt);
2451 check_stmt_rc(rc, stmt);
2452 rc= mysql_stmt_fetch(stmt);
2453
2454 /* binds are unequal, no rows should be returned */
2455 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
2456
2457 /* Set one of the dates to zero */
2458 tm[0].year= tm[0].month= tm[0].day= 0;
2459 tm[1]= tm[0];
2460 mysql_stmt_execute(stmt);
2461 rc= mysql_stmt_fetch(stmt);
2462 FAIL_UNLESS(rc == 0, "rc != 0");
2463
2464 mysql_stmt_close(stmt);
2465 stmt_text= "DROP TABLE t1";
2466 rc= mysql_real_query(mysql, SL(stmt_text));
2467 check_mysql_rc(rc, mysql);
2468 return OK;
2469}
2470
2471static int test_bug4236(MYSQL *mysql)
2472{
2473 MYSQL_STMT *stmt, *stmt1;
2474 const char *stmt_text;
2475 int rc;
2476 MYSQL_STMT backup;
2477 MYSQL *mysql1;
2478
2479
2480 stmt= mysql_stmt_init(mysql);
2481
2482 /* mysql_stmt_execute() of statement with statement id= 0 crashed server */
2483 stmt_text= "SELECT 1";
2484 /* We need to prepare statement to pass by possible check in libmysql */
2485 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2486 check_stmt_rc(rc, stmt); /* Hack to check that server works OK if statement wasn't found */
2487 backup.stmt_id= stmt->stmt_id;
2488 stmt->stmt_id= 0;
2489 rc= mysql_stmt_execute(stmt);
2490 FAIL_IF(!rc, "Error expected");
2491
2492 /* lets try to hack with a new connection */
2493 mysql1= test_connect(NULL);
2494 stmt1= mysql_stmt_init(mysql1);
2495 stmt_text= "SELECT 2";
2496 rc= mysql_stmt_prepare(stmt1, SL(stmt_text));
2497 check_stmt_rc(rc, stmt);
2498
2499 stmt->stmt_id= stmt1->stmt_id;
2500 rc= mysql_stmt_execute(stmt);
2501 FAIL_IF(!rc, "Error expected");
2502
2503 mysql_stmt_close(stmt1);
2504 mysql_close(mysql1);
2505
2506 /* Restore original statement id to be able to reprepare it */
2507 stmt->stmt_id= backup.stmt_id;
2508
2509 mysql_stmt_close(stmt);
2510 return OK;
2511}
2512
2513static int test_bug5126(MYSQL *mysql)
2514{
2515 MYSQL_STMT *stmt;
2516 MYSQL_BIND my_bind[2];
2517 int32 c1, c2;
2518 const char *stmt_text;
2519 int rc;
2520
2521
2522 stmt_text= "DROP TABLE IF EXISTS t1";
2523 rc= mysql_real_query(mysql, SL(stmt_text));
2524 check_mysql_rc(rc, mysql);
2525
2526 stmt_text= "CREATE TABLE t1 (a mediumint, b int)";
2527 rc= mysql_real_query(mysql, SL(stmt_text));
2528 check_mysql_rc(rc, mysql);
2529
2530 stmt_text= "INSERT INTO t1 VALUES (8386608, 1)";
2531 rc= mysql_real_query(mysql, SL(stmt_text));
2532 check_mysql_rc(rc, mysql);
2533
2534 stmt= mysql_stmt_init(mysql);
2535 stmt_text= "SELECT a, b FROM t1";
2536 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2537 check_stmt_rc(rc, stmt);
2538 rc= mysql_stmt_execute(stmt);
2539 check_stmt_rc(rc, stmt);
2540 /* Bind output buffers */
2541 memset(my_bind, '\0', sizeof(my_bind));
2542 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2543 my_bind[0].buffer= &c1;
2544 my_bind[1].buffer_type= MYSQL_TYPE_LONG;
2545 my_bind[1].buffer= &c2;
2546
2547 mysql_stmt_bind_result(stmt, my_bind);
2548
2549 rc= mysql_stmt_fetch(stmt);
2550 FAIL_UNLESS(rc == 0, "rc != 0");
2551 FAIL_UNLESS(c1 == 8386608 && c2 == 1, "c1 != 8386608 || c2 != 1");
2552 mysql_stmt_close(stmt);
2553 return OK;
2554}
2555
2556static int test_bug5194(MYSQL *mysql)
2557{
2558 MYSQL_STMT *stmt;
2559 MYSQL_BIND *my_bind;
2560 char *query;
2561 char *param_str;
2562 int param_str_length;
2563 const char *stmt_text;
2564 int rc;
2565 float float_array[250] =
2566 {
2567 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2568 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2569 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2570 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2571 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2572 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2573 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2574 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2575 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2576 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2577 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2578 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2579 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
2580 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2581 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2582 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2583 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2584 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2585 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2586 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2587 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2588 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2589 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2590 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
2591 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25
2592 };
2593 float *fa_ptr= float_array;
2594 /* Number of columns per row */
2595 const int COLUMN_COUNT= sizeof(float_array)/sizeof(*float_array);
2596 /* Number of rows per bulk insert to start with */
2597 const int MIN_ROWS_PER_INSERT= 262;
2598 /* Max number of rows per bulk insert to end with */
2599 const int MAX_ROWS_PER_INSERT= 300;
2600 const int MAX_PARAM_COUNT= COLUMN_COUNT*MAX_ROWS_PER_INSERT;
2601 const char *query_template= "insert into t1 values %s";
2602 const int CHARS_PER_PARAM= 5; /* space needed to place ", ?" in the query */
2603 const int uint16_max= 65535;
2604 int nrows, i;
2605
2606
2607 stmt_text= "drop table if exists t1";
2608 rc= mysql_real_query(mysql, SL(stmt_text));
2609
2610 stmt_text= "create table if not exists t1"
2611 "(c1 float, c2 float, c3 float, c4 float, c5 float, c6 float, "
2612 "c7 float, c8 float, c9 float, c10 float, c11 float, c12 float, "
2613 "c13 float, c14 float, c15 float, c16 float, c17 float, c18 float, "
2614 "c19 float, c20 float, c21 float, c22 float, c23 float, c24 float, "
2615 "c25 float, c26 float, c27 float, c28 float, c29 float, c30 float, "
2616 "c31 float, c32 float, c33 float, c34 float, c35 float, c36 float, "
2617 "c37 float, c38 float, c39 float, c40 float, c41 float, c42 float, "
2618 "c43 float, c44 float, c45 float, c46 float, c47 float, c48 float, "
2619 "c49 float, c50 float, c51 float, c52 float, c53 float, c54 float, "
2620 "c55 float, c56 float, c57 float, c58 float, c59 float, c60 float, "
2621 "c61 float, c62 float, c63 float, c64 float, c65 float, c66 float, "
2622 "c67 float, c68 float, c69 float, c70 float, c71 float, c72 float, "
2623 "c73 float, c74 float, c75 float, c76 float, c77 float, c78 float, "
2624 "c79 float, c80 float, c81 float, c82 float, c83 float, c84 float, "
2625 "c85 float, c86 float, c87 float, c88 float, c89 float, c90 float, "
2626 "c91 float, c92 float, c93 float, c94 float, c95 float, c96 float, "
2627 "c97 float, c98 float, c99 float, c100 float, c101 float, c102 float, "
2628 "c103 float, c104 float, c105 float, c106 float, c107 float, c108 float, "
2629 "c109 float, c110 float, c111 float, c112 float, c113 float, c114 float, "
2630 "c115 float, c116 float, c117 float, c118 float, c119 float, c120 float, "
2631 "c121 float, c122 float, c123 float, c124 float, c125 float, c126 float, "
2632 "c127 float, c128 float, c129 float, c130 float, c131 float, c132 float, "
2633 "c133 float, c134 float, c135 float, c136 float, c137 float, c138 float, "
2634 "c139 float, c140 float, c141 float, c142 float, c143 float, c144 float, "
2635 "c145 float, c146 float, c147 float, c148 float, c149 float, c150 float, "
2636 "c151 float, c152 float, c153 float, c154 float, c155 float, c156 float, "
2637 "c157 float, c158 float, c159 float, c160 float, c161 float, c162 float, "
2638 "c163 float, c164 float, c165 float, c166 float, c167 float, c168 float, "
2639 "c169 float, c170 float, c171 float, c172 float, c173 float, c174 float, "
2640 "c175 float, c176 float, c177 float, c178 float, c179 float, c180 float, "
2641 "c181 float, c182 float, c183 float, c184 float, c185 float, c186 float, "
2642 "c187 float, c188 float, c189 float, c190 float, c191 float, c192 float, "
2643 "c193 float, c194 float, c195 float, c196 float, c197 float, c198 float, "
2644 "c199 float, c200 float, c201 float, c202 float, c203 float, c204 float, "
2645 "c205 float, c206 float, c207 float, c208 float, c209 float, c210 float, "
2646 "c211 float, c212 float, c213 float, c214 float, c215 float, c216 float, "
2647 "c217 float, c218 float, c219 float, c220 float, c221 float, c222 float, "
2648 "c223 float, c224 float, c225 float, c226 float, c227 float, c228 float, "
2649 "c229 float, c230 float, c231 float, c232 float, c233 float, c234 float, "
2650 "c235 float, c236 float, c237 float, c238 float, c239 float, c240 float, "
2651 "c241 float, c242 float, c243 float, c244 float, c245 float, c246 float, "
2652 "c247 float, c248 float, c249 float, c250 float)";
2653 rc= mysql_real_query(mysql, SL(stmt_text));
2654 check_mysql_rc(rc, mysql);
2655
2656 my_bind= (MYSQL_BIND*) malloc(MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
2657 query= (char*) malloc(strlen(query_template) +
2658 MAX_PARAM_COUNT * CHARS_PER_PARAM + 1);
2659 param_str= (char*) malloc(COLUMN_COUNT * CHARS_PER_PARAM);
2660
2661 FAIL_IF(my_bind == 0 || query == 0 || param_str == 0, "Not enough memory")
2662
2663 stmt= mysql_stmt_init(mysql);
2664
2665 /* setup a template for one row of parameters */
2666 sprintf(param_str, "(");
2667 for (i= 1; i < COLUMN_COUNT; ++i)
2668 strcat(param_str, "?, ");
2669 strcat(param_str, "?)");
2670 param_str_length= (int)strlen(param_str);
2671
2672 /* setup bind array */
2673 memset(my_bind, '\0', MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
2674 for (i= 0; i < MAX_PARAM_COUNT; ++i)
2675 {
2676 my_bind[i].buffer_type= MYSQL_TYPE_FLOAT;
2677 my_bind[i].buffer= fa_ptr;
2678 if (++fa_ptr == float_array + COLUMN_COUNT)
2679 fa_ptr= float_array;
2680 }
2681
2682 /*
2683 Test each number of rows per bulk insert, so that we can see where
2684 MySQL fails.
2685 */
2686 for (nrows= MIN_ROWS_PER_INSERT; nrows <= MAX_ROWS_PER_INSERT; ++nrows)
2687 {
2688 char *query_ptr;
2689 /* Create statement text for current number of rows */
2690 sprintf(query, query_template, param_str);
2691 query_ptr= query + (unsigned long)strlen(query);
2692 for (i= 1; i < nrows; ++i)
2693 {
2694 memcpy(query_ptr, ", ", 2);
2695 query_ptr+= 2;
2696 memcpy(query_ptr, param_str, param_str_length);
2697 query_ptr+= param_str_length;
2698 }
2699 *query_ptr= '\0';
2700
2701 rc= mysql_stmt_prepare(stmt, query, (ulong)(query_ptr - query));
2702
2703 if (rc && nrows * COLUMN_COUNT > uint16_max) /* expected error */
2704 break;
2705
2706 check_stmt_rc(rc, stmt);
2707
2708 /* bind the parameter array and execute the query */
2709 rc= mysql_stmt_bind_param(stmt, my_bind);
2710 check_stmt_rc(rc, stmt);
2711 rc= mysql_stmt_execute(stmt);
2712 check_stmt_rc(rc, stmt);
2713 rc= mysql_stmt_reset(stmt);
2714 }
2715
2716 free(param_str);
2717 free(query);
2718 rc= mysql_stmt_close(stmt);
2719 check_stmt_rc(rc, stmt);
2720 free(my_bind);
2721 stmt_text= "drop table t1";
2722 rc= mysql_real_query(mysql, SL(stmt_text));
2723 check_mysql_rc(rc, mysql);
2724 return OK;
2725}
2726
2727static int test_bug5315(MYSQL *mysql)
2728{
2729 MYSQL_STMT *stmt;
2730 const char *stmt_text;
2731 int rc;
2732
2733 if (!is_mariadb)
2734 return SKIP;
2735
2736 stmt_text= "SELECT 1";
2737 stmt= mysql_stmt_init(mysql);
2738 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2739 check_stmt_rc(rc, stmt);
2740 rc= mysql_change_user(mysql, username, password, schema);
2741 check_mysql_rc(rc, mysql);
2742
2743 rc= mysql_stmt_execute(stmt);
2744 FAIL_UNLESS(rc != 0, "Error expected");
2745
2746 rc= mysql_stmt_close(stmt);
2747 check_stmt_rc(rc, stmt);
2748
2749 stmt= mysql_stmt_init(mysql);
2750 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2751 check_stmt_rc(rc, stmt);
2752 rc= mysql_stmt_execute(stmt);
2753 check_stmt_rc(rc, stmt);
2754 mysql_stmt_close(stmt);
2755 return OK;
2756}
2757
2758static int test_bug5399(MYSQL *mysql)
2759{
2760 /*
2761 Ascii 97 is 'a', which gets mapped to Ascii 65 'A' unless internal
2762 statement id hash in the server uses binary collation.
2763 */
2764#define NUM_OF_USED_STMT 97
2765 MYSQL_STMT *stmt_list[NUM_OF_USED_STMT];
2766 MYSQL_STMT **stmt;
2767 MYSQL_BIND my_bind[1];
2768 char buff[600];
2769 int rc;
2770 int32 no;
2771
2772
2773 memset(my_bind, '\0', sizeof(my_bind)); my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2774 my_bind[0].buffer= &no;
2775
2776 for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
2777 {
2778 sprintf(buff, "select %d", (int) (stmt - stmt_list));
2779 *stmt= mysql_stmt_init(mysql);
2780 rc= mysql_stmt_prepare(*stmt, SL(buff));
2781 check_stmt_rc(rc, *stmt); mysql_stmt_bind_result(*stmt, my_bind);
2782 }
2783
2784 for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
2785 {
2786 rc= mysql_stmt_execute(*stmt);
2787 check_stmt_rc(rc, *stmt);
2788 rc= mysql_stmt_store_result(*stmt);
2789 check_stmt_rc(rc, *stmt);
2790 rc= mysql_stmt_fetch(*stmt);
2791 FAIL_UNLESS((int32) (stmt - stmt_list) == no, "");
2792 }
2793
2794 for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
2795 mysql_stmt_close(*stmt);
2796#undef NUM_OF_USED_STMT
2797 return OK;
2798}
2799
2800static int test_bug6046(MYSQL *mysql)
2801{
2802 MYSQL_STMT *stmt;
2803 const char *stmt_text;
2804 int rc;
2805 short b= 1;
2806 MYSQL_BIND my_bind[1];
2807
2808
2809 stmt_text= "DROP TABLE IF EXISTS t1";
2810 rc= mysql_real_query(mysql, SL(stmt_text));
2811 check_mysql_rc(rc, mysql);
2812 stmt_text= "CREATE TABLE t1 (a int, b int)";
2813 rc= mysql_real_query(mysql, SL(stmt_text));
2814 check_mysql_rc(rc, mysql);
2815 stmt_text= "INSERT INTO t1 VALUES (1,1),(2,2),(3,1),(4,2)";
2816 rc= mysql_real_query(mysql, SL(stmt_text));
2817 check_mysql_rc(rc, mysql);
2818
2819 stmt= mysql_stmt_init(mysql);
2820
2821 stmt_text= "SELECT t1.a FROM t1 NATURAL JOIN t1 as X1 "
2822 "WHERE t1.b > ? ORDER BY t1.a";
2823
2824 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2825 check_stmt_rc(rc, stmt);
2826 b= 1;
2827 memset(my_bind, '\0', sizeof(my_bind)); my_bind[0].buffer= &b;
2828 my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
2829
2830 mysql_stmt_bind_param(stmt, my_bind);
2831
2832 rc= mysql_stmt_execute(stmt);
2833 check_stmt_rc(rc, stmt);
2834 mysql_stmt_store_result(stmt);
2835
2836 rc= mysql_stmt_execute(stmt);
2837 check_stmt_rc(rc, stmt);
2838 mysql_stmt_close(stmt);
2839 return OK;
2840}
2841
2842static int test_bug6049(MYSQL *mysql)
2843{
2844 MYSQL_STMT *stmt;
2845 MYSQL_BIND my_bind[1];
2846 MYSQL_RES *res;
2847 MYSQL_ROW row;
2848 const char *stmt_text;
2849 char buffer[30];
2850 ulong length;
2851 int rc;
2852
2853
2854 stmt_text= "SELECT MAKETIME(-25, 12, 12)";
2855
2856 rc= mysql_real_query(mysql, SL(stmt_text));
2857 check_mysql_rc(rc, mysql);
2858 res= mysql_store_result(mysql);
2859 row= mysql_fetch_row(res);
2860
2861 stmt= mysql_stmt_init(mysql);
2862 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2863 check_stmt_rc(rc, stmt);
2864 rc= mysql_stmt_execute(stmt);
2865 check_stmt_rc(rc, stmt);
2866 memset(my_bind, '\0', sizeof(my_bind));
2867 my_bind[0].buffer_type = MYSQL_TYPE_STRING;
2868 my_bind[0].buffer = &buffer;
2869 my_bind[0].buffer_length = sizeof(buffer);
2870 my_bind[0].length = &length;
2871
2872 mysql_stmt_bind_result(stmt, my_bind);
2873 rc= mysql_stmt_fetch(stmt);
2874 check_stmt_rc(rc, stmt);
2875
2876 FAIL_UNLESS(strcmp(row[0], (char*) buffer) == 0, "row[0] != buffer");
2877
2878 mysql_free_result(res);
2879 mysql_stmt_close(stmt);
2880 return OK;
2881}
2882
2883static int test_bug6058(MYSQL *mysql)
2884{
2885 MYSQL_STMT *stmt;
2886 MYSQL_BIND my_bind[1];
2887 MYSQL_RES *res;
2888 MYSQL_ROW row;
2889 const char *stmt_text;
2890 char buffer[30];
2891 ulong length;
2892 int rc;
2893
2894
2895 stmt_text= "SELECT CAST('0000-00-00' AS DATE)";
2896
2897 rc= mysql_real_query(mysql, SL(stmt_text));
2898 check_mysql_rc(rc, mysql);
2899 res= mysql_store_result(mysql);
2900 row= mysql_fetch_row(res);
2901
2902 stmt= mysql_stmt_init(mysql);
2903 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2904 check_stmt_rc(rc, stmt);
2905 rc= mysql_stmt_execute(stmt);
2906 check_stmt_rc(rc, stmt);
2907 memset(my_bind, '\0', sizeof(my_bind));
2908 my_bind[0].buffer_type = MYSQL_TYPE_STRING;
2909 my_bind[0].buffer = &buffer;
2910 my_bind[0].buffer_length = sizeof(buffer);
2911 my_bind[0].length = &length;
2912
2913 mysql_stmt_bind_result(stmt, my_bind);
2914 rc= mysql_stmt_fetch(stmt);
2915 check_stmt_rc(rc, stmt);
2916
2917 FAIL_UNLESS(strcmp(row[0], buffer) == 0, "row[0] != buffer");
2918
2919 mysql_free_result(res);
2920 mysql_stmt_close(stmt);
2921 return OK;
2922}
2923
2924
2925static int test_bug6059(MYSQL *mysql)
2926{
2927 MYSQL_STMT *stmt;
2928 const char *stmt_text;
2929 int rc;
2930
2931 stmt_text= "SELECT 'foo' INTO OUTFILE 'x.3'";
2932
2933 stmt= mysql_stmt_init(mysql);
2934 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2935 check_stmt_rc(rc, stmt);
2936 FAIL_UNLESS(mysql_stmt_field_count(stmt) == 0, "");
2937 mysql_stmt_close(stmt);
2938 return OK;
2939}
2940
2941static int test_bug6096(MYSQL *mysql)
2942{
2943 MYSQL_STMT *stmt;
2944 MYSQL_RES *query_result, *stmt_metadata;
2945 const char *stmt_text;
2946 MYSQL_BIND my_bind[12];
2947 MYSQL_FIELD *query_field_list, *stmt_field_list;
2948 ulong query_field_count, stmt_field_count;
2949 int rc;
2950 my_bool update_max_length= TRUE;
2951 uint i;
2952
2953
2954 stmt_text= "drop table if exists t1";
2955 rc= mysql_real_query(mysql, SL(stmt_text));
2956 check_mysql_rc(rc, mysql);
2957
2958 mysql_query(mysql, "set sql_mode=''");
2959 stmt_text= "create table t1 (c_tinyint tinyint, c_smallint smallint, "
2960 " c_mediumint mediumint, c_int int, "
2961 " c_bigint bigint, c_float float, "
2962 " c_double double, c_varchar varchar(20), "
2963 " c_char char(20), c_time time, c_date date, "
2964 " c_datetime datetime)";
2965 rc= mysql_real_query(mysql, SL(stmt_text));
2966 check_mysql_rc(rc, mysql);
2967 stmt_text= "insert into t1 values (-100, -20000, 30000000, 4, 8, 1.0, "
2968 "2.0, 'abc', 'def', now(), now(), now())";
2969 rc= mysql_real_query(mysql, SL(stmt_text));
2970 check_mysql_rc(rc, mysql);
2971
2972 stmt_text= "select * from t1";
2973
2974 /* Run select in prepared and non-prepared mode and compare metadata */
2975 rc= mysql_real_query(mysql, SL(stmt_text));
2976 check_mysql_rc(rc, mysql);
2977 query_result= mysql_store_result(mysql);
2978 query_field_list= mysql_fetch_fields(query_result);
2979 FAIL_IF(!query_field_list, "fetch_fields failed");
2980 query_field_count= mysql_num_fields(query_result);
2981
2982 stmt= mysql_stmt_init(mysql);
2983 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2984 check_stmt_rc(rc, stmt); rc= mysql_stmt_execute(stmt);
2985 check_stmt_rc(rc, stmt); mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH,
2986 (void*) &update_max_length);
2987 mysql_stmt_store_result(stmt);
2988 stmt_metadata= mysql_stmt_result_metadata(stmt);
2989 stmt_field_list= mysql_fetch_fields(stmt_metadata);
2990 stmt_field_count= mysql_num_fields(stmt_metadata);
2991 FAIL_UNLESS(stmt_field_count == query_field_count, "");
2992
2993
2994 /* Bind and fetch the data */
2995
2996 memset(my_bind, '\0', sizeof(my_bind));
2997 for (i= 0; i < stmt_field_count; ++i)
2998 {
2999 my_bind[i].buffer_type= MYSQL_TYPE_STRING;
3000 my_bind[i].buffer_length= stmt_field_list[i].max_length + 1;
3001 my_bind[i].buffer= malloc(my_bind[i].buffer_length);
3002 }
3003 mysql_stmt_bind_result(stmt, my_bind);
3004 rc= mysql_stmt_fetch(stmt);
3005 diag("rc=%d", rc);
3006 check_stmt_rc(rc, stmt);
3007 rc= mysql_stmt_fetch(stmt);
3008 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3009
3010 /* Clean up */
3011
3012 for (i= 0; i < stmt_field_count; ++i)
3013 free(my_bind[i].buffer);
3014 mysql_stmt_close(stmt);
3015 mysql_free_result(query_result);
3016 mysql_free_result(stmt_metadata);
3017 stmt_text= "drop table t1";
3018 rc= mysql_real_query(mysql, SL(stmt_text));
3019 check_mysql_rc(rc, mysql);
3020 return OK;
3021}
3022
3023/* Bug#7990 - mysql_stmt_close doesn't reset mysql->net.last_error */
3024
3025static int test_bug7990(MYSQL *mysql)
3026{
3027 MYSQL_STMT *stmt;
3028 int rc;
3029
3030 stmt= mysql_stmt_init(mysql);
3031 rc= mysql_stmt_prepare(stmt, "foo", 3);
3032 /*
3033 XXX: the fact that we store errno both in STMT and in
3034 MYSQL is not documented and is subject to change in 5.0
3035 */
3036 FAIL_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql), "Error expected");
3037 mysql_stmt_close(stmt);
3038 FAIL_UNLESS(!mysql_errno(mysql), "errno != 0");
3039 return OK;
3040}
3041
3042/* Bug#8330 - mysql_stmt_execute crashes (libmysql) */
3043
3044static int test_bug8330(MYSQL *mysql)
3045{
3046 const char *stmt_text;
3047 MYSQL_STMT *stmt[2];
3048 int i, rc;
3049 const char *query= "select a,b from t1 where a=?";
3050 MYSQL_BIND my_bind[2];
3051 long lval[2]= {1,2};
3052
3053 stmt_text= "drop table if exists t1";
3054 /* in case some previos test failed */
3055 rc= mysql_real_query(mysql, SL(stmt_text));
3056 check_mysql_rc(rc, mysql);
3057 stmt_text= "create table t1 (a int, b int)";
3058 rc= mysql_real_query(mysql, SL(stmt_text));
3059 check_mysql_rc(rc, mysql);
3060
3061 memset(my_bind, '\0', sizeof(my_bind));
3062 for (i=0; i < 2; i++)
3063 {
3064 stmt[i]= mysql_stmt_init(mysql);
3065 rc= mysql_stmt_prepare(stmt[i], SL(query));
3066 check_stmt_rc(rc, stmt[i]);
3067 my_bind[i].buffer_type= MYSQL_TYPE_LONG;
3068 my_bind[i].buffer= (void*) &lval[i];
3069 my_bind[i].is_null= 0;
3070 mysql_stmt_bind_param(stmt[i], &my_bind[i]);
3071 }
3072
3073 rc= mysql_stmt_execute(stmt[0]);
3074 check_stmt_rc(rc, stmt[0]);
3075 rc= mysql_stmt_execute(stmt[1]);
3076 FAIL_UNLESS(rc && mysql_stmt_errno(stmt[1]) == CR_COMMANDS_OUT_OF_SYNC, "Error expected");
3077 rc= mysql_stmt_execute(stmt[0]);
3078 check_stmt_rc(rc, stmt[0]);
3079 mysql_stmt_close(stmt[0]);
3080 mysql_stmt_close(stmt[1]);
3081
3082 stmt_text= "drop table t1";
3083 rc= mysql_real_query(mysql, SL(stmt_text));
3084 check_mysql_rc(rc, mysql);
3085 return OK;
3086}
3087
3088/* Test misc field information, bug: #74 */
3089
3090static int test_field_misc(MYSQL *mysql)
3091{
3092 MYSQL_STMT *stmt;
3093 MYSQL_RES *result;
3094 int rc;
3095
3096
3097 rc= mysql_query(mysql, "SELECT @@autocommit");
3098 check_mysql_rc(rc, mysql);
3099
3100 result= mysql_store_result(mysql);
3101 FAIL_IF(!result, "Invalid result set");
3102
3103 rc= 0;
3104 while (mysql_fetch_row(result))
3105 rc++;
3106 FAIL_UNLESS(rc == 1, "rowcount != 1");
3107
3108 verify_prepare_field(result, 0,
3109 "@@autocommit", "", /* field and its org name */
3110 MYSQL_TYPE_LONGLONG, /* field type */
3111 "", "", /* table and its org name */
3112 "", 1, 0); /* db name, length(its bool flag)*/
3113
3114 mysql_free_result(result);
3115
3116 stmt= mysql_stmt_init(mysql);
3117 FAIL_IF(!stmt, mysql_error(mysql));
3118 rc= mysql_stmt_prepare(stmt, SL("SELECT @@autocommit"));
3119 check_stmt_rc(rc, stmt);
3120
3121 rc= mysql_stmt_execute(stmt);
3122 check_stmt_rc(rc, stmt);
3123
3124 result= mysql_stmt_result_metadata(stmt);
3125 FAIL_IF(!result, "Invalid result set");
3126
3127 rc= 0;
3128 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
3129 rc++;
3130 FAIL_UNLESS(rc == 1, "rowcount != 1");
3131
3132 verify_prepare_field(result, 0,
3133 "@@autocommit", "", /* field and its org name */
3134 MYSQL_TYPE_LONGLONG, /* field type */
3135 "", "", /* table and its org name */
3136 "", 1, 0); /* db name, length(its bool flag)*/
3137
3138 mysql_free_result(result);
3139 mysql_stmt_close(stmt);
3140
3141 stmt= mysql_stmt_init(mysql);
3142 FAIL_IF(!stmt, mysql_error(mysql));
3143 rc= mysql_stmt_prepare(stmt, SL("SELECT @@max_error_count"));
3144 check_stmt_rc(rc, stmt);
3145
3146 result= mysql_stmt_result_metadata(stmt);
3147 FAIL_IF(!result, "Invalid result set");
3148
3149 rc= mysql_stmt_execute(stmt);
3150 check_stmt_rc(rc, stmt);
3151
3152 rc= 0;
3153 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
3154 rc++;
3155 FAIL_UNLESS(rc == 1, "rowcount != 1");
3156
3157 if (verify_prepare_field(result, 0,
3158 "@@max_error_count", "", /* field and its org name */
3159 MYSQL_TYPE_LONGLONG, /* field type */
3160 "", "", /* table and its org name */
3161 /* db name, length */
3162 "", MY_INT64_NUM_DECIMAL_DIGITS , 0))
3163 goto error;
3164
3165 mysql_free_result(result);
3166 mysql_stmt_close(stmt);
3167
3168 stmt= mysql_stmt_init(mysql);
3169 FAIL_IF(!stmt, mysql_error(mysql));
3170 rc= mysql_stmt_prepare(stmt, SL("SELECT @@max_allowed_packet"));
3171 check_stmt_rc(rc, stmt);
3172
3173 result= mysql_stmt_result_metadata(stmt);
3174 FAIL_IF(!result, "Invalid result set");
3175
3176 rc= mysql_stmt_execute(stmt);
3177 check_stmt_rc(rc, stmt);
3178
3179 rc= 0;
3180 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
3181 rc++;
3182 FAIL_UNLESS(rc == 1, "rowcount != 1");
3183
3184 if (verify_prepare_field(result, 0,
3185 "@@max_allowed_packet", "", /* field and its org name */
3186 MYSQL_TYPE_LONGLONG, /* field type */
3187 "", "", /* table and its org name */
3188 /* db name, length */
3189 "", MY_INT64_NUM_DECIMAL_DIGITS, 0))
3190 goto error;
3191
3192 mysql_free_result(result);
3193 mysql_stmt_close(stmt);
3194
3195 stmt= mysql_stmt_init(mysql);
3196 FAIL_IF(!stmt, mysql_error(mysql));
3197 rc= mysql_stmt_prepare(stmt, SL("SELECT @@sql_warnings"));
3198 check_stmt_rc(rc, stmt);
3199
3200 result= mysql_stmt_result_metadata(stmt);
3201 FAIL_IF(!result, "Invalid result set");
3202
3203 rc= mysql_stmt_execute(stmt);
3204 check_stmt_rc(rc, stmt);
3205
3206 rc= 0;
3207 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
3208 rc++;
3209 FAIL_UNLESS(rc == 1, "rowcount != 1");
3210
3211 if (verify_prepare_field(result, 0,
3212 "@@sql_warnings", "", /* field and its org name */
3213 MYSQL_TYPE_LONGLONG, /* field type */
3214 "", "", /* table and its org name */
3215 "", 1, 0)) /* db name, length */
3216 goto error;
3217
3218 mysql_free_result(result);
3219 mysql_stmt_close(stmt);
3220 return OK;
3221
3222error:
3223 mysql_free_result(result);
3224 mysql_stmt_close(stmt);
3225 return FAIL;
3226}
3227
3228/* Test a memory ovverun bug */
3229
3230static int test_mem_overun(MYSQL *mysql)
3231{
3232 char buffer[10000], field[12];
3233 MYSQL_STMT *stmt;
3234 MYSQL_RES *field_res, *res;
3235 int rc, i, length;
3236
3237 /*
3238 Test a memory ovverun bug when a table had 1000 fields with
3239 a row of data
3240 */
3241 rc= mysql_query(mysql, "drop table if exists t_mem_overun");
3242 check_mysql_rc(rc, mysql);
3243
3244 strcpy(buffer, "create table t_mem_overun(");
3245 for (i= 0; i < 1000; i++)
3246 {
3247 sprintf(field, "c%d int, ", i);
3248 strcat(buffer, field);
3249 }
3250 length= (int)strlen(buffer);
3251 buffer[length-2]= ')';
3252 buffer[--length]= '\0';
3253
3254 rc= mysql_real_query(mysql, buffer, length);
3255 check_mysql_rc(rc, mysql);
3256
3257 strcpy(buffer, "insert into t_mem_overun values(");
3258 for (i= 0; i < 1000; i++)
3259 {
3260 strcat(buffer, "1, ");
3261 }
3262 length= (int)strlen(buffer);
3263 buffer[length-2]= ')';
3264 buffer[--length]= '\0';
3265
3266 rc= mysql_real_query(mysql, buffer, length);
3267 check_mysql_rc(rc, mysql);
3268
3269 rc= mysql_query(mysql, "select * from t_mem_overun");
3270 check_mysql_rc(rc, mysql);
3271
3272 res= mysql_store_result(mysql);
3273 rc= 0;
3274 while (mysql_fetch_row(res))
3275 rc++;
3276 FAIL_UNLESS(rc == 1, "rowcount != 1");
3277 mysql_free_result(res);
3278
3279 stmt= mysql_stmt_init(mysql);
3280 FAIL_IF(!stmt, mysql_error(mysql));
3281 rc= mysql_stmt_prepare(stmt, SL("select * from t_mem_overun"));
3282 check_stmt_rc(rc, stmt);
3283
3284 rc= mysql_stmt_execute(stmt);
3285 check_stmt_rc(rc, stmt);
3286
3287 field_res= mysql_stmt_result_metadata(stmt);
3288 FAIL_IF(!field_res, "Invalid result set");
3289
3290 FAIL_UNLESS( 1000 == mysql_num_fields(field_res), "fields != 1000");
3291
3292 rc= mysql_stmt_store_result(stmt);
3293 check_stmt_rc(rc, stmt);
3294
3295 rc= mysql_stmt_fetch(stmt);
3296 check_stmt_rc(rc, stmt);
3297
3298 rc= mysql_stmt_fetch(stmt);
3299 FAIL_UNLESS(rc == MYSQL_NO_DATA, "");
3300
3301 mysql_free_result(field_res);
3302
3303 mysql_stmt_close(stmt);
3304 rc= mysql_query(mysql, "drop table if exists t_mem_overun");
3305 check_mysql_rc(rc, mysql);
3306 return OK;
3307}
3308
3309static int test_bug8722(MYSQL *mysql)
3310{
3311 MYSQL_STMT *stmt;
3312 int rc;
3313 const char *stmt_text;
3314
3315 /* Prepare test data */
3316 stmt_text= "drop table if exists t1";
3317 rc= mysql_real_query(mysql, SL(stmt_text));
3318 check_mysql_rc(rc, mysql);
3319 stmt_text= "drop view if exists v1";
3320 rc= mysql_real_query(mysql, SL(stmt_text));
3321 check_mysql_rc(rc, mysql);
3322 stmt_text= "CREATE TABLE t1 (c1 varchar(10), c2 varchar(10), c3 varchar(10),"
3323 " c4 varchar(10), c5 varchar(10), c6 varchar(10),"
3324 " c7 varchar(10), c8 varchar(10), c9 varchar(10),"
3325 "c10 varchar(10))";
3326 rc= mysql_real_query(mysql, SL(stmt_text));
3327 check_mysql_rc(rc, mysql);
3328 stmt_text= "INSERT INTO t1 VALUES (1,2,3,4,5,6,7,8,9,10)";
3329 rc= mysql_real_query(mysql, SL(stmt_text));
3330 check_mysql_rc(rc, mysql);
3331 stmt_text= "CREATE VIEW v1 AS SELECT * FROM t1";
3332 rc= mysql_real_query(mysql, SL(stmt_text));
3333 check_mysql_rc(rc, mysql);
3334
3335 stmt= mysql_stmt_init(mysql);
3336 stmt_text= "select * from v1";
3337 rc= mysql_stmt_prepare(stmt, SL(stmt_text));
3338 check_stmt_rc(rc, stmt);
3339 mysql_stmt_close(stmt);
3340 stmt_text= "drop table if exists t1, v1";
3341 rc= mysql_query(mysql, "DROP TABLE t1");
3342 check_mysql_rc(rc, mysql);
3343 rc= mysql_query(mysql, "DROP VIEW v1");
3344 check_mysql_rc(rc, mysql);
3345 return OK;
3346}
3347
3348/* Test DECIMAL conversion */
3349
3350static int test_decimal_bug(MYSQL *mysql)
3351{
3352 MYSQL_STMT *stmt;
3353 MYSQL_BIND my_bind[1];
3354 char data[30];
3355 int rc;
3356 my_bool is_null;
3357
3358 mysql_autocommit(mysql, TRUE);
3359
3360 rc= mysql_query(mysql, "drop table if exists test_decimal_bug");
3361 check_mysql_rc(rc, mysql);
3362
3363 rc= mysql_query(mysql, "create table test_decimal_bug(c1 decimal(10, 2))");
3364 check_mysql_rc(rc, mysql);
3365
3366 rc= mysql_query(mysql, "insert into test_decimal_bug value(8), (10.22), (5.61)");
3367 check_mysql_rc(rc, mysql);
3368
3369 stmt= mysql_stmt_init(mysql);
3370 FAIL_IF(!stmt, mysql_error(mysql));
3371 rc= mysql_stmt_prepare(stmt, SL("select c1 from test_decimal_bug where c1=?"));
3372 check_stmt_rc(rc, stmt);
3373
3374 /*
3375 We need to bzero bind structure because mysql_stmt_bind_param checks all
3376 its members.
3377 */
3378 memset(my_bind, '\0', sizeof(my_bind));
3379
3380 memset(data, 0, sizeof(data));
3381 my_bind[0].buffer_type= MYSQL_TYPE_NEWDECIMAL;
3382 my_bind[0].buffer= (void *)data;
3383 my_bind[0].buffer_length= 25;
3384 my_bind[0].is_null= &is_null;
3385
3386 is_null= 0;
3387 rc= mysql_stmt_bind_param(stmt, my_bind);
3388 check_stmt_rc(rc, stmt);
3389
3390 strcpy(data, "8.0");
3391 rc= mysql_stmt_execute(stmt);
3392 check_stmt_rc(rc, stmt);
3393
3394 data[0]= 0;
3395 rc= mysql_stmt_bind_result(stmt, my_bind);
3396 check_stmt_rc(rc, stmt);
3397
3398 rc= mysql_stmt_fetch(stmt);
3399 check_stmt_rc(rc, stmt);
3400
3401 FAIL_UNLESS(strcmp(data, "8.00") == 0, "data != '8.00'");
3402
3403 rc= mysql_stmt_fetch(stmt);
3404 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3405
3406 strcpy(data, "5.61");
3407 rc= mysql_stmt_execute(stmt);
3408 check_stmt_rc(rc, stmt);
3409
3410 data[0]= 0;
3411 rc= mysql_stmt_bind_result(stmt, my_bind);
3412 check_stmt_rc(rc, stmt);
3413
3414 rc= mysql_stmt_fetch(stmt);
3415 check_stmt_rc(rc, stmt);
3416
3417 FAIL_UNLESS(strcmp(data, "5.61") == 0, "data != '5.61'");
3418
3419 rc= mysql_stmt_fetch(stmt);
3420 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3421
3422 is_null= 1;
3423 rc= mysql_stmt_execute(stmt);
3424 check_stmt_rc(rc, stmt);
3425
3426 rc= mysql_stmt_fetch(stmt);
3427 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3428
3429 strcpy(data, "10.22"); is_null= 0;
3430 rc= mysql_stmt_execute(stmt);
3431 check_stmt_rc(rc, stmt);
3432
3433 data[0]= 0;
3434 rc= mysql_stmt_bind_result(stmt, my_bind);
3435 check_stmt_rc(rc, stmt);
3436
3437 rc= mysql_stmt_fetch(stmt);
3438 check_stmt_rc(rc, stmt);
3439
3440 FAIL_UNLESS(strcmp(data, "10.22") == 0, "data != '10.22'");
3441
3442 rc= mysql_stmt_fetch(stmt);
3443 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3444
3445 mysql_stmt_close(stmt);
3446 rc= mysql_query(mysql, "drop table if exists test_decimal_bug");
3447 check_mysql_rc(rc, mysql);
3448 return OK;
3449}
3450
3451/* Test EXPLAIN bug (#115, reported by mark@mysql.com & georg@php.net). */
3452
3453static int test_explain_bug(MYSQL *mysql)
3454{
3455 MYSQL_STMT *stmt;
3456 MYSQL_RES *result;
3457 int rc;
3458
3459 if (!is_mariadb)
3460 return SKIP;
3461
3462 mysql_autocommit(mysql, TRUE);
3463
3464 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_explain");
3465 check_mysql_rc(rc, mysql);
3466
3467 rc= mysql_query(mysql, "CREATE TABLE test_explain(id int, name char(2))");
3468 check_mysql_rc(rc, mysql);
3469
3470 stmt= mysql_stmt_init(mysql);
3471 FAIL_IF(!stmt, mysql_error(mysql));
3472 rc= mysql_stmt_prepare(stmt, SL("explain test_explain"));
3473 check_stmt_rc(rc, stmt);
3474
3475 rc= mysql_stmt_execute(stmt);
3476 check_stmt_rc(rc, stmt);
3477
3478 rc= 0;
3479 while (!mysql_stmt_fetch(stmt))
3480 rc++;
3481 FAIL_UNLESS(rc == 2, "rowcount != 2");
3482
3483 result= mysql_stmt_result_metadata(stmt);
3484 FAIL_IF(!result, "Invalid result set");
3485
3486 FAIL_UNLESS(6 == mysql_num_fields(result), "fields != 6");
3487
3488 if (verify_prepare_field(result, 0, "Field", "COLUMN_NAME",
3489 mysql_get_server_version(mysql) <= 50000 ?
3490 MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
3491 0, 0,
3492 mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3493 64, 0))
3494 goto error;
3495
3496 if (verify_prepare_field(result, 1, "Type", "COLUMN_TYPE", MYSQL_TYPE_BLOB,
3497 0, 0,
3498 mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3499 0, 0))
3500 goto error;
3501
3502 if (verify_prepare_field(result, 2, "Null", "IS_NULLABLE",
3503 mysql_get_server_version(mysql) <= 50000 ?
3504 MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
3505 0, 0,
3506 mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3507 3, 0))
3508 goto error;
3509
3510 if (verify_prepare_field(result, 3, "Key", "COLUMN_KEY",
3511 mysql_get_server_version(mysql) <= 50000 ?
3512 MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
3513 0, 0,
3514 mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3515 3, 0))
3516 goto error;
3517
3518 if ( mysql_get_server_version(mysql) >= 50027 )
3519 {
3520 /* The patch for bug#23037 changes column type of DEAULT to blob */
3521 if (verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
3522 MYSQL_TYPE_BLOB, 0, 0,
3523 mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3524 0, 0))
3525 goto error;
3526 }
3527 else
3528 {
3529 if (verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
3530 mysql_get_server_version(mysql) >= 50027 ?
3531 MYSQL_TYPE_BLOB :
3532 mysql_get_server_version(mysql) <= 50000 ?
3533 MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
3534 0, 0,
3535 mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3536 mysql_get_server_version(mysql) >= 50027 ? 0 :64, 0))
3537 goto error;
3538 }
3539
3540 if (verify_prepare_field(result, 5, "Extra", "EXTRA",
3541 mysql_get_server_version(mysql) <= 50000 ?
3542 MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
3543 0, 0,
3544 mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3545 27, 0))
3546 goto error;
3547
3548 mysql_free_result(result);
3549 mysql_stmt_close(stmt);
3550
3551 stmt= mysql_stmt_init(mysql);
3552 FAIL_IF(!stmt, mysql_error(mysql));
3553 rc= mysql_stmt_prepare(stmt, SL("explain select id, name FROM test_explain"));
3554 check_stmt_rc(rc, stmt);
3555
3556 rc= mysql_stmt_execute(stmt);
3557 check_stmt_rc(rc, stmt);
3558
3559 rc= 0;
3560 while (!mysql_stmt_fetch(stmt))
3561 rc++;
3562 FAIL_UNLESS(rc == 1, "rowcount != 1");
3563
3564 result= mysql_stmt_result_metadata(stmt);
3565 FAIL_IF(!result, "Invalid result set");
3566
3567 FAIL_UNLESS(10 == mysql_num_fields(result), "fields != 10");
3568
3569 if (verify_prepare_field(result, 0, "id", "", MYSQL_TYPE_LONGLONG, "", "", "", 3, 0))
3570 goto error;
3571
3572 if (verify_prepare_field(result, 1, "select_type", "", MYSQL_TYPE_VAR_STRING, "", "", "", 19, 0))
3573 goto error;
3574
3575 if (verify_prepare_field(result, 2, "table", "", MYSQL_TYPE_VAR_STRING, "", "", "", NAME_CHAR_LEN, 0))
3576 goto error;
3577
3578 if (verify_prepare_field(result, 3, "type", "", MYSQL_TYPE_VAR_STRING, "", "", "", 10, 0))
3579 goto error;
3580
3581 if (verify_prepare_field(result, 4, "possible_keys", "", MYSQL_TYPE_VAR_STRING, "", "", "", NAME_CHAR_LEN*MAX_KEY, 0))
3582 goto error;
3583
3584 if ( verify_prepare_field(result, 5, "key", "", MYSQL_TYPE_VAR_STRING, "", "", "", NAME_CHAR_LEN, 0))
3585 goto error;
3586
3587 if (mysql_get_server_version(mysql) <= 50000)
3588 {
3589 if (verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_LONGLONG, "", "", "", 3, 0))
3590 goto error;
3591 }
3592 else if (mysql_get_server_version(mysql) <= 60000)
3593 {
3594 if (verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_VAR_STRING, "", "", "", NAME_CHAR_LEN*MAX_KEY, 0))
3595 goto error;
3596 }
3597 else
3598 {
3599 if (verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_VAR_STRING, "", "", "", (MAX_KEY_LENGTH_DECIMAL_WIDTH + 1) * MAX_KEY, 0))
3600 goto error;
3601 }
3602
3603 if (verify_prepare_field(result, 7, "ref", "", MYSQL_TYPE_VAR_STRING, "", "", "",
3604 NAME_CHAR_LEN*16, 0))
3605 goto error;
3606
3607 if (verify_prepare_field(result, 8, "rows", "", MYSQL_TYPE_LONGLONG, "", "", "", 10, 0))
3608 goto error;
3609
3610 if (verify_prepare_field(result, 9, "Extra", "", MYSQL_TYPE_VAR_STRING, "", "", "", 255, 0))
3611 goto error;
3612
3613 mysql_free_result(result);
3614 mysql_stmt_close(stmt);
3615 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_explain");
3616 check_mysql_rc(rc, mysql);
3617 return OK;
3618error:
3619 mysql_free_result(result);
3620 mysql_stmt_close(stmt);
3621 return FAIL;
3622}
3623
3624static int test_sshort_bug(MYSQL *mysql)
3625{
3626 MYSQL_STMT *stmt;
3627 MYSQL_BIND my_bind[4];
3628 short short_value;
3629 int32 long_value;
3630 ulong s_length, l_length, ll_length, t_length;
3631 ulonglong longlong_value;
3632 int rc;
3633 uchar tiny_value;
3634
3635 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sshort");
3636 check_mysql_rc(rc, mysql);
3637
3638 rc= mysql_query(mysql, "CREATE TABLE test_sshort(a smallint signed, \
3639 b smallint signed, \
3640 c smallint unsigned, \
3641 d smallint unsigned)");
3642 check_mysql_rc(rc, mysql);
3643
3644 rc= mysql_query(mysql, "INSERT INTO test_sshort VALUES(-5999, -5999, 35999, 200)");
3645 check_mysql_rc(rc, mysql);
3646
3647
3648 stmt= mysql_stmt_init(mysql);
3649 FAIL_IF(!stmt, mysql_error(mysql));
3650 rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM test_sshort"));
3651 check_stmt_rc(rc, stmt);
3652
3653 rc= mysql_stmt_execute(stmt);
3654 check_stmt_rc(rc, stmt);
3655
3656 memset(my_bind, '\0', sizeof(my_bind));
3657 my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
3658 my_bind[0].buffer= (void *)&short_value;
3659 my_bind[0].length= &s_length;
3660
3661 my_bind[1].buffer_type= MYSQL_TYPE_LONG;
3662 my_bind[1].buffer= (void *)&long_value;
3663 my_bind[1].length= &l_length;
3664
3665 my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
3666 my_bind[2].buffer= (void *)&longlong_value;
3667 my_bind[2].length= &ll_length;
3668
3669 my_bind[3].buffer_type= MYSQL_TYPE_TINY;
3670 my_bind[3].buffer= (void *)&tiny_value;
3671 my_bind[3].is_unsigned= TRUE;
3672 my_bind[3].length= &t_length;
3673
3674 rc= mysql_stmt_bind_result(stmt, my_bind);
3675 check_stmt_rc(rc, stmt);
3676
3677 rc= mysql_stmt_fetch(stmt);
3678 check_stmt_rc(rc, stmt);
3679
3680 FAIL_UNLESS(short_value == -5999, "sv != -5999");
3681 FAIL_UNLESS(s_length == 2, "s_length != 2");
3682
3683 FAIL_UNLESS(long_value == -5999, "l_v != -5999");
3684 FAIL_UNLESS(l_length == 4, "l_length != 4");
3685
3686 FAIL_UNLESS(longlong_value == 35999, "llv != 35999");
3687 FAIL_UNLESS(ll_length == 8, "ll_length != 8");
3688
3689 FAIL_UNLESS(tiny_value == 200, "t_v != 200");
3690 FAIL_UNLESS(t_length == 1, "t_length != 1");
3691
3692 rc= mysql_stmt_fetch(stmt);
3693 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3694
3695 mysql_stmt_close(stmt);
3696 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sshort");
3697 check_mysql_rc(rc, mysql);
3698 return OK;
3699}
3700
3701
3702/* Test a misc tinyint-signed conversion bug */
3703
3704static int test_stiny_bug(MYSQL *mysql)
3705{
3706 MYSQL_STMT *stmt;
3707 MYSQL_BIND my_bind[4];
3708 short short_value;
3709 int32 long_value;
3710 ulong s_length, l_length, ll_length, t_length;
3711 ulonglong longlong_value;
3712 int rc;
3713 uchar tiny_value;
3714
3715 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stiny");
3716 check_mysql_rc(rc, mysql);
3717
3718 rc= mysql_query(mysql, "CREATE TABLE test_stiny(a tinyint signed, \
3719 b tinyint signed, \
3720 c tinyint unsigned, \
3721 d tinyint unsigned)");
3722 check_mysql_rc(rc, mysql);
3723
3724 rc= mysql_query(mysql, "INSERT INTO test_stiny VALUES(-128, -127, 255, 0)");
3725 check_mysql_rc(rc, mysql);
3726
3727
3728 stmt= mysql_stmt_init(mysql);
3729 FAIL_IF(!stmt, mysql_error(mysql));
3730 rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM test_stiny"));
3731 check_stmt_rc(rc, stmt);
3732
3733 rc= mysql_stmt_execute(stmt);
3734 check_stmt_rc(rc, stmt);
3735
3736 memset(my_bind, '\0', sizeof(my_bind));
3737 my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
3738 my_bind[0].buffer= (void *)&short_value;
3739 my_bind[0].length= &s_length;
3740
3741 my_bind[1].buffer_type= MYSQL_TYPE_LONG;
3742 my_bind[1].buffer= (void *)&long_value;
3743 my_bind[1].length= &l_length;
3744
3745 my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
3746 my_bind[2].buffer= (void *)&longlong_value;
3747 my_bind[2].length= &ll_length;
3748
3749 my_bind[3].buffer_type= MYSQL_TYPE_TINY;
3750 my_bind[3].buffer= (void *)&tiny_value;
3751 my_bind[3].length= &t_length;
3752
3753 rc= mysql_stmt_bind_result(stmt, my_bind);
3754 check_stmt_rc(rc, stmt);
3755
3756 rc= mysql_stmt_fetch(stmt);
3757 check_stmt_rc(rc, stmt);
3758
3759 FAIL_UNLESS(short_value == -128, "s_v != -128");
3760 FAIL_UNLESS(s_length == 2, "s_length != 2");
3761
3762 FAIL_UNLESS(long_value == -127, "l_v != -127");
3763 FAIL_UNLESS(l_length == 4, "l_length != 4");
3764
3765 FAIL_UNLESS(longlong_value == 255, "llv != 255");
3766 FAIL_UNLESS(ll_length == 8, "ll_length != 8");
3767
3768 FAIL_UNLESS(tiny_value == 0, "t_v != 0");
3769 FAIL_UNLESS(t_length == 1, "t_length != 1");
3770
3771 rc= mysql_stmt_fetch(stmt);
3772 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3773
3774 mysql_stmt_close(stmt);
3775 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stiny");
3776 check_mysql_rc(rc, mysql);
3777 return OK;
3778}
3779
3780static int test_bug53311(MYSQL *mysql)
3781{
3782 int rc;
3783 MYSQL_STMT *stmt;
3784 int i;
3785 const char *query= "INSERT INTO bug53311 VALUES (1)";
3786
3787 rc= mysql_options(mysql, MYSQL_OPT_RECONNECT, "1");
3788 check_mysql_rc(rc, mysql);
3789
3790 rc= mysql_query(mysql, "DROP TABLE IF EXISTS bug53311");
3791 check_mysql_rc(rc, mysql);
3792
3793 rc= mysql_query(mysql, "CREATE TABLE bug53311 (a int)");
3794 check_mysql_rc(rc, mysql);
3795
3796 stmt= mysql_stmt_init(mysql);
3797 rc= mysql_stmt_prepare(stmt, SL(query));
3798 check_stmt_rc(rc, stmt);
3799
3800 for (i=0; i < 2; i++)
3801 {
3802 rc= mysql_stmt_execute(stmt);
3803 check_stmt_rc(rc, stmt);
3804 }
3805
3806 /* kill connection */
3807 rc= mysql_kill(mysql, mysql_thread_id(mysql));
3808
3809 rc= mysql_stmt_execute(stmt);
3810 FAIL_IF(rc == 0, "Error expected");
3811 FAIL_IF(mysql_stmt_errno(stmt) == 0, "Errno != 0 expected");
3812 rc= mysql_stmt_close(stmt);
3813 check_mysql_rc(rc, mysql);
3814 rc= mysql_query(mysql, "DROP TABLE IF EXISTS bug53311");
3815 check_mysql_rc(rc, mysql);
3816
3817 return OK;
3818}
3819#define PREPARE_SQL "EXPLAIN SELECT t1.*, t2.* FROM test AS t1, test AS t2"
3820
3821#ifdef NOT_IN_USE
3822static int test_metadata(MYSQL *mysql)
3823{
3824 int rc;
3825
3826 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test");
3827 check_mysql_rc(rc, mysql);
3828 rc= mysql_query(mysql, "CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE=MYISAM");
3829 check_mysql_rc(rc, mysql);
3830
3831 rc= mysql_query(mysql, "INSERT INTO test(id, label) VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')");
3832 check_mysql_rc(rc, mysql);
3833 printf("Client=%s\n", mysql_get_client_info());
3834 printf("Server=%s\n", mysql_get_server_info(mysql));
3835
3836 {
3837 MYSQL_STMT * stmt = mysql_stmt_init(mysql);
3838 if (!stmt) {
3839 fprintf(stderr, "Failed to init stmt: Error: %s\n", mysql_error(mysql));
3840 goto end;
3841 }
3842 if (mysql_stmt_prepare(stmt, PREPARE_SQL, sizeof(PREPARE_SQL) - 1)) {
3843 fprintf(stderr, "Failed to prepare stmt: Error: %s\n", mysql_stmt_error(stmt));
3844 goto end2;
3845 }
3846 if (mysql_stmt_execute(stmt)) {
3847 fprintf(stderr, "Failed to execute stmt: Error: %s\n", mysql_stmt_error(stmt));
3848 goto end2;
3849 }
3850 {
3851 MYSQL_FIELD * field = NULL;
3852 MYSQL_RES * res = mysql_stmt_result_metadata(stmt);
3853 if (!res) {
3854 fprintf(stderr, "Failed to get metadata: Error: %s\n", mysql_stmt_error(stmt));
3855 goto end2;
3856 }
3857 while ((field = mysql_fetch_field(res))) {
3858 printf("name=%s\n", field->name);
3859 printf("catalog=%s\n", field->catalog);
3860 }
3861 mysql_free_result(res);
3862
3863 }
3864end2:
3865 mysql_stmt_close(stmt);
3866 }
3867end:
3868 return 0;
3869}
3870#endif
3871
3872static int test_conc_5(MYSQL *mysql)
3873{
3874 const char *query= "SELECT a FROM t1";
3875 MYSQL_RES *res;
3876 MYSQL_STMT *stmt;
3877 MYSQL_FIELD *fields;
3878 int rc;
3879
3880 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
3881 check_mysql_rc(rc, mysql);
3882 rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
3883 check_mysql_rc(rc, mysql);
3884 rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
3885 check_mysql_rc(rc, mysql);
3886
3887 stmt= mysql_stmt_init(mysql);
3888 FAIL_IF(!stmt, "couldn't allocate memory");
3889
3890 rc= mysql_stmt_prepare(stmt, SL(query));
3891 check_stmt_rc(rc, stmt);
3892 rc= mysql_stmt_execute(stmt);
3893 check_stmt_rc(rc, stmt);
3894
3895 res= mysql_stmt_result_metadata(stmt);
3896 FAIL_IF(!res, "Can't obtain resultset");
3897
3898 fields= mysql_fetch_fields(res);
3899 FAIL_IF(!fields, "Can't obtain fields");
3900
3901 FAIL_IF(strcmp("def", fields[0].catalog), "unexpected value for field->catalog");
3902
3903 mysql_free_result(res);
3904 mysql_stmt_close(stmt);
3905 return OK;
3906}
3907
3908static int test_conc141(MYSQL *mysql)
3909{
3910 int rc;
3911 const char *query= "CALL p_conc141";
3912 MYSQL_STMT *stmt= mysql_stmt_init(mysql);
3913
3914 rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc141");
3915 check_mysql_rc(rc, mysql);
3916 rc= mysql_query(mysql, "CREATE TABLE conc141 (KeyVal int not null primary key)");
3917 check_mysql_rc(rc, mysql);
3918 rc= mysql_query(mysql, "INSERT INTO conc141 VALUES(1)");
3919 check_mysql_rc(rc, mysql);
3920 rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p_conc141");
3921 check_mysql_rc(rc, mysql);
3922 rc= mysql_query(mysql, "CREATE PROCEDURE p_conc141()\n"
3923 "BEGIN\n"
3924 "select * from conc141;\n"
3925 "insert into conc141(KeyVal) VALUES(1);\n"
3926 "END");
3927 check_mysql_rc(rc, mysql);
3928
3929 rc= mysql_stmt_prepare(stmt, SL(query));
3930 check_stmt_rc(rc, stmt);
3931
3932 rc= mysql_stmt_execute(stmt);
3933 check_stmt_rc(rc, stmt);
3934 /* skip first result */
3935 rc= mysql_stmt_next_result(stmt);
3936 FAIL_IF(rc==-1, "No more results and error expected");
3937 mysql_stmt_free_result(stmt);
3938 FAIL_IF(mysql_stmt_errno(stmt), "No Error expected");
3939 rc= mysql_stmt_execute(stmt);
3940 check_stmt_rc(rc, stmt);
3941 mysql_stmt_close(stmt);
3942 rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc141");
3943 check_mysql_rc(rc, mysql);
3944 rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p_conc141");
3945 check_mysql_rc(rc, mysql);
3946 return OK;
3947}
3948
3949static int test_conc154(MYSQL *mysql)
3950{
3951 MYSQL_STMT *stmt;
3952 const char *stmtstr= "SELECT * FROM t1";
3953 int rc;
3954
3955 /* 1st: empty result set without free_result */
3956 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
3957 check_mysql_rc(rc, mysql);
3958 rc= mysql_query(mysql, "CREATE TABLE t1 (a varchar(20))");
3959 check_mysql_rc(rc, mysql);
3960
3961 stmt= mysql_stmt_init(mysql);
3962 rc= mysql_stmt_prepare(stmt, SL(stmtstr));
3963 check_stmt_rc(rc, stmt);
3964
3965 rc= mysql_stmt_execute(stmt);
3966 check_stmt_rc(rc, stmt);
3967
3968 rc= mysql_stmt_store_result(stmt);
3969 check_stmt_rc(rc, stmt);
3970
3971 rc= mysql_stmt_execute(stmt);
3972 check_stmt_rc(rc, stmt);
3973
3974 rc= mysql_stmt_store_result(stmt);
3975 check_stmt_rc(rc, stmt);
3976
3977 mysql_stmt_close(stmt);
3978
3979 /* 2nd: empty result set with free_result */
3980 stmt= mysql_stmt_init(mysql);
3981 rc= mysql_stmt_prepare(stmt, SL(stmtstr));
3982 check_stmt_rc(rc, stmt);
3983
3984 rc= mysql_stmt_execute(stmt);
3985 check_stmt_rc(rc, stmt);
3986
3987 rc= mysql_stmt_store_result(stmt);
3988 check_stmt_rc(rc, stmt);
3989
3990 rc= mysql_stmt_free_result(stmt);
3991 check_stmt_rc(rc, stmt);
3992
3993 rc= mysql_stmt_execute(stmt);
3994 check_stmt_rc(rc, stmt);
3995
3996 rc= mysql_stmt_store_result(stmt);
3997 check_stmt_rc(rc, stmt);
3998 rc= mysql_stmt_free_result(stmt);
3999 check_stmt_rc(rc, stmt);
4000
4001 mysql_stmt_close(stmt);
4002
4003 /* 3rd: non empty result without free_result */
4004 rc= mysql_query(mysql, "INSERT INTO t1 VALUES ('test_conc154')");
4005 check_mysql_rc(rc, mysql);
4006
4007 stmt= mysql_stmt_init(mysql);
4008 rc= mysql_stmt_prepare(stmt, SL(stmtstr));
4009 check_stmt_rc(rc, stmt);
4010
4011 rc= mysql_stmt_execute(stmt);
4012 check_stmt_rc(rc, stmt);
4013
4014 rc= mysql_stmt_store_result(stmt);
4015 check_stmt_rc(rc, stmt);
4016
4017 rc= mysql_stmt_execute(stmt);
4018 check_stmt_rc(rc, stmt);
4019
4020 rc= mysql_stmt_store_result(stmt);
4021 check_stmt_rc(rc, stmt);
4022
4023 mysql_stmt_close(stmt);
4024
4025 /* 4th non empty result set with free_result */
4026 stmt= mysql_stmt_init(mysql);
4027 rc= mysql_stmt_prepare(stmt, SL(stmtstr));
4028 check_stmt_rc(rc, stmt);
4029
4030 rc= mysql_stmt_execute(stmt);
4031 check_stmt_rc(rc, stmt);
4032
4033 rc= mysql_stmt_store_result(stmt);
4034 check_stmt_rc(rc, stmt);
4035
4036 rc= mysql_stmt_free_result(stmt);
4037 check_stmt_rc(rc, stmt);
4038
4039 rc= mysql_stmt_execute(stmt);
4040 check_stmt_rc(rc, stmt);
4041
4042 rc= mysql_stmt_store_result(stmt);
4043 check_stmt_rc(rc, stmt);
4044 rc= mysql_stmt_free_result(stmt);
4045 check_stmt_rc(rc, stmt);
4046
4047 mysql_stmt_close(stmt);
4048 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4049 check_mysql_rc(rc, mysql);
4050
4051 return OK;
4052}
4053
4054static int test_conc155(MYSQL *mysql)
4055{
4056 MYSQL_STMT *stmt;
4057 MYSQL_BIND bind;
4058 char buffer[50];
4059 int rc;
4060
4061 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4062 check_mysql_rc(rc, mysql);
4063 rc= mysql_query(mysql, "CREATE TABLE t1 (a TEXT)");
4064 check_mysql_rc(rc, mysql);
4065 rc= mysql_query(mysql, "INSERT INTO t1 VALUES ('zero terminated string')");
4066 check_mysql_rc(rc, mysql);
4067
4068 stmt= mysql_stmt_init(mysql);
4069 rc= mysql_stmt_prepare(stmt, SL("SELECT a FROM t1"));
4070 check_stmt_rc(rc, stmt);
4071
4072 rc= mysql_stmt_execute(stmt);
4073 check_stmt_rc(rc, stmt);
4074
4075 memset(buffer, 'X', 50);
4076 memset(&bind, 0, sizeof(MYSQL_BIND));
4077
4078 bind.buffer= buffer;
4079 bind.buffer_length= 50;
4080 bind.buffer_type= MYSQL_TYPE_STRING;
4081
4082 rc= mysql_stmt_bind_result(stmt, &bind);
4083 check_stmt_rc(rc, stmt);
4084
4085 rc= mysql_stmt_fetch(stmt);
4086 check_stmt_rc(rc, stmt);
4087
4088 if (strlen(buffer) != strlen("zero terminated string"))
4089 {
4090 diag("Wrong buffer length");
4091 return FAIL;
4092 }
4093
4094 mysql_stmt_close(stmt);
4095 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4096 check_mysql_rc(rc, mysql);
4097 return OK;
4098}
4099
4100static int test_conc168(MYSQL *mysql)
4101{
4102 MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4103
4104 MYSQL_BIND bind;
4105 char buffer[100];
4106 int rc;
4107
4108 rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc168");
4109 check_mysql_rc(rc, mysql);
4110 rc= mysql_query(mysql, "CREATE TABLE conc168(a datetime(3))");
4111 check_mysql_rc(rc, mysql);
4112 rc= mysql_query(mysql, "INSERT INTO conc168 VALUES ('2016-03-09 07:51:49.000'),('2016-03-09 07:51:49.001'),('2016-03-09 07:51:49.010')");
4113 check_mysql_rc(rc, mysql);
4114
4115 memset(&bind, 0, sizeof(MYSQL_BIND));
4116 bind.buffer= buffer;
4117 bind.buffer_type= MYSQL_TYPE_STRING;
4118 bind.buffer_length= 100;
4119
4120 rc= mysql_stmt_prepare(stmt, SL("SELECT a FROM conc168"));
4121 check_stmt_rc(rc, stmt);
4122
4123 rc= mysql_stmt_execute(stmt);
4124 check_stmt_rc(rc, stmt);
4125
4126 rc= mysql_stmt_bind_result(stmt, &bind);
4127 check_stmt_rc(rc, stmt);
4128
4129 rc= mysql_stmt_fetch(stmt);
4130 check_stmt_rc(rc, stmt);
4131 FAIL_IF(strcmp(buffer, "2016-03-09 07:51:49.000"), "expected: 2016-03-09 07:51:49.000");
4132
4133 rc= mysql_stmt_fetch(stmt);
4134 check_stmt_rc(rc, stmt);
4135 FAIL_IF(strcmp(buffer, "2016-03-09 07:51:49.001"), "expected: 2016-03-09 07:51:49.001");
4136
4137 rc= mysql_stmt_fetch(stmt);
4138 check_stmt_rc(rc, stmt);
4139 FAIL_IF(strcmp(buffer, "2016-03-09 07:51:49.010"), "expected: 2016-03-09 07:51:49.010");
4140
4141 mysql_stmt_close(stmt);
4142 rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc168");
4143 check_mysql_rc(rc, mysql);
4144 return OK;
4145}
4146
4147static int test_conc167(MYSQL *mysql)
4148{
4149 MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4150
4151 MYSQL_BIND bind[3];
4152 char buffer[100];
4153 int bit1=0, bit2=0;
4154 int rc;
4155 const char *stmt_str= "SELECT a,b,c FROM conc168";
4156
4157 rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc168");
4158 check_mysql_rc(rc, mysql);
4159 rc= mysql_query(mysql, "CREATE TABLE conc168(a bit, b bit, c varchar(10))");
4160 check_mysql_rc(rc, mysql);
4161 rc= mysql_query(mysql, "INSERT INTO conc168 VALUES (1,0, 'test12345')");
4162 check_mysql_rc(rc, mysql);
4163
4164 memset(bind, 0, 3 * sizeof(MYSQL_BIND));
4165 bind[0].buffer= &bit1;
4166 bind[0].buffer_type= MYSQL_TYPE_BIT;
4167 bind[0].buffer_length= sizeof(int);
4168 bind[1].buffer= &bit2;
4169 bind[1].buffer_type= MYSQL_TYPE_BIT;
4170 bind[1].buffer_length= sizeof(int);
4171 bind[2].buffer= buffer;
4172 bind[2].buffer_type= MYSQL_TYPE_STRING;
4173 bind[2].buffer_length= 100;
4174
4175 rc= mysql_stmt_prepare(stmt, SL(stmt_str));
4176 check_stmt_rc(rc, stmt);
4177
4178 rc= mysql_stmt_execute(stmt);
4179 check_stmt_rc(rc, stmt);
4180
4181 rc= mysql_stmt_bind_result(stmt, bind);
4182 check_stmt_rc(rc, stmt);
4183
4184 rc= mysql_stmt_store_result(stmt);
4185 check_stmt_rc(rc, stmt);
4186
4187 rc= mysql_stmt_fetch(stmt);
4188 check_stmt_rc(rc, stmt);
4189
4190 diag("bit=%d %d char=%s", bit1, bit2, buffer);
4191
4192 mysql_stmt_close(stmt);
4193 return OK;
4194}
4195
4196static int test_conc177(MYSQL *mysql)
4197{
4198 MYSQL_STMT *stmt;
4199 int rc;
4200 MYSQL_BIND bind[2];
4201 const char *stmt_str= "SELECT a,b FROM t1";
4202 char buf1[128], buf2[128];
4203
4204 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4205 check_mysql_rc(rc, mysql);
4206
4207 rc= mysql_query(mysql, "CREATE TABLE t1 (a double zerofill default 8.8,b float zerofill default 8.8)");
4208 check_mysql_rc(rc, mysql);
4209 rc= mysql_query(mysql, "INSERT INTO t1 VALUES (DEFAULT, DEFAULT)");
4210 check_mysql_rc(rc, mysql);
4211
4212 stmt= mysql_stmt_init(mysql);
4213 rc= mysql_stmt_prepare(stmt, SL(stmt_str));
4214 check_stmt_rc(rc, stmt);
4215 rc= mysql_stmt_execute(stmt);
4216 check_stmt_rc(rc, stmt);
4217
4218 memset(bind, 0, 2 * sizeof(MYSQL_BIND));
4219 bind[0].buffer= &buf1;
4220 bind[0].buffer_type= MYSQL_TYPE_STRING;
4221 bind[0].buffer_length= 128;
4222 bind[1].buffer= &buf2;
4223 bind[1].buffer_type= MYSQL_TYPE_STRING;
4224 bind[1].buffer_length= 128;
4225
4226 rc= mysql_stmt_bind_result(stmt, bind);
4227 check_stmt_rc(rc, stmt);
4228
4229 rc= mysql_stmt_fetch(stmt);
4230 mysql_stmt_close(stmt);
4231
4232 diag("buf1 %s\nbuf2 %s", buf1, buf2);
4233
4234 FAIL_IF(strcmp(buf1, "00000000000000000008.8"), "Expected 00000000000000000008.8");
4235 FAIL_IF(strcmp(buf2, "0000000008.8"), "Expected 0000000008.8");
4236
4237 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4238 check_mysql_rc(rc, mysql);
4239
4240 rc= mysql_query(mysql, "CREATE TABLE t1 (a int(8) zerofill default 1, b int(4) zerofill default 1)");
4241 check_mysql_rc(rc, mysql);
4242 rc= mysql_query(mysql, "INSERT INTO t1 VALUES (DEFAULT, DEFAULT)");
4243 check_mysql_rc(rc, mysql);
4244
4245 stmt= mysql_stmt_init(mysql);
4246 rc= mysql_stmt_prepare(stmt, SL(stmt_str));
4247 check_stmt_rc(rc, stmt);
4248 rc= mysql_stmt_execute(stmt);
4249 check_stmt_rc(rc, stmt);
4250
4251 memset(bind, 0, 2 * sizeof(MYSQL_BIND));
4252 bind[0].buffer= &buf1;
4253 bind[0].buffer_type= MYSQL_TYPE_STRING;
4254 bind[0].buffer_length= 128;
4255 bind[1].buffer= &buf2;
4256 bind[1].buffer_type= MYSQL_TYPE_STRING;
4257 bind[1].buffer_length= 128;
4258
4259 rc= mysql_stmt_bind_result(stmt, bind);
4260 check_stmt_rc(rc, stmt);
4261
4262 rc= mysql_stmt_fetch(stmt);
4263 mysql_stmt_close(stmt);
4264
4265 diag("buf1 %s\nbuf2 %s", buf1, buf2);
4266
4267 FAIL_IF(strcmp(buf1, "00000001"), "Expected 00000001");
4268 FAIL_IF(strcmp(buf2, "0001"), "Expected 0001");
4269 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4270 check_mysql_rc(rc, mysql);
4271 return OK;
4272}
4273
4274static int test_conc179(MYSQL *mysql)
4275{
4276 MYSQL_STMT *stmt;
4277 int rc;
4278 const char *stmtstr= "CREATE TABLE t1 (`blurb_id` int NOT NULL DEFAULT 0, `blurb` text default '', PRIMARY KEY (blurb_id)) ENGINE='FEDERATED' DEFAULT CHARSET=latin1 CONNECTION='mysql://root@127.0.0.1:$SLAVE_MYPORT/test/t1'";
4279
4280 rc= mysql_query(mysql, "set sql_mode=''");
4281 check_mysql_rc(rc, mysql);
4282
4283 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4284 check_mysql_rc(rc, mysql);
4285
4286 stmt= mysql_stmt_init(mysql);
4287 rc= mysql_stmt_prepare(stmt, SL(stmtstr));
4288 check_stmt_rc(rc, stmt);
4289
4290 if (mysql_get_server_version(mysql) >= 100100)
4291 {
4292 FAIL_IF(mysql_warning_count(mysql) < 2, "expected 2 or more warnings");
4293 FAIL_IF(mysql_stmt_warning_count(stmt) < 2, "expected 2 or more warnings");
4294 }
4295
4296 mysql_stmt_close(stmt);
4297 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4298 check_mysql_rc(rc, mysql);
4299
4300 return OK;
4301}
4302
4303static int test_conc182(MYSQL *mysql)
4304{
4305 MYSQL_STMT *stmt;
4306 int rc;
4307 MYSQL_BIND bind[2];
4308 char buf1[22];
4309 MYSQL_RES *result;
4310 MYSQL_ROW row;
4311
4312 stmt= mysql_stmt_init(mysql);
4313 rc= mariadb_stmt_execute_direct(stmt, "DROP TABLE IF EXISTS t1", -1);
4314 check_stmt_rc(rc, stmt);
4315 rc= mariadb_stmt_execute_direct(stmt, "DROP TABLE IF EXISTS t1", -1);
4316 check_stmt_rc(rc, stmt);
4317 rc= mariadb_stmt_execute_direct(stmt, "SELECT 1", -1);
4318 check_stmt_rc(rc, stmt);
4319 rc= mariadb_stmt_execute_direct(stmt, "SELECT 1", -1);
4320 check_stmt_rc(rc, stmt);
4321
4322 rc= mysql_stmt_close(stmt);
4323 check_mysql_rc(rc, mysql);
4324
4325 rc= mysql_query(mysql, "SELECT row_count()");
4326 result= mysql_store_result(mysql);
4327 row= mysql_fetch_row(result);
4328 diag("buf: %s", row[0]);
4329 mysql_free_result(result);
4330
4331
4332 stmt= mysql_stmt_init(mysql);
4333 rc= mysql_stmt_prepare(stmt, "SELECT row_count()", -1);
4334 check_stmt_rc(rc, stmt);
4335 rc= mysql_stmt_execute(stmt);
4336
4337 memset(bind, 0, 2 * sizeof(MYSQL_BIND));
4338 bind[0].buffer= &buf1;
4339 bind[0].buffer_length= bind[1].buffer_length= 20;
4340 bind[0].buffer_type= bind[1].buffer_type= MYSQL_TYPE_STRING;
4341
4342 rc= mysql_stmt_bind_result(stmt, bind);
4343
4344 while(!mysql_stmt_fetch(stmt))
4345 diag("b1: %s", buf1);
4346 rc= mysql_stmt_close(stmt);
4347 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4348 check_mysql_rc(rc, mysql);
4349 return OK;
4350}
4351
4352static int test_conc181(MYSQL *mysql)
4353{
4354 MYSQL_STMT *stmt;
4355 int rc;
4356 MYSQL_BIND bind;
4357 const char *stmt_str= "SELECT a FROM t1";
4358 float f=1;
4359 my_bool err= 0;
4360
4361 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4362 check_mysql_rc(rc, mysql);
4363 rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
4364 check_mysql_rc(rc, mysql);
4365 rc= mysql_query(mysql, "INSERT INTO t1 VALUES(1073741825)");
4366 check_mysql_rc(rc, mysql);
4367
4368 stmt= mysql_stmt_init(mysql);
4369 rc= mysql_stmt_prepare(stmt, SL(stmt_str));
4370 check_stmt_rc(rc, stmt);
4371
4372 rc= mysql_stmt_execute(stmt);
4373 check_stmt_rc(rc, stmt);
4374
4375 memset(&bind, 0, sizeof(MYSQL_BIND));
4376 bind.buffer= &f;
4377 bind.error= &err;
4378 bind.buffer_type= MYSQL_TYPE_FLOAT;
4379 rc= mysql_stmt_bind_result(stmt, &bind);
4380 check_stmt_rc(rc, stmt);
4381
4382 rc= mysql_stmt_fetch(stmt);
4383 diag("rc=%d err=%d float=%f, %d", rc, err, f, MYSQL_DATA_TRUNCATED);
4384
4385 rc= mysql_stmt_close(stmt);
4386 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4387 check_mysql_rc(rc, mysql);
4388 return OK;
4389}
4390
4391static int test_conc198(MYSQL *mysql)
4392{
4393 MYSQL_STMT *stmt1, *stmt2;
4394 MYSQL_BIND my_bind[1];
4395 int32 a;
4396 int rc;
4397 int num_rows= 0;
4398 ulong type;
4399 ulong prefetch_rows= 3;
4400
4401
4402 mysql_query(mysql, "drop table if exists t1");
4403 mysql_query(mysql, "create table t1 (id integer not null primary key)");
4404 rc= mysql_query(mysql, "insert into t1 (id) values "
4405 " (1), (2), (3), (4), (5), (6), (7), (8), (9)");
4406 check_mysql_rc(rc, mysql);
4407
4408 stmt1= mysql_stmt_init(mysql);
4409 stmt2= mysql_stmt_init(mysql);
4410 /* Not implemented in 5.0 */
4411 type= (ulong) CURSOR_TYPE_SCROLLABLE;
4412 rc= mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (void*) &type);
4413 FAIL_UNLESS(rc, "Error expected");
4414 rc= mysql_stmt_attr_set(stmt2, STMT_ATTR_CURSOR_TYPE, (void*) &type);
4415 FAIL_UNLESS(rc, "Error expected");
4416
4417 type= (ulong) CURSOR_TYPE_READ_ONLY;
4418 rc= mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (void*) &type);
4419 check_stmt_rc(rc, stmt1);
4420 rc= mysql_stmt_attr_set(stmt2, STMT_ATTR_CURSOR_TYPE, (void*) &type);
4421 check_stmt_rc(rc, stmt2);
4422 rc= mysql_stmt_attr_set(stmt1, STMT_ATTR_PREFETCH_ROWS,
4423 (void*) &prefetch_rows);
4424 check_stmt_rc(rc, stmt1);
4425 rc= mysql_stmt_attr_set(stmt2, STMT_ATTR_PREFETCH_ROWS,
4426 (void*) &prefetch_rows);
4427 check_stmt_rc(rc, stmt2);
4428 rc= mysql_stmt_prepare(stmt1, "SELECT * FROM t1 ORDER by id ASC" , -1);
4429 check_stmt_rc(rc, stmt1);
4430 rc= mysql_stmt_prepare(stmt2, "SELECT * FROM t1 ORDER by id DESC", -1);
4431 check_stmt_rc(rc, stmt2);
4432
4433 rc= mysql_stmt_execute(stmt1);
4434 check_stmt_rc(rc, stmt1);
4435 rc= mysql_stmt_execute(stmt2);
4436 check_stmt_rc(rc, stmt2);
4437
4438 memset(my_bind, '\0', sizeof(my_bind));
4439 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
4440 my_bind[0].buffer= (void*) &a;
4441 my_bind[0].buffer_length= sizeof(a);
4442 mysql_stmt_bind_result(stmt1, my_bind);
4443 mysql_stmt_bind_result(stmt2, my_bind);
4444
4445 while ((rc= mysql_stmt_fetch(stmt1)) == 0)
4446 ++num_rows;
4447 FAIL_UNLESS(num_rows == 9, "num_rows != 9");
4448
4449 num_rows= 0;
4450 while ((rc= mysql_stmt_fetch(stmt2)) == 0)
4451 ++num_rows;
4452 FAIL_UNLESS(num_rows == 9, "num_rows != 9");
4453
4454 rc= mysql_stmt_close(stmt1);
4455 rc= mysql_stmt_close(stmt2);
4456 FAIL_UNLESS(rc == 0, "");
4457
4458 rc= mysql_query(mysql, "drop table t1");
4459 check_mysql_rc(rc, mysql);
4460 return OK;
4461}
4462
4463static int test_conc205(MYSQL *mysql)
4464{
4465 MYSQL_STMT *stmt;
4466 MYSQL_BIND my_bind[3];
4467 char data[8];
4468 ulong length[3];
4469 int rc, int_col;
4470 short smint_col;
4471 my_bool is_null[3];
4472 const char *query = "SELECT text_col, smint_col, int_col FROM test_conc205";
4473
4474 rc= mysql_query(mysql, "drop table if exists test_conc205");
4475 check_mysql_rc(rc, mysql);
4476 rc= mysql_query(mysql, "CREATE TABLE test_conc205 (text_col TEXT, smint_col SMALLINT, int_col INT)");
4477 check_mysql_rc(rc, mysql);
4478 rc= mysql_query(mysql, "INSERT INTO test_conc205 VALUES('data01', 21893, 1718038908), ('data2', -25734, -1857802040)");
4479 check_mysql_rc(rc, mysql);
4480
4481 stmt= mysql_stmt_init(mysql);
4482 FAIL_IF(!stmt, mysql_error(mysql));
4483
4484 rc= mysql_stmt_prepare(stmt, SL(query));
4485 check_stmt_rc(rc, stmt);
4486
4487 memset(my_bind, '\0', sizeof(my_bind));
4488 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
4489 my_bind[0].buffer= (void *)data;
4490 my_bind[0].buffer_length= sizeof(data);
4491 my_bind[0].is_null= &is_null[0];
4492 my_bind[0].length= &length[0];
4493
4494 my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
4495 my_bind[1].buffer= &smint_col;
4496 my_bind[1].buffer_length= 2;
4497 my_bind[1].is_null= &is_null[1];
4498 my_bind[1].length= &length[1];
4499
4500 my_bind[2].buffer_type= MYSQL_TYPE_LONG;
4501 my_bind[2].buffer= &int_col;
4502 my_bind[2].buffer_length= 4;
4503 my_bind[2].is_null= &is_null[2];
4504 my_bind[2].length= &length[2];
4505
4506 rc= mysql_stmt_execute(stmt);
4507 check_stmt_rc(rc, stmt);
4508
4509 rc= mysql_stmt_bind_result(stmt, my_bind);
4510 check_stmt_rc(rc, stmt);
4511
4512 rc= mysql_stmt_fetch(stmt);
4513 check_stmt_rc(rc, stmt);
4514
4515 FAIL_IF(length[0] != 6, "Wrong fetched string length");
4516 FAIL_IF(length[1] != 2, "Wrong fetched short length");
4517 FAIL_IF(length[2] != 4, "Wrong fetched int length");
4518
4519 FAIL_IF(strncmp(data, "data01", length[0] + 1) != 0, "Wrong string value");
4520 FAIL_IF(smint_col != 21893, "Expected 21893");
4521 FAIL_IF(int_col != 1718038908, "Expected 1718038908");
4522
4523 rc= mysql_stmt_fetch(stmt);
4524 check_stmt_rc(rc, stmt);
4525
4526 FAIL_IF(length[0] != 5, "Wrong fetched string length");
4527 FAIL_IF(length[1] != 2, "Wrong fetched short length");
4528 FAIL_IF(length[2] != 4, "Wrong fetched int length");
4529
4530 FAIL_IF(strncmp(data, "data2", length[0] + 1) != 0, "Wrong string value");
4531 FAIL_IF(smint_col != -25734, "Expected 21893");
4532 FAIL_IF(int_col != -1857802040, "Expected 1718038908");
4533
4534 rc= mysql_stmt_fetch(stmt);
4535 FAIL_IF(rc != MYSQL_NO_DATA, "Expected MYSQL_NO_DATA");
4536
4537 mysql_stmt_close(stmt);
4538
4539 rc= mysql_query(mysql, "drop table test_conc205");
4540 check_mysql_rc(rc, mysql);
4541
4542 return OK;
4543}
4544
4545static int test_conc217(MYSQL *mysql)
4546{
4547 MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4548 int rc;
4549
4550 rc= mariadb_stmt_execute_direct(stmt, "SELECT 1 FROM nonexisting_table", -1);
4551 FAIL_IF(rc==0, "Expected error\n");
4552 rc= mysql_query(mysql, "drop table if exists t_count");
4553 check_mysql_rc(rc, mysql);
4554 rc= mysql_stmt_close(stmt);
4555 check_mysql_rc(rc, mysql);
4556 return OK;
4557}
4558
4559static int test_conc208(MYSQL *mysql)
4560{
4561 MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4562 int rc;
4563 int data;
4564 MYSQL_BIND bind;
4565
4566 rc= mysql_stmt_prepare(stmt, "SELECT \"100\" UNION SELECT \"88\" UNION SELECT \"389789\"", -1);
4567 check_stmt_rc(rc, stmt);
4568
4569 memset(&bind, 0, sizeof(MYSQL_BIND));
4570 bind.buffer_type= MYSQL_TYPE_LONG;
4571 bind.buffer= (void *)&data;
4572
4573 rc= mysql_stmt_execute(stmt);
4574 check_stmt_rc(rc, stmt);
4575
4576 rc= mysql_stmt_bind_result(stmt, &bind);
4577
4578 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
4579 {
4580 diag("data=%d", data);
4581 FAIL_IF(data != 100 && data != 88 && data != 389789, "Wrong value");
4582 }
4583 mysql_stmt_close(stmt);
4584 return OK;
4585}
4586
4587static int test_mdev14165(MYSQL *mysql)
4588{
4589 int rc;
4590 MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4591 MYSQL_FIELD *fields;
4592 MYSQL_RES *result;
4593 my_bool val= 1;
4594 MYSQL_BIND bind[1];
4595 char buf1[52];
4596
4597 rc= mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, &val);
4598
4599 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4600 rc= mysql_query(mysql, "CREATE TABLE t1 (i INT(20) ZEROFILL)");
4601 check_mysql_rc(rc, mysql);
4602 rc= mysql_query(mysql, "INSERT INTO t1 VALUES (2),(1)");
4603 check_mysql_rc(rc, mysql);
4604 rc= mysql_stmt_prepare(stmt, "SELECT i FROM t1", -1);
4605 check_stmt_rc(rc, stmt);
4606
4607 rc= mysql_stmt_execute(stmt);
4608 check_stmt_rc(rc, stmt);
4609
4610 memset(bind, 0, sizeof(MYSQL_BIND));
4611 bind[0].buffer_type= MYSQL_TYPE_STRING;
4612 bind[0].buffer_length= 51;
4613 bind[0].buffer= buf1;
4614
4615 mysql_stmt_bind_result(stmt, bind);
4616
4617 rc= mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &val);
4618 check_stmt_rc(rc, stmt);
4619 rc= mysql_stmt_store_result(stmt);
4620 check_stmt_rc(rc, stmt);
4621
4622 result= mysql_stmt_result_metadata(stmt);
4623
4624 fields= mysql_fetch_fields(result);
4625
4626 FAIL_IF(fields[0].length < 20, "Expected length=20");
4627 FAIL_IF(fields[0].max_length < 20, "Expected max_length=20");
4628
4629 mysql_stmt_fetch(stmt);
4630
4631 FAIL_UNLESS(strcmp(buf1, "00000000000000000002") == 0, "Wrong result");
4632 mysql_free_result(result);
4633
4634 mysql_stmt_close(stmt);
4635
4636 rc= mysql_query(mysql, "DROP TABLE t1");
4637 check_mysql_rc(rc, mysql);
4638 return OK;
4639}
4640
4641static int test_compress(MYSQL *mysql)
4642{
4643 MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4644 int rc;
4645
4646 rc= mariadb_stmt_execute_direct(stmt, SL("SELECT 1 FROM DUAL"));
4647 check_stmt_rc(rc, stmt);
4648
4649 mysql_stmt_close(stmt);
4650
4651 return OK;
4652}
4653
4654static int test_codbc138(MYSQL *mysql)
4655{
4656 int rc;
4657 MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4658 MYSQL_BIND bind[1];
4659 MYSQL_TIME tm;
4660
4661 rc= mysql_stmt_prepare(stmt, SL("SELECT DATE_ADD('2018-02-01', INTERVAL -188 DAY)"));
4662 check_stmt_rc(rc, stmt);
4663
4664 rc= mysql_stmt_execute(stmt);
4665 check_stmt_rc(rc, stmt);
4666
4667 memset(bind, 0, sizeof(MYSQL_BIND));
4668 bind[0].buffer_type= MYSQL_TYPE_DATETIME;
4669 bind[0].buffer= &tm;
4670 bind[0].buffer_length= sizeof(MYSQL_TIME);
4671
4672 rc= mysql_stmt_bind_result(stmt, bind);
4673 check_stmt_rc(rc, stmt);
4674
4675 rc= mysql_stmt_fetch(stmt);
4676 check_stmt_rc(rc, stmt);
4677
4678 if (tm.year != 2017 && tm.day != 28 && tm.month != 7)
4679 {
4680 diag("Error: Expected 2017-07-02");
4681 return FAIL;
4682 }
4683 if (tm.minute | tm.second || tm.second_part)
4684 {
4685 diag("Error: minute, second or second_part is not zero");
4686 return FAIL;
4687 }
4688
4689 mysql_stmt_close(stmt);
4690 return OK;
4691}
4692
4693struct my_tests_st my_tests[] = {
4694 {"test_compress", test_compress, TEST_CONNECTION_NEW, CLIENT_COMPRESS, NULL, NULL},
4695 {"test_codbc138", test_codbc138, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4696 {"test_conc208", test_conc208, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4697 {"test_mdev14165", test_mdev14165, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4698 {"test_conc208", test_conc208, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4699 {"test_conc217", test_conc217, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4700 {"test_conc205", test_conc205, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4701 {"test_conc198", test_conc198, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4702 {"test_conc182", test_conc182, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4703 {"test_conc181", test_conc181, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4704 {"test_conc179", test_conc179, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4705 {"test_conc177", test_conc177, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4706 {"test_conc167", test_conc167, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4707 {"test_conc168", test_conc168, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4708 {"test_conc155", test_conc155, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
4709 {"test_conc154", test_conc154, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4710 {"test_conc141", test_conc141, TEST_CONNECTION_NEW, 0, NULL , NULL},
4711 {"test_conc67", test_conc67, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4712 {"test_conc_5", test_conc_5, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4713 {"test_bug1115", test_bug1115, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4714 {"test_bug1180", test_bug1180, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4715 {"test_bug1644", test_bug1644, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4716 {"test_bug11037", test_bug11037, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4717 {"test_bug11183", test_bug11183, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4718 {"test_bug12744", test_bug12744, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4719 {"test_bug1500", test_bug1500, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4720 {"test_bug15510", test_bug15510, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4721 {"test_bug15518", test_bug15518, TEST_CONNECTION_NEW | TEST_CONNECTION_DONT_CLOSE, CLIENT_MULTI_STATEMENTS, NULL , NULL},
4722 {"test_bug15613", test_bug15613, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4723 {"test_bug16144", test_bug16144, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4724 {"test_bug1664", test_bug1664, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4725 {"test_bug1946", test_bug1946, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4726 {"test_bug2247", test_bug2247, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4727 {"test_bug2248", test_bug2248, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4728 {"test_bug20152", test_bug20152, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4729 {"test_bug23383", test_bug23383, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4730 {"test_bug27592", test_bug27592, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4731 {"test_bug28934", test_bug28934, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4732 {"test_bug36004", test_bug36004, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4733 {"test_bug3035", test_bug3035, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4734 {"test_bug3117", test_bug3117, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4735 {"test_bug3796", test_bug3796, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4736 {"test_bug4026", test_bug4026, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4737 {"test_bug4030", test_bug4030, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4738 {"test_bug4079", test_bug4079, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4739 {"test_bug4172", test_bug4172, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4740 {"test_bug4231", test_bug4231, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4741 {"test_bug4236", test_bug4236, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4742 {"test_bug5126", test_bug5126, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4743 {"test_bug5194", test_bug5194, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4744 {"test_bug5315", test_bug5315, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4745 {"test_bug5399", test_bug5399, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4746 {"test_bug6046", test_bug6046, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4747 {"test_bug6049", test_bug6049, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4748 {"test_bug6058", test_bug6058, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4749 {"test_bug6059", test_bug6059, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4750 {"test_bug6096", test_bug6096, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4751 {"test_bug7990", test_bug7990, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4752 {"test_bug8330", test_bug8330, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4753 {"test_bug8722", test_bug8722, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4754 {"test_ps_conj_select", test_ps_conj_select, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4755 {"test_ps_null_param", test_ps_null_param, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4756 {"test_ps_query_cache", test_ps_query_cache, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4757 {"test_ushort_bug", test_ushort_bug, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4758 {"test_field_misc", test_field_misc, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4759 {"test_mem_overun", test_mem_overun, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4760 {"test_decimal_bug", test_decimal_bug, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4761 {"test_explain_bug", test_explain_bug, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4762 {"test_sshort_bug", test_sshort_bug, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4763 {"test_stiny_bug", test_stiny_bug, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
4764 {"test_bug53311", test_bug53311, TEST_CONNECTION_NEW, 0, NULL , NULL},
4765 {NULL, NULL, 0, 0, NULL, NULL}
4766};
4767
4768int main(int argc, char **argv)
4769{
4770 if (argc > 1)
4771 get_options(argc, argv);
4772
4773 get_envvars();
4774
4775 run_tests(my_tests);
4776
4777 return(exit_status());
4778}
4779
4780