2
Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
4
The 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
6
MySQL Connectors. There are special exceptions to the terms and
7
conditions of the GPLv2 as it is applied to this software, see the
8
FLOSS License Exception
9
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
11
This program is free software; you can redistribute it and/or modify
12
it under the terms of the GNU General Public License as published
13
by the Free Software Foundation; version 2 of the License.
15
This program is distributed in the hope that it will be useful, but
16
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20
You should have received a copy of the GNU General Public License along
21
with this program; if not, write to the Free Software Foundation, Inc.,
22
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
Some basic tests of the client API.
30
static int client_store_result(MYSQL *mysql)
35
rc= mysql_query(mysql, "SELECT 'foo' FROM DUAL UNION SELECT 'bar' FROM DUAL");
36
check_mysql_rc(rc, mysql);
39
result= mysql_store_result(mysql);
40
FAIL_IF(!result, "Invalid result set");
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");
46
while (mysql_fetch_row(result))
49
FAIL_IF(rowcount != 2, "rowcount != 2");
51
mysql_free_result(result);
56
static int client_use_result(MYSQL *mysql)
61
rc= mysql_query(mysql, "SELECT 'foo' FROM DUAL UNION SELECT 'bar' FROM DUAL");
62
check_mysql_rc(rc, mysql);
65
result= mysql_use_result(mysql);
66
FAIL_IF(!result, "Invalid result set");
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");
72
while (mysql_fetch_row(result))
75
FAIL_IF(rowcount != 2, "rowcount != 2");
77
mysql_free_result(result);
82
static int test_free_result(MYSQL *mysql)
85
MYSQL_BIND my_bind[1];
89
char query[MAX_TEST_QUERY_LENGTH];
91
rc= mysql_query(mysql, "drop table if exists test_free_result");
92
check_mysql_rc(rc, mysql);
94
rc= mysql_query(mysql, "create table test_free_result("
95
"c1 int primary key auto_increment)");
96
check_mysql_rc(rc, mysql);
98
rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
99
check_mysql_rc(rc, mysql);
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, query, strlen(query));
105
check_stmt_rc(rc, stmt);
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;
112
rc= mysql_stmt_execute(stmt);
113
check_stmt_rc(rc, stmt);
115
rc= mysql_stmt_bind_result(stmt, my_bind);
116
check_stmt_rc(rc, stmt);
118
rc= mysql_stmt_fetch(stmt);
119
check_stmt_rc(rc, stmt);
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;
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");
133
rc= mysql_stmt_fetch(stmt);
134
check_stmt_rc(rc, stmt);
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;
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");
148
rc= mysql_query(mysql, "drop table test_free_result");
149
FAIL_IF(!rc, "Error commands out of sync expected");
151
rc= mysql_stmt_free_result(stmt);
152
check_stmt_rc(rc, stmt);
154
rc= mysql_query(mysql, "drop table test_free_result");
155
check_mysql_rc(rc, mysql); /* should be successful */
157
mysql_stmt_close(stmt);
163
/* Test mysql_stmt_store_result() */
165
static int test_free_store_result(MYSQL *mysql)
168
MYSQL_BIND my_bind[1];
172
char query[MAX_TEST_QUERY_LENGTH];
174
rc= mysql_query(mysql, "drop table if exists test_free_result");
175
check_mysql_rc(rc, mysql);
177
rc= mysql_query(mysql, "create table test_free_result(c1 int primary key auto_increment)");
178
check_mysql_rc(rc, mysql);
180
rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
181
check_mysql_rc(rc, mysql);
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, query, strlen(query));
187
check_stmt_rc(rc, stmt);
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;
196
rc= mysql_stmt_execute(stmt);
197
check_stmt_rc(rc, stmt);
199
rc= mysql_stmt_bind_result(stmt, my_bind);
200
check_stmt_rc(rc, stmt);
202
rc= mysql_stmt_store_result(stmt);
203
check_stmt_rc(rc, stmt);
205
rc= mysql_stmt_fetch(stmt);
206
check_stmt_rc(rc, stmt);
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;
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");
220
rc= mysql_stmt_fetch(stmt);
221
check_stmt_rc(rc, stmt);
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;
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");
235
rc= mysql_stmt_free_result(stmt);
236
check_stmt_rc(rc, stmt);
238
rc= mysql_query(mysql, "drop table test_free_result");
239
check_mysql_rc(rc, mysql);
241
mysql_stmt_close(stmt);
246
static int test_store_result(MYSQL *mysql)
252
MYSQL_BIND my_bind[2];
253
ulong length, length1;
255
char query[MAX_TEST_QUERY_LENGTH];
257
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
258
check_mysql_rc(rc, mysql);
260
rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
261
check_mysql_rc(rc, mysql);
263
rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
264
check_mysql_rc(rc, mysql);
266
rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
267
check_mysql_rc(rc, mysql);
269
rc= mysql_commit(mysql);
270
check_mysql_rc(rc, mysql);
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];
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];
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, query, strlen(query));
291
check_stmt_rc(rc, stmt);
293
rc= mysql_stmt_bind_result(stmt, my_bind);
294
check_stmt_rc(rc, stmt);
296
rc= mysql_stmt_execute(stmt);
297
check_stmt_rc(rc, stmt);
299
rc= mysql_stmt_store_result(stmt);
300
check_stmt_rc(rc, stmt);
302
rc= mysql_stmt_fetch(stmt);
303
check_stmt_rc(rc, stmt);
305
FAIL_UNLESS(nData == 10, "nData != 10");
306
FAIL_UNLESS(strcmp(szData, "venu") == 0, "szData != 'Venu'");
307
FAIL_UNLESS(length1 == 4, "length1 != 4");
309
rc= mysql_stmt_fetch(stmt);
310
check_stmt_rc(rc, stmt);
312
FAIL_UNLESS(nData == 20, "nData != 20");
313
FAIL_UNLESS(strcmp(szData, "mysql") == 0, "szDaza != 'mysql'");
314
FAIL_UNLESS(length1 == 5, "length1 != 5");
317
rc= mysql_stmt_fetch(stmt);
318
check_stmt_rc(rc, stmt);
320
FAIL_UNLESS(is_null[0], "isnull set");
321
FAIL_UNLESS(strcmp(szData, "monty") == 0, "szData != 'monty'");
322
FAIL_UNLESS(length1 == 5, "length1 != 5");
324
rc= mysql_stmt_fetch(stmt);
325
FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
327
rc= mysql_stmt_execute(stmt);
328
check_stmt_rc(rc, stmt);
330
rc= mysql_stmt_store_result(stmt);
331
check_stmt_rc(rc, stmt);
333
rc= mysql_stmt_fetch(stmt);
334
check_stmt_rc(rc, stmt);
336
FAIL_UNLESS(nData == 10, "nData != 10");
337
FAIL_UNLESS(strcmp(szData, "venu") == 0, "szData != 'Venu'");
338
FAIL_UNLESS(length1 == 4, "length1 != 4");
340
rc= mysql_stmt_fetch(stmt);
341
check_stmt_rc(rc, stmt);
343
FAIL_UNLESS(nData == 20, "nData != 20");
344
FAIL_UNLESS(strcmp(szData, "mysql") == 0, "szDaza != 'mysql'");
345
FAIL_UNLESS(length1 == 5, "length1 != 5");
348
rc= mysql_stmt_fetch(stmt);
349
check_stmt_rc(rc, stmt);
351
FAIL_UNLESS(is_null[0], "isnull set");
352
FAIL_UNLESS(strcmp(szData, "monty") == 0, "szData != 'monty'");
353
FAIL_UNLESS(length1 == 5, "length1 != 5");
355
rc= mysql_stmt_fetch(stmt);
356
FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
358
mysql_stmt_close(stmt);
364
/* Test simple bind store result */
366
static int test_store_result1(MYSQL *mysql)
370
char query[MAX_TEST_QUERY_LENGTH];
372
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
373
check_mysql_rc(rc, mysql);
375
rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
376
check_mysql_rc(rc, mysql);
378
rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
379
check_mysql_rc(rc, mysql);
381
rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
382
check_mysql_rc(rc, mysql);
384
rc= mysql_commit(mysql);
385
check_mysql_rc(rc, mysql);
387
strcpy(query, "SELECT * FROM test_store_result");
388
stmt= mysql_stmt_init(mysql);
389
FAIL_IF(!stmt, mysql_error(mysql));
390
rc= mysql_stmt_prepare(stmt, query, strlen(query));
391
check_stmt_rc(rc, stmt);
393
rc= mysql_stmt_execute(stmt);
394
check_stmt_rc(rc, stmt);
396
rc= mysql_stmt_store_result(stmt);
397
check_stmt_rc(rc, stmt);
400
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
402
FAIL_UNLESS(rc == 3, "rowcount != 3");
404
rc= mysql_stmt_execute(stmt);
405
check_stmt_rc(rc, stmt);
407
rc= mysql_stmt_store_result(stmt);
408
check_stmt_rc(rc, stmt);
411
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
413
FAIL_UNLESS(rc == 3, "rowcount != 3");
415
mysql_stmt_close(stmt);
421
/* Another test for bind and store result */
423
static int test_store_result2(MYSQL *mysql)
429
MYSQL_BIND my_bind[1];
430
char query[MAX_TEST_QUERY_LENGTH];
432
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
433
check_mysql_rc(rc, mysql);
435
rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
436
check_mysql_rc(rc, mysql);
438
rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
439
check_mysql_rc(rc, mysql);
441
rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
442
check_mysql_rc(rc, mysql);
444
rc= mysql_commit(mysql);
445
check_mysql_rc(rc, mysql);
447
memset(my_bind, '\0', sizeof(my_bind));
449
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
450
my_bind[0].buffer= (void *) &nData; /* integer data */
451
my_bind[0].length= &length;
452
my_bind[0].is_null= 0;
454
strcpy((char *)query , "SELECT col1 FROM test_store_result where col1= ?");
455
stmt= mysql_stmt_init(mysql);
456
FAIL_IF(!stmt, mysql_error(mysql));
457
rc= mysql_stmt_prepare(stmt, query, strlen(query));
458
check_stmt_rc(rc, stmt);
460
rc= mysql_stmt_bind_param(stmt, my_bind);
461
check_stmt_rc(rc, stmt);
463
rc= mysql_stmt_bind_result(stmt, my_bind);
464
check_stmt_rc(rc, stmt);
466
nData= 10; length= 0;
467
rc= mysql_stmt_execute(stmt);
468
check_stmt_rc(rc, stmt);
471
rc= mysql_stmt_store_result(stmt);
472
check_stmt_rc(rc, stmt);
474
rc= mysql_stmt_fetch(stmt);
475
check_stmt_rc(rc, stmt);
477
FAIL_UNLESS(nData == 10, "nData != 10");
479
rc= mysql_stmt_fetch(stmt);
480
FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
483
rc= mysql_stmt_execute(stmt);
484
check_stmt_rc(rc, stmt);
487
rc= mysql_stmt_store_result(stmt);
488
check_stmt_rc(rc, stmt);
490
rc= mysql_stmt_fetch(stmt);
491
check_stmt_rc(rc, stmt);
493
FAIL_UNLESS(nData == 20, "nData != 20");
495
rc= mysql_stmt_fetch(stmt);
496
FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
497
mysql_stmt_close(stmt);
502
static int test_bug11718(MYSQL *mysql)
506
const char *query= "select str_to_date(concat(f3),'%Y%m%d') from t1,t2 "
507
"where f1=f2 order by f1";
509
rc= mysql_query(mysql, "drop table if exists t1, t2");
510
check_mysql_rc(rc, mysql);
511
rc= mysql_query(mysql, "create table t1 (f1 int)");
512
check_mysql_rc(rc, mysql);
513
rc= mysql_query(mysql, "create table t2 (f2 int, f3 numeric(8))");
514
check_mysql_rc(rc, mysql);
515
rc= mysql_query(mysql, "insert into t1 values (1), (2)");
516
check_mysql_rc(rc, mysql);
517
rc= mysql_query(mysql, "insert into t2 values (1,20050101), (2,20050202)");
518
check_mysql_rc(rc, mysql);
519
rc= mysql_query(mysql, query);
520
check_mysql_rc(rc, mysql);
521
res = mysql_store_result(mysql);
523
FAIL_UNLESS(res->fields[0].type == MYSQL_TYPE_DATE, "type != MYSQL_TYPE_DATE");
524
mysql_free_result(res);
525
rc= mysql_query(mysql, "drop table t1, t2");
526
check_mysql_rc(rc, mysql);
531
static int test_bug19671(MYSQL *mysql)
536
mysql_query(mysql, "set sql_mode=''");
537
rc= mysql_query(mysql, "drop table if exists t1");
538
check_mysql_rc(rc, mysql)
540
rc= mysql_query(mysql, "drop view if exists v1");
541
check_mysql_rc(rc, mysql)
543
rc= mysql_query(mysql, "create table t1(f1 int)");
544
check_mysql_rc(rc, mysql)
546
rc= mysql_query(mysql, "create view v1 as select va.* from t1 va");
547
check_mysql_rc(rc, mysql)
549
result= mysql_list_fields(mysql, "v1", NULL);
550
FAIL_IF(!result, "Invalid result set");
553
while (mysql_fetch_row(result))
555
FAIL_UNLESS(rc == 0, "");
557
if (verify_prepare_field(result, 0, "f1", "f1", MYSQL_TYPE_LONG,
558
"v1", "v1", schema, 11, "0")) {
559
mysql_free_result(result);
560
diag("verify_prepare_field failed");
564
mysql_free_result(result);
565
check_mysql_rc(mysql_query(mysql, "drop view v1"), mysql)
566
check_mysql_rc(mysql_query(mysql, "drop table t1"), mysql)
571
Bug#21726: Incorrect result with multiple invocations of
574
Test that client gets updated value of insert_id on UPDATE that uses
575
LAST_INSERT_ID(expr).
576
select_query added to test for bug
577
#26921 Problem in mysql_insert_id() Embedded C API function
579
static int test_bug21726(MYSQL *mysql)
581
const char *create_table[]=
583
"DROP TABLE IF EXISTS t1",
584
"CREATE TABLE t1 (i INT)",
585
"INSERT INTO t1 VALUES (1)",
587
const char *update_query= "UPDATE t1 SET i= LAST_INSERT_ID(i + 1)";
589
my_ulonglong insert_id;
590
const char *select_query= "SELECT * FROM t1";
593
rc= mysql_query(mysql, create_table[0]);
594
check_mysql_rc(rc, mysql);
595
rc= mysql_query(mysql, create_table[1]);
596
check_mysql_rc(rc, mysql);
597
rc= mysql_query(mysql, create_table[2]);
598
check_mysql_rc(rc, mysql);
600
rc= mysql_query(mysql, update_query);
601
check_mysql_rc(rc, mysql)
602
insert_id= mysql_insert_id(mysql);
603
FAIL_UNLESS(insert_id == 2, "insert_id != 2");
605
rc= mysql_query(mysql, update_query);
606
check_mysql_rc(rc, mysql)
607
insert_id= mysql_insert_id(mysql);
608
FAIL_UNLESS(insert_id == 3, "insert_id != 3");
610
rc= mysql_query(mysql, select_query);
611
check_mysql_rc(rc, mysql)
612
insert_id= mysql_insert_id(mysql);
613
FAIL_UNLESS(insert_id == 3, "insert_id != 3");
614
result= mysql_store_result(mysql);
615
mysql_free_result(result);
620
/* Bug#6761 - mysql_list_fields doesn't work */
622
static int test_bug6761(MYSQL *mysql)
624
const char *stmt_text;
628
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
629
check_mysql_rc(rc, mysql);
631
stmt_text= "CREATE TABLE t1 (a int, b char(255), c decimal)";
632
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
633
check_mysql_rc(rc, mysql);
635
res= mysql_list_fields(mysql, "t1", "%");
636
FAIL_UNLESS(res && mysql_num_fields(res) == 3, "num_fields != 3");
637
mysql_free_result(res);
639
stmt_text= "DROP TABLE t1";
640
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
641
check_mysql_rc(rc, mysql);
645
/* Test field flags (verify .NET provider) */
647
static int test_field_flags(MYSQL *mysql)
653
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_flags");
654
check_mysql_rc(rc, mysql);
656
rc= mysql_query(mysql, "CREATE TABLE test_field_flags(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, \
663
check_mysql_rc(rc, mysql);
665
/* with table name included with TRUE column name */
666
rc= mysql_query(mysql, "SELECT * FROM test_field_flags");
667
check_mysql_rc(rc, mysql);
669
result= mysql_use_result(mysql);
670
FAIL_IF(!result, "Invalid result set");
672
mysql_field_seek(result, 0);
674
field= mysql_fetch_field(result);
675
FAIL_UNLESS(field->flags & NOT_NULL_FLAG &&
676
field->flags & PRI_KEY_FLAG &&
677
field->flags & AUTO_INCREMENT_FLAG, "Wrong flags for field 0");
679
field= mysql_fetch_field(result);
680
FAIL_UNLESS(field->flags & NOT_NULL_FLAG, "Wrong flags for field 1");
682
field= mysql_fetch_field(result);
683
FAIL_UNLESS(field->flags & UNIQUE_KEY_FLAG, "Wrong flags for field 2");
685
field= mysql_fetch_field(result);
686
FAIL_UNLESS(field->flags & MULTIPLE_KEY_FLAG, "Wrong flags for field 3");
688
field= mysql_fetch_field(result);
689
FAIL_UNLESS(field->flags & NOT_NULL_FLAG, "Wrong flags for field 4");
691
mysql_free_result(result);
695
/* Test real and alias names */
697
static int test_field_names(MYSQL *mysql)
703
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names1");
704
check_mysql_rc(rc, mysql);
706
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names2");
707
check_mysql_rc(rc, mysql);
709
rc= mysql_query(mysql, "CREATE TABLE test_field_names1(id int, name varchar(50))");
710
check_mysql_rc(rc, mysql);
712
rc= mysql_query(mysql, "CREATE TABLE test_field_names2(id int, name varchar(50))");
713
check_mysql_rc(rc, mysql);
715
/* with table name included with TRUE column name */
716
rc= mysql_query(mysql, "SELECT id as 'id-alias' FROM test_field_names1");
717
check_mysql_rc(rc, mysql);
719
result= mysql_use_result(mysql);
720
FAIL_IF(!result, "Invalid result set");
723
while (mysql_fetch_row(result))
725
FAIL_UNLESS(rc == 0, "rowcount != 0");
726
mysql_free_result(result);
728
/* with table name included with TRUE column name */
729
rc= mysql_query(mysql, "SELECT t1.id as 'id-alias', test_field_names2.name FROM test_field_names1 t1, test_field_names2");
730
check_mysql_rc(rc, mysql);
732
result= mysql_use_result(mysql);
733
FAIL_IF(!result, "Invalid result set");
736
while (mysql_fetch_row(result))
738
FAIL_UNLESS(rc == 0, "rowcount != 0");
739
mysql_free_result(result);
743
/* Test FUNCTION field info / DATE_FORMAT() table_name . */
745
static int test_func_fields(MYSQL *mysql)
752
rc= mysql_autocommit(mysql, TRUE);
753
check_mysql_rc(rc, mysql);
755
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_dateformat");
756
check_mysql_rc(rc, mysql);
758
rc= mysql_query(mysql, "CREATE TABLE test_dateformat(id int, \
760
check_mysql_rc(rc, mysql);
762
rc= mysql_query(mysql, "INSERT INTO test_dateformat(id) values(10)");
763
check_mysql_rc(rc, mysql);
765
rc= mysql_query(mysql, "SELECT ts FROM test_dateformat");
766
check_mysql_rc(rc, mysql);
768
result= mysql_store_result(mysql);
769
FAIL_IF(!result, "Invalid result set");
771
field= mysql_fetch_field(result);
772
FAIL_IF(!field, "Invalid field");
773
FAIL_UNLESS(strcmp(field->table, "test_dateformat") == 0, "field->table != 'test_dateformat'");
775
field= mysql_fetch_field(result);
776
FAIL_IF(field, "no more fields expected");
778
mysql_free_result(result);
781
rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'venu' FROM test_dateformat");
782
check_mysql_rc(rc, mysql);
784
result= mysql_store_result(mysql);
785
FAIL_IF(!result, "Invalid result set");
787
field= mysql_fetch_field(result);
788
FAIL_IF(!field, "Invalid field");
789
FAIL_UNLESS(field->table[0] == '\0', "field->table != ''");
791
field= mysql_fetch_field(result);
792
FAIL_IF(field, "no more fields expected");
794
mysql_free_result(result);
796
/* FIELD ALIAS TEST */
797
rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'YEAR' FROM test_dateformat");
798
check_mysql_rc(rc, mysql);
800
result= mysql_store_result(mysql);
801
FAIL_IF(!result, "Invalid result set");
803
field= mysql_fetch_field(result);
804
FAIL_IF(!field, "Invalid field");
805
FAIL_UNLESS(strcmp(field->name, "YEAR") == 0, "name != 'YEAR'");
806
FAIL_UNLESS(field->org_name[0] == '\0', "org_name != ''");
808
field= mysql_fetch_field(result);
809
FAIL_IF(field, "no more fields expected");
811
mysql_free_result(result);
815
/* Test mysql_list_fields() */
817
static int test_list_fields(MYSQL *mysql)
822
rc= mysql_query(mysql, "drop table if exists t1");
823
check_mysql_rc(rc, mysql);
825
rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10) default 'mysql')");
826
check_mysql_rc(rc, mysql);
828
result= mysql_list_fields(mysql, "t1", NULL);
829
FAIL_IF(!result, "Invalid result set");
832
while (mysql_fetch_row(result))
834
FAIL_UNLESS(rc == 0, "rowcount != 0");
836
if (verify_prepare_field(result, 0, "c1", "c1", MYSQL_TYPE_LONG,
841
if (verify_prepare_field(result, 1, "c2", "c2", MYSQL_TYPE_STRING,
843
schema, 10, "mysql"))
846
mysql_free_result(result);
847
check_mysql_rc(mysql_query(mysql, "drop table t1"), mysql);
851
mysql_free_result(result);
852
check_mysql_rc(mysql_query(mysql, "drop table t1"), mysql);
856
/* Test correct max length for MEDIUMTEXT and LONGTEXT columns */
858
static int test_bug9735(MYSQL *mysql)
864
rc= mysql_query(mysql, "drop table if exists t1");
865
check_mysql_rc(rc, mysql);
866
rc= mysql_query(mysql, "create table t1 (a mediumtext, b longtext) "
867
"character set latin1");
868
check_mysql_rc(rc, mysql);
869
rc= mysql_query(mysql, "select * from t1");
870
check_mysql_rc(rc, mysql);
871
res= mysql_store_result(mysql);
872
if (verify_prepare_field(res, 0, "a", "a", MYSQL_TYPE_BLOB,
873
"t1", "t1", schema, (1U << 24)-1, 0))
875
if (verify_prepare_field(res, 1, "b", "b", MYSQL_TYPE_BLOB,
876
"t1", "t1", schema, ~0U, 0))
878
mysql_free_result(res);
879
rc= mysql_query(mysql, "drop table t1");
880
check_mysql_rc(rc, mysql);
883
mysql_free_result(res);
884
rc= mysql_query(mysql, "drop table t1");
889
Check that mysql_next_result works properly in case when one of
890
the statements used in a multi-statement query is erroneous
893
static int test_bug9992(MYSQL *mysql)
898
/* Sic: SHOW DATABASE is incorrect syntax. */
899
rc= mysql_query(mysql, "SHOW TABLES; SHOW DATABASE; SELECT 1;");
900
check_mysql_rc(rc, mysql);
902
res= mysql_store_result(mysql);
903
FAIL_UNLESS(res, "Invalid resultset");
904
mysql_free_result(res);
905
rc= mysql_next_result(mysql);
906
FAIL_UNLESS(rc == 1, "Error expected"); /* Got errors, as expected */
911
/* Test the support of multi-statement executions */
913
static int test_multi_statements(MYSQL *mysql)
919
const char *query= "\
920
DROP TABLE IF EXISTS test_multi_tab;\
921
CREATE TABLE test_multi_tab(id int, name char(20));\
922
INSERT INTO test_multi_tab(id) VALUES(10), (20);\
923
INSERT INTO test_multi_tab VALUES(20, 'insert;comma');\
924
SELECT * FROM test_multi_tab;\
925
UPDATE test_multi_tab SET name='new;name' WHERE id=20;\
926
DELETE FROM test_multi_tab WHERE name='new;name';\
927
SELECT * FROM test_multi_tab;\
928
DELETE FROM test_multi_tab WHERE id=10;\
929
SELECT * FROM test_multi_tab;\
930
DROP TABLE test_multi_tab;\
932
DROP TABLE IF EXISTS test_multi_tab";
933
uint count, exp_value;
934
uint rows[]= {0, 0, 2, 1, 3, 2, 2, 1, 1, 0, 0, 1, 0};
937
First test that we get an error for multi statements
938
(Because default connection is not opened with CLIENT_MULTI_STATEMENTS)
941
mysql = test_connect(NULL);
942
rc= mysql_query(mysql, query); /* syntax error */
943
FAIL_IF(!rc, "Error expected");
945
rc= mysql_next_result(mysql);
946
FAIL_UNLESS(rc == -1, "rc != -1");
947
rc= mysql_more_results(mysql);
948
FAIL_UNLESS(rc == 0, "rc != 0");
953
mysql_local->reconnect= 1;
955
rc= mysql_query(mysql_local, query);
956
check_mysql_rc(rc, mysql);
958
for (count= 0 ; count < array_elements(rows) ; count++)
960
if ((result= mysql_store_result(mysql_local)))
962
mysql_free_result(result);
965
exp_value= (uint) mysql_affected_rows(mysql_local);
966
FAIL_IF(rows[count] != exp_value, "row[count] != exp_value");
967
if (count != array_elements(rows) -1)
969
rc= mysql_more_results(mysql_local);
970
FAIL_IF(!rc, "More results expected");
971
rc= mysql_next_result(mysql_local);
972
check_mysql_rc(rc, mysql_local);
976
rc= mysql_more_results(mysql_local);
977
FAIL_UNLESS(rc == 0, "rc != 0");
978
rc= mysql_next_result(mysql_local);
979
FAIL_UNLESS(rc == -1, "rc != -1");
983
/* check that errors abort multi statements */
985
rc= mysql_query(mysql_local, "select 1+1+a;select 1+1");
986
FAIL_IF(!rc, "Error expected");
987
rc= mysql_more_results(mysql_local);
988
FAIL_UNLESS(rc == 0, "rc != 0");
989
rc= mysql_next_result(mysql_local);
990
FAIL_UNLESS(rc == -1, "rc != -1");
992
rc= mysql_query(mysql_local, "select 1+1;select 1+1+a;select 1");
993
check_mysql_rc(rc, mysql);
994
result= mysql_store_result(mysql_local);
995
FAIL_IF(!result, "Invalid result set");
996
mysql_free_result(result);
997
rc= mysql_more_results(mysql_local);
998
FAIL_UNLESS(rc == 1, "rc != 1");
999
rc= mysql_next_result(mysql_local);
1000
FAIL_UNLESS(rc > 0, "rc <= 0");
1003
Ensure that we can now do a simple query (this checks that the server is
1004
not trying to send us the results for the last 'select 1'
1006
rc= mysql_query(mysql_local, "select 1+1+1");
1007
check_mysql_rc(rc, mysql);
1008
result= mysql_store_result(mysql_local);
1009
FAIL_IF(!result, "Invalid result set");
1010
mysql_free_result(result);
1013
Check if errors in one of the queries handled properly.
1015
rc= mysql_query(mysql_local, "select 1; select * from not_existing_table");
1016
check_mysql_rc(rc, mysql);
1017
result= mysql_store_result(mysql_local);
1018
mysql_free_result(result);
1020
rc= mysql_next_result(mysql_local);
1021
FAIL_UNLESS(rc > 0, "rc <= 0");
1023
rc= mysql_next_result(mysql_local);
1024
FAIL_UNLESS(rc < 0, "rc >= 0");
1031
struct my_tests_st my_tests[] = {
1032
{"client_store_result", client_store_result, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1033
{"client_use_result", client_use_result, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1034
{"test_free_result", test_free_result, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1035
{"test_free_store_result", test_free_store_result, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1036
{"test_store_result", test_store_result, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1037
{"test_store_result1", test_store_result1, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1038
{"test_store_result2", test_store_result2, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1039
{"test_bug11718", test_bug11718, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1040
{"test_bug19671", test_bug19671, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1041
{"test_bug21726", test_bug21726, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1042
{"test_bug6761", test_bug6761, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1043
{"test_field_flags", test_field_flags, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1044
{"test_field_names", test_field_names, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1045
{"test_func_fields", test_func_fields, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1046
{"test_list_fields", test_list_fields, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1047
{"test_bug9735", test_bug9735, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
1048
{"test_bug9992", test_bug9992, TEST_CONNECTION_NEW, CLIENT_MULTI_STATEMENTS, NULL, NULL},
1049
{"test_multi_statements", test_multi_statements, TEST_CONNECTION_NEW, CLIENT_MULTI_STATEMENTS, NULL, NULL},
1050
{NULL, NULL, 0, 0, NULL, NULL}
1054
int main(int argc, char **argv)
1057
// get_options(&argc, &argv);
1061
run_tests(my_tests);
1063
return(exit_status());