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/**
25 Some basic tests of the client API.
26*/
27
28#include "my_test.h"
29
30static int client_store_result(MYSQL *mysql)
31{
32 MYSQL_RES *result;
33 int rc, rowcount= 0;
34
35 rc= mysql_query(mysql, "SELECT 'foo' FROM DUAL UNION SELECT 'bar' FROM DUAL");
36 check_mysql_rc(rc, mysql);
37
38 /* get the result */
39 result= mysql_store_result(mysql);
40 FAIL_IF(!result, "Invalid result set");
41
42 /* since we use store result, we should be able execute other api calls */
43 rc= mysql_ping(mysql);
44 FAIL_IF(rc, "mysql_ping failed");
45
46 while (mysql_fetch_row(result))
47 rowcount++;
48
49 FAIL_IF(rowcount != 2, "rowcount != 2");
50
51 mysql_free_result(result);
52
53 return OK;
54}
55
56static int client_use_result(MYSQL *mysql)
57{
58 MYSQL_RES *result;
59 int rc, rowcount= 0;
60
61 rc= mysql_query(mysql, "SELECT 'foo' FROM DUAL UNION SELECT 'bar' FROM DUAL");
62 check_mysql_rc(rc, mysql);
63
64 /* get the result */
65 result= mysql_use_result(mysql);
66 FAIL_IF(!result, "Invalid result set");
67
68 /* since we use use result, we shouldn't be able execute other api calls */
69 rc= mysql_ping(mysql);
70 FAIL_IF(!rc, "Error expected");
71
72 while (mysql_fetch_row(result))
73 rowcount++;
74
75 FAIL_IF(rowcount != 2, "rowcount != 2");
76
77 mysql_free_result(result);
78
79 return OK;
80}
81
82static int test_free_result(MYSQL *mysql)
83{
84 MYSQL_STMT *stmt;
85 MYSQL_BIND my_bind[1];
86 char c2[5];
87 ulong bl1, l2;
88 int rc, c1, bc1;
89 char query[MAX_TEST_QUERY_LENGTH];
90
91 rc= mysql_query(mysql, "drop table if exists test_free_result");
92 check_mysql_rc(rc, mysql);
93
94 rc= mysql_query(mysql, "create table test_free_result("
95 "c1 int primary key auto_increment)");
96 check_mysql_rc(rc, mysql);
97
98 rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
99 check_mysql_rc(rc, mysql);
100
101 strcpy(query, "select * from test_free_result");
102 stmt= mysql_stmt_init(mysql);
103 FAIL_IF(!stmt, mysql_error(mysql));
104 rc= mysql_stmt_prepare(stmt, SL(query));
105 check_stmt_rc(rc, stmt);
106
107 memset(my_bind, '\0', sizeof(my_bind));
108 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
109 my_bind[0].buffer= (void *)&bc1;
110 my_bind[0].length= &bl1;
111
112 rc= mysql_stmt_execute(stmt);
113 check_stmt_rc(rc, stmt);
114
115 rc= mysql_stmt_bind_result(stmt, my_bind);
116 check_stmt_rc(rc, stmt);
117
118 rc= mysql_stmt_fetch(stmt);
119 check_stmt_rc(rc, stmt);
120
121 c2[0]= '\0'; l2= 0;
122 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
123 my_bind[0].buffer= (void *)c2;
124 my_bind[0].buffer_length= 7;
125 my_bind[0].is_null= 0;
126 my_bind[0].length= &l2;
127
128 rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
129 check_stmt_rc(rc, stmt);
130 FAIL_UNLESS(strncmp(c2, "1", 1) == 0, "c2 != '1'");
131 FAIL_UNLESS(l2 == 1, "l2 != 1");
132
133 rc= mysql_stmt_fetch(stmt);
134 check_stmt_rc(rc, stmt);
135
136 c1= 0, l2= 0;
137 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
138 my_bind[0].buffer= (void *)&c1;
139 my_bind[0].buffer_length= 0;
140 my_bind[0].is_null= 0;
141 my_bind[0].length= &l2;
142
143 rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
144 check_stmt_rc(rc, stmt);
145 FAIL_UNLESS(c1 == 2, "c1 != 2");
146 FAIL_UNLESS(l2 == 4, "l2 != 4");
147
148 rc= mysql_query(mysql, "drop table test_free_result");
149 FAIL_IF(!rc, "Error commands out of sync expected");
150
151 rc= mysql_stmt_free_result(stmt);
152 check_stmt_rc(rc, stmt);
153
154 rc= mysql_query(mysql, "drop table test_free_result");
155 check_mysql_rc(rc, mysql); /* should be successful */
156
157 mysql_stmt_close(stmt);
158
159 return OK;
160}
161
162
163/* Test mysql_stmt_store_result() */
164
165static int test_free_store_result(MYSQL *mysql)
166{
167 MYSQL_STMT *stmt;
168 MYSQL_BIND my_bind[1];
169 char c2[5];
170 ulong bl1, l2;
171 int rc, c1, bc1;
172 char query[MAX_TEST_QUERY_LENGTH];
173
174 rc= mysql_query(mysql, "drop table if exists test_free_result");
175 check_mysql_rc(rc, mysql);
176
177 rc= mysql_query(mysql, "create table test_free_result(c1 int primary key auto_increment)");
178 check_mysql_rc(rc, mysql);
179
180 rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
181 check_mysql_rc(rc, mysql);
182
183 strcpy(query, "select * from test_free_result");
184 stmt= mysql_stmt_init(mysql);
185 FAIL_IF(!stmt, mysql_error(mysql));
186 rc= mysql_stmt_prepare(stmt, SL(query));
187 check_stmt_rc(rc, stmt);
188
189 memset(my_bind, '\0', sizeof(my_bind));
190 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
191 my_bind[0].buffer= (void *)&bc1;
192 my_bind[0].buffer_length= 0;
193 my_bind[0].is_null= 0;
194 my_bind[0].length= &bl1;
195
196 rc= mysql_stmt_execute(stmt);
197 check_stmt_rc(rc, stmt);
198
199 rc= mysql_stmt_bind_result(stmt, my_bind);
200 check_stmt_rc(rc, stmt);
201
202 rc= mysql_stmt_store_result(stmt);
203 check_stmt_rc(rc, stmt);
204
205 rc= mysql_stmt_fetch(stmt);
206 check_stmt_rc(rc, stmt);
207
208 c2[0]= '\0'; l2= 0;
209 my_bind[0].buffer_type= MYSQL_TYPE_STRING;
210 my_bind[0].buffer= (void *)c2;
211 my_bind[0].buffer_length= 7;
212 my_bind[0].is_null= 0;
213 my_bind[0].length= &l2;
214
215 rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
216 check_stmt_rc(rc, stmt);
217 FAIL_UNLESS(strncmp(c2, "1", 1) == 0, "c2 != '1'");
218 FAIL_UNLESS(l2 == 1, "l2 != 1");
219
220 rc= mysql_stmt_fetch(stmt);
221 check_stmt_rc(rc, stmt);
222
223 c1= 0, l2= 0;
224 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
225 my_bind[0].buffer= (void *)&c1;
226 my_bind[0].buffer_length= 0;
227 my_bind[0].is_null= 0;
228 my_bind[0].length= &l2;
229
230 rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
231 check_stmt_rc(rc, stmt);
232 FAIL_UNLESS(c1 == 2, "c1 != 2");
233 FAIL_UNLESS(l2 == 4, "l2 != 4");
234
235 rc= mysql_stmt_free_result(stmt);
236 check_stmt_rc(rc, stmt);
237
238 rc= mysql_query(mysql, "drop table test_free_result");
239 check_mysql_rc(rc, mysql);
240
241 mysql_stmt_close(stmt);
242
243 return OK;
244}
245
246static int test_store_result(MYSQL *mysql)
247{
248 MYSQL_STMT *stmt;
249 int rc;
250 int32 nData;
251 char szData[100];
252 MYSQL_BIND my_bind[2];
253 ulong length, length1;
254 my_bool is_null[2];
255 char query[MAX_TEST_QUERY_LENGTH];
256
257 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
258 check_mysql_rc(rc, mysql);
259
260 rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
261 check_mysql_rc(rc, mysql);
262
263 rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
264 check_mysql_rc(rc, mysql);
265
266 rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
267 check_mysql_rc(rc, mysql);
268
269 rc= mysql_commit(mysql);
270 check_mysql_rc(rc, mysql);
271
272 /* fetch */
273 memset(my_bind, '\0', sizeof(my_bind));
274 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
275 my_bind[0].buffer= (void *) &nData; /* integer data */
276 my_bind[0].length= &length;
277 my_bind[0].is_null= &is_null[0];
278
279 length= 0;
280 my_bind[1].buffer_type= MYSQL_TYPE_STRING;
281 my_bind[1].buffer= szData; /* string data */
282 my_bind[1].buffer_length= sizeof(szData);
283 my_bind[1].length= &length1;
284 my_bind[1].is_null= &is_null[1];
285 length1= 0;
286
287 strcpy(query, "SELECT * FROM test_store_result");
288 stmt= mysql_stmt_init(mysql);
289 FAIL_IF(!stmt, mysql_error(mysql));
290 rc= mysql_stmt_prepare(stmt, SL(query));
291 check_stmt_rc(rc, stmt);
292
293 rc= mysql_stmt_bind_result(stmt, my_bind);
294 check_stmt_rc(rc, stmt);
295
296 rc= mysql_stmt_execute(stmt);
297 check_stmt_rc(rc, stmt);
298
299 rc= mysql_stmt_store_result(stmt);
300 check_stmt_rc(rc, stmt);
301
302 rc= mysql_stmt_fetch(stmt);
303 check_stmt_rc(rc, stmt);
304
305 FAIL_UNLESS(nData == 10, "nData != 10");
306 FAIL_UNLESS(strcmp(szData, "venu") == 0, "szData != 'Venu'");
307 FAIL_UNLESS(length1 == 4, "length1 != 4");
308
309 rc= mysql_stmt_fetch(stmt);
310 check_stmt_rc(rc, stmt);
311
312 FAIL_UNLESS(nData == 20, "nData != 20");
313 FAIL_UNLESS(strcmp(szData, "mysql") == 0, "szDaza != 'mysql'");
314 FAIL_UNLESS(length1 == 5, "length1 != 5");
315
316 length= 99;
317 rc= mysql_stmt_fetch(stmt);
318 check_stmt_rc(rc, stmt);
319
320 FAIL_UNLESS(is_null[0], "isnull set");
321 FAIL_UNLESS(strcmp(szData, "monty") == 0, "szData != 'monty'");
322 FAIL_UNLESS(length1 == 5, "length1 != 5");
323
324 rc= mysql_stmt_fetch(stmt);
325 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
326
327 rc= mysql_stmt_execute(stmt);
328 check_stmt_rc(rc, stmt);
329
330 rc= mysql_stmt_store_result(stmt);
331 check_stmt_rc(rc, stmt);
332
333 rc= mysql_stmt_fetch(stmt);
334 check_stmt_rc(rc, stmt);
335
336 FAIL_UNLESS(nData == 10, "nData != 10");
337 FAIL_UNLESS(strcmp(szData, "venu") == 0, "szData != 'Venu'");
338 FAIL_UNLESS(length1 == 4, "length1 != 4");
339
340 rc= mysql_stmt_fetch(stmt);
341 check_stmt_rc(rc, stmt);
342
343 FAIL_UNLESS(nData == 20, "nData != 20");
344 FAIL_UNLESS(strcmp(szData, "mysql") == 0, "szDaza != 'mysql'");
345 FAIL_UNLESS(length1 == 5, "length1 != 5");
346
347 length= 99;
348 rc= mysql_stmt_fetch(stmt);
349 check_stmt_rc(rc, stmt);
350
351 FAIL_UNLESS(is_null[0], "isnull set");
352 FAIL_UNLESS(strcmp(szData, "monty") == 0, "szData != 'monty'");
353 FAIL_UNLESS(length1 == 5, "length1 != 5");
354
355 rc= mysql_stmt_fetch(stmt);
356 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
357
358 mysql_stmt_close(stmt);
359 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
360 check_mysql_rc(rc, mysql);
361
362 return OK;
363}
364
365
366/* Test simple bind store result */
367
368static int test_store_result1(MYSQL *mysql)
369{
370 MYSQL_STMT *stmt;
371 int rc;
372 char query[MAX_TEST_QUERY_LENGTH];
373
374 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
375 check_mysql_rc(rc, mysql);
376
377 rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
378 check_mysql_rc(rc, mysql);
379
380 rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
381 check_mysql_rc(rc, mysql);
382
383 rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
384 check_mysql_rc(rc, mysql);
385
386 rc= mysql_commit(mysql);
387 check_mysql_rc(rc, mysql);
388
389 strcpy(query, "SELECT * FROM test_store_result");
390 stmt= mysql_stmt_init(mysql);
391 FAIL_IF(!stmt, mysql_error(mysql));
392 rc= mysql_stmt_prepare(stmt, SL(query));
393 check_stmt_rc(rc, stmt);
394
395 rc= mysql_stmt_execute(stmt);
396 check_stmt_rc(rc, stmt);
397
398 rc= mysql_stmt_store_result(stmt);
399 check_stmt_rc(rc, stmt);
400
401 rc= 0;
402 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
403 rc++;
404 FAIL_UNLESS(rc == 3, "rowcount != 3");
405
406 rc= mysql_stmt_execute(stmt);
407 check_stmt_rc(rc, stmt);
408
409 rc= mysql_stmt_store_result(stmt);
410 check_stmt_rc(rc, stmt);
411
412 rc= 0;
413 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
414 rc++;
415 FAIL_UNLESS(rc == 3, "rowcount != 3");
416
417 mysql_stmt_close(stmt);
418 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
419 check_mysql_rc(rc, mysql);
420
421 return OK;
422}
423
424
425/* Another test for bind and store result */
426
427static int test_store_result2(MYSQL *mysql)
428{
429 MYSQL_STMT *stmt;
430 int rc;
431 int nData;
432 ulong length;
433 MYSQL_BIND my_bind[1];
434 char query[MAX_TEST_QUERY_LENGTH];
435
436 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
437 check_mysql_rc(rc, mysql);
438
439 rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
440 check_mysql_rc(rc, mysql);
441
442 rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
443 check_mysql_rc(rc, mysql);
444
445 rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
446 check_mysql_rc(rc, mysql);
447
448 rc= mysql_commit(mysql);
449 check_mysql_rc(rc, mysql);
450
451 memset(my_bind, '\0', sizeof(my_bind));
452
453 my_bind[0].buffer_type= MYSQL_TYPE_LONG;
454 my_bind[0].buffer= (void *) &nData; /* integer data */
455 my_bind[0].length= &length;
456 my_bind[0].is_null= 0;
457
458 strcpy((char *)query , "SELECT col1 FROM test_store_result where col1= ?");
459 stmt= mysql_stmt_init(mysql);
460 FAIL_IF(!stmt, mysql_error(mysql));
461 rc= mysql_stmt_prepare(stmt, SL(query));
462 check_stmt_rc(rc, stmt);
463
464 rc= mysql_stmt_bind_param(stmt, my_bind);
465 check_stmt_rc(rc, stmt);
466
467 rc= mysql_stmt_bind_result(stmt, my_bind);
468 check_stmt_rc(rc, stmt);
469
470 nData= 10; length= 0;
471 rc= mysql_stmt_execute(stmt);
472 check_stmt_rc(rc, stmt);
473
474 nData= 0;
475 rc= mysql_stmt_store_result(stmt);
476 check_stmt_rc(rc, stmt);
477
478 rc= mysql_stmt_fetch(stmt);
479 check_stmt_rc(rc, stmt);
480
481 FAIL_UNLESS(nData == 10, "nData != 10");
482
483 rc= mysql_stmt_fetch(stmt);
484 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
485
486 nData= 20;
487 rc= mysql_stmt_execute(stmt);
488 check_stmt_rc(rc, stmt);
489
490 nData= 0;
491 rc= mysql_stmt_store_result(stmt);
492 check_stmt_rc(rc, stmt);
493
494 rc= mysql_stmt_fetch(stmt);
495 check_stmt_rc(rc, stmt);
496
497 FAIL_UNLESS(nData == 20, "nData != 20");
498
499 rc= mysql_stmt_fetch(stmt);
500 FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
501 mysql_stmt_close(stmt);
502 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
503 check_mysql_rc(rc, mysql);
504
505 return OK;
506}
507
508static int test_bug11718(MYSQL *mysql)
509{
510 MYSQL_RES *res;
511 int rc;
512 const char *query= "select str_to_date(concat(f3),'%Y%m%d') from t1,t2 "
513 "where f1=f2 order by f1";
514
515 rc= mysql_query(mysql, "drop table if exists t1, t2");
516 check_mysql_rc(rc, mysql);
517 rc= mysql_query(mysql, "create table t1 (f1 int)");
518 check_mysql_rc(rc, mysql);
519 rc= mysql_query(mysql, "create table t2 (f2 int, f3 numeric(8))");
520 check_mysql_rc(rc, mysql);
521 rc= mysql_query(mysql, "insert into t1 values (1), (2)");
522 check_mysql_rc(rc, mysql);
523 rc= mysql_query(mysql, "insert into t2 values (1,20050101), (2,20050202)");
524 check_mysql_rc(rc, mysql);
525 rc= mysql_query(mysql, query);
526 check_mysql_rc(rc, mysql);
527 res = mysql_store_result(mysql);
528
529 FAIL_UNLESS(res->fields[0].type == MYSQL_TYPE_DATE, "type != MYSQL_TYPE_DATE");
530 mysql_free_result(res);
531 rc= mysql_query(mysql, "drop table t1, t2");
532 check_mysql_rc(rc, mysql);
533
534 return OK;
535}
536
537static int test_bug19671(MYSQL *mysql)
538{
539 MYSQL_RES *result;
540 int rc;
541
542 mysql_query(mysql, "set sql_mode=''");
543 rc= mysql_query(mysql, "drop table if exists t1");
544 check_mysql_rc(rc, mysql)
545
546 rc= mysql_query(mysql, "drop view if exists v1");
547 check_mysql_rc(rc, mysql)
548
549 rc= mysql_query(mysql, "create table t1(f1 int)");
550 check_mysql_rc(rc, mysql)
551
552 rc= mysql_query(mysql, "create view v1 as select va.* from t1 va");
553 check_mysql_rc(rc, mysql)
554
555 result= mysql_list_fields(mysql, "v1", NULL);
556 FAIL_IF(!result, "Invalid result set");
557
558 rc= 0;
559 while (mysql_fetch_row(result))
560 rc++;
561 FAIL_UNLESS(rc == 0, "");
562
563 if (verify_prepare_field(result, 0, "f1", "f1", MYSQL_TYPE_LONG,
564 "v1", "v1", schema, 11, "0")) {
565 mysql_free_result(result);
566 diag("verify_prepare_field failed");
567 return FAIL;
568 }
569
570 mysql_free_result(result);
571 check_mysql_rc(mysql_query(mysql, "drop view v1"), mysql)
572 check_mysql_rc(mysql_query(mysql, "drop table t1"), mysql)
573 return OK;
574}
575
576/*
577 Bug#21726: Incorrect result with multiple invocations of
578 LAST_INSERT_ID
579
580 Test that client gets updated value of insert_id on UPDATE that uses
581 LAST_INSERT_ID(expr).
582 select_query added to test for bug
583 #26921 Problem in mysql_insert_id() Embedded C API function
584*/
585static int test_bug21726(MYSQL *mysql)
586{
587 const char *create_table[]=
588 {
589 "DROP TABLE IF EXISTS t1",
590 "CREATE TABLE t1 (i INT)",
591 "INSERT INTO t1 VALUES (1)",
592 };
593 const char *update_query= "UPDATE t1 SET i= LAST_INSERT_ID(i + 1)";
594 int rc;
595 unsigned long long insert_id;
596 const char *select_query= "SELECT * FROM t1";
597 MYSQL_RES *result;
598
599 rc= mysql_query(mysql, create_table[0]);
600 check_mysql_rc(rc, mysql);
601 rc= mysql_query(mysql, create_table[1]);
602 check_mysql_rc(rc, mysql);
603 rc= mysql_query(mysql, create_table[2]);
604 check_mysql_rc(rc, mysql);
605
606 rc= mysql_query(mysql, update_query);
607 check_mysql_rc(rc, mysql)
608 insert_id= mysql_insert_id(mysql);
609 FAIL_UNLESS(insert_id == 2, "insert_id != 2");
610
611 rc= mysql_query(mysql, update_query);
612 check_mysql_rc(rc, mysql)
613 insert_id= mysql_insert_id(mysql);
614 FAIL_UNLESS(insert_id == 3, "insert_id != 3");
615
616 rc= mysql_query(mysql, select_query);
617 check_mysql_rc(rc, mysql)
618 insert_id= mysql_insert_id(mysql);
619 FAIL_UNLESS(insert_id == 3, "insert_id != 3");
620 result= mysql_store_result(mysql);
621 mysql_free_result(result);
622
623 return OK;
624}
625
626/* Bug#6761 - mysql_list_fields doesn't work */
627
628static int test_bug6761(MYSQL *mysql)
629{
630 const char *stmt_text;
631 MYSQL_RES *res;
632 int rc;
633
634 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
635 check_mysql_rc(rc, mysql);
636
637 stmt_text= "CREATE TABLE t1 (a int, b char(255), c decimal)";
638 rc= mysql_real_query(mysql, stmt_text, (unsigned long)strlen(stmt_text));
639 check_mysql_rc(rc, mysql);
640
641 res= mysql_list_fields(mysql, "t1", "%");
642 FAIL_UNLESS(res && mysql_num_fields(res) == 3, "num_fields != 3");
643 mysql_free_result(res);
644
645 stmt_text= "DROP TABLE t1";
646 rc= mysql_real_query(mysql, stmt_text, (unsigned long)strlen(stmt_text));
647 check_mysql_rc(rc, mysql);
648 return OK;
649}
650
651/* Test field flags (verify .NET provider) */
652
653static int test_field_flags(MYSQL *mysql)
654{
655 int rc;
656 MYSQL_RES *result;
657 MYSQL_FIELD *field;
658
659 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_flags");
660 check_mysql_rc(rc, mysql);
661
662 rc= mysql_query(mysql, "CREATE TABLE test_field_flags(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, \
663 id1 int NOT NULL, \
664 id2 int UNIQUE, \
665 id3 int, \
666 id4 int NOT NULL, \
667 id5 int, \
668 KEY(id3, id4))");
669 check_mysql_rc(rc, mysql);
670
671 /* with table name included with TRUE column name */
672 rc= mysql_query(mysql, "SELECT * FROM test_field_flags");
673 check_mysql_rc(rc, mysql);
674
675 result= mysql_use_result(mysql);
676 FAIL_IF(!result, "Invalid result set");
677
678 mysql_field_seek(result, 0);
679
680 field= mysql_fetch_field(result);
681 FAIL_UNLESS(field->flags & NOT_NULL_FLAG &&
682 field->flags & PRI_KEY_FLAG &&
683 field->flags & AUTO_INCREMENT_FLAG, "Wrong flags for field 0");
684
685 field= mysql_fetch_field(result);
686 FAIL_UNLESS(field->flags & NOT_NULL_FLAG, "Wrong flags for field 1");
687
688 field= mysql_fetch_field(result);
689 FAIL_UNLESS(field->flags & UNIQUE_KEY_FLAG, "Wrong flags for field 2");
690
691 field= mysql_fetch_field(result);
692 FAIL_UNLESS(field->flags & MULTIPLE_KEY_FLAG, "Wrong flags for field 3");
693
694 field= mysql_fetch_field(result);
695 FAIL_UNLESS(field->flags & NOT_NULL_FLAG, "Wrong flags for field 4");
696
697 mysql_free_result(result);
698 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_flags");
699 check_mysql_rc(rc, mysql);
700 return OK;
701}
702
703/* Test real and alias names */
704
705static int test_field_names(MYSQL *mysql)
706{
707 int rc;
708 MYSQL_RES *result;
709
710
711 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names1");
712 check_mysql_rc(rc, mysql);
713
714 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names2");
715 check_mysql_rc(rc, mysql);
716
717 rc= mysql_query(mysql, "CREATE TABLE test_field_names1(id int, name varchar(50))");
718 check_mysql_rc(rc, mysql);
719
720 rc= mysql_query(mysql, "CREATE TABLE test_field_names2(id int, name varchar(50))");
721 check_mysql_rc(rc, mysql);
722
723 /* with table name included with TRUE column name */
724 rc= mysql_query(mysql, "SELECT id as 'id-alias' FROM test_field_names1");
725 check_mysql_rc(rc, mysql);
726
727 result= mysql_use_result(mysql);
728 FAIL_IF(!result, "Invalid result set");
729
730 rc= 0;
731 while (mysql_fetch_row(result))
732 rc++;
733 FAIL_UNLESS(rc == 0, "rowcount != 0");
734 mysql_free_result(result);
735
736 /* with table name included with TRUE column name */
737 rc= mysql_query(mysql, "SELECT t1.id as 'id-alias', test_field_names2.name FROM test_field_names1 t1, test_field_names2");
738 check_mysql_rc(rc, mysql);
739
740 result= mysql_use_result(mysql);
741 FAIL_IF(!result, "Invalid result set");
742
743 rc= 0;
744 while (mysql_fetch_row(result))
745 rc++;
746 FAIL_UNLESS(rc == 0, "rowcount != 0");
747 mysql_free_result(result);
748 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names1");
749 check_mysql_rc(rc, mysql);
750
751 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names2");
752 check_mysql_rc(rc, mysql);
753 return OK;
754}
755
756/* Test FUNCTION field info / DATE_FORMAT() table_name . */
757
758static int test_func_fields(MYSQL *mysql)
759{
760 int rc;
761 MYSQL_RES *result;
762 MYSQL_FIELD *field;
763
764
765 rc= mysql_autocommit(mysql, TRUE);
766 check_mysql_rc(rc, mysql);
767
768 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_dateformat");
769 check_mysql_rc(rc, mysql);
770
771 rc= mysql_query(mysql, "CREATE TABLE test_dateformat(id int, \
772 ts timestamp)");
773 check_mysql_rc(rc, mysql);
774
775 rc= mysql_query(mysql, "INSERT INTO test_dateformat(id) values(10)");
776 check_mysql_rc(rc, mysql);
777
778 rc= mysql_query(mysql, "SELECT ts FROM test_dateformat");
779 check_mysql_rc(rc, mysql);
780
781 result= mysql_store_result(mysql);
782 FAIL_IF(!result, "Invalid result set");
783
784 field= mysql_fetch_field(result);
785 FAIL_IF(!field, "Invalid field");
786 FAIL_UNLESS(strcmp(field->table, "test_dateformat") == 0, "field->table != 'test_dateformat'");
787
788 field= mysql_fetch_field(result);
789 FAIL_IF(field, "no more fields expected");
790
791 mysql_free_result(result);
792
793 /* DATE_FORMAT */
794 rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'venu' FROM test_dateformat");
795 check_mysql_rc(rc, mysql);
796
797 result= mysql_store_result(mysql);
798 FAIL_IF(!result, "Invalid result set");
799
800 field= mysql_fetch_field(result);
801 FAIL_IF(!field, "Invalid field");
802 FAIL_UNLESS(field->table[0] == '\0', "field->table != ''");
803
804 field= mysql_fetch_field(result);
805 FAIL_IF(field, "no more fields expected");
806
807 mysql_free_result(result);
808
809 /* FIELD ALIAS TEST */
810 rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'YEAR' FROM test_dateformat");
811 check_mysql_rc(rc, mysql);
812
813 result= mysql_store_result(mysql);
814 FAIL_IF(!result, "Invalid result set");
815
816 field= mysql_fetch_field(result);
817 FAIL_IF(!field, "Invalid field");
818 FAIL_UNLESS(strcmp(field->name, "YEAR") == 0, "name != 'YEAR'");
819 FAIL_UNLESS(field->org_name[0] == '\0', "org_name != ''");
820
821 field= mysql_fetch_field(result);
822 FAIL_IF(field, "no more fields expected");
823
824 mysql_free_result(result);
825 rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_dateformat");
826 check_mysql_rc(rc, mysql);
827 return OK;
828}
829
830/* Test mysql_list_fields() */
831
832static int test_list_fields(MYSQL *mysql)
833{
834 MYSQL_RES *result;
835 int rc;
836
837 rc= mysql_query(mysql, "drop table if exists t1");
838 check_mysql_rc(rc, mysql);
839
840 rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10) default 'mysql')");
841 check_mysql_rc(rc, mysql);
842
843 result= mysql_list_fields(mysql, "t1", NULL);
844 FAIL_IF(!result, "Invalid result set");
845
846 rc= 0;
847 while (mysql_fetch_row(result))
848 rc++;
849 FAIL_UNLESS(rc == 0, "rowcount != 0");
850
851 if (verify_prepare_field(result, 0, "c1", "c1", MYSQL_TYPE_LONG,
852 "t1", "t1",
853 schema, 11, "0"))
854 goto error;
855
856 if (verify_prepare_field(result, 1, "c2", "c2", MYSQL_TYPE_STRING,
857 "t1", "t1",
858 schema, 10, "mysql"))
859 goto error;
860
861 mysql_free_result(result);
862 check_mysql_rc(mysql_query(mysql, "drop table t1"), mysql);
863 return OK;
864
865error:
866 mysql_free_result(result);
867 check_mysql_rc(mysql_query(mysql, "drop table t1"), mysql);
868 return FAIL;
869}
870
871/* Test correct max length for MEDIUMTEXT and LONGTEXT columns */
872
873static int test_bug9735(MYSQL *mysql)
874{
875 MYSQL_RES *res;
876 int rc;
877
878
879 rc= mysql_query(mysql, "drop table if exists t1");
880 check_mysql_rc(rc, mysql);
881 rc= mysql_query(mysql, "create table t1 (a mediumtext, b longtext) "
882 "character set latin1");
883 check_mysql_rc(rc, mysql);
884 rc= mysql_query(mysql, "select * from t1");
885 check_mysql_rc(rc, mysql);
886 res= mysql_store_result(mysql);
887 if (verify_prepare_field(res, 0, "a", "a", MYSQL_TYPE_BLOB,
888 "t1", "t1", schema, (1U << 24)-1, 0))
889 goto error;
890 if (verify_prepare_field(res, 1, "b", "b", MYSQL_TYPE_BLOB,
891 "t1", "t1", schema, ~0U, 0))
892 goto error;
893 mysql_free_result(res);
894 rc= mysql_query(mysql, "drop table t1");
895 check_mysql_rc(rc, mysql);
896 return OK;
897error:
898 mysql_free_result(res);
899 rc= mysql_query(mysql, "drop table t1");
900 return FAIL;
901}
902
903/*
904 Check that mysql_next_result works properly in case when one of
905 the statements used in a multi-statement query is erroneous
906*/
907
908static int test_bug9992(MYSQL *mysql)
909{
910 MYSQL_RES* res ;
911 int rc;
912
913 /* Sic: SHOW DATABASE is incorrect syntax. */
914 rc= mysql_query(mysql, "SHOW TABLES; SHOW DATABASE; SELECT 1;");
915 check_mysql_rc(rc, mysql);
916
917 res= mysql_store_result(mysql);
918 FAIL_UNLESS(res, "Invalid resultset");
919 mysql_free_result(res);
920 rc= mysql_next_result(mysql);
921 FAIL_UNLESS(rc == 1, "Error expected"); /* Got errors, as expected */
922
923 return OK;
924}
925
926/* Test the support of multi-statement executions */
927
928static int test_multi_statements(MYSQL *mysql)
929{
930 MYSQL *mysql_local;
931 MYSQL_RES *result;
932 int rc;
933
934 const char *query= "\
935DROP TABLE IF EXISTS test_multi_tab;\
936CREATE TABLE test_multi_tab(id int, name char(20));\
937INSERT INTO test_multi_tab(id) VALUES(10), (20);\
938INSERT INTO test_multi_tab VALUES(20, 'insert;comma');\
939SELECT * FROM test_multi_tab;\
940UPDATE test_multi_tab SET name='new;name' WHERE id=20;\
941DELETE FROM test_multi_tab WHERE name='new;name';\
942SELECT * FROM test_multi_tab;\
943DELETE FROM test_multi_tab WHERE id=10;\
944SELECT * FROM test_multi_tab;\
945DROP TABLE test_multi_tab;\
946select 1;\
947DROP TABLE IF EXISTS test_multi_tab";
948 uint count, exp_value;
949 uint rows[]= {0, 0, 2, 1, 3, 2, 2, 1, 1, 0, 0, 1, 0};
950 my_bool reconnect= 1;
951
952 /*
953 First test that we get an error for multi statements
954 (Because default connection is not opened with CLIENT_MULTI_STATEMENTS)
955 */
956 mysql_local= mysql;
957 mysql = test_connect(NULL);
958 rc= mysql_query(mysql, query); /* syntax error */
959 FAIL_IF(!rc, "Error expected");
960
961 rc= mysql_next_result(mysql);
962 FAIL_UNLESS(rc == -1, "rc != -1");
963 rc= mysql_more_results(mysql);
964 FAIL_UNLESS(rc == 0, "rc != 0");
965
966 mysql_close(mysql);
967 mysql= mysql_local;
968
969 mysql_options(mysql_local, MYSQL_OPT_RECONNECT, &reconnect);
970
971 rc= mysql_query(mysql_local, query);
972 check_mysql_rc(rc, mysql);
973
974 for (count= 0 ; count < array_elements(rows) ; count++)
975 {
976 if ((result= mysql_store_result(mysql_local)))
977 {
978 mysql_free_result(result);
979 }
980
981 exp_value= (uint) mysql_affected_rows(mysql_local);
982 FAIL_IF(rows[count] != exp_value, "row[count] != exp_value");
983 if (count != array_elements(rows) -1)
984 {
985 rc= mysql_more_results(mysql_local);
986 FAIL_IF(!rc, "More results expected");
987 rc= mysql_next_result(mysql_local);
988 check_mysql_rc(rc, mysql_local);
989 }
990 else
991 {
992 rc= mysql_more_results(mysql_local);
993 FAIL_UNLESS(rc == 0, "rc != 0");
994 rc= mysql_next_result(mysql_local);
995 FAIL_UNLESS(rc == -1, "rc != -1");
996 }
997 }
998
999 /* check that errors abort multi statements */
1000
1001 rc= mysql_query(mysql_local, "select 1+1+a;select 1+1");
1002 FAIL_IF(!rc, "Error expected");
1003 rc= mysql_more_results(mysql_local);
1004 FAIL_UNLESS(rc == 0, "rc != 0");
1005 rc= mysql_next_result(mysql_local);
1006 FAIL_UNLESS(rc == -1, "rc != -1");
1007
1008 rc= mysql_query(mysql_local, "select 1+1;select 1+1+a;select 1");
1009 check_mysql_rc(rc, mysql);
1010 result= mysql_store_result(mysql_local);
1011 FAIL_IF(!result, "Invalid result set");
1012 mysql_free_result(result);
1013 rc= mysql_more_results(mysql_local);
1014 FAIL_UNLESS(rc == 1, "rc != 1");
1015 rc= mysql_next_result(mysql_local);
1016 FAIL_UNLESS(rc > 0, "rc <= 0");
1017
1018 /*
1019 Ensure that we can now do a simple query (this checks that the server is
1020 not trying to send us the results for the last 'select 1'
1021 */
1022 rc= mysql_query(mysql_local, "select 1+1+1");
1023 check_mysql_rc(rc, mysql);
1024 result= mysql_store_result(mysql_local);
1025 FAIL_IF(!result, "Invalid result set");
1026 mysql_free_result(result);
1027
1028 /*
1029 Check if errors in one of the queries handled properly.
1030 */
1031 rc= mysql_query(mysql_local, "select 1; select * from not_existing_table");
1032 check_mysql_rc(rc, mysql);
1033 result= mysql_store_result(mysql_local);
1034 mysql_free_result(result);
1035
1036 rc= mysql_next_result(mysql_local);
1037 FAIL_UNLESS(rc > 0, "rc <= 0");
1038
1039 rc= mysql_next_result(mysql_local);
1040 FAIL_UNLESS(rc < 0, "rc >= 0");
1041
1042 return OK;
1043}
1044
1045static int test_conc160(MYSQL *mysql)
1046{
1047 MYSQL_RES *result;
1048 MYSQL_FIELD *field;
1049 int rc;
1050
1051 rc= mysql_query(mysql, "SELECT cast(1.234 AS DECIMAL)");
1052 check_mysql_rc(rc, mysql);
1053
1054 result= mysql_store_result(mysql);
1055 field= mysql_fetch_field(result);
1056
1057 FAIL_UNLESS(field->flags & NUM_FLAG, "Numceric flag not set");
1058
1059 mysql_free_result(result);
1060 return OK;
1061}
1062
1063
1064
1065struct my_tests_st my_tests[] = {
1066 {"test_conc160", test_conc160, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1067 {"client_store_result", client_store_result, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1068 {"client_use_result", client_use_result, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1069 {"test_free_result", test_free_result, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1070 {"test_free_store_result", test_free_store_result, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1071 {"test_store_result", test_store_result, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1072 {"test_store_result1", test_store_result1, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1073 {"test_store_result2", test_store_result2, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1074 {"test_bug11718", test_bug11718, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1075 {"test_bug19671", test_bug19671, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1076 {"test_bug21726", test_bug21726, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1077 {"test_bug6761", test_bug6761, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1078 {"test_field_flags", test_field_flags, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1079 {"test_field_names", test_field_names, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1080 {"test_func_fields", test_func_fields, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1081 {"test_list_fields", test_list_fields, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1082 {"test_bug9735", test_bug9735, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1083 {"test_bug9992", test_bug9992, TEST_CONNECTION_NEW, CLIENT_MULTI_STATEMENTS, NULL, NULL},
1084 {"test_multi_statements", test_multi_statements, TEST_CONNECTION_NEW, CLIENT_MULTI_STATEMENTS, NULL, NULL},
1085 {NULL, NULL, 0, 0, NULL, NULL}
1086};
1087
1088
1089int main(int argc, char **argv)
1090{
1091 if (argc > 1)
1092 get_options(argc, argv);
1093
1094 get_envvars();
1095
1096 run_tests(my_tests);
1097
1098 return(exit_status());
1099}
1100