1
/* Copyright (C) 2003-2004 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/***************************************************************************
17
This is a test sample to test the new features in MySQL client-server
20
Main author: venu ( venu@mysql.com )
21
***************************************************************************/
24
XXX: PLEASE RUN THIS PROGRAM UNDER VALGRIND AND VERIFY THAT YOUR TEST
25
DOESN'T CONTAIN WARNINGS/ERRORS BEFORE YOU PUSH.
29
#include <my_global.h>
33
#include <my_getopt.h>
35
#include <mysqld_error.h>
38
#define MAX_TEST_QUERY_LENGTH 300 /* MAX QUERY BUFFER LENGTH */
39
#define MAX_KEY MAX_INDEXES
40
#define MAX_SERVER_ARGS 64
42
/* set default options */
43
static int opt_testcase = 0;
44
static char *opt_db= 0;
45
static char *opt_user= 0;
46
static char *opt_password= 0;
47
static char *opt_host= 0;
48
static char *opt_unix_socket= 0;
49
static unsigned int opt_port;
50
static my_bool tty_password= 0, opt_silent= 0;
52
static MYSQL *mysql= 0;
53
static char current_db[]= "client_test_db";
54
static unsigned int test_count= 0;
55
static unsigned int opt_count= 0;
56
static unsigned int iter_count= 0;
57
static my_bool have_innodb= FALSE;
59
static const char *opt_basedir= "./";
60
static const char *opt_vardir= "mysql-test/var";
62
static longlong opt_getopt_ll_test= 0;
64
static int embedded_server_arg_count= 0;
65
static char *embedded_server_args[MAX_SERVER_ARGS];
67
static const char *embedded_server_groups[]= {
70
"mysql_client_test_SERVER",
74
static time_t start_time, end_time;
75
static double total_time;
77
const char *default_dbug_option= "d:t:o,/tmp/mysql_client_test.trace";
85
#define myheader(str) \
86
DBUG_PRINT("test", ("name: %s", str)); \
89
fprintf(stdout, "\n\n#####################################\n"); \
90
fprintf(stdout, "%u of (%u/%u): %s", test_count++, iter_count, \
92
fprintf(stdout, " \n#####################################\n"); \
95
#define myheader_r(str) \
96
DBUG_PRINT("test", ("name: %s", str)); \
99
fprintf(stdout, "\n\n#####################################\n"); \
100
fprintf(stdout, "%s", str); \
101
fprintf(stdout, " \n#####################################\n"); \
104
static void print_error(const char *msg);
105
static void print_st_error(MYSQL_STMT *stmt, const char *msg);
106
static void client_disconnect(void);
110
Abort unless given experssion is non-zero.
116
We can't use any kind of system assert as we need to
117
preserve tested invariants in release builds as well.
120
#define DIE_UNLESS(expr) \
121
((void) ((expr) ? 0 : (die(__FILE__, __LINE__, #expr), 0)))
122
#define DIE_IF(expr) \
123
((void) ((expr) ? (die(__FILE__, __LINE__, #expr), 0) : 0))
125
die(__FILE__, __LINE__, #expr)
127
static void die(const char *file, int line, const char *expr)
130
fprintf(stderr, "%s:%d: check failed: '%s'\n", file, line, expr);
136
#define myerror(msg) print_error(msg)
137
#define mysterror(stmt, msg) print_st_error(stmt, msg)
139
#define myquery(RES) \
144
DIE_UNLESS(r == 0); \
147
#define myquery_r(r) \
151
DIE_UNLESS(r != 0); \
154
#define check_execute(stmt, r) \
157
mysterror(stmt, NULL); \
161
#define check_execute_r(stmt, r) \
164
mysterror(stmt, NULL); \
168
#define check_stmt(stmt) \
172
DIE_UNLESS(stmt != 0); \
175
#define check_stmt_r(stmt) \
179
DIE_UNLESS(stmt == 0);\
182
#define mytest(x) if (!(x)) {myerror(NULL);DIE_UNLESS(FALSE);}
183
#define mytest_r(x) if ((x)) {myerror(NULL);DIE_UNLESS(FALSE);}
186
/* A workaround for Sun Forte 5.6 on Solaris x86 */
188
static int cmp_double(double *a, double *b)
194
/* Print the error message */
196
static void print_error(const char *msg)
200
if (mysql && mysql_errno(mysql))
202
if (mysql->server_version)
203
fprintf(stdout, "\n [MySQL-%s]", mysql->server_version);
205
fprintf(stdout, "\n [MySQL]");
206
fprintf(stdout, "[%d] %s\n", mysql_errno(mysql), mysql_error(mysql));
209
fprintf(stderr, " [MySQL] %s\n", msg);
214
static void print_st_error(MYSQL_STMT *stmt, const char *msg)
218
if (stmt && mysql_stmt_errno(stmt))
220
if (stmt->mysql && stmt->mysql->server_version)
221
fprintf(stdout, "\n [MySQL-%s]", stmt->mysql->server_version);
223
fprintf(stdout, "\n [MySQL]");
225
fprintf(stdout, "[%d] %s\n", mysql_stmt_errno(stmt),
226
mysql_stmt_error(stmt));
229
fprintf(stderr, " [MySQL] %s\n", msg);
233
/* Check if the connection has InnoDB tables */
235
static my_bool check_have_innodb(MYSQL *conn)
242
rc= mysql_query(conn, "show variables like 'have_innodb'");
244
res= mysql_use_result(conn);
247
row= mysql_fetch_row(res);
250
result= strcmp(row[1], "YES") == 0;
251
mysql_free_result(res);
257
This is to be what mysql_query() is for mysql_real_query(), for
258
mysql_simple_prepare(): a variant without the 'length' parameter.
261
static MYSQL_STMT *STDCALL
262
mysql_simple_prepare(MYSQL *mysql_arg, const char *query)
264
MYSQL_STMT *stmt= mysql_stmt_init(mysql_arg);
265
if (stmt && mysql_stmt_prepare(stmt, query, strlen(query)))
267
mysql_stmt_close(stmt);
274
/* Connect to the server */
276
static void client_connect(ulong flag)
279
static char query[MAX_TEST_QUERY_LENGTH];
280
myheader_r("client_connect");
283
fprintf(stdout, "\n Establishing a connection to '%s' ...",
284
opt_host ? opt_host : "");
286
if (!(mysql= mysql_init(NULL)))
289
myerror("mysql_init() failed");
292
/* enable local infile, in non-binary builds often disabled by default */
293
mysql_options(mysql, MYSQL_OPT_LOCAL_INFILE, 0);
295
if (!(mysql_real_connect(mysql, opt_host, opt_user,
296
opt_password, opt_db ? opt_db:"test", opt_port,
297
opt_unix_socket, flag)))
300
myerror("connection failed");
302
fprintf(stdout, "\n Check the connection options using --help or -?\n");
308
fprintf(stdout, "OK");
310
/* set AUTOCOMMIT to ON*/
311
mysql_autocommit(mysql, TRUE);
315
fprintf(stdout, "\nConnected to MySQL server version: %s (%lu)\n",
316
mysql_get_server_info(mysql),
317
(ulong) mysql_get_server_version(mysql));
318
fprintf(stdout, "\n Creating a test database '%s' ...", current_db);
320
strxmov(query, "CREATE DATABASE IF NOT EXISTS ", current_db, NullS);
322
rc= mysql_query(mysql, query);
325
strxmov(query, "USE ", current_db, NullS);
326
rc= mysql_query(mysql, query);
328
have_innodb= check_have_innodb(mysql);
331
fprintf(stdout, "OK");
335
/* Close the connection */
337
static void client_disconnect()
339
static char query[MAX_TEST_QUERY_LENGTH];
341
myheader_r("client_disconnect");
346
fprintf(stdout, "\n dropping the test database '%s' ...", current_db);
347
strxmov(query, "DROP DATABASE IF EXISTS ", current_db, NullS);
349
mysql_query(mysql, query);
351
fprintf(stdout, "OK");
354
fprintf(stdout, "\n closing the connection ...");
357
fprintf(stdout, "OK\n");
362
/* Query processing */
364
static void client_query()
368
myheader("client_query");
370
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
373
rc= mysql_query(mysql, "CREATE TABLE t1("
374
"id int primary key auto_increment, "
375
"name varchar(20))");
378
rc= mysql_query(mysql, "CREATE TABLE t1(id int, name varchar(20))");
381
rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('mysql')");
384
rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('monty')");
387
rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('venu')");
390
rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
393
rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
396
rc= mysql_query(mysql, "UPDATE t1 SET name= 'updated' "
397
"WHERE name= 'deleted'");
400
rc= mysql_query(mysql, "UPDATE t1 SET id= 3 WHERE name= 'updated'");
403
myquery(mysql_query(mysql, "drop table t1"));
409
static void my_print_dashes(MYSQL_RES *result)
414
mysql_field_seek(result, 0);
418
for(i= 0; i< mysql_num_fields(result); i++)
420
field= mysql_fetch_field(result);
421
for(j= 0; j < field->max_length+2; j++)
429
/* Print resultset metadata information */
431
static void my_print_result_metadata(MYSQL_RES *result)
435
unsigned int field_count;
437
mysql_field_seek(result, 0);
444
field_count= mysql_num_fields(result);
445
for(i= 0; i< field_count; i++)
447
field= mysql_fetch_field(result);
448
j= strlen(field->name);
449
if (j < field->max_length)
450
j= field->max_length;
451
if (j < 4 && !IS_NOT_NULL(field->flags))
453
field->max_length= j;
457
my_print_dashes(result);
462
mysql_field_seek(result, 0);
463
for(i= 0; i< field_count; i++)
465
field= mysql_fetch_field(result);
467
fprintf(stdout, " %-*s |", (int) field->max_length, field->name);
472
my_print_dashes(result);
477
/* Process the result set */
479
static int my_process_result_set(MYSQL_RES *result)
484
unsigned int row_count= 0;
489
my_print_result_metadata(result);
491
while ((row= mysql_fetch_row(result)) != NULL)
493
mysql_field_seek(result, 0);
500
for(i= 0; i< mysql_num_fields(result); i++)
502
field= mysql_fetch_field(result);
506
fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
507
else if (IS_NUM(field->type))
508
fprintf(stdout, " %*s |", (int) field->max_length, row[i]);
510
fprintf(stdout, " %-*s |", (int) field->max_length, row[i]);
523
my_print_dashes(result);
525
if (mysql_errno(mysql) != 0)
526
fprintf(stderr, "\n\tmysql_fetch_row() failed\n");
528
fprintf(stdout, "\n\t%d %s returned\n", row_count,
529
row_count == 1 ? "row" : "rows");
535
static int my_process_result(MYSQL *mysql_arg)
540
if (!(result= mysql_store_result(mysql_arg)))
543
row_count= my_process_result_set(result);
545
mysql_free_result(result);
550
/* Process the statement result set */
552
#define MAX_RES_FIELDS 50
553
#define MAX_FIELD_DATA_SIZE 255
555
static int my_process_stmt_result(MYSQL_STMT *stmt)
559
MYSQL_BIND buffer[MAX_RES_FIELDS];
562
char data[MAX_RES_FIELDS][MAX_FIELD_DATA_SIZE];
563
ulong length[MAX_RES_FIELDS];
564
my_bool is_null[MAX_RES_FIELDS];
567
if (!(result= mysql_stmt_result_metadata(stmt))) /* No meta info */
569
while (!mysql_stmt_fetch(stmt))
574
field_count= min(mysql_num_fields(result), MAX_RES_FIELDS);
576
bzero((char*) buffer, sizeof(buffer));
577
bzero((char*) length, sizeof(length));
578
bzero((char*) is_null, sizeof(is_null));
580
for(i= 0; i < field_count; i++)
582
buffer[i].buffer_type= MYSQL_TYPE_STRING;
583
buffer[i].buffer_length= MAX_FIELD_DATA_SIZE;
584
buffer[i].length= &length[i];
585
buffer[i].buffer= (void *) data[i];
586
buffer[i].is_null= &is_null[i];
589
rc= mysql_stmt_bind_result(stmt, buffer);
590
check_execute(stmt, rc);
593
mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*)&rc);
594
rc= mysql_stmt_store_result(stmt);
595
check_execute(stmt, rc);
596
my_print_result_metadata(result);
598
mysql_field_seek(result, 0);
599
while ((rc= mysql_stmt_fetch(stmt)) == 0)
606
mysql_field_seek(result, 0);
607
for (i= 0; i < field_count; i++)
609
field= mysql_fetch_field(result);
613
fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
614
else if (length[i] == 0)
616
data[i][0]= '\0'; /* unmodified buffer */
617
fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
619
else if (IS_NUM(field->type))
620
fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
622
fprintf(stdout, " %-*s |", (int) field->max_length, data[i]);
632
DIE_UNLESS(rc == MYSQL_NO_DATA);
636
my_print_dashes(result);
637
fprintf(stdout, "\n\t%d %s returned\n", row_count,
638
row_count == 1 ? "row" : "rows");
640
mysql_free_result(result);
645
/* Prepare statement, execute, and process result set for given query */
647
int my_stmt_result(const char *buff)
654
fprintf(stdout, "\n\n %s", buff);
655
stmt= mysql_simple_prepare(mysql, buff);
658
rc= mysql_stmt_execute(stmt);
659
check_execute(stmt, rc);
661
row_count= my_process_stmt_result(stmt);
662
mysql_stmt_close(stmt);
668
/* Utility function to verify a particular column data */
670
static void verify_col_data(const char *table, const char *col,
671
const char *exp_data)
673
static char query[MAX_TEST_QUERY_LENGTH];
680
strxmov(query, "SELECT ", col, " FROM ", table, " LIMIT 1", NullS);
682
fprintf(stdout, "\n %s", query);
683
rc= mysql_query(mysql, query);
689
result= mysql_use_result(mysql);
692
if (!(row= mysql_fetch_row(result)) || !row[field])
694
fprintf(stdout, "\n *** ERROR: FAILED TO GET THE RESULT ***");
697
if (strcmp(row[field], exp_data))
699
fprintf(stdout, "\n obtained: `%s` (expected: `%s`)",
700
row[field], exp_data);
703
mysql_free_result(result);
707
/* Utility function to verify the field members */
709
#define verify_prepare_field(result,no,name,org_name,type,table,\
710
org_table,db,length,def) \
711
do_verify_prepare_field((result),(no),(name),(org_name),(type), \
712
(table),(org_table),(db),(length),(def), \
715
static void do_verify_prepare_field(MYSQL_RES *result,
716
unsigned int no, const char *name,
717
const char *org_name,
718
enum enum_field_types type,
720
const char *org_table, const char *db,
721
unsigned long length, const char *def,
722
const char *file, int line)
727
if (!(field= mysql_fetch_field_direct(result, no)))
729
fprintf(stdout, "\n *** ERROR: FAILED TO GET THE RESULT ***");
732
cs= get_charset(field->charsetnr, 0);
736
fprintf(stdout, "\n field[%d]:", no);
737
fprintf(stdout, "\n name :`%s`\t(expected: `%s`)", field->name, name);
738
fprintf(stdout, "\n org_name :`%s`\t(expected: `%s`)",
739
field->org_name, org_name);
740
fprintf(stdout, "\n type :`%d`\t(expected: `%d`)", field->type, type);
742
fprintf(stdout, "\n table :`%s`\t(expected: `%s`)",
743
field->table, table);
745
fprintf(stdout, "\n org_table:`%s`\t(expected: `%s`)",
746
field->org_table, org_table);
747
fprintf(stdout, "\n database :`%s`\t(expected: `%s`)", field->db, db);
748
fprintf(stdout, "\n length :`%lu`\t(expected: `%lu`)",
749
field->length, length * cs->mbmaxlen);
750
fprintf(stdout, "\n maxlength:`%ld`", field->max_length);
751
fprintf(stdout, "\n charsetnr:`%d`", field->charsetnr);
752
fprintf(stdout, "\n default :`%s`\t(expected: `%s`)",
753
field->def ? field->def : "(null)", def ? def: "(null)");
754
fprintf(stdout, "\n");
756
DIE_UNLESS(strcmp(field->name, name) == 0);
757
DIE_UNLESS(strcmp(field->org_name, org_name) == 0);
759
XXX: silent column specification change works based on number of
760
bytes a column occupies. So CHAR -> VARCHAR upgrade is possible even
761
for CHAR(2) column if its character set is multibyte.
762
VARCHAR -> CHAR downgrade won't work for VARCHAR(3) as one would
765
if (cs->mbmaxlen == 1)
767
if (field->type != type)
770
"Expected field type: %d, got type: %d in file %s, line %d\n",
771
(int) type, (int) field->type, file, line);
772
DIE_UNLESS(field->type == type);
776
DIE_UNLESS(strcmp(field->table, table) == 0);
778
DIE_UNLESS(strcmp(field->org_table, org_table) == 0);
779
DIE_UNLESS(strcmp(field->db, db) == 0);
781
Character set should be taken into account for multibyte encodings, such
782
as utf8. Field length is calculated as number of characters * maximum
783
number of bytes a character can occupy.
785
if (length && field->length != length * cs->mbmaxlen)
787
fprintf(stderr, "Expected field length: %d, got length: %d\n",
788
(int) (length * cs->mbmaxlen), (int) field->length);
789
DIE_UNLESS(field->length == length * cs->mbmaxlen);
792
DIE_UNLESS(strcmp(field->def, def) == 0);
796
/* Utility function to verify the parameter count */
798
static void verify_param_count(MYSQL_STMT *stmt, long exp_count)
800
long param_count= mysql_stmt_param_count(stmt);
802
fprintf(stdout, "\n total parameters in stmt: `%ld` (expected: `%ld`)",
803
param_count, exp_count);
804
DIE_UNLESS(param_count == exp_count);
808
/* Utility function to verify the total affected rows */
810
static void verify_st_affected_rows(MYSQL_STMT *stmt, ulonglong exp_count)
812
ulonglong affected_rows= mysql_stmt_affected_rows(stmt);
814
fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
815
(long) affected_rows, (long) exp_count);
816
DIE_UNLESS(affected_rows == exp_count);
820
/* Utility function to verify the total affected rows */
822
static void verify_affected_rows(ulonglong exp_count)
824
ulonglong affected_rows= mysql_affected_rows(mysql);
826
fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
827
(long) affected_rows, (long) exp_count);
828
DIE_UNLESS(affected_rows == exp_count);
832
/* Utility function to verify the total fields count */
834
static void verify_field_count(MYSQL_RES *result, uint exp_count)
836
uint field_count= mysql_num_fields(result);
838
fprintf(stdout, "\n total fields in the result set: `%d` (expected: `%d`)",
839
field_count, exp_count);
840
DIE_UNLESS(field_count == exp_count);
844
/* Utility function to execute a query using prepare-execute */
846
#ifndef EMBEDDED_LIBRARY
847
static void execute_prepare_query(const char *query, ulonglong exp_count)
850
ulonglong affected_rows;
853
stmt= mysql_simple_prepare(mysql, query);
856
rc= mysql_stmt_execute(stmt);
859
affected_rows= mysql_stmt_affected_rows(stmt);
861
fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
862
(long) affected_rows, (long) exp_count);
864
DIE_UNLESS(affected_rows == exp_count);
865
mysql_stmt_close(stmt);
869
/* Store result processing */
871
static void client_store_result()
876
myheader("client_store_result");
878
rc= mysql_query(mysql, "SELECT * FROM t1");
882
result= mysql_store_result(mysql);
885
(void) my_process_result_set(result);
886
mysql_free_result(result);
890
/* Fetch the results */
892
static void client_use_result()
896
myheader("client_use_result");
898
rc= mysql_query(mysql, "SELECT * FROM t1");
902
result= mysql_use_result(mysql);
905
(void) my_process_result_set(result);
906
mysql_free_result(result);
911
Accepts arbitrary number of queries and runs them against the database.
912
Used to fill tables for each test.
915
void fill_tables(const char **query_list, unsigned query_count)
919
DBUG_ENTER("fill_tables");
920
for (query= query_list; query < query_list + query_count;
923
rc= mysql_query(mysql, *query);
930
All state of fetch from one statement: statement handle, out buffers,
932
See fetch_n for for the only use case.
935
enum { MAX_COLUMN_LENGTH= 255 };
937
typedef struct st_stmt_fetch
943
MYSQL_BIND *bind_array;
945
unsigned long *out_data_length;
946
unsigned column_count;
952
Create statement handle, prepare it with statement, execute and allocate
956
void stmt_fetch_init(Stmt_fetch *fetch, unsigned stmt_no_arg,
957
const char *query_arg)
959
unsigned long type= CURSOR_TYPE_READ_ONLY;
963
DBUG_ENTER("stmt_fetch_init");
965
/* Save query and statement number for error messages */
966
fetch->stmt_no= stmt_no_arg;
967
fetch->query= query_arg;
969
fetch->handle= mysql_stmt_init(mysql);
971
rc= mysql_stmt_prepare(fetch->handle, fetch->query, strlen(fetch->query));
972
check_execute(fetch->handle, rc);
975
The attribute is sent to server on execute and asks to open read-only
978
mysql_stmt_attr_set(fetch->handle, STMT_ATTR_CURSOR_TYPE,
979
(const void*) &type);
981
rc= mysql_stmt_execute(fetch->handle);
982
check_execute(fetch->handle, rc);
984
/* Find out total number of columns in result set */
985
metadata= mysql_stmt_result_metadata(fetch->handle);
986
fetch->column_count= mysql_num_fields(metadata);
987
mysql_free_result(metadata);
990
Now allocate bind handles and buffers for output data:
991
calloc memory to reduce number of MYSQL_BIND members we need to
995
fetch->bind_array= (MYSQL_BIND *) calloc(1, sizeof(MYSQL_BIND) *
996
fetch->column_count);
997
fetch->out_data= (char**) calloc(1, sizeof(char*) * fetch->column_count);
998
fetch->out_data_length= (ulong*) calloc(1, sizeof(ulong) *
999
fetch->column_count);
1000
for (i= 0; i < fetch->column_count; ++i)
1002
fetch->out_data[i]= (char*) calloc(1, MAX_COLUMN_LENGTH);
1003
fetch->bind_array[i].buffer_type= MYSQL_TYPE_STRING;
1004
fetch->bind_array[i].buffer= fetch->out_data[i];
1005
fetch->bind_array[i].buffer_length= MAX_COLUMN_LENGTH;
1006
fetch->bind_array[i].length= fetch->out_data_length + i;
1009
mysql_stmt_bind_result(fetch->handle, fetch->bind_array);
1011
fetch->row_count= 0;
1012
fetch->is_open= TRUE;
1014
/* Ready for reading rows */
1019
/* Fetch and print one row from cursor */
1021
int stmt_fetch_fetch_row(Stmt_fetch *fetch)
1025
DBUG_ENTER("stmt_fetch_fetch_row");
1027
if ((rc= mysql_stmt_fetch(fetch->handle)) == 0)
1031
printf("Stmt %d fetched row %d:\n", fetch->stmt_no, fetch->row_count);
1032
for (i= 0; i < fetch->column_count; ++i)
1034
fetch->out_data[i][fetch->out_data_length[i]]= '\0';
1036
printf("column %d: %s\n", i+1, fetch->out_data[i]);
1040
fetch->is_open= FALSE;
1045
void stmt_fetch_close(Stmt_fetch *fetch)
1048
DBUG_ENTER("stmt_fetch_close");
1050
for (i= 0; i < fetch->column_count; ++i)
1051
free(fetch->out_data[i]);
1052
free(fetch->out_data);
1053
free(fetch->out_data_length);
1054
free(fetch->bind_array);
1055
mysql_stmt_close(fetch->handle);
1060
For given array of queries, open query_count cursors and fetch
1061
from them in simultaneous manner.
1062
In case there was an error in one of the cursors, continue
1063
reading from the rest.
1066
enum fetch_type { USE_ROW_BY_ROW_FETCH= 0, USE_STORE_RESULT= 1 };
1068
my_bool fetch_n(const char **query_list, unsigned query_count,
1069
enum fetch_type fetch_type)
1071
unsigned open_statements= query_count;
1072
int rc, error_count= 0;
1073
Stmt_fetch *fetch_array= (Stmt_fetch*) calloc(1, sizeof(Stmt_fetch) *
1076
DBUG_ENTER("fetch_n");
1078
for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1080
/* Init will exit(1) in case of error */
1081
stmt_fetch_init(fetch, fetch - fetch_array,
1082
query_list[fetch - fetch_array]);
1085
if (fetch_type == USE_STORE_RESULT)
1087
for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1089
rc= mysql_stmt_store_result(fetch->handle);
1090
check_execute(fetch->handle, rc);
1094
while (open_statements)
1096
for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1098
if (fetch->is_open && (rc= stmt_fetch_fetch_row(fetch)))
1102
We try to fetch from the rest of the statements in case of
1105
if (rc != MYSQL_NO_DATA)
1108
"Got error reading rows from statement %d,\n"
1110
"error message: %s", (int) (fetch - fetch_array),
1112
mysql_stmt_error(fetch->handle));
1119
fprintf(stderr, "Fetch FAILED");
1122
unsigned total_row_count= 0;
1123
for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1124
total_row_count+= fetch->row_count;
1126
printf("Success, total rows fetched: %d\n", total_row_count);
1128
for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1129
stmt_fetch_close(fetch);
1131
DBUG_RETURN(error_count != 0);
1134
/* Separate thread query to test some cases */
1136
static my_bool thread_query(char *query)
1143
fprintf(stdout, "\n in thread_query(%s)", query);
1144
if (!(l_mysql= mysql_init(NULL)))
1146
myerror("mysql_init() failed");
1149
if (!(mysql_real_connect(l_mysql, opt_host, opt_user,
1150
opt_password, current_db, opt_port,
1151
opt_unix_socket, 0)))
1153
myerror("connection failed");
1157
l_mysql->reconnect= 1;
1158
if (mysql_query(l_mysql, (char *)query))
1160
fprintf(stderr, "Query failed (%s)\n", mysql_error(l_mysql));
1164
mysql_commit(l_mysql);
1166
mysql_close(l_mysql);
1171
/* Query processing */
1173
static void test_debug_example()
1178
myheader("test_debug_example");
1180
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_debug_example");
1183
rc= mysql_query(mysql, "CREATE TABLE test_debug_example("
1184
"id INT PRIMARY KEY AUTO_INCREMENT, "
1185
"name VARCHAR(20), xxx INT)");
1188
rc= mysql_query(mysql, "INSERT INTO test_debug_example (name) "
1189
"VALUES ('mysql')");
1192
rc= mysql_query(mysql, "UPDATE test_debug_example SET name='updated' "
1193
"WHERE name='deleted'");
1196
rc= mysql_query(mysql, "SELECT * FROM test_debug_example where name='mysql'");
1199
result= mysql_use_result(mysql);
1202
(void) my_process_result_set(result);
1203
mysql_free_result(result);
1205
rc= mysql_query(mysql, "DROP TABLE test_debug_example");
1210
/* Test autocommit feature for BDB tables */
1212
static void test_tran_bdb()
1218
myheader("test_tran_bdb");
1220
/* set AUTOCOMMIT to OFF */
1221
rc= mysql_autocommit(mysql, FALSE);
1224
rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction");
1228
/* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
1229
rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction( "
1230
"col1 int , col2 varchar(30)) ENGINE= BDB");
1233
/* insert a row and commit the transaction */
1234
rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')");
1237
rc= mysql_commit(mysql);
1240
/* now insert the second row, and roll back the transaction */
1241
rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')");
1244
rc= mysql_rollback(mysql);
1247
/* delete first row, and roll it back */
1248
rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10");
1251
rc= mysql_rollback(mysql);
1254
/* test the results now, only one row should exist */
1255
rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
1258
/* get the result */
1259
result= mysql_store_result(mysql);
1262
(void) my_process_result_set(result);
1263
mysql_free_result(result);
1265
/* test the results now, only one row should exist */
1266
rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
1269
/* get the result */
1270
result= mysql_use_result(mysql);
1273
row= mysql_fetch_row(result);
1276
row= mysql_fetch_row(result);
1279
mysql_free_result(result);
1280
mysql_autocommit(mysql, TRUE);
1284
/* Test autocommit feature for InnoDB tables */
1286
static void test_tran_innodb()
1292
myheader("test_tran_innodb");
1294
/* set AUTOCOMMIT to OFF */
1295
rc= mysql_autocommit(mysql, FALSE);
1298
rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction");
1301
/* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
1302
rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction(col1 int, "
1303
"col2 varchar(30)) ENGINE= InnoDB");
1306
/* insert a row and commit the transaction */
1307
rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')");
1310
rc= mysql_commit(mysql);
1313
/* now insert the second row, and roll back the transaction */
1314
rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')");
1317
rc= mysql_rollback(mysql);
1320
/* delete first row, and roll it back */
1321
rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10");
1324
rc= mysql_rollback(mysql);
1327
/* test the results now, only one row should exist */
1328
rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
1331
/* get the result */
1332
result= mysql_store_result(mysql);
1335
(void) my_process_result_set(result);
1336
mysql_free_result(result);
1338
/* test the results now, only one row should exist */
1339
rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
1342
/* get the result */
1343
result= mysql_use_result(mysql);
1346
row= mysql_fetch_row(result);
1349
row= mysql_fetch_row(result);
1352
mysql_free_result(result);
1353
mysql_autocommit(mysql, TRUE);
1357
/* Test for BUG#7242 */
1359
static void test_prepare_insert_update()
1364
const char *testcase[]= {
1365
"CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B))",
1366
"INSERT t1 VALUES (1,2,10), (3,4,20)",
1367
"INSERT t1 VALUES (5,6,30), (7,4,40), (8,9,60) ON DUPLICATE KEY UPDATE c=c+100",
1369
"INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0",
1371
"INSERT t1 VALUES (2,1,11), (7,4,40) ON DUPLICATE KEY UPDATE c=c+VALUES(a)",
1373
const char **cur_query;
1375
myheader("test_prepare_insert_update");
1377
for (cur_query= testcase; *cur_query; cur_query++)
1379
char query[MAX_TEST_QUERY_LENGTH];
1380
printf("\nRunning query: %s", *cur_query);
1381
strmov(query, *cur_query);
1382
stmt= mysql_simple_prepare(mysql, query);
1385
verify_param_count(stmt, 0);
1386
rc= mysql_stmt_execute(stmt);
1388
check_execute(stmt, rc);
1389
/* try the last query several times */
1392
for (i=0; i < 3;i++)
1394
printf("\nExecuting last statement again");
1395
rc= mysql_stmt_execute(stmt);
1396
check_execute(stmt, rc);
1397
rc= mysql_stmt_execute(stmt);
1398
check_execute(stmt, rc);
1401
mysql_stmt_close(stmt);
1404
rc= mysql_commit(mysql);
1408
/* Test simple prepares of all DML statements */
1410
static void test_prepare_simple()
1414
char query[MAX_TEST_QUERY_LENGTH];
1416
myheader("test_prepare_simple");
1418
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_simple");
1421
rc= mysql_query(mysql, "CREATE TABLE test_prepare_simple("
1422
"id int, name varchar(50))");
1426
strmov(query, "INSERT INTO test_prepare_simple VALUES(?, ?)");
1427
stmt= mysql_simple_prepare(mysql, query);
1430
verify_param_count(stmt, 2);
1431
mysql_stmt_close(stmt);
1434
strmov(query, "UPDATE test_prepare_simple SET id=? "
1435
"WHERE id=? AND CONVERT(name USING utf8)= ?");
1436
stmt= mysql_simple_prepare(mysql, query);
1439
verify_param_count(stmt, 3);
1440
mysql_stmt_close(stmt);
1443
strmov(query, "DELETE FROM test_prepare_simple WHERE id=10");
1444
stmt= mysql_simple_prepare(mysql, query);
1447
verify_param_count(stmt, 0);
1449
rc= mysql_stmt_execute(stmt);
1450
check_execute(stmt, rc);
1451
mysql_stmt_close(stmt);
1454
strmov(query, "DELETE FROM test_prepare_simple WHERE id=?");
1455
stmt= mysql_simple_prepare(mysql, query);
1458
verify_param_count(stmt, 1);
1460
mysql_stmt_close(stmt);
1463
strmov(query, "SELECT * FROM test_prepare_simple WHERE id=? "
1464
"AND CONVERT(name USING utf8)= ?");
1465
stmt= mysql_simple_prepare(mysql, query);
1468
verify_param_count(stmt, 2);
1470
mysql_stmt_close(stmt);
1472
/* now fetch the results ..*/
1473
rc= mysql_commit(mysql);
1478
/* Test simple prepare field results */
1480
static void test_prepare_field_result()
1485
char query[MAX_TEST_QUERY_LENGTH];
1487
myheader("test_prepare_field_result");
1489
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_field_result");
1492
rc= mysql_query(mysql, "CREATE TABLE test_prepare_field_result(int_c int, "
1493
"var_c varchar(50), ts_c timestamp, "
1494
"char_c char(4), date_c date, extra tinyint)");
1498
strmov(query, "SELECT int_c, var_c, date_c as date, ts_c, char_c FROM "
1499
" test_prepare_field_result as t1 WHERE int_c=?");
1500
stmt= mysql_simple_prepare(mysql, query);
1503
verify_param_count(stmt, 1);
1505
result= mysql_stmt_result_metadata(stmt);
1508
my_print_result_metadata(result);
1511
fprintf(stdout, "\n\n field attributes:\n");
1512
verify_prepare_field(result, 0, "int_c", "int_c", MYSQL_TYPE_LONG,
1513
"t1", "test_prepare_field_result", current_db, 11, 0);
1514
verify_prepare_field(result, 1, "var_c", "var_c", MYSQL_TYPE_VAR_STRING,
1515
"t1", "test_prepare_field_result", current_db, 50, 0);
1516
verify_prepare_field(result, 2, "date", "date_c", MYSQL_TYPE_DATE,
1517
"t1", "test_prepare_field_result", current_db, 10, 0);
1518
verify_prepare_field(result, 3, "ts_c", "ts_c", MYSQL_TYPE_TIMESTAMP,
1519
"t1", "test_prepare_field_result", current_db, 19, 0);
1520
verify_prepare_field(result, 4, "char_c", "char_c",
1521
(mysql_get_server_version(mysql) <= 50000 ?
1522
MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING),
1523
"t1", "test_prepare_field_result", current_db, 4, 0);
1525
verify_field_count(result, 5);
1526
mysql_free_result(result);
1527
mysql_stmt_close(stmt);
1531
/* Test simple prepare field results */
1533
static void test_prepare_syntax()
1537
char query[MAX_TEST_QUERY_LENGTH];
1539
myheader("test_prepare_syntax");
1541
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_syntax");
1544
rc= mysql_query(mysql, "CREATE TABLE test_prepare_syntax("
1545
"id int, name varchar(50), extra int)");
1548
strmov(query, "INSERT INTO test_prepare_syntax VALUES(?");
1549
stmt= mysql_simple_prepare(mysql, query);
1552
strmov(query, "SELECT id, name FROM test_prepare_syntax WHERE id=? AND WHERE");
1553
stmt= mysql_simple_prepare(mysql, query);
1556
/* now fetch the results ..*/
1557
rc= mysql_commit(mysql);
1562
/* Test a simple prepare */
1564
static void test_prepare()
1568
int int_data, o_int_data;
1569
char str_data[50], data[50];
1570
char tiny_data, o_tiny_data;
1571
short small_data, o_small_data;
1572
longlong big_data, o_big_data;
1573
float real_data, o_real_data;
1574
double double_data, o_double_data;
1575
ulong length[7], len;
1578
MYSQL_BIND my_bind[7];
1579
char query[MAX_TEST_QUERY_LENGTH];
1581
myheader("test_prepare");
1583
rc= mysql_autocommit(mysql, TRUE);
1586
rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
1589
rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 tinyint, "
1590
"col2 varchar(15), col3 int, "
1591
"col4 smallint, col5 bigint, "
1592
"col6 float, col7 double )");
1595
/* insert by prepare */
1596
strxmov(query, "INSERT INTO my_prepare VALUES(?, ?, ?, ?, ?, ?, ?)", NullS);
1597
stmt= mysql_simple_prepare(mysql, query);
1600
verify_param_count(stmt, 7);
1602
bzero((char*) my_bind, sizeof(my_bind));
1605
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
1606
my_bind[0].buffer= (void *)&tiny_data;
1608
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
1609
my_bind[1].buffer= (void *)str_data;
1610
my_bind[1].buffer_length= 1000; /* Max string length */
1612
my_bind[2].buffer_type= MYSQL_TYPE_LONG;
1613
my_bind[2].buffer= (void *)&int_data;
1615
my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
1616
my_bind[3].buffer= (void *)&small_data;
1618
my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
1619
my_bind[4].buffer= (void *)&big_data;
1621
my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
1622
my_bind[5].buffer= (void *)&real_data;
1624
my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
1625
my_bind[6].buffer= (void *)&double_data;
1627
for (i= 0; i < (int) array_elements(my_bind); i++)
1629
my_bind[i].length= &length[i];
1630
my_bind[i].is_null= &is_null[i];
1634
rc= mysql_stmt_bind_param(stmt, my_bind);
1635
check_execute(stmt, rc);
1641
double_data= 6578.001;
1643
/* now, execute the prepared statement to insert 10 records.. */
1644
for (tiny_data= 0; tiny_data < 100; tiny_data++)
1646
length[1]= my_sprintf(str_data, (str_data, "MySQL%d", int_data));
1647
rc= mysql_stmt_execute(stmt);
1648
check_execute(stmt, rc);
1653
double_data += 10.09;
1656
mysql_stmt_close(stmt);
1658
/* now fetch the results ..*/
1659
rc= mysql_commit(mysql);
1662
/* test the results now, only one row should exist */
1663
rc= my_stmt_result("SELECT * FROM my_prepare");
1664
DIE_UNLESS(tiny_data == (char) rc);
1666
stmt= mysql_simple_prepare(mysql, "SELECT * FROM my_prepare");
1669
rc= mysql_stmt_bind_result(stmt, my_bind);
1670
check_execute(stmt, rc);
1672
/* get the result */
1673
rc= mysql_stmt_execute(stmt);
1674
check_execute(stmt, rc);
1680
o_double_data= 6578.001;
1682
/* now, execute the prepared statement to insert 10 records.. */
1683
for (o_tiny_data= 0; o_tiny_data < 100; o_tiny_data++)
1685
len= my_sprintf(data, (data, "MySQL%d", o_int_data));
1687
rc= mysql_stmt_fetch(stmt);
1688
check_execute(stmt, rc);
1692
fprintf(stdout, "\n");
1693
fprintf(stdout, "\n\t tiny : %d (%lu)", tiny_data, length[0]);
1694
fprintf(stdout, "\n\t short : %d (%lu)", small_data, length[3]);
1695
fprintf(stdout, "\n\t int : %d (%lu)", int_data, length[2]);
1696
fprintf(stdout, "\n\t big : %s (%lu)", llstr(big_data, llbuf),
1699
fprintf(stdout, "\n\t float : %f (%lu)", real_data, length[5]);
1700
fprintf(stdout, "\n\t double : %f (%lu)", double_data, length[6]);
1702
fprintf(stdout, "\n\t str : %s (%lu)", str_data, length[1]);
1705
DIE_UNLESS(tiny_data == o_tiny_data);
1706
DIE_UNLESS(is_null[0] == 0);
1707
DIE_UNLESS(length[0] == 1);
1709
DIE_UNLESS(int_data == o_int_data);
1710
DIE_UNLESS(length[2] == 4);
1712
DIE_UNLESS(small_data == o_small_data);
1713
DIE_UNLESS(length[3] == 2);
1715
DIE_UNLESS(big_data == o_big_data);
1716
DIE_UNLESS(length[4] == 8);
1718
DIE_UNLESS(real_data == o_real_data);
1719
DIE_UNLESS(length[5] == 4);
1721
DIE_UNLESS(cmp_double(&double_data, &o_double_data));
1722
DIE_UNLESS(length[6] == 8);
1724
DIE_UNLESS(strcmp(data, str_data) == 0);
1725
DIE_UNLESS(length[1] == len);
1731
o_double_data += 10.09;
1734
rc= mysql_stmt_fetch(stmt);
1735
DIE_UNLESS(rc == MYSQL_NO_DATA);
1737
mysql_stmt_close(stmt);
1742
/* Test double comparision */
1744
static void test_double_compare()
1748
char real_data[10], tiny_data;
1751
MYSQL_BIND my_bind[3];
1753
char query[MAX_TEST_QUERY_LENGTH];
1755
myheader("test_double_compare");
1757
rc= mysql_autocommit(mysql, TRUE);
1760
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_double_compare");
1763
rc= mysql_query(mysql, "CREATE TABLE test_double_compare(col1 tinyint, "
1764
" col2 float, col3 double )");
1767
rc= mysql_query(mysql, "INSERT INTO test_double_compare "
1768
"VALUES (1, 10.2, 34.5)");
1771
strmov(query, "UPDATE test_double_compare SET col1=100 "
1772
"WHERE col1 = ? AND col2 = ? AND COL3 = ?");
1773
stmt= mysql_simple_prepare(mysql, query);
1776
verify_param_count(stmt, 3);
1778
/* Always bzero bind array because there can be internal members */
1779
bzero((char*) my_bind, sizeof(my_bind));
1782
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
1783
my_bind[0].buffer= (void *)&tiny_data;
1786
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
1787
my_bind[1].buffer= (void *)&real_data;
1788
my_bind[1].buffer_length= sizeof(real_data);
1789
my_bind[1].length= &length[1];
1793
my_bind[2].buffer_type= MYSQL_TYPE_DOUBLE;
1794
my_bind[2].buffer= (void *)&double_data;
1797
strmov(real_data, "10.2");
1799
rc= mysql_stmt_bind_param(stmt, my_bind);
1800
check_execute(stmt, rc);
1802
rc= mysql_stmt_execute(stmt);
1803
check_execute(stmt, rc);
1805
verify_affected_rows(0);
1807
mysql_stmt_close(stmt);
1809
/* now fetch the results ..*/
1810
rc= mysql_commit(mysql);
1813
/* test the results now, only one row should exist */
1814
rc= mysql_query(mysql, "SELECT * FROM test_double_compare");
1817
/* get the result */
1818
result= mysql_store_result(mysql);
1821
rc= my_process_result_set(result);
1822
DIE_UNLESS((int)tiny_data == rc);
1823
mysql_free_result(result);
1827
/* Test simple null */
1829
static void test_null()
1834
MYSQL_BIND my_bind[2];
1836
char query[MAX_TEST_QUERY_LENGTH];
1838
myheader("test_null");
1840
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_null");
1843
rc= mysql_query(mysql, "CREATE TABLE test_null(col1 int, col2 varchar(50))");
1846
/* insert by prepare, wrong column name */
1847
strmov(query, "INSERT INTO test_null(col3, col2) VALUES(?, ?)");
1848
stmt= mysql_simple_prepare(mysql, query);
1851
strmov(query, "INSERT INTO test_null(col1, col2) VALUES(?, ?)");
1852
stmt= mysql_simple_prepare(mysql, query);
1855
verify_param_count(stmt, 2);
1857
/* Always bzero all members of bind parameter */
1858
bzero((char*) my_bind, sizeof(my_bind));
1860
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1861
my_bind[0].is_null= &is_null[0];
1863
my_bind[1]= my_bind[0];
1865
rc= mysql_stmt_bind_param(stmt, my_bind);
1866
check_execute(stmt, rc);
1868
/* now, execute the prepared statement to insert 10 records.. */
1869
for (nData= 0; nData<10; nData++)
1871
rc= mysql_stmt_execute(stmt);
1872
check_execute(stmt, rc);
1875
/* Re-bind with MYSQL_TYPE_NULL */
1876
my_bind[0].buffer_type= MYSQL_TYPE_NULL;
1877
is_null[0]= 0; /* reset */
1878
my_bind[1]= my_bind[0];
1880
rc= mysql_stmt_bind_param(stmt, my_bind);
1881
check_execute(stmt, rc);
1883
for (nData= 0; nData<10; nData++)
1885
rc= mysql_stmt_execute(stmt);
1886
check_execute(stmt, rc);
1889
mysql_stmt_close(stmt);
1891
/* now fetch the results ..*/
1892
rc= mysql_commit(mysql);
1896
rc= my_stmt_result("SELECT * FROM test_null");;
1897
DIE_UNLESS((int) nData == rc);
1900
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1901
my_bind[0].buffer= (void *)&nData; /* this buffer won't be altered */
1902
my_bind[0].length= 0;
1903
my_bind[1]= my_bind[0];
1904
my_bind[0].is_null= &is_null[0];
1905
my_bind[1].is_null= &is_null[1];
1907
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_null");
1910
rc= mysql_stmt_execute(stmt);
1911
check_execute(stmt, rc);
1913
rc= mysql_stmt_bind_result(stmt, my_bind);
1914
check_execute(stmt, rc);
1917
is_null[0]= is_null[1]= 0;
1918
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
1920
DIE_UNLESS(is_null[0]);
1921
DIE_UNLESS(is_null[1]);
1923
is_null[0]= is_null[1]= 0;
1925
DIE_UNLESS(rc == (int) nData);
1926
mysql_stmt_close(stmt);
1930
/* Test for NULL as PS parameter (BUG#3367, BUG#3371) */
1932
static void test_ps_null_param()
1941
MYSQL_BIND out_bind;
1943
my_bool out_is_null;
1944
char out_str_data[20];
1946
const char *queries[]= {"select ?", "select ?+1",
1947
"select col1 from test_ps_nulls where col1 <=> ?",
1950
const char **cur_query= queries;
1952
myheader("test_null_ps_param_in_result");
1954
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ps_nulls");
1957
rc= mysql_query(mysql, "CREATE TABLE test_ps_nulls(col1 int)");
1960
rc= mysql_query(mysql, "INSERT INTO test_ps_nulls values (1), (null)");
1963
/* Always bzero all members of bind parameter */
1964
bzero((char*) &in_bind, sizeof(in_bind));
1965
bzero((char*) &out_bind, sizeof(out_bind));
1967
in_bind.buffer_type= MYSQL_TYPE_LONG;
1968
in_bind.is_null= &in_is_null;
1970
in_bind.buffer= (void *)&in_long;
1974
out_bind.buffer_type= MYSQL_TYPE_STRING;
1975
out_bind.is_null= &out_is_null;
1976
out_bind.length= &out_length;
1977
out_bind.buffer= out_str_data;
1978
out_bind.buffer_length= array_elements(out_str_data);
1980
/* Execute several queries, all returning NULL in result. */
1981
for(cur_query= queries; *cur_query; cur_query++)
1983
char query[MAX_TEST_QUERY_LENGTH];
1984
strmov(query, *cur_query);
1985
stmt= mysql_simple_prepare(mysql, query);
1987
verify_param_count(stmt, 1);
1989
rc= mysql_stmt_bind_param(stmt, &in_bind);
1990
check_execute(stmt, rc);
1991
rc= mysql_stmt_bind_result(stmt, &out_bind);
1992
check_execute(stmt, rc);
1993
rc= mysql_stmt_execute(stmt);
1994
check_execute(stmt, rc);
1995
rc= mysql_stmt_fetch(stmt);
1996
DIE_UNLESS(rc != MYSQL_NO_DATA);
1997
DIE_UNLESS(out_is_null);
1998
rc= mysql_stmt_fetch(stmt);
1999
DIE_UNLESS(rc == MYSQL_NO_DATA);
2000
mysql_stmt_close(stmt);
2005
/* Test fetch null */
2007
static void test_fetch_null()
2012
MYSQL_BIND my_bind[11];
2014
my_bool is_null[11];
2015
char query[MAX_TEST_QUERY_LENGTH];
2017
myheader("test_fetch_null");
2019
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_fetch_null");
2022
rc= mysql_query(mysql, "CREATE TABLE test_fetch_null("
2023
" col1 tinyint, col2 smallint, "
2024
" col3 int, col4 bigint, "
2025
" col5 float, col6 double, "
2026
" col7 date, col8 time, "
2027
" col9 varbinary(10), "
2028
" col10 varchar(50), "
2029
" col11 char(20))");
2032
rc= mysql_query(mysql, "INSERT INTO test_fetch_null (col11) "
2033
"VALUES (1000), (88), (389789)");
2036
rc= mysql_commit(mysql);
2040
bzero((char*) my_bind, sizeof(my_bind));
2041
for (i= 0; i < (int) array_elements(my_bind); i++)
2043
my_bind[i].buffer_type= MYSQL_TYPE_LONG;
2044
my_bind[i].is_null= &is_null[i];
2045
my_bind[i].length= &length[i];
2047
my_bind[i-1].buffer= (void *)&nData; /* Last column is not null */
2049
strmov((char *)query , "SELECT * FROM test_fetch_null");
2051
rc= my_stmt_result(query);
2052
DIE_UNLESS(rc == 3);
2054
stmt= mysql_simple_prepare(mysql, query);
2057
rc= mysql_stmt_bind_result(stmt, my_bind);
2058
check_execute(stmt, rc);
2060
rc= mysql_stmt_execute(stmt);
2061
check_execute(stmt, rc);
2064
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
2067
for (i= 0; i < 10; i++)
2070
fprintf(stdout, "\n data[%d] : %s", i,
2071
is_null[i] ? "NULL" : "NOT NULL");
2072
DIE_UNLESS(is_null[i]);
2075
fprintf(stdout, "\n data[%d]: %d", i, nData);
2076
DIE_UNLESS(nData == 1000 || nData == 88 || nData == 389789);
2077
DIE_UNLESS(is_null[i] == 0);
2078
DIE_UNLESS(length[i] == 4);
2080
DIE_UNLESS(rc == 3);
2081
mysql_stmt_close(stmt);
2085
/* Test simple select */
2087
static void test_select_version()
2092
myheader("test_select_version");
2094
stmt= mysql_simple_prepare(mysql, "SELECT @@version");
2097
verify_param_count(stmt, 0);
2099
rc= mysql_stmt_execute(stmt);
2100
check_execute(stmt, rc);
2102
my_process_stmt_result(stmt);
2103
mysql_stmt_close(stmt);
2107
/* Test simple show */
2109
static void test_select_show_table()
2114
myheader("test_select_show_table");
2116
stmt= mysql_simple_prepare(mysql, "SHOW TABLES FROM mysql");
2119
verify_param_count(stmt, 0);
2121
for (i= 1; i < 3; i++)
2123
rc= mysql_stmt_execute(stmt);
2124
check_execute(stmt, rc);
2127
my_process_stmt_result(stmt);
2128
mysql_stmt_close(stmt);
2132
/* Test simple select to debug */
2134
static void test_select_direct()
2139
myheader("test_select_direct");
2141
rc= mysql_autocommit(mysql, TRUE);
2144
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
2147
rc= mysql_query(mysql, "CREATE TABLE test_select(id int, id1 tinyint, "
2150
" name varchar(50))");
2153
/* insert a row and commit the transaction */
2154
rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 5, 2.3, 4.5, 'venu')");
2157
rc= mysql_commit(mysql);
2160
rc= mysql_query(mysql, "SELECT * FROM test_select");
2163
/* get the result */
2164
result= mysql_store_result(mysql);
2167
(void) my_process_result_set(result);
2168
mysql_free_result(result);
2172
/* Test simple select with prepare */
2174
static void test_select_prepare()
2179
myheader("test_select_prepare");
2181
rc= mysql_autocommit(mysql, TRUE);
2184
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
2187
rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
2190
/* insert a row and commit the transaction */
2191
rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
2194
rc= mysql_commit(mysql);
2197
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
2200
rc= mysql_stmt_execute(stmt);
2201
check_execute(stmt, rc);
2203
rc= my_process_stmt_result(stmt);
2204
DIE_UNLESS(rc == 1);
2205
mysql_stmt_close(stmt);
2207
rc= mysql_query(mysql, "DROP TABLE test_select");
2210
rc= mysql_query(mysql, "CREATE TABLE test_select(id tinyint, id1 int, "
2211
" id2 float, id3 float, "
2212
" name varchar(50))");
2215
/* insert a row and commit the transaction */
2216
rc= mysql_query(mysql, "INSERT INTO test_select(id, id1, id2, name) VALUES(10, 5, 2.3, 'venu')");
2219
rc= mysql_commit(mysql);
2222
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
2225
rc= mysql_stmt_execute(stmt);
2226
check_execute(stmt, rc);
2228
rc= my_process_stmt_result(stmt);
2229
DIE_UNLESS(rc == 1);
2230
mysql_stmt_close(stmt);
2234
/* Test simple select */
2236
static void test_select()
2242
MYSQL_BIND my_bind[2];
2244
char query[MAX_TEST_QUERY_LENGTH];
2246
myheader("test_select");
2248
rc= mysql_autocommit(mysql, TRUE);
2251
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
2254
rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
2257
/* insert a row and commit the transaction */
2258
rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
2261
/* now insert the second row, and roll back the transaction */
2262
rc= mysql_query(mysql, "INSERT INTO test_select VALUES(20, 'mysql')");
2265
rc= mysql_commit(mysql);
2268
strmov(query, "SELECT * FROM test_select WHERE id= ? "
2269
"AND CONVERT(name USING utf8) =?");
2270
stmt= mysql_simple_prepare(mysql, query);
2273
verify_param_count(stmt, 2);
2275
/* Always bzero all members of bind parameter */
2276
bzero((char*) my_bind, sizeof(my_bind));
2280
strmov(szData, (char *)"venu");
2281
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
2282
my_bind[1].buffer= (void *)szData;
2283
my_bind[1].buffer_length= 4;
2284
my_bind[1].length= &length[1];
2287
my_bind[0].buffer= (void *)&nData;
2288
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2290
rc= mysql_stmt_bind_param(stmt, my_bind);
2291
check_execute(stmt, rc);
2293
rc= mysql_stmt_execute(stmt);
2294
check_execute(stmt, rc);
2296
rc= my_process_stmt_result(stmt);
2297
DIE_UNLESS(rc == 1);
2299
mysql_stmt_close(stmt);
2304
Test for BUG#3420 ("select id1, value1 from t where id= ? or value= ?"
2305
returns all rows in the table)
2308
static void test_ps_conj_select()
2312
MYSQL_BIND my_bind[2];
2315
unsigned long str_length;
2316
char query[MAX_TEST_QUERY_LENGTH];
2317
myheader("test_ps_conj_select");
2319
rc= mysql_query(mysql, "drop table if exists t1");
2322
rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
2323
"value2 varchar(100), value1 varchar(100))");
2326
rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
2327
"(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
2330
strmov(query, "select id1, value1 from t1 where id1= ? or "
2331
"CONVERT(value1 USING utf8)= ?");
2332
stmt= mysql_simple_prepare(mysql, query);
2335
verify_param_count(stmt, 2);
2337
/* Always bzero all members of bind parameter */
2338
bzero((char*) my_bind, sizeof(my_bind));
2340
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2341
my_bind[0].buffer= (void *)&int_data;
2343
my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
2344
my_bind[1].buffer= (void *)str_data;
2345
my_bind[1].buffer_length= array_elements(str_data);
2346
my_bind[1].length= &str_length;
2348
rc= mysql_stmt_bind_param(stmt, my_bind);
2349
check_execute(stmt, rc);
2352
strmov(str_data, "hh");
2353
str_length= strlen(str_data);
2355
rc= mysql_stmt_execute(stmt);
2356
check_execute(stmt, rc);
2358
rc= my_process_stmt_result(stmt);
2359
DIE_UNLESS(rc == 3);
2361
mysql_stmt_close(stmt);
2365
/* reads Qcache_hits from server and returns its value */
2366
static uint query_cache_hits(MYSQL *conn)
2373
rc= mysql_query(conn, "show status like 'qcache_hits'");
2375
res= mysql_use_result(conn);
2378
row= mysql_fetch_row(res);
2381
result= atoi(row[1]);
2382
mysql_free_result(res);
2388
utility for the next test; expects 3 rows in the result from a SELECT,
2389
compares each row/field with an expected value.
2391
#define test_ps_query_cache_result(i1,s1,l1,i2,s2,l2,i3,s3,l3) \
2392
r_metadata= mysql_stmt_result_metadata(stmt); \
2393
DIE_UNLESS(r_metadata != NULL); \
2394
rc= mysql_stmt_fetch(stmt); \
2395
check_execute(stmt, rc); \
2397
fprintf(stdout, "\n row 1: %d, %s(%lu)", r_int_data, \
2398
r_str_data, r_str_length); \
2399
DIE_UNLESS((r_int_data == i1) && (r_str_length == l1) && \
2400
(strcmp(r_str_data, s1) == 0)); \
2401
rc= mysql_stmt_fetch(stmt); \
2402
check_execute(stmt, rc); \
2404
fprintf(stdout, "\n row 2: %d, %s(%lu)", r_int_data, \
2405
r_str_data, r_str_length); \
2406
DIE_UNLESS((r_int_data == i2) && (r_str_length == l2) && \
2407
(strcmp(r_str_data, s2) == 0)); \
2408
rc= mysql_stmt_fetch(stmt); \
2409
check_execute(stmt, rc); \
2411
fprintf(stdout, "\n row 3: %d, %s(%lu)", r_int_data, \
2412
r_str_data, r_str_length); \
2413
DIE_UNLESS((r_int_data == i3) && (r_str_length == l3) && \
2414
(strcmp(r_str_data, s3) == 0)); \
2415
rc= mysql_stmt_fetch(stmt); \
2416
DIE_UNLESS(rc == MYSQL_NO_DATA); \
2417
mysql_free_result(r_metadata);
2421
Test that prepared statements make use of the query cache just as normal
2422
statements (BUG#735).
2424
static void test_ps_query_cache()
2426
MYSQL *lmysql= mysql;
2429
MYSQL_BIND p_bind[2],r_bind[2]; /* p: param bind; r: result bind */
2430
int32 p_int_data, r_int_data;
2431
char p_str_data[32], r_str_data[32];
2432
unsigned long p_str_length, r_str_length;
2433
MYSQL_RES *r_metadata;
2434
char query[MAX_TEST_QUERY_LENGTH];
2436
enum enum_test_ps_query_cache
2439
We iterate the same prepare/executes block, but have iterations where
2440
we vary the query cache conditions.
2442
/* the query cache is enabled for the duration of prep&execs: */
2445
same but using a new connection (to see if qcache serves results from
2446
the previous connection as it should):
2448
TEST_QCACHE_ON_WITH_OTHER_CONN,
2450
First border case: disables the query cache before prepare and
2451
re-enables it before execution (to test if we have no bug then):
2455
Second border case: enables the query cache before prepare and
2456
disables it before execution:
2460
enum enum_test_ps_query_cache iteration;
2462
myheader("test_ps_query_cache");
2464
/* prepare the table */
2466
rc= mysql_query(mysql, "drop table if exists t1");
2469
rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
2470
"value2 varchar(100), value1 varchar(100))");
2473
rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
2474
"(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
2477
for (iteration= TEST_QCACHE_ON; iteration <= TEST_QCACHE_ON_OFF; iteration++)
2480
switch (iteration) {
2481
case TEST_QCACHE_ON:
2482
case TEST_QCACHE_ON_OFF:
2483
rc= mysql_query(lmysql, "set global query_cache_size=1000000");
2486
case TEST_QCACHE_OFF_ON:
2487
rc= mysql_query(lmysql, "set global query_cache_size=0");
2490
case TEST_QCACHE_ON_WITH_OTHER_CONN:
2492
fprintf(stdout, "\n Establishing a test connection ...");
2493
if (!(lmysql= mysql_init(NULL)))
2495
printf("mysql_init() failed");
2498
if (!(mysql_real_connect(lmysql, opt_host, opt_user,
2499
opt_password, current_db, opt_port,
2500
opt_unix_socket, 0)))
2502
printf("connection failed");
2503
mysql_close(lmysql);
2507
fprintf(stdout, "OK");
2510
strmov(query, "select id1, value1 from t1 where id1= ? or "
2511
"CONVERT(value1 USING utf8)= ?");
2512
stmt= mysql_simple_prepare(lmysql, query);
2515
verify_param_count(stmt, 2);
2517
switch (iteration) {
2518
case TEST_QCACHE_OFF_ON:
2519
rc= mysql_query(lmysql, "set global query_cache_size=1000000");
2522
case TEST_QCACHE_ON_OFF:
2523
rc= mysql_query(lmysql, "set global query_cache_size=0");
2529
bzero((char*) p_bind, sizeof(p_bind));
2530
p_bind[0].buffer_type= MYSQL_TYPE_LONG;
2531
p_bind[0].buffer= (void *)&p_int_data;
2532
p_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
2533
p_bind[1].buffer= (void *)p_str_data;
2534
p_bind[1].buffer_length= array_elements(p_str_data);
2535
p_bind[1].length= &p_str_length;
2537
rc= mysql_stmt_bind_param(stmt, p_bind);
2538
check_execute(stmt, rc);
2541
strmov(p_str_data, "hh");
2542
p_str_length= strlen(p_str_data);
2544
bzero((char*) r_bind, sizeof(r_bind));
2545
r_bind[0].buffer_type= MYSQL_TYPE_LONG;
2546
r_bind[0].buffer= (void *)&r_int_data;
2547
r_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
2548
r_bind[1].buffer= (void *)r_str_data;
2549
r_bind[1].buffer_length= array_elements(r_str_data);
2550
r_bind[1].length= &r_str_length;
2552
rc= mysql_stmt_bind_result(stmt, r_bind);
2553
check_execute(stmt, rc);
2555
rc= mysql_stmt_execute(stmt);
2556
check_execute(stmt, rc);
2558
test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
2560
/* now retry with the same parameter values and see qcache hits */
2561
hits1= query_cache_hits(lmysql);
2562
rc= mysql_stmt_execute(stmt);
2563
check_execute(stmt, rc);
2564
test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
2565
hits2= query_cache_hits(lmysql);
2567
case TEST_QCACHE_ON_WITH_OTHER_CONN:
2568
case TEST_QCACHE_ON: /* should have hit */
2569
DIE_UNLESS(hits2-hits1 == 1);
2571
case TEST_QCACHE_OFF_ON:
2572
case TEST_QCACHE_ON_OFF: /* should not have hit */
2573
DIE_UNLESS(hits2-hits1 == 0);
2577
/* now modify parameter values and see qcache hits */
2578
strmov(p_str_data, "ii");
2579
p_str_length= strlen(p_str_data);
2580
rc= mysql_stmt_execute(stmt);
2581
check_execute(stmt, rc);
2582
test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
2583
hits1= query_cache_hits(lmysql);
2586
case TEST_QCACHE_ON:
2587
case TEST_QCACHE_OFF_ON:
2588
case TEST_QCACHE_ON_OFF: /* should not have hit */
2589
DIE_UNLESS(hits2-hits1 == 0);
2591
case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
2592
DIE_UNLESS(hits1-hits2 == 1);
2596
rc= mysql_stmt_execute(stmt);
2597
check_execute(stmt, rc);
2599
test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
2600
hits2= query_cache_hits(lmysql);
2602
mysql_stmt_close(stmt);
2605
case TEST_QCACHE_ON: /* should have hit */
2606
DIE_UNLESS(hits2-hits1 == 1);
2608
case TEST_QCACHE_OFF_ON:
2609
case TEST_QCACHE_ON_OFF: /* should not have hit */
2610
DIE_UNLESS(hits2-hits1 == 0);
2612
case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
2613
DIE_UNLESS(hits2-hits1 == 1);
2617
} /* for(iteration=...) */
2619
if (lmysql != mysql)
2620
mysql_close(lmysql);
2622
rc= mysql_query(mysql, "set global query_cache_size=0");
2627
/* Test BUG#1115 (incorrect string parameter value allocation) */
2629
static void test_bug1115()
2633
MYSQL_BIND my_bind[1];
2636
char query[MAX_TEST_QUERY_LENGTH];
2638
myheader("test_bug1115");
2640
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
2643
rc= mysql_query(mysql, "CREATE TABLE test_select(\
2644
session_id char(9) NOT NULL, \
2645
a int(8) unsigned NOT NULL, \
2646
b int(5) NOT NULL, \
2647
c int(5) NOT NULL, \
2648
d datetime NOT NULL)");
2650
rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
2651
"(\"abc\", 1, 2, 3, 2003-08-30), "
2652
"(\"abd\", 1, 2, 3, 2003-08-30), "
2653
"(\"abf\", 1, 2, 3, 2003-08-30), "
2654
"(\"abg\", 1, 2, 3, 2003-08-30), "
2655
"(\"abh\", 1, 2, 3, 2003-08-30), "
2656
"(\"abj\", 1, 2, 3, 2003-08-30), "
2657
"(\"abk\", 1, 2, 3, 2003-08-30), "
2658
"(\"abl\", 1, 2, 3, 2003-08-30), "
2659
"(\"abq\", 1, 2, 3, 2003-08-30) ");
2661
rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
2662
"(\"abw\", 1, 2, 3, 2003-08-30), "
2663
"(\"abe\", 1, 2, 3, 2003-08-30), "
2664
"(\"abr\", 1, 2, 3, 2003-08-30), "
2665
"(\"abt\", 1, 2, 3, 2003-08-30), "
2666
"(\"aby\", 1, 2, 3, 2003-08-30), "
2667
"(\"abu\", 1, 2, 3, 2003-08-30), "
2668
"(\"abi\", 1, 2, 3, 2003-08-30), "
2669
"(\"abo\", 1, 2, 3, 2003-08-30), "
2670
"(\"abp\", 1, 2, 3, 2003-08-30), "
2671
"(\"abz\", 1, 2, 3, 2003-08-30), "
2672
"(\"abx\", 1, 2, 3, 2003-08-30)");
2675
strmov(query, "SELECT * FROM test_select WHERE "
2676
"CONVERT(session_id USING utf8)= ?");
2677
stmt= mysql_simple_prepare(mysql, query);
2680
verify_param_count(stmt, 1);
2682
/* Always bzero all members of bind parameter */
2683
bzero((char*) my_bind, sizeof(my_bind));
2685
strmov(szData, (char *)"abc");
2686
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2687
my_bind[0].buffer= (void *)szData;
2688
my_bind[0].buffer_length= 10;
2689
my_bind[0].length= &length[0];
2692
rc= mysql_stmt_bind_param(stmt, my_bind);
2693
check_execute(stmt, rc);
2695
rc= mysql_stmt_execute(stmt);
2696
check_execute(stmt, rc);
2698
rc= my_process_stmt_result(stmt);
2699
DIE_UNLESS(rc == 1);
2701
strmov(szData, (char *)"venu");
2702
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2703
my_bind[0].buffer= (void *)szData;
2704
my_bind[0].buffer_length= 10;
2705
my_bind[0].length= &length[0];
2707
my_bind[0].is_null= 0;
2709
rc= mysql_stmt_bind_param(stmt, my_bind);
2710
check_execute(stmt, rc);
2712
rc= mysql_stmt_execute(stmt);
2713
check_execute(stmt, rc);
2715
rc= my_process_stmt_result(stmt);
2716
DIE_UNLESS(rc == 0);
2718
strmov(szData, (char *)"abc");
2719
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2720
my_bind[0].buffer= (void *)szData;
2721
my_bind[0].buffer_length= 10;
2722
my_bind[0].length= &length[0];
2724
my_bind[0].is_null= 0;
2726
rc= mysql_stmt_bind_param(stmt, my_bind);
2727
check_execute(stmt, rc);
2729
rc= mysql_stmt_execute(stmt);
2730
check_execute(stmt, rc);
2732
rc= my_process_stmt_result(stmt);
2733
DIE_UNLESS(rc == 1);
2735
mysql_stmt_close(stmt);
2739
/* Test BUG#1180 (optimized away part of WHERE clause) */
2741
static void test_bug1180()
2745
MYSQL_BIND my_bind[1];
2748
char query[MAX_TEST_QUERY_LENGTH];
2750
myheader("test_select_bug");
2752
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
2755
rc= mysql_query(mysql, "CREATE TABLE test_select(session_id char(9) NOT NULL)");
2757
rc= mysql_query(mysql, "INSERT INTO test_select VALUES (\"abc\")");
2760
strmov(query, "SELECT * FROM test_select WHERE ?= \"1111\" and "
2761
"session_id= \"abc\"");
2762
stmt= mysql_simple_prepare(mysql, query);
2765
verify_param_count(stmt, 1);
2767
/* Always bzero all members of bind parameter */
2768
bzero((char*) my_bind, sizeof(my_bind));
2770
strmov(szData, (char *)"abc");
2771
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2772
my_bind[0].buffer= (void *)szData;
2773
my_bind[0].buffer_length= 10;
2774
my_bind[0].length= &length[0];
2776
my_bind[0].is_null= 0;
2778
rc= mysql_stmt_bind_param(stmt, my_bind);
2779
check_execute(stmt, rc);
2781
rc= mysql_stmt_execute(stmt);
2782
check_execute(stmt, rc);
2784
rc= my_process_stmt_result(stmt);
2785
DIE_UNLESS(rc == 0);
2787
strmov(szData, (char *)"1111");
2788
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2789
my_bind[0].buffer= (void *)szData;
2790
my_bind[0].buffer_length= 10;
2791
my_bind[0].length= &length[0];
2793
my_bind[0].is_null= 0;
2795
rc= mysql_stmt_bind_param(stmt, my_bind);
2796
check_execute(stmt, rc);
2798
rc= mysql_stmt_execute(stmt);
2799
check_execute(stmt, rc);
2801
rc= my_process_stmt_result(stmt);
2802
DIE_UNLESS(rc == 1);
2804
strmov(szData, (char *)"abc");
2805
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2806
my_bind[0].buffer= (void *)szData;
2807
my_bind[0].buffer_length= 10;
2808
my_bind[0].length= &length[0];
2810
my_bind[0].is_null= 0;
2812
rc= mysql_stmt_bind_param(stmt, my_bind);
2813
check_execute(stmt, rc);
2815
rc= mysql_stmt_execute(stmt);
2816
check_execute(stmt, rc);
2818
rc= my_process_stmt_result(stmt);
2819
DIE_UNLESS(rc == 0);
2821
mysql_stmt_close(stmt);
2826
Test BUG#1644 (Insertion of more than 3 NULL columns with parameter
2830
static void test_bug1644()
2835
MYSQL_BIND my_bind[4];
2839
char query[MAX_TEST_QUERY_LENGTH];
2841
myheader("test_bug1644");
2843
rc= mysql_query(mysql, "DROP TABLE IF EXISTS foo_dfr");
2846
rc= mysql_query(mysql,
2847
"CREATE TABLE foo_dfr(col1 int, col2 int, col3 int, col4 int);");
2850
strmov(query, "INSERT INTO foo_dfr VALUES (?, ?, ?, ? )");
2851
stmt= mysql_simple_prepare(mysql, query);
2854
verify_param_count(stmt, 4);
2856
/* Always bzero all members of bind parameter */
2857
bzero((char*) my_bind, sizeof(my_bind));
2861
for (i= 0 ; i < 4 ; i++)
2863
my_bind[i].buffer_type= MYSQL_TYPE_LONG;
2864
my_bind[i].buffer= (void *)#
2865
my_bind[i].is_null= &isnull;
2868
rc= mysql_stmt_bind_param(stmt, my_bind);
2869
check_execute(stmt, rc);
2871
rc= mysql_stmt_execute(stmt);
2872
check_execute(stmt, rc);
2875
for (i= 0 ; i < 4 ; i++)
2876
my_bind[i].is_null= &isnull;
2878
rc= mysql_stmt_bind_param(stmt, my_bind);
2879
check_execute(stmt, rc);
2881
rc= mysql_stmt_execute(stmt);
2882
check_execute(stmt, rc);
2886
for (i= 0 ; i < 4 ; i++)
2887
my_bind[i].is_null= &isnull;
2889
rc= mysql_stmt_bind_param(stmt, my_bind);
2890
check_execute(stmt, rc);
2892
rc= mysql_stmt_execute(stmt);
2893
check_execute(stmt, rc);
2895
mysql_stmt_close(stmt);
2897
rc= mysql_query(mysql, "SELECT * FROM foo_dfr");
2900
result= mysql_store_result(mysql);
2903
rc= my_process_result_set(result);
2904
DIE_UNLESS(rc == 3);
2906
mysql_data_seek(result, 0);
2908
row= mysql_fetch_row(result);
2910
for (i= 0 ; i < 4 ; i++)
2912
DIE_UNLESS(strcmp(row[i], "22") == 0);
2914
row= mysql_fetch_row(result);
2916
for (i= 0 ; i < 4 ; i++)
2918
DIE_UNLESS(row[i] == 0);
2920
row= mysql_fetch_row(result);
2922
for (i= 0 ; i < 4 ; i++)
2924
DIE_UNLESS(strcmp(row[i], "88") == 0);
2926
row= mysql_fetch_row(result);
2929
mysql_free_result(result);
2933
/* Test simple select show */
2935
static void test_select_show()
2939
char query[MAX_TEST_QUERY_LENGTH];
2941
myheader("test_select_show");
2943
mysql_autocommit(mysql, TRUE);
2945
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_show");
2948
rc= mysql_query(mysql, "CREATE TABLE test_show(id int(4) NOT NULL primary "
2949
" key, name char(2))");
2952
stmt= mysql_simple_prepare(mysql, "show columns from test_show");
2955
verify_param_count(stmt, 0);
2957
rc= mysql_stmt_execute(stmt);
2958
check_execute(stmt, rc);
2960
my_process_stmt_result(stmt);
2961
mysql_stmt_close(stmt);
2963
stmt= mysql_simple_prepare(mysql, "show tables from mysql like ?");
2966
strxmov(query, "show tables from ", current_db, " like \'test_show\'", NullS);
2967
stmt= mysql_simple_prepare(mysql, query);
2970
rc= mysql_stmt_execute(stmt);
2971
check_execute(stmt, rc);
2973
my_process_stmt_result(stmt);
2974
mysql_stmt_close(stmt);
2976
stmt= mysql_simple_prepare(mysql, "describe test_show");
2979
rc= mysql_stmt_execute(stmt);
2980
check_execute(stmt, rc);
2982
my_process_stmt_result(stmt);
2983
mysql_stmt_close(stmt);
2985
stmt= mysql_simple_prepare(mysql, "show keys from test_show");
2988
rc= mysql_stmt_execute(stmt);
2989
check_execute(stmt, rc);
2991
rc= my_process_stmt_result(stmt);
2992
DIE_UNLESS(rc == 1);
2993
mysql_stmt_close(stmt);
2997
/* Test simple update */
2999
static void test_simple_update()
3006
MYSQL_BIND my_bind[2];
3008
char query[MAX_TEST_QUERY_LENGTH];
3010
myheader("test_simple_update");
3012
rc= mysql_autocommit(mysql, TRUE);
3015
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
3018
rc= mysql_query(mysql, "CREATE TABLE test_update(col1 int, "
3019
" col2 varchar(50), col3 int )");
3022
rc= mysql_query(mysql, "INSERT INTO test_update VALUES(1, 'MySQL', 100)");
3025
verify_affected_rows(1);
3027
rc= mysql_commit(mysql);
3030
/* insert by prepare */
3031
strmov(query, "UPDATE test_update SET col2= ? WHERE col1= ?");
3032
stmt= mysql_simple_prepare(mysql, query);
3035
verify_param_count(stmt, 2);
3037
/* Always bzero all members of bind parameter */
3038
bzero((char*) my_bind, sizeof(my_bind));
3041
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
3042
my_bind[0].buffer= szData; /* string data */
3043
my_bind[0].buffer_length= sizeof(szData);
3044
my_bind[0].length= &length[0];
3045
length[0]= my_sprintf(szData, (szData, "updated-data"));
3047
my_bind[1].buffer= (void *) &nData;
3048
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
3050
rc= mysql_stmt_bind_param(stmt, my_bind);
3051
check_execute(stmt, rc);
3053
rc= mysql_stmt_execute(stmt);
3054
check_execute(stmt, rc);
3055
verify_affected_rows(1);
3057
mysql_stmt_close(stmt);
3059
/* now fetch the results ..*/
3060
rc= mysql_commit(mysql);
3063
/* test the results now, only one row should exist */
3064
rc= mysql_query(mysql, "SELECT * FROM test_update");
3067
/* get the result */
3068
result= mysql_store_result(mysql);
3071
rc= my_process_result_set(result);
3072
DIE_UNLESS(rc == 1);
3073
mysql_free_result(result);
3077
/* Test simple long data handling */
3079
static void test_long_data()
3085
MYSQL_BIND my_bind[3];
3086
char query[MAX_TEST_QUERY_LENGTH];
3088
myheader("test_long_data");
3090
rc= mysql_autocommit(mysql, TRUE);
3093
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
3096
rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, "
3097
" col2 long varchar, col3 long varbinary)");
3100
strmov(query, "INSERT INTO test_long_data(col1, col2) VALUES(?)");
3101
stmt= mysql_simple_prepare(mysql, query);
3104
strmov(query, "INSERT INTO test_long_data(col1, col2, col3) VALUES(?, ?, ?)");
3105
stmt= mysql_simple_prepare(mysql, query);
3108
verify_param_count(stmt, 3);
3110
/* Always bzero all members of bind parameter */
3111
bzero((char*) my_bind, sizeof(my_bind));
3113
my_bind[0].buffer= (void *)&int_data;
3114
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3116
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
3118
my_bind[2]= my_bind[1];
3119
rc= mysql_stmt_bind_param(stmt, my_bind);
3120
check_execute(stmt, rc);
3123
data= (char *)"Michael";
3125
/* supply data in pieces */
3126
rc= mysql_stmt_send_long_data(stmt, 1, data, strlen(data));
3127
data= (char *)" 'Monty' Widenius";
3128
rc= mysql_stmt_send_long_data(stmt, 1, data, strlen(data));
3129
check_execute(stmt, rc);
3130
rc= mysql_stmt_send_long_data(stmt, 2, "Venu (venu@mysql.com)", 4);
3131
check_execute(stmt, rc);
3134
rc= mysql_stmt_execute(stmt);
3136
fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
3137
check_execute(stmt, rc);
3139
rc= mysql_commit(mysql);
3142
/* now fetch the results ..*/
3143
rc= mysql_query(mysql, "SELECT * FROM test_long_data");
3146
/* get the result */
3147
result= mysql_store_result(mysql);
3150
rc= my_process_result_set(result);
3151
DIE_UNLESS(rc == 1);
3152
mysql_free_result(result);
3154
verify_col_data("test_long_data", "col1", "999");
3155
verify_col_data("test_long_data", "col2", "Michael 'Monty' Widenius");
3156
verify_col_data("test_long_data", "col3", "Venu");
3157
mysql_stmt_close(stmt);
3161
/* Test long data (string) handling */
3163
static void test_long_data_str()
3171
MYSQL_BIND my_bind[2];
3173
char query[MAX_TEST_QUERY_LENGTH];
3175
myheader("test_long_data_str");
3177
rc= mysql_autocommit(mysql, TRUE);
3180
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
3183
rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(id int, longstr long varchar)");
3186
strmov(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
3187
stmt= mysql_simple_prepare(mysql, query);
3190
verify_param_count(stmt, 2);
3192
/* Always bzero all members of bind parameter */
3193
bzero((char*) my_bind, sizeof(my_bind));
3195
my_bind[0].buffer= (void *)&length;
3196
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3197
my_bind[0].is_null= &is_null[0];
3201
my_bind[1].buffer= data; /* string data */
3202
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
3203
my_bind[1].length= &length1;
3204
my_bind[1].is_null= &is_null[1];
3206
rc= mysql_stmt_bind_param(stmt, my_bind);
3207
check_execute(stmt, rc);
3210
strmov(data, "MySQL AB");
3212
/* supply data in pieces */
3213
for(i= 0; i < 4; i++)
3215
rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 5);
3216
check_execute(stmt, rc);
3219
rc= mysql_stmt_execute(stmt);
3221
fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
3222
check_execute(stmt, rc);
3224
mysql_stmt_close(stmt);
3226
rc= mysql_commit(mysql);
3229
/* now fetch the results ..*/
3230
rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr FROM test_long_data_str");
3233
/* get the result */
3234
result= mysql_store_result(mysql);
3237
rc= my_process_result_set(result);
3238
DIE_UNLESS(rc == 1);
3239
mysql_free_result(result);
3241
my_sprintf(data, (data, "%d", i*5));
3242
verify_col_data("test_long_data_str", "LENGTH(longstr)", data);
3245
strxmov(data, data, "MySQL", NullS);
3246
verify_col_data("test_long_data_str", "longstr", data);
3248
rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
3253
/* Test long data (string) handling */
3255
static void test_long_data_str1()
3261
ulong max_blob_length, blob_length, length1;
3264
MYSQL_BIND my_bind[2];
3266
char query[MAX_TEST_QUERY_LENGTH];
3268
myheader("test_long_data_str1");
3270
rc= mysql_autocommit(mysql, TRUE);
3273
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
3276
rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(longstr long varchar, blb long varbinary)");
3279
strmov(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
3280
stmt= mysql_simple_prepare(mysql, query);
3283
verify_param_count(stmt, 2);
3285
/* Always bzero all members of bind parameter */
3286
bzero((char*) my_bind, sizeof(my_bind));
3288
my_bind[0].buffer= data; /* string data */
3289
my_bind[0].buffer_length= sizeof(data);
3290
my_bind[0].length= &length1;
3291
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
3294
my_bind[1]= my_bind[0];
3295
my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
3297
rc= mysql_stmt_bind_param(stmt, my_bind);
3298
check_execute(stmt, rc);
3299
length= my_sprintf(data, (data, "MySQL AB"));
3301
/* supply data in pieces */
3302
for (i= 0; i < 3; i++)
3304
rc= mysql_stmt_send_long_data(stmt, 0, data, length);
3305
check_execute(stmt, rc);
3307
rc= mysql_stmt_send_long_data(stmt, 1, data, 2);
3308
check_execute(stmt, rc);
3312
rc= mysql_stmt_execute(stmt);
3314
fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
3315
check_execute(stmt, rc);
3317
mysql_stmt_close(stmt);
3319
rc= mysql_commit(mysql);
3322
/* now fetch the results ..*/
3323
rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr, LENGTH(blb), blb FROM test_long_data_str");
3326
/* get the result */
3327
result= mysql_store_result(mysql);
3329
mysql_field_seek(result, 1);
3330
field= mysql_fetch_field(result);
3331
max_blob_length= field->max_length;
3335
rc= my_process_result_set(result);
3336
DIE_UNLESS(rc == 1);
3337
mysql_free_result(result);
3339
my_sprintf(data, (data, "%ld", (long)i*length));
3340
verify_col_data("test_long_data_str", "length(longstr)", data);
3342
my_sprintf(data, (data, "%d", i*2));
3343
verify_col_data("test_long_data_str", "length(blb)", data);
3345
/* Test length of field->max_length */
3346
stmt= mysql_simple_prepare(mysql, "SELECT * from test_long_data_str");
3348
verify_param_count(stmt, 0);
3350
rc= mysql_stmt_execute(stmt);
3351
check_execute(stmt, rc);
3353
rc= mysql_stmt_store_result(stmt);
3354
check_execute(stmt, rc);
3356
result= mysql_stmt_result_metadata(stmt);
3357
field= mysql_fetch_fields(result);
3359
/* First test what happens if STMT_ATTR_UPDATE_MAX_LENGTH is not used */
3360
DIE_UNLESS(field->max_length == 0);
3361
mysql_free_result(result);
3363
/* Enable updating of field->max_length */
3365
mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &true_value);
3366
rc= mysql_stmt_execute(stmt);
3367
check_execute(stmt, rc);
3369
rc= mysql_stmt_store_result(stmt);
3370
check_execute(stmt, rc);
3372
result= mysql_stmt_result_metadata(stmt);
3373
field= mysql_fetch_fields(result);
3375
DIE_UNLESS(field->max_length == max_blob_length);
3377
/* Fetch results into a data buffer that is smaller than data */
3378
bzero((char*) my_bind, sizeof(*my_bind));
3379
my_bind[0].buffer_type= MYSQL_TYPE_BLOB;
3380
my_bind[0].buffer= (void *) &data; /* this buffer won't be altered */
3381
my_bind[0].buffer_length= 16;
3382
my_bind[0].length= &blob_length;
3383
my_bind[0].error= &my_bind[0].error_value;
3384
rc= mysql_stmt_bind_result(stmt, my_bind);
3387
rc= mysql_stmt_fetch(stmt);
3388
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
3389
DIE_UNLESS(my_bind[0].error_value);
3390
DIE_UNLESS(strlen(data) == 16);
3391
DIE_UNLESS(blob_length == max_blob_length);
3393
/* Fetch all data */
3394
bzero((char*) (my_bind+1), sizeof(*my_bind));
3395
my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
3396
my_bind[1].buffer= (void *) &data; /* this buffer won't be altered */
3397
my_bind[1].buffer_length= sizeof(data);
3398
my_bind[1].length= &blob_length;
3399
bzero(data, sizeof(data));
3400
mysql_stmt_fetch_column(stmt, my_bind+1, 0, 0);
3401
DIE_UNLESS(strlen(data) == max_blob_length);
3403
mysql_free_result(result);
3404
mysql_stmt_close(stmt);
3406
/* Drop created table */
3407
rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
3412
/* Test long data (binary) handling */
3414
static void test_long_data_bin()
3421
MYSQL_BIND my_bind[2];
3422
char query[MAX_TEST_QUERY_LENGTH];
3425
myheader("test_long_data_bin");
3427
rc= mysql_autocommit(mysql, TRUE);
3430
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_bin");
3433
rc= mysql_query(mysql, "CREATE TABLE test_long_data_bin(id int, longbin long varbinary)");
3436
strmov(query, "INSERT INTO test_long_data_bin VALUES(?, ?)");
3437
stmt= mysql_simple_prepare(mysql, query);
3440
verify_param_count(stmt, 2);
3442
/* Always bzero all members of bind parameter */
3443
bzero((char*) my_bind, sizeof(my_bind));
3445
my_bind[0].buffer= (void *)&length;
3446
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3449
my_bind[1].buffer= data; /* string data */
3450
my_bind[1].buffer_type= MYSQL_TYPE_LONG_BLOB;
3451
rc= mysql_stmt_bind_param(stmt, my_bind);
3452
check_execute(stmt, rc);
3455
strmov(data, "MySQL AB");
3457
/* supply data in pieces */
3460
for (i= 0; i < 100; i++)
3462
rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 4);
3463
check_execute(stmt, rc);
3467
rc= mysql_stmt_execute(stmt);
3469
fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
3470
check_execute(stmt, rc);
3472
mysql_stmt_close(stmt);
3474
rc= mysql_commit(mysql);
3477
/* now fetch the results ..*/
3478
rc= mysql_query(mysql, "SELECT LENGTH(longbin), longbin FROM test_long_data_bin");
3481
/* get the result */
3482
result= mysql_store_result(mysql);
3485
rc= my_process_result_set(result);
3486
DIE_UNLESS(rc == 1);
3487
mysql_free_result(result);
3491
/* Test simple delete */
3493
static void test_simple_delete()
3497
char szData[30]= {0};
3500
MYSQL_BIND my_bind[2];
3502
char query[MAX_TEST_QUERY_LENGTH];
3504
myheader("test_simple_delete");
3506
rc= mysql_autocommit(mysql, TRUE);
3509
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_simple_delete");
3512
rc= mysql_query(mysql, "CREATE TABLE test_simple_delete(col1 int, \
3513
col2 varchar(50), col3 int )");
3516
rc= mysql_query(mysql, "INSERT INTO test_simple_delete VALUES(1, 'MySQL', 100)");
3519
verify_affected_rows(1);
3521
rc= mysql_commit(mysql);
3524
/* insert by prepare */
3525
strmov(query, "DELETE FROM test_simple_delete WHERE col1= ? AND "
3526
"CONVERT(col2 USING utf8)= ? AND col3= 100");
3527
stmt= mysql_simple_prepare(mysql, query);
3530
verify_param_count(stmt, 2);
3532
/* Always bzero all members of bind parameter */
3533
bzero((char*) my_bind, sizeof(my_bind));
3536
strmov(szData, "MySQL");
3537
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
3538
my_bind[1].buffer= szData; /* string data */
3539
my_bind[1].buffer_length= sizeof(szData);
3540
my_bind[1].length= &length[1];
3543
my_bind[0].buffer= (void *)&nData;
3544
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3546
rc= mysql_stmt_bind_param(stmt, my_bind);
3547
check_execute(stmt, rc);
3549
rc= mysql_stmt_execute(stmt);
3550
check_execute(stmt, rc);
3552
verify_affected_rows(1);
3554
mysql_stmt_close(stmt);
3556
/* now fetch the results ..*/
3557
rc= mysql_commit(mysql);
3560
/* test the results now, only one row should exist */
3561
rc= mysql_query(mysql, "SELECT * FROM test_simple_delete");
3564
/* get the result */
3565
result= mysql_store_result(mysql);
3568
rc= my_process_result_set(result);
3569
DIE_UNLESS(rc == 0);
3570
mysql_free_result(result);
3574
/* Test simple update */
3576
static void test_update()
3583
MYSQL_BIND my_bind[2];
3585
char query[MAX_TEST_QUERY_LENGTH];
3587
myheader("test_update");
3589
rc= mysql_autocommit(mysql, TRUE);
3592
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
3595
rc= mysql_query(mysql, "CREATE TABLE test_update("
3596
"col1 int primary key auto_increment, "
3597
"col2 varchar(50), col3 int )");
3600
strmov(query, "INSERT INTO test_update(col2, col3) VALUES(?, ?)");
3601
stmt= mysql_simple_prepare(mysql, query);
3604
verify_param_count(stmt, 2);
3606
/* Always bzero all members of bind parameter */
3607
bzero((char*) my_bind, sizeof(my_bind));
3610
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
3611
my_bind[0].buffer= szData;
3612
my_bind[0].buffer_length= sizeof(szData);
3613
my_bind[0].length= &length[0];
3614
length[0]= my_sprintf(szData, (szData, "inserted-data"));
3616
my_bind[1].buffer= (void *)&nData;
3617
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
3619
rc= mysql_stmt_bind_param(stmt, my_bind);
3620
check_execute(stmt, rc);
3623
rc= mysql_stmt_execute(stmt);
3624
check_execute(stmt, rc);
3626
verify_affected_rows(1);
3627
mysql_stmt_close(stmt);
3629
strmov(query, "UPDATE test_update SET col2= ? WHERE col3= ?");
3630
stmt= mysql_simple_prepare(mysql, query);
3633
verify_param_count(stmt, 2);
3636
/* Always bzero all members of bind parameter */
3637
bzero((char*) my_bind, sizeof(my_bind));
3639
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
3640
my_bind[0].buffer= szData;
3641
my_bind[0].buffer_length= sizeof(szData);
3642
my_bind[0].length= &length[0];
3643
length[0]= my_sprintf(szData, (szData, "updated-data"));
3645
my_bind[1].buffer= (void *)&nData;
3646
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
3648
rc= mysql_stmt_bind_param(stmt, my_bind);
3649
check_execute(stmt, rc);
3651
rc= mysql_stmt_execute(stmt);
3652
check_execute(stmt, rc);
3653
verify_affected_rows(1);
3655
mysql_stmt_close(stmt);
3657
/* now fetch the results ..*/
3658
rc= mysql_commit(mysql);
3661
/* test the results now, only one row should exist */
3662
rc= mysql_query(mysql, "SELECT * FROM test_update");
3665
/* get the result */
3666
result= mysql_store_result(mysql);
3669
rc= my_process_result_set(result);
3670
DIE_UNLESS(rc == 1);
3671
mysql_free_result(result);
3675
/* Test prepare without parameters */
3677
static void test_prepare_noparam()
3682
char query[MAX_TEST_QUERY_LENGTH];
3684
myheader("test_prepare_noparam");
3686
rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
3690
rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 int, col2 varchar(50))");
3693
/* insert by prepare */
3694
strmov(query, "INSERT INTO my_prepare VALUES(10, 'venu')");
3695
stmt= mysql_simple_prepare(mysql, query);
3698
verify_param_count(stmt, 0);
3700
rc= mysql_stmt_execute(stmt);
3701
check_execute(stmt, rc);
3703
mysql_stmt_close(stmt);
3705
/* now fetch the results ..*/
3706
rc= mysql_commit(mysql);
3709
/* test the results now, only one row should exist */
3710
rc= mysql_query(mysql, "SELECT * FROM my_prepare");
3713
/* get the result */
3714
result= mysql_store_result(mysql);
3717
rc= my_process_result_set(result);
3718
DIE_UNLESS(rc == 1);
3719
mysql_free_result(result);
3723
/* Test simple bind result */
3725
static void test_bind_result()
3732
MYSQL_BIND my_bind[2];
3735
myheader("test_bind_result");
3737
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
3740
rc= mysql_query(mysql, "CREATE TABLE test_bind_result(col1 int , col2 varchar(50))");
3743
rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(10, 'venu')");
3746
rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(20, 'MySQL')");
3749
rc= mysql_query(mysql, "INSERT INTO test_bind_result(col2) VALUES('monty')");
3752
rc= mysql_commit(mysql);
3757
bzero((char*) my_bind, sizeof(my_bind));
3758
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3759
my_bind[0].buffer= (void *) &nData; /* integer data */
3760
my_bind[0].is_null= &is_null[0];
3762
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
3763
my_bind[1].buffer= szData; /* string data */
3764
my_bind[1].buffer_length= sizeof(szData);
3765
my_bind[1].length= &length1;
3766
my_bind[1].is_null= &is_null[1];
3768
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_result");
3771
rc= mysql_stmt_bind_result(stmt, my_bind);
3772
check_execute(stmt, rc);
3774
rc= mysql_stmt_execute(stmt);
3775
check_execute(stmt, rc);
3777
rc= mysql_stmt_fetch(stmt);
3778
check_execute(stmt, rc);
3781
fprintf(stdout, "\n row 1: %d, %s(%lu)", nData, szData, length1);
3782
DIE_UNLESS(nData == 10);
3783
DIE_UNLESS(strcmp(szData, "venu") == 0);
3784
DIE_UNLESS(length1 == 4);
3786
rc= mysql_stmt_fetch(stmt);
3787
check_execute(stmt, rc);
3790
fprintf(stdout, "\n row 2: %d, %s(%lu)", nData, szData, length1);
3791
DIE_UNLESS(nData == 20);
3792
DIE_UNLESS(strcmp(szData, "MySQL") == 0);
3793
DIE_UNLESS(length1 == 5);
3795
rc= mysql_stmt_fetch(stmt);
3796
check_execute(stmt, rc);
3798
if (!opt_silent && is_null[0])
3799
fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
3800
DIE_UNLESS(is_null[0]);
3801
DIE_UNLESS(strcmp(szData, "monty") == 0);
3802
DIE_UNLESS(length1 == 5);
3804
rc= mysql_stmt_fetch(stmt);
3805
DIE_UNLESS(rc == MYSQL_NO_DATA);
3807
mysql_stmt_close(stmt);
3811
/* Test ext bind result */
3813
static void test_bind_result_ext()
3823
char szData[20], bData[20];
3824
ulong szLength, bLength;
3825
MYSQL_BIND my_bind[8];
3829
myheader("test_bind_result_ext");
3831
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
3834
rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, "
3836
" c3 int, c4 bigint, "
3837
" c5 float, c6 double, "
3838
" c7 varbinary(10), "
3839
" c8 varchar(50))");
3842
rc= mysql_query(mysql, "INSERT INTO test_bind_result "
3843
"VALUES (19, 2999, 3999, 4999999, "
3844
" 2345.6, 5678.89563, 'venu', 'mysql')");
3847
rc= mysql_commit(mysql);
3850
bzero((char*) my_bind, sizeof(my_bind));
3851
for (i= 0; i < (int) array_elements(my_bind); i++)
3853
my_bind[i].length= &length[i];
3854
my_bind[i].is_null= &is_null[i];
3857
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
3858
my_bind[0].buffer= (void *)&t_data;
3860
my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
3861
my_bind[2].buffer_type= MYSQL_TYPE_LONG;
3863
my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
3864
my_bind[1].buffer= (void *)&s_data;
3866
my_bind[2].buffer= (void *)&i_data;
3867
my_bind[3].buffer= (void *)&b_data;
3869
my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
3870
my_bind[4].buffer= (void *)&f_data;
3872
my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
3873
my_bind[5].buffer= (void *)&d_data;
3875
my_bind[6].buffer_type= MYSQL_TYPE_STRING;
3876
my_bind[6].buffer= (void *)szData;
3877
my_bind[6].buffer_length= sizeof(szData);
3878
my_bind[6].length= &szLength;
3880
my_bind[7].buffer_type= MYSQL_TYPE_TINY_BLOB;
3881
my_bind[7].buffer= (void *)&bData;
3882
my_bind[7].length= &bLength;
3883
my_bind[7].buffer_length= sizeof(bData);
3885
stmt= mysql_simple_prepare(mysql, "select * from test_bind_result");
3888
rc= mysql_stmt_bind_result(stmt, my_bind);
3889
check_execute(stmt, rc);
3891
rc= mysql_stmt_execute(stmt);
3892
check_execute(stmt, rc);
3894
rc= mysql_stmt_fetch(stmt);
3895
check_execute(stmt, rc);
3899
fprintf(stdout, "\n data (tiny) : %d", t_data);
3900
fprintf(stdout, "\n data (short) : %d", s_data);
3901
fprintf(stdout, "\n data (int) : %d", i_data);
3902
fprintf(stdout, "\n data (big) : %s", llstr(b_data, llbuf));
3904
fprintf(stdout, "\n data (float) : %f", f_data);
3905
fprintf(stdout, "\n data (double) : %f", d_data);
3907
fprintf(stdout, "\n data (str) : %s(%lu)", szData, szLength);
3909
bData[bLength]= '\0'; /* bData is binary */
3910
fprintf(stdout, "\n data (bin) : %s(%lu)", bData, bLength);
3913
DIE_UNLESS(t_data == 19);
3914
DIE_UNLESS(s_data == 2999);
3915
DIE_UNLESS(i_data == 3999);
3916
DIE_UNLESS(b_data == 4999999);
3917
/*DIE_UNLESS(f_data == 2345.60);*/
3918
/*DIE_UNLESS(d_data == 5678.89563);*/
3919
DIE_UNLESS(strcmp(szData, "venu") == 0);
3920
DIE_UNLESS(strncmp(bData, "mysql", 5) == 0);
3921
DIE_UNLESS(szLength == 4);
3922
DIE_UNLESS(bLength == 5);
3924
rc= mysql_stmt_fetch(stmt);
3925
DIE_UNLESS(rc == MYSQL_NO_DATA);
3927
mysql_stmt_close(stmt);
3931
/* Test ext bind result */
3933
static void test_bind_result_ext1()
3946
MYSQL_BIND my_bind[8];
3949
myheader("test_bind_result_ext1");
3951
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
3954
rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, c2 smallint, \
3955
c3 int, c4 bigint, \
3956
c5 float, c6 double, \
3961
rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(120, 2999, 3999, 54, \
3966
rc= mysql_commit(mysql);
3969
bzero((char*) my_bind, sizeof(my_bind));
3970
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
3971
my_bind[0].buffer= (void *) t_data;
3972
my_bind[0].buffer_length= sizeof(t_data);
3973
my_bind[0].error= &my_bind[0].error_value;
3975
my_bind[1].buffer_type= MYSQL_TYPE_FLOAT;
3976
my_bind[1].buffer= (void *)&s_data;
3977
my_bind[1].buffer_length= 0;
3978
my_bind[1].error= &my_bind[1].error_value;
3980
my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
3981
my_bind[2].buffer= (void *)&i_data;
3982
my_bind[2].buffer_length= 0;
3983
my_bind[2].error= &my_bind[2].error_value;
3985
my_bind[3].buffer_type= MYSQL_TYPE_TINY;
3986
my_bind[3].buffer= (void *)&b_data;
3987
my_bind[3].buffer_length= 0;
3988
my_bind[3].error= &my_bind[3].error_value;
3990
my_bind[4].buffer_type= MYSQL_TYPE_LONG;
3991
my_bind[4].buffer= (void *)&f_data;
3992
my_bind[4].buffer_length= 0;
3993
my_bind[4].error= &my_bind[4].error_value;
3995
my_bind[5].buffer_type= MYSQL_TYPE_STRING;
3996
my_bind[5].buffer= (void *)d_data;
3997
my_bind[5].buffer_length= sizeof(d_data);
3998
my_bind[5].error= &my_bind[5].error_value;
4000
my_bind[6].buffer_type= MYSQL_TYPE_LONG;
4001
my_bind[6].buffer= (void *)&bData;
4002
my_bind[6].buffer_length= 0;
4003
my_bind[6].error= &my_bind[6].error_value;
4005
my_bind[7].buffer_type= MYSQL_TYPE_DOUBLE;
4006
my_bind[7].buffer= (void *)&szData;
4007
my_bind[7].buffer_length= 0;
4008
my_bind[7].error= &my_bind[7].error_value;
4010
for (i= 0; i < array_elements(my_bind); i++)
4012
my_bind[i].is_null= &is_null[i];
4013
my_bind[i].length= &length[i];
4016
stmt= mysql_simple_prepare(mysql, "select * from test_bind_result");
4019
rc= mysql_stmt_bind_result(stmt, my_bind);
4020
check_execute(stmt, rc);
4022
rc= mysql_stmt_execute(stmt);
4023
check_execute(stmt, rc);
4025
rc= mysql_stmt_fetch(stmt);
4026
printf("rc=%d\n", rc);
4027
DIE_UNLESS(rc == 0);
4031
fprintf(stdout, "\n data (tiny) : %s(%lu)", t_data, length[0]);
4032
fprintf(stdout, "\n data (short) : %f(%lu)", s_data, length[1]);
4033
fprintf(stdout, "\n data (int) : %d(%lu)", i_data, length[2]);
4034
fprintf(stdout, "\n data (big) : %d(%lu)", b_data, length[3]);
4036
fprintf(stdout, "\n data (float) : %d(%lu)", f_data, length[4]);
4037
fprintf(stdout, "\n data (double) : %s(%lu)", d_data, length[5]);
4039
fprintf(stdout, "\n data (bin) : %ld(%lu)", bData, length[6]);
4040
fprintf(stdout, "\n data (str) : %g(%lu)", szData, length[7]);
4043
DIE_UNLESS(strcmp(t_data, "120") == 0);
4044
DIE_UNLESS(i_data == 3999);
4045
DIE_UNLESS(f_data == 2);
4046
DIE_UNLESS(strcmp(d_data, "58.89") == 0);
4047
DIE_UNLESS(b_data == 54);
4049
DIE_UNLESS(length[0] == 3);
4050
DIE_UNLESS(length[1] == 4);
4051
DIE_UNLESS(length[2] == 2);
4052
DIE_UNLESS(length[3] == 1);
4053
DIE_UNLESS(length[4] == 4);
4054
DIE_UNLESS(length[5] == 5);
4055
DIE_UNLESS(length[6] == 4);
4056
DIE_UNLESS(length[7] == 8);
4058
rc= mysql_stmt_fetch(stmt);
4059
DIE_UNLESS(rc == MYSQL_NO_DATA);
4061
mysql_stmt_close(stmt);
4065
/* Generalized fetch conversion routine for all basic types */
4067
static void bind_fetch(int row_count)
4070
int rc, i, count= row_count;
4080
MYSQL_BIND my_bind[7];
4083
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_bind_fetch VALUES "
4084
"(?, ?, ?, ?, ?, ?, ?)");
4087
verify_param_count(stmt, 7);
4089
/* Always bzero all members of bind parameter */
4090
bzero((char*) my_bind, sizeof(my_bind));
4092
for (i= 0; i < (int) array_elements(my_bind); i++)
4094
my_bind[i].buffer_type= MYSQL_TYPE_LONG;
4095
my_bind[i].buffer= (void *) &data[i];
4097
rc= mysql_stmt_bind_param(stmt, my_bind);
4098
check_execute(stmt, rc);
4103
for (i= 0; i < (int) array_elements(my_bind); i++)
4108
rc= mysql_stmt_execute(stmt);
4109
check_execute(stmt, rc);
4112
rc= mysql_commit(mysql);
4115
mysql_stmt_close(stmt);
4117
rc= my_stmt_result("SELECT * FROM test_bind_fetch");
4118
DIE_UNLESS(row_count == rc);
4120
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_fetch");
4123
for (i= 0; i < (int) array_elements(my_bind); i++)
4125
my_bind[i].buffer= (void *) &data[i];
4126
my_bind[i].length= &length[i];
4127
my_bind[i].is_null= &is_null[i];
4130
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
4131
my_bind[0].buffer= (void *)&i8_data;
4133
my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
4134
my_bind[1].buffer= (void *)&i16_data;
4136
my_bind[2].buffer_type= MYSQL_TYPE_LONG;
4137
my_bind[2].buffer= (void *)&i32_data;
4139
my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
4140
my_bind[3].buffer= (void *)&i64_data;
4142
my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
4143
my_bind[4].buffer= (void *)&f_data;
4145
my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
4146
my_bind[5].buffer= (void *)&d_data;
4148
my_bind[6].buffer_type= MYSQL_TYPE_STRING;
4149
my_bind[6].buffer= (void *)&s_data;
4150
my_bind[6].buffer_length= sizeof(s_data);
4152
rc= mysql_stmt_bind_result(stmt, my_bind);
4153
check_execute(stmt, rc);
4155
rc= mysql_stmt_execute(stmt);
4156
check_execute(stmt, rc);
4158
rc= mysql_stmt_store_result(stmt);
4159
check_execute(stmt, rc);
4163
rc= mysql_stmt_fetch(stmt);
4164
check_execute(stmt, rc);
4168
fprintf(stdout, "\n");
4169
fprintf(stdout, "\n tiny : %ld(%lu)", (ulong) i8_data, length[0]);
4170
fprintf(stdout, "\n short : %ld(%lu)", (ulong) i16_data, length[1]);
4171
fprintf(stdout, "\n int : %ld(%lu)", (ulong) i32_data, length[2]);
4172
fprintf(stdout, "\n longlong : %ld(%lu)", (ulong) i64_data, length[3]);
4173
fprintf(stdout, "\n float : %f(%lu)", f_data, length[4]);
4174
fprintf(stdout, "\n double : %g(%lu)", d_data, length[5]);
4175
fprintf(stdout, "\n char : %s(%lu)", s_data, length[6]);
4180
DIE_UNLESS((int) i8_data == rc);
4181
DIE_UNLESS(length[0] == 1);
4185
DIE_UNLESS((int) i16_data == rc);
4186
DIE_UNLESS(length[1] == 2);
4190
DIE_UNLESS((int) i32_data == rc);
4191
DIE_UNLESS(length[2] == 4);
4195
DIE_UNLESS((int) i64_data == rc);
4196
DIE_UNLESS(length[3] == 8);
4200
DIE_UNLESS((int)f_data == rc);
4201
DIE_UNLESS(length[4] == 4);
4205
DIE_UNLESS((int)d_data == rc);
4206
DIE_UNLESS(length[5] == 8);
4212
long len= my_sprintf(buff, (buff, "%d", rc));
4213
DIE_UNLESS(strcmp(s_data, buff) == 0);
4214
DIE_UNLESS(length[6] == (ulong) len);
4217
rc= mysql_stmt_fetch(stmt);
4218
DIE_UNLESS(rc == MYSQL_NO_DATA);
4220
mysql_stmt_close(stmt);
4224
/* Test fetching of date, time and ts */
4226
static void test_fetch_date()
4231
char date[25], my_time[25], ts[25], ts_4[25], ts_6[20], dt[20];
4232
ulong d_length, t_length, ts_length, ts4_length, ts6_length,
4233
dt_length, y_length;
4234
MYSQL_BIND my_bind[8];
4238
myheader("test_fetch_date");
4240
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
4243
rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 date, c2 time, \
4251
rc= mysql_query(mysql, "SET SQL_MODE=''");
4252
rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES('2002-01-02', \
4254
'2002-01-02 17:46:59', \
4257
'2020', '1999-12-29')");
4260
rc= mysql_commit(mysql);
4263
bzero((char*) my_bind, sizeof(my_bind));
4264
for (i= 0; i < array_elements(my_bind); i++)
4266
my_bind[i].is_null= &is_null[i];
4267
my_bind[i].length= &length[i];
4270
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
4271
my_bind[1]= my_bind[2]= my_bind[0];
4273
my_bind[0].buffer= (void *)&date;
4274
my_bind[0].buffer_length= sizeof(date);
4275
my_bind[0].length= &d_length;
4277
my_bind[1].buffer= (void *)&my_time;
4278
my_bind[1].buffer_length= sizeof(my_time);
4279
my_bind[1].length= &t_length;
4281
my_bind[2].buffer= (void *)&ts;
4282
my_bind[2].buffer_length= sizeof(ts);
4283
my_bind[2].length= &ts_length;
4285
my_bind[3].buffer_type= MYSQL_TYPE_LONG;
4286
my_bind[3].buffer= (void *)&year;
4287
my_bind[3].length= &y_length;
4289
my_bind[4].buffer_type= MYSQL_TYPE_STRING;
4290
my_bind[4].buffer= (void *)&dt;
4291
my_bind[4].buffer_length= sizeof(dt);
4292
my_bind[4].length= &dt_length;
4294
my_bind[5].buffer_type= MYSQL_TYPE_STRING;
4295
my_bind[5].buffer= (void *)&ts_4;
4296
my_bind[5].buffer_length= sizeof(ts_4);
4297
my_bind[5].length= &ts4_length;
4299
my_bind[6].buffer_type= MYSQL_TYPE_STRING;
4300
my_bind[6].buffer= (void *)&ts_6;
4301
my_bind[6].buffer_length= sizeof(ts_6);
4302
my_bind[6].length= &ts6_length;
4304
rc= my_stmt_result("SELECT * FROM test_bind_result");
4305
DIE_UNLESS(rc == 1);
4307
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_result");
4310
rc= mysql_stmt_bind_result(stmt, my_bind);
4311
check_execute(stmt, rc);
4313
rc= mysql_stmt_execute(stmt);
4314
check_execute(stmt, rc);
4317
rc= mysql_stmt_fetch(stmt);
4318
check_execute(stmt, rc);
4322
fprintf(stdout, "\n date : %s(%lu)", date, d_length);
4323
fprintf(stdout, "\n time : %s(%lu)", my_time, t_length);
4324
fprintf(stdout, "\n ts : %s(%lu)", ts, ts_length);
4325
fprintf(stdout, "\n year : %d(%lu)", year, y_length);
4326
fprintf(stdout, "\n dt : %s(%lu)", dt, dt_length);
4327
fprintf(stdout, "\n ts(4) : %s(%lu)", ts_4, ts4_length);
4328
fprintf(stdout, "\n ts(6) : %s(%lu)", ts_6, ts6_length);
4331
DIE_UNLESS(strcmp(date, "2002-01-02") == 0);
4332
DIE_UNLESS(d_length == 10);
4334
DIE_UNLESS(strcmp(my_time, "12:49:00") == 0);
4335
DIE_UNLESS(t_length == 8);
4337
DIE_UNLESS(strcmp(ts, "2002-01-02 17:46:59") == 0);
4338
DIE_UNLESS(ts_length == 19);
4340
DIE_UNLESS(year == 2010);
4341
DIE_UNLESS(y_length == 4);
4343
DIE_UNLESS(strcmp(dt, "2010-07-10 00:00:00") == 0);
4344
DIE_UNLESS(dt_length == 19);
4346
DIE_UNLESS(strcmp(ts_4, "0000-00-00 00:00:00") == 0);
4347
DIE_UNLESS(ts4_length == strlen("0000-00-00 00:00:00"));
4349
DIE_UNLESS(strcmp(ts_6, "1999-12-29 00:00:00") == 0);
4350
DIE_UNLESS(ts6_length == 19);
4352
rc= mysql_stmt_fetch(stmt);
4353
DIE_UNLESS(rc == MYSQL_NO_DATA);
4355
mysql_stmt_close(stmt);
4359
/* Test fetching of str to all types */
4361
static void test_fetch_str()
4365
myheader("test_fetch_str");
4367
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4370
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 char(10), \
4383
/* Test fetching of long to all types */
4385
static void test_fetch_long()
4389
myheader("test_fetch_long");
4391
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4394
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 int unsigned, \
4407
/* Test fetching of short to all types */
4409
static void test_fetch_short()
4413
myheader("test_fetch_short");
4415
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4418
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 smallint unsigned, \
4420
c3 smallint unsigned, \
4424
c7 smallint unsigned)");
4431
/* Test fetching of tiny to all types */
4433
static void test_fetch_tiny()
4437
myheader("test_fetch_tiny");
4439
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4442
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 tinyint unsigned, \
4444
c3 tinyint unsigned, \
4448
c7 tinyint unsigned)");
4456
/* Test fetching of longlong to all types */
4458
static void test_fetch_bigint()
4462
myheader("test_fetch_bigint");
4464
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4467
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 bigint, \
4469
c3 bigint unsigned, \
4470
c4 bigint unsigned, \
4471
c5 bigint unsigned, \
4472
c6 bigint unsigned, \
4473
c7 bigint unsigned)");
4481
/* Test fetching of float to all types */
4483
static void test_fetch_float()
4487
myheader("test_fetch_float");
4489
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4492
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 float(3), \
4494
c3 float unsigned, \
4498
c7 float(10) unsigned)");
4506
/* Test fetching of double to all types */
4508
static void test_fetch_double()
4512
myheader("test_fetch_double");
4514
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4517
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 double(5, 2), "
4518
"c2 double unsigned, c3 double unsigned, "
4519
"c4 double unsigned, c5 double unsigned, "
4520
"c6 double unsigned, c7 double unsigned)");
4528
/* Test simple prepare with all possible types */
4530
static void test_prepare_ext()
4539
MYSQL_BIND my_bind[6];
4540
char query[MAX_TEST_QUERY_LENGTH];
4541
myheader("test_prepare_ext");
4543
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_ext");
4546
sql= (char *)"CREATE TABLE test_prepare_ext"
4556
" c9 double precision,"
4558
" c11 decimal(7, 4),"
4559
" c12 numeric(8, 4),"
4578
" c31 enum('one', 'two', 'three'),"
4579
" c32 set('monday', 'tuesday', 'wednesday'))";
4581
rc= mysql_query(mysql, sql);
4584
/* insert by prepare - all integers */
4585
strmov(query, (char *)"INSERT INTO test_prepare_ext(c1, c2, c3, c4, c5, c6) VALUES(?, ?, ?, ?, ?, ?)");
4586
stmt= mysql_simple_prepare(mysql, query);
4589
verify_param_count(stmt, 6);
4591
/* Always bzero all members of bind parameter */
4592
bzero((char*) my_bind, sizeof(my_bind));
4595
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
4596
my_bind[0].buffer= (void *)&tData;
4599
my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
4600
my_bind[1].buffer= (void *)&sData;
4603
my_bind[2].buffer_type= MYSQL_TYPE_LONG;
4604
my_bind[2].buffer= (void *)&nData;
4607
my_bind[3].buffer_type= MYSQL_TYPE_LONG;
4608
my_bind[3].buffer= (void *)&nData;
4611
my_bind[4].buffer_type= MYSQL_TYPE_LONG;
4612
my_bind[4].buffer= (void *)&nData;
4615
my_bind[5].buffer_type= MYSQL_TYPE_LONGLONG;
4616
my_bind[5].buffer= (void *)&bData;
4618
rc= mysql_stmt_bind_param(stmt, my_bind);
4619
check_execute(stmt, rc);
4622
* integer to integer
4624
for (nData= 0; nData<10; nData++, tData++, sData++, bData++)
4626
rc= mysql_stmt_execute(stmt);
4627
check_execute(stmt, rc);
4629
mysql_stmt_close(stmt);
4631
/* now fetch the results ..*/
4633
stmt= mysql_simple_prepare(mysql, "SELECT c1, c2, c3, c4, c5, c6 "
4634
"FROM test_prepare_ext");
4637
/* get the result */
4638
rc= mysql_stmt_execute(stmt);
4639
check_execute(stmt, rc);
4641
rc= my_process_stmt_result(stmt);
4642
DIE_UNLESS(nData == rc);
4644
mysql_stmt_close(stmt);
4648
/* Test real and alias names */
4650
static void test_field_names()
4655
myheader("test_field_names");
4658
fprintf(stdout, "\n %d, %d, %d", MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NEWDATE, MYSQL_TYPE_ENUM);
4659
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names1");
4662
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names2");
4665
rc= mysql_query(mysql, "CREATE TABLE test_field_names1(id int, name varchar(50))");
4668
rc= mysql_query(mysql, "CREATE TABLE test_field_names2(id int, name varchar(50))");
4671
/* with table name included with TRUE column name */
4672
rc= mysql_query(mysql, "SELECT id as 'id-alias' FROM test_field_names1");
4675
result= mysql_use_result(mysql);
4678
rc= my_process_result_set(result);
4679
DIE_UNLESS(rc == 0);
4680
mysql_free_result(result);
4682
/* with table name included with TRUE column name */
4683
rc= mysql_query(mysql, "SELECT t1.id as 'id-alias', test_field_names2.name FROM test_field_names1 t1, test_field_names2");
4686
result= mysql_use_result(mysql);
4689
rc= my_process_result_set(result);
4690
DIE_UNLESS(rc == 0);
4691
mysql_free_result(result);
4697
static void test_warnings()
4702
myheader("test_warnings");
4704
mysql_query(mysql, "DROP TABLE if exists test_non_exists");
4706
rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
4710
fprintf(stdout, "\n total warnings: %d", mysql_warning_count(mysql));
4711
rc= mysql_query(mysql, "SHOW WARNINGS");
4714
result= mysql_store_result(mysql);
4717
rc= my_process_result_set(result);
4718
DIE_UNLESS(rc == 1);
4719
mysql_free_result(result);
4725
static void test_errors()
4730
myheader("test_errors");
4732
mysql_query(mysql, "DROP TABLE if exists test_non_exists");
4734
rc= mysql_query(mysql, "DROP TABLE test_non_exists");
4737
rc= mysql_query(mysql, "SHOW ERRORS");
4740
result= mysql_store_result(mysql);
4743
(void) my_process_result_set(result);
4744
mysql_free_result(result);
4748
/* Test simple prepare-insert */
4750
static void test_insert()
4757
MYSQL_BIND my_bind[2];
4760
myheader("test_insert");
4762
rc= mysql_autocommit(mysql, TRUE);
4765
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
4768
rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
4769
col2 varchar(50))");
4772
/* insert by prepare */
4773
stmt= mysql_simple_prepare(mysql,
4774
"INSERT INTO test_prep_insert VALUES(?, ?)");
4777
verify_param_count(stmt, 2);
4780
We need to bzero bind structure because mysql_stmt_bind_param checks all
4783
bzero((char*) my_bind, sizeof(my_bind));
4786
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
4787
my_bind[0].buffer= (void *)&tiny_data;
4790
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
4791
my_bind[1].buffer= str_data;
4792
my_bind[1].buffer_length= sizeof(str_data);;
4793
my_bind[1].length= &length;
4795
rc= mysql_stmt_bind_param(stmt, my_bind);
4796
check_execute(stmt, rc);
4798
/* now, execute the prepared statement to insert 10 records.. */
4799
for (tiny_data= 0; tiny_data < 3; tiny_data++)
4801
length= my_sprintf(str_data, (str_data, "MySQL%d", tiny_data));
4802
rc= mysql_stmt_execute(stmt);
4803
check_execute(stmt, rc);
4806
mysql_stmt_close(stmt);
4808
/* now fetch the results ..*/
4809
rc= mysql_commit(mysql);
4812
/* test the results now, only one row should exist */
4813
rc= mysql_query(mysql, "SELECT * FROM test_prep_insert");
4816
/* get the result */
4817
result= mysql_store_result(mysql);
4820
rc= my_process_result_set(result);
4821
DIE_UNLESS((int) tiny_data == rc);
4822
mysql_free_result(result);
4827
/* Test simple prepare-resultset info */
4829
static void test_prepare_resultset()
4835
myheader("test_prepare_resultset");
4837
rc= mysql_autocommit(mysql, TRUE);
4840
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_resultset");
4843
rc= mysql_query(mysql, "CREATE TABLE test_prepare_resultset(id int, \
4844
name varchar(50), extra double)");
4847
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_prepare_resultset");
4850
verify_param_count(stmt, 0);
4852
result= mysql_stmt_result_metadata(stmt);
4854
my_print_result_metadata(result);
4855
mysql_free_result(result);
4856
mysql_stmt_close(stmt);
4860
/* Test field flags (verify .NET provider) */
4862
static void test_field_flags()
4870
myheader("test_field_flags");
4872
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_flags");
4875
rc= mysql_query(mysql, "CREATE TABLE test_field_flags(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, \
4884
/* with table name included with TRUE column name */
4885
rc= mysql_query(mysql, "SELECT * FROM test_field_flags");
4888
result= mysql_use_result(mysql);
4891
mysql_field_seek(result, 0);
4893
fputc('\n', stdout);
4895
for(i= 0; i< mysql_num_fields(result); i++)
4897
field= mysql_fetch_field(result);
4900
fprintf(stdout, "\n field:%d", i);
4901
if (field->flags & NOT_NULL_FLAG)
4902
fprintf(stdout, "\n NOT_NULL_FLAG");
4903
if (field->flags & PRI_KEY_FLAG)
4904
fprintf(stdout, "\n PRI_KEY_FLAG");
4905
if (field->flags & UNIQUE_KEY_FLAG)
4906
fprintf(stdout, "\n UNIQUE_KEY_FLAG");
4907
if (field->flags & MULTIPLE_KEY_FLAG)
4908
fprintf(stdout, "\n MULTIPLE_KEY_FLAG");
4909
if (field->flags & AUTO_INCREMENT_FLAG)
4910
fprintf(stdout, "\n AUTO_INCREMENT_FLAG");
4914
mysql_free_result(result);
4918
/* Test mysql_stmt_close for open stmts */
4920
static void test_stmt_close()
4923
MYSQL_STMT *stmt1, *stmt2, *stmt3, *stmt_x;
4924
MYSQL_BIND my_bind[1];
4928
char query[MAX_TEST_QUERY_LENGTH];
4930
myheader("test_stmt_close");
4933
fprintf(stdout, "\n Establishing a test connection ...");
4934
if (!(lmysql= mysql_init(NULL)))
4936
myerror("mysql_init() failed");
4939
if (!(mysql_real_connect(lmysql, opt_host, opt_user,
4940
opt_password, current_db, opt_port,
4941
opt_unix_socket, 0)))
4943
myerror("connection failed");
4946
lmysql->reconnect= 1;
4948
fprintf(stdout, "OK");
4951
/* set AUTOCOMMIT to ON*/
4952
mysql_autocommit(lmysql, TRUE);
4954
rc= mysql_query(lmysql, "DROP TABLE IF EXISTS test_stmt_close");
4957
rc= mysql_query(lmysql, "CREATE TABLE test_stmt_close(id int)");
4960
strmov(query, "DO \"nothing\"");
4961
stmt1= mysql_simple_prepare(lmysql, query);
4964
verify_param_count(stmt1, 0);
4966
strmov(query, "INSERT INTO test_stmt_close(id) VALUES(?)");
4967
stmt_x= mysql_simple_prepare(mysql, query);
4970
verify_param_count(stmt_x, 1);
4972
strmov(query, "UPDATE test_stmt_close SET id= ? WHERE id= ?");
4973
stmt3= mysql_simple_prepare(lmysql, query);
4976
verify_param_count(stmt3, 2);
4978
strmov(query, "SELECT * FROM test_stmt_close WHERE id= ?");
4979
stmt2= mysql_simple_prepare(lmysql, query);
4982
verify_param_count(stmt2, 1);
4984
rc= mysql_stmt_close(stmt1);
4986
fprintf(stdout, "\n mysql_close_stmt(1) returned: %d", rc);
4987
DIE_UNLESS(rc == 0);
4990
Originally we were going to close all statements automatically in
4991
mysql_close(). This proved to not work well - users weren't able to
4992
close statements by hand once mysql_close() had been called.
4993
Now mysql_close() doesn't free any statements, so this test doesn't
4994
serve its original designation any more.
4995
Here we free stmt2 and stmt3 by hand to avoid memory leaks.
4997
mysql_stmt_close(stmt2);
4998
mysql_stmt_close(stmt3);
4999
mysql_close(lmysql);
5002
We need to bzero bind structure because mysql_stmt_bind_param checks all
5005
bzero((char*) my_bind, sizeof(my_bind));
5007
my_bind[0].buffer= (void *)&count;
5008
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
5011
rc= mysql_stmt_bind_param(stmt_x, my_bind);
5012
check_execute(stmt_x, rc);
5014
rc= mysql_stmt_execute(stmt_x);
5015
check_execute(stmt_x, rc);
5017
verify_st_affected_rows(stmt_x, 1);
5019
rc= mysql_stmt_close(stmt_x);
5021
fprintf(stdout, "\n mysql_close_stmt(x) returned: %d", rc);
5022
DIE_UNLESS( rc == 0);
5024
rc= mysql_query(mysql, "SELECT id FROM test_stmt_close");
5027
result= mysql_store_result(mysql);
5030
rc= my_process_result_set(result);
5031
DIE_UNLESS(rc == 1);
5032
mysql_free_result(result);
5036
/* Test simple set-variable prepare */
5038
static void test_set_variable()
5040
MYSQL_STMT *stmt, *stmt1;
5042
int set_count, def_count, get_count;
5044
char var[NAME_LEN+1];
5045
MYSQL_BIND set_bind[1], get_bind[2];
5047
myheader("test_set_variable");
5049
mysql_autocommit(mysql, TRUE);
5051
stmt1= mysql_simple_prepare(mysql, "show variables like 'max_error_count'");
5055
We need to bzero bind structure because mysql_stmt_bind_param checks all
5058
bzero((char*) get_bind, sizeof(get_bind));
5060
get_bind[0].buffer_type= MYSQL_TYPE_STRING;
5061
get_bind[0].buffer= (void *)var;
5062
get_bind[0].length= &length;
5063
get_bind[0].buffer_length= (int)NAME_LEN;
5066
get_bind[1].buffer_type= MYSQL_TYPE_LONG;
5067
get_bind[1].buffer= (void *)&get_count;
5069
rc= mysql_stmt_execute(stmt1);
5070
check_execute(stmt1, rc);
5072
rc= mysql_stmt_bind_result(stmt1, get_bind);
5073
check_execute(stmt1, rc);
5075
rc= mysql_stmt_fetch(stmt1);
5076
check_execute(stmt1, rc);
5079
fprintf(stdout, "\n max_error_count(default): %d", get_count);
5080
def_count= get_count;
5082
DIE_UNLESS(strcmp(var, "max_error_count") == 0);
5083
rc= mysql_stmt_fetch(stmt1);
5084
DIE_UNLESS(rc == MYSQL_NO_DATA);
5086
stmt= mysql_simple_prepare(mysql, "set max_error_count= ?");
5089
bzero((char*) set_bind, sizeof(set_bind));
5091
set_bind[0].buffer_type= MYSQL_TYPE_LONG;
5092
set_bind[0].buffer= (void *)&set_count;
5094
rc= mysql_stmt_bind_param(stmt, set_bind);
5095
check_execute(stmt, rc);
5098
rc= mysql_stmt_execute(stmt);
5099
check_execute(stmt, rc);
5101
mysql_commit(mysql);
5103
rc= mysql_stmt_execute(stmt1);
5104
check_execute(stmt1, rc);
5106
rc= mysql_stmt_fetch(stmt1);
5107
check_execute(stmt1, rc);
5110
fprintf(stdout, "\n max_error_count : %d", get_count);
5111
DIE_UNLESS(get_count == set_count);
5113
rc= mysql_stmt_fetch(stmt1);
5114
DIE_UNLESS(rc == MYSQL_NO_DATA);
5116
/* restore back to default */
5117
set_count= def_count;
5118
rc= mysql_stmt_execute(stmt);
5119
check_execute(stmt, rc);
5121
rc= mysql_stmt_execute(stmt1);
5122
check_execute(stmt1, rc);
5124
rc= mysql_stmt_fetch(stmt1);
5125
check_execute(stmt1, rc);
5128
fprintf(stdout, "\n max_error_count(default): %d", get_count);
5129
DIE_UNLESS(get_count == set_count);
5131
rc= mysql_stmt_fetch(stmt1);
5132
DIE_UNLESS(rc == MYSQL_NO_DATA);
5134
mysql_stmt_close(stmt);
5135
mysql_stmt_close(stmt1);
5140
/* Insert meta info .. */
5142
static void test_insert_meta()
5149
myheader("test_insert_meta");
5151
rc= mysql_autocommit(mysql, TRUE);
5154
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
5157
rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
5158
col2 varchar(50), col3 varchar(30))");
5161
strmov(query, "INSERT INTO test_prep_insert VALUES(10, 'venu1', 'test')");
5162
stmt= mysql_simple_prepare(mysql, query);
5165
verify_param_count(stmt, 0);
5167
result= mysql_param_result(stmt);
5170
mysql_stmt_close(stmt);
5172
strmov(query, "INSERT INTO test_prep_insert VALUES(?, 'venu', ?)");
5173
stmt= mysql_simple_prepare(mysql, query);
5176
verify_param_count(stmt, 2);
5178
result= mysql_param_result(stmt);
5181
my_print_result_metadata(result);
5183
mysql_field_seek(result, 0);
5184
field= mysql_fetch_field(result);
5187
fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col1");
5188
DIE_UNLESS(strcmp(field->name, "col1") == 0);
5190
field= mysql_fetch_field(result);
5193
fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col3");
5194
DIE_UNLESS(strcmp(field->name, "col3") == 0);
5196
field= mysql_fetch_field(result);
5199
mysql_free_result(result);
5200
mysql_stmt_close(stmt);
5204
/* Update meta info .. */
5206
static void test_update_meta()
5213
myheader("test_update_meta");
5215
rc= mysql_autocommit(mysql, TRUE);
5218
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_update");
5221
rc= mysql_query(mysql, "CREATE TABLE test_prep_update(col1 tinyint, \
5222
col2 varchar(50), col3 varchar(30))");
5225
strmov(query, "UPDATE test_prep_update SET col1=10, col2='venu1' WHERE col3='test'");
5226
stmt= mysql_simple_prepare(mysql, query);
5229
verify_param_count(stmt, 0);
5231
result= mysql_param_result(stmt);
5234
mysql_stmt_close(stmt);
5236
strmov(query, "UPDATE test_prep_update SET col1=?, col2='venu' WHERE col3=?");
5237
stmt= mysql_simple_prepare(mysql, query);
5240
verify_param_count(stmt, 2);
5242
result= mysql_param_result(stmt);
5245
my_print_result_metadata(result);
5247
mysql_field_seek(result, 0);
5248
field= mysql_fetch_field(result);
5252
fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
5253
fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
5255
DIE_UNLESS(strcmp(field->name, "col1") == 0);
5256
DIE_UNLESS(strcmp(field->table, "test_prep_update") == 0);
5258
field= mysql_fetch_field(result);
5262
fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col3");
5263
fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
5265
DIE_UNLESS(strcmp(field->name, "col3") == 0);
5266
DIE_UNLESS(strcmp(field->table, "test_prep_update") == 0);
5268
field= mysql_fetch_field(result);
5271
mysql_free_result(result);
5272
mysql_stmt_close(stmt);
5276
/* Select meta info .. */
5278
static void test_select_meta()
5285
myheader("test_select_meta");
5287
rc= mysql_autocommit(mysql, TRUE);
5290
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_select");
5293
rc= mysql_query(mysql, "CREATE TABLE test_prep_select(col1 tinyint, \
5294
col2 varchar(50), col3 varchar(30))");
5297
strmov(query, "SELECT * FROM test_prep_select WHERE col1=10");
5298
stmt= mysql_simple_prepare(mysql, query);
5301
verify_param_count(stmt, 0);
5303
result= mysql_param_result(stmt);
5306
strmov(query, "SELECT col1, col3 from test_prep_select WHERE col1=? AND col3='test' AND col2= ?");
5307
stmt= mysql_simple_prepare(mysql, query);
5310
verify_param_count(stmt, 2);
5312
result= mysql_param_result(stmt);
5315
my_print_result_metadata(result);
5317
mysql_field_seek(result, 0);
5318
field= mysql_fetch_field(result);
5322
fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
5323
fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
5325
DIE_UNLESS(strcmp(field->name, "col1") == 0);
5326
DIE_UNLESS(strcmp(field->table, "test_prep_select") == 0);
5328
field= mysql_fetch_field(result);
5332
fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col2");
5333
fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
5335
DIE_UNLESS(strcmp(field->name, "col2") == 0);
5336
DIE_UNLESS(strcmp(field->table, "test_prep_select") == 0);
5338
field= mysql_fetch_field(result);
5341
mysql_free_result(result);
5342
mysql_stmt_close(stmt);
5347
/* Test FUNCTION field info / DATE_FORMAT() table_name . */
5349
static void test_func_fields()
5355
myheader("test_func_fields");
5357
rc= mysql_autocommit(mysql, TRUE);
5360
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_dateformat");
5363
rc= mysql_query(mysql, "CREATE TABLE test_dateformat(id int, \
5367
rc= mysql_query(mysql, "INSERT INTO test_dateformat(id) values(10)");
5370
rc= mysql_query(mysql, "SELECT ts FROM test_dateformat");
5373
result= mysql_store_result(mysql);
5376
field= mysql_fetch_field(result);
5379
fprintf(stdout, "\n table name: `%s` (expected: `%s`)", field->table,
5381
DIE_UNLESS(strcmp(field->table, "test_dateformat") == 0);
5383
field= mysql_fetch_field(result);
5384
mytest_r(field); /* no more fields */
5386
mysql_free_result(result);
5389
rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'venu' FROM test_dateformat");
5392
result= mysql_store_result(mysql);
5395
field= mysql_fetch_field(result);
5398
fprintf(stdout, "\n table name: `%s` (expected: `%s`)", field->table, "");
5399
DIE_UNLESS(field->table[0] == '\0');
5401
field= mysql_fetch_field(result);
5402
mytest_r(field); /* no more fields */
5404
mysql_free_result(result);
5406
/* FIELD ALIAS TEST */
5407
rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'YEAR' FROM test_dateformat");
5410
result= mysql_store_result(mysql);
5413
field= mysql_fetch_field(result);
5417
printf("\n field name: `%s` (expected: `%s`)", field->name, "YEAR");
5418
printf("\n field org name: `%s` (expected: `%s`)", field->org_name, "");
5420
DIE_UNLESS(strcmp(field->name, "YEAR") == 0);
5421
DIE_UNLESS(field->org_name[0] == '\0');
5423
field= mysql_fetch_field(result);
5424
mytest_r(field); /* no more fields */
5426
mysql_free_result(result);
5430
/* Multiple stmts .. */
5432
static void test_multi_stmt()
5435
MYSQL_STMT *stmt, *stmt1, *stmt2;
5439
MYSQL_BIND my_bind[2];
5442
myheader("test_multi_stmt");
5444
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_multi_table");
5447
rc= mysql_query(mysql, "CREATE TABLE test_multi_table(id int, name char(20))");
5450
rc= mysql_query(mysql, "INSERT INTO test_multi_table values(10, 'mysql')");
5453
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_multi_table "
5457
stmt2= mysql_simple_prepare(mysql, "UPDATE test_multi_table "
5458
"SET name='updated' WHERE id=10");
5461
verify_param_count(stmt, 1);
5464
We need to bzero bind structure because mysql_stmt_bind_param checks all
5467
bzero((char*) my_bind, sizeof(my_bind));
5469
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
5470
my_bind[0].buffer= (void *)&id;
5471
my_bind[0].is_null= &is_null[0];
5472
my_bind[0].length= &length[0];
5476
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
5477
my_bind[1].buffer= (void *)name;
5478
my_bind[1].buffer_length= sizeof(name);
5479
my_bind[1].length= &length[1];
5480
my_bind[1].is_null= &is_null[1];
5482
rc= mysql_stmt_bind_param(stmt, my_bind);
5483
check_execute(stmt, rc);
5485
rc= mysql_stmt_bind_result(stmt, my_bind);
5486
check_execute(stmt, rc);
5489
rc= mysql_stmt_execute(stmt);
5490
check_execute(stmt, rc);
5493
rc= mysql_stmt_fetch(stmt);
5494
check_execute(stmt, rc);
5498
fprintf(stdout, "\n int_data: %lu(%lu)", (ulong) id, length[0]);
5499
fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
5501
DIE_UNLESS(id == 10);
5502
DIE_UNLESS(strcmp(name, "mysql") == 0);
5504
rc= mysql_stmt_fetch(stmt);
5505
DIE_UNLESS(rc == MYSQL_NO_DATA);
5507
/* alter the table schema now */
5508
stmt1= mysql_simple_prepare(mysql, "DELETE FROM test_multi_table "
5510
"CONVERT(name USING utf8)=?");
5513
verify_param_count(stmt1, 2);
5515
rc= mysql_stmt_bind_param(stmt1, my_bind);
5516
check_execute(stmt1, rc);
5518
rc= mysql_stmt_execute(stmt2);
5519
check_execute(stmt2, rc);
5521
verify_st_affected_rows(stmt2, 1);
5523
rc= mysql_stmt_execute(stmt);
5524
check_execute(stmt, rc);
5526
rc= mysql_stmt_fetch(stmt);
5527
check_execute(stmt, rc);
5531
fprintf(stdout, "\n int_data: %lu(%lu)", (ulong) id, length[0]);
5532
fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
5534
DIE_UNLESS(id == 10);
5535
DIE_UNLESS(strcmp(name, "updated") == 0);
5537
rc= mysql_stmt_fetch(stmt);
5538
DIE_UNLESS(rc == MYSQL_NO_DATA);
5540
rc= mysql_stmt_execute(stmt1);
5541
check_execute(stmt1, rc);
5543
verify_st_affected_rows(stmt1, 1);
5545
mysql_stmt_close(stmt1);
5547
rc= mysql_stmt_execute(stmt);
5548
check_execute(stmt, rc);
5550
rc= mysql_stmt_fetch(stmt);
5551
DIE_UNLESS(rc == MYSQL_NO_DATA);
5553
rc= my_stmt_result("SELECT * FROM test_multi_table");
5554
DIE_UNLESS(rc == 0);
5556
mysql_stmt_close(stmt);
5557
mysql_stmt_close(stmt2);
5562
/* Test simple sample - manual */
5564
static void test_manual_sample()
5566
unsigned int param_count;
5572
ulonglong affected_rows;
5573
MYSQL_BIND my_bind[3];
5575
char query[MAX_TEST_QUERY_LENGTH];
5577
myheader("test_manual_sample");
5580
Sample which is incorporated directly in the manual under Prepared
5581
statements section (Example from mysql_stmt_execute()
5584
mysql_autocommit(mysql, 1);
5585
if (mysql_query(mysql, "DROP TABLE IF EXISTS test_table"))
5587
fprintf(stderr, "\n drop table failed");
5588
fprintf(stderr, "\n %s", mysql_error(mysql));
5591
if (mysql_query(mysql, "CREATE TABLE test_table(col1 int, col2 varchar(50), \
5595
fprintf(stderr, "\n create table failed");
5596
fprintf(stderr, "\n %s", mysql_error(mysql));
5600
/* Prepare a insert query with 3 parameters */
5601
strmov(query, "INSERT INTO test_table(col1, col2, col3) values(?, ?, ?)");
5602
if (!(stmt= mysql_simple_prepare(mysql, query)))
5604
fprintf(stderr, "\n prepare, insert failed");
5605
fprintf(stderr, "\n %s", mysql_error(mysql));
5609
fprintf(stdout, "\n prepare, insert successful");
5611
/* Get the parameter count from the statement */
5612
param_count= mysql_stmt_param_count(stmt);
5615
fprintf(stdout, "\n total parameters in insert: %d", param_count);
5616
if (param_count != 3) /* validate parameter count */
5618
fprintf(stderr, "\n invalid parameter count returned by MySQL");
5622
/* Bind the data for the parameters */
5625
We need to bzero bind structure because mysql_stmt_bind_param checks all
5628
bzero((char*) my_bind, sizeof(my_bind));
5631
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
5632
my_bind[0].buffer= (void *)&int_data;
5635
my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
5636
my_bind[1].buffer= (void *)str_data;
5637
my_bind[1].buffer_length= sizeof(str_data);
5640
my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
5641
my_bind[2].buffer= (void *)&small_data;
5642
my_bind[2].is_null= &is_null;
5645
/* Bind the buffers */
5646
if (mysql_stmt_bind_param(stmt, my_bind))
5648
fprintf(stderr, "\n param bind failed");
5649
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
5653
/* Specify the data */
5654
int_data= 10; /* integer */
5655
strmov(str_data, "MySQL"); /* string */
5657
/* INSERT SMALLINT data as NULL */
5660
/* Execute the insert statement - 1*/
5661
if (mysql_stmt_execute(stmt))
5663
fprintf(stderr, "\n execute 1 failed");
5664
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
5668
/* Get the total rows affected */
5669
affected_rows= mysql_stmt_affected_rows(stmt);
5672
fprintf(stdout, "\n total affected rows: %ld", (ulong) affected_rows);
5673
if (affected_rows != 1) /* validate affected rows */
5675
fprintf(stderr, "\n invalid affected rows by MySQL");
5679
/* Re-execute the insert, by changing the values */
5681
strmov(str_data, "The most popular open source database");
5682
small_data= 1000; /* smallint */
5683
is_null= 0; /* reset */
5685
/* Execute the insert statement - 2*/
5686
if (mysql_stmt_execute(stmt))
5688
fprintf(stderr, "\n execute 2 failed");
5689
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
5693
/* Get the total rows affected */
5694
affected_rows= mysql_stmt_affected_rows(stmt);
5697
fprintf(stdout, "\n total affected rows: %ld", (ulong) affected_rows);
5698
if (affected_rows != 1) /* validate affected rows */
5700
fprintf(stderr, "\n invalid affected rows by MySQL");
5704
/* Close the statement */
5705
if (mysql_stmt_close(stmt))
5707
fprintf(stderr, "\n failed while closing the statement");
5708
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
5711
rc= my_stmt_result("SELECT * FROM test_table");
5712
DIE_UNLESS(rc == 2);
5714
/* DROP THE TABLE */
5715
if (mysql_query(mysql, "DROP TABLE test_table"))
5717
fprintf(stderr, "\n drop table failed");
5718
fprintf(stderr, "\n %s", mysql_error(mysql));
5722
fprintf(stdout, "Success !!!");
5726
/* Test alter table scenario in the middle of prepare */
5728
static void test_prepare_alter()
5732
MYSQL_BIND my_bind[1];
5735
myheader("test_prepare_alter");
5737
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_alter");
5740
rc= mysql_query(mysql, "CREATE TABLE test_prep_alter(id int, name char(20))");
5743
rc= mysql_query(mysql, "INSERT INTO test_prep_alter values(10, 'venu'), (20, 'mysql')");
5746
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_prep_alter VALUES(?, 'monty')");
5749
verify_param_count(stmt, 1);
5752
We need to bzero bind structure because mysql_stmt_bind_param checks all
5755
bzero((char*) my_bind, sizeof(my_bind));
5758
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
5759
my_bind[0].buffer= (void *)&id;
5760
my_bind[0].is_null= &is_null;
5762
rc= mysql_stmt_bind_param(stmt, my_bind);
5763
check_execute(stmt, rc);
5766
rc= mysql_stmt_execute(stmt);
5767
check_execute(stmt, rc);
5769
if (thread_query((char *)"ALTER TABLE test_prep_alter change id id_new varchar(20)"))
5773
rc= mysql_stmt_execute(stmt);
5774
check_execute(stmt, rc);
5776
rc= my_stmt_result("SELECT * FROM test_prep_alter");
5777
DIE_UNLESS(rc == 4);
5779
mysql_stmt_close(stmt);
5783
/* Test the support of multi-statement executions */
5785
static void test_multi_statements()
5791
const char *query= "\
5792
DROP TABLE IF EXISTS test_multi_tab;\
5793
CREATE TABLE test_multi_tab(id int, name char(20));\
5794
INSERT INTO test_multi_tab(id) VALUES(10), (20);\
5795
INSERT INTO test_multi_tab VALUES(20, 'insert;comma');\
5796
SELECT * FROM test_multi_tab;\
5797
UPDATE test_multi_tab SET name='new;name' WHERE id=20;\
5798
DELETE FROM test_multi_tab WHERE name='new;name';\
5799
SELECT * FROM test_multi_tab;\
5800
DELETE FROM test_multi_tab WHERE id=10;\
5801
SELECT * FROM test_multi_tab;\
5802
DROP TABLE test_multi_tab;\
5804
DROP TABLE IF EXISTS test_multi_tab";
5805
uint count, exp_value;
5806
uint rows[]= {0, 0, 2, 1, 3, 2, 2, 1, 1, 0, 0, 1, 0};
5808
myheader("test_multi_statements");
5811
First test that we get an error for multi statements
5812
(Because default connection is not opened with CLIENT_MULTI_STATEMENTS)
5814
rc= mysql_query(mysql, query); /* syntax error */
5817
rc= mysql_next_result(mysql);
5818
DIE_UNLESS(rc == -1);
5819
rc= mysql_more_results(mysql);
5820
DIE_UNLESS(rc == 0);
5822
if (!(mysql_local= mysql_init(NULL)))
5824
fprintf(stdout, "\n mysql_init() failed");
5828
/* Create connection that supports multi statements */
5829
if (!(mysql_real_connect(mysql_local, opt_host, opt_user,
5830
opt_password, current_db, opt_port,
5831
opt_unix_socket, CLIENT_MULTI_STATEMENTS)))
5833
fprintf(stdout, "\n connection failed(%s)", mysql_error(mysql_local));
5836
mysql_local->reconnect= 1;
5838
rc= mysql_query(mysql_local, query);
5841
for (count= 0 ; count < array_elements(rows) ; count++)
5844
fprintf(stdout, "\n Query %d: ", count);
5845
if ((result= mysql_store_result(mysql_local)))
5847
(void) my_process_result_set(result);
5848
mysql_free_result(result);
5850
else if (!opt_silent)
5851
fprintf(stdout, "OK, %ld row(s) affected, %ld warning(s)\n",
5852
(ulong) mysql_affected_rows(mysql_local),
5853
(ulong) mysql_warning_count(mysql_local));
5855
exp_value= (uint) mysql_affected_rows(mysql_local);
5856
if (rows[count] != exp_value)
5858
fprintf(stderr, "row %d had affected rows: %d, should be %d\n",
5859
count, exp_value, rows[count]);
5862
if (count != array_elements(rows) -1)
5864
if (!(rc= mysql_more_results(mysql_local)))
5867
"mysql_more_result returned wrong value: %d for row %d\n",
5871
if ((rc= mysql_next_result(mysql_local)))
5873
exp_value= mysql_errno(mysql_local);
5880
rc= mysql_more_results(mysql_local);
5881
DIE_UNLESS(rc == 0);
5882
rc= mysql_next_result(mysql_local);
5883
DIE_UNLESS(rc == -1);
5887
/* check that errors abort multi statements */
5889
rc= mysql_query(mysql_local, "select 1+1+a;select 1+1");
5891
rc= mysql_more_results(mysql_local);
5892
DIE_UNLESS(rc == 0);
5893
rc= mysql_next_result(mysql_local);
5894
DIE_UNLESS(rc == -1);
5896
rc= mysql_query(mysql_local, "select 1+1;select 1+1+a;select 1");
5898
result= mysql_store_result(mysql_local);
5900
mysql_free_result(result);
5901
rc= mysql_more_results(mysql_local);
5902
DIE_UNLESS(rc == 1);
5903
rc= mysql_next_result(mysql_local);
5907
Ensure that we can now do a simple query (this checks that the server is
5908
not trying to send us the results for the last 'select 1'
5910
rc= mysql_query(mysql_local, "select 1+1+1");
5912
result= mysql_store_result(mysql_local);
5914
(void) my_process_result_set(result);
5915
mysql_free_result(result);
5918
Check if errors in one of the queries handled properly.
5920
rc= mysql_query(mysql_local, "select 1; select * from not_existing_table");
5922
result= mysql_store_result(mysql_local);
5923
mysql_free_result(result);
5925
rc= mysql_next_result(mysql_local);
5928
rc= mysql_next_result(mysql_local);
5931
mysql_close(mysql_local);
5936
Check that Prepared statement cannot contain several
5940
static void test_prepare_multi_statements()
5944
char query[MAX_TEST_QUERY_LENGTH];
5945
myheader("test_prepare_multi_statements");
5947
if (!(mysql_local= mysql_init(NULL)))
5949
fprintf(stderr, "\n mysql_init() failed");
5953
if (!(mysql_real_connect(mysql_local, opt_host, opt_user,
5954
opt_password, current_db, opt_port,
5955
opt_unix_socket, CLIENT_MULTI_STATEMENTS)))
5957
fprintf(stderr, "\n connection failed(%s)", mysql_error(mysql_local));
5960
mysql_local->reconnect= 1;
5961
strmov(query, "select 1; select 'another value'");
5962
stmt= mysql_simple_prepare(mysql_local, query);
5964
mysql_close(mysql_local);
5968
/* Test simple bind store result */
5970
static void test_store_result()
5976
MYSQL_BIND my_bind[2];
5977
ulong length, length1;
5980
myheader("test_store_result");
5982
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
5985
rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
5988
rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
5991
rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
5994
rc= mysql_commit(mysql);
5998
bzero((char*) my_bind, sizeof(my_bind));
5999
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
6000
my_bind[0].buffer= (void *) &nData; /* integer data */
6001
my_bind[0].length= &length;
6002
my_bind[0].is_null= &is_null[0];
6005
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
6006
my_bind[1].buffer= szData; /* string data */
6007
my_bind[1].buffer_length= sizeof(szData);
6008
my_bind[1].length= &length1;
6009
my_bind[1].is_null= &is_null[1];
6012
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_store_result");
6015
rc= mysql_stmt_bind_result(stmt, my_bind);
6016
check_execute(stmt, rc);
6018
rc= mysql_stmt_execute(stmt);
6019
check_execute(stmt, rc);
6021
rc= mysql_stmt_store_result(stmt);
6022
check_execute(stmt, rc);
6024
rc= mysql_stmt_fetch(stmt);
6025
check_execute(stmt, rc);
6028
fprintf(stdout, "\n row 1: %ld, %s(%lu)", (long) nData, szData, length1);
6029
DIE_UNLESS(nData == 10);
6030
DIE_UNLESS(strcmp(szData, "venu") == 0);
6031
DIE_UNLESS(length1 == 4);
6033
rc= mysql_stmt_fetch(stmt);
6034
check_execute(stmt, rc);
6037
fprintf(stdout, "\n row 2: %ld, %s(%lu)", (long) nData, szData, length1);
6038
DIE_UNLESS(nData == 20);
6039
DIE_UNLESS(strcmp(szData, "mysql") == 0);
6040
DIE_UNLESS(length1 == 5);
6043
rc= mysql_stmt_fetch(stmt);
6044
check_execute(stmt, rc);
6046
if (!opt_silent && is_null[0])
6047
fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
6048
DIE_UNLESS(is_null[0]);
6049
DIE_UNLESS(strcmp(szData, "monty") == 0);
6050
DIE_UNLESS(length1 == 5);
6052
rc= mysql_stmt_fetch(stmt);
6053
DIE_UNLESS(rc == MYSQL_NO_DATA);
6055
rc= mysql_stmt_execute(stmt);
6056
check_execute(stmt, rc);
6058
rc= mysql_stmt_store_result(stmt);
6059
check_execute(stmt, rc);
6061
rc= mysql_stmt_fetch(stmt);
6062
check_execute(stmt, rc);
6065
fprintf(stdout, "\n row 1: %ld, %s(%lu)", (long) nData, szData, length1);
6066
DIE_UNLESS(nData == 10);
6067
DIE_UNLESS(strcmp(szData, "venu") == 0);
6068
DIE_UNLESS(length1 == 4);
6070
rc= mysql_stmt_fetch(stmt);
6071
check_execute(stmt, rc);
6074
fprintf(stdout, "\n row 2: %ld, %s(%lu)", (long) nData, szData, length1);
6075
DIE_UNLESS(nData == 20);
6076
DIE_UNLESS(strcmp(szData, "mysql") == 0);
6077
DIE_UNLESS(length1 == 5);
6080
rc= mysql_stmt_fetch(stmt);
6081
check_execute(stmt, rc);
6083
if (!opt_silent && is_null[0])
6084
fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
6085
DIE_UNLESS(is_null[0]);
6086
DIE_UNLESS(strcmp(szData, "monty") == 0);
6087
DIE_UNLESS(length1 == 5);
6089
rc= mysql_stmt_fetch(stmt);
6090
DIE_UNLESS(rc == MYSQL_NO_DATA);
6092
mysql_stmt_close(stmt);
6096
/* Test simple bind store result */
6098
static void test_store_result1()
6103
myheader("test_store_result1");
6105
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
6108
rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
6111
rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
6114
rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
6117
rc= mysql_commit(mysql);
6120
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_store_result");
6123
rc= mysql_stmt_execute(stmt);
6124
check_execute(stmt, rc);
6126
rc= mysql_stmt_store_result(stmt);
6127
check_execute(stmt, rc);
6130
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
6133
fprintf(stdout, "\n total rows: %d", rc);
6134
DIE_UNLESS(rc == 3);
6136
rc= mysql_stmt_execute(stmt);
6137
check_execute(stmt, rc);
6139
rc= mysql_stmt_store_result(stmt);
6140
check_execute(stmt, rc);
6143
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
6146
fprintf(stdout, "\n total rows: %d", rc);
6147
DIE_UNLESS(rc == 3);
6149
mysql_stmt_close(stmt);
6153
/* Another test for bind and store result */
6155
static void test_store_result2()
6161
MYSQL_BIND my_bind[1];
6162
char query[MAX_TEST_QUERY_LENGTH];
6164
myheader("test_store_result2");
6166
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
6169
rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
6172
rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
6175
rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
6178
rc= mysql_commit(mysql);
6182
We need to bzero bind structure because mysql_stmt_bind_param checks all
6185
bzero((char*) my_bind, sizeof(my_bind));
6187
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
6188
my_bind[0].buffer= (void *) &nData; /* integer data */
6189
my_bind[0].length= &length;
6190
my_bind[0].is_null= 0;
6192
strmov((char *)query , "SELECT col1 FROM test_store_result where col1= ?");
6193
stmt= mysql_simple_prepare(mysql, query);
6196
rc= mysql_stmt_bind_param(stmt, my_bind);
6197
check_execute(stmt, rc);
6199
rc= mysql_stmt_bind_result(stmt, my_bind);
6200
check_execute(stmt, rc);
6202
nData= 10; length= 0;
6203
rc= mysql_stmt_execute(stmt);
6204
check_execute(stmt, rc);
6207
rc= mysql_stmt_store_result(stmt);
6208
check_execute(stmt, rc);
6210
rc= mysql_stmt_fetch(stmt);
6211
check_execute(stmt, rc);
6214
fprintf(stdout, "\n row 1: %d", nData);
6215
DIE_UNLESS(nData == 10);
6217
rc= mysql_stmt_fetch(stmt);
6218
DIE_UNLESS(rc == MYSQL_NO_DATA);
6221
rc= mysql_stmt_execute(stmt);
6222
check_execute(stmt, rc);
6225
rc= mysql_stmt_store_result(stmt);
6226
check_execute(stmt, rc);
6228
rc= mysql_stmt_fetch(stmt);
6229
check_execute(stmt, rc);
6232
fprintf(stdout, "\n row 1: %d", nData);
6233
DIE_UNLESS(nData == 20);
6235
rc= mysql_stmt_fetch(stmt);
6236
DIE_UNLESS(rc == MYSQL_NO_DATA);
6237
mysql_stmt_close(stmt);
6241
/* Test simple subselect prepare */
6243
static void test_subselect()
6248
MYSQL_BIND my_bind[1];
6249
DBUG_ENTER("test_subselect");
6251
myheader("test_subselect");
6253
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sub1");
6256
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sub2");
6259
rc= mysql_query(mysql, "CREATE TABLE test_sub1(id int)");
6262
rc= mysql_query(mysql, "CREATE TABLE test_sub2(id int, id1 int)");
6265
rc= mysql_query(mysql, "INSERT INTO test_sub1 values(2)");
6268
rc= mysql_query(mysql, "INSERT INTO test_sub2 VALUES(1, 7), (2, 7)");
6271
rc= mysql_commit(mysql);
6276
We need to bzero bind structure because mysql_stmt_bind_param checks all
6279
bzero((char*) my_bind, sizeof(my_bind));
6281
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
6282
my_bind[0].buffer= (void *) &id;
6283
my_bind[0].length= 0;
6284
my_bind[0].is_null= 0;
6286
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_sub2(id) SELECT * FROM test_sub1 WHERE id= ?");
6289
rc= mysql_stmt_bind_param(stmt, my_bind);
6290
check_execute(stmt, rc);
6293
rc= mysql_stmt_execute(stmt);
6294
check_execute(stmt, rc);
6296
verify_st_affected_rows(stmt, 1);
6299
rc= mysql_stmt_execute(stmt);
6300
check_execute(stmt, rc);
6302
verify_st_affected_rows(stmt, 0);
6304
mysql_stmt_close(stmt);
6306
rc= my_stmt_result("SELECT * FROM test_sub2");
6307
DIE_UNLESS(rc == 3);
6309
rc= my_stmt_result("SELECT ROW(1, 7) IN (select id, id1 "
6310
"from test_sub2 WHERE id1= 8)");
6311
DIE_UNLESS(rc == 1);
6312
rc= my_stmt_result("SELECT ROW(1, 7) IN (select id, id1 "
6313
"from test_sub2 WHERE id1= 7)");
6314
DIE_UNLESS(rc == 1);
6316
stmt= mysql_simple_prepare(mysql, ("SELECT ROW(1, 7) IN (select id, id1 "
6317
"from test_sub2 WHERE id1= ?)"));
6320
rc= mysql_stmt_bind_param(stmt, my_bind);
6321
check_execute(stmt, rc);
6323
rc= mysql_stmt_bind_result(stmt, my_bind);
6324
check_execute(stmt, rc);
6327
rc= mysql_stmt_execute(stmt);
6328
check_execute(stmt, rc);
6330
rc= mysql_stmt_fetch(stmt);
6331
check_execute(stmt, rc);
6334
fprintf(stdout, "\n row 1: %d", id);
6335
DIE_UNLESS(id == 1);
6337
rc= mysql_stmt_fetch(stmt);
6338
DIE_UNLESS(rc == MYSQL_NO_DATA);
6341
rc= mysql_stmt_execute(stmt);
6342
check_execute(stmt, rc);
6344
rc= mysql_stmt_fetch(stmt);
6345
check_execute(stmt, rc);
6348
fprintf(stdout, "\n row 1: %d", id);
6349
DIE_UNLESS(id == 0);
6351
rc= mysql_stmt_fetch(stmt);
6352
DIE_UNLESS(rc == MYSQL_NO_DATA);
6354
mysql_stmt_close(stmt);
6360
Generalized conversion routine to handle DATE, TIME and DATETIME
6361
conversion using MYSQL_TIME structure
6364
static void test_bind_date_conv(uint row_count)
6366
MYSQL_STMT *stmt= 0;
6367
uint rc, i, count= row_count;
6369
MYSQL_BIND my_bind[4];
6370
my_bool is_null[4]= {0};
6373
uint year, month, day, hour, minute, sec;
6375
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_date VALUES(?, ?, ?, ?)");
6378
verify_param_count(stmt, 4);
6381
We need to bzero bind structure because mysql_stmt_bind_param checks all
6384
bzero((char*) my_bind, sizeof(my_bind));
6386
my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
6387
my_bind[1].buffer_type= MYSQL_TYPE_TIME;
6388
my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
6389
my_bind[3].buffer_type= MYSQL_TYPE_DATE;
6391
for (i= 0; i < (int) array_elements(my_bind); i++)
6393
my_bind[i].buffer= (void *) &tm[i];
6394
my_bind[i].is_null= &is_null[i];
6395
my_bind[i].length= &length[i];
6396
my_bind[i].buffer_length= 30;
6410
rc= mysql_stmt_bind_param(stmt, my_bind);
6411
check_execute(stmt, rc);
6413
for (count= 0; count < row_count; count++)
6415
for (i= 0; i < (int) array_elements(my_bind); i++)
6418
tm[i].second_part= second_part+count;
6419
if (my_bind[i].buffer_type != MYSQL_TYPE_TIME)
6421
tm[i].year= year+count;
6422
tm[i].month= month+count;
6423
tm[i].day= day+count;
6426
tm[i].year= tm[i].month= tm[i].day= 0;
6427
if (my_bind[i].buffer_type != MYSQL_TYPE_DATE)
6429
tm[i].hour= hour+count;
6430
tm[i].minute= minute+count;
6431
tm[i].second= sec+count;
6434
tm[i].hour= tm[i].minute= tm[i].second= 0;
6436
rc= mysql_stmt_execute(stmt);
6437
check_execute(stmt, rc);
6440
rc= mysql_commit(mysql);
6443
mysql_stmt_close(stmt);
6445
rc= my_stmt_result("SELECT * FROM test_date");
6446
DIE_UNLESS(row_count == rc);
6448
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_date");
6451
rc= mysql_stmt_bind_result(stmt, my_bind);
6452
check_execute(stmt, rc);
6454
rc= mysql_stmt_execute(stmt);
6455
check_execute(stmt, rc);
6457
rc= mysql_stmt_store_result(stmt);
6458
check_execute(stmt, rc);
6460
for (count= 0; count < row_count; count++)
6462
rc= mysql_stmt_fetch(stmt);
6463
DIE_UNLESS(rc == 0 || rc == MYSQL_DATA_TRUNCATED);
6466
fprintf(stdout, "\n");
6467
for (i= 0; i < array_elements(my_bind); i++)
6470
fprintf(stdout, "\ntime[%d]: %02d-%02d-%02d %02d:%02d:%02d.%02lu",
6471
i, tm[i].year, tm[i].month, tm[i].day,
6472
tm[i].hour, tm[i].minute, tm[i].second,
6474
DIE_UNLESS(tm[i].year == 0 || tm[i].year == year+count);
6475
DIE_UNLESS(tm[i].month == 0 || tm[i].month == month+count);
6476
DIE_UNLESS(tm[i].day == 0 || tm[i].day == day+count);
6478
DIE_UNLESS(tm[i].hour == 0 || tm[i].hour == hour+count);
6479
DIE_UNLESS(tm[i].minute == 0 || tm[i].minute == minute+count);
6480
DIE_UNLESS(tm[i].second == 0 || tm[i].second == sec+count);
6481
DIE_UNLESS(tm[i].second_part == 0 ||
6482
tm[i].second_part == second_part+count);
6485
rc= mysql_stmt_fetch(stmt);
6486
DIE_UNLESS(rc == MYSQL_NO_DATA);
6488
mysql_stmt_close(stmt);
6492
/* Test DATE, TIME, DATETIME and TS with MYSQL_TIME conversion */
6494
static void test_date()
6498
myheader("test_date");
6500
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
6503
rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP, \
6510
test_bind_date_conv(5);
6514
/* Test all time types to DATE and DATE to all types */
6516
static void test_date_date()
6520
myheader("test_date_date");
6522
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
6525
rc= mysql_query(mysql, "CREATE TABLE test_date(c1 DATE, \
6532
test_bind_date_conv(3);
6536
/* Test all time types to TIME and TIME to all types */
6538
static void test_date_time()
6542
myheader("test_date_time");
6544
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
6547
rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIME, \
6554
test_bind_date_conv(3);
6558
/* Test all time types to TIMESTAMP and TIMESTAMP to all types */
6560
static void test_date_ts()
6564
myheader("test_date_ts");
6566
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
6569
rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP, \
6576
test_bind_date_conv(2);
6580
/* Test all time types to DATETIME and DATETIME to all types */
6582
static void test_date_dt()
6586
myheader("test_date_dt");
6588
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
6591
rc= mysql_query(mysql, "CREATE TABLE test_date(c1 datetime, "
6592
" c2 datetime, c3 datetime, c4 date)");
6595
test_bind_date_conv(2);
6599
/* Misc tests to keep pure coverage happy */
6601
static void test_pure_coverage()
6604
MYSQL_BIND my_bind[1];
6608
myheader("test_pure_coverage");
6610
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_pure");
6613
rc= mysql_query(mysql, "CREATE TABLE test_pure(c1 int, c2 varchar(20))");
6616
stmt= mysql_simple_prepare(mysql, "insert into test_pure(c67788) values(10)");
6619
/* Query without params and result should allow to bind 0 arrays */
6620
stmt= mysql_simple_prepare(mysql, "insert into test_pure(c2) values(10)");
6623
rc= mysql_stmt_bind_param(stmt, (MYSQL_BIND*)0);
6624
check_execute(stmt, rc);
6626
rc= mysql_stmt_execute(stmt);
6627
check_execute(stmt, rc);
6629
rc= mysql_stmt_bind_result(stmt, (MYSQL_BIND*)0);
6630
DIE_UNLESS(rc == 1);
6632
mysql_stmt_close(stmt);
6634
stmt= mysql_simple_prepare(mysql, "insert into test_pure(c2) values(?)");
6638
We need to bzero bind structure because mysql_stmt_bind_param checks all
6641
bzero((char*) my_bind, sizeof(my_bind));
6643
my_bind[0].length= &length;
6644
my_bind[0].is_null= 0;
6645
my_bind[0].buffer_length= 0;
6647
my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
6648
rc= mysql_stmt_bind_param(stmt, my_bind);
6649
check_execute_r(stmt, rc); /* unsupported buffer type */
6651
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
6652
rc= mysql_stmt_bind_param(stmt, my_bind);
6653
check_execute(stmt, rc);
6655
rc= mysql_stmt_store_result(stmt);
6656
check_execute(stmt, rc);
6658
mysql_stmt_close(stmt);
6660
stmt= mysql_simple_prepare(mysql, "select * from test_pure");
6661
check_execute(stmt, rc);
6663
rc= mysql_stmt_execute(stmt);
6664
check_execute(stmt, rc);
6666
my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
6667
rc= mysql_stmt_bind_result(stmt, my_bind);
6668
check_execute_r(stmt, rc); /* unsupported buffer type */
6670
rc= mysql_stmt_store_result(stmt);
6671
check_execute(stmt, rc);
6673
rc= mysql_stmt_store_result(stmt);
6674
check_execute_r(stmt, rc); /* commands out of sync */
6676
mysql_stmt_close(stmt);
6678
mysql_query(mysql, "DROP TABLE test_pure");
6682
/* Test for string buffer fetch */
6684
static void test_buffers()
6687
MYSQL_BIND my_bind[1];
6693
myheader("test_buffers");
6695
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_buffer");
6698
rc= mysql_query(mysql, "CREATE TABLE test_buffer(str varchar(20))");
6701
rc= mysql_query(mysql, "insert into test_buffer values('MySQL')\
6702
, ('Database'), ('Open-Source'), ('Popular')");
6705
stmt= mysql_simple_prepare(mysql, "select str from test_buffer");
6708
rc= mysql_stmt_execute(stmt);
6709
check_execute(stmt, rc);
6711
bzero(buffer, sizeof(buffer)); /* Avoid overruns in printf() */
6713
bzero((char*) my_bind, sizeof(my_bind));
6714
my_bind[0].length= &length;
6715
my_bind[0].is_null= &is_null;
6716
my_bind[0].buffer_length= 1;
6717
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
6718
my_bind[0].buffer= (void *)buffer;
6719
my_bind[0].error= &my_bind[0].error_value;
6721
rc= mysql_stmt_bind_result(stmt, my_bind);
6722
check_execute(stmt, rc);
6724
rc= mysql_stmt_store_result(stmt);
6725
check_execute(stmt, rc);
6728
rc= mysql_stmt_fetch(stmt);
6729
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
6730
DIE_UNLESS(my_bind[0].error_value);
6732
fprintf(stdout, "\n data: %s (%lu)", buffer, length);
6733
DIE_UNLESS(buffer[0] == 'M');
6734
DIE_UNLESS(buffer[1] == 'X');
6735
DIE_UNLESS(length == 5);
6737
my_bind[0].buffer_length= 8;
6738
rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
6739
check_execute(stmt, rc);
6741
rc= mysql_stmt_fetch(stmt);
6742
check_execute(stmt, rc);
6744
fprintf(stdout, "\n data: %s (%lu)", buffer, length);
6745
DIE_UNLESS(strncmp(buffer, "Database", 8) == 0);
6746
DIE_UNLESS(length == 8);
6748
my_bind[0].buffer_length= 12;
6749
rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
6750
check_execute(stmt, rc);
6752
rc= mysql_stmt_fetch(stmt);
6753
check_execute(stmt, rc);
6755
fprintf(stdout, "\n data: %s (%lu)", buffer, length);
6756
DIE_UNLESS(strcmp(buffer, "Open-Source") == 0);
6757
DIE_UNLESS(length == 11);
6759
my_bind[0].buffer_length= 6;
6760
rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
6761
check_execute(stmt, rc);
6763
rc= mysql_stmt_fetch(stmt);
6764
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
6765
DIE_UNLESS(my_bind[0].error_value);
6767
fprintf(stdout, "\n data: %s (%lu)", buffer, length);
6768
DIE_UNLESS(strncmp(buffer, "Popula", 6) == 0);
6769
DIE_UNLESS(length == 7);
6771
mysql_stmt_close(stmt);
6775
/* Test the direct query execution in the middle of open stmts */
6777
static void test_open_direct()
6783
myheader("test_open_direct");
6785
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_open_direct");
6788
rc= mysql_query(mysql, "CREATE TABLE test_open_direct(id int, name char(6))");
6791
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_open_direct values(10, 'mysql')");
6794
rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
6797
result= mysql_store_result(mysql);
6800
rc= my_process_result_set(result);
6801
DIE_UNLESS(rc == 0);
6802
mysql_free_result(result);
6804
rc= mysql_stmt_execute(stmt);
6805
check_execute(stmt, rc);
6807
verify_st_affected_rows(stmt, 1);
6809
rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
6812
result= mysql_store_result(mysql);
6815
rc= my_process_result_set(result);
6816
DIE_UNLESS(rc == 1);
6817
mysql_free_result(result);
6819
rc= mysql_stmt_execute(stmt);
6820
check_execute(stmt, rc);
6822
verify_st_affected_rows(stmt, 1);
6824
rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
6827
result= mysql_store_result(mysql);
6830
rc= my_process_result_set(result);
6831
DIE_UNLESS(rc == 2);
6832
mysql_free_result(result);
6834
mysql_stmt_close(stmt);
6836
/* run a direct query in the middle of a fetch */
6837
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_open_direct");
6840
rc= mysql_stmt_execute(stmt);
6841
check_execute(stmt, rc);
6843
rc= mysql_stmt_fetch(stmt);
6844
check_execute(stmt, rc);
6846
rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
6849
rc= mysql_stmt_close(stmt);
6850
check_execute(stmt, rc);
6852
rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
6855
/* run a direct query with store result */
6856
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_open_direct");
6859
rc= mysql_stmt_execute(stmt);
6860
check_execute(stmt, rc);
6862
rc= mysql_stmt_store_result(stmt);
6863
check_execute(stmt, rc);
6865
rc= mysql_stmt_fetch(stmt);
6866
check_execute(stmt, rc);
6868
rc= mysql_query(mysql, "drop table test_open_direct");
6871
rc= mysql_stmt_close(stmt);
6872
check_execute(stmt, rc);
6876
/* Test fetch without prior bound buffers */
6878
static void test_fetch_nobuffs()
6881
MYSQL_BIND my_bind[4];
6885
myheader("test_fetch_nobuffs");
6887
stmt= mysql_simple_prepare(mysql, "SELECT DATABASE(), CURRENT_USER(), \
6888
CURRENT_DATE(), CURRENT_TIME()");
6891
rc= mysql_stmt_execute(stmt);
6892
check_execute(stmt, rc);
6895
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
6899
fprintf(stdout, "\n total rows : %d", rc);
6900
DIE_UNLESS(rc == 1);
6902
bzero((char*) my_bind, sizeof(MYSQL_BIND));
6903
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
6904
my_bind[0].buffer= (void *)str[0];
6905
my_bind[0].buffer_length= sizeof(str[0]);
6906
my_bind[1]= my_bind[2]= my_bind[3]= my_bind[0];
6907
my_bind[1].buffer= (void *)str[1];
6908
my_bind[2].buffer= (void *)str[2];
6909
my_bind[3].buffer= (void *)str[3];
6911
rc= mysql_stmt_bind_result(stmt, my_bind);
6912
check_execute(stmt, rc);
6914
rc= mysql_stmt_execute(stmt);
6915
check_execute(stmt, rc);
6918
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
6923
fprintf(stdout, "\n CURRENT_DATABASE(): %s", str[0]);
6924
fprintf(stdout, "\n CURRENT_USER() : %s", str[1]);
6925
fprintf(stdout, "\n CURRENT_DATE() : %s", str[2]);
6926
fprintf(stdout, "\n CURRENT_TIME() : %s", str[3]);
6930
fprintf(stdout, "\n total rows : %d", rc);
6931
DIE_UNLESS(rc == 1);
6933
mysql_stmt_close(stmt);
6937
/* Test a misc bug */
6939
static void test_ushort_bug()
6942
MYSQL_BIND my_bind[4];
6945
ulong s_length, l_length, ll_length, t_length;
6946
ulonglong longlong_value;
6950
myheader("test_ushort_bug");
6952
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ushort");
6955
rc= mysql_query(mysql, "CREATE TABLE test_ushort(a smallint unsigned, \
6956
b smallint unsigned, \
6957
c smallint unsigned, \
6958
d smallint unsigned)");
6961
rc= mysql_query(mysql,
6962
"INSERT INTO test_ushort VALUES(35999, 35999, 35999, 200)");
6966
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_ushort");
6969
rc= mysql_stmt_execute(stmt);
6970
check_execute(stmt, rc);
6972
bzero((char*) my_bind, sizeof(my_bind));
6973
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
6974
my_bind[0].buffer= (void *)&short_value;
6975
my_bind[0].is_unsigned= TRUE;
6976
my_bind[0].length= &s_length;
6978
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
6979
my_bind[1].buffer= (void *)&long_value;
6980
my_bind[1].length= &l_length;
6982
my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
6983
my_bind[2].buffer= (void *)&longlong_value;
6984
my_bind[2].length= &ll_length;
6986
my_bind[3].buffer_type= MYSQL_TYPE_TINY;
6987
my_bind[3].buffer= (void *)&tiny_value;
6988
my_bind[3].is_unsigned= TRUE;
6989
my_bind[3].length= &t_length;
6991
rc= mysql_stmt_bind_result(stmt, my_bind);
6992
check_execute(stmt, rc);
6994
rc= mysql_stmt_fetch(stmt);
6995
check_execute(stmt, rc);
6999
fprintf(stdout, "\n ushort : %d (%ld)", short_value, s_length);
7000
fprintf(stdout, "\n ulong : %lu (%ld)", (ulong) long_value, l_length);
7001
fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
7003
fprintf(stdout, "\n tinyint : %d (%ld)", tiny_value, t_length);
7006
DIE_UNLESS(short_value == 35999);
7007
DIE_UNLESS(s_length == 2);
7009
DIE_UNLESS(long_value == 35999);
7010
DIE_UNLESS(l_length == 4);
7012
DIE_UNLESS(longlong_value == 35999);
7013
DIE_UNLESS(ll_length == 8);
7015
DIE_UNLESS(tiny_value == 200);
7016
DIE_UNLESS(t_length == 1);
7018
rc= mysql_stmt_fetch(stmt);
7019
DIE_UNLESS(rc == MYSQL_NO_DATA);
7021
mysql_stmt_close(stmt);
7025
/* Test a misc smallint-signed conversion bug */
7027
static void test_sshort_bug()
7030
MYSQL_BIND my_bind[4];
7033
ulong s_length, l_length, ll_length, t_length;
7034
ulonglong longlong_value;
7039
myheader("test_sshort_bug");
7041
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sshort");
7044
rc= mysql_query(mysql, "CREATE TABLE test_sshort(a smallint signed, \
7045
b smallint signed, \
7046
c smallint unsigned, \
7047
d smallint unsigned)");
7050
rc= mysql_query(mysql, "INSERT INTO test_sshort VALUES(-5999, -5999, 35999, 200)");
7054
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_sshort");
7057
rc= mysql_stmt_execute(stmt);
7058
check_execute(stmt, rc);
7060
bzero((char*) my_bind, sizeof(my_bind));
7061
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
7062
my_bind[0].buffer= (void *)&short_value;
7063
my_bind[0].length= &s_length;
7065
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
7066
my_bind[1].buffer= (void *)&long_value;
7067
my_bind[1].length= &l_length;
7069
my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
7070
my_bind[2].buffer= (void *)&longlong_value;
7071
my_bind[2].length= &ll_length;
7073
my_bind[3].buffer_type= MYSQL_TYPE_TINY;
7074
my_bind[3].buffer= (void *)&tiny_value;
7075
my_bind[3].is_unsigned= TRUE;
7076
my_bind[3].length= &t_length;
7078
rc= mysql_stmt_bind_result(stmt, my_bind);
7079
check_execute(stmt, rc);
7081
rc= mysql_stmt_fetch(stmt);
7082
check_execute(stmt, rc);
7086
fprintf(stdout, "\n sshort : %d (%ld)", short_value, s_length);
7087
fprintf(stdout, "\n slong : %ld (%ld)", (long) long_value, l_length);
7088
fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
7090
fprintf(stdout, "\n tinyint : %d (%ld)", tiny_value, t_length);
7093
DIE_UNLESS(short_value == -5999);
7094
DIE_UNLESS(s_length == 2);
7096
DIE_UNLESS(long_value == -5999);
7097
DIE_UNLESS(l_length == 4);
7099
DIE_UNLESS(longlong_value == 35999);
7100
DIE_UNLESS(ll_length == 8);
7102
DIE_UNLESS(tiny_value == 200);
7103
DIE_UNLESS(t_length == 1);
7105
rc= mysql_stmt_fetch(stmt);
7106
DIE_UNLESS(rc == MYSQL_NO_DATA);
7108
mysql_stmt_close(stmt);
7112
/* Test a misc tinyint-signed conversion bug */
7114
static void test_stiny_bug()
7117
MYSQL_BIND my_bind[4];
7120
ulong s_length, l_length, ll_length, t_length;
7121
ulonglong longlong_value;
7126
myheader("test_stiny_bug");
7128
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stiny");
7131
rc= mysql_query(mysql, "CREATE TABLE test_stiny(a tinyint signed, \
7133
c tinyint unsigned, \
7134
d tinyint unsigned)");
7137
rc= mysql_query(mysql, "INSERT INTO test_stiny VALUES(-128, -127, 255, 0)");
7141
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_stiny");
7144
rc= mysql_stmt_execute(stmt);
7145
check_execute(stmt, rc);
7147
bzero((char*) my_bind, sizeof(my_bind));
7148
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
7149
my_bind[0].buffer= (void *)&short_value;
7150
my_bind[0].length= &s_length;
7152
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
7153
my_bind[1].buffer= (void *)&long_value;
7154
my_bind[1].length= &l_length;
7156
my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
7157
my_bind[2].buffer= (void *)&longlong_value;
7158
my_bind[2].length= &ll_length;
7160
my_bind[3].buffer_type= MYSQL_TYPE_TINY;
7161
my_bind[3].buffer= (void *)&tiny_value;
7162
my_bind[3].length= &t_length;
7164
rc= mysql_stmt_bind_result(stmt, my_bind);
7165
check_execute(stmt, rc);
7167
rc= mysql_stmt_fetch(stmt);
7168
check_execute(stmt, rc);
7172
fprintf(stdout, "\n sshort : %d (%ld)", short_value, s_length);
7173
fprintf(stdout, "\n slong : %ld (%ld)", (long) long_value, l_length);
7174
fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
7176
fprintf(stdout, "\n tinyint : %d (%ld)", tiny_value, t_length);
7179
DIE_UNLESS(short_value == -128);
7180
DIE_UNLESS(s_length == 2);
7182
DIE_UNLESS(long_value == -127);
7183
DIE_UNLESS(l_length == 4);
7185
DIE_UNLESS(longlong_value == 255);
7186
DIE_UNLESS(ll_length == 8);
7188
DIE_UNLESS(tiny_value == 0);
7189
DIE_UNLESS(t_length == 1);
7191
rc= mysql_stmt_fetch(stmt);
7192
DIE_UNLESS(rc == MYSQL_NO_DATA);
7194
mysql_stmt_close(stmt);
7198
/* Test misc field information, bug: #74 */
7200
static void test_field_misc()
7204
MYSQL_BIND my_bind[1];
7205
char table_type[NAME_LEN];
7209
myheader("test_field_misc");
7211
rc= mysql_query(mysql, "SELECT @@autocommit");
7214
result= mysql_store_result(mysql);
7217
rc= my_process_result_set(result);
7218
DIE_UNLESS(rc == 1);
7220
verify_prepare_field(result, 0,
7221
"@@autocommit", "", /* field and its org name */
7222
MYSQL_TYPE_LONGLONG, /* field type */
7223
"", "", /* table and its org name */
7224
"", 1, 0); /* db name, length(its bool flag)*/
7226
mysql_free_result(result);
7228
stmt= mysql_simple_prepare(mysql, "SELECT @@autocommit");
7231
rc= mysql_stmt_execute(stmt);
7232
check_execute(stmt, rc);
7234
result= mysql_stmt_result_metadata(stmt);
7237
rc= my_process_stmt_result(stmt);
7238
DIE_UNLESS(rc == 1);
7240
verify_prepare_field(result, 0,
7241
"@@autocommit", "", /* field and its org name */
7242
MYSQL_TYPE_LONGLONG, /* field type */
7243
"", "", /* table and its org name */
7244
"", 1, 0); /* db name, length(its bool flag)*/
7246
mysql_free_result(result);
7247
mysql_stmt_close(stmt);
7249
stmt= mysql_simple_prepare(mysql, "SELECT @@storage_engine");
7252
rc= mysql_stmt_execute(stmt);
7253
check_execute(stmt, rc);
7255
bzero((char*) my_bind, sizeof(my_bind));
7256
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
7257
my_bind[0].buffer= table_type;
7258
my_bind[0].length= &type_length;
7259
my_bind[0].buffer_length= NAME_LEN;
7261
rc= mysql_stmt_bind_result(stmt, my_bind);
7262
check_execute(stmt, rc);
7264
rc= mysql_stmt_fetch(stmt);
7265
check_execute(stmt, rc);
7267
fprintf(stdout, "\n default table type: %s(%ld)", table_type, type_length);
7269
rc= mysql_stmt_fetch(stmt);
7270
DIE_UNLESS(rc == MYSQL_NO_DATA);
7272
mysql_stmt_close(stmt);
7274
stmt= mysql_simple_prepare(mysql, "SELECT @@storage_engine");
7277
result= mysql_stmt_result_metadata(stmt);
7279
DIE_UNLESS(mysql_stmt_field_count(stmt) == mysql_num_fields(result));
7281
rc= mysql_stmt_execute(stmt);
7282
check_execute(stmt, rc);
7284
DIE_UNLESS(1 == my_process_stmt_result(stmt));
7286
verify_prepare_field(result, 0,
7287
"@@storage_engine", "", /* field and its org name */
7288
mysql_get_server_version(mysql) <= 50000 ?
7289
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7290
"", "", /* table and its org name */
7291
"", type_length, 0); /* db name, length */
7293
mysql_free_result(result);
7294
mysql_stmt_close(stmt);
7296
stmt= mysql_simple_prepare(mysql, "SELECT @@max_error_count");
7299
result= mysql_stmt_result_metadata(stmt);
7302
rc= mysql_stmt_execute(stmt);
7303
check_execute(stmt, rc);
7305
rc= my_process_stmt_result(stmt);
7306
DIE_UNLESS(rc == 1);
7308
verify_prepare_field(result, 0,
7309
"@@max_error_count", "", /* field and its org name */
7310
MYSQL_TYPE_LONGLONG, /* field type */
7311
"", "", /* table and its org name */
7312
"", 10, 0); /* db name, length */
7314
mysql_free_result(result);
7315
mysql_stmt_close(stmt);
7317
stmt= mysql_simple_prepare(mysql, "SELECT @@max_allowed_packet");
7320
result= mysql_stmt_result_metadata(stmt);
7323
rc= mysql_stmt_execute(stmt);
7324
check_execute(stmt, rc);
7326
DIE_UNLESS(1 == my_process_stmt_result(stmt));
7328
verify_prepare_field(result, 0,
7329
"@@max_allowed_packet", "", /* field and its org name */
7330
MYSQL_TYPE_LONGLONG, /* field type */
7331
"", "", /* table and its org name */
7332
"", 10, 0); /* db name, length */
7334
mysql_free_result(result);
7335
mysql_stmt_close(stmt);
7337
stmt= mysql_simple_prepare(mysql, "SELECT @@sql_warnings");
7340
result= mysql_stmt_result_metadata(stmt);
7343
rc= mysql_stmt_execute(stmt);
7344
check_execute(stmt, rc);
7346
rc= my_process_stmt_result(stmt);
7347
DIE_UNLESS(rc == 1);
7349
verify_prepare_field(result, 0,
7350
"@@sql_warnings", "", /* field and its org name */
7351
MYSQL_TYPE_LONGLONG, /* field type */
7352
"", "", /* table and its org name */
7353
"", 1, 0); /* db name, length */
7355
mysql_free_result(result);
7356
mysql_stmt_close(stmt);
7361
Test SET OPTION feature with prepare stmts
7362
bug #85 (reported by mark@mysql.com)
7365
static void test_set_option()
7371
myheader("test_set_option");
7373
mysql_autocommit(mysql, TRUE);
7375
/* LIMIT the rows count to 2 */
7376
rc= mysql_query(mysql, "SET OPTION SQL_SELECT_LIMIT= 2");
7379
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_limit");
7382
rc= mysql_query(mysql, "CREATE TABLE test_limit(a tinyint)");
7385
rc= mysql_query(mysql, "INSERT INTO test_limit VALUES(10), (20), (30), (40)");
7389
fprintf(stdout, "\n with SQL_SELECT_LIMIT= 2 (direct)");
7390
rc= mysql_query(mysql, "SELECT * FROM test_limit");
7393
result= mysql_store_result(mysql);
7396
rc= my_process_result_set(result);
7397
DIE_UNLESS(rc == 2);
7399
mysql_free_result(result);
7402
fprintf(stdout, "\n with SQL_SELECT_LIMIT=2 (prepare)");
7403
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_limit");
7406
rc= mysql_stmt_execute(stmt);
7407
check_execute(stmt, rc);
7409
rc= my_process_stmt_result(stmt);
7410
DIE_UNLESS(rc == 2);
7412
mysql_stmt_close(stmt);
7414
/* RESET the LIMIT the rows count to 0 */
7416
fprintf(stdout, "\n with SQL_SELECT_LIMIT=DEFAULT (prepare)");
7417
rc= mysql_query(mysql, "SET OPTION SQL_SELECT_LIMIT=DEFAULT");
7420
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_limit");
7423
rc= mysql_stmt_execute(stmt);
7424
check_execute(stmt, rc);
7426
rc= my_process_stmt_result(stmt);
7427
DIE_UNLESS(rc == 4);
7429
mysql_stmt_close(stmt);
7434
Test a misc GRANT option
7435
bug #89 (reported by mark@mysql.com)
7438
#ifndef EMBEDDED_LIBRARY
7439
static void test_prepare_grant()
7442
char query[MAX_TEST_QUERY_LENGTH];
7444
myheader("test_prepare_grant");
7446
mysql_autocommit(mysql, TRUE);
7448
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_grant");
7451
rc= mysql_query(mysql, "CREATE TABLE test_grant(a tinyint primary key auto_increment)");
7454
strxmov(query, "GRANT INSERT, UPDATE, SELECT ON ", current_db,
7455
".test_grant TO 'test_grant'@",
7456
opt_host ? opt_host : "'localhost'", NullS);
7458
if (mysql_query(mysql, query))
7460
myerror("GRANT failed");
7463
If server started with --skip-grant-tables, skip this test, else
7464
exit to indicate an error
7466
ER_UNKNOWN_COM_ERROR= 1047
7468
if (mysql_errno(mysql) != 1047)
7473
MYSQL *org_mysql= mysql, *lmysql;
7477
fprintf(stdout, "\n Establishing a test connection ...");
7478
if (!(lmysql= mysql_init(NULL)))
7480
myerror("mysql_init() failed");
7483
if (!(mysql_real_connect(lmysql, opt_host, "test_grant",
7484
"", current_db, opt_port,
7485
opt_unix_socket, 0)))
7487
myerror("connection failed");
7488
mysql_close(lmysql);
7491
lmysql->reconnect= 1;
7493
fprintf(stdout, "OK");
7496
rc= mysql_query(mysql, "INSERT INTO test_grant VALUES(NULL)");
7499
rc= mysql_query(mysql, "INSERT INTO test_grant(a) VALUES(NULL)");
7502
execute_prepare_query("INSERT INTO test_grant(a) VALUES(NULL)", 1);
7503
execute_prepare_query("INSERT INTO test_grant VALUES(NULL)", 1);
7504
execute_prepare_query("UPDATE test_grant SET a=9 WHERE a=1", 1);
7505
rc= my_stmt_result("SELECT a FROM test_grant");
7506
DIE_UNLESS(rc == 4);
7508
/* Both DELETE expected to fail as user does not have DELETE privs */
7510
rc= mysql_query(mysql, "DELETE FROM test_grant");
7513
stmt= mysql_simple_prepare(mysql, "DELETE FROM test_grant");
7516
rc= my_stmt_result("SELECT * FROM test_grant");
7517
DIE_UNLESS(rc == 4);
7519
mysql_close(lmysql);
7522
rc= mysql_query(mysql, "delete from mysql.user where User='test_grant'");
7524
DIE_UNLESS(1 == mysql_affected_rows(mysql));
7526
rc= mysql_query(mysql, "delete from mysql.tables_priv where User='test_grant'");
7528
DIE_UNLESS(1 == mysql_affected_rows(mysql));
7532
#endif /* EMBEDDED_LIBRARY */
7535
Test a crash when invalid/corrupted .frm is used in the
7537
bug #93 (reported by serg@mysql.com).
7540
static void test_frm_bug()
7543
MYSQL_BIND my_bind[2];
7547
char data_dir[FN_REFLEN];
7548
char test_frm[FN_REFLEN];
7551
myheader("test_frm_bug");
7553
mysql_autocommit(mysql, TRUE);
7555
rc= mysql_query(mysql, "drop table if exists test_frm_bug");
7558
rc= mysql_query(mysql, "flush tables");
7561
stmt= mysql_simple_prepare(mysql, "show variables like 'datadir'");
7564
rc= mysql_stmt_execute(stmt);
7565
check_execute(stmt, rc);
7567
bzero((char*) my_bind, sizeof(my_bind));
7568
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
7569
my_bind[0].buffer= data_dir;
7570
my_bind[0].buffer_length= FN_REFLEN;
7571
my_bind[1]= my_bind[0];
7573
rc= mysql_stmt_bind_result(stmt, my_bind);
7574
check_execute(stmt, rc);
7576
rc= mysql_stmt_fetch(stmt);
7577
check_execute(stmt, rc);
7580
fprintf(stdout, "\n data directory: %s", data_dir);
7582
rc= mysql_stmt_fetch(stmt);
7583
DIE_UNLESS(rc == MYSQL_NO_DATA);
7585
strxmov(test_frm, data_dir, "/", current_db, "/", "test_frm_bug.frm", NullS);
7588
fprintf(stdout, "\n test_frm: %s", test_frm);
7590
if (!(test_file= my_fopen(test_frm, (int) (O_RDWR | O_CREAT), MYF(MY_WME))))
7592
fprintf(stdout, "\n ERROR: my_fopen failed for '%s'", test_frm);
7593
fprintf(stdout, "\n test cancelled");
7597
fprintf(test_file, "this is a junk file for test");
7599
rc= mysql_query(mysql, "SHOW TABLE STATUS like 'test_frm_bug'");
7602
result= mysql_store_result(mysql);
7603
mytest(result);/* It can't be NULL */
7605
rc= my_process_result_set(result);
7606
DIE_UNLESS(rc == 1);
7608
mysql_data_seek(result, 0);
7610
row= mysql_fetch_row(result);
7614
fprintf(stdout, "\n Comment: %s", row[17]);
7615
DIE_UNLESS(row[17] != 0);
7617
mysql_free_result(result);
7618
mysql_stmt_close(stmt);
7620
my_fclose(test_file, MYF(0));
7621
mysql_query(mysql, "drop table if exists test_frm_bug");
7625
/* Test DECIMAL conversion */
7627
static void test_decimal_bug()
7630
MYSQL_BIND my_bind[1];
7635
myheader("test_decimal_bug");
7637
mysql_autocommit(mysql, TRUE);
7639
rc= mysql_query(mysql, "drop table if exists test_decimal_bug");
7642
rc= mysql_query(mysql, "create table test_decimal_bug(c1 decimal(10, 2))");
7645
rc= mysql_query(mysql, "insert into test_decimal_bug value(8), (10.22), (5.61)");
7648
stmt= mysql_simple_prepare(mysql, "select c1 from test_decimal_bug where c1= ?");
7652
We need to bzero bind structure because mysql_stmt_bind_param checks all
7655
bzero((char*) my_bind, sizeof(my_bind));
7657
my_bind[0].buffer_type= MYSQL_TYPE_NEWDECIMAL;
7658
my_bind[0].buffer= (void *)data;
7659
my_bind[0].buffer_length= 25;
7660
my_bind[0].is_null= &is_null;
7663
rc= mysql_stmt_bind_param(stmt, my_bind);
7664
check_execute(stmt, rc);
7666
strmov(data, "8.0");
7667
rc= mysql_stmt_execute(stmt);
7668
check_execute(stmt, rc);
7671
rc= mysql_stmt_bind_result(stmt, my_bind);
7672
check_execute(stmt, rc);
7674
rc= mysql_stmt_fetch(stmt);
7675
check_execute(stmt, rc);
7678
fprintf(stdout, "\n data: %s", data);
7679
DIE_UNLESS(strcmp(data, "8.00") == 0);
7681
rc= mysql_stmt_fetch(stmt);
7682
DIE_UNLESS(rc == MYSQL_NO_DATA);
7684
strmov(data, "5.61");
7685
rc= mysql_stmt_execute(stmt);
7686
check_execute(stmt, rc);
7689
rc= mysql_stmt_bind_result(stmt, my_bind);
7690
check_execute(stmt, rc);
7692
rc= mysql_stmt_fetch(stmt);
7693
check_execute(stmt, rc);
7696
fprintf(stdout, "\n data: %s", data);
7697
DIE_UNLESS(strcmp(data, "5.61") == 0);
7699
rc= mysql_stmt_fetch(stmt);
7700
DIE_UNLESS(rc == MYSQL_NO_DATA);
7703
rc= mysql_stmt_execute(stmt);
7704
check_execute(stmt, rc);
7706
rc= mysql_stmt_fetch(stmt);
7707
DIE_UNLESS(rc == MYSQL_NO_DATA);
7709
strmov(data, "10.22"); is_null= 0;
7710
rc= mysql_stmt_execute(stmt);
7711
check_execute(stmt, rc);
7714
rc= mysql_stmt_bind_result(stmt, my_bind);
7715
check_execute(stmt, rc);
7717
rc= mysql_stmt_fetch(stmt);
7718
check_execute(stmt, rc);
7721
fprintf(stdout, "\n data: %s", data);
7722
DIE_UNLESS(strcmp(data, "10.22") == 0);
7724
rc= mysql_stmt_fetch(stmt);
7725
DIE_UNLESS(rc == MYSQL_NO_DATA);
7727
mysql_stmt_close(stmt);
7731
/* Test EXPLAIN bug (#115, reported by mark@mysql.com & georg@php.net). */
7733
static void test_explain_bug()
7739
myheader("test_explain_bug");
7741
mysql_autocommit(mysql, TRUE);
7743
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_explain");
7746
rc= mysql_query(mysql, "CREATE TABLE test_explain(id int, name char(2))");
7749
stmt= mysql_simple_prepare(mysql, "explain test_explain");
7752
rc= mysql_stmt_execute(stmt);
7753
check_execute(stmt, rc);
7755
rc= my_process_stmt_result(stmt);
7756
DIE_UNLESS(rc == 2);
7758
result= mysql_stmt_result_metadata(stmt);
7762
fprintf(stdout, "\n total fields in the result: %d",
7763
mysql_num_fields(result));
7764
DIE_UNLESS(6 == mysql_num_fields(result));
7766
verify_prepare_field(result, 0, "Field", "COLUMN_NAME",
7767
mysql_get_server_version(mysql) <= 50000 ?
7768
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7771
verify_prepare_field(result, 1, "Type", "COLUMN_TYPE", MYSQL_TYPE_BLOB,
7774
verify_prepare_field(result, 2, "Null", "IS_NULLABLE",
7775
mysql_get_server_version(mysql) <= 50000 ?
7776
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7779
verify_prepare_field(result, 3, "Key", "COLUMN_KEY",
7780
mysql_get_server_version(mysql) <= 50000 ?
7781
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7784
if ( mysql_get_server_version(mysql) >= 50027 )
7786
/* The patch for bug#23037 changes column type of DEAULT to blob */
7787
verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
7788
MYSQL_TYPE_BLOB, 0, 0, "", 0, 0);
7792
verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
7793
mysql_get_server_version(mysql) >= 50027 ?
7795
mysql_get_server_version(mysql) <= 50000 ?
7796
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7798
mysql_get_server_version(mysql) >= 50027 ? 0 :64, 0);
7801
verify_prepare_field(result, 5, "Extra", "EXTRA",
7802
mysql_get_server_version(mysql) <= 50000 ?
7803
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7806
mysql_free_result(result);
7807
mysql_stmt_close(stmt);
7809
stmt= mysql_simple_prepare(mysql, "explain select id, name FROM test_explain");
7812
rc= mysql_stmt_execute(stmt);
7813
check_execute(stmt, rc);
7815
rc= my_process_stmt_result(stmt);
7816
DIE_UNLESS(rc == 1);
7818
result= mysql_stmt_result_metadata(stmt);
7822
fprintf(stdout, "\n total fields in the result: %d",
7823
mysql_num_fields(result));
7824
DIE_UNLESS(10 == mysql_num_fields(result));
7826
verify_prepare_field(result, 0, "id", "", MYSQL_TYPE_LONGLONG,
7829
verify_prepare_field(result, 1, "select_type", "", MYSQL_TYPE_VAR_STRING,
7832
verify_prepare_field(result, 2, "table", "", MYSQL_TYPE_VAR_STRING,
7833
"", "", "", NAME_CHAR_LEN, 0);
7835
verify_prepare_field(result, 3, "type", "", MYSQL_TYPE_VAR_STRING,
7838
verify_prepare_field(result, 4, "possible_keys", "", MYSQL_TYPE_VAR_STRING,
7839
"", "", "", NAME_CHAR_LEN*MAX_KEY, 0);
7841
verify_prepare_field(result, 5, "key", "", MYSQL_TYPE_VAR_STRING,
7842
"", "", "", NAME_CHAR_LEN, 0);
7844
if (mysql_get_server_version(mysql) <= 50000)
7846
verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_LONGLONG, "",
7851
verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_VAR_STRING, "",
7855
verify_prepare_field(result, 7, "ref", "", MYSQL_TYPE_VAR_STRING,
7856
"", "", "", NAME_CHAR_LEN*16, 0);
7858
verify_prepare_field(result, 8, "rows", "", MYSQL_TYPE_LONGLONG,
7861
verify_prepare_field(result, 9, "Extra", "", MYSQL_TYPE_VAR_STRING,
7862
"", "", "", 255, 0);
7864
mysql_free_result(result);
7865
mysql_stmt_close(stmt);
7868
#ifdef NOT_YET_WORKING
7871
Test math functions.
7872
Bug #148 (reported by salle@mysql.com).
7875
#define myerrno(n) check_errcode(n)
7877
static void check_errcode(const unsigned int err)
7879
if (!opt_silent || mysql_errno(mysql) != err)
7881
if (mysql->server_version)
7882
fprintf(stdout, "\n [MySQL-%s]", mysql->server_version);
7884
fprintf(stdout, "\n [MySQL]");
7885
fprintf(stdout, "[%d] %s\n", mysql_errno(mysql), mysql_error(mysql));
7887
DIE_UNLESS(mysql_errno(mysql) == err);
7891
static void test_drop_temp()
7895
myheader("test_drop_temp");
7897
rc= mysql_query(mysql, "DROP DATABASE IF EXISTS test_drop_temp_db");
7900
rc= mysql_query(mysql, "CREATE DATABASE test_drop_temp_db");
7903
rc= mysql_query(mysql, "CREATE TABLE test_drop_temp_db.t1(c1 int, c2 char(1))");
7906
rc= mysql_query(mysql, "delete from mysql.db where Db='test_drop_temp_db'");
7909
rc= mysql_query(mysql, "delete from mysql.db where Db='test_drop_temp_db'");
7912
strxmov(query, "GRANT SELECT, USAGE, DROP ON test_drop_temp_db.* TO test_temp@",
7913
opt_host ? opt_host : "localhost", NullS);
7915
if (mysql_query(mysql, query))
7917
myerror("GRANT failed");
7920
If server started with --skip-grant-tables, skip this test, else
7921
exit to indicate an error
7923
ER_UNKNOWN_COM_ERROR= 1047
7925
if (mysql_errno(mysql) != 1047)
7930
MYSQL *org_mysql= mysql, *lmysql;
7933
fprintf(stdout, "\n Establishing a test connection ...");
7934
if (!(lmysql= mysql_init(NULL)))
7936
myerror("mysql_init() failed");
7940
rc= mysql_query(mysql, "flush privileges");
7943
if (!(mysql_real_connect(lmysql, opt_host ? opt_host : "localhost", "test_temp",
7944
"", "test_drop_temp_db", opt_port,
7945
opt_unix_socket, 0)))
7948
myerror("connection failed");
7949
mysql_close(lmysql);
7952
lmysql->reconnect= 1;
7954
fprintf(stdout, "OK");
7957
rc= mysql_query(mysql, "INSERT INTO t1 VALUES(10, 'C')");
7958
myerrno((uint)1142);
7960
rc= mysql_query(mysql, "DROP TABLE t1");
7961
myerrno((uint)1142);
7964
rc= mysql_query(mysql, "CREATE TEMPORARY TABLE test_drop_temp_db.t1(c1 int)");
7967
rc= mysql_query(mysql, "CREATE TEMPORARY TABLE test_drop_temp_db.t2 LIKE test_drop_temp_db.t1");
7972
rc= mysql_query(mysql, "DROP TABLE t1, t2");
7975
rc= mysql_query(mysql, "DROP TEMPORARY TABLE t1");
7978
rc= mysql_query(mysql, "DROP TEMPORARY TABLE t2");
7981
mysql_close(lmysql);
7984
rc= mysql_query(mysql, "drop database test_drop_temp_db");
7986
DIE_UNLESS(1 == mysql_affected_rows(mysql));
7988
rc= mysql_query(mysql, "delete from mysql.user where User='test_temp'");
7990
DIE_UNLESS(1 == mysql_affected_rows(mysql));
7993
rc= mysql_query(mysql, "delete from mysql.tables_priv where User='test_temp'");
7995
DIE_UNLESS(1 == mysql_affected_rows(mysql));
8001
/* Test warnings for cuted rows */
8003
static void test_cuted_rows()
8008
myheader("test_cuted_rows");
8010
mysql_query(mysql, "DROP TABLE if exists t1");
8011
mysql_query(mysql, "DROP TABLE if exists t2");
8013
rc= mysql_query(mysql, "CREATE TABLE t1(c1 tinyint)");
8016
rc= mysql_query(mysql, "CREATE TABLE t2(c1 int not null)");
8019
rc= mysql_query(mysql, "INSERT INTO t1 values(10), (NULL), (NULL)");
8022
count= mysql_warning_count(mysql);
8024
fprintf(stdout, "\n total warnings: %d", count);
8025
DIE_UNLESS(count == 0);
8027
rc= mysql_query(mysql, "INSERT INTO t2 SELECT * FROM t1");
8030
count= mysql_warning_count(mysql);
8032
fprintf(stdout, "\n total warnings: %d", count);
8033
DIE_UNLESS(count == 2);
8035
rc= mysql_query(mysql, "SHOW WARNINGS");
8038
result= mysql_store_result(mysql);
8041
rc= my_process_result_set(result);
8042
DIE_UNLESS(rc == 2);
8043
mysql_free_result(result);
8045
rc= mysql_query(mysql, "INSERT INTO t1 VALUES('junk'), (876789)");
8048
count= mysql_warning_count(mysql);
8050
fprintf(stdout, "\n total warnings: %d", count);
8051
DIE_UNLESS(count == 2);
8053
rc= mysql_query(mysql, "SHOW WARNINGS");
8056
result= mysql_store_result(mysql);
8059
rc= my_process_result_set(result);
8060
DIE_UNLESS(rc == 2);
8061
mysql_free_result(result);
8065
/* Test update/binary logs */
8067
static void test_logs()
8070
MYSQL_BIND my_bind[2];
8076
myheader("test_logs");
8079
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_logs");
8082
rc= mysql_query(mysql, "CREATE TABLE test_logs(id smallint, name varchar(20))");
8085
strmov((char *)data, "INSERT INTO test_logs VALUES(?, ?)");
8086
stmt= mysql_simple_prepare(mysql, data);
8090
We need to bzero bind structure because mysql_stmt_bind_param checks all
8093
bzero((char*) my_bind, sizeof(my_bind));
8095
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
8096
my_bind[0].buffer= (void *)&id;
8098
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
8099
my_bind[1].buffer= (void *)&data;
8100
my_bind[1].buffer_length= 255;
8101
my_bind[1].length= &length;
8104
length= (ulong)(strmov((char *)data, "MySQL - Open Source Database")- data);
8106
rc= mysql_stmt_bind_param(stmt, my_bind);
8107
check_execute(stmt, rc);
8109
rc= mysql_stmt_execute(stmt);
8110
check_execute(stmt, rc);
8112
strmov((char *)data, "'");
8115
rc= mysql_stmt_execute(stmt);
8116
check_execute(stmt, rc);
8118
strmov((char *)data, "\"");
8121
rc= mysql_stmt_execute(stmt);
8122
check_execute(stmt, rc);
8124
length= (ulong)(strmov((char *)data, "my\'sql\'")-data);
8125
rc= mysql_stmt_execute(stmt);
8126
check_execute(stmt, rc);
8128
length= (ulong)(strmov((char *)data, "my\"sql\"")-data);
8129
rc= mysql_stmt_execute(stmt);
8130
check_execute(stmt, rc);
8132
mysql_stmt_close(stmt);
8134
strmov((char *)data, "INSERT INTO test_logs VALUES(20, 'mysql')");
8135
stmt= mysql_simple_prepare(mysql, data);
8138
rc= mysql_stmt_execute(stmt);
8139
check_execute(stmt, rc);
8141
rc= mysql_stmt_execute(stmt);
8142
check_execute(stmt, rc);
8144
mysql_stmt_close(stmt);
8146
strmov((char *)data, "SELECT * FROM test_logs WHERE id=?");
8147
stmt= mysql_simple_prepare(mysql, data);
8150
rc= mysql_stmt_bind_param(stmt, my_bind);
8151
check_execute(stmt, rc);
8153
rc= mysql_stmt_execute(stmt);
8154
check_execute(stmt, rc);
8156
my_bind[1].buffer_length= 255;
8157
rc= mysql_stmt_bind_result(stmt, my_bind);
8158
check_execute(stmt, rc);
8160
rc= mysql_stmt_fetch(stmt);
8161
check_execute(stmt, rc);
8165
fprintf(stdout, "id : %d\n", id);
8166
fprintf(stdout, "name : %s(%ld)\n", data, length);
8169
DIE_UNLESS(id == 9876);
8170
DIE_UNLESS(length == 19 || length == 20); /* Due to VARCHAR(20) */
8171
DIE_UNLESS(is_prefix(data, "MySQL - Open Source") == 1);
8173
rc= mysql_stmt_fetch(stmt);
8174
check_execute(stmt, rc);
8177
fprintf(stdout, "\n name : %s(%ld)", data, length);
8179
DIE_UNLESS(length == 1);
8180
DIE_UNLESS(strcmp(data, "'") == 0);
8182
rc= mysql_stmt_fetch(stmt);
8183
check_execute(stmt, rc);
8186
fprintf(stdout, "\n name : %s(%ld)", data, length);
8188
DIE_UNLESS(length == 1);
8189
DIE_UNLESS(strcmp(data, "\"") == 0);
8191
rc= mysql_stmt_fetch(stmt);
8192
check_execute(stmt, rc);
8195
fprintf(stdout, "\n name : %s(%ld)", data, length);
8197
DIE_UNLESS(length == 7);
8198
DIE_UNLESS(strcmp(data, "my\'sql\'") == 0);
8200
rc= mysql_stmt_fetch(stmt);
8201
check_execute(stmt, rc);
8204
fprintf(stdout, "\n name : %s(%ld)", data, length);
8206
DIE_UNLESS(length == 7);
8207
/*DIE_UNLESS(strcmp(data, "my\"sql\"") == 0); */
8209
rc= mysql_stmt_fetch(stmt);
8210
DIE_UNLESS(rc == MYSQL_NO_DATA);
8212
mysql_stmt_close(stmt);
8214
rc= mysql_query(mysql, "DROP TABLE test_logs");
8219
/* Test 'n' statements create and close */
8221
static void test_nstmts()
8226
static uint i, total_stmts= 2000;
8227
MYSQL_BIND my_bind[1];
8229
myheader("test_nstmts");
8231
mysql_autocommit(mysql, TRUE);
8233
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_nstmts");
8236
rc= mysql_query(mysql, "CREATE TABLE test_nstmts(id int)");
8240
We need to bzero bind structure because mysql_stmt_bind_param checks all
8243
bzero((char*) my_bind, sizeof(my_bind));
8245
my_bind[0].buffer= (void *)&i;
8246
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8248
for (i= 0; i < total_stmts; i++)
8251
fprintf(stdout, "\r stmt: %d", i);
8253
strmov(query, "insert into test_nstmts values(?)");
8254
stmt= mysql_simple_prepare(mysql, query);
8257
rc= mysql_stmt_bind_param(stmt, my_bind);
8258
check_execute(stmt, rc);
8260
rc= mysql_stmt_execute(stmt);
8261
check_execute(stmt, rc);
8263
mysql_stmt_close(stmt);
8266
stmt= mysql_simple_prepare(mysql, " select count(*) from test_nstmts");
8269
rc= mysql_stmt_execute(stmt);
8270
check_execute(stmt, rc);
8273
rc= mysql_stmt_bind_result(stmt, my_bind);
8274
check_execute(stmt, rc);
8276
rc= mysql_stmt_fetch(stmt);
8277
check_execute(stmt, rc);
8279
fprintf(stdout, "\n total rows: %d", i);
8280
DIE_UNLESS( i == total_stmts);
8282
rc= mysql_stmt_fetch(stmt);
8283
DIE_UNLESS(rc == MYSQL_NO_DATA);
8285
mysql_stmt_close(stmt);
8287
rc= mysql_query(mysql, "DROP TABLE test_nstmts");
8292
/* Test stmt seek() functions */
8294
static void test_fetch_seek()
8297
MYSQL_BIND my_bind[3];
8298
MYSQL_ROW_OFFSET row;
8301
char c2[11], c3[20];
8303
myheader("test_fetch_seek");
8304
rc= mysql_query(mysql, "drop table if exists t1");
8308
rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10), c3 timestamp)");
8311
rc= mysql_query(mysql, "insert into t1(c2) values('venu'), ('mysql'), ('open'), ('source')");
8314
stmt= mysql_simple_prepare(mysql, "select * from t1");
8317
bzero((char*) my_bind, sizeof(my_bind));
8318
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8319
my_bind[0].buffer= (void *)&c1;
8321
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
8322
my_bind[1].buffer= (void *)c2;
8323
my_bind[1].buffer_length= sizeof(c2);
8325
my_bind[2]= my_bind[1];
8326
my_bind[2].buffer= (void *)c3;
8327
my_bind[2].buffer_length= sizeof(c3);
8329
rc= mysql_stmt_execute(stmt);
8330
check_execute(stmt, rc);
8332
rc= mysql_stmt_bind_result(stmt, my_bind);
8333
check_execute(stmt, rc);
8335
rc= mysql_stmt_store_result(stmt);
8336
check_execute(stmt, rc);
8338
rc= mysql_stmt_fetch(stmt);
8339
check_execute(stmt, rc);
8342
fprintf(stdout, "\n row 0: %ld, %s, %s", (long) c1, c2, c3);
8344
row= mysql_stmt_row_tell(stmt);
8346
row= mysql_stmt_row_seek(stmt, row);
8348
rc= mysql_stmt_fetch(stmt);
8349
check_execute(stmt, rc);
8352
fprintf(stdout, "\n row 2: %ld, %s, %s", (long) c1, c2, c3);
8354
row= mysql_stmt_row_seek(stmt, row);
8356
rc= mysql_stmt_fetch(stmt);
8357
check_execute(stmt, rc);
8360
fprintf(stdout, "\n row 2: %ld, %s, %s", (long) c1, c2, c3);
8362
mysql_stmt_data_seek(stmt, 0);
8364
rc= mysql_stmt_fetch(stmt);
8365
check_execute(stmt, rc);
8368
fprintf(stdout, "\n row 0: %ld, %s, %s", (long) c1, c2, c3);
8370
rc= mysql_stmt_fetch(stmt);
8371
check_execute(stmt, rc);
8373
rc= mysql_stmt_fetch(stmt);
8374
check_execute(stmt, rc);
8376
rc= mysql_stmt_fetch(stmt);
8377
check_execute(stmt, rc);
8379
rc= mysql_stmt_fetch(stmt);
8380
DIE_UNLESS(rc == MYSQL_NO_DATA);
8382
mysql_stmt_close(stmt);
8383
myquery(mysql_query(mysql, "drop table t1"));
8387
/* Test mysql_stmt_fetch_column() with offset */
8389
static void test_fetch_offset()
8392
MYSQL_BIND my_bind[1];
8399
myheader("test_fetch_offset");
8401
rc= mysql_query(mysql, "drop table if exists t1");
8404
rc= mysql_query(mysql, "create table t1(a char(10))");
8407
rc= mysql_query(mysql, "insert into t1 values('abcdefghij'), (null)");
8410
stmt= mysql_simple_prepare(mysql, "select * from t1");
8413
bzero((char*) my_bind, sizeof(my_bind));
8414
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8415
my_bind[0].buffer= (void *)data;
8416
my_bind[0].buffer_length= 11;
8417
my_bind[0].is_null= &is_null;
8418
my_bind[0].length= &length;
8420
rc= mysql_stmt_execute(stmt);
8421
check_execute(stmt, rc);
8423
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8424
check_execute_r(stmt, rc);
8426
rc= mysql_stmt_bind_result(stmt, my_bind);
8427
check_execute(stmt, rc);
8429
rc= mysql_stmt_store_result(stmt);
8430
check_execute(stmt, rc);
8432
rc= mysql_stmt_fetch(stmt);
8433
check_execute(stmt, rc);
8436
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8437
check_execute(stmt, rc);
8439
fprintf(stdout, "\n col 1: %s (%ld)", data, length);
8440
DIE_UNLESS(strncmp(data, "abcd", 4) == 0 && length == 10);
8442
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 5);
8443
check_execute(stmt, rc);
8445
fprintf(stdout, "\n col 1: %s (%ld)", data, length);
8446
DIE_UNLESS(strncmp(data, "fg", 2) == 0 && length == 10);
8448
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 9);
8449
check_execute(stmt, rc);
8451
fprintf(stdout, "\n col 0: %s (%ld)", data, length);
8452
DIE_UNLESS(strncmp(data, "j", 1) == 0 && length == 10);
8454
rc= mysql_stmt_fetch(stmt);
8455
check_execute(stmt, rc);
8459
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8460
check_execute(stmt, rc);
8462
DIE_UNLESS(is_null == 1);
8464
rc= mysql_stmt_fetch(stmt);
8465
DIE_UNLESS(rc == MYSQL_NO_DATA);
8467
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8468
check_execute_r(stmt, rc);
8470
mysql_stmt_close(stmt);
8472
myquery(mysql_query(mysql, "drop table t1"));
8476
/* Test mysql_stmt_fetch_column() */
8478
static void test_fetch_column()
8481
MYSQL_BIND my_bind[2];
8482
char c2[20], bc2[20];
8483
ulong l1, l2, bl1, bl2;
8486
myheader("test_fetch_column");
8488
rc= mysql_query(mysql, "drop table if exists t1");
8491
rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10))");
8494
rc= mysql_query(mysql, "insert into t1(c2) values('venu'), ('mysql')");
8497
stmt= mysql_simple_prepare(mysql, "select * from t1 order by c2 desc");
8500
bzero((char*) my_bind, sizeof(my_bind));
8501
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8502
my_bind[0].buffer= (void *)&bc1;
8503
my_bind[0].buffer_length= 0;
8504
my_bind[0].is_null= 0;
8505
my_bind[0].length= &bl1;
8506
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
8507
my_bind[1].buffer= (void *)bc2;
8508
my_bind[1].buffer_length= 7;
8509
my_bind[1].is_null= 0;
8510
my_bind[1].length= &bl2;
8512
rc= mysql_stmt_execute(stmt);
8513
check_execute(stmt, rc);
8515
rc= mysql_stmt_bind_result(stmt, my_bind);
8516
check_execute(stmt, rc);
8518
rc= mysql_stmt_store_result(stmt);
8519
check_execute(stmt, rc);
8521
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0); /* No-op at this point */
8522
check_execute_r(stmt, rc);
8524
rc= mysql_stmt_fetch(stmt);
8525
check_execute(stmt, rc);
8528
fprintf(stdout, "\n row 0: %d, %s", bc1, bc2);
8531
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8532
my_bind[0].buffer= (void *)c2;
8533
my_bind[0].buffer_length= 7;
8534
my_bind[0].is_null= 0;
8535
my_bind[0].length= &l2;
8537
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8538
check_execute(stmt, rc);
8540
fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
8541
DIE_UNLESS(strncmp(c2, "venu", 4) == 0 && l2 == 4);
8544
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8545
check_execute(stmt, rc);
8547
fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
8548
DIE_UNLESS(strcmp(c2, "venu") == 0 && l2 == 4);
8551
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8552
my_bind[0].buffer= (void *)&c1;
8553
my_bind[0].buffer_length= 0;
8554
my_bind[0].is_null= 0;
8555
my_bind[0].length= &l1;
8557
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8558
check_execute(stmt, rc);
8560
fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
8561
DIE_UNLESS(c1 == 1 && l1 == 4);
8563
rc= mysql_stmt_fetch(stmt);
8564
check_execute(stmt, rc);
8567
fprintf(stdout, "\n row 1: %d, %s", bc1, bc2);
8570
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8571
my_bind[0].buffer= (void *)c2;
8572
my_bind[0].buffer_length= 7;
8573
my_bind[0].is_null= 0;
8574
my_bind[0].length= &l2;
8576
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8577
check_execute(stmt, rc);
8579
fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
8580
DIE_UNLESS(strncmp(c2, "mysq", 4) == 0 && l2 == 5);
8583
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8584
check_execute(stmt, rc);
8586
fprintf(stdout, "\n col 1: %si(%ld)", c2, l2);
8587
DIE_UNLESS(strcmp(c2, "mysql") == 0 && l2 == 5);
8590
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8591
my_bind[0].buffer= (void *)&c1;
8592
my_bind[0].buffer_length= 0;
8593
my_bind[0].is_null= 0;
8594
my_bind[0].length= &l1;
8596
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8597
check_execute(stmt, rc);
8599
fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
8600
DIE_UNLESS(c1 == 2 && l1 == 4);
8602
rc= mysql_stmt_fetch(stmt);
8603
DIE_UNLESS(rc == MYSQL_NO_DATA);
8605
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8606
check_execute_r(stmt, rc);
8608
mysql_stmt_close(stmt);
8609
myquery(mysql_query(mysql, "drop table t1"));
8613
/* Test mysql_list_fields() */
8615
static void test_list_fields()
8619
myheader("test_list_fields");
8621
rc= mysql_query(mysql, "drop table if exists t1");
8624
rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10) default 'mysql')");
8627
result= mysql_list_fields(mysql, "t1", NULL);
8630
rc= my_process_result_set(result);
8631
DIE_UNLESS(rc == 0);
8633
verify_prepare_field(result, 0, "c1", "c1", MYSQL_TYPE_LONG,
8635
current_db, 11, "0");
8637
verify_prepare_field(result, 1, "c2", "c2", MYSQL_TYPE_STRING,
8639
current_db, 10, "mysql");
8641
mysql_free_result(result);
8642
myquery(mysql_query(mysql, "drop table t1"));
8646
static void test_bug19671()
8650
myheader("test_bug19671");
8652
mysql_query(mysql, "set sql_mode=''");
8653
rc= mysql_query(mysql, "drop table if exists t1");
8656
rc= mysql_query(mysql, "drop view if exists v1");
8659
rc= mysql_query(mysql, "create table t1(f1 int)");
8662
rc= mysql_query(mysql, "create view v1 as select va.* from t1 va");
8665
result= mysql_list_fields(mysql, "v1", NULL);
8668
rc= my_process_result_set(result);
8669
DIE_UNLESS(rc == 0);
8671
verify_prepare_field(result, 0, "f1", "f1", MYSQL_TYPE_LONG,
8672
"v1", "v1", current_db, 11, "0");
8674
mysql_free_result(result);
8675
myquery(mysql_query(mysql, "drop view v1"));
8676
myquery(mysql_query(mysql, "drop table t1"));
8680
/* Test a memory ovverun bug */
8682
static void test_mem_overun()
8684
char buffer[10000], field[10];
8686
MYSQL_RES *field_res;
8689
myheader("test_mem_overun");
8692
Test a memory ovverun bug when a table had 1000 fields with
8695
rc= mysql_query(mysql, "drop table if exists t_mem_overun");
8698
strxmov(buffer, "create table t_mem_overun(", NullS);
8699
for (i= 0; i < 1000; i++)
8701
sprintf(field, "c%d int", i);
8702
strxmov(buffer, buffer, field, ", ", NullS);
8704
length= strlen(buffer);
8705
buffer[length-2]= ')';
8706
buffer[--length]= '\0';
8708
rc= mysql_real_query(mysql, buffer, length);
8711
strxmov(buffer, "insert into t_mem_overun values(", NullS);
8712
for (i= 0; i < 1000; i++)
8714
strxmov(buffer, buffer, "1, ", NullS);
8716
length= strlen(buffer);
8717
buffer[length-2]= ')';
8718
buffer[--length]= '\0';
8720
rc= mysql_real_query(mysql, buffer, length);
8723
rc= mysql_query(mysql, "select * from t_mem_overun");
8726
rc= my_process_result(mysql);
8727
DIE_UNLESS(rc == 1);
8729
stmt= mysql_simple_prepare(mysql, "select * from t_mem_overun");
8732
rc= mysql_stmt_execute(stmt);
8733
check_execute(stmt, rc);
8735
field_res= mysql_stmt_result_metadata(stmt);
8739
fprintf(stdout, "\n total fields : %d", mysql_num_fields(field_res));
8740
DIE_UNLESS( 1000 == mysql_num_fields(field_res));
8742
rc= mysql_stmt_store_result(stmt);
8743
check_execute(stmt, rc);
8745
rc= mysql_stmt_fetch(stmt);
8746
check_execute(stmt, rc);
8748
rc= mysql_stmt_fetch(stmt);
8749
DIE_UNLESS(rc == MYSQL_NO_DATA);
8751
mysql_free_result(field_res);
8753
mysql_stmt_close(stmt);
8757
/* Test mysql_stmt_free_result() */
8759
static void test_free_result()
8762
MYSQL_BIND my_bind[1];
8767
myheader("test_free_result");
8769
rc= mysql_query(mysql, "drop table if exists test_free_result");
8772
rc= mysql_query(mysql, "create table test_free_result("
8773
"c1 int primary key auto_increment)");
8776
rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
8779
stmt= mysql_simple_prepare(mysql, "select * from test_free_result");
8782
bzero((char*) my_bind, sizeof(my_bind));
8783
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8784
my_bind[0].buffer= (void *)&bc1;
8785
my_bind[0].length= &bl1;
8787
rc= mysql_stmt_execute(stmt);
8788
check_execute(stmt, rc);
8790
rc= mysql_stmt_bind_result(stmt, my_bind);
8791
check_execute(stmt, rc);
8793
rc= mysql_stmt_fetch(stmt);
8794
check_execute(stmt, rc);
8797
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8798
my_bind[0].buffer= (void *)c2;
8799
my_bind[0].buffer_length= 7;
8800
my_bind[0].is_null= 0;
8801
my_bind[0].length= &l2;
8803
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8804
check_execute(stmt, rc);
8806
fprintf(stdout, "\n col 0: %s(%ld)", c2, l2);
8807
DIE_UNLESS(strncmp(c2, "1", 1) == 0 && l2 == 1);
8809
rc= mysql_stmt_fetch(stmt);
8810
check_execute(stmt, rc);
8813
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8814
my_bind[0].buffer= (void *)&c1;
8815
my_bind[0].buffer_length= 0;
8816
my_bind[0].is_null= 0;
8817
my_bind[0].length= &l2;
8819
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8820
check_execute(stmt, rc);
8822
fprintf(stdout, "\n col 0: %d(%ld)", c1, l2);
8823
DIE_UNLESS(c1 == 2 && l2 == 4);
8825
rc= mysql_query(mysql, "drop table test_free_result");
8826
myquery_r(rc); /* error should be, COMMANDS OUT OF SYNC */
8828
rc= mysql_stmt_free_result(stmt);
8829
check_execute(stmt, rc);
8831
rc= mysql_query(mysql, "drop table test_free_result");
8832
myquery(rc); /* should be successful */
8834
mysql_stmt_close(stmt);
8838
/* Test mysql_stmt_store_result() */
8840
static void test_free_store_result()
8843
MYSQL_BIND my_bind[1];
8848
myheader("test_free_store_result");
8850
rc= mysql_query(mysql, "drop table if exists test_free_result");
8853
rc= mysql_query(mysql, "create table test_free_result(c1 int primary key auto_increment)");
8856
rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
8859
stmt= mysql_simple_prepare(mysql, "select * from test_free_result");
8862
bzero((char*) my_bind, sizeof(my_bind));
8863
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8864
my_bind[0].buffer= (void *)&bc1;
8865
my_bind[0].buffer_length= 0;
8866
my_bind[0].is_null= 0;
8867
my_bind[0].length= &bl1;
8869
rc= mysql_stmt_execute(stmt);
8870
check_execute(stmt, rc);
8872
rc= mysql_stmt_bind_result(stmt, my_bind);
8873
check_execute(stmt, rc);
8875
rc= mysql_stmt_store_result(stmt);
8876
check_execute(stmt, rc);
8878
rc= mysql_stmt_fetch(stmt);
8879
check_execute(stmt, rc);
8882
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8883
my_bind[0].buffer= (void *)c2;
8884
my_bind[0].buffer_length= 7;
8885
my_bind[0].is_null= 0;
8886
my_bind[0].length= &l2;
8888
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8889
check_execute(stmt, rc);
8891
fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
8892
DIE_UNLESS(strncmp(c2, "1", 1) == 0 && l2 == 1);
8894
rc= mysql_stmt_fetch(stmt);
8895
check_execute(stmt, rc);
8898
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8899
my_bind[0].buffer= (void *)&c1;
8900
my_bind[0].buffer_length= 0;
8901
my_bind[0].is_null= 0;
8902
my_bind[0].length= &l2;
8904
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8905
check_execute(stmt, rc);
8907
fprintf(stdout, "\n col 0: %d(%ld)", c1, l2);
8908
DIE_UNLESS(c1 == 2 && l2 == 4);
8910
rc= mysql_stmt_free_result(stmt);
8911
check_execute(stmt, rc);
8913
rc= mysql_query(mysql, "drop table test_free_result");
8916
mysql_stmt_close(stmt);
8922
static void test_sqlmode()
8925
MYSQL_BIND my_bind[2];
8928
char query[MAX_TEST_QUERY_LENGTH];
8930
myheader("test_sqlmode");
8932
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_piping");
8935
rc= mysql_query(mysql, "CREATE TABLE test_piping(name varchar(10))");
8938
/* PIPES_AS_CONCAT */
8939
strmov(query, "SET SQL_MODE= \"PIPES_AS_CONCAT\"");
8941
fprintf(stdout, "\n With %s", query);
8942
rc= mysql_query(mysql, query);
8945
strmov(query, "INSERT INTO test_piping VALUES(?||?)");
8947
fprintf(stdout, "\n query: %s", query);
8948
stmt= mysql_simple_prepare(mysql, query);
8952
fprintf(stdout, "\n total parameters: %ld", mysql_stmt_param_count(stmt));
8955
We need to bzero bind structure because mysql_stmt_bind_param checks all
8958
bzero((char*) my_bind, sizeof(my_bind));
8960
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8961
my_bind[0].buffer= (void *)c1;
8962
my_bind[0].buffer_length= 2;
8964
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
8965
my_bind[1].buffer= (void *)c2;
8966
my_bind[1].buffer_length= 3;
8968
rc= mysql_stmt_bind_param(stmt, my_bind);
8969
check_execute(stmt, rc);
8971
strmov(c1, "My"); strmov(c2, "SQL");
8972
rc= mysql_stmt_execute(stmt);
8973
check_execute(stmt, rc);
8974
mysql_stmt_close(stmt);
8976
verify_col_data("test_piping", "name", "MySQL");
8978
rc= mysql_query(mysql, "DELETE FROM test_piping");
8981
strmov(query, "SELECT connection_id ()");
8983
fprintf(stdout, "\n query: %s", query);
8984
stmt= mysql_simple_prepare(mysql, query);
8986
mysql_stmt_close(stmt);
8989
strmov(query, "SET SQL_MODE= \"ANSI\"");
8991
fprintf(stdout, "\n With %s", query);
8992
rc= mysql_query(mysql, query);
8995
strmov(query, "INSERT INTO test_piping VALUES(?||?)");
8997
fprintf(stdout, "\n query: %s", query);
8998
stmt= mysql_simple_prepare(mysql, query);
9001
fprintf(stdout, "\n total parameters: %ld", mysql_stmt_param_count(stmt));
9003
rc= mysql_stmt_bind_param(stmt, my_bind);
9004
check_execute(stmt, rc);
9006
strmov(c1, "My"); strmov(c2, "SQL");
9007
rc= mysql_stmt_execute(stmt);
9008
check_execute(stmt, rc);
9010
mysql_stmt_close(stmt);
9011
verify_col_data("test_piping", "name", "MySQL");
9013
/* ANSI mode spaces ... */
9014
strmov(query, "SELECT connection_id ()");
9016
fprintf(stdout, "\n query: %s", query);
9017
stmt= mysql_simple_prepare(mysql, query);
9020
rc= mysql_stmt_execute(stmt);
9021
check_execute(stmt, rc);
9023
rc= mysql_stmt_fetch(stmt);
9024
check_execute(stmt, rc);
9026
rc= mysql_stmt_fetch(stmt);
9027
DIE_UNLESS(rc == MYSQL_NO_DATA);
9029
fprintf(stdout, "\n returned 1 row\n");
9031
mysql_stmt_close(stmt);
9033
/* IGNORE SPACE MODE */
9034
strmov(query, "SET SQL_MODE= \"IGNORE_SPACE\"");
9036
fprintf(stdout, "\n With %s", query);
9037
rc= mysql_query(mysql, query);
9040
strmov(query, "SELECT connection_id ()");
9042
fprintf(stdout, "\n query: %s", query);
9043
stmt= mysql_simple_prepare(mysql, query);
9046
rc= mysql_stmt_execute(stmt);
9047
check_execute(stmt, rc);
9049
rc= mysql_stmt_fetch(stmt);
9050
check_execute(stmt, rc);
9052
rc= mysql_stmt_fetch(stmt);
9053
DIE_UNLESS(rc == MYSQL_NO_DATA);
9055
fprintf(stdout, "\n returned 1 row");
9057
mysql_stmt_close(stmt);
9061
/* Test for timestamp handling */
9063
static void test_ts()
9066
MYSQL_BIND my_bind[6];
9068
MYSQL_RES *prep_res;
9071
int rc, field_count;
9073
char query[MAX_TEST_QUERY_LENGTH];
9074
const char *queries [3]= {"SELECT a, b, c FROM test_ts WHERE %c=?",
9075
"SELECT a, b, c FROM test_ts WHERE %c=?",
9076
"SELECT a, b, c FROM test_ts WHERE %c=CAST(? AS DATE)"};
9077
myheader("test_ts");
9079
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ts");
9082
rc= mysql_query(mysql, "CREATE TABLE test_ts(a DATE, b TIME, c TIMESTAMP)");
9085
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_ts VALUES(?, ?, ?), (?, ?, ?)");
9095
length= (long)(strmov(strts, "2003-07-12 21:07:46") - strts);
9098
We need to bzero bind structure because mysql_stmt_bind_param checks all
9101
bzero((char*) my_bind, sizeof(my_bind));
9103
my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
9104
my_bind[0].buffer= (void *)&ts;
9105
my_bind[0].buffer_length= sizeof(ts);
9107
my_bind[2]= my_bind[1]= my_bind[0];
9109
my_bind[3].buffer_type= MYSQL_TYPE_STRING;
9110
my_bind[3].buffer= (void *)strts;
9111
my_bind[3].buffer_length= sizeof(strts);
9112
my_bind[3].length= &length;
9114
my_bind[5]= my_bind[4]= my_bind[3];
9116
rc= mysql_stmt_bind_param(stmt, my_bind);
9117
check_execute(stmt, rc);
9119
rc= mysql_stmt_execute(stmt);
9120
check_execute(stmt, rc);
9122
mysql_stmt_close(stmt);
9124
verify_col_data("test_ts", "a", "2003-07-12");
9125
verify_col_data("test_ts", "b", "21:07:46");
9126
verify_col_data("test_ts", "c", "2003-07-12 21:07:46");
9128
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_ts");
9131
prep_res= mysql_stmt_result_metadata(stmt);
9134
rc= mysql_stmt_execute(stmt);
9135
check_execute(stmt, rc);
9137
rc= my_process_stmt_result(stmt);
9138
DIE_UNLESS(rc == 2);
9139
field_count= mysql_num_fields(prep_res);
9141
mysql_free_result(prep_res);
9142
mysql_stmt_close(stmt);
9144
for (name= 'a'; field_count--; name++)
9148
sprintf(query, queries[field_count], name);
9151
fprintf(stdout, "\n %s", query);
9152
stmt= mysql_simple_prepare(mysql, query);
9155
rc= mysql_stmt_bind_param(stmt, my_bind);
9156
check_execute(stmt, rc);
9158
rc= mysql_stmt_execute(stmt);
9159
check_execute(stmt, rc);
9161
while (mysql_stmt_fetch(stmt) == 0)
9165
fprintf(stdout, "\n returned '%d' rows", row_count);
9166
DIE_UNLESS(row_count == 2);
9167
mysql_stmt_close(stmt);
9172
/* Test for bug #1500. */
9174
static void test_bug1500()
9177
MYSQL_BIND my_bind[3];
9179
int32 int_data[3]= {2, 3, 4};
9182
myheader("test_bug1500");
9184
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bg1500");
9187
rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (i INT)");
9190
rc= mysql_query(mysql, "INSERT INTO test_bg1500 VALUES (1), (2)");
9193
rc= mysql_commit(mysql);
9196
stmt= mysql_simple_prepare(mysql, "SELECT i FROM test_bg1500 WHERE i IN (?, ?, ?)");
9198
verify_param_count(stmt, 3);
9201
We need to bzero bind structure because mysql_stmt_bind_param checks all
9204
bzero((char*) my_bind, sizeof(my_bind));
9206
my_bind[0].buffer= (void *)int_data;
9207
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
9208
my_bind[2]= my_bind[1]= my_bind[0];
9209
my_bind[1].buffer= (void *)(int_data + 1);
9210
my_bind[2].buffer= (void *)(int_data + 2);
9212
rc= mysql_stmt_bind_param(stmt, my_bind);
9213
check_execute(stmt, rc);
9215
rc= mysql_stmt_execute(stmt);
9216
check_execute(stmt, rc);
9218
rc= my_process_stmt_result(stmt);
9219
DIE_UNLESS(rc == 1);
9221
mysql_stmt_close(stmt);
9223
rc= mysql_query(mysql, "DROP TABLE test_bg1500");
9226
rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (s VARCHAR(25), FULLTEXT(s)) engine=MyISAM");
9229
rc= mysql_query(mysql,
9230
"INSERT INTO test_bg1500 VALUES ('Gravedigger'), ('Greed'), ('Hollow Dogs')");
9233
rc= mysql_commit(mysql);
9236
stmt= mysql_simple_prepare(mysql,
9237
"SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (?)");
9240
verify_param_count(stmt, 1);
9243
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
9244
my_bind[0].buffer= (void *) data;
9245
my_bind[0].buffer_length= strlen(data);
9246
my_bind[0].is_null= 0;
9247
my_bind[0].length= 0;
9249
rc= mysql_stmt_bind_param(stmt, my_bind);
9250
check_execute(stmt, rc);
9252
rc= mysql_stmt_execute(stmt);
9253
check_execute(stmt, rc);
9255
rc= my_process_stmt_result(stmt);
9256
DIE_UNLESS(rc == 1);
9258
mysql_stmt_close(stmt);
9260
/* This should work too */
9261
stmt= mysql_simple_prepare(mysql,
9262
"SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (CONCAT(?, 'digger'))");
9265
verify_param_count(stmt, 1);
9268
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
9269
my_bind[0].buffer= (void *) data;
9270
my_bind[0].buffer_length= strlen(data);
9272
rc= mysql_stmt_bind_param(stmt, my_bind);
9273
check_execute(stmt, rc);
9275
rc= mysql_stmt_execute(stmt);
9276
check_execute(stmt, rc);
9278
rc= my_process_stmt_result(stmt);
9279
DIE_UNLESS(rc == 1);
9281
mysql_stmt_close(stmt);
9285
static void test_bug1946()
9289
const char *query= "INSERT INTO prepare_command VALUES (?)";
9291
myheader("test_bug1946");
9293
rc= mysql_query(mysql, "DROP TABLE IF EXISTS prepare_command");
9296
rc= mysql_query(mysql, "CREATE TABLE prepare_command(ID INT)");
9299
stmt= mysql_simple_prepare(mysql, query);
9301
rc= mysql_real_query(mysql, query, strlen(query));
9302
DIE_UNLESS(rc != 0);
9304
fprintf(stdout, "Got error (as expected):\n");
9307
mysql_stmt_close(stmt);
9308
rc= mysql_query(mysql, "DROP TABLE prepare_command");
9312
static void test_parse_error_and_bad_length()
9317
/* check that we get 4 syntax errors over the 4 calls */
9318
myheader("test_parse_error_and_bad_length");
9320
rc= mysql_query(mysql, "SHOW DATABAAAA");
9323
fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
9324
rc= mysql_real_query(mysql, "SHOW DATABASES", 100);
9327
fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
9329
stmt= mysql_simple_prepare(mysql, "SHOW DATABAAAA");
9332
fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
9333
stmt= mysql_stmt_init(mysql);
9335
rc= mysql_stmt_prepare(stmt, "SHOW DATABASES", 100);
9336
DIE_UNLESS(rc != 0);
9338
fprintf(stdout, "Got error (as expected): '%s'\n", mysql_stmt_error(stmt));
9339
mysql_stmt_close(stmt);
9343
static void test_bug2247()
9349
const char *create= "CREATE TABLE bug2247(id INT UNIQUE AUTO_INCREMENT)";
9350
const char *insert= "INSERT INTO bug2247 VALUES (NULL)";
9351
const char *SELECT= "SELECT id FROM bug2247";
9352
const char *update= "UPDATE bug2247 SET id=id+10";
9353
const char *drop= "DROP TABLE IF EXISTS bug2247";
9354
ulonglong exp_count;
9355
enum { NUM_ROWS= 5 };
9357
myheader("test_bug2247");
9360
fprintf(stdout, "\nChecking if stmt_affected_rows is not affected by\n"
9361
"mysql_query ... ");
9362
/* create table and insert few rows */
9363
rc= mysql_query(mysql, drop);
9366
rc= mysql_query(mysql, create);
9369
stmt= mysql_simple_prepare(mysql, insert);
9371
for (i= 0; i < NUM_ROWS; ++i)
9373
rc= mysql_stmt_execute(stmt);
9374
check_execute(stmt, rc);
9376
exp_count= mysql_stmt_affected_rows(stmt);
9377
DIE_UNLESS(exp_count == 1);
9379
rc= mysql_query(mysql, SELECT);
9382
mysql_store_result overwrites mysql->affected_rows. Check that
9383
mysql_stmt_affected_rows() returns the same value, whereas
9384
mysql_affected_rows() value is correct.
9386
res= mysql_store_result(mysql);
9389
DIE_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS);
9390
DIE_UNLESS(exp_count == mysql_stmt_affected_rows(stmt));
9392
rc= mysql_query(mysql, update);
9394
DIE_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS);
9395
DIE_UNLESS(exp_count == mysql_stmt_affected_rows(stmt));
9397
mysql_free_result(res);
9398
mysql_stmt_close(stmt);
9400
/* check that mysql_stmt_store_result modifies mysql_stmt_affected_rows */
9401
stmt= mysql_simple_prepare(mysql, SELECT);
9404
rc= mysql_stmt_execute(stmt);
9405
check_execute(stmt, rc);
9406
rc= mysql_stmt_store_result(stmt);
9407
check_execute(stmt, rc);
9408
exp_count= mysql_stmt_affected_rows(stmt);
9409
DIE_UNLESS(exp_count == NUM_ROWS);
9411
rc= mysql_query(mysql, insert);
9413
DIE_UNLESS(mysql_affected_rows(mysql) == 1);
9414
DIE_UNLESS(mysql_stmt_affected_rows(stmt) == exp_count);
9416
mysql_stmt_close(stmt);
9418
fprintf(stdout, "OK");
9422
static void test_subqueries()
9426
const char *query= "SELECT (SELECT SUM(a+b) FROM t2 where t1.b=t2.b GROUP BY t1.a LIMIT 1) as scalar_s, exists (select 1 from t2 where t2.a/2=t1.a) as exists_s, a in (select a+3 from t2) as in_s, (a-1, b-1) in (select a, b from t2) as in_row_s FROM t1, (select a x, b y from t2) tt WHERE x=a";
9428
myheader("test_subqueries");
9430
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
9433
rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
9436
rc= mysql_query(mysql,
9437
"insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
9440
rc= mysql_query(mysql, "create table t2 select * from t1;");
9443
stmt= mysql_simple_prepare(mysql, query);
9445
for (i= 0; i < 3; i++)
9447
rc= mysql_stmt_execute(stmt);
9448
check_execute(stmt, rc);
9449
rc= my_process_stmt_result(stmt);
9450
DIE_UNLESS(rc == 5);
9452
mysql_stmt_close(stmt);
9454
rc= mysql_query(mysql, "DROP TABLE t1, t2");
9459
static void test_bad_union()
9462
const char *query= "SELECT 1, 2 union SELECT 1";
9464
myheader("test_bad_union");
9466
stmt= mysql_simple_prepare(mysql, query);
9467
DIE_UNLESS(stmt == 0);
9472
static void test_distinct()
9477
"SELECT 2+count(distinct b), group_concat(a) FROM t1 group by a";
9479
myheader("test_distinct");
9481
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
9484
rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
9487
rc= mysql_query(mysql,
9488
"insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), \
9489
(1, 10), (2, 20), (3, 30), (4, 40), (5, 50);");
9492
for (i= 0; i < 3; i++)
9494
stmt= mysql_simple_prepare(mysql, query);
9496
rc= mysql_stmt_execute(stmt);
9497
check_execute(stmt, rc);
9498
rc= my_process_stmt_result(stmt);
9499
DIE_UNLESS(rc == 5);
9500
mysql_stmt_close(stmt);
9503
rc= mysql_query(mysql, "DROP TABLE t1");
9509
Test for bug#2248 "mysql_fetch without prior mysql_stmt_execute hangs"
9512
static void test_bug2248()
9516
const char *query1= "SELECT DATABASE()";
9517
const char *query2= "INSERT INTO test_bug2248 VALUES (10)";
9519
myheader("test_bug2248");
9521
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bug2248");
9524
rc= mysql_query(mysql, "CREATE TABLE test_bug2248 (id int)");
9527
stmt= mysql_simple_prepare(mysql, query1);
9530
/* This should not hang */
9531
rc= mysql_stmt_fetch(stmt);
9532
check_execute_r(stmt, rc);
9535
rc= mysql_stmt_store_result(stmt);
9536
check_execute_r(stmt, rc);
9538
mysql_stmt_close(stmt);
9540
stmt= mysql_simple_prepare(mysql, query2);
9543
rc= mysql_stmt_execute(stmt);
9544
check_execute(stmt, rc);
9546
/* This too should not hang but should return proper error */
9547
rc= mysql_stmt_fetch(stmt);
9548
DIE_UNLESS(rc == 1);
9550
/* This too should not hang but should not bark */
9551
rc= mysql_stmt_store_result(stmt);
9552
check_execute(stmt, rc);
9554
/* This should return proper error */
9555
rc= mysql_stmt_fetch(stmt);
9556
check_execute_r(stmt, rc);
9557
DIE_UNLESS(rc == 1);
9559
mysql_stmt_close(stmt);
9561
rc= mysql_query(mysql, "DROP TABLE test_bug2248");
9566
static void test_subqueries_ref()
9570
const char *query= "SELECT a as ccc from t1 outr where a+1=(SELECT 1+outr.a from t1 where outr.a+1=a+1 and a=1)";
9572
myheader("test_subqueries_ref");
9574
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
9577
rc= mysql_query(mysql, "CREATE TABLE t1 (a int);");
9580
rc= mysql_query(mysql,
9581
"insert into t1 values (1), (2), (3), (4), (5);");
9584
stmt= mysql_simple_prepare(mysql, query);
9586
for (i= 0; i < 3; i++)
9588
rc= mysql_stmt_execute(stmt);
9589
check_execute(stmt, rc);
9590
rc= my_process_stmt_result(stmt);
9591
DIE_UNLESS(rc == 1);
9593
mysql_stmt_close(stmt);
9595
rc= mysql_query(mysql, "DROP TABLE t1");
9600
static void test_union()
9605
myheader("test_union");
9607
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
9610
rc= mysql_query(mysql,
9612
"(id INTEGER NOT NULL PRIMARY KEY, "
9613
" name VARCHAR(20) NOT NULL)");
9615
rc= mysql_query(mysql,
9616
"INSERT INTO t1 (id, name) VALUES "
9617
"(2, 'Ja'), (3, 'Ede'), "
9618
"(4, 'Haag'), (5, 'Kabul'), "
9619
"(6, 'Almere'), (7, 'Utrecht'), "
9620
"(8, 'Qandahar'), (9, 'Amsterdam'), "
9621
"(10, 'Amersfoort'), (11, 'Constantine')");
9623
rc= mysql_query(mysql,
9625
"(id INTEGER NOT NULL PRIMARY KEY, "
9626
" name VARCHAR(20) NOT NULL)");
9628
rc= mysql_query(mysql,
9629
"INSERT INTO t2 (id, name) VALUES "
9630
"(4, 'Guam'), (5, 'Aruba'), "
9631
"(6, 'Angola'), (7, 'Albania'), "
9632
"(8, 'Anguilla'), (9, 'Argentina'), "
9633
"(10, 'Azerbaijan'), (11, 'Afghanistan'), "
9634
"(12, 'Burkina Faso'), (13, 'Faroe Islands')");
9637
stmt= mysql_simple_prepare(mysql,
9638
"SELECT t1.name FROM t1 UNION "
9639
"SELECT t2.name FROM t2");
9642
rc= mysql_stmt_execute(stmt);
9643
check_execute(stmt, rc);
9644
rc= my_process_stmt_result(stmt);
9645
DIE_UNLESS(rc == 20);
9646
mysql_stmt_close(stmt);
9648
rc= mysql_query(mysql, "DROP TABLE t1, t2");
9653
static void test_bug3117()
9662
myheader("test_bug3117");
9664
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
9667
rc= mysql_query(mysql, "CREATE TABLE t1 (id int auto_increment primary key)");
9670
stmt= mysql_simple_prepare(mysql, "SELECT LAST_INSERT_ID()");
9673
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
9676
rc= mysql_stmt_execute(stmt);
9677
check_execute(stmt, rc);
9679
bzero((char*) &buffer, sizeof(buffer));
9680
buffer.buffer_type= MYSQL_TYPE_LONGLONG;
9681
buffer.buffer_length= sizeof(lii);
9682
buffer.buffer= (void *)&lii;
9683
buffer.length= &length;
9684
buffer.is_null= &is_null;
9686
rc= mysql_stmt_bind_result(stmt, &buffer);
9687
check_execute(stmt, rc);
9689
rc= mysql_stmt_store_result(stmt);
9690
check_execute(stmt, rc);
9692
rc= mysql_stmt_fetch(stmt);
9693
check_execute(stmt, rc);
9695
DIE_UNLESS(is_null == 0 && lii == 1);
9697
fprintf(stdout, "\n\tLAST_INSERT_ID()= 1 ok\n");
9699
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
9702
rc= mysql_stmt_execute(stmt);
9703
check_execute(stmt, rc);
9705
rc= mysql_stmt_fetch(stmt);
9706
check_execute(stmt, rc);
9708
DIE_UNLESS(is_null == 0 && lii == 2);
9710
fprintf(stdout, "\tLAST_INSERT_ID()= 2 ok\n");
9712
mysql_stmt_close(stmt);
9714
rc= mysql_query(mysql, "DROP TABLE t1");
9719
static void test_join()
9723
const char *query[]= {"SELECT * FROM t2 join t1 on (t1.a=t2.a)",
9724
"SELECT * FROM t2 natural join t1",
9725
"SELECT * FROM t2 join t1 using(a)",
9726
"SELECT * FROM t2 left join t1 on(t1.a=t2.a)",
9727
"SELECT * FROM t2 natural left join t1",
9728
"SELECT * FROM t2 left join t1 using(a)",
9729
"SELECT * FROM t2 right join t1 on(t1.a=t2.a)",
9730
"SELECT * FROM t2 natural right join t1",
9731
"SELECT * FROM t2 right join t1 using(a)"};
9733
myheader("test_join");
9735
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
9738
rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
9741
rc= mysql_query(mysql,
9742
"insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
9745
rc= mysql_query(mysql, "CREATE TABLE t2 (a int , c int);");
9748
rc= mysql_query(mysql,
9749
"insert into t2 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
9752
for (j= 0; j < 9; j++)
9754
stmt= mysql_simple_prepare(mysql, query[j]);
9756
for (i= 0; i < 3; i++)
9758
rc= mysql_stmt_execute(stmt);
9759
check_execute(stmt, rc);
9760
rc= my_process_stmt_result(stmt);
9761
DIE_UNLESS(rc == 5);
9763
mysql_stmt_close(stmt);
9766
rc= mysql_query(mysql, "DROP TABLE t1, t2");
9771
static void test_selecttmp()
9775
const char *query= "select a, (select count(distinct t1.b) as sum from t1, t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3";
9777
myheader("test_select_tmp");
9779
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3");
9782
rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
9785
rc= mysql_query(mysql, "create table t2 (a int, b int);");
9788
rc= mysql_query(mysql, "create table t3 (a int, b int);");
9791
rc= mysql_query(mysql,
9792
"insert into t1 values (0, 100), (1, 2), (1, 3), (2, 2), (2, 7), \
9793
(2, -1), (3, 10);");
9795
rc= mysql_query(mysql,
9796
"insert into t2 values (0, 0), (1, 1), (2, 1), (3, 1), (4, 1);");
9798
rc= mysql_query(mysql,
9799
"insert into t3 values (3, 3), (2, 2), (1, 1);");
9802
stmt= mysql_simple_prepare(mysql, query);
9804
for (i= 0; i < 3; i++)
9806
rc= mysql_stmt_execute(stmt);
9807
check_execute(stmt, rc);
9808
rc= my_process_stmt_result(stmt);
9809
DIE_UNLESS(rc == 3);
9811
mysql_stmt_close(stmt);
9813
rc= mysql_query(mysql, "DROP TABLE t1, t2, t3");
9818
static void test_create_drop()
9820
MYSQL_STMT *stmt_create, *stmt_drop, *stmt_select, *stmt_create_select;
9823
myheader("test_table_manipulation");
9825
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
9828
rc= mysql_query(mysql, "create table t2 (a int);");
9831
rc= mysql_query(mysql, "create table t1 (a int);");
9834
rc= mysql_query(mysql, "insert into t2 values (3), (2), (1);");
9837
query= (char*)"create table t1 (a int)";
9838
stmt_create= mysql_simple_prepare(mysql, query);
9839
check_stmt(stmt_create);
9841
query= (char*)"drop table t1";
9842
stmt_drop= mysql_simple_prepare(mysql, query);
9843
check_stmt(stmt_drop);
9845
query= (char*)"select a in (select a from t2) from t1";
9846
stmt_select= mysql_simple_prepare(mysql, query);
9847
check_stmt(stmt_select);
9849
rc= mysql_query(mysql, "DROP TABLE t1");
9852
query= (char*)"create table t1 select a from t2";
9853
stmt_create_select= mysql_simple_prepare(mysql, query);
9854
check_stmt(stmt_create_select);
9856
for (i= 0; i < 3; i++)
9858
rc= mysql_stmt_execute(stmt_create);
9859
check_execute(stmt_create, rc);
9861
fprintf(stdout, "created %i\n", i);
9863
rc= mysql_stmt_execute(stmt_select);
9864
check_execute(stmt_select, rc);
9865
rc= my_process_stmt_result(stmt_select);
9866
DIE_UNLESS(rc == 0);
9868
rc= mysql_stmt_execute(stmt_drop);
9869
check_execute(stmt_drop, rc);
9871
fprintf(stdout, "dropped %i\n", i);
9873
rc= mysql_stmt_execute(stmt_create_select);
9874
check_execute(stmt_create, rc);
9876
fprintf(stdout, "created select %i\n", i);
9878
rc= mysql_stmt_execute(stmt_select);
9879
check_execute(stmt_select, rc);
9880
rc= my_process_stmt_result(stmt_select);
9881
DIE_UNLESS(rc == 3);
9883
rc= mysql_stmt_execute(stmt_drop);
9884
check_execute(stmt_drop, rc);
9886
fprintf(stdout, "dropped %i\n", i);
9889
mysql_stmt_close(stmt_create);
9890
mysql_stmt_close(stmt_drop);
9891
mysql_stmt_close(stmt_select);
9892
mysql_stmt_close(stmt_create_select);
9894
rc= mysql_query(mysql, "DROP TABLE t2");
9899
static void test_rename()
9902
const char *query= "rename table t1 to t2, t3 to t4";
9904
myheader("test_table_manipulation");
9906
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
9909
stmt= mysql_simple_prepare(mysql, query);
9912
rc= mysql_query(mysql, "create table t1 (a int)");
9915
rc= mysql_stmt_execute(stmt);
9916
check_execute_r(stmt, rc);
9918
fprintf(stdout, "rename without t3\n");
9920
rc= mysql_query(mysql, "create table t3 (a int)");
9923
rc= mysql_stmt_execute(stmt);
9924
check_execute(stmt, rc);
9926
fprintf(stdout, "rename with t3\n");
9928
rc= mysql_stmt_execute(stmt);
9929
check_execute_r(stmt, rc);
9931
fprintf(stdout, "rename renamed\n");
9933
rc= mysql_query(mysql, "rename table t2 to t1, t4 to t3");
9936
rc= mysql_stmt_execute(stmt);
9937
check_execute(stmt, rc);
9939
fprintf(stdout, "rename reverted\n");
9941
mysql_stmt_close(stmt);
9943
rc= mysql_query(mysql, "DROP TABLE t2, t4");
9948
static void test_do_set()
9950
MYSQL_STMT *stmt_do, *stmt_set;
9953
myheader("test_do_set");
9955
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
9958
rc= mysql_query(mysql, "create table t1 (a int)");
9961
query= (char*)"do @var:=(1 in (select * from t1))";
9962
stmt_do= mysql_simple_prepare(mysql, query);
9963
check_stmt(stmt_do);
9965
query= (char*)"set @var=(1 in (select * from t1))";
9966
stmt_set= mysql_simple_prepare(mysql, query);
9967
check_stmt(stmt_set);
9969
for (i= 0; i < 3; i++)
9971
rc= mysql_stmt_execute(stmt_do);
9972
check_execute(stmt_do, rc);
9974
fprintf(stdout, "do %i\n", i);
9975
rc= mysql_stmt_execute(stmt_set);
9976
check_execute(stmt_set, rc);
9978
fprintf(stdout, "set %i\n", i);
9981
mysql_stmt_close(stmt_do);
9982
mysql_stmt_close(stmt_set);
9986
static void test_multi()
9988
MYSQL_STMT *stmt_delete, *stmt_update, *stmt_select1, *stmt_select2;
9990
MYSQL_BIND my_bind[1];
9994
myheader("test_multi");
9997
We need to bzero bind structure because mysql_stmt_bind_param checks all
10000
bzero((char*) my_bind, sizeof(my_bind));
10002
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
10003
my_bind[0].buffer= (void *)¶m;
10004
my_bind[0].length= &length;
10006
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
10009
rc= mysql_query(mysql, "create table t1 (a int, b int)");
10012
rc= mysql_query(mysql, "create table t2 (a int, b int)");
10015
rc= mysql_query(mysql, "insert into t1 values (3, 3), (2, 2), (1, 1)");
10018
rc= mysql_query(mysql, "insert into t2 values (3, 3), (2, 2), (1, 1)");
10021
query= (char*)"delete t1, t2 from t1, t2 where t1.a=t2.a and t1.b=10";
10022
stmt_delete= mysql_simple_prepare(mysql, query);
10023
check_stmt(stmt_delete);
10025
query= (char*)"update t1, t2 set t1.b=10, t2.b=10 where t1.a=t2.a and t1.b=?";
10026
stmt_update= mysql_simple_prepare(mysql, query);
10027
check_stmt(stmt_update);
10029
query= (char*)"select * from t1";
10030
stmt_select1= mysql_simple_prepare(mysql, query);
10031
check_stmt(stmt_select1);
10033
query= (char*)"select * from t2";
10034
stmt_select2= mysql_simple_prepare(mysql, query);
10035
check_stmt(stmt_select2);
10037
for(i= 0; i < 3; i++)
10039
rc= mysql_stmt_bind_param(stmt_update, my_bind);
10040
check_execute(stmt_update, rc);
10042
rc= mysql_stmt_execute(stmt_update);
10043
check_execute(stmt_update, rc);
10045
fprintf(stdout, "update %ld\n", (long) param);
10047
rc= mysql_stmt_execute(stmt_delete);
10048
check_execute(stmt_delete, rc);
10050
fprintf(stdout, "delete %ld\n", (long) param);
10052
rc= mysql_stmt_execute(stmt_select1);
10053
check_execute(stmt_select1, rc);
10054
rc= my_process_stmt_result(stmt_select1);
10055
DIE_UNLESS(rc == 3-param);
10057
rc= mysql_stmt_execute(stmt_select2);
10058
check_execute(stmt_select2, rc);
10059
rc= my_process_stmt_result(stmt_select2);
10060
DIE_UNLESS(rc == 3-param);
10065
mysql_stmt_close(stmt_delete);
10066
mysql_stmt_close(stmt_update);
10067
mysql_stmt_close(stmt_select1);
10068
mysql_stmt_close(stmt_select2);
10069
rc= mysql_query(mysql, "drop table t1, t2");
10074
static void test_insert_select()
10076
MYSQL_STMT *stmt_insert, *stmt_select;
10080
myheader("test_insert_select");
10082
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
10085
rc= mysql_query(mysql, "create table t1 (a int)");
10088
rc= mysql_query(mysql, "create table t2 (a int)");
10091
rc= mysql_query(mysql, "insert into t2 values (1)");
10094
query= (char*)"insert into t1 select a from t2";
10095
stmt_insert= mysql_simple_prepare(mysql, query);
10096
check_stmt(stmt_insert);
10098
query= (char*)"select * from t1";
10099
stmt_select= mysql_simple_prepare(mysql, query);
10100
check_stmt(stmt_select);
10102
for(i= 0; i < 3; i++)
10104
rc= mysql_stmt_execute(stmt_insert);
10105
check_execute(stmt_insert, rc);
10107
fprintf(stdout, "insert %u\n", i);
10109
rc= mysql_stmt_execute(stmt_select);
10110
check_execute(stmt_select, rc);
10111
rc= my_process_stmt_result(stmt_select);
10112
DIE_UNLESS(rc == (int)(i+1));
10115
mysql_stmt_close(stmt_insert);
10116
mysql_stmt_close(stmt_select);
10117
rc= mysql_query(mysql, "drop table t1, t2");
10122
static void test_bind_nagative()
10124
MYSQL_STMT *stmt_insert;
10127
MYSQL_BIND my_bind[1];
10129
ulong my_length= 0L;
10130
my_bool my_null= FALSE;
10131
myheader("test_insert_select");
10133
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
10136
rc= mysql_query(mysql, "create temporary table t1 (c1 int unsigned)");
10139
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1), (-1)");
10142
query= (char*)"INSERT INTO t1 VALUES (?)";
10143
stmt_insert= mysql_simple_prepare(mysql, query);
10144
check_stmt(stmt_insert);
10146
/* bind parameters */
10147
bzero((char*) my_bind, sizeof(my_bind));
10149
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
10150
my_bind[0].buffer= (void *)&my_val;
10151
my_bind[0].length= &my_length;
10152
my_bind[0].is_null= (char*)&my_null;
10154
rc= mysql_stmt_bind_param(stmt_insert, my_bind);
10155
check_execute(stmt_insert, rc);
10158
rc= mysql_stmt_execute(stmt_insert);
10159
check_execute(stmt_insert, rc);
10161
mysql_stmt_close(stmt_insert);
10162
rc= mysql_query(mysql, "drop table t1");
10167
static void test_derived()
10171
MYSQL_BIND my_bind[1];
10173
ulong my_length= 0L;
10174
my_bool my_null= FALSE;
10176
"select count(1) from (select f.id from t1 f where f.id=?) as x";
10178
myheader("test_derived");
10180
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
10183
rc= mysql_query(mysql, "create table t1 (id int(8), primary key (id)) \
10184
ENGINE=InnoDB DEFAULT CHARSET=utf8");
10187
rc= mysql_query(mysql, "insert into t1 values (1)");
10190
stmt= mysql_simple_prepare(mysql, query);
10193
We need to bzero bind structure because mysql_stmt_bind_param checks all
10196
bzero((char*) my_bind, sizeof(my_bind));
10198
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
10199
my_bind[0].buffer= (void *)&my_val;
10200
my_bind[0].length= &my_length;
10201
my_bind[0].is_null= (char*)&my_null;
10203
rc= mysql_stmt_bind_param(stmt, my_bind);
10204
check_execute(stmt, rc);
10206
for (i= 0; i < 3; i++)
10208
rc= mysql_stmt_execute(stmt);
10209
check_execute(stmt, rc);
10210
rc= my_process_stmt_result(stmt);
10211
DIE_UNLESS(rc == 1);
10213
mysql_stmt_close(stmt);
10215
rc= mysql_query(mysql, "DROP TABLE t1");
10220
static void test_xjoin()
10225
"select t.id, p1.value, n1.value, p2.value, n2.value from t3 t LEFT JOIN t1 p1 ON (p1.id=t.param1_id) LEFT JOIN t2 p2 ON (p2.id=t.param2_id) LEFT JOIN t4 n1 ON (n1.id=p1.name_id) LEFT JOIN t4 n2 ON (n2.id=p2.name_id) where t.id=1";
10227
myheader("test_xjoin");
10229
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
10232
rc= mysql_query(mysql, "create table t3 (id int(8), param1_id int(8), param2_id int(8)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
10235
rc= mysql_query(mysql, "create table t1 ( id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
10238
rc= mysql_query(mysql, "create table t2 (id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
10241
rc= mysql_query(mysql, "create table t4(id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
10244
rc= mysql_query(mysql, "insert into t3 values (1, 1, 1), (2, 2, null)");
10247
rc= mysql_query(mysql, "insert into t1 values (1, 1, 'aaa'), (2, null, 'bbb')");
10250
rc= mysql_query(mysql, "insert into t2 values (1, 2, 'ccc')");
10253
rc= mysql_query(mysql, "insert into t4 values (1, 'Name1'), (2, null)");
10256
stmt= mysql_simple_prepare(mysql, query);
10259
for (i= 0; i < 3; i++)
10261
rc= mysql_stmt_execute(stmt);
10262
check_execute(stmt, rc);
10263
rc= my_process_stmt_result(stmt);
10264
DIE_UNLESS(rc == 1);
10266
mysql_stmt_close(stmt);
10268
rc= mysql_query(mysql, "DROP TABLE t1, t2, t3, t4");
10273
static void test_bug3035()
10277
MYSQL_BIND bind_array[12], *my_bind= bind_array, *bind_end= my_bind + 12;
10284
longlong int64_val;
10285
ulonglong uint64_val;
10286
double double_val, udouble_val, double_tmp;
10287
char longlong_as_string[22], ulonglong_as_string[22];
10289
/* mins and maxes */
10290
const int8 int8_min= -128;
10291
const int8 int8_max= 127;
10292
const uint8 uint8_min= 0;
10293
const uint8 uint8_max= 255;
10295
const int16 int16_min= -32768;
10296
const int16 int16_max= 32767;
10297
const uint16 uint16_min= 0;
10298
const uint16 uint16_max= 65535;
10300
const int32 int32_max= 2147483647L;
10301
const int32 int32_min= -int32_max - 1;
10302
const uint32 uint32_min= 0;
10303
const uint32 uint32_max= 4294967295U;
10305
/* it might not work okay everyplace */
10306
const longlong int64_max= LL(9223372036854775807);
10307
const longlong int64_min= -int64_max - 1;
10309
const ulonglong uint64_min= 0U;
10310
const ulonglong uint64_max= ULL(18446744073709551615);
10312
const char *stmt_text;
10314
myheader("test_bug3035");
10316
stmt_text= "DROP TABLE IF EXISTS t1";
10317
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10320
stmt_text= "CREATE TABLE t1 (i8 TINYINT, ui8 TINYINT UNSIGNED, "
10321
"i16 SMALLINT, ui16 SMALLINT UNSIGNED, "
10322
"i32 INT, ui32 INT UNSIGNED, "
10323
"i64 BIGINT, ui64 BIGINT UNSIGNED, "
10324
"id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT)";
10325
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10328
bzero((char*) bind_array, sizeof(bind_array));
10330
for (my_bind= bind_array; my_bind < bind_end; my_bind++)
10331
my_bind->error= &my_bind->error_value;
10333
bind_array[0].buffer_type= MYSQL_TYPE_TINY;
10334
bind_array[0].buffer= (void *) &int8_val;
10336
bind_array[1].buffer_type= MYSQL_TYPE_TINY;
10337
bind_array[1].buffer= (void *) &uint8_val;
10338
bind_array[1].is_unsigned= 1;
10340
bind_array[2].buffer_type= MYSQL_TYPE_SHORT;
10341
bind_array[2].buffer= (void *) &int16_val;
10343
bind_array[3].buffer_type= MYSQL_TYPE_SHORT;
10344
bind_array[3].buffer= (void *) &uint16_val;
10345
bind_array[3].is_unsigned= 1;
10347
bind_array[4].buffer_type= MYSQL_TYPE_LONG;
10348
bind_array[4].buffer= (void *) &int32_val;
10350
bind_array[5].buffer_type= MYSQL_TYPE_LONG;
10351
bind_array[5].buffer= (void *) &uint32_val;
10352
bind_array[5].is_unsigned= 1;
10354
bind_array[6].buffer_type= MYSQL_TYPE_LONGLONG;
10355
bind_array[6].buffer= (void *) &int64_val;
10357
bind_array[7].buffer_type= MYSQL_TYPE_LONGLONG;
10358
bind_array[7].buffer= (void *) &uint64_val;
10359
bind_array[7].is_unsigned= 1;
10361
stmt= mysql_stmt_init(mysql);
10364
stmt_text= "INSERT INTO t1 (i8, ui8, i16, ui16, i32, ui32, i64, ui64) "
10365
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
10366
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10367
check_execute(stmt, rc);
10369
mysql_stmt_bind_param(stmt, bind_array);
10371
int8_val= int8_min;
10372
uint8_val= uint8_min;
10373
int16_val= int16_min;
10374
uint16_val= uint16_min;
10375
int32_val= int32_min;
10376
uint32_val= uint32_min;
10377
int64_val= int64_min;
10378
uint64_val= uint64_min;
10380
rc= mysql_stmt_execute(stmt);
10381
check_execute(stmt, rc);
10383
int8_val= int8_max;
10384
uint8_val= uint8_max;
10385
int16_val= int16_max;
10386
uint16_val= uint16_max;
10387
int32_val= int32_max;
10388
uint32_val= uint32_max;
10389
int64_val= int64_max;
10390
uint64_val= uint64_max;
10392
rc= mysql_stmt_execute(stmt);
10393
check_execute(stmt, rc);
10395
stmt_text= "SELECT i8, ui8, i16, ui16, i32, ui32, i64, ui64, ui64, "
10396
"cast(ui64 as signed), ui64, cast(ui64 as signed)"
10397
"FROM t1 ORDER BY id ASC";
10399
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10400
check_execute(stmt, rc);
10402
rc= mysql_stmt_execute(stmt);
10403
check_execute(stmt, rc);
10405
bind_array[8].buffer_type= MYSQL_TYPE_DOUBLE;
10406
bind_array[8].buffer= (void *) &udouble_val;
10408
bind_array[9].buffer_type= MYSQL_TYPE_DOUBLE;
10409
bind_array[9].buffer= (void *) &double_val;
10411
bind_array[10].buffer_type= MYSQL_TYPE_STRING;
10412
bind_array[10].buffer= (void *) &ulonglong_as_string;
10413
bind_array[10].buffer_length= sizeof(ulonglong_as_string);
10415
bind_array[11].buffer_type= MYSQL_TYPE_STRING;
10416
bind_array[11].buffer= (void *) &longlong_as_string;
10417
bind_array[11].buffer_length= sizeof(longlong_as_string);
10419
mysql_stmt_bind_result(stmt, bind_array);
10421
rc= mysql_stmt_fetch(stmt);
10422
check_execute(stmt, rc);
10424
DIE_UNLESS(int8_val == int8_min);
10425
DIE_UNLESS(uint8_val == uint8_min);
10426
DIE_UNLESS(int16_val == int16_min);
10427
DIE_UNLESS(uint16_val == uint16_min);
10428
DIE_UNLESS(int32_val == int32_min);
10429
DIE_UNLESS(uint32_val == uint32_min);
10430
DIE_UNLESS(int64_val == int64_min);
10431
DIE_UNLESS(uint64_val == uint64_min);
10432
DIE_UNLESS(double_val == (longlong) uint64_min);
10433
double_tmp= ulonglong2double(uint64_val);
10434
DIE_UNLESS(cmp_double(&udouble_val, &double_tmp));
10435
DIE_UNLESS(!strcmp(longlong_as_string, "0"));
10436
DIE_UNLESS(!strcmp(ulonglong_as_string, "0"));
10438
rc= mysql_stmt_fetch(stmt);
10442
printf("Truncation mask: ");
10443
for (my_bind= bind_array; my_bind < bind_end; my_bind++)
10444
printf("%d", (int) my_bind->error_value);
10447
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED || rc == 0);
10449
DIE_UNLESS(int8_val == int8_max);
10450
DIE_UNLESS(uint8_val == uint8_max);
10451
DIE_UNLESS(int16_val == int16_max);
10452
DIE_UNLESS(uint16_val == uint16_max);
10453
DIE_UNLESS(int32_val == int32_max);
10454
DIE_UNLESS(uint32_val == uint32_max);
10455
DIE_UNLESS(int64_val == int64_max);
10456
DIE_UNLESS(uint64_val == uint64_max);
10457
DIE_UNLESS(double_val == (longlong) uint64_val);
10458
double_tmp= ulonglong2double(uint64_val);
10459
DIE_UNLESS(cmp_double(&udouble_val, &double_tmp));
10460
DIE_UNLESS(!strcmp(longlong_as_string, "-1"));
10461
DIE_UNLESS(!strcmp(ulonglong_as_string, "18446744073709551615"));
10463
rc= mysql_stmt_fetch(stmt);
10464
DIE_UNLESS(rc == MYSQL_NO_DATA);
10466
mysql_stmt_close(stmt);
10468
stmt_text= "DROP TABLE t1";
10469
mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10473
static void test_union2()
10478
myheader("test_union2");
10480
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
10483
rc= mysql_query(mysql, "CREATE TABLE t1(col1 INT, \
10484
col2 VARCHAR(40), \
10489
stmt= mysql_simple_prepare(mysql,
10490
"select col1 FROM t1 where col1=1 union distinct "
10491
"select col1 FROM t1 where col1=2");
10494
for (i= 0; i < 3; i++)
10496
rc= mysql_stmt_execute(stmt);
10497
check_execute(stmt, rc);
10498
rc= my_process_stmt_result(stmt);
10499
DIE_UNLESS(rc == 0);
10502
mysql_stmt_close(stmt);
10504
rc= mysql_query(mysql, "DROP TABLE t1");
10510
This tests for various mysql_stmt_send_long_data bugs described in #1664
10513
static void test_bug1664()
10518
const char *str_data= "Simple string";
10519
MYSQL_BIND my_bind[2];
10520
const char *query= "INSERT INTO test_long_data(col2, col1) VALUES(?, ?)";
10522
myheader("test_bug1664");
10524
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
10527
rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, col2 long varchar)");
10530
stmt= mysql_stmt_init(mysql);
10532
rc= mysql_stmt_prepare(stmt, query, strlen(query));
10533
check_execute(stmt, rc);
10535
verify_param_count(stmt, 2);
10537
bzero((char*) my_bind, sizeof(my_bind));
10539
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
10540
my_bind[0].buffer= (void *)str_data;
10541
my_bind[0].buffer_length= strlen(str_data);
10543
my_bind[1].buffer= (void *)&int_data;
10544
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
10546
rc= mysql_stmt_bind_param(stmt, my_bind);
10547
check_execute(stmt, rc);
10552
Let us supply empty long_data. This should work and should
10553
not break following execution.
10556
rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
10557
check_execute(stmt, rc);
10559
rc= mysql_stmt_execute(stmt);
10560
check_execute(stmt, rc);
10562
verify_col_data("test_long_data", "col1", "1");
10563
verify_col_data("test_long_data", "col2", "");
10565
rc= mysql_query(mysql, "DELETE FROM test_long_data");
10568
/* This should pass OK */
10569
data= (char *)"Data";
10570
rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
10571
check_execute(stmt, rc);
10573
rc= mysql_stmt_execute(stmt);
10574
check_execute(stmt, rc);
10576
verify_col_data("test_long_data", "col1", "1");
10577
verify_col_data("test_long_data", "col2", "Data");
10580
rc= mysql_query(mysql, "DELETE FROM test_long_data");
10584
Now we are changing int parameter and don't do anything
10585
with first parameter. Second mysql_stmt_execute() should run
10586
OK treating this first parameter as string parameter.
10591
rc= mysql_stmt_execute(stmt);
10592
check_execute(stmt, rc);
10594
verify_col_data("test_long_data", "col1", "2");
10595
verify_col_data("test_long_data", "col2", str_data);
10598
rc= mysql_query(mysql, "DELETE FROM test_long_data");
10602
Now we are sending other long data. It should not be
10603
concatened to previous.
10606
data= (char *)"SomeOtherData";
10607
rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
10608
check_execute(stmt, rc);
10610
rc= mysql_stmt_execute(stmt);
10611
check_execute(stmt, rc);
10613
verify_col_data("test_long_data", "col1", "2");
10614
verify_col_data("test_long_data", "col2", "SomeOtherData");
10616
mysql_stmt_close(stmt);
10619
rc= mysql_query(mysql, "DELETE FROM test_long_data");
10622
/* Now let us test how mysql_stmt_reset works. */
10623
stmt= mysql_stmt_init(mysql);
10625
rc= mysql_stmt_prepare(stmt, query, strlen(query));
10626
check_execute(stmt, rc);
10627
rc= mysql_stmt_bind_param(stmt, my_bind);
10628
check_execute(stmt, rc);
10630
data= (char *)"SomeData";
10631
rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
10632
check_execute(stmt, rc);
10634
rc= mysql_stmt_reset(stmt);
10635
check_execute(stmt, rc);
10637
rc= mysql_stmt_execute(stmt);
10638
check_execute(stmt, rc);
10640
verify_col_data("test_long_data", "col1", "2");
10641
verify_col_data("test_long_data", "col2", str_data);
10643
mysql_stmt_close(stmt);
10645
/* Final clean up */
10646
rc= mysql_query(mysql, "DROP TABLE test_long_data");
10651
static void test_order_param()
10656
myheader("test_order_param");
10658
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
10661
rc= mysql_query(mysql, "CREATE TABLE t1(a INT, b char(10))");
10664
stmt= mysql_simple_prepare(mysql,
10665
"select sum(a) + 200, 1 from t1 "
10667
"select sum(a) + 200, 1 from t1 group by b ");
10669
mysql_stmt_close(stmt);
10671
stmt= mysql_simple_prepare(mysql,
10672
"select sum(a) + 200, ? from t1 group by b "
10674
"select sum(a) + 200, 1 from t1 group by b ");
10676
mysql_stmt_close(stmt);
10678
stmt= mysql_simple_prepare(mysql,
10679
"select sum(a) + 200, ? from t1 "
10681
"select sum(a) + 200, 1 from t1 group by b ");
10683
mysql_stmt_close(stmt);
10685
rc= mysql_query(mysql, "DROP TABLE t1");
10690
static void test_union_param()
10695
MYSQL_BIND my_bind[2];
10697
ulong my_length= 3L;
10698
my_bool my_null= FALSE;
10699
myheader("test_union_param");
10701
strmov(my_val, "abc");
10703
query= (char*)"select ? as my_col union distinct select ?";
10704
stmt= mysql_simple_prepare(mysql, query);
10708
We need to bzero bind structure because mysql_stmt_bind_param checks all
10711
bzero((char*) my_bind, sizeof(my_bind));
10713
/* bind parameters */
10714
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
10715
my_bind[0].buffer= (char*) &my_val;
10716
my_bind[0].buffer_length= 4;
10717
my_bind[0].length= &my_length;
10718
my_bind[0].is_null= (char*)&my_null;
10719
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
10720
my_bind[1].buffer= (char*) &my_val;
10721
my_bind[1].buffer_length= 4;
10722
my_bind[1].length= &my_length;
10723
my_bind[1].is_null= (char*)&my_null;
10725
rc= mysql_stmt_bind_param(stmt, my_bind);
10726
check_execute(stmt, rc);
10728
for (i= 0; i < 3; i++)
10730
rc= mysql_stmt_execute(stmt);
10731
check_execute(stmt, rc);
10732
rc= my_process_stmt_result(stmt);
10733
DIE_UNLESS(rc == 1);
10736
mysql_stmt_close(stmt);
10740
static void test_ps_i18n()
10744
const char *stmt_text;
10745
MYSQL_BIND bind_array[2];
10747
/* Represented as numbers to keep UTF8 tools from clobbering them. */
10748
const char *koi8= "\xee\xd5\x2c\x20\xda\xc1\x20\xd2\xd9\xc2\xc1\xcc\xcb\xd5";
10749
const char *cp1251= "\xcd\xf3\x2c\x20\xe7\xe0\x20\xf0\xfb\xe1\xe0\xeb\xea\xf3";
10750
char buf1[16], buf2[16];
10751
ulong buf1_len, buf2_len;
10754
myheader("test_ps_i18n");
10756
stmt_text= "DROP TABLE IF EXISTS t1";
10757
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10761
Create table with binary columns, set session character set to cp1251,
10762
client character set to koi8, and make sure that there is conversion
10763
on insert and no conversion on select
10766
stmt_text= "CREATE TABLE t1 (c1 VARBINARY(255), c2 VARBINARY(255))";
10768
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10771
stmt_text= "SET CHARACTER_SET_CLIENT=koi8r, "
10772
"CHARACTER_SET_CONNECTION=cp1251, "
10773
"CHARACTER_SET_RESULTS=koi8r";
10775
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10778
bzero((char*) bind_array, sizeof(bind_array));
10780
bind_array[0].buffer_type= MYSQL_TYPE_STRING;
10781
bind_array[0].buffer= (void *) koi8;
10782
bind_array[0].buffer_length= strlen(koi8);
10784
bind_array[1].buffer_type= MYSQL_TYPE_STRING;
10785
bind_array[1].buffer= (void *) koi8;
10786
bind_array[1].buffer_length= strlen(koi8);
10788
stmt= mysql_stmt_init(mysql);
10791
stmt_text= "INSERT INTO t1 (c1, c2) VALUES (?, ?)";
10793
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10794
check_execute(stmt, rc);
10796
mysql_stmt_bind_param(stmt, bind_array);
10798
mysql_stmt_send_long_data(stmt, 0, koi8, strlen(koi8));
10800
rc= mysql_stmt_execute(stmt);
10801
check_execute(stmt, rc);
10803
stmt_text= "SELECT c1, c2 FROM t1";
10805
/* c1 and c2 are binary so no conversion will be done on select */
10806
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10807
check_execute(stmt, rc);
10809
rc= mysql_stmt_execute(stmt);
10810
check_execute(stmt, rc);
10812
bind_array[0].buffer= buf1;
10813
bind_array[0].buffer_length= sizeof(buf1);
10814
bind_array[0].length= &buf1_len;
10816
bind_array[1].buffer= buf2;
10817
bind_array[1].buffer_length= sizeof(buf2);
10818
bind_array[1].length= &buf2_len;
10820
mysql_stmt_bind_result(stmt, bind_array);
10822
rc= mysql_stmt_fetch(stmt);
10823
check_execute(stmt, rc);
10825
DIE_UNLESS(buf1_len == strlen(cp1251));
10826
DIE_UNLESS(buf2_len == strlen(cp1251));
10827
DIE_UNLESS(!memcmp(buf1, cp1251, buf1_len));
10828
DIE_UNLESS(!memcmp(buf2, cp1251, buf1_len));
10830
rc= mysql_stmt_fetch(stmt);
10831
DIE_UNLESS(rc == MYSQL_NO_DATA);
10833
stmt_text= "DROP TABLE IF EXISTS t1";
10834
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10838
Now create table with two cp1251 columns, set client character
10839
set to koi8 and supply columns of one row as string and another as
10840
binary data. Binary data must not be converted on insert, and both
10841
columns must be converted to client character set on select.
10844
stmt_text= "CREATE TABLE t1 (c1 VARCHAR(255) CHARACTER SET cp1251, "
10845
"c2 VARCHAR(255) CHARACTER SET cp1251)";
10847
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10850
stmt_text= "INSERT INTO t1 (c1, c2) VALUES (?, ?)";
10852
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10853
check_execute(stmt, rc);
10855
/* this data must be converted */
10856
bind_array[0].buffer_type= MYSQL_TYPE_STRING;
10857
bind_array[0].buffer= (void *) koi8;
10858
bind_array[0].buffer_length= strlen(koi8);
10860
bind_array[1].buffer_type= MYSQL_TYPE_STRING;
10861
bind_array[1].buffer= (void *) koi8;
10862
bind_array[1].buffer_length= strlen(koi8);
10864
mysql_stmt_bind_param(stmt, bind_array);
10866
mysql_stmt_send_long_data(stmt, 0, koi8, strlen(koi8));
10868
rc= mysql_stmt_execute(stmt);
10869
check_execute(stmt, rc);
10871
/* this data must not be converted */
10872
bind_array[0].buffer_type= MYSQL_TYPE_BLOB;
10873
bind_array[0].buffer= (void *) cp1251;
10874
bind_array[0].buffer_length= strlen(cp1251);
10876
bind_array[1].buffer_type= MYSQL_TYPE_BLOB;
10877
bind_array[1].buffer= (void *) cp1251;
10878
bind_array[1].buffer_length= strlen(cp1251);
10880
mysql_stmt_bind_param(stmt, bind_array);
10882
mysql_stmt_send_long_data(stmt, 0, cp1251, strlen(cp1251));
10884
rc= mysql_stmt_execute(stmt);
10885
check_execute(stmt, rc);
10887
/* Fetch data and verify that rows are in koi8 */
10889
stmt_text= "SELECT c1, c2 FROM t1";
10891
/* c1 and c2 are binary so no conversion will be done on select */
10892
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10893
check_execute(stmt, rc);
10895
rc= mysql_stmt_execute(stmt);
10896
check_execute(stmt, rc);
10898
bind_array[0].buffer= buf1;
10899
bind_array[0].buffer_length= sizeof(buf1);
10900
bind_array[0].length= &buf1_len;
10902
bind_array[1].buffer= buf2;
10903
bind_array[1].buffer_length= sizeof(buf2);
10904
bind_array[1].length= &buf2_len;
10906
mysql_stmt_bind_result(stmt, bind_array);
10908
while ((rc= mysql_stmt_fetch(stmt)) == 0)
10910
DIE_UNLESS(buf1_len == strlen(koi8));
10911
DIE_UNLESS(buf2_len == strlen(koi8));
10912
DIE_UNLESS(!memcmp(buf1, koi8, buf1_len));
10913
DIE_UNLESS(!memcmp(buf2, koi8, buf1_len));
10915
DIE_UNLESS(rc == MYSQL_NO_DATA);
10916
mysql_stmt_close(stmt);
10918
stmt_text= "DROP TABLE t1";
10919
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10921
stmt_text= "SET NAMES DEFAULT";
10922
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10927
static void test_bug3796()
10930
MYSQL_BIND my_bind[1];
10931
const char *concat_arg0= "concat_with_";
10932
enum { OUT_BUFF_SIZE= 30 };
10933
char out_buff[OUT_BUFF_SIZE];
10934
char canonical_buff[OUT_BUFF_SIZE];
10936
const char *stmt_text;
10939
myheader("test_bug3796");
10941
/* Create and fill test table */
10942
stmt_text= "DROP TABLE IF EXISTS t1";
10943
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10946
stmt_text= "CREATE TABLE t1 (a INT, b VARCHAR(30))";
10947
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10950
stmt_text= "INSERT INTO t1 VALUES(1, 'ONE'), (2, 'TWO')";
10951
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10954
/* Create statement handle and prepare it with select */
10955
stmt= mysql_stmt_init(mysql);
10956
stmt_text= "SELECT concat(?, b) FROM t1";
10958
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10959
check_execute(stmt, rc);
10961
/* Bind input buffers */
10962
bzero((char*) my_bind, sizeof(my_bind));
10964
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
10965
my_bind[0].buffer= (void *) concat_arg0;
10966
my_bind[0].buffer_length= strlen(concat_arg0);
10968
mysql_stmt_bind_param(stmt, my_bind);
10970
/* Execute the select statement */
10971
rc= mysql_stmt_execute(stmt);
10972
check_execute(stmt, rc);
10974
my_bind[0].buffer= (void *) out_buff;
10975
my_bind[0].buffer_length= OUT_BUFF_SIZE;
10976
my_bind[0].length= &out_length;
10978
mysql_stmt_bind_result(stmt, my_bind);
10980
rc= mysql_stmt_fetch(stmt);
10982
printf("Concat result: '%s'\n", out_buff);
10983
check_execute(stmt, rc);
10984
strmov(canonical_buff, concat_arg0);
10985
strcat(canonical_buff, "ONE");
10986
DIE_UNLESS(strlen(canonical_buff) == out_length &&
10987
strncmp(out_buff, canonical_buff, out_length) == 0);
10989
rc= mysql_stmt_fetch(stmt);
10990
check_execute(stmt, rc);
10991
strmov(canonical_buff + strlen(concat_arg0), "TWO");
10992
DIE_UNLESS(strlen(canonical_buff) == out_length &&
10993
strncmp(out_buff, canonical_buff, out_length) == 0);
10995
printf("Concat result: '%s'\n", out_buff);
10997
rc= mysql_stmt_fetch(stmt);
10998
DIE_UNLESS(rc == MYSQL_NO_DATA);
11000
mysql_stmt_close(stmt);
11002
stmt_text= "DROP TABLE IF EXISTS t1";
11003
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11008
static void test_bug4026()
11011
MYSQL_BIND my_bind[2];
11012
MYSQL_TIME time_in, time_out;
11013
MYSQL_TIME datetime_in, datetime_out;
11014
const char *stmt_text;
11017
myheader("test_bug4026");
11019
/* Check that microseconds are inserted and selected successfully */
11021
/* Create a statement handle and prepare it with select */
11022
stmt= mysql_stmt_init(mysql);
11023
stmt_text= "SELECT ?, ?";
11025
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11026
check_execute(stmt, rc);
11028
/* Bind input buffers */
11029
bzero((char*) my_bind, sizeof(my_bind));
11030
bzero((char*) &time_in, sizeof(time_in));
11031
bzero((char*) &time_out, sizeof(time_out));
11032
bzero((char*) &datetime_in, sizeof(datetime_in));
11033
bzero((char*) &datetime_out, sizeof(datetime_out));
11035
my_bind[0].buffer_type= MYSQL_TYPE_TIME;
11036
my_bind[0].buffer= (void *) &time_in;
11037
my_bind[1].buffer_type= MYSQL_TYPE_DATETIME;
11038
my_bind[1].buffer= (void *) &datetime_in;
11041
time_in.minute= 59;
11042
time_in.second= 59;
11043
time_in.second_part= 123456;
11045
This is not necessary, just to make DIE_UNLESS below work: this field
11046
is filled in when time is received from server
11048
time_in.time_type= MYSQL_TIMESTAMP_TIME;
11050
datetime_in= time_in;
11051
datetime_in.year= 2003;
11052
datetime_in.month= 12;
11053
datetime_in.day= 31;
11054
datetime_in.time_type= MYSQL_TIMESTAMP_DATETIME;
11056
mysql_stmt_bind_param(stmt, my_bind);
11058
/* Execute the select statement */
11059
rc= mysql_stmt_execute(stmt);
11060
check_execute(stmt, rc);
11062
my_bind[0].buffer= (void *) &time_out;
11063
my_bind[1].buffer= (void *) &datetime_out;
11065
mysql_stmt_bind_result(stmt, my_bind);
11067
rc= mysql_stmt_fetch(stmt);
11068
DIE_UNLESS(rc == 0);
11071
printf("%d:%d:%d.%lu\n", time_out.hour, time_out.minute, time_out.second,
11072
time_out.second_part);
11073
printf("%d-%d-%d %d:%d:%d.%lu\n", datetime_out.year, datetime_out.month,
11074
datetime_out.day, datetime_out.hour,
11075
datetime_out.minute, datetime_out.second,
11076
datetime_out.second_part);
11078
DIE_UNLESS(memcmp(&time_in, &time_out, sizeof(time_in)) == 0);
11079
DIE_UNLESS(memcmp(&datetime_in, &datetime_out, sizeof(datetime_in)) == 0);
11080
mysql_stmt_close(stmt);
11084
static void test_bug4079()
11087
MYSQL_BIND my_bind[1];
11088
const char *stmt_text;
11092
myheader("test_bug4079");
11094
/* Create and fill table */
11095
mysql_query(mysql, "DROP TABLE IF EXISTS t1");
11096
mysql_query(mysql, "CREATE TABLE t1 (a int)");
11097
mysql_query(mysql, "INSERT INTO t1 VALUES (1), (2)");
11099
/* Prepare erroneous statement */
11100
stmt= mysql_stmt_init(mysql);
11101
stmt_text= "SELECT 1 < (SELECT a FROM t1)";
11103
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11104
check_execute(stmt, rc);
11106
/* Execute the select statement */
11107
rc= mysql_stmt_execute(stmt);
11108
check_execute(stmt, rc);
11110
/* Bind input buffers */
11111
bzero((char*) my_bind, sizeof(my_bind));
11113
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
11114
my_bind[0].buffer= (void *) &res;
11116
mysql_stmt_bind_result(stmt, my_bind);
11118
rc= mysql_stmt_fetch(stmt);
11119
DIE_UNLESS(rc != 0 && rc != MYSQL_NO_DATA);
11121
printf("Got error from mysql_stmt_fetch (as expected):\n%s\n",
11122
mysql_stmt_error(stmt));
11123
/* buggy version of libmysql hanged up here */
11124
mysql_stmt_close(stmt);
11128
static void test_bug4236()
11131
const char *stmt_text;
11135
myheader("test_bug4296");
11137
stmt= mysql_stmt_init(mysql);
11139
/* mysql_stmt_execute() of statement with statement id= 0 crashed server */
11140
stmt_text= "SELECT 1";
11141
/* We need to prepare statement to pass by possible check in libmysql */
11142
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11143
check_execute(stmt, rc);
11144
/* Hack to check that server works OK if statement wasn't found */
11145
backup.stmt_id= stmt->stmt_id;
11147
rc= mysql_stmt_execute(stmt);
11149
/* Restore original statement id to be able to reprepare it */
11150
stmt->stmt_id= backup.stmt_id;
11152
mysql_stmt_close(stmt);
11156
static void test_bug4030()
11159
MYSQL_BIND my_bind[3];
11160
MYSQL_TIME time_canonical, time_out;
11161
MYSQL_TIME date_canonical, date_out;
11162
MYSQL_TIME datetime_canonical, datetime_out;
11163
const char *stmt_text;
11166
myheader("test_bug4030");
11168
/* Check that microseconds are inserted and selected successfully */
11170
/* Execute a query with time values in prepared mode */
11171
stmt= mysql_stmt_init(mysql);
11172
stmt_text= "SELECT '23:59:59.123456', '2003-12-31', "
11173
"'2003-12-31 23:59:59.123456'";
11174
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11175
check_execute(stmt, rc);
11176
rc= mysql_stmt_execute(stmt);
11177
check_execute(stmt, rc);
11179
/* Bind output buffers */
11180
bzero((char*) my_bind, sizeof(my_bind));
11181
bzero((char*) &time_canonical, sizeof(time_canonical));
11182
bzero((char*) &time_out, sizeof(time_out));
11183
bzero((char*) &date_canonical, sizeof(date_canonical));
11184
bzero((char*) &date_out, sizeof(date_out));
11185
bzero((char*) &datetime_canonical, sizeof(datetime_canonical));
11186
bzero((char*) &datetime_out, sizeof(datetime_out));
11188
my_bind[0].buffer_type= MYSQL_TYPE_TIME;
11189
my_bind[0].buffer= (void *) &time_out;
11190
my_bind[1].buffer_type= MYSQL_TYPE_DATE;
11191
my_bind[1].buffer= (void *) &date_out;
11192
my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
11193
my_bind[2].buffer= (void *) &datetime_out;
11195
time_canonical.hour= 23;
11196
time_canonical.minute= 59;
11197
time_canonical.second= 59;
11198
time_canonical.second_part= 123456;
11199
time_canonical.time_type= MYSQL_TIMESTAMP_TIME;
11201
date_canonical.year= 2003;
11202
date_canonical.month= 12;
11203
date_canonical.day= 31;
11204
date_canonical.time_type= MYSQL_TIMESTAMP_DATE;
11206
datetime_canonical= time_canonical;
11207
datetime_canonical.year= 2003;
11208
datetime_canonical.month= 12;
11209
datetime_canonical.day= 31;
11210
datetime_canonical.time_type= MYSQL_TIMESTAMP_DATETIME;
11212
mysql_stmt_bind_result(stmt, my_bind);
11214
rc= mysql_stmt_fetch(stmt);
11215
DIE_UNLESS(rc == 0);
11218
printf("%d:%d:%d.%lu\n", time_out.hour, time_out.minute, time_out.second,
11219
time_out.second_part);
11220
printf("%d-%d-%d\n", date_out.year, date_out.month, date_out.day);
11221
printf("%d-%d-%d %d:%d:%d.%lu\n", datetime_out.year, datetime_out.month,
11222
datetime_out.day, datetime_out.hour,
11223
datetime_out.minute, datetime_out.second,
11224
datetime_out.second_part);
11226
DIE_UNLESS(memcmp(&time_canonical, &time_out, sizeof(time_out)) == 0);
11227
DIE_UNLESS(memcmp(&date_canonical, &date_out, sizeof(date_out)) == 0);
11228
DIE_UNLESS(memcmp(&datetime_canonical, &datetime_out, sizeof(datetime_out)) == 0);
11229
mysql_stmt_close(stmt);
11232
static void test_view()
11236
MYSQL_BIND my_bind[1];
11241
"SELECT COUNT(*) FROM v1 WHERE SERVERNAME=?";
11243
myheader("test_view");
11245
rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,t2,t3,v1");
11248
rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1,t2,t3");
11250
rc= mysql_query(mysql,"CREATE TABLE t1 ("
11251
" SERVERGRP varchar(20) NOT NULL default '', "
11252
" DBINSTANCE varchar(20) NOT NULL default '', "
11253
" PRIMARY KEY (SERVERGRP)) "
11254
" CHARSET=latin1 collate=latin1_bin");
11256
rc= mysql_query(mysql,"CREATE TABLE t2 ("
11257
" SERVERNAME varchar(20) NOT NULL, "
11258
" SERVERGRP varchar(20) NOT NULL, "
11259
" PRIMARY KEY (SERVERNAME)) "
11260
" CHARSET=latin1 COLLATE latin1_bin");
11262
rc= mysql_query(mysql,
11263
"CREATE TABLE t3 ("
11264
" SERVERGRP varchar(20) BINARY NOT NULL, "
11265
" TABNAME varchar(30) NOT NULL, MAPSTATE char(1) NOT NULL, "
11266
" ACTSTATE char(1) NOT NULL , "
11267
" LOCAL_NAME varchar(30) NOT NULL, "
11268
" CHG_DATE varchar(8) NOT NULL default '00000000', "
11269
" CHG_TIME varchar(6) NOT NULL default '000000', "
11270
" MXUSER varchar(12) NOT NULL default '', "
11271
" PRIMARY KEY (SERVERGRP, TABNAME, MAPSTATE, ACTSTATE, "
11272
" LOCAL_NAME)) CHARSET=latin1 COLLATE latin1_bin");
11274
rc= mysql_query(mysql,"CREATE VIEW v1 AS select sql_no_cache"
11275
" T0001.SERVERNAME AS SERVERNAME, T0003.TABNAME AS"
11276
" TABNAME,T0003.LOCAL_NAME AS LOCAL_NAME,T0002.DBINSTANCE AS"
11277
" DBINSTANCE from t2 T0001 join t1 T0002 join t3 T0003 where"
11278
" ((T0002.SERVERGRP = T0001.SERVERGRP) and"
11279
" (T0002.SERVERGRP = T0003.SERVERGRP)"
11280
" and (T0003.MAPSTATE = _latin1'A') and"
11281
" (T0003.ACTSTATE = _latin1' '))");
11284
stmt= mysql_stmt_init(mysql);
11285
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11286
check_execute(stmt, rc);
11288
strmov(str_data, "TEST");
11289
bzero((char*) my_bind, sizeof(my_bind));
11290
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
11291
my_bind[0].buffer= (char *)&str_data;
11292
my_bind[0].buffer_length= 50;
11293
my_bind[0].length= &length;
11295
my_bind[0].is_null= (char*)&is_null;
11296
rc= mysql_stmt_bind_param(stmt, my_bind);
11297
check_execute(stmt,rc);
11299
for (i= 0; i < 3; i++)
11301
rc= mysql_stmt_execute(stmt);
11302
check_execute(stmt, rc);
11303
rc= my_process_stmt_result(stmt);
11304
DIE_UNLESS(1 == rc);
11306
mysql_stmt_close(stmt);
11308
rc= mysql_query(mysql, "DROP TABLE t1,t2,t3");
11310
rc= mysql_query(mysql, "DROP VIEW v1");
11315
static void test_view_where()
11320
"select v1.c,v2.c from v1, v2";
11322
myheader("test_view_where");
11324
rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1,v2");
11327
rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,v2,t1");
11329
rc= mysql_query(mysql,"CREATE TABLE t1 (a int, b int)");
11331
rc= mysql_query(mysql,"insert into t1 values (1,2), (1,3), (2,4), (2,5), (3,10)");
11333
rc= mysql_query(mysql,"create view v1 (c) as select b from t1 where a<3");
11335
rc= mysql_query(mysql,"create view v2 (c) as select b from t1 where a>=3");
11338
stmt= mysql_stmt_init(mysql);
11339
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11340
check_execute(stmt, rc);
11342
for (i= 0; i < 3; i++)
11344
rc= mysql_stmt_execute(stmt);
11345
check_execute(stmt, rc);
11346
rc= my_process_stmt_result(stmt);
11347
DIE_UNLESS(4 == rc);
11349
mysql_stmt_close(stmt);
11351
rc= mysql_query(mysql, "DROP TABLE t1");
11353
rc= mysql_query(mysql, "DROP VIEW v1, v2");
11358
static void test_view_2where()
11362
MYSQL_BIND my_bind[8];
11363
char parms[8][100];
11366
"select relid, report, handle, log_group, username, variant, type, "
11367
"version, erfdat, erftime, erfname, aedat, aetime, aename, dependvars, "
11368
"inactive from V_LTDX where mandt = ? and relid = ? and report = ? and "
11369
"handle = ? and log_group = ? and username in ( ? , ? ) and type = ?";
11371
myheader("test_view_2where");
11373
rc= mysql_query(mysql, "DROP TABLE IF EXISTS LTDX");
11375
rc= mysql_query(mysql, "DROP VIEW IF EXISTS V_LTDX");
11377
rc= mysql_query(mysql,
11378
"CREATE TABLE LTDX (MANDT char(3) NOT NULL default '000', "
11379
" RELID char(2) NOT NULL, REPORT varchar(40) NOT NULL,"
11380
" HANDLE varchar(4) NOT NULL, LOG_GROUP varchar(4) NOT NULL,"
11381
" USERNAME varchar(12) NOT NULL,"
11382
" VARIANT varchar(12) NOT NULL,"
11383
" TYPE char(1) NOT NULL, SRTF2 int(11) NOT NULL,"
11384
" VERSION varchar(6) NOT NULL default '000000',"
11385
" ERFDAT varchar(8) NOT NULL default '00000000',"
11386
" ERFTIME varchar(6) NOT NULL default '000000',"
11387
" ERFNAME varchar(12) NOT NULL,"
11388
" AEDAT varchar(8) NOT NULL default '00000000',"
11389
" AETIME varchar(6) NOT NULL default '000000',"
11390
" AENAME varchar(12) NOT NULL,"
11391
" DEPENDVARS varchar(10) NOT NULL,"
11392
" INACTIVE char(1) NOT NULL, CLUSTR smallint(6) NOT NULL,"
11394
" PRIMARY KEY (MANDT, RELID, REPORT, HANDLE, LOG_GROUP, "
11395
"USERNAME, VARIANT, TYPE, SRTF2))"
11396
" CHARSET=latin1 COLLATE latin1_bin");
11398
rc= mysql_query(mysql,
11399
"CREATE VIEW V_LTDX AS select T0001.MANDT AS "
11400
" MANDT,T0001.RELID AS RELID,T0001.REPORT AS "
11401
" REPORT,T0001.HANDLE AS HANDLE,T0001.LOG_GROUP AS "
11402
" LOG_GROUP,T0001.USERNAME AS USERNAME,T0001.VARIANT AS "
11403
" VARIANT,T0001.TYPE AS TYPE,T0001.VERSION AS "
11404
" VERSION,T0001.ERFDAT AS ERFDAT,T0001.ERFTIME AS "
11405
" ERFTIME,T0001.ERFNAME AS ERFNAME,T0001.AEDAT AS "
11406
" AEDAT,T0001.AETIME AS AETIME,T0001.AENAME AS "
11407
" AENAME,T0001.DEPENDVARS AS DEPENDVARS,T0001.INACTIVE AS "
11408
" INACTIVE from LTDX T0001 where (T0001.SRTF2 = 0)");
11410
bzero((char*) my_bind, sizeof(my_bind));
11411
for (i=0; i < 8; i++) {
11412
strmov(parms[i], "1");
11413
my_bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
11414
my_bind[i].buffer = (char *)&parms[i];
11415
my_bind[i].buffer_length = 100;
11416
my_bind[i].is_null = 0;
11417
my_bind[i].length = &length[i];
11420
stmt= mysql_stmt_init(mysql);
11421
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11422
check_execute(stmt, rc);
11424
rc= mysql_stmt_bind_param(stmt, my_bind);
11425
check_execute(stmt,rc);
11427
rc= mysql_stmt_execute(stmt);
11428
check_execute(stmt, rc);
11429
rc= my_process_stmt_result(stmt);
11430
DIE_UNLESS(0 == rc);
11432
mysql_stmt_close(stmt);
11434
rc= mysql_query(mysql, "DROP VIEW V_LTDX");
11436
rc= mysql_query(mysql, "DROP TABLE LTDX");
11441
static void test_view_star()
11445
MYSQL_BIND my_bind[8];
11446
char parms[8][100];
11448
const char *query= "SELECT * FROM vt1 WHERE a IN (?,?)";
11450
myheader("test_view_star");
11452
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, vt1");
11454
rc= mysql_query(mysql, "DROP VIEW IF EXISTS t1, vt1");
11456
rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
11458
rc= mysql_query(mysql, "CREATE VIEW vt1 AS SELECT a FROM t1");
11460
bzero((char*) my_bind, sizeof(my_bind));
11461
for (i= 0; i < 2; i++) {
11462
sprintf((char *)&parms[i], "%d", i);
11463
my_bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
11464
my_bind[i].buffer = (char *)&parms[i];
11465
my_bind[i].buffer_length = 100;
11466
my_bind[i].is_null = 0;
11467
my_bind[i].length = &length[i];
11471
stmt= mysql_stmt_init(mysql);
11472
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11473
check_execute(stmt, rc);
11475
rc= mysql_stmt_bind_param(stmt, my_bind);
11476
check_execute(stmt,rc);
11478
for (i= 0; i < 3; i++)
11480
rc= mysql_stmt_execute(stmt);
11481
check_execute(stmt, rc);
11482
rc= my_process_stmt_result(stmt);
11483
DIE_UNLESS(0 == rc);
11486
mysql_stmt_close(stmt);
11488
rc= mysql_query(mysql, "DROP TABLE t1");
11490
rc= mysql_query(mysql, "DROP VIEW vt1");
11495
static void test_view_insert()
11497
MYSQL_STMT *insert_stmt, *select_stmt;
11499
MYSQL_BIND my_bind[1];
11501
ulong my_length = 0L;
11504
"insert into v1 values (?)";
11506
myheader("test_view_insert");
11508
rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
11510
rc = mysql_query(mysql, "DROP VIEW IF EXISTS t1,v1");
11513
rc= mysql_query(mysql,"create table t1 (a int, primary key (a))");
11516
rc= mysql_query(mysql, "create view v1 as select a from t1 where a>=1");
11519
insert_stmt= mysql_stmt_init(mysql);
11520
rc= mysql_stmt_prepare(insert_stmt, query, strlen(query));
11521
check_execute(insert_stmt, rc);
11522
query= "select * from t1";
11523
select_stmt= mysql_stmt_init(mysql);
11524
rc= mysql_stmt_prepare(select_stmt, query, strlen(query));
11525
check_execute(select_stmt, rc);
11527
bzero((char*) my_bind, sizeof(my_bind));
11528
my_bind[0].buffer_type = MYSQL_TYPE_LONG;
11529
my_bind[0].buffer = (char *)&my_val;
11530
my_bind[0].length = &my_length;
11531
my_bind[0].is_null = (char*)&my_null;
11532
rc= mysql_stmt_bind_param(insert_stmt, my_bind);
11533
check_execute(insert_stmt, rc);
11535
for (i= 0; i < 3; i++)
11540
rc= mysql_stmt_execute(insert_stmt);
11541
check_execute(insert_stmt, rc);
11543
rc= mysql_stmt_execute(select_stmt);
11544
check_execute(select_stmt, rc);
11545
rowcount= (int)my_process_stmt_result(select_stmt);
11546
DIE_UNLESS((i+1) == rowcount);
11548
mysql_stmt_close(insert_stmt);
11549
mysql_stmt_close(select_stmt);
11551
rc= mysql_query(mysql, "DROP VIEW v1");
11553
rc= mysql_query(mysql, "DROP TABLE t1");
11558
static void test_left_join_view()
11563
"select t1.a, v1.x from t1 left join v1 on (t1.a= v1.x);";
11565
myheader("test_left_join_view");
11567
rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
11570
rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1");
11572
rc= mysql_query(mysql,"CREATE TABLE t1 (a int)");
11574
rc= mysql_query(mysql,"insert into t1 values (1), (2), (3)");
11576
rc= mysql_query(mysql,"create view v1 (x) as select a from t1 where a > 1");
11578
stmt= mysql_stmt_init(mysql);
11579
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11580
check_execute(stmt, rc);
11582
for (i= 0; i < 3; i++)
11584
rc= mysql_stmt_execute(stmt);
11585
check_execute(stmt, rc);
11586
rc= my_process_stmt_result(stmt);
11587
DIE_UNLESS(3 == rc);
11589
mysql_stmt_close(stmt);
11591
rc= mysql_query(mysql, "DROP VIEW v1");
11593
rc= mysql_query(mysql, "DROP TABLE t1");
11598
static void test_view_insert_fields()
11601
char parm[11][1000];
11604
MYSQL_BIND my_bind[11];
11605
const char *query= "INSERT INTO `v1` ( `K1C4` ,`K2C4` ,`K3C4` ,`K4N4` ,`F1C4` ,`F2I4` ,`F3N5` ,`F7F8` ,`F6N4` ,`F5C8` ,`F9D8` ) VALUES( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )";
11607
myheader("test_view_insert_fields");
11609
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, v1");
11611
rc= mysql_query(mysql, "DROP VIEW IF EXISTS t1, v1");
11613
rc= mysql_query(mysql,
11614
"CREATE TABLE t1 (K1C4 varchar(4) NOT NULL,"
11615
"K2C4 varchar(4) NOT NULL, K3C4 varchar(4) NOT NULL,"
11616
"K4N4 varchar(4) NOT NULL default '0000',"
11617
"F1C4 varchar(4) NOT NULL, F2I4 int(11) NOT NULL,"
11618
"F3N5 varchar(5) NOT NULL default '00000',"
11619
"F4I4 int(11) NOT NULL default '0', F5C8 varchar(8) NOT NULL,"
11620
"F6N4 varchar(4) NOT NULL default '0000',"
11621
"F7F8 double NOT NULL default '0',"
11622
"F8F8 double NOT NULL default '0',"
11623
"F9D8 decimal(8,2) NOT NULL default '0.00',"
11624
"PRIMARY KEY (K1C4,K2C4,K3C4,K4N4)) "
11625
"CHARSET=latin1 COLLATE latin1_bin");
11627
rc= mysql_query(mysql,
11628
"CREATE VIEW v1 AS select sql_no_cache "
11629
" K1C4 AS K1C4, K2C4 AS K2C4, K3C4 AS K3C4, K4N4 AS K4N4, "
11630
" F1C4 AS F1C4, F2I4 AS F2I4, F3N5 AS F3N5,"
11631
" F7F8 AS F7F8, F6N4 AS F6N4, F5C8 AS F5C8, F9D8 AS F9D8"
11634
bzero((char*) my_bind, sizeof(my_bind));
11635
for (i= 0; i < 11; i++)
11638
my_bind[i].buffer_type= MYSQL_TYPE_STRING;
11639
my_bind[i].is_null= 0;
11640
my_bind[i].buffer= (char *)&parm[i];
11642
strmov(parm[i], "1");
11643
my_bind[i].buffer_length= 2;
11644
my_bind[i].length= &l[i];
11646
stmt= mysql_stmt_init(mysql);
11647
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11648
check_execute(stmt, rc);
11649
rc= mysql_stmt_bind_param(stmt, my_bind);
11650
check_execute(stmt, rc);
11652
rc= mysql_stmt_execute(stmt);
11653
check_execute(stmt, rc);
11654
mysql_stmt_close(stmt);
11656
query= "select * from t1";
11657
stmt= mysql_stmt_init(mysql);
11658
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11659
check_execute(stmt, rc);
11660
rc= mysql_stmt_execute(stmt);
11661
check_execute(stmt, rc);
11662
rc= my_process_stmt_result(stmt);
11663
DIE_UNLESS(1 == rc);
11665
mysql_stmt_close(stmt);
11666
rc= mysql_query(mysql, "DROP VIEW v1");
11668
rc= mysql_query(mysql, "DROP TABLE t1");
11673
static void test_bug5126()
11676
MYSQL_BIND my_bind[2];
11678
const char *stmt_text;
11681
myheader("test_bug5126");
11683
stmt_text= "DROP TABLE IF EXISTS t1";
11684
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11687
stmt_text= "CREATE TABLE t1 (a mediumint, b int)";
11688
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11691
stmt_text= "INSERT INTO t1 VALUES (8386608, 1)";
11692
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11695
stmt= mysql_stmt_init(mysql);
11696
stmt_text= "SELECT a, b FROM t1";
11697
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11698
check_execute(stmt, rc);
11699
rc= mysql_stmt_execute(stmt);
11700
check_execute(stmt, rc);
11702
/* Bind output buffers */
11703
bzero((char*) my_bind, sizeof(my_bind));
11705
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
11706
my_bind[0].buffer= &c1;
11707
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
11708
my_bind[1].buffer= &c2;
11710
mysql_stmt_bind_result(stmt, my_bind);
11712
rc= mysql_stmt_fetch(stmt);
11713
DIE_UNLESS(rc == 0);
11714
DIE_UNLESS(c1 == 8386608 && c2 == 1);
11716
printf("%ld, %ld\n", (long) c1, (long) c2);
11717
mysql_stmt_close(stmt);
11721
static void test_bug4231()
11724
MYSQL_BIND my_bind[2];
11726
const char *stmt_text;
11729
myheader("test_bug4231");
11731
stmt_text= "DROP TABLE IF EXISTS t1";
11732
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11735
stmt_text= "CREATE TABLE t1 (a int)";
11736
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11739
stmt_text= "INSERT INTO t1 VALUES (1)";
11740
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11743
stmt= mysql_stmt_init(mysql);
11744
stmt_text= "SELECT a FROM t1 WHERE ? = ?";
11745
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11746
check_execute(stmt, rc);
11748
/* Bind input buffers */
11749
bzero((char*) my_bind, sizeof(my_bind));
11750
bzero((char*) tm, sizeof(tm));
11752
my_bind[0].buffer_type= MYSQL_TYPE_DATE;
11753
my_bind[0].buffer= &tm[0];
11754
my_bind[1].buffer_type= MYSQL_TYPE_DATE;
11755
my_bind[1].buffer= &tm[1];
11757
mysql_stmt_bind_param(stmt, my_bind);
11758
check_execute(stmt, rc);
11761
First set server-side params to some non-zero non-equal values:
11762
then we will check that they are not used when client sends
11765
tm[0].time_type = MYSQL_TIMESTAMP_DATE;
11770
--tm[1].year; /* tm[0] != tm[1] */
11772
rc= mysql_stmt_execute(stmt);
11773
check_execute(stmt, rc);
11775
rc= mysql_stmt_fetch(stmt);
11777
/* binds are unequal, no rows should be returned */
11778
DIE_UNLESS(rc == MYSQL_NO_DATA);
11780
/* Set one of the dates to zero */
11781
tm[0].year= tm[0].month= tm[0].day= 0;
11783
mysql_stmt_execute(stmt);
11784
rc= mysql_stmt_fetch(stmt);
11785
DIE_UNLESS(rc == 0);
11787
mysql_stmt_close(stmt);
11788
stmt_text= "DROP TABLE t1";
11789
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11794
static void test_bug5399()
11797
Ascii 97 is 'a', which gets mapped to Ascii 65 'A' unless internal
11798
statement id hash in the server uses binary collation.
11800
#define NUM_OF_USED_STMT 97
11801
MYSQL_STMT *stmt_list[NUM_OF_USED_STMT];
11803
MYSQL_BIND my_bind[1];
11808
myheader("test_bug5399");
11810
bzero((char*) my_bind, sizeof(my_bind));
11811
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
11812
my_bind[0].buffer= &no;
11814
for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
11816
sprintf(buff, "select %d", (int) (stmt - stmt_list));
11817
*stmt= mysql_stmt_init(mysql);
11818
rc= mysql_stmt_prepare(*stmt, buff, strlen(buff));
11819
check_execute(*stmt, rc);
11820
mysql_stmt_bind_result(*stmt, my_bind);
11823
printf("%d statements prepared.\n", NUM_OF_USED_STMT);
11825
for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
11827
rc= mysql_stmt_execute(*stmt);
11828
check_execute(*stmt, rc);
11829
rc= mysql_stmt_store_result(*stmt);
11830
check_execute(*stmt, rc);
11831
rc= mysql_stmt_fetch(*stmt);
11832
DIE_UNLESS(rc == 0);
11833
DIE_UNLESS((int32) (stmt - stmt_list) == no);
11836
for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
11837
mysql_stmt_close(*stmt);
11838
#undef NUM_OF_USED_STMT
11842
static void test_bug5194()
11845
MYSQL_BIND *my_bind;
11848
int param_str_length;
11849
const char *stmt_text;
11851
float float_array[250] =
11853
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11854
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11855
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11856
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11857
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11858
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11859
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11860
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11861
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11862
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11863
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11864
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11865
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11866
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11867
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11868
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11869
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11870
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11871
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11872
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11873
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11874
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11875
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11876
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11877
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25
11879
float *fa_ptr= float_array;
11880
/* Number of columns per row */
11881
const int COLUMN_COUNT= sizeof(float_array)/sizeof(*float_array);
11882
/* Number of rows per bulk insert to start with */
11883
const int MIN_ROWS_PER_INSERT= 262;
11884
/* Max number of rows per bulk insert to end with */
11885
const int MAX_ROWS_PER_INSERT= 300;
11886
const int MAX_PARAM_COUNT= COLUMN_COUNT*MAX_ROWS_PER_INSERT;
11887
const char *query_template= "insert into t1 values %s";
11888
const int CHARS_PER_PARAM= 5; /* space needed to place ", ?" in the query */
11889
const int uint16_max= 65535;
11892
myheader("test_bug5194");
11894
stmt_text= "drop table if exists t1";
11895
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11897
stmt_text= "create table if not exists t1"
11898
"(c1 float, c2 float, c3 float, c4 float, c5 float, c6 float, "
11899
"c7 float, c8 float, c9 float, c10 float, c11 float, c12 float, "
11900
"c13 float, c14 float, c15 float, c16 float, c17 float, c18 float, "
11901
"c19 float, c20 float, c21 float, c22 float, c23 float, c24 float, "
11902
"c25 float, c26 float, c27 float, c28 float, c29 float, c30 float, "
11903
"c31 float, c32 float, c33 float, c34 float, c35 float, c36 float, "
11904
"c37 float, c38 float, c39 float, c40 float, c41 float, c42 float, "
11905
"c43 float, c44 float, c45 float, c46 float, c47 float, c48 float, "
11906
"c49 float, c50 float, c51 float, c52 float, c53 float, c54 float, "
11907
"c55 float, c56 float, c57 float, c58 float, c59 float, c60 float, "
11908
"c61 float, c62 float, c63 float, c64 float, c65 float, c66 float, "
11909
"c67 float, c68 float, c69 float, c70 float, c71 float, c72 float, "
11910
"c73 float, c74 float, c75 float, c76 float, c77 float, c78 float, "
11911
"c79 float, c80 float, c81 float, c82 float, c83 float, c84 float, "
11912
"c85 float, c86 float, c87 float, c88 float, c89 float, c90 float, "
11913
"c91 float, c92 float, c93 float, c94 float, c95 float, c96 float, "
11914
"c97 float, c98 float, c99 float, c100 float, c101 float, c102 float, "
11915
"c103 float, c104 float, c105 float, c106 float, c107 float, c108 float, "
11916
"c109 float, c110 float, c111 float, c112 float, c113 float, c114 float, "
11917
"c115 float, c116 float, c117 float, c118 float, c119 float, c120 float, "
11918
"c121 float, c122 float, c123 float, c124 float, c125 float, c126 float, "
11919
"c127 float, c128 float, c129 float, c130 float, c131 float, c132 float, "
11920
"c133 float, c134 float, c135 float, c136 float, c137 float, c138 float, "
11921
"c139 float, c140 float, c141 float, c142 float, c143 float, c144 float, "
11922
"c145 float, c146 float, c147 float, c148 float, c149 float, c150 float, "
11923
"c151 float, c152 float, c153 float, c154 float, c155 float, c156 float, "
11924
"c157 float, c158 float, c159 float, c160 float, c161 float, c162 float, "
11925
"c163 float, c164 float, c165 float, c166 float, c167 float, c168 float, "
11926
"c169 float, c170 float, c171 float, c172 float, c173 float, c174 float, "
11927
"c175 float, c176 float, c177 float, c178 float, c179 float, c180 float, "
11928
"c181 float, c182 float, c183 float, c184 float, c185 float, c186 float, "
11929
"c187 float, c188 float, c189 float, c190 float, c191 float, c192 float, "
11930
"c193 float, c194 float, c195 float, c196 float, c197 float, c198 float, "
11931
"c199 float, c200 float, c201 float, c202 float, c203 float, c204 float, "
11932
"c205 float, c206 float, c207 float, c208 float, c209 float, c210 float, "
11933
"c211 float, c212 float, c213 float, c214 float, c215 float, c216 float, "
11934
"c217 float, c218 float, c219 float, c220 float, c221 float, c222 float, "
11935
"c223 float, c224 float, c225 float, c226 float, c227 float, c228 float, "
11936
"c229 float, c230 float, c231 float, c232 float, c233 float, c234 float, "
11937
"c235 float, c236 float, c237 float, c238 float, c239 float, c240 float, "
11938
"c241 float, c242 float, c243 float, c244 float, c245 float, c246 float, "
11939
"c247 float, c248 float, c249 float, c250 float)";
11940
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11943
my_bind= (MYSQL_BIND*) malloc(MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
11944
query= (char*) malloc(strlen(query_template) +
11945
MAX_PARAM_COUNT * CHARS_PER_PARAM + 1);
11946
param_str= (char*) malloc(COLUMN_COUNT * CHARS_PER_PARAM);
11948
if (my_bind == 0 || query == 0 || param_str == 0)
11950
fprintf(stderr, "Can't allocate enough memory for query structs\n");
11960
stmt= mysql_stmt_init(mysql);
11962
/* setup a template for one row of parameters */
11963
sprintf(param_str, "(");
11964
for (i= 1; i < COLUMN_COUNT; ++i)
11965
strcat(param_str, "?, ");
11966
strcat(param_str, "?)");
11967
param_str_length= strlen(param_str);
11969
/* setup bind array */
11970
bzero((char*) my_bind, MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
11971
for (i= 0; i < MAX_PARAM_COUNT; ++i)
11973
my_bind[i].buffer_type= MYSQL_TYPE_FLOAT;
11974
my_bind[i].buffer= fa_ptr;
11975
if (++fa_ptr == float_array + COLUMN_COUNT)
11976
fa_ptr= float_array;
11980
Test each number of rows per bulk insert, so that we can see where
11983
for (nrows= MIN_ROWS_PER_INSERT; nrows <= MAX_ROWS_PER_INSERT; ++nrows)
11986
/* Create statement text for current number of rows */
11987
sprintf(query, query_template, param_str);
11988
query_ptr= query + strlen(query);
11989
for (i= 1; i < nrows; ++i)
11991
memcpy(query_ptr, ", ", 2);
11993
memcpy(query_ptr, param_str, param_str_length);
11994
query_ptr+= param_str_length;
11998
rc= mysql_stmt_prepare(stmt, query, query_ptr - query);
11999
if (rc && nrows * COLUMN_COUNT > uint16_max)
12002
printf("Failed to prepare a statement with %d placeholders "
12003
"(as expected).\n", nrows * COLUMN_COUNT);
12007
check_execute(stmt, rc);
12010
printf("Insert: query length= %d, row count= %d, param count= %lu\n",
12011
(int) strlen(query), nrows, mysql_stmt_param_count(stmt));
12013
/* bind the parameter array and execute the query */
12014
rc= mysql_stmt_bind_param(stmt, my_bind);
12015
check_execute(stmt, rc);
12017
rc= mysql_stmt_execute(stmt);
12018
check_execute(stmt, rc);
12019
mysql_stmt_reset(stmt);
12022
mysql_stmt_close(stmt);
12026
stmt_text= "drop table t1";
12027
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12032
static void test_bug5315()
12035
const char *stmt_text;
12038
myheader("test_bug5315");
12040
stmt_text= "SELECT 1";
12041
stmt= mysql_stmt_init(mysql);
12042
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12043
DIE_UNLESS(rc == 0);
12045
printf("Excuting mysql_change_user\n");
12046
mysql_change_user(mysql, opt_user, opt_password, current_db);
12048
printf("Excuting mysql_stmt_execute\n");
12049
rc= mysql_stmt_execute(stmt);
12050
DIE_UNLESS(rc != 0);
12054
printf("Got error (as expected): '%s'\n", mysql_stmt_error(stmt));
12056
/* check that connection is OK */
12058
printf("Excuting mysql_stmt_close\n");
12059
mysql_stmt_close(stmt);
12061
printf("Excuting mysql_stmt_init\n");
12062
stmt= mysql_stmt_init(mysql);
12063
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12064
DIE_UNLESS(rc == 0);
12065
rc= mysql_stmt_execute(stmt);
12066
DIE_UNLESS(rc == 0);
12067
mysql_stmt_close(stmt);
12071
static void test_bug6049()
12074
MYSQL_BIND my_bind[1];
12077
const char *stmt_text;
12082
myheader("test_bug6049");
12084
stmt_text= "SELECT MAKETIME(-25, 12, 12)";
12086
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12088
res= mysql_store_result(mysql);
12089
row= mysql_fetch_row(res);
12091
stmt= mysql_stmt_init(mysql);
12092
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12093
check_execute(stmt, rc);
12094
rc= mysql_stmt_execute(stmt);
12095
check_execute(stmt, rc);
12097
bzero((char*) my_bind, sizeof(my_bind));
12098
my_bind[0].buffer_type = MYSQL_TYPE_STRING;
12099
my_bind[0].buffer = &buffer;
12100
my_bind[0].buffer_length = sizeof(buffer);
12101
my_bind[0].length = &length;
12103
mysql_stmt_bind_result(stmt, my_bind);
12104
rc= mysql_stmt_fetch(stmt);
12105
DIE_UNLESS(rc == 0);
12109
printf("Result from query: %s\n", row[0]);
12110
printf("Result from prepared statement: %s\n", (char*) buffer);
12113
DIE_UNLESS(strcmp(row[0], (char*) buffer) == 0);
12115
mysql_free_result(res);
12116
mysql_stmt_close(stmt);
12120
static void test_bug6058()
12123
MYSQL_BIND my_bind[1];
12126
const char *stmt_text;
12131
myheader("test_bug6058");
12133
stmt_text= "SELECT CAST('0000-00-00' AS DATE)";
12135
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12137
res= mysql_store_result(mysql);
12138
row= mysql_fetch_row(res);
12140
stmt= mysql_stmt_init(mysql);
12141
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12142
check_execute(stmt, rc);
12143
rc= mysql_stmt_execute(stmt);
12144
check_execute(stmt, rc);
12146
bzero((char*) my_bind, sizeof(my_bind));
12147
my_bind[0].buffer_type = MYSQL_TYPE_STRING;
12148
my_bind[0].buffer = &buffer;
12149
my_bind[0].buffer_length = sizeof(buffer);
12150
my_bind[0].length = &length;
12152
mysql_stmt_bind_result(stmt, my_bind);
12153
rc= mysql_stmt_fetch(stmt);
12154
DIE_UNLESS(rc == 0);
12158
printf("Result from query: %s\n", row[0]);
12159
printf("Result from prepared statement: %s\n", buffer);
12162
DIE_UNLESS(strcmp(row[0], buffer) == 0);
12164
mysql_free_result(res);
12165
mysql_stmt_close(stmt);
12169
static void test_bug6059()
12172
const char *stmt_text;
12174
myheader("test_bug6059");
12176
stmt_text= "SELECT 'foo' INTO OUTFILE 'x.3'";
12178
stmt= mysql_stmt_init(mysql);
12179
(void) mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12180
DIE_UNLESS(mysql_stmt_field_count(stmt) == 0);
12181
mysql_stmt_close(stmt);
12185
static void test_bug6046()
12188
const char *stmt_text;
12191
MYSQL_BIND my_bind[1];
12193
myheader("test_bug6046");
12195
stmt_text= "DROP TABLE IF EXISTS t1";
12196
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12198
stmt_text= "CREATE TABLE t1 (a int, b int)";
12199
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12201
stmt_text= "INSERT INTO t1 VALUES (1,1),(2,2),(3,1),(4,2)";
12202
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12205
stmt= mysql_stmt_init(mysql);
12207
stmt_text= "SELECT t1.a FROM t1 NATURAL JOIN t1 as X1 "
12208
"WHERE t1.b > ? ORDER BY t1.a";
12210
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12211
check_execute(stmt, rc);
12214
bzero((char*) my_bind, sizeof(my_bind));
12215
my_bind[0].buffer= &b;
12216
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
12218
mysql_stmt_bind_param(stmt, my_bind);
12220
rc= mysql_stmt_execute(stmt);
12221
check_execute(stmt, rc);
12222
mysql_stmt_store_result(stmt);
12224
rc= mysql_stmt_execute(stmt);
12225
check_execute(stmt, rc);
12227
mysql_stmt_close(stmt);
12232
static void test_basic_cursors()
12234
const char *basic_tables[]=
12236
"DROP TABLE IF EXISTS t1, t2",
12239
"(id INTEGER NOT NULL PRIMARY KEY, "
12240
" name VARCHAR(20) NOT NULL)",
12242
"INSERT INTO t1 (id, name) VALUES "
12243
" (2, 'Ja'), (3, 'Ede'), "
12244
" (4, 'Haag'), (5, 'Kabul'), "
12245
" (6, 'Almere'), (7, 'Utrecht'), "
12246
" (8, 'Qandahar'), (9, 'Amsterdam'), "
12247
" (10, 'Amersfoort'), (11, 'Constantine')",
12250
"(id INTEGER NOT NULL PRIMARY KEY, "
12251
" name VARCHAR(20) NOT NULL)",
12253
"INSERT INTO t2 (id, name) VALUES "
12254
" (4, 'Guam'), (5, 'Aruba'), "
12255
" (6, 'Angola'), (7, 'Albania'), "
12256
" (8, 'Anguilla'), (9, 'Argentina'), "
12257
" (10, 'Azerbaijan'), (11, 'Afghanistan'), "
12258
" (12, 'Burkina Faso'), (13, 'Faroe Islands')"
12260
const char *queries[]=
12262
"SELECT * FROM t1",
12266
DBUG_ENTER("test_basic_cursors");
12267
myheader("test_basic_cursors");
12269
fill_tables(basic_tables, sizeof(basic_tables)/sizeof(*basic_tables));
12271
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
12272
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
12277
static void test_cursors_with_union()
12279
const char *queries[]=
12281
"SELECT t1.name FROM t1 UNION SELECT t2.name FROM t2",
12282
"SELECT t1.id FROM t1 WHERE t1.id < 5"
12284
myheader("test_cursors_with_union");
12285
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
12286
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
12290
static void test_cursors_with_procedure()
12292
const char *queries[]=
12294
"SELECT * FROM t1 procedure analyse()"
12296
myheader("test_cursors_with_procedure");
12297
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
12298
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
12303
Altough mysql_create_db(), mysql_rm_db() are deprecated since 4.0 they
12304
should not crash server and should not hang in case of errors.
12306
Since those functions can't be seen in modern API (unless client library
12307
was compiled with USE_OLD_FUNCTIONS define) we use simple_command() macro.
12309
static void test_bug6081()
12312
myheader("test_bug6081");
12314
rc= simple_command(mysql, COM_DROP_DB, (uchar*) current_db,
12315
(ulong)strlen(current_db), 0);
12316
if (rc == 0 && mysql_errno(mysql) != ER_UNKNOWN_COM_ERROR)
12318
myerror(NULL); /* purecov: inspected */
12319
die(__FILE__, __LINE__, "COM_DROP_DB failed"); /* purecov: inspected */
12321
rc= simple_command(mysql, COM_DROP_DB, (uchar*) current_db,
12322
(ulong)strlen(current_db), 0U);
12324
rc= simple_command(mysql, COM_CREATE_DB, (uchar*) current_db,
12325
(ulong)strlen(current_db), 0U);
12326
if (rc == 0 && mysql_errno(mysql) != ER_UNKNOWN_COM_ERROR)
12328
myerror(NULL); /* purecov: inspected */
12329
die(__FILE__, __LINE__, "COM_CREATE_DB failed"); /* purecov: inspected */
12331
rc= simple_command(mysql, COM_CREATE_DB, (uchar*) current_db,
12332
(ulong)strlen(current_db), 0U);
12334
rc= mysql_select_db(mysql, current_db);
12339
static void test_bug6096()
12342
MYSQL_RES *query_result, *stmt_metadata;
12343
const char *stmt_text;
12344
MYSQL_BIND my_bind[12];
12345
MYSQL_FIELD *query_field_list, *stmt_field_list;
12346
ulong query_field_count, stmt_field_count;
12348
my_bool update_max_length= TRUE;
12351
myheader("test_bug6096");
12353
stmt_text= "drop table if exists t1";
12354
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12357
mysql_query(mysql, "set sql_mode=''");
12358
stmt_text= "create table t1 (c_tinyint tinyint, c_smallint smallint, "
12359
" c_mediumint mediumint, c_int int, "
12360
" c_bigint bigint, c_float float, "
12361
" c_double double, c_varchar varchar(20), "
12362
" c_char char(20), c_time time, c_date date, "
12363
" c_datetime datetime)";
12364
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12366
stmt_text= "insert into t1 values (-100, -20000, 30000000, 4, 8, 1.0, "
12367
"2.0, 'abc', 'def', now(), now(), now())";
12368
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12371
stmt_text= "select * from t1";
12373
/* Run select in prepared and non-prepared mode and compare metadata */
12374
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12376
query_result= mysql_store_result(mysql);
12377
query_field_list= mysql_fetch_fields(query_result);
12378
query_field_count= mysql_num_fields(query_result);
12380
stmt= mysql_stmt_init(mysql);
12381
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12382
check_execute(stmt, rc);
12383
rc= mysql_stmt_execute(stmt);
12384
check_execute(stmt, rc);
12385
mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH,
12386
(void*) &update_max_length);
12387
mysql_stmt_store_result(stmt);
12388
stmt_metadata= mysql_stmt_result_metadata(stmt);
12389
stmt_field_list= mysql_fetch_fields(stmt_metadata);
12390
stmt_field_count= mysql_num_fields(stmt_metadata);
12391
DIE_UNLESS(stmt_field_count == query_field_count);
12393
/* Print out and check the metadata */
12397
printf(" ------------------------------------------------------------\n");
12398
printf(" | Metadata \n");
12399
printf(" ------------------------------------------------------------\n");
12400
printf(" | Query | Prepared statement \n");
12401
printf(" ------------------------------------------------------------\n");
12402
printf(" field name | length | max_length | length | max_length\n");
12403
printf(" ------------------------------------------------------------\n");
12405
for (i= 0; i < query_field_count; ++i)
12407
MYSQL_FIELD *f1= &query_field_list[i], *f2= &stmt_field_list[i];
12408
printf(" %-11s | %9lu | %10lu | %9lu | %10lu \n",
12409
f1->name, f1->length, f1->max_length, f2->length, f2->max_length);
12410
DIE_UNLESS(f1->length == f2->length);
12412
printf(" ---------------------------------------------------------------\n");
12415
/* Bind and fetch the data */
12417
bzero((char*) my_bind, sizeof(my_bind));
12418
for (i= 0; i < stmt_field_count; ++i)
12420
my_bind[i].buffer_type= MYSQL_TYPE_STRING;
12421
my_bind[i].buffer_length= stmt_field_list[i].max_length + 1;
12422
my_bind[i].buffer= malloc(my_bind[i].buffer_length);
12424
mysql_stmt_bind_result(stmt, my_bind);
12425
rc= mysql_stmt_fetch(stmt);
12426
check_execute(stmt, rc);
12427
rc= mysql_stmt_fetch(stmt);
12428
DIE_UNLESS(rc == MYSQL_NO_DATA);
12432
for (i= 0; i < stmt_field_count; ++i)
12433
free(my_bind[i].buffer);
12434
mysql_stmt_close(stmt);
12435
mysql_free_result(query_result);
12436
mysql_free_result(stmt_metadata);
12437
stmt_text= "drop table t1";
12438
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12444
Test of basic checks that are performed in server for components
12445
of MYSQL_TIME parameters.
12448
static void test_datetime_ranges()
12450
const char *stmt_text;
12453
MYSQL_BIND my_bind[6];
12456
myheader("test_datetime_ranges");
12458
stmt_text= "drop table if exists t1";
12459
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12462
stmt_text= "create table t1 (year datetime, month datetime, day datetime, "
12463
"hour datetime, min datetime, sec datetime)";
12464
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12467
stmt= mysql_simple_prepare(mysql,
12468
"INSERT INTO t1 VALUES (?, ?, ?, ?, ?, ?)");
12470
verify_param_count(stmt, 6);
12472
bzero((char*) my_bind, sizeof(my_bind));
12473
for (i= 0; i < 6; i++)
12475
my_bind[i].buffer_type= MYSQL_TYPE_DATETIME;
12476
my_bind[i].buffer= &tm[i];
12478
rc= mysql_stmt_bind_param(stmt, my_bind);
12479
check_execute(stmt, rc);
12481
tm[0].year= 2004; tm[0].month= 11; tm[0].day= 10;
12482
tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
12483
tm[0].second_part= 0; tm[0].neg= 0;
12485
tm[5]= tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
12486
tm[0].year= 10000; tm[1].month= 13; tm[2].day= 32;
12487
tm[3].hour= 24; tm[4].minute= 60; tm[5].second= 60;
12489
rc= mysql_stmt_execute(stmt);
12490
check_execute(stmt, rc);
12491
DIE_UNLESS(mysql_warning_count(mysql) != 6);
12493
verify_col_data("t1", "year", "0000-00-00 00:00:00");
12494
verify_col_data("t1", "month", "0000-00-00 00:00:00");
12495
verify_col_data("t1", "day", "0000-00-00 00:00:00");
12496
verify_col_data("t1", "hour", "0000-00-00 00:00:00");
12497
verify_col_data("t1", "min", "0000-00-00 00:00:00");
12498
verify_col_data("t1", "sec", "0000-00-00 00:00:00");
12500
mysql_stmt_close(stmt);
12502
stmt_text= "delete from t1";
12503
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12506
stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 (year, month, day) "
12507
"VALUES (?, ?, ?)");
12509
verify_param_count(stmt, 3);
12512
We reuse contents of bind and tm arrays left from previous part of test.
12514
for (i= 0; i < 3; i++)
12515
my_bind[i].buffer_type= MYSQL_TYPE_DATE;
12517
rc= mysql_stmt_bind_param(stmt, my_bind);
12518
check_execute(stmt, rc);
12520
rc= mysql_stmt_execute(stmt);
12521
check_execute(stmt, rc);
12522
DIE_UNLESS(mysql_warning_count(mysql) != 3);
12524
verify_col_data("t1", "year", "0000-00-00 00:00:00");
12525
verify_col_data("t1", "month", "0000-00-00 00:00:00");
12526
verify_col_data("t1", "day", "0000-00-00 00:00:00");
12528
mysql_stmt_close(stmt);
12530
stmt_text= "drop table t1";
12531
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12534
stmt_text= "create table t1 (day_ovfl time, day time, hour time, min time, sec time)";
12535
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12538
stmt= mysql_simple_prepare(mysql,
12539
"INSERT INTO t1 VALUES (?, ?, ?, ?, ?)");
12541
verify_param_count(stmt, 5);
12544
Again we reuse what we can from previous part of test.
12546
for (i= 0; i < 5; i++)
12547
my_bind[i].buffer_type= MYSQL_TYPE_TIME;
12549
rc= mysql_stmt_bind_param(stmt, my_bind);
12550
check_execute(stmt, rc);
12552
tm[0].year= 0; tm[0].month= 0; tm[0].day= 10;
12553
tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
12554
tm[0].second_part= 0; tm[0].neg= 0;
12556
tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
12557
tm[0].day= 35; tm[1].day= 34; tm[2].hour= 30; tm[3].minute= 60; tm[4].second= 60;
12559
rc= mysql_stmt_execute(stmt);
12560
check_execute(stmt, rc);
12561
DIE_UNLESS(mysql_warning_count(mysql) == 2);
12563
verify_col_data("t1", "day_ovfl", "838:59:59");
12564
verify_col_data("t1", "day", "828:30:30");
12565
verify_col_data("t1", "hour", "270:30:30");
12566
verify_col_data("t1", "min", "00:00:00");
12567
verify_col_data("t1", "sec", "00:00:00");
12569
mysql_stmt_close(stmt);
12571
stmt_text= "drop table t1";
12572
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12577
static void test_bug4172()
12580
MYSQL_BIND my_bind[3];
12581
const char *stmt_text;
12585
char f[100], d[100], e[100];
12586
ulong f_len, d_len, e_len;
12588
myheader("test_bug4172");
12590
mysql_query(mysql, "DROP TABLE IF EXISTS t1");
12591
mysql_query(mysql, "CREATE TABLE t1 (f float, d double, e decimal(10,4))");
12592
mysql_query(mysql, "INSERT INTO t1 VALUES (12345.1234, 123456.123456, "
12595
stmt= mysql_stmt_init(mysql);
12596
stmt_text= "SELECT f, d, e FROM t1";
12598
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12599
check_execute(stmt, rc);
12600
rc= mysql_stmt_execute(stmt);
12601
check_execute(stmt, rc);
12603
bzero((char*) my_bind, sizeof(my_bind));
12604
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
12605
my_bind[0].buffer= f;
12606
my_bind[0].buffer_length= sizeof(f);
12607
my_bind[0].length= &f_len;
12608
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
12609
my_bind[1].buffer= d;
12610
my_bind[1].buffer_length= sizeof(d);
12611
my_bind[1].length= &d_len;
12612
my_bind[2].buffer_type= MYSQL_TYPE_STRING;
12613
my_bind[2].buffer= e;
12614
my_bind[2].buffer_length= sizeof(e);
12615
my_bind[2].length= &e_len;
12617
mysql_stmt_bind_result(stmt, my_bind);
12619
mysql_stmt_store_result(stmt);
12620
rc= mysql_stmt_fetch(stmt);
12621
check_execute(stmt, rc);
12623
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12625
res= mysql_store_result(mysql);
12626
row= mysql_fetch_row(res);
12630
printf("Binary protocol: float=%s, double=%s, decimal(10,4)=%s\n",
12632
printf("Text protocol: float=%s, double=%s, decimal(10,4)=%s\n",
12633
row[0], row[1], row[2]);
12635
DIE_UNLESS(!strcmp(f, row[0]) && !strcmp(d, row[1]) && !strcmp(e, row[2]));
12637
mysql_free_result(res);
12638
mysql_stmt_close(stmt);
12642
static void test_conversion()
12645
const char *stmt_text;
12647
MYSQL_BIND my_bind[1];
12651
myheader("test_conversion");
12653
stmt_text= "DROP TABLE IF EXISTS t1";
12654
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12656
stmt_text= "CREATE TABLE t1 (a TEXT) DEFAULT CHARSET latin1";
12657
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12659
stmt_text= "SET character_set_connection=utf8, character_set_client=utf8, "
12660
" character_set_results=latin1";
12661
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12664
stmt= mysql_stmt_init(mysql);
12666
stmt_text= "INSERT INTO t1 (a) VALUES (?)";
12667
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12668
check_execute(stmt, rc);
12670
bzero((char*) my_bind, sizeof(my_bind));
12671
my_bind[0].buffer= buff;
12672
my_bind[0].length= &length;
12673
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
12675
mysql_stmt_bind_param(stmt, my_bind);
12677
buff[0]= (uchar) 0xC3;
12678
buff[1]= (uchar) 0xA0;
12681
rc= mysql_stmt_execute(stmt);
12682
check_execute(stmt, rc);
12684
stmt_text= "SELECT a FROM t1";
12685
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12686
check_execute(stmt, rc);
12687
rc= mysql_stmt_execute(stmt);
12688
check_execute(stmt, rc);
12690
my_bind[0].buffer_length= sizeof(buff);
12691
mysql_stmt_bind_result(stmt, my_bind);
12693
rc= mysql_stmt_fetch(stmt);
12694
DIE_UNLESS(rc == 0);
12695
DIE_UNLESS(length == 1);
12696
DIE_UNLESS((uchar) buff[0] == 0xE0);
12697
rc= mysql_stmt_fetch(stmt);
12698
DIE_UNLESS(rc == MYSQL_NO_DATA);
12700
mysql_stmt_close(stmt);
12701
stmt_text= "DROP TABLE t1";
12702
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12704
stmt_text= "SET NAMES DEFAULT";
12705
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12709
static void test_rewind(void)
12712
MYSQL_BIND my_bind;
12714
const char *stmt_text;
12715
long unsigned int length=4, Data=0;
12718
myheader("test_rewind");
12720
stmt_text= "CREATE TABLE t1 (a int)";
12721
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12723
stmt_text= "INSERT INTO t1 VALUES(2),(3),(4)";
12724
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12727
stmt= mysql_stmt_init(mysql);
12729
stmt_text= "SELECT * FROM t1";
12730
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12731
check_execute(stmt, rc);
12733
bzero((char*) &my_bind, sizeof(MYSQL_BIND));
12734
my_bind.buffer_type= MYSQL_TYPE_LONG;
12735
my_bind.buffer= (void *)&Data; /* this buffer won't be altered */
12736
my_bind.length= &length;
12737
my_bind.is_null= &isnull;
12739
rc= mysql_stmt_execute(stmt);
12740
check_execute(stmt, rc);
12742
rc= mysql_stmt_store_result(stmt);
12743
DIE_UNLESS(rc == 0);
12745
rc= mysql_stmt_bind_result(stmt, &my_bind);
12746
DIE_UNLESS(rc == 0);
12748
/* retreive all result sets till we are at the end */
12749
while(!mysql_stmt_fetch(stmt))
12751
printf("fetched result:%ld\n", Data);
12753
DIE_UNLESS(rc != MYSQL_NO_DATA);
12755
/* seek to the first row */
12756
mysql_stmt_data_seek(stmt, 0);
12758
/* now we should be able to fetch the results again */
12759
/* but mysql_stmt_fetch returns MYSQL_NO_DATA */
12760
while(!(rc= mysql_stmt_fetch(stmt)))
12762
printf("fetched result after seek:%ld\n", Data);
12764
DIE_UNLESS(rc == MYSQL_NO_DATA);
12766
stmt_text= "DROP TABLE t1";
12767
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12769
rc= mysql_stmt_free_result(stmt);
12770
rc= mysql_stmt_close(stmt);
12774
static void test_truncation()
12777
const char *stmt_text;
12780
MYSQL_BIND *bind_array, *my_bind;
12782
myheader("test_truncation");
12784
/* Prepare the test table */
12785
rc= mysql_query(mysql, "drop table if exists t1");
12788
stmt_text= "create table t1 ("
12789
"i8 tinyint, ui8 tinyint unsigned, "
12790
"i16 smallint, i16_1 smallint, "
12791
"ui16 smallint unsigned, i32 int, i32_1 int, "
12792
"d double, d_1 double, ch char(30), ch_1 char(30), "
12793
"tx text, tx_1 text, ch_2 char(30) "
12795
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12797
stmt_text= "insert into t1 VALUES ("
12800
"32000, " /* i16 */
12801
"-32767, " /* i16_1 */
12802
"64000, " /* ui16 */
12803
"1073741824, " /* i32 */
12804
"1073741825, " /* i32_1 */
12805
"123.456, " /* d */
12806
"-12345678910, " /* d_1 */
12807
"'111111111111111111111111111111',"/* ch */
12808
"'abcdef', " /* ch_1 */
12809
"'12345 ', " /* tx */
12810
"'12345.67 ', " /* tx_1 */
12811
"'12345.67abc'" /* ch_2 */
12813
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12816
stmt_text= "select i8 c1, i8 c2, ui8 c3, i16_1 c4, ui16 c5, "
12817
" i16 c6, ui16 c7, i32 c8, i32_1 c9, i32_1 c10, "
12818
" d c11, d_1 c12, d_1 c13, ch c14, ch_1 c15, tx c16, "
12819
" tx_1 c17, ch_2 c18 "
12822
stmt= mysql_stmt_init(mysql);
12823
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12824
check_execute(stmt, rc);
12825
rc= mysql_stmt_execute(stmt);
12826
check_execute(stmt, rc);
12827
bind_count= (uint) mysql_stmt_field_count(stmt);
12829
/*************** Fill in the bind structure and bind it **************/
12830
bind_array= malloc(sizeof(MYSQL_BIND) * bind_count);
12831
bzero((char*) bind_array, sizeof(MYSQL_BIND) * bind_count);
12832
for (my_bind= bind_array; my_bind < bind_array + bind_count; my_bind++)
12833
my_bind->error= &my_bind->error_value;
12834
my_bind= bind_array;
12836
my_bind->buffer= malloc(sizeof(uint8));
12837
my_bind->buffer_type= MYSQL_TYPE_TINY;
12838
my_bind->is_unsigned= TRUE;
12840
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12841
my_bind->buffer= malloc(sizeof(uint32));
12842
my_bind->buffer_type= MYSQL_TYPE_LONG;
12843
my_bind->is_unsigned= TRUE;
12845
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12846
my_bind->buffer= malloc(sizeof(int8));
12847
my_bind->buffer_type= MYSQL_TYPE_TINY;
12849
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12850
my_bind->buffer= malloc(sizeof(uint16));
12851
my_bind->buffer_type= MYSQL_TYPE_SHORT;
12852
my_bind->is_unsigned= TRUE;
12854
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12855
my_bind->buffer= malloc(sizeof(int16));
12856
my_bind->buffer_type= MYSQL_TYPE_SHORT;
12858
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12859
my_bind->buffer= malloc(sizeof(uint16));
12860
my_bind->buffer_type= MYSQL_TYPE_SHORT;
12861
my_bind->is_unsigned= TRUE;
12863
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12864
my_bind->buffer= malloc(sizeof(int8));
12865
my_bind->buffer_type= MYSQL_TYPE_TINY;
12866
my_bind->is_unsigned= TRUE;
12868
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12869
my_bind->buffer= malloc(sizeof(float));
12870
my_bind->buffer_type= MYSQL_TYPE_FLOAT;
12872
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12873
my_bind->buffer= malloc(sizeof(float));
12874
my_bind->buffer_type= MYSQL_TYPE_FLOAT;
12876
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12877
my_bind->buffer= malloc(sizeof(double));
12878
my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
12880
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12881
my_bind->buffer= malloc(sizeof(longlong));
12882
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12884
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12885
my_bind->buffer= malloc(sizeof(ulonglong));
12886
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12887
my_bind->is_unsigned= TRUE;
12889
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12890
my_bind->buffer= malloc(sizeof(longlong));
12891
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12893
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12894
my_bind->buffer= malloc(sizeof(longlong));
12895
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12897
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12898
my_bind->buffer= malloc(sizeof(longlong));
12899
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12901
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12902
my_bind->buffer= malloc(sizeof(longlong));
12903
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12905
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12906
my_bind->buffer= malloc(sizeof(double));
12907
my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
12909
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12910
my_bind->buffer= malloc(sizeof(double));
12911
my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
12913
rc= mysql_stmt_bind_result(stmt, bind_array);
12914
check_execute(stmt, rc);
12915
rc= mysql_stmt_fetch(stmt);
12916
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
12918
/*************** Verify truncation results ***************************/
12919
my_bind= bind_array;
12921
/* signed tiny -> tiny */
12922
DIE_UNLESS(*my_bind->error && * (int8*) my_bind->buffer == -10);
12924
/* signed tiny -> uint32 */
12925
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12926
DIE_UNLESS(*my_bind->error && * (int32*) my_bind->buffer == -10);
12928
/* unsigned tiny -> tiny */
12929
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12930
DIE_UNLESS(*my_bind->error && * (uint8*) my_bind->buffer == 200);
12932
/* short -> ushort */
12933
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12934
DIE_UNLESS(*my_bind->error && * (int16*) my_bind->buffer == -32767);
12936
/* ushort -> short */
12937
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12938
DIE_UNLESS(*my_bind->error && * (uint16*) my_bind->buffer == 64000);
12940
/* short -> ushort (no truncation, data is in the range of target type) */
12941
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12942
DIE_UNLESS(! *my_bind->error && * (uint16*) my_bind->buffer == 32000);
12944
/* ushort -> utiny */
12945
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12946
DIE_UNLESS(*my_bind->error && * (int8*) my_bind->buffer == 0);
12948
/* int -> float: no truncation, the number is a power of two */
12949
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12950
DIE_UNLESS(! *my_bind->error && * (float*) my_bind->buffer == 1073741824);
12952
/* int -> float: truncation, not enough bits in float */
12953
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12954
DIE_UNLESS(*my_bind->error);
12956
/* int -> double: no truncation */
12957
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12958
DIE_UNLESS(! *my_bind->error && * (double*) my_bind->buffer == 1073741825);
12960
/* double -> longlong: fractional part is lost */
12961
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12963
/* double -> ulonglong, negative fp number to unsigned integer */
12964
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12965
/* Value in the buffer is not defined: don't test it */
12966
DIE_UNLESS(*my_bind->error);
12968
/* double -> longlong, negative fp number to signed integer: no loss */
12969
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12970
DIE_UNLESS(! *my_bind->error && * (longlong*) my_bind->buffer == LL(-12345678910));
12972
/* big numeric string -> number */
12973
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12974
DIE_UNLESS(*my_bind->error);
12976
/* junk string -> number */
12977
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12978
DIE_UNLESS(*my_bind->error && *(longlong*) my_bind->buffer == 0);
12980
/* string with trailing spaces -> number */
12981
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12982
DIE_UNLESS(! *my_bind->error && *(longlong*) my_bind->buffer == 12345);
12984
/* string with trailing spaces -> double */
12985
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12986
DIE_UNLESS(! *my_bind->error && *(double*) my_bind->buffer == 12345.67);
12988
/* string with trailing junk -> double */
12989
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12991
XXX: There must be a truncation error: but it's not the way the server
12992
behaves, so let's leave it for now.
12994
DIE_UNLESS(*(double*) my_bind->buffer == 12345.67);
12996
TODO: string -> double, double -> time, double -> string (truncation
12997
errors are not supported here yet)
12998
longlong -> time/date/datetime
12999
date -> time, date -> timestamp, date -> number
13000
time -> string, time -> date, time -> timestamp,
13001
number -> date string -> date
13003
/*************** Cleanup *********************************************/
13005
mysql_stmt_close(stmt);
13007
for (my_bind= bind_array; my_bind < bind_array + bind_count; my_bind++)
13008
free(my_bind->buffer);
13011
rc= mysql_query(mysql, "drop table t1");
13015
static void test_truncation_option()
13018
const char *stmt_text;
13023
MYSQL_BIND my_bind;
13025
myheader("test_truncation_option");
13027
/* Prepare the test table */
13028
stmt_text= "select -1";
13030
stmt= mysql_stmt_init(mysql);
13031
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13032
check_execute(stmt, rc);
13033
rc= mysql_stmt_execute(stmt);
13034
check_execute(stmt, rc);
13036
bzero((char*) &my_bind, sizeof(my_bind));
13038
my_bind.buffer= (void*) &buf;
13039
my_bind.buffer_type= MYSQL_TYPE_TINY;
13040
my_bind.is_unsigned= TRUE;
13041
my_bind.error= &error;
13043
rc= mysql_stmt_bind_result(stmt, &my_bind);
13044
check_execute(stmt, rc);
13045
rc= mysql_stmt_fetch(stmt);
13046
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
13048
rc= mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option);
13050
/* need to rebind for the new setting to take effect */
13051
rc= mysql_stmt_bind_result(stmt, &my_bind);
13052
check_execute(stmt, rc);
13053
rc= mysql_stmt_execute(stmt);
13054
check_execute(stmt, rc);
13055
rc= mysql_stmt_fetch(stmt);
13056
check_execute(stmt, rc);
13057
/* The only change is rc - error pointers are still filled in */
13058
DIE_UNLESS(error == 1);
13059
/* restore back the defaults */
13061
mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option);
13063
mysql_stmt_close(stmt);
13067
/* Bug#6761 - mysql_list_fields doesn't work */
13069
static void test_bug6761(void)
13071
const char *stmt_text;
13074
myheader("test_bug6761");
13076
stmt_text= "CREATE TABLE t1 (a int, b char(255), c decimal)";
13077
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13080
res= mysql_list_fields(mysql, "t1", "%");
13081
DIE_UNLESS(res && mysql_num_fields(res) == 3);
13082
mysql_free_result(res);
13084
stmt_text= "DROP TABLE t1";
13085
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13090
/* Bug#8330 - mysql_stmt_execute crashes (libmysql) */
13092
static void test_bug8330()
13094
const char *stmt_text;
13095
MYSQL_STMT *stmt[2];
13097
const char *query= "select a,b from t1 where a=?";
13098
MYSQL_BIND my_bind[2];
13101
myheader("test_bug8330");
13103
stmt_text= "drop table if exists t1";
13104
/* in case some previos test failed */
13105
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13107
stmt_text= "create table t1 (a int, b int)";
13108
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13111
bzero((char*) my_bind, sizeof(my_bind));
13112
for (i=0; i < 2; i++)
13114
stmt[i]= mysql_stmt_init(mysql);
13115
rc= mysql_stmt_prepare(stmt[i], query, strlen(query));
13116
check_execute(stmt[i], rc);
13118
my_bind[i].buffer_type= MYSQL_TYPE_LONG;
13119
my_bind[i].buffer= (void*) &lval[i];
13120
my_bind[i].is_null= 0;
13121
mysql_stmt_bind_param(stmt[i], &my_bind[i]);
13124
rc= mysql_stmt_execute(stmt[0]);
13125
check_execute(stmt[0], rc);
13127
rc= mysql_stmt_execute(stmt[1]);
13128
DIE_UNLESS(rc && mysql_stmt_errno(stmt[1]) == CR_COMMANDS_OUT_OF_SYNC);
13129
rc= mysql_stmt_execute(stmt[0]);
13130
check_execute(stmt[0], rc);
13132
mysql_stmt_close(stmt[0]);
13133
mysql_stmt_close(stmt[1]);
13135
stmt_text= "drop table t1";
13136
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13141
/* Bug#7990 - mysql_stmt_close doesn't reset mysql->net.last_error */
13143
static void test_bug7990()
13147
myheader("test_bug7990");
13149
stmt= mysql_stmt_init(mysql);
13150
rc= mysql_stmt_prepare(stmt, "foo", 3);
13152
XXX: the fact that we store errno both in STMT and in
13153
MYSQL is not documented and is subject to change in 5.0
13155
DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql));
13156
mysql_stmt_close(stmt);
13157
DIE_UNLESS(!mysql_errno(mysql));
13161
Bug #15518 - Reusing a stmt that has failed during prepare
13162
does not clear error
13165
static void test_bug15518()
13170
myheader("test_bug15518");
13172
mysql1= mysql_init(NULL);
13174
if (!mysql_real_connect(mysql1, opt_host, opt_user, opt_password,
13175
opt_db ? opt_db : "test", opt_port, opt_unix_socket,
13176
CLIENT_MULTI_STATEMENTS))
13178
fprintf(stderr, "Failed to connect to the database\n");
13182
stmt= mysql_stmt_init(mysql1);
13185
The prepare of foo should fail with errno 1064 since
13186
it's not a valid query
13188
rc= mysql_stmt_prepare(stmt, "foo", 3);
13190
fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
13191
rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
13192
DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql1));
13195
Use the same stmt and reprepare with another query that
13198
rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
13200
fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
13201
rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
13202
DIE_UNLESS(!rc || mysql_stmt_errno(stmt) || mysql_errno(mysql1));
13204
mysql_stmt_close(stmt);
13205
DIE_UNLESS(!mysql_errno(mysql1));
13208
part2, when connection to server has been closed
13209
after first prepare
13211
stmt= mysql_stmt_init(mysql1);
13212
rc= mysql_stmt_prepare(stmt, "foo", 3);
13214
fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
13215
rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
13216
DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql1));
13218
/* Close connection to server */
13219
mysql_close(mysql1);
13222
Use the same stmt and reprepare with another query that
13223
suceeds. The prepare should fail with error 2013 since
13224
connection to server has been closed.
13226
rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
13228
fprintf(stdout, "rc: %d, mysql_stmt_errno: %d\n",
13229
rc, mysql_stmt_errno(stmt));
13230
DIE_UNLESS(rc && mysql_stmt_errno(stmt));
13232
mysql_stmt_close(stmt);
13236
static void disable_general_log()
13239
rc= mysql_query(mysql, "set @@global.general_log=off");
13244
static void enable_general_log(int truncate)
13248
rc= mysql_query(mysql, "set @save_global_general_log=@@global.general_log");
13251
rc= mysql_query(mysql, "set @@global.general_log=on");
13256
rc= mysql_query(mysql, "truncate mysql.general_log");
13262
static void restore_general_log()
13265
rc= mysql_query(mysql, "set @@global.general_log=@save_global_general_log");
13270
static void test_view_sp_list_fields()
13275
myheader("test_view_sp_list_fields");
13277
rc= mysql_query(mysql, "DROP FUNCTION IF EXISTS f1");
13279
rc= mysql_query(mysql, "DROP TABLE IF EXISTS v1, t1, t2");
13281
rc= mysql_query(mysql, "DROP VIEW IF EXISTS v1, t1, t2");
13283
rc= mysql_query(mysql, "create function f1 () returns int return 5");
13285
rc= mysql_query(mysql, "create table t1 (s1 char,s2 char)");
13287
rc= mysql_query(mysql, "create table t2 (s1 int);");
13289
rc= mysql_query(mysql, "create view v1 as select s2,sum(s1) - \
13290
count(s2) as vx from t1 group by s2 having sum(s1) - count(s2) < (select f1() \
13293
res= mysql_list_fields(mysql, "v1", NullS);
13294
DIE_UNLESS(res != 0 && mysql_num_fields(res) != 0);
13295
rc= mysql_query(mysql, "DROP FUNCTION f1");
13297
rc= mysql_query(mysql, "DROP VIEW v1");
13299
rc= mysql_query(mysql, "DROP TABLE t1, t2");
13300
mysql_free_result(res);
13307
Test mysql_real_escape_string() with gbk charset
13309
The important part is that 0x27 (') is the second-byte in a invalid
13310
two-byte GBK character here. But 0xbf5c is a valid GBK character, so
13311
it needs to be escaped as 0x5cbf27
13313
#define TEST_BUG8378_IN "\xef\xbb\xbf\x27\xbf\x10"
13314
#define TEST_BUG8378_OUT "\xef\xbb\x5c\xbf\x5c\x27\x5c\xbf\x10"
13316
static void test_bug8378()
13318
#if defined(HAVE_CHARSET_gbk) && !defined(EMBEDDED_LIBRARY)
13320
char out[9]; /* strlen(TEST_BUG8378)*2+1 */
13324
myheader("test_bug8378");
13327
fprintf(stdout, "\n Establishing a test connection ...");
13328
if (!(lmysql= mysql_init(NULL)))
13330
myerror("mysql_init() failed");
13333
if (mysql_options(lmysql, MYSQL_SET_CHARSET_NAME, "gbk"))
13335
myerror("mysql_options() failed");
13338
if (!(mysql_real_connect(lmysql, opt_host, opt_user,
13339
opt_password, current_db, opt_port,
13340
opt_unix_socket, 0)))
13342
myerror("connection failed");
13346
fprintf(stdout, "OK");
13348
len= mysql_real_escape_string(lmysql, out, TEST_BUG8378_IN, 4);
13350
/* No escaping should have actually happened. */
13351
DIE_UNLESS(memcmp(out, TEST_BUG8378_OUT, len) == 0);
13353
sprintf(buf, "SELECT '%s'", out);
13355
rc=mysql_real_query(lmysql, buf, strlen(buf));
13358
mysql_close(lmysql);
13363
static void test_bug8722()
13367
const char *stmt_text;
13369
myheader("test_bug8722");
13370
/* Prepare test data */
13371
stmt_text= "drop table if exists t1, v1";
13372
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13374
stmt_text= "CREATE TABLE t1 (c1 varchar(10), c2 varchar(10), c3 varchar(10),"
13375
" c4 varchar(10), c5 varchar(10), c6 varchar(10),"
13376
" c7 varchar(10), c8 varchar(10), c9 varchar(10),"
13377
"c10 varchar(10))";
13378
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13380
stmt_text= "INSERT INTO t1 VALUES (1,2,3,4,5,6,7,8,9,10)";
13381
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13383
stmt_text= "CREATE VIEW v1 AS SELECT * FROM t1";
13384
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13386
/* Note: if you uncomment following block everything works fine */
13388
rc= mysql_query(mysql, "sellect * from v1");
13390
mysql_free_result(mysql_store_result(mysql));
13393
stmt= mysql_stmt_init(mysql);
13394
stmt_text= "select * from v1";
13395
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13396
check_execute(stmt, rc);
13397
mysql_stmt_close(stmt);
13398
stmt_text= "drop table if exists t1, v1";
13399
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13404
MYSQL_STMT *open_cursor(const char *query)
13407
const ulong type= (ulong)CURSOR_TYPE_READ_ONLY;
13409
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
13410
rc= mysql_stmt_prepare(stmt, query, strlen(query));
13411
check_execute(stmt, rc);
13413
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
13418
static void test_bug8880()
13420
MYSQL_STMT *stmt_list[2], **stmt;
13421
MYSQL_STMT **stmt_list_end= (MYSQL_STMT**) stmt_list + 2;
13424
myheader("test_bug8880");
13426
mysql_query(mysql, "drop table if exists t1");
13427
mysql_query(mysql, "create table t1 (a int not null primary key, b int)");
13428
rc= mysql_query(mysql, "insert into t1 values (1,1)");
13429
myquery(rc); /* one check is enough */
13431
when inserting 2 rows everything works well
13432
mysql_query(mysql, "INSERT INTO t1 VALUES (1,1),(2,2)");
13434
for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
13435
*stmt= open_cursor("select a from t1");
13436
for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
13438
rc= mysql_stmt_execute(*stmt);
13439
check_execute(*stmt, rc);
13441
for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
13442
mysql_stmt_close(*stmt);
13446
static void test_bug9159()
13450
const char *stmt_text= "select a, b from t1";
13451
const unsigned long type= CURSOR_TYPE_READ_ONLY;
13453
myheader("test_bug9159");
13455
mysql_query(mysql, "drop table if exists t1");
13456
mysql_query(mysql, "create table t1 (a int not null primary key, b int)");
13457
rc= mysql_query(mysql, "insert into t1 values (1,1)");
13460
stmt= mysql_stmt_init(mysql);
13461
mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13462
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void *)&type);
13464
mysql_stmt_execute(stmt);
13465
mysql_stmt_close(stmt);
13466
rc= mysql_query(mysql, "drop table if exists t1");
13471
/* Crash when opening a cursor to a query with DISTICNT and no key */
13473
static void test_bug9520()
13476
MYSQL_BIND my_bind[1];
13479
int rc, row_count= 0;
13481
myheader("test_bug9520");
13483
mysql_query(mysql, "drop table if exists t1");
13484
mysql_query(mysql, "create table t1 (a char(5), b char(5), c char(5),"
13485
" primary key (a, b, c))");
13486
rc= mysql_query(mysql, "insert into t1 values ('x', 'y', 'z'), "
13487
" ('a', 'b', 'c'), ('k', 'l', 'm')");
13490
stmt= open_cursor("select distinct b from t1");
13494
stmt= open_cursor("select distinct a from t1");
13497
rc= mysql_stmt_execute(stmt);
13498
check_execute(stmt, rc);
13500
bzero((char*) my_bind, sizeof(my_bind));
13501
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
13502
my_bind[0].buffer= (char*) a;
13503
my_bind[0].buffer_length= sizeof(a);
13504
my_bind[0].length= &a_len;
13506
mysql_stmt_bind_result(stmt, my_bind);
13508
while (!(rc= mysql_stmt_fetch(stmt)))
13511
DIE_UNLESS(rc == MYSQL_NO_DATA);
13514
printf("Fetched %d rows\n", row_count);
13515
DBUG_ASSERT(row_count == 3);
13517
mysql_stmt_close(stmt);
13519
rc= mysql_query(mysql, "drop table t1");
13525
We can't have more than one cursor open for a prepared statement.
13526
Test re-executions of a PS with cursor; mysql_stmt_reset must close
13527
the cursor attached to the statement, if there is one.
13530
static void test_bug9478()
13533
MYSQL_BIND my_bind[1];
13537
DBUG_ENTER("test_bug9478");
13539
myheader("test_bug9478");
13541
mysql_query(mysql, "drop table if exists t1");
13542
mysql_query(mysql, "create table t1 (id integer not null primary key, "
13543
" name varchar(20) not null)");
13544
rc= mysql_query(mysql, "insert into t1 (id, name) values "
13545
" (1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
13548
stmt= open_cursor("select name from t1 where id=2");
13550
bzero((char*) my_bind, sizeof(my_bind));
13551
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
13552
my_bind[0].buffer= (char*) a;
13553
my_bind[0].buffer_length= sizeof(a);
13554
my_bind[0].length= &a_len;
13555
mysql_stmt_bind_result(stmt, my_bind);
13557
for (i= 0; i < 5; i++)
13559
rc= mysql_stmt_execute(stmt);
13560
check_execute(stmt, rc);
13561
rc= mysql_stmt_fetch(stmt);
13562
check_execute(stmt, rc);
13563
if (!opt_silent && i == 0)
13564
printf("Fetched row: %s\n", a);
13567
The query above is a one-row result set. Therefore, there is no
13568
cursor associated with it, as the server won't bother with opening
13569
a cursor for a one-row result set. The first row was read from the
13570
server in the fetch above. But there is eof packet pending in the
13571
network. mysql_stmt_execute will flush the packet and successfully
13572
execute the statement.
13575
rc= mysql_stmt_execute(stmt);
13576
check_execute(stmt, rc);
13578
rc= mysql_stmt_fetch(stmt);
13579
check_execute(stmt, rc);
13580
if (!opt_silent && i == 0)
13581
printf("Fetched row: %s\n", a);
13582
rc= mysql_stmt_fetch(stmt);
13583
DIE_UNLESS(rc == MYSQL_NO_DATA);
13587
/* Fill in the fetch packet */
13588
int4store(buff, stmt->stmt_id);
13589
buff[4]= 1; /* prefetch rows */
13590
rc= ((*mysql->methods->advanced_command)(mysql, COM_STMT_FETCH,
13592
sizeof(buff), 0,0,1,NULL) ||
13593
(*mysql->methods->read_query_result)(mysql));
13595
if (!opt_silent && i == 0)
13596
printf("Got error (as expected): %s\n", mysql_error(mysql));
13599
rc= mysql_stmt_execute(stmt);
13600
check_execute(stmt, rc);
13602
rc= mysql_stmt_fetch(stmt);
13603
check_execute(stmt, rc);
13604
if (!opt_silent && i == 0)
13605
printf("Fetched row: %s\n", a);
13607
rc= mysql_stmt_reset(stmt);
13608
check_execute(stmt, rc);
13609
rc= mysql_stmt_fetch(stmt);
13610
DIE_UNLESS(rc && mysql_stmt_errno(stmt));
13611
if (!opt_silent && i == 0)
13612
printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
13614
rc= mysql_stmt_close(stmt);
13615
DIE_UNLESS(rc == 0);
13617
/* Test the case with a server side cursor */
13618
stmt= open_cursor("select name from t1");
13620
mysql_stmt_bind_result(stmt, my_bind);
13622
for (i= 0; i < 5; i++)
13624
DBUG_PRINT("loop",("i: %d", i));
13625
rc= mysql_stmt_execute(stmt);
13626
check_execute(stmt, rc);
13627
rc= mysql_stmt_fetch(stmt);
13628
check_execute(stmt, rc);
13629
if (!opt_silent && i == 0)
13630
printf("Fetched row: %s\n", a);
13631
rc= mysql_stmt_execute(stmt);
13632
check_execute(stmt, rc);
13634
while (! (rc= mysql_stmt_fetch(stmt)))
13636
if (!opt_silent && i == 0)
13637
printf("Fetched row: %s\n", a);
13639
DIE_UNLESS(rc == MYSQL_NO_DATA);
13641
rc= mysql_stmt_execute(stmt);
13642
check_execute(stmt, rc);
13644
rc= mysql_stmt_fetch(stmt);
13645
check_execute(stmt, rc);
13646
if (!opt_silent && i == 0)
13647
printf("Fetched row: %s\n", a);
13649
rc= mysql_stmt_reset(stmt);
13650
check_execute(stmt, rc);
13651
rc= mysql_stmt_fetch(stmt);
13652
DIE_UNLESS(rc && mysql_stmt_errno(stmt));
13653
if (!opt_silent && i == 0)
13654
printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
13657
rc= mysql_stmt_close(stmt);
13658
DIE_UNLESS(rc == 0);
13660
rc= mysql_query(mysql, "drop table t1");
13667
Error message is returned for unsupported features.
13668
Test also cursors with non-default PREFETCH_ROWS
13671
static void test_bug9643()
13674
MYSQL_BIND my_bind[1];
13677
const char *stmt_text;
13680
ulong prefetch_rows= 5;
13682
myheader("test_bug9643");
13684
mysql_query(mysql, "drop table if exists t1");
13685
mysql_query(mysql, "create table t1 (id integer not null primary key)");
13686
rc= mysql_query(mysql, "insert into t1 (id) values "
13687
" (1), (2), (3), (4), (5), (6), (7), (8), (9)");
13690
stmt= mysql_stmt_init(mysql);
13691
/* Not implemented in 5.0 */
13692
type= (ulong) CURSOR_TYPE_SCROLLABLE;
13693
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
13696
printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
13698
type= (ulong) CURSOR_TYPE_READ_ONLY;
13699
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
13700
check_execute(stmt, rc);
13701
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS,
13702
(void*) &prefetch_rows);
13703
check_execute(stmt, rc);
13704
stmt_text= "select * from t1";
13705
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13706
check_execute(stmt, rc);
13708
bzero((char*) my_bind, sizeof(my_bind));
13709
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
13710
my_bind[0].buffer= (void*) &a;
13711
my_bind[0].buffer_length= sizeof(a);
13712
mysql_stmt_bind_result(stmt, my_bind);
13714
rc= mysql_stmt_execute(stmt);
13715
check_execute(stmt, rc);
13717
while ((rc= mysql_stmt_fetch(stmt)) == 0)
13719
DIE_UNLESS(num_rows == 9);
13721
rc= mysql_stmt_close(stmt);
13722
DIE_UNLESS(rc == 0);
13724
rc= mysql_query(mysql, "drop table t1");
13729
Bug#11111: fetch from view returns wrong data
13732
static void test_bug11111()
13735
MYSQL_BIND my_bind[2];
13740
const char *query= "SELECT DISTINCT f1,ff2 FROM v1";
13742
myheader("test_bug11111");
13744
rc= mysql_query(mysql, "drop table if exists t1, t2, v1");
13746
rc= mysql_query(mysql, "drop view if exists t1, t2, v1");
13748
rc= mysql_query(mysql, "create table t1 (f1 int, f2 int)");
13750
rc= mysql_query(mysql, "create table t2 (ff1 int, ff2 int)");
13752
rc= mysql_query(mysql, "create view v1 as select * from t1, t2 where f1=ff1");
13754
rc= mysql_query(mysql, "insert into t1 values (1,1), (2,2), (3,3)");
13756
rc= mysql_query(mysql, "insert into t2 values (1,1), (2,2), (3,3)");
13759
stmt= mysql_stmt_init(mysql);
13761
mysql_stmt_prepare(stmt, query, strlen(query));
13762
mysql_stmt_execute(stmt);
13764
bzero((char*) my_bind, sizeof(my_bind));
13765
for (i=0; i < 2; i++)
13767
my_bind[i].buffer_type= MYSQL_TYPE_STRING;
13768
my_bind[i].buffer= (uchar* *)&buf[i];
13769
my_bind[i].buffer_length= 20;
13770
my_bind[i].length= &len[i];
13773
rc= mysql_stmt_bind_result(stmt, my_bind);
13774
check_execute(stmt, rc);
13776
rc= mysql_stmt_fetch(stmt);
13777
check_execute(stmt, rc);
13779
printf("return: %s", buf[1]);
13780
DIE_UNLESS(!strcmp(buf[1],"1"));
13781
mysql_stmt_close(stmt);
13782
rc= mysql_query(mysql, "drop view v1");
13784
rc= mysql_query(mysql, "drop table t1, t2");
13789
Check that proper cleanups are done for prepared statement when
13790
fetching thorugh a cursor.
13793
static void test_bug10729()
13796
MYSQL_BIND my_bind[1];
13799
const char *stmt_text;
13801
const char *name_array[3]= { "aaa", "bbb", "ccc" };
13804
myheader("test_bug10729");
13806
mysql_query(mysql, "drop table if exists t1");
13807
mysql_query(mysql, "create table t1 (id integer not null primary key,"
13808
"name VARCHAR(20) NOT NULL)");
13809
rc= mysql_query(mysql, "insert into t1 (id, name) values "
13810
"(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
13813
stmt= mysql_stmt_init(mysql);
13815
type= (ulong) CURSOR_TYPE_READ_ONLY;
13816
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
13817
check_execute(stmt, rc);
13818
stmt_text= "select name from t1";
13819
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13820
check_execute(stmt, rc);
13822
bzero((char*) my_bind, sizeof(my_bind));
13823
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
13824
my_bind[0].buffer= (void*) a;
13825
my_bind[0].buffer_length= sizeof(a);
13826
mysql_stmt_bind_result(stmt, my_bind);
13828
for (i= 0; i < 3; i++)
13831
rc= mysql_stmt_execute(stmt);
13832
check_execute(stmt, rc);
13833
while ((rc= mysql_stmt_fetch(stmt)) == 0)
13835
DIE_UNLESS(strcmp(a, name_array[row_no]) == 0);
13837
printf("%d: %s\n", row_no, a);
13840
DIE_UNLESS(rc == MYSQL_NO_DATA);
13842
rc= mysql_stmt_close(stmt);
13843
DIE_UNLESS(rc == 0);
13845
rc= mysql_query(mysql, "drop table t1");
13851
Check that mysql_next_result works properly in case when one of
13852
the statements used in a multi-statement query is erroneous
13855
static void test_bug9992()
13861
myheader("test_bug9992");
13864
printf("Establishing a connection with option CLIENT_MULTI_STATEMENTS..\n");
13866
mysql1= mysql_init(NULL);
13868
if (!mysql_real_connect(mysql1, opt_host, opt_user, opt_password,
13869
opt_db ? opt_db : "test", opt_port, opt_unix_socket,
13870
CLIENT_MULTI_STATEMENTS))
13872
fprintf(stderr, "Failed to connect to the database\n");
13877
/* Sic: SHOW DATABASE is incorrect syntax. */
13878
rc= mysql_query(mysql1, "SHOW TABLES; SHOW DATABASE; SELECT 1;");
13882
fprintf(stderr, "[%d] %s\n", mysql_errno(mysql1), mysql_error(mysql1));
13887
printf("Testing mysql_store_result/mysql_next_result..\n");
13889
res= mysql_store_result(mysql1);
13891
mysql_free_result(res);
13892
rc= mysql_next_result(mysql1);
13893
DIE_UNLESS(rc == 1); /* Got errors, as expected */
13896
fprintf(stdout, "Got error, as expected:\n [%d] %s\n",
13897
mysql_errno(mysql1), mysql_error(mysql1));
13899
mysql_close(mysql1);
13902
/* Bug#10736: cursors and subqueries, memroot management */
13904
static void test_bug10736()
13907
MYSQL_BIND my_bind[1];
13910
const char *stmt_text;
13914
myheader("test_bug10736");
13916
mysql_query(mysql, "drop table if exists t1");
13917
mysql_query(mysql, "create table t1 (id integer not null primary key,"
13918
"name VARCHAR(20) NOT NULL)");
13919
rc= mysql_query(mysql, "insert into t1 (id, name) values "
13920
"(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
13923
stmt= mysql_stmt_init(mysql);
13925
type= (ulong) CURSOR_TYPE_READ_ONLY;
13926
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
13927
check_execute(stmt, rc);
13928
stmt_text= "select name from t1 where name=(select name from t1 where id=2)";
13929
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13930
check_execute(stmt, rc);
13932
bzero((char*) my_bind, sizeof(my_bind));
13933
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
13934
my_bind[0].buffer= (void*) a;
13935
my_bind[0].buffer_length= sizeof(a);
13936
mysql_stmt_bind_result(stmt, my_bind);
13938
for (i= 0; i < 3; i++)
13941
rc= mysql_stmt_execute(stmt);
13942
check_execute(stmt, rc);
13943
while ((rc= mysql_stmt_fetch(stmt)) == 0)
13946
printf("%d: %s\n", row_no, a);
13949
DIE_UNLESS(rc == MYSQL_NO_DATA);
13951
rc= mysql_stmt_close(stmt);
13952
DIE_UNLESS(rc == 0);
13954
rc= mysql_query(mysql, "drop table t1");
13958
/* Bug#10794: cursors, packets out of order */
13960
static void test_bug10794()
13962
MYSQL_STMT *stmt, *stmt1;
13963
MYSQL_BIND my_bind[2];
13968
const char *stmt_text;
13972
myheader("test_bug10794");
13974
mysql_query(mysql, "drop table if exists t1");
13975
mysql_query(mysql, "create table t1 (id integer not null primary key,"
13976
"name varchar(20) not null)");
13977
stmt= mysql_stmt_init(mysql);
13978
stmt_text= "insert into t1 (id, name) values (?, ?)";
13979
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13980
check_execute(stmt, rc);
13981
bzero((char*) my_bind, sizeof(my_bind));
13982
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
13983
my_bind[0].buffer= (void*) &id_val;
13984
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
13985
my_bind[1].buffer= (void*) a;
13986
my_bind[1].length= &a_len;
13987
rc= mysql_stmt_bind_param(stmt, my_bind);
13988
check_execute(stmt, rc);
13989
for (i= 0; i < 42; i++)
13992
sprintf(a, "a%d", i);
13993
a_len= strlen(a); /* safety against broken sprintf */
13994
rc= mysql_stmt_execute(stmt);
13995
check_execute(stmt, rc);
13997
stmt_text= "select name from t1";
13998
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13999
type= (ulong) CURSOR_TYPE_READ_ONLY;
14000
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14001
stmt1= mysql_stmt_init(mysql);
14002
mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14003
bzero((char*) my_bind, sizeof(my_bind));
14004
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
14005
my_bind[0].buffer= (void*) a;
14006
my_bind[0].buffer_length= sizeof(a);
14007
my_bind[0].length= &a_len;
14008
rc= mysql_stmt_bind_result(stmt, my_bind);
14009
check_execute(stmt, rc);
14010
rc= mysql_stmt_execute(stmt);
14011
check_execute(stmt, rc);
14012
rc= mysql_stmt_fetch(stmt);
14013
check_execute(stmt, rc);
14015
printf("Fetched row from stmt: %s\n", a);
14016
/* Don't optimize: an attribute of the original test case */
14017
mysql_stmt_free_result(stmt);
14018
mysql_stmt_reset(stmt);
14019
stmt_text= "select name from t1 where id=10";
14020
rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
14021
check_execute(stmt1, rc);
14022
rc= mysql_stmt_bind_result(stmt1, my_bind);
14023
check_execute(stmt1, rc);
14024
rc= mysql_stmt_execute(stmt1);
14027
rc= mysql_stmt_fetch(stmt1);
14028
if (rc == MYSQL_NO_DATA)
14031
printf("End of data in stmt1\n");
14034
check_execute(stmt1, rc);
14036
printf("Fetched row from stmt1: %s\n", a);
14038
mysql_stmt_close(stmt);
14039
mysql_stmt_close(stmt1);
14041
rc= mysql_query(mysql, "drop table t1");
14046
/* Bug#11172: cursors, crash on a fetch from a datetime column */
14048
static void test_bug11172()
14051
MYSQL_BIND bind_in[1], bind_out[2];
14054
const char *stmt_text;
14058
myheader("test_bug11172");
14060
mysql_query(mysql, "drop table if exists t1");
14061
mysql_query(mysql, "create table t1 (id integer not null primary key,"
14062
"hired date not null)");
14063
rc= mysql_query(mysql,
14064
"insert into t1 (id, hired) values (1, '1933-08-24'), "
14065
"(2, '1965-01-01'), (3, '1949-08-17'), (4, '1945-07-07'), "
14066
"(5, '1941-05-15'), (6, '1978-09-15'), (7, '1936-03-28')");
14068
stmt= mysql_stmt_init(mysql);
14069
stmt_text= "SELECT id, hired FROM t1 WHERE hired=?";
14070
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14071
check_execute(stmt, rc);
14073
type= (ulong) CURSOR_TYPE_READ_ONLY;
14074
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14076
bzero((char*) bind_in, sizeof(bind_in));
14077
bzero((char*) bind_out, sizeof(bind_out));
14078
bzero((char*) &hired, sizeof(hired));
14082
bind_in[0].buffer_type= MYSQL_TYPE_DATE;
14083
bind_in[0].buffer= (void*) &hired;
14084
bind_in[0].buffer_length= sizeof(hired);
14085
bind_out[0].buffer_type= MYSQL_TYPE_LONG;
14086
bind_out[0].buffer= (void*) &id;
14087
bind_out[1]= bind_in[0];
14089
for (i= 0; i < 3; i++)
14091
rc= mysql_stmt_bind_param(stmt, bind_in);
14092
check_execute(stmt, rc);
14093
rc= mysql_stmt_bind_result(stmt, bind_out);
14094
check_execute(stmt, rc);
14095
rc= mysql_stmt_execute(stmt);
14096
check_execute(stmt, rc);
14097
while ((rc= mysql_stmt_fetch(stmt)) == 0)
14100
printf("fetched data %d:%d-%d-%d\n", id,
14101
hired.year, hired.month, hired.day);
14103
DIE_UNLESS(rc == MYSQL_NO_DATA);
14104
if (!mysql_stmt_free_result(stmt))
14105
mysql_stmt_reset(stmt);
14107
mysql_stmt_close(stmt);
14108
mysql_rollback(mysql);
14109
mysql_rollback(mysql);
14111
rc= mysql_query(mysql, "drop table t1");
14116
/* Bug#11656: cursors, crash on a fetch from a query with distinct. */
14118
static void test_bug11656()
14121
MYSQL_BIND my_bind[2];
14123
const char *stmt_text;
14128
myheader("test_bug11656");
14130
mysql_query(mysql, "drop table if exists t1");
14132
rc= mysql_query(mysql, "create table t1 ("
14133
"server varchar(40) not null, "
14134
"test_kind varchar(1) not null, "
14135
"test_id varchar(30) not null , "
14136
"primary key (server,test_kind,test_id))");
14139
stmt_text= "select distinct test_kind, test_id from t1 "
14140
"where server in (?, ?)";
14141
stmt= mysql_stmt_init(mysql);
14142
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14143
check_execute(stmt, rc);
14144
type= (ulong) CURSOR_TYPE_READ_ONLY;
14145
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14147
bzero((char*) my_bind, sizeof(my_bind));
14148
strmov(buf[0], "pcint502_MY2");
14149
strmov(buf[1], "*");
14150
for (i=0; i < 2; i++)
14152
my_bind[i].buffer_type= MYSQL_TYPE_STRING;
14153
my_bind[i].buffer= (uchar* *)&buf[i];
14154
my_bind[i].buffer_length= strlen(buf[i]);
14156
mysql_stmt_bind_param(stmt, my_bind);
14158
rc= mysql_stmt_execute(stmt);
14159
check_execute(stmt, rc);
14161
rc= mysql_stmt_fetch(stmt);
14162
DIE_UNLESS(rc == MYSQL_NO_DATA);
14164
mysql_stmt_close(stmt);
14165
rc= mysql_query(mysql, "drop table t1");
14171
Check that the server signals when NO_BACKSLASH_ESCAPES mode is in effect,
14172
and mysql_real_escape_string() does the right thing as a result.
14175
static void test_bug10214()
14180
myheader("test_bug10214");
14182
DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES));
14184
len= mysql_real_escape_string(mysql, out, "a'b\\c", 5);
14185
DIE_UNLESS(memcmp(out, "a\\'b\\\\c", len) == 0);
14187
mysql_query(mysql, "set sql_mode='NO_BACKSLASH_ESCAPES'");
14188
DIE_UNLESS(mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES);
14190
len= mysql_real_escape_string(mysql, out, "a'b\\c", 5);
14191
DIE_UNLESS(memcmp(out, "a''b\\c", len) == 0);
14193
mysql_query(mysql, "set sql_mode=''");
14196
static void test_client_character_set()
14198
MY_CHARSET_INFO cs;
14199
char *csname= (char*) "utf8";
14200
char *csdefault= (char*)mysql_character_set_name(mysql);
14203
myheader("test_client_character_set");
14205
rc= mysql_set_character_set(mysql, csname);
14206
DIE_UNLESS(rc == 0);
14208
mysql_get_character_set_info(mysql, &cs);
14209
DIE_UNLESS(!strcmp(cs.csname, "utf8"));
14210
DIE_UNLESS(!strcmp(cs.name, "utf8_general_ci"));
14211
/* Restore the default character set */
14212
rc= mysql_set_character_set(mysql, csdefault);
14216
/* Test correct max length for MEDIUMTEXT and LONGTEXT columns */
14218
static void test_bug9735()
14223
myheader("test_bug9735");
14225
rc= mysql_query(mysql, "drop table if exists t1");
14227
rc= mysql_query(mysql, "create table t1 (a mediumtext, b longtext) "
14228
"character set latin1");
14230
rc= mysql_query(mysql, "select * from t1");
14232
res= mysql_store_result(mysql);
14233
verify_prepare_field(res, 0, "a", "a", MYSQL_TYPE_BLOB,
14234
"t1", "t1", current_db, (1U << 24)-1, 0);
14235
verify_prepare_field(res, 1, "b", "b", MYSQL_TYPE_BLOB,
14236
"t1", "t1", current_db, ~0U, 0);
14237
mysql_free_result(res);
14238
rc= mysql_query(mysql, "drop table t1");
14243
/* Bug#11183 "mysql_stmt_reset() doesn't reset information about error" */
14245
static void test_bug11183()
14249
char bug_statement[]= "insert into t1 values (1)";
14251
myheader("test_bug11183");
14253
mysql_query(mysql, "drop table t1 if exists");
14254
mysql_query(mysql, "create table t1 (a int)");
14256
stmt= mysql_stmt_init(mysql);
14257
DIE_UNLESS(stmt != 0);
14259
rc= mysql_stmt_prepare(stmt, bug_statement, strlen(bug_statement));
14260
check_execute(stmt, rc);
14262
rc= mysql_query(mysql, "drop table t1");
14265
/* Trying to execute statement that should fail on execute stage */
14266
rc= mysql_stmt_execute(stmt);
14269
mysql_stmt_reset(stmt);
14270
DIE_UNLESS(mysql_stmt_errno(stmt) == 0);
14272
mysql_query(mysql, "create table t1 (a int)");
14274
/* Trying to execute statement that should pass ok */
14275
if (mysql_stmt_execute(stmt))
14277
mysql_stmt_reset(stmt);
14278
DIE_UNLESS(mysql_stmt_errno(stmt) == 0);
14281
mysql_stmt_close(stmt);
14283
rc= mysql_query(mysql, "drop table t1");
14287
static void test_bug11037()
14291
const char *stmt_text;
14293
myheader("test_bug11037");
14295
mysql_query(mysql, "drop table if exists t1");
14297
rc= mysql_query(mysql, "create table t1 (id int not null)");
14300
rc= mysql_query(mysql, "insert into t1 values (1)");
14303
stmt_text= "select id FROM t1";
14304
stmt= mysql_stmt_init(mysql);
14305
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14307
/* expected error */
14308
rc = mysql_stmt_fetch(stmt);
14311
fprintf(stdout, "Got error, as expected:\n [%d] %s\n",
14312
mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
14314
rc= mysql_stmt_execute(stmt);
14315
check_execute(stmt, rc);
14317
rc= mysql_stmt_fetch(stmt);
14320
rc= mysql_stmt_fetch(stmt);
14321
DIE_UNLESS(rc==MYSQL_NO_DATA);
14323
rc= mysql_stmt_fetch(stmt);
14324
DIE_UNLESS(rc==MYSQL_NO_DATA);
14326
mysql_stmt_close(stmt);
14327
rc= mysql_query(mysql, "drop table t1");
14331
/* Bug#10760: cursors, crash in a fetch after rollback. */
14333
static void test_bug10760()
14336
MYSQL_BIND my_bind[1];
14338
const char *stmt_text;
14344
myheader("test_bug10760");
14346
mysql_query(mysql, "drop table if exists t1, t2");
14348
/* create tables */
14349
rc= mysql_query(mysql, "create table t1 (id integer not null primary key)"
14352
for (; i < 42; ++i)
14355
sprintf(buf, "insert into t1 (id) values (%d)", i+1);
14356
rc= mysql_query(mysql, buf);
14359
mysql_autocommit(mysql, FALSE);
14360
/* create statement */
14361
stmt= mysql_stmt_init(mysql);
14362
type= (ulong) CURSOR_TYPE_READ_ONLY;
14363
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14366
1: check that a deadlock within the same connection
14367
is resolved and an error is returned. The deadlock is modelled
14369
con1: open cursor for select * from t1;
14370
con1: insert into t1 (id) values (1)
14372
stmt_text= "select id from t1 order by 1";
14373
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14374
check_execute(stmt, rc);
14375
rc= mysql_stmt_execute(stmt);
14376
check_execute(stmt, rc);
14377
rc= mysql_query(mysql, "update t1 set id=id+100");
14379
If cursors are not materialized, the update will return an error;
14380
we mainly test that it won't deadlock.
14382
if (rc && !opt_silent)
14383
printf("Got error (as expected): %s\n", mysql_error(mysql));
14385
2: check that MyISAM tables used in cursors survive
14388
rc= mysql_rollback(mysql); /* should not close the cursor */
14390
rc= mysql_stmt_fetch(stmt);
14391
check_execute(stmt, rc);
14394
3: check that cursors to InnoDB tables are closed (for now) by
14400
printf("Testing that cursors are closed at COMMIT/ROLLBACK requires "
14405
stmt_text= "select id from t1 order by 1";
14406
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14407
check_execute(stmt, rc);
14409
rc= mysql_query(mysql, "alter table t1 engine=InnoDB");
14412
bzero(my_bind, sizeof(my_bind));
14413
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
14414
my_bind[0].buffer= (void*) id_buf;
14415
my_bind[0].buffer_length= sizeof(id_buf);
14416
my_bind[0].length= &id_len;
14417
check_execute(stmt, rc);
14418
mysql_stmt_bind_result(stmt, my_bind);
14420
rc= mysql_stmt_execute(stmt);
14421
rc= mysql_stmt_fetch(stmt);
14422
DIE_UNLESS(rc == 0);
14424
printf("Fetched row %s\n", id_buf);
14425
rc= mysql_rollback(mysql); /* should close the cursor */
14428
rc= mysql_stmt_fetch(stmt);
14431
printf("Got error (as expected): %s\n", mysql_error(mysql));
14435
mysql_stmt_close(stmt);
14436
rc= mysql_query(mysql, "drop table t1");
14438
mysql_autocommit(mysql, TRUE); /* restore default */
14441
static void test_bug12001()
14443
MYSQL *mysql_local;
14445
const char *query= "DROP TABLE IF EXISTS test_table;"
14446
"CREATE TABLE test_table(id INT);"
14447
"INSERT INTO test_table VALUES(10);"
14448
"UPDATE test_table SET id=20 WHERE id=10;"
14449
"SELECT * FROM test_table;"
14450
"INSERT INTO non_existent_table VALUES(11);";
14453
myheader("test_bug12001");
14455
if (!(mysql_local= mysql_init(NULL)))
14457
fprintf(stdout, "\n mysql_init() failed");
14461
/* Create connection that supports multi statements */
14462
if (!mysql_real_connect(mysql_local, opt_host, opt_user,
14463
opt_password, current_db, opt_port,
14464
opt_unix_socket, CLIENT_MULTI_STATEMENTS |
14465
CLIENT_MULTI_RESULTS))
14467
fprintf(stdout, "\n mysql_real_connect() failed");
14471
rc= mysql_query(mysql_local, query);
14476
if (mysql_field_count(mysql_local) &&
14477
(result= mysql_use_result(mysql_local)))
14479
mysql_free_result(result);
14482
while (!(res= mysql_next_result(mysql_local)));
14484
rc= mysql_query(mysql_local, "DROP TABLE IF EXISTS test_table");
14487
mysql_close(mysql_local);
14488
DIE_UNLESS(res==1);
14492
/* Bug#11909: wrong metadata if fetching from two cursors */
14494
static void test_bug11909()
14496
MYSQL_STMT *stmt1, *stmt2;
14497
MYSQL_BIND my_bind[7];
14499
char firstname[20], midinit[20], lastname[20], workdept[20];
14500
ulong firstname_len, midinit_len, lastname_len, workdept_len;
14504
const char *stmt_text;
14506
myheader("test_bug11909");
14508
stmt_text= "drop table if exists t1";
14509
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14512
stmt_text= "create table t1 ("
14513
" empno int(11) not null, firstname varchar(20) not null,"
14514
" midinit varchar(20) not null, lastname varchar(20) not null,"
14515
" workdept varchar(6) not null, salary double not null,"
14516
" bonus float not null, primary key (empno)"
14517
") default charset=latin1 collate=latin1_bin";
14518
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14521
stmt_text= "insert into t1 values "
14522
"(10, 'CHRISTINE', 'I', 'HAAS', 'A00', 52750, 1000), "
14523
"(20, 'MICHAEL', 'L', 'THOMPSON', 'B01', 41250, 800),"
14524
"(30, 'SALLY', 'A', 'KWAN', 'C01', 38250, 800),"
14525
"(50, 'JOHN', 'B', 'GEYER', 'E01', 40175, 800), "
14526
"(60, 'IRVING', 'F', 'STERN', 'D11', 32250, 500)";
14527
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14530
/* ****** Begin of trace ****** */
14532
stmt1= open_cursor("SELECT empno, firstname, midinit, lastname,"
14533
"workdept, salary, bonus FROM t1");
14535
bzero(my_bind, sizeof(my_bind));
14536
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
14537
my_bind[0].buffer= (void*) &empno;
14539
my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
14540
my_bind[1].buffer= (void*) firstname;
14541
my_bind[1].buffer_length= sizeof(firstname);
14542
my_bind[1].length= &firstname_len;
14544
my_bind[2].buffer_type= MYSQL_TYPE_VAR_STRING;
14545
my_bind[2].buffer= (void*) midinit;
14546
my_bind[2].buffer_length= sizeof(midinit);
14547
my_bind[2].length= &midinit_len;
14549
my_bind[3].buffer_type= MYSQL_TYPE_VAR_STRING;
14550
my_bind[3].buffer= (void*) lastname;
14551
my_bind[3].buffer_length= sizeof(lastname);
14552
my_bind[3].length= &lastname_len;
14554
my_bind[4].buffer_type= MYSQL_TYPE_VAR_STRING;
14555
my_bind[4].buffer= (void*) workdept;
14556
my_bind[4].buffer_length= sizeof(workdept);
14557
my_bind[4].length= &workdept_len;
14559
my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
14560
my_bind[5].buffer= (void*) &salary;
14562
my_bind[6].buffer_type= MYSQL_TYPE_FLOAT;
14563
my_bind[6].buffer= (void*) &bonus;
14564
rc= mysql_stmt_bind_result(stmt1, my_bind);
14565
check_execute(stmt1, rc);
14567
rc= mysql_stmt_execute(stmt1);
14568
check_execute(stmt1, rc);
14570
rc= mysql_stmt_fetch(stmt1);
14571
DIE_UNLESS(rc == 0);
14572
DIE_UNLESS(empno == 10);
14573
DIE_UNLESS(strcmp(firstname, "CHRISTINE") == 0);
14574
DIE_UNLESS(strcmp(midinit, "I") == 0);
14575
DIE_UNLESS(strcmp(lastname, "HAAS") == 0);
14576
DIE_UNLESS(strcmp(workdept, "A00") == 0);
14577
DIE_UNLESS(salary == (double) 52750.0);
14578
DIE_UNLESS(bonus == (float) 1000.0);
14580
stmt2= open_cursor("SELECT empno, firstname FROM t1");
14581
rc= mysql_stmt_bind_result(stmt2, my_bind);
14582
check_execute(stmt2, rc);
14584
rc= mysql_stmt_execute(stmt2);
14585
check_execute(stmt2, rc);
14587
rc= mysql_stmt_fetch(stmt2);
14588
DIE_UNLESS(rc == 0);
14590
DIE_UNLESS(empno == 10);
14591
DIE_UNLESS(strcmp(firstname, "CHRISTINE") == 0);
14593
rc= mysql_stmt_reset(stmt2);
14594
check_execute(stmt2, rc);
14596
/* ERROR: next statement should return 0 */
14598
rc= mysql_stmt_fetch(stmt1);
14599
DIE_UNLESS(rc == 0);
14601
mysql_stmt_close(stmt1);
14602
mysql_stmt_close(stmt2);
14603
rc= mysql_rollback(mysql);
14606
rc= mysql_query(mysql, "drop table t1");
14610
/* Cursors: opening a cursor to a compilicated query with ORDER BY */
14612
static void test_bug11901()
14614
/* MYSQL_STMT *stmt;
14615
MYSQL_BIND my_bind[2]; */
14617
/* char workdept[20];
14618
ulong workdept_len;
14620
const char *stmt_text;
14622
myheader("test_bug11901");
14624
stmt_text= "drop table if exists t1, t2";
14625
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14628
stmt_text= "create table t1 ("
14629
" empno int(11) not null, firstname varchar(20) not null,"
14630
" midinit varchar(20) not null, lastname varchar(20) not null,"
14631
" workdept varchar(6) not null, salary double not null,"
14632
" bonus float not null, primary key (empno), "
14633
" unique key (workdept, empno) "
14634
") default charset=latin1 collate=latin1_bin";
14635
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14638
stmt_text= "insert into t1 values "
14639
"(10, 'CHRISTINE', 'I', 'HAAS', 'A00', 52750, 1000),"
14640
"(20, 'MICHAEL', 'L', 'THOMPSON', 'B01', 41250, 800), "
14641
"(30, 'SALLY', 'A', 'KWAN', 'C01', 38250, 800), "
14642
"(50, 'JOHN', 'B', 'GEYER', 'E01', 40175, 800), "
14643
"(60, 'IRVING', 'F', 'STERN', 'D11', 32250, 500), "
14644
"(70, 'EVA', 'D', 'PULASKI', 'D21', 36170, 700), "
14645
"(90, 'EILEEN', 'W', 'HENDERSON', 'E11', 29750, 600), "
14646
"(100, 'THEODORE', 'Q', 'SPENSER', 'E21', 26150, 500), "
14647
"(110, 'VINCENZO', 'G', 'LUCCHESSI', 'A00', 46500, 900), "
14648
"(120, 'SEAN', '', 'O\\'CONNELL', 'A00', 29250, 600), "
14649
"(130, 'DOLORES', 'M', 'QUINTANA', 'C01', 23800, 500), "
14650
"(140, 'HEATHER', 'A', 'NICHOLLS', 'C01', 28420, 600), "
14651
"(150, 'BRUCE', '', 'ADAMSON', 'D11', 25280, 500), "
14652
"(160, 'ELIZABETH', 'R', 'PIANKA', 'D11', 22250, 400), "
14653
"(170, 'MASATOSHI', 'J', 'YOSHIMURA', 'D11', 24680, 500), "
14654
"(180, 'MARILYN', 'S', 'SCOUTTEN', 'D11', 21340, 500), "
14655
"(190, 'JAMES', 'H', 'WALKER', 'D11', 20450, 400), "
14656
"(200, 'DAVID', '', 'BROWN', 'D11', 27740, 600), "
14657
"(210, 'WILLIAM', 'T', 'JONES', 'D11', 18270, 400), "
14658
"(220, 'JENNIFER', 'K', 'LUTZ', 'D11', 29840, 600), "
14659
"(230, 'JAMES', 'J', 'JEFFERSON', 'D21', 22180, 400), "
14660
"(240, 'SALVATORE', 'M', 'MARINO', 'D21', 28760, 600), "
14661
"(250, 'DANIEL', 'S', 'SMITH', 'D21', 19180, 400), "
14662
"(260, 'SYBIL', 'P', 'JOHNSON', 'D21', 17250, 300), "
14663
"(270, 'MARIA', 'L', 'PEREZ', 'D21', 27380, 500), "
14664
"(280, 'ETHEL', 'R', 'SCHNEIDER', 'E11', 26250, 500), "
14665
"(290, 'JOHN', 'R', 'PARKER', 'E11', 15340, 300), "
14666
"(300, 'PHILIP', 'X', 'SMITH', 'E11', 17750, 400), "
14667
"(310, 'MAUDE', 'F', 'SETRIGHT', 'E11', 15900, 300), "
14668
"(320, 'RAMLAL', 'V', 'MEHTA', 'E21', 19950, 400), "
14669
"(330, 'WING', '', 'LEE', 'E21', 25370, 500), "
14670
"(340, 'JASON', 'R', 'GOUNOT', 'E21', 23840, 500)";
14672
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14675
stmt_text= "create table t2 ("
14676
" deptno varchar(6) not null, deptname varchar(20) not null,"
14677
" mgrno int(11) not null, location varchar(20) not null,"
14678
" admrdept varchar(6) not null, refcntd int(11) not null,"
14679
" refcntu int(11) not null, primary key (deptno)"
14680
") default charset=latin1 collate=latin1_bin";
14681
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14684
stmt_text= "insert into t2 values "
14685
"('A00', 'SPIFFY COMPUTER SERV', 10, '', 'A00', 0, 0), "
14686
"('B01', 'PLANNING', 20, '', 'A00', 0, 0), "
14687
"('C01', 'INFORMATION CENTER', 30, '', 'A00', 0, 0), "
14688
"('D01', 'DEVELOPMENT CENTER', 0, '', 'A00', 0, 0),"
14689
"('D11', 'MANUFACTURING SYSTEM', 60, '', 'D01', 0, 0), "
14690
"('D21', 'ADMINISTRATION SYSTE', 70, '', 'D01', 0, 0), "
14691
"('E01', 'SUPPORT SERVICES', 50, '', 'A00', 0, 0), "
14692
"('E11', 'OPERATIONS', 90, '', 'E01', 0, 0), "
14693
"('E21', 'SOFTWARE SUPPORT', 100,'', 'E01', 0, 0)";
14694
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14697
/* ****** Begin of trace ****** */
14698
/* WL#1110 - disabled test case failure - crash. */
14700
stmt= open_cursor("select t1.emp, t1.workdept "
14701
"from (t1 left join t2 on t2.deptno = t1.workdept) "
14702
"where t2.deptno in "
14703
" (select t2.deptno "
14704
" from (t1 left join t2 on t2.deptno = t1.workdept) "
14705
" where t1.empno = ?) "
14707
bzero(my_bind, sizeof(my_bind));
14709
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
14710
my_bind[0].buffer= &empno;
14711
rc= mysql_stmt_bind_param(stmt, my_bind);
14712
check_execute(stmt, rc);
14714
my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
14715
my_bind[1].buffer= (void*) workdept;
14716
my_bind[1].buffer_length= sizeof(workdept);
14717
my_bind[1].length= &workdept_len;
14719
rc= mysql_stmt_bind_result(stmt, my_bind);
14720
check_execute(stmt, rc);
14724
/* ERROR: next statement causes a server crash */
14726
rc= mysql_stmt_execute(stmt);
14727
check_execute(stmt, rc);
14729
mysql_stmt_close(stmt);
14731
rc= mysql_query(mysql, "drop table t1, t2");
14736
/* Bug#11904: mysql_stmt_attr_set CURSOR_TYPE_READ_ONLY grouping wrong result */
14738
static void test_bug11904()
14742
const char *stmt_text;
14743
const ulong type= (ulong)CURSOR_TYPE_READ_ONLY;
14744
MYSQL_BIND my_bind[2];
14746
char row_data[11]= {0};
14748
myheader("test_bug11904");
14750
/* create tables */
14751
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bug11904b");
14753
rc= mysql_query(mysql, "CREATE TABLE bug11904b (id int, name char(10), primary key(id, name))");
14756
rc= mysql_query(mysql, "INSERT INTO bug11904b VALUES (1, 'sofia'), (1,'plovdiv'),"
14757
" (1,'varna'), (2,'LA'), (2,'new york'), (3,'heidelberg'),"
14758
" (3,'berlin'), (3, 'frankfurt')");
14761
mysql_commit(mysql);
14762
/* create statement */
14763
stmt1= mysql_stmt_init(mysql);
14764
mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14766
stmt_text= "SELECT id, MIN(name) FROM bug11904b GROUP BY id";
14768
rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
14769
check_execute(stmt1, rc);
14771
memset(my_bind, 0, sizeof(my_bind));
14772
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
14773
my_bind[0].buffer=& country_id;
14774
my_bind[0].buffer_length= 0;
14775
my_bind[0].length= 0;
14777
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
14778
my_bind[1].buffer=& row_data;
14779
my_bind[1].buffer_length= sizeof(row_data) - 1;
14780
my_bind[1].length= 0;
14782
rc= mysql_stmt_bind_result(stmt1, my_bind);
14783
check_execute(stmt1, rc);
14785
rc= mysql_stmt_execute(stmt1);
14786
check_execute(stmt1, rc);
14788
rc= mysql_stmt_fetch(stmt1);
14789
check_execute(stmt1, rc);
14790
DIE_UNLESS(country_id == 1);
14791
DIE_UNLESS(memcmp(row_data, "plovdiv", 7) == 0);
14793
rc= mysql_stmt_fetch(stmt1);
14794
check_execute(stmt1, rc);
14795
DIE_UNLESS(country_id == 2);
14796
DIE_UNLESS(memcmp(row_data, "LA", 2) == 0);
14798
rc= mysql_stmt_fetch(stmt1);
14799
check_execute(stmt1, rc);
14800
DIE_UNLESS(country_id == 3);
14801
DIE_UNLESS(memcmp(row_data, "berlin", 6) == 0);
14803
rc= mysql_stmt_close(stmt1);
14804
check_execute(stmt1, rc);
14806
rc= mysql_query(mysql, "drop table bug11904b");
14811
/* Bug#12243: multiple cursors, crash in a fetch after commit. */
14813
static void test_bug12243()
14815
MYSQL_STMT *stmt1, *stmt2;
14817
const char *stmt_text;
14820
myheader("test_bug12243");
14825
printf("This test requires InnoDB.\n");
14829
/* create tables */
14830
mysql_query(mysql, "drop table if exists t1");
14831
mysql_query(mysql, "create table t1 (a int) engine=InnoDB");
14832
rc= mysql_query(mysql, "insert into t1 (a) values (1), (2)");
14834
mysql_autocommit(mysql, FALSE);
14835
/* create statement */
14836
stmt1= mysql_stmt_init(mysql);
14837
stmt2= mysql_stmt_init(mysql);
14838
type= (ulong) CURSOR_TYPE_READ_ONLY;
14839
mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14840
mysql_stmt_attr_set(stmt2, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14842
stmt_text= "select a from t1";
14844
rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
14845
check_execute(stmt1, rc);
14846
rc= mysql_stmt_execute(stmt1);
14847
check_execute(stmt1, rc);
14848
rc= mysql_stmt_fetch(stmt1);
14849
check_execute(stmt1, rc);
14851
rc= mysql_stmt_prepare(stmt2, stmt_text, strlen(stmt_text));
14852
check_execute(stmt2, rc);
14853
rc= mysql_stmt_execute(stmt2);
14854
check_execute(stmt2, rc);
14855
rc= mysql_stmt_fetch(stmt2);
14856
check_execute(stmt2, rc);
14858
rc= mysql_stmt_close(stmt1);
14859
check_execute(stmt1, rc);
14860
rc= mysql_commit(mysql);
14862
rc= mysql_stmt_fetch(stmt2);
14863
check_execute(stmt2, rc);
14865
mysql_stmt_close(stmt2);
14866
rc= mysql_query(mysql, "drop table t1");
14868
mysql_autocommit(mysql, TRUE); /* restore default */
14873
Bug#11718: query with function, join and order by returns wrong type
14876
static void test_bug11718()
14880
const char *query= "select str_to_date(concat(f3),'%Y%m%d') from t1,t2 "
14881
"where f1=f2 order by f1";
14883
myheader("test_bug11718");
14885
rc= mysql_query(mysql, "drop table if exists t1, t2");
14887
rc= mysql_query(mysql, "create table t1 (f1 int)");
14889
rc= mysql_query(mysql, "create table t2 (f2 int, f3 numeric(8))");
14891
rc= mysql_query(mysql, "insert into t1 values (1), (2)");
14893
rc= mysql_query(mysql, "insert into t2 values (1,20050101), (2,20050202)");
14895
rc= mysql_query(mysql, query);
14897
res = mysql_store_result(mysql);
14900
printf("return type: %s", (res->fields[0].type == MYSQL_TYPE_DATE)?"DATE":
14902
DIE_UNLESS(res->fields[0].type == MYSQL_TYPE_DATE);
14903
mysql_free_result(res);
14904
rc= mysql_query(mysql, "drop table t1, t2");
14910
Bug #12925: Bad handling of maximum values in getopt
14912
static void test_bug12925()
14914
myheader("test_bug12925");
14915
if (opt_getopt_ll_test)
14916
DIE_UNLESS(opt_getopt_ll_test == LL(25600*1024*1024));
14921
Bug#14210 "Simple query with > operator on large table gives server
14925
static void test_bug14210()
14929
const char *stmt_text;
14932
myheader("test_bug14210");
14934
mysql_query(mysql, "drop table if exists t1");
14936
To trigger the problem the table must be InnoDB, although the problem
14937
itself is not InnoDB related. In case the table is MyISAM this test
14940
mysql_query(mysql, "create table t1 (a varchar(255)) engine=InnoDB");
14941
rc= mysql_query(mysql, "insert into t1 (a) values (repeat('a', 256))");
14943
rc= mysql_query(mysql, "set @@session.max_heap_table_size=16384");
14944
/* Create a big enough table (more than max_heap_table_size) */
14945
for (i= 0; i < 8; i++)
14947
rc= mysql_query(mysql, "insert into t1 (a) select a from t1");
14950
/* create statement */
14951
stmt= mysql_stmt_init(mysql);
14952
type= (ulong) CURSOR_TYPE_READ_ONLY;
14953
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14955
stmt_text= "select a from t1";
14957
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14958
check_execute(stmt, rc);
14959
rc= mysql_stmt_execute(stmt);
14960
while ((rc= mysql_stmt_fetch(stmt)) == 0)
14962
DIE_UNLESS(rc == MYSQL_NO_DATA);
14964
rc= mysql_stmt_close(stmt);
14966
rc= mysql_query(mysql, "drop table t1");
14968
rc= mysql_query(mysql, "set @@session.max_heap_table_size=default");
14972
/* Bug#13488: wrong column metadata when fetching from cursor */
14974
static void test_bug13488()
14976
MYSQL_BIND my_bind[3];
14978
int rc, f1, f2, f3, i;
14979
const ulong type= CURSOR_TYPE_READ_ONLY;
14980
const char *query= "select * from t1 left join t2 on f1=f2 where f1=1";
14982
myheader("test_bug13488");
14984
rc= mysql_query(mysql, "drop table if exists t1, t2");
14986
rc= mysql_query(mysql, "create table t1 (f1 int not null primary key)");
14988
rc= mysql_query(mysql, "create table t2 (f2 int not null primary key, "
14989
"f3 int not null)");
14991
rc= mysql_query(mysql, "insert into t1 values (1), (2)");
14993
rc= mysql_query(mysql, "insert into t2 values (1,2), (2,4)");
14996
memset(my_bind, 0, sizeof(my_bind));
14997
for (i= 0; i < 3; i++)
14999
my_bind[i].buffer_type= MYSQL_TYPE_LONG;
15000
my_bind[i].buffer_length= 4;
15001
my_bind[i].length= 0;
15003
my_bind[0].buffer=&f1;
15004
my_bind[1].buffer=&f2;
15005
my_bind[2].buffer=&f3;
15007
stmt1= mysql_stmt_init(mysql);
15008
rc= mysql_stmt_attr_set(stmt1,STMT_ATTR_CURSOR_TYPE, (const void *)&type);
15009
check_execute(stmt1, rc);
15011
rc= mysql_stmt_prepare(stmt1, query, strlen(query));
15012
check_execute(stmt1, rc);
15014
rc= mysql_stmt_execute(stmt1);
15015
check_execute(stmt1, rc);
15017
rc= mysql_stmt_bind_result(stmt1, my_bind);
15018
check_execute(stmt1, rc);
15020
rc= mysql_stmt_fetch(stmt1);
15021
check_execute(stmt1, rc);
15023
rc= mysql_stmt_free_result(stmt1);
15024
check_execute(stmt1, rc);
15026
rc= mysql_stmt_reset(stmt1);
15027
check_execute(stmt1, rc);
15029
rc= mysql_stmt_close(stmt1);
15030
check_execute(stmt1, rc);
15033
printf("data is: %s", (f1 == 1 && f2 == 1 && f3 == 2)?"OK":
15035
DIE_UNLESS(f1 == 1 && f2 == 1 && f3 == 2);
15036
rc= mysql_query(mysql, "drop table t1, t2");
15041
Bug#13524: warnings of a previous command are not reset when fetching
15045
static void test_bug13524()
15049
unsigned int warning_count;
15050
const ulong type= CURSOR_TYPE_READ_ONLY;
15051
const char *query= "select * from t1";
15053
myheader("test_bug13524");
15055
rc= mysql_query(mysql, "drop table if exists t1, t2");
15057
rc= mysql_query(mysql, "create table t1 (a int not null primary key)");
15059
rc= mysql_query(mysql, "insert into t1 values (1), (2), (3), (4)");
15062
stmt= mysql_stmt_init(mysql);
15063
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
15064
check_execute(stmt, rc);
15066
rc= mysql_stmt_prepare(stmt, query, strlen(query));
15067
check_execute(stmt, rc);
15069
rc= mysql_stmt_execute(stmt);
15070
check_execute(stmt, rc);
15072
rc= mysql_stmt_fetch(stmt);
15073
check_execute(stmt, rc);
15075
warning_count= mysql_warning_count(mysql);
15076
DIE_UNLESS(warning_count == 0);
15078
/* Check that DROP TABLE produced a warning (no such table) */
15079
rc= mysql_query(mysql, "drop table if exists t2");
15081
warning_count= mysql_warning_count(mysql);
15082
DIE_UNLESS(warning_count == 1);
15085
Check that fetch from a cursor cleared the warning from the previous
15088
rc= mysql_stmt_fetch(stmt);
15089
check_execute(stmt, rc);
15090
warning_count= mysql_warning_count(mysql);
15091
DIE_UNLESS(warning_count == 0);
15094
mysql_stmt_close(stmt);
15095
rc= mysql_query(mysql, "drop table t1");
15100
Bug#14845 "mysql_stmt_fetch returns MYSQL_NO_DATA when COUNT(*) is 0"
15103
static void test_bug14845()
15107
const ulong type= CURSOR_TYPE_READ_ONLY;
15108
const char *query= "select count(*) from t1 where 1 = 0";
15110
myheader("test_bug14845");
15112
rc= mysql_query(mysql, "drop table if exists t1");
15114
rc= mysql_query(mysql, "create table t1 (id int(11) default null, "
15115
"name varchar(20) default null)"
15116
"engine=MyISAM DEFAULT CHARSET=utf8");
15118
rc= mysql_query(mysql, "insert into t1 values (1,'abc'),(2,'def')");
15121
stmt= mysql_stmt_init(mysql);
15122
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
15123
check_execute(stmt, rc);
15125
rc= mysql_stmt_prepare(stmt, query, strlen(query));
15126
check_execute(stmt, rc);
15128
rc= mysql_stmt_execute(stmt);
15129
check_execute(stmt, rc);
15131
rc= mysql_stmt_fetch(stmt);
15132
DIE_UNLESS(rc == 0);
15134
rc= mysql_stmt_fetch(stmt);
15135
DIE_UNLESS(rc == MYSQL_NO_DATA);
15138
mysql_stmt_close(stmt);
15139
rc= mysql_query(mysql, "drop table t1");
15145
Bug #15510: mysql_warning_count returns 0 after mysql_stmt_fetch which
15148
static void test_bug15510()
15152
const char *query= "select 1 from dual where 1/0";
15154
myheader("test_bug15510");
15156
rc= mysql_query(mysql, "set @@sql_mode='ERROR_FOR_DIVISION_BY_ZERO'");
15159
stmt= mysql_stmt_init(mysql);
15161
rc= mysql_stmt_prepare(stmt, query, strlen(query));
15162
check_execute(stmt, rc);
15164
rc= mysql_stmt_execute(stmt);
15165
check_execute(stmt, rc);
15167
rc= mysql_stmt_fetch(stmt);
15168
DIE_UNLESS(mysql_warning_count(mysql));
15171
mysql_stmt_close(stmt);
15172
rc= mysql_query(mysql, "set @@sql_mode=''");
15177
/* Test MYSQL_OPT_RECONNECT, Bug#15719 */
15179
static void test_opt_reconnect()
15182
my_bool my_true= TRUE;
15184
myheader("test_opt_reconnect");
15186
if (!(lmysql= mysql_init(NULL)))
15188
myerror("mysql_init() failed");
15193
fprintf(stdout, "reconnect before mysql_options: %d\n", lmysql->reconnect);
15194
DIE_UNLESS(lmysql->reconnect == 0);
15196
if (mysql_options(lmysql, MYSQL_OPT_RECONNECT, &my_true))
15198
myerror("mysql_options failed: unknown option MYSQL_OPT_RECONNECT\n");
15202
/* reconnect should be 1 */
15204
fprintf(stdout, "reconnect after mysql_options: %d\n", lmysql->reconnect);
15205
DIE_UNLESS(lmysql->reconnect == 1);
15207
if (!(mysql_real_connect(lmysql, opt_host, opt_user,
15208
opt_password, current_db, opt_port,
15209
opt_unix_socket, 0)))
15211
myerror("connection failed");
15215
/* reconnect should still be 1 */
15217
fprintf(stdout, "reconnect after mysql_real_connect: %d\n",
15218
lmysql->reconnect);
15219
DIE_UNLESS(lmysql->reconnect == 1);
15221
mysql_close(lmysql);
15223
if (!(lmysql= mysql_init(NULL)))
15225
myerror("mysql_init() failed");
15230
fprintf(stdout, "reconnect before mysql_real_connect: %d\n", lmysql->reconnect);
15231
DIE_UNLESS(lmysql->reconnect == 0);
15233
if (!(mysql_real_connect(lmysql, opt_host, opt_user,
15234
opt_password, current_db, opt_port,
15235
opt_unix_socket, 0)))
15237
myerror("connection failed");
15241
/* reconnect should still be 0 */
15243
fprintf(stdout, "reconnect after mysql_real_connect: %d\n",
15244
lmysql->reconnect);
15245
DIE_UNLESS(lmysql->reconnect == 0);
15247
mysql_close(lmysql);
15251
#ifndef EMBEDDED_LIBRARY
15253
static void test_bug12744()
15255
MYSQL_STMT *prep_stmt = NULL;
15258
myheader("test_bug12744");
15260
lmysql= mysql_init(NULL);
15261
DIE_UNLESS(lmysql);
15263
if (!mysql_real_connect(lmysql, opt_host, opt_user, opt_password,
15264
current_db, opt_port, opt_unix_socket, 0))
15266
fprintf(stderr, "Failed to connect to the database\n");
15270
prep_stmt= mysql_stmt_init(lmysql);
15271
rc= mysql_stmt_prepare(prep_stmt, "SELECT 1", 8);
15272
DIE_UNLESS(rc == 0);
15274
mysql_close(lmysql);
15276
rc= mysql_stmt_execute(prep_stmt);
15278
rc= mysql_stmt_reset(prep_stmt);
15280
rc= mysql_stmt_close(prep_stmt);
15281
DIE_UNLESS(rc == 0);
15284
#endif /* EMBEDDED_LIBRARY */
15286
/* Bug #16143: mysql_stmt_sqlstate returns an empty string instead of '00000' */
15288
static void test_bug16143()
15291
myheader("test_bug16143");
15293
stmt= mysql_stmt_init(mysql);
15294
/* Check mysql_stmt_sqlstate return "no error" */
15295
DIE_UNLESS(strcmp(mysql_stmt_sqlstate(stmt), "00000") == 0);
15297
mysql_stmt_close(stmt);
15301
/* Bug #16144: mysql_stmt_attr_get type error */
15303
static void test_bug16144()
15305
const my_bool flag_orig= (my_bool) 0xde;
15306
my_bool flag= flag_orig;
15308
myheader("test_bug16144");
15310
/* Check that attr_get returns correct data on little and big endian CPUs */
15311
stmt= mysql_stmt_init(mysql);
15312
mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (const void*) &flag);
15313
mysql_stmt_attr_get(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &flag);
15314
DIE_UNLESS(flag == flag_orig);
15316
mysql_stmt_close(stmt);
15320
Bug #15613: "libmysqlclient API function mysql_stmt_prepare returns wrong
15324
static void test_bug15613()
15327
const char *stmt_text;
15328
MYSQL_RES *metadata;
15329
MYSQL_FIELD *field;
15331
myheader("test_bug15613");
15333
/* I. Prepare the table */
15334
rc= mysql_query(mysql, "set names latin1");
15336
mysql_query(mysql, "drop table if exists t1");
15337
rc= mysql_query(mysql,
15338
"create table t1 (t text character set utf8, "
15339
"tt tinytext character set utf8, "
15340
"mt mediumtext character set utf8, "
15341
"lt longtext character set utf8, "
15342
"vl varchar(255) character set latin1,"
15343
"vb varchar(255) character set binary,"
15344
"vu varchar(255) character set utf8)");
15347
stmt= mysql_stmt_init(mysql);
15349
/* II. Check SELECT metadata */
15350
stmt_text= ("select t, tt, mt, lt, vl, vb, vu from t1");
15351
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
15352
metadata= mysql_stmt_result_metadata(stmt);
15353
field= mysql_fetch_fields(metadata);
15356
printf("Field lengths (client character set is latin1):\n"
15357
"text character set utf8:\t\t%lu\n"
15358
"tinytext character set utf8:\t\t%lu\n"
15359
"mediumtext character set utf8:\t\t%lu\n"
15360
"longtext character set utf8:\t\t%lu\n"
15361
"varchar(255) character set latin1:\t%lu\n"
15362
"varchar(255) character set binary:\t%lu\n"
15363
"varchar(255) character set utf8:\t%lu\n",
15364
field[0].length, field[1].length, field[2].length, field[3].length,
15365
field[4].length, field[5].length, field[6].length);
15367
DIE_UNLESS(field[0].length == 65535);
15368
DIE_UNLESS(field[1].length == 255);
15369
DIE_UNLESS(field[2].length == 16777215);
15370
DIE_UNLESS(field[3].length == 4294967295UL);
15371
DIE_UNLESS(field[4].length == 255);
15372
DIE_UNLESS(field[5].length == 255);
15373
DIE_UNLESS(field[6].length == 255);
15374
mysql_free_result(metadata);
15375
mysql_stmt_free_result(stmt);
15378
rc= mysql_query(mysql, "drop table t1");
15380
rc= mysql_query(mysql, "set names default");
15382
mysql_stmt_close(stmt);
15386
Bug#17667: An attacker has the opportunity to bypass query logging.
15388
Note! Also tests Bug#21813, where prepared statements are used to
15391
static void test_bug17667()
15395
enum query_type { QT_NORMAL, QT_PREPARED};
15396
struct buffer_and_length {
15397
enum query_type qt;
15398
const char *buffer;
15401
{ QT_NORMAL, "drop table if exists bug17667", 29 },
15402
{ QT_NORMAL, "create table bug17667 (c varchar(20))", 37 },
15403
{ QT_NORMAL, "insert into bug17667 (c) values ('regular') /* NUL=\0 with comment */", 68 },
15405
"insert into bug17667 (c) values ('prepared') /* NUL=\0 with comment */", 69, },
15406
{ QT_NORMAL, "insert into bug17667 (c) values ('NUL=\0 in value')", 50 },
15407
{ QT_NORMAL, "insert into bug17667 (c) values ('5 NULs=\0\0\0\0\0')", 48 },
15408
{ QT_PREPARED, "insert into bug17667 (c) values ('6 NULs=\0\0\0\0\0\0')", 50 },
15409
{ QT_NORMAL, "/* NUL=\0 with comment */ insert into bug17667 (c) values ('encore')", 67 },
15410
{ QT_NORMAL, "drop table bug17667", 19 },
15411
{ QT_NORMAL, NULL, 0 } };
15413
struct buffer_and_length *statement_cursor;
15415
char *master_log_filename;
15417
myheader("test_bug17667");
15419
master_log_filename = (char *) malloc(strlen(opt_vardir) + strlen("/log/master.log") + 1);
15420
strxmov(master_log_filename, opt_vardir, "/log/master.log", NullS);
15422
printf("Opening '%s'\n", master_log_filename);
15423
log_file= my_fopen(master_log_filename, (int) (O_RDONLY | O_BINARY), MYF(0));
15424
free(master_log_filename);
15426
if (log_file == NULL)
15430
printf("Could not find the log file, VARDIR/log/master.log, so "
15431
"test_bug17667 is not run.\n"
15432
"Run test from the mysql-test/mysql-test-run* program to set up "
15433
"correct environment for this test.\n\n");
15438
enable_general_log(1);
15440
for (statement_cursor= statements; statement_cursor->buffer != NULL;
15441
statement_cursor++)
15443
if (statement_cursor->qt == QT_NORMAL)
15445
/* Run statement as normal query */
15446
rc= mysql_real_query(mysql, statement_cursor->buffer,
15447
statement_cursor->length);
15450
else if (statement_cursor->qt == QT_PREPARED)
15453
Run as prepared statement
15455
NOTE! All these queries should be in the log twice,
15456
one time for prepare and one time for execute
15458
stmt= mysql_stmt_init(mysql);
15460
rc= mysql_stmt_prepare(stmt, statement_cursor->buffer,
15461
statement_cursor->length);
15462
check_execute(stmt, rc);
15464
rc= mysql_stmt_execute(stmt);
15465
check_execute(stmt, rc);
15467
mysql_stmt_close(stmt);
15475
/* Make sure the server has written the logs to disk before reading it */
15476
rc= mysql_query(mysql, "flush logs");
15479
for (statement_cursor= statements; statement_cursor->buffer != NULL;
15480
statement_cursor++)
15482
int expected_hits= 1, hits= 0;
15483
char line_buffer[MAX_TEST_QUERY_LENGTH*2];
15484
/* more than enough room for the query and some marginalia. */
15486
/* Prepared statments always occurs twice in log */
15487
if (statement_cursor->qt == QT_PREPARED)
15490
/* Loop until we found expected number of log entries */
15492
/* Loop until statement is found in log */
15494
memset(line_buffer, '/', MAX_TEST_QUERY_LENGTH*2);
15496
if(fgets(line_buffer, MAX_TEST_QUERY_LENGTH*2, log_file) == NULL)
15498
/* If fgets returned NULL, it indicates either error or EOF */
15499
if (feof(log_file))
15500
DIE("Found EOF before all statements where found");
15502
fprintf(stderr, "Got error %d while reading from file\n",
15507
} while (my_memmem(line_buffer, MAX_TEST_QUERY_LENGTH*2,
15508
statement_cursor->buffer,
15509
statement_cursor->length) == NULL);
15511
} while (hits < expected_hits);
15514
printf("Found statement starting with \"%s\"\n",
15515
statement_cursor->buffer);
15518
restore_general_log();
15521
printf("success. All queries found intact in the log.\n");
15523
my_fclose(log_file, MYF(0));
15528
Bug#14169: type of group_concat() result changed to blob if tmp_table was
15531
static void test_bug14169()
15534
const char *stmt_text;
15536
MYSQL_FIELD *field;
15539
myheader("test_bug14169");
15541
rc= mysql_query(mysql, "drop table if exists t1");
15543
rc= mysql_query(mysql, "set session group_concat_max_len=1024");
15545
rc= mysql_query(mysql, "create table t1 (f1 int unsigned, f2 varchar(255))");
15547
rc= mysql_query(mysql, "insert into t1 values (1,repeat('a',255)),"
15548
"(2,repeat('b',255))");
15550
stmt= mysql_stmt_init(mysql);
15551
stmt_text= "select f2,group_concat(f1) from t1 group by f2";
15552
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
15554
res= mysql_stmt_result_metadata(stmt);
15555
field= mysql_fetch_fields(res);
15557
printf("GROUP_CONCAT() result type %i", field[1].type);
15558
DIE_UNLESS(field[1].type == MYSQL_TYPE_BLOB);
15559
mysql_free_result(res);
15560
mysql_stmt_free_result(stmt);
15561
mysql_stmt_close(stmt);
15563
rc= mysql_query(mysql, "drop table t1");
15568
Test that mysql_insert_id() behaves as documented in our manual
15570
static void test_mysql_insert_id()
15575
myheader("test_mysql_insert_id");
15577
rc= mysql_query(mysql, "drop table if exists t1");
15579
/* table without auto_increment column */
15580
rc= mysql_query(mysql, "create table t1 (f1 int, f2 varchar(255), key(f1))");
15582
rc= mysql_query(mysql, "insert into t1 values (1,'a')");
15584
res= mysql_insert_id(mysql);
15585
DIE_UNLESS(res == 0);
15586
rc= mysql_query(mysql, "insert into t1 values (null,'b')");
15588
res= mysql_insert_id(mysql);
15589
DIE_UNLESS(res == 0);
15590
rc= mysql_query(mysql, "insert into t1 select 5,'c'");
15592
res= mysql_insert_id(mysql);
15593
DIE_UNLESS(res == 0);
15596
Test for bug #34889: mysql_client_test::test_mysql_insert_id test fails
15599
rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
15601
rc= mysql_query(mysql, "insert into t2 values (null,'b')");
15603
rc= mysql_query(mysql, "insert into t1 select 5,'c'");
15605
res= mysql_insert_id(mysql);
15606
DIE_UNLESS(res == 0);
15607
rc= mysql_query(mysql, "drop table t2");
15610
rc= mysql_query(mysql, "insert into t1 select null,'d'");
15612
res= mysql_insert_id(mysql);
15613
DIE_UNLESS(res == 0);
15614
rc= mysql_query(mysql, "insert into t1 values (null,last_insert_id(300))");
15616
res= mysql_insert_id(mysql);
15617
DIE_UNLESS(res == 300);
15618
rc= mysql_query(mysql, "insert into t1 select null,last_insert_id(400)");
15620
res= mysql_insert_id(mysql);
15622
Behaviour change: old code used to return 0; but 400 is consistent
15623
with INSERT VALUES, and the manual's section of mysql_insert_id() does not
15624
say INSERT SELECT should be different.
15626
DIE_UNLESS(res == 400);
15628
/* table with auto_increment column */
15629
rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
15631
rc= mysql_query(mysql, "insert into t2 values (1,'a')");
15633
res= mysql_insert_id(mysql);
15634
DIE_UNLESS(res == 1);
15635
/* this should not influence next INSERT if it doesn't have auto_inc */
15636
rc= mysql_query(mysql, "insert into t1 values (10,'e')");
15638
res= mysql_insert_id(mysql);
15639
DIE_UNLESS(res == 0);
15641
rc= mysql_query(mysql, "insert into t2 values (null,'b')");
15643
res= mysql_insert_id(mysql);
15644
DIE_UNLESS(res == 2);
15645
rc= mysql_query(mysql, "insert into t2 select 5,'c'");
15647
res= mysql_insert_id(mysql);
15649
Manual says that for multirow insert this should have been 5, but does not
15650
say for INSERT SELECT. This is a behaviour change: old code used to return
15651
0. We try to be consistent with INSERT VALUES.
15653
DIE_UNLESS(res == 5);
15654
rc= mysql_query(mysql, "insert into t2 select null,'d'");
15656
res= mysql_insert_id(mysql);
15657
DIE_UNLESS(res == 6);
15658
/* with more than one row */
15659
rc= mysql_query(mysql, "insert into t2 values (10,'a'),(11,'b')");
15661
res= mysql_insert_id(mysql);
15662
DIE_UNLESS(res == 11);
15663
rc= mysql_query(mysql, "insert into t2 select 12,'a' union select 13,'b'");
15665
res= mysql_insert_id(mysql);
15667
Manual says that for multirow insert this should have been 13, but does
15668
not say for INSERT SELECT. This is a behaviour change: old code used to
15669
return 0. We try to be consistent with INSERT VALUES.
15671
DIE_UNLESS(res == 13);
15672
rc= mysql_query(mysql, "insert into t2 values (null,'a'),(null,'b')");
15674
res= mysql_insert_id(mysql);
15675
DIE_UNLESS(res == 14);
15676
rc= mysql_query(mysql, "insert into t2 select null,'a' union select null,'b'");
15678
res= mysql_insert_id(mysql);
15679
DIE_UNLESS(res == 16);
15680
rc= mysql_query(mysql, "insert into t2 select 12,'a' union select 13,'b'");
15682
rc= mysql_query(mysql, "insert ignore into t2 select 12,'a' union select 13,'b'");
15684
res= mysql_insert_id(mysql);
15685
DIE_UNLESS(res == 0);
15686
rc= mysql_query(mysql, "insert into t2 values (12,'a'),(13,'b')");
15688
res= mysql_insert_id(mysql);
15689
DIE_UNLESS(res == 0);
15690
rc= mysql_query(mysql, "insert ignore into t2 values (12,'a'),(13,'b')");
15692
res= mysql_insert_id(mysql);
15693
DIE_UNLESS(res == 0);
15694
/* mixing autogenerated and explicit values */
15695
rc= mysql_query(mysql, "insert into t2 values (null,'e'),(12,'a'),(13,'b')");
15697
rc= mysql_query(mysql, "insert into t2 values (null,'e'),(12,'a'),(13,'b'),(25,'g')");
15699
rc= mysql_query(mysql, "insert into t2 values (null,last_insert_id(300))");
15701
res= mysql_insert_id(mysql);
15703
according to the manual, this might be 20 or 300, but it looks like
15704
auto_increment column takes priority over last_insert_id().
15706
DIE_UNLESS(res == 20);
15707
/* If first autogenerated number fails and 2nd works: */
15708
rc= mysql_query(mysql, "drop table t2");
15710
rc= mysql_query(mysql, "create table t2 (f1 int not null primary key "
15711
"auto_increment, f2 varchar(255), unique (f2))");
15713
rc= mysql_query(mysql, "insert into t2 values (null,'e')");
15714
res= mysql_insert_id(mysql);
15715
DIE_UNLESS(res == 1);
15716
rc= mysql_query(mysql, "insert ignore into t2 values (null,'e'),(null,'a'),(null,'e')");
15718
res= mysql_insert_id(mysql);
15719
DIE_UNLESS(res == 2);
15720
/* If autogenerated fails and explicit works: */
15721
rc= mysql_query(mysql, "insert ignore into t2 values (null,'e'),(12,'c'),(null,'d')");
15723
res= mysql_insert_id(mysql);
15725
Behaviour change: old code returned 3 (first autogenerated, even if it
15726
fails); we now return first successful autogenerated.
15728
DIE_UNLESS(res == 13);
15729
/* UPDATE may update mysql_insert_id() if it uses LAST_INSERT_ID(#) */
15730
rc= mysql_query(mysql, "update t2 set f1=14 where f1=12");
15732
res= mysql_insert_id(mysql);
15733
DIE_UNLESS(res == 0);
15734
rc= mysql_query(mysql, "update t2 set f1=0 where f1=14");
15736
res= mysql_insert_id(mysql);
15737
DIE_UNLESS(res == 0);
15738
rc= mysql_query(mysql, "update t2 set f2=last_insert_id(372) where f1=0");
15740
res= mysql_insert_id(mysql);
15741
DIE_UNLESS(res == 372);
15742
/* check that LAST_INSERT_ID() does not update mysql_insert_id(): */
15743
rc= mysql_query(mysql, "insert into t2 values (null,'g')");
15745
res= mysql_insert_id(mysql);
15746
DIE_UNLESS(res == 15);
15747
rc= mysql_query(mysql, "update t2 set f2=(@li:=last_insert_id()) where f1=15");
15749
res= mysql_insert_id(mysql);
15750
DIE_UNLESS(res == 0);
15752
Behaviour change: now if ON DUPLICATE KEY UPDATE updates a row,
15753
mysql_insert_id() returns the id of the row, instead of not being
15756
rc= mysql_query(mysql, "insert into t2 values (null,@li) on duplicate key "
15757
"update f2=concat('we updated ',f2)");
15759
res= mysql_insert_id(mysql);
15760
DIE_UNLESS(res == 15);
15762
rc= mysql_query(mysql, "drop table t1,t2");
15767
Bug#20152: mysql_stmt_execute() writes to MYSQL_TYPE_DATE buffer
15770
static void test_bug20152()
15772
MYSQL_BIND my_bind[1];
15776
const char *query= "INSERT INTO t1 (f1) VALUES (?)";
15778
myheader("test_bug20152");
15780
memset(my_bind, 0, sizeof(my_bind));
15781
my_bind[0].buffer_type= MYSQL_TYPE_DATE;
15782
my_bind[0].buffer= (void*)&tm;
15791
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
15793
rc= mysql_query(mysql, "CREATE TABLE t1 (f1 DATE)");
15796
stmt= mysql_stmt_init(mysql);
15797
rc= mysql_stmt_prepare(stmt, query, strlen(query));
15798
check_execute(stmt, rc);
15799
rc= mysql_stmt_bind_param(stmt, my_bind);
15800
check_execute(stmt, rc);
15801
rc= mysql_stmt_execute(stmt);
15802
check_execute(stmt, rc);
15803
rc= mysql_stmt_close(stmt);
15804
check_execute(stmt, rc);
15805
rc= mysql_query(mysql, "DROP TABLE t1");
15808
if (tm.hour == 14 && tm.minute == 9 && tm.second == 42) {
15812
printf("[14:09:42] != [%02d:%02d:%02d]\n", tm.hour, tm.minute, tm.second);
15817
/* Bug#15752 "Lost connection to MySQL server when calling a SP from C API" */
15819
static void test_bug15752()
15823
const int ITERATION_COUNT= 100;
15824
const char *query= "CALL p1()";
15826
myheader("test_bug15752");
15828
rc= mysql_query(mysql, "drop procedure if exists p1");
15830
rc= mysql_query(mysql, "create procedure p1() select 1");
15833
mysql_init(&mysql_local);
15834
if (! mysql_real_connect(&mysql_local, opt_host, opt_user,
15835
opt_password, current_db, opt_port,
15837
CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS))
15839
printf("Unable connect to MySQL server: %s\n", mysql_error(&mysql_local));
15842
rc= mysql_real_query(&mysql_local, query, strlen(query));
15844
mysql_free_result(mysql_store_result(&mysql_local));
15846
rc= mysql_real_query(&mysql_local, query, strlen(query));
15847
DIE_UNLESS(rc && mysql_errno(&mysql_local) == CR_COMMANDS_OUT_OF_SYNC);
15850
printf("Got error (as expected): %s\n", mysql_error(&mysql_local));
15852
/* Check some other commands too */
15854
DIE_UNLESS(mysql_next_result(&mysql_local) == 0);
15855
mysql_free_result(mysql_store_result(&mysql_local));
15856
DIE_UNLESS(mysql_next_result(&mysql_local) == -1);
15858
/* The second problem is not reproducible: add the test case */
15859
for (i = 0; i < ITERATION_COUNT; i++)
15861
if (mysql_real_query(&mysql_local, query, strlen(query)))
15863
printf("\ni=%d %s failed: %s\n", i, query, mysql_error(&mysql_local));
15866
mysql_free_result(mysql_store_result(&mysql_local));
15867
DIE_UNLESS(mysql_next_result(&mysql_local) == 0);
15868
mysql_free_result(mysql_store_result(&mysql_local));
15869
DIE_UNLESS(mysql_next_result(&mysql_local) == -1);
15872
mysql_close(&mysql_local);
15873
rc= mysql_query(mysql, "drop procedure p1");
15878
Bug#21206: memory corruption when too many cursors are opened at once
15880
Memory corruption happens when more than 1024 cursors are open
15883
static void test_bug21206()
15885
const size_t cursor_count= 1025;
15887
const char *create_table[]=
15889
"DROP TABLE IF EXISTS t1",
15890
"CREATE TABLE t1 (i INT)",
15891
"INSERT INTO t1 VALUES (1), (2), (3)"
15893
const char *query= "SELECT * FROM t1";
15895
Stmt_fetch *fetch_array=
15896
(Stmt_fetch*) calloc(cursor_count, sizeof(Stmt_fetch));
15900
DBUG_ENTER("test_bug21206");
15901
myheader("test_bug21206");
15903
fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
15905
for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
15907
/* Init will exit(1) in case of error */
15908
stmt_fetch_init(fetch, fetch - fetch_array, query);
15911
for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
15912
stmt_fetch_close(fetch);
15920
Ensure we execute the status code while testing
15923
static void test_status()
15925
const char *status;
15926
DBUG_ENTER("test_status");
15927
myheader("test_status");
15929
if (!(status= mysql_stat(mysql)))
15931
myerror("mysql_stat failed"); /* purecov: inspected */
15932
die(__FILE__, __LINE__, "mysql_stat failed"); /* purecov: inspected */
15938
Bug#21726: Incorrect result with multiple invocations of
15941
Test that client gets updated value of insert_id on UPDATE that uses
15942
LAST_INSERT_ID(expr).
15943
select_query added to test for bug
15944
#26921 Problem in mysql_insert_id() Embedded C API function
15946
static void test_bug21726()
15948
const char *create_table[]=
15950
"DROP TABLE IF EXISTS t1",
15951
"CREATE TABLE t1 (i INT)",
15952
"INSERT INTO t1 VALUES (1)",
15954
const char *update_query= "UPDATE t1 SET i= LAST_INSERT_ID(i + 1)";
15956
my_ulonglong insert_id;
15957
const char *select_query= "SELECT * FROM t1";
15960
DBUG_ENTER("test_bug21726");
15961
myheader("test_bug21726");
15963
fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
15965
rc= mysql_query(mysql, update_query);
15967
insert_id= mysql_insert_id(mysql);
15968
DIE_UNLESS(insert_id == 2);
15970
rc= mysql_query(mysql, update_query);
15972
insert_id= mysql_insert_id(mysql);
15973
DIE_UNLESS(insert_id == 3);
15975
rc= mysql_query(mysql, select_query);
15977
insert_id= mysql_insert_id(mysql);
15978
DIE_UNLESS(insert_id == 3);
15979
result= mysql_store_result(mysql);
15980
mysql_free_result(result);
15987
BUG#23383: mysql_affected_rows() returns different values than
15988
mysql_stmt_affected_rows()
15990
Test that both mysql_affected_rows() and mysql_stmt_affected_rows()
15991
return -1 on error, 0 when no rows were affected, and (positive) row
15992
count when some rows were affected.
15994
static void test_bug23383()
15996
const char *insert_query= "INSERT INTO t1 VALUES (1), (2)";
15997
const char *update_query= "UPDATE t1 SET i= 4 WHERE i = 3";
15999
my_ulonglong row_count;
16002
DBUG_ENTER("test_bug23383");
16003
myheader("test_bug23383");
16005
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
16008
rc= mysql_query(mysql, "CREATE TABLE t1 (i INT UNIQUE)");
16011
rc= mysql_query(mysql, insert_query);
16013
row_count= mysql_affected_rows(mysql);
16014
DIE_UNLESS(row_count == 2);
16016
rc= mysql_query(mysql, insert_query);
16017
DIE_UNLESS(rc != 0);
16018
row_count= mysql_affected_rows(mysql);
16019
DIE_UNLESS(row_count == (my_ulonglong)-1);
16021
rc= mysql_query(mysql, update_query);
16023
row_count= mysql_affected_rows(mysql);
16024
DIE_UNLESS(row_count == 0);
16026
rc= mysql_query(mysql, "DELETE FROM t1");
16029
stmt= mysql_stmt_init(mysql);
16030
DIE_UNLESS(stmt != 0);
16032
rc= mysql_stmt_prepare(stmt, insert_query, strlen(insert_query));
16033
check_execute(stmt, rc);
16035
rc= mysql_stmt_execute(stmt);
16036
check_execute(stmt, rc);
16037
row_count= mysql_stmt_affected_rows(stmt);
16038
DIE_UNLESS(row_count == 2);
16040
rc= mysql_stmt_execute(stmt);
16041
DIE_UNLESS(rc != 0);
16042
row_count= mysql_stmt_affected_rows(stmt);
16043
DIE_UNLESS(row_count == (my_ulonglong)-1);
16045
rc= mysql_stmt_prepare(stmt, update_query, strlen(update_query));
16046
check_execute(stmt, rc);
16048
rc= mysql_stmt_execute(stmt);
16049
check_execute(stmt, rc);
16050
row_count= mysql_stmt_affected_rows(stmt);
16051
DIE_UNLESS(row_count == 0);
16053
rc= mysql_stmt_close(stmt);
16054
check_execute(stmt, rc);
16056
rc= mysql_query(mysql, "DROP TABLE t1");
16064
BUG#21635: MYSQL_FIELD struct's member strings seem to misbehave for
16067
Check that for MIN(), MAX(), COUNT() only MYSQL_FIELD::name is set
16068
to either expression or its alias, and db, org_table, table,
16069
org_name fields are empty strings.
16071
static void test_bug21635()
16073
const char *expr[]=
16075
"MIN(i)", "MIN(i)",
16076
"MIN(i) AS A1", "A1",
16077
"MAX(i)", "MAX(i)",
16078
"MAX(i) AS A2", "A2",
16079
"COUNT(i)", "COUNT(i)",
16080
"COUNT(i) AS A3", "A3",
16082
char query[MAX_TEST_QUERY_LENGTH];
16085
MYSQL_FIELD *field;
16086
unsigned int field_count, i, j;
16089
DBUG_ENTER("test_bug21635");
16090
myheader("test_bug21635");
16092
query_end= strxmov(query, "SELECT ", NullS);
16093
for (i= 0; i < sizeof(expr) / sizeof(*expr) / 2; ++i)
16094
query_end= strxmov(query_end, expr[i * 2], ", ", NullS);
16095
query_end= strxmov(query_end - 2, " FROM t1 GROUP BY i", NullS);
16096
DIE_UNLESS(query_end - query < MAX_TEST_QUERY_LENGTH);
16098
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
16100
rc= mysql_query(mysql, "CREATE TABLE t1 (i INT)");
16103
We need this loop to ensure correct behavior with both constant and
16104
non-constant tables.
16106
for (j= 0; j < 2 ; j++)
16108
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
16111
rc= mysql_real_query(mysql, query, query_end - query);
16114
result= mysql_use_result(mysql);
16115
DIE_UNLESS(result);
16117
field_count= mysql_field_count(mysql);
16118
for (i= 0; i < field_count; ++i)
16120
field= mysql_fetch_field_direct(result, i);
16123
printf("%s -> %s ... ", expr[i * 2], field->name);
16125
DIE_UNLESS(field->db[0] == 0 && field->org_table[0] == 0 &&
16126
field->table[0] == 0 && field->org_name[0] == 0);
16127
DIE_UNLESS(strcmp(field->name, expr[i * 2 + 1]) == 0);
16133
mysql_free_result(result);
16135
rc= mysql_query(mysql, "DROP TABLE t1");
16142
Bug#24179 "select b into $var" fails with --cursor_protocol"
16143
The failure is correct, check that the returned message is meaningful.
16146
static void test_bug24179()
16151
DBUG_ENTER("test_bug24179");
16152
myheader("test_bug24179");
16154
stmt= open_cursor("select 1 into @a");
16155
rc= mysql_stmt_execute(stmt);
16159
printf("Got error (as expected): %d %s\n",
16160
mysql_stmt_errno(stmt),
16161
mysql_stmt_error(stmt));
16163
DIE_UNLESS(mysql_stmt_errno(stmt) == 1323);
16164
mysql_stmt_close(stmt);
16171
Bug#32265 Server returns different metadata if prepared statement is used
16174
static void test_bug32265()
16178
MYSQL_FIELD *field;
16179
MYSQL_RES *metadata;
16181
DBUG_ENTER("test_bug32265");
16182
myheader("test_bug32265");
16184
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
16186
rc= mysql_query(mysql, "CREATE TABLE t1 (a INTEGER)");
16188
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
16190
rc= mysql_query(mysql, "CREATE VIEW v1 AS SELECT * FROM t1");
16193
stmt= open_cursor("SELECT * FROM t1");
16194
rc= mysql_stmt_execute(stmt);
16195
check_execute(stmt, rc);
16197
metadata= mysql_stmt_result_metadata(stmt);
16198
field= mysql_fetch_field(metadata);
16200
DIE_UNLESS(strcmp(field->table, "t1") == 0);
16201
DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
16202
DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
16203
mysql_free_result(metadata);
16204
mysql_stmt_close(stmt);
16206
stmt= open_cursor("SELECT a '' FROM t1 ``");
16207
rc= mysql_stmt_execute(stmt);
16208
check_execute(stmt, rc);
16210
metadata= mysql_stmt_result_metadata(stmt);
16211
field= mysql_fetch_field(metadata);
16212
DIE_UNLESS(strcmp(field->table, "") == 0);
16213
DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
16214
DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
16215
mysql_free_result(metadata);
16216
mysql_stmt_close(stmt);
16218
stmt= open_cursor("SELECT a '' FROM t1 ``");
16219
rc= mysql_stmt_execute(stmt);
16220
check_execute(stmt, rc);
16222
metadata= mysql_stmt_result_metadata(stmt);
16223
field= mysql_fetch_field(metadata);
16224
DIE_UNLESS(strcmp(field->table, "") == 0);
16225
DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
16226
DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
16227
mysql_free_result(metadata);
16228
mysql_stmt_close(stmt);
16230
stmt= open_cursor("SELECT * FROM v1");
16231
rc= mysql_stmt_execute(stmt);
16232
check_execute(stmt, rc);
16234
metadata= mysql_stmt_result_metadata(stmt);
16235
field= mysql_fetch_field(metadata);
16236
DIE_UNLESS(strcmp(field->table, "v1") == 0);
16237
DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
16238
DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
16239
mysql_free_result(metadata);
16240
mysql_stmt_close(stmt);
16242
stmt= open_cursor("SELECT * FROM v1 /* SIC */ GROUP BY 1");
16243
rc= mysql_stmt_execute(stmt);
16244
check_execute(stmt, rc);
16246
metadata= mysql_stmt_result_metadata(stmt);
16247
field= mysql_fetch_field(metadata);
16248
DIE_UNLESS(strcmp(field->table, "v1") == 0);
16249
DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
16250
DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
16251
mysql_free_result(metadata);
16252
mysql_stmt_close(stmt);
16254
rc= mysql_query(mysql, "DROP VIEW v1");
16256
rc= mysql_query(mysql, "DROP TABLE t1");
16263
Bug#28075 "COM_DEBUG crashes mysqld"
16266
static void test_bug28075()
16270
DBUG_ENTER("test_bug28075");
16271
myheader("test_bug28075");
16273
rc= mysql_dump_debug_info(mysql);
16274
DIE_UNLESS(rc == 0);
16276
rc= mysql_ping(mysql);
16277
DIE_UNLESS(rc == 0);
16284
Bug#27876 (SF with cyrillic variable name fails during execution (regression))
16287
static void test_bug27876()
16292
uchar utf8_func[] =
16294
0xd1, 0x84, 0xd1, 0x83, 0xd0, 0xbd, 0xd0, 0xba,
16295
0xd1, 0x86, 0xd0, 0xb8, 0xd0, 0xb9, 0xd0, 0xba,
16300
uchar utf8_param[] =
16302
0xd0, 0xbf, 0xd0, 0xb0, 0xd1, 0x80, 0xd0, 0xb0,
16303
0xd0, 0xbc, 0xd0, 0xb5, 0xd1, 0x82, 0xd1, 0x8a,
16304
0xd1, 0x80, 0x5f, 0xd0, 0xb2, 0xd0, 0xb5, 0xd1,
16305
0x80, 0xd1, 0x81, 0xd0, 0xb8, 0xd1, 0x8f,
16311
DBUG_ENTER("test_bug27876");
16312
myheader("test_bug27876");
16314
rc= mysql_query(mysql, "set names utf8");
16317
rc= mysql_query(mysql, "select version()");
16319
result= mysql_store_result(mysql);
16321
mysql_free_result(result);
16323
sprintf(query, "DROP FUNCTION IF EXISTS %s", (char*) utf8_func);
16324
rc= mysql_query(mysql, query);
16328
"CREATE FUNCTION %s( %s VARCHAR(25))"
16329
" RETURNS VARCHAR(25) DETERMINISTIC RETURN %s",
16330
(char*) utf8_func, (char*) utf8_param, (char*) utf8_param);
16331
rc= mysql_query(mysql, query);
16333
sprintf(query, "SELECT %s(VERSION())", (char*) utf8_func);
16334
rc= mysql_query(mysql, query);
16336
result= mysql_store_result(mysql);
16338
mysql_free_result(result);
16340
sprintf(query, "DROP FUNCTION %s", (char*) utf8_func);
16341
rc= mysql_query(mysql, query);
16344
rc= mysql_query(mysql, "set names default");
16350
Bug#28505: mysql_affected_rows() returns wrong value if CLIENT_FOUND_ROWS
16354
static void test_bug28505()
16358
myquery(mysql_query(mysql, "drop table if exists t1"));
16359
myquery(mysql_query(mysql, "create table t1(f1 int primary key)"));
16360
myquery(mysql_query(mysql, "insert into t1 values(1)"));
16361
myquery(mysql_query(mysql,
16362
"insert into t1 values(1) on duplicate key update f1=1"));
16363
res= mysql_affected_rows(mysql);
16365
myquery(mysql_query(mysql, "drop table t1"));
16370
Bug#28934: server crash when receiving malformed com_execute packets
16373
static void test_bug28934()
16376
MYSQL_BIND bind[5];
16380
myquery(mysql_query(mysql, "drop table if exists t1"));
16381
myquery(mysql_query(mysql, "create table t1(id int)"));
16383
myquery(mysql_query(mysql, "insert into t1 values(1),(2),(3),(4),(5)"));
16384
stmt= mysql_simple_prepare(mysql,"select * from t1 where id in(?,?,?,?,?)");
16387
memset (&bind, 0, sizeof (bind));
16388
for (cnt= 0; cnt < 5; cnt++)
16390
bind[cnt].buffer_type= MYSQL_TYPE_LONG;
16391
bind[cnt].buffer= (char*)&cnt;
16392
bind[cnt].buffer_length= 0;
16394
myquery(mysql_stmt_bind_param(stmt, bind));
16396
stmt->param_count=2;
16397
error= mysql_stmt_execute(stmt);
16398
DIE_UNLESS(error != 0);
16400
mysql_stmt_close(stmt);
16402
myquery(mysql_query(mysql, "drop table t1"));
16406
Test mysql_change_user() C API and COM_CHANGE_USER
16409
static void test_change_user()
16412
const char *user_pw= "mysqltest_pw";
16413
const char *user_no_pw= "mysqltest_no_pw";
16414
const char *pw= "password";
16415
const char *db= "mysqltest_user_test_database";
16418
DBUG_ENTER("test_change_user");
16419
myheader("test_change_user");
16421
/* Prepare environment */
16422
sprintf(buff, "drop database if exists %s", db);
16423
rc= mysql_query(mysql, buff);
16426
sprintf(buff, "create database %s", db);
16427
rc= mysql_query(mysql, buff);
16431
"grant select on %s.* to %s@'%%' identified by '%s'",
16435
rc= mysql_query(mysql, buff);
16439
"grant select on %s.* to %s@'%%'",
16442
rc= mysql_query(mysql, buff);
16446
/* Try some combinations */
16447
rc= mysql_change_user(mysql, NULL, NULL, NULL);
16450
printf("Got error (as expected): %s\n", mysql_error(mysql));
16453
rc= mysql_change_user(mysql, "", NULL, NULL);
16456
printf("Got error (as expected): %s\n", mysql_error(mysql));
16458
rc= mysql_change_user(mysql, "", "", NULL);
16461
printf("Got error (as expected): %s\n", mysql_error(mysql));
16463
rc= mysql_change_user(mysql, "", "", "");
16466
printf("Got error (as expected): %s\n", mysql_error(mysql));
16468
rc= mysql_change_user(mysql, NULL, "", "");
16471
printf("Got error (as expected): %s\n", mysql_error(mysql));
16474
rc= mysql_change_user(mysql, NULL, NULL, "");
16477
printf("Got error (as expected): %s\n", mysql_error(mysql));
16479
rc= mysql_change_user(mysql, "", NULL, "");
16482
printf("Got error (as expected): %s\n", mysql_error(mysql));
16484
rc= mysql_change_user(mysql, user_pw, NULL, "");
16487
printf("Got error (as expected): %s\n", mysql_error(mysql));
16489
rc= mysql_change_user(mysql, user_pw, "", "");
16492
printf("Got error (as expected): %s\n", mysql_error(mysql));
16494
rc= mysql_change_user(mysql, user_pw, "", NULL);
16497
printf("Got error (as expected): %s\n", mysql_error(mysql));
16499
rc= mysql_change_user(mysql, user_pw, NULL, NULL);
16502
printf("Got error (as expected): %s\n", mysql_error(mysql));
16504
rc= mysql_change_user(mysql, user_pw, "", db);
16507
printf("Got error (as expected): %s\n", mysql_error(mysql));
16509
rc= mysql_change_user(mysql, user_pw, NULL, db);
16512
printf("Got error (as expected): %s\n", mysql_error(mysql));
16514
rc= mysql_change_user(mysql, user_pw, pw, db);
16517
rc= mysql_change_user(mysql, user_pw, pw, NULL);
16520
rc= mysql_change_user(mysql, user_pw, pw, "");
16523
rc= mysql_change_user(mysql, user_no_pw, pw, db);
16526
printf("Got error (as expected): %s\n", mysql_error(mysql));
16528
rc= mysql_change_user(mysql, user_no_pw, pw, "");
16531
printf("Got error (as expected): %s\n", mysql_error(mysql));
16533
rc= mysql_change_user(mysql, user_no_pw, pw, NULL);
16536
printf("Got error (as expected): %s\n", mysql_error(mysql));
16538
rc= mysql_change_user(mysql, user_no_pw, "", NULL);
16541
rc= mysql_change_user(mysql, user_no_pw, "", "");
16544
rc= mysql_change_user(mysql, user_no_pw, "", db);
16547
rc= mysql_change_user(mysql, user_no_pw, NULL, db);
16550
rc= mysql_change_user(mysql, "", pw, db);
16553
printf("Got error (as expected): %s\n", mysql_error(mysql));
16555
rc= mysql_change_user(mysql, "", pw, "");
16558
printf("Got error (as expected): %s\n", mysql_error(mysql));
16560
rc= mysql_change_user(mysql, "", pw, NULL);
16563
printf("Got error (as expected): %s\n", mysql_error(mysql));
16565
rc= mysql_change_user(mysql, NULL, pw, NULL);
16568
printf("Got error (as expected): %s\n", mysql_error(mysql));
16570
rc= mysql_change_user(mysql, NULL, NULL, db);
16573
printf("Got error (as expected): %s\n", mysql_error(mysql));
16575
rc= mysql_change_user(mysql, NULL, "", db);
16578
printf("Got error (as expected): %s\n", mysql_error(mysql));
16580
rc= mysql_change_user(mysql, "", "", db);
16583
printf("Got error (as expected): %s\n", mysql_error(mysql));
16585
/* Cleanup the environment */
16587
mysql_change_user(mysql, opt_user, opt_password, current_db);
16589
sprintf(buff, "drop database %s", db);
16590
rc= mysql_query(mysql, buff);
16593
sprintf(buff, "drop user %s@'%%'", user_pw);
16594
rc= mysql_query(mysql, buff);
16597
sprintf(buff, "drop user %s@'%%'", user_no_pw);
16598
rc= mysql_query(mysql, buff);
16605
Bug#27592 (stack overrun when storing datetime value using prepared statements)
16608
static void test_bug27592()
16610
const int NUM_ITERATIONS= 40;
16613
MYSQL_STMT *stmt= NULL;
16614
MYSQL_BIND bind[1];
16615
MYSQL_TIME time_val;
16617
DBUG_ENTER("test_bug27592");
16618
myheader("test_bug27592");
16620
mysql_query(mysql, "DROP TABLE IF EXISTS t1");
16621
mysql_query(mysql, "CREATE TABLE t1(c2 DATETIME)");
16623
stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 VALUES (?)");
16626
memset(bind, 0, sizeof(bind));
16628
bind[0].buffer_type= MYSQL_TYPE_DATETIME;
16629
bind[0].buffer= (char *) &time_val;
16630
bind[0].length= NULL;
16632
for (i= 0; i < NUM_ITERATIONS; i++)
16634
time_val.year= 2007;
16638
time_val.minute= 41;
16639
time_val.second= 3;
16641
time_val.second_part=0;
16644
rc= mysql_stmt_bind_param(stmt, bind);
16645
check_execute(stmt, rc);
16647
rc= mysql_stmt_execute(stmt);
16648
check_execute(stmt, rc);
16651
mysql_stmt_close(stmt);
16657
Bug#29687 mysql_stmt_store_result memory leak in libmysqld
16660
static void test_bug29687()
16662
const int NUM_ITERATIONS= 40;
16665
MYSQL_STMT *stmt= NULL;
16667
DBUG_ENTER("test_bug29687");
16668
myheader("test_bug29687");
16670
stmt= mysql_simple_prepare(mysql, "SELECT 1 FROM dual WHERE 0=2");
16673
for (i= 0; i < NUM_ITERATIONS; i++)
16675
rc= mysql_stmt_execute(stmt);
16676
check_execute(stmt, rc);
16677
mysql_stmt_store_result(stmt);
16678
while (mysql_stmt_fetch(stmt)==0);
16679
mysql_stmt_free_result(stmt);
16682
mysql_stmt_close(stmt);
16688
Bug #29692 Single row inserts can incorrectly report a huge number of
16692
static void test_bug29692()
16696
if (!(conn= mysql_init(NULL)))
16698
myerror("test_bug29692 init failed");
16702
if (!(mysql_real_connect(conn, opt_host, opt_user,
16703
opt_password, opt_db ? opt_db:"test", opt_port,
16704
opt_unix_socket, CLIENT_FOUND_ROWS)))
16706
myerror("test_bug29692 connection failed");
16707
mysql_close(mysql);
16710
myquery(mysql_query(conn, "drop table if exists t1"));
16711
myquery(mysql_query(conn, "create table t1(f1 int)"));
16712
myquery(mysql_query(conn, "insert into t1 values(1)"));
16713
DIE_UNLESS(1 == mysql_affected_rows(conn));
16714
myquery(mysql_query(conn, "drop table t1"));
16719
Bug#29306 Truncated data in MS Access with decimal (3,1) columns in a VIEW
16722
static void test_bug29306()
16724
MYSQL_FIELD *field;
16728
DBUG_ENTER("test_bug29306");
16729
myheader("test_bug29306");
16731
rc= mysql_query(mysql, "DROP TABLE IF EXISTS tab17557");
16733
rc= mysql_query(mysql, "DROP VIEW IF EXISTS view17557");
16735
rc= mysql_query(mysql, "CREATE TABLE tab17557 (dd decimal (3,1))");
16737
rc= mysql_query(mysql, "CREATE VIEW view17557 as SELECT dd FROM tab17557");
16739
rc= mysql_query(mysql, "INSERT INTO tab17557 VALUES (7.6)");
16742
/* Checking the view */
16743
res= mysql_list_fields(mysql, "view17557", NULL);
16744
while ((field= mysql_fetch_field(res)))
16748
printf("field name %s\n", field->name);
16749
printf("field table %s\n", field->table);
16750
printf("field decimals %d\n", field->decimals);
16751
if (field->decimals < 1)
16752
printf("Error! No decimals! \n");
16755
DIE_UNLESS(field->decimals == 1);
16757
mysql_free_result(res);
16759
rc= mysql_query(mysql, "DROP TABLE tab17557");
16761
rc= mysql_query(mysql, "DROP VIEW view17557");
16767
Bug#30472: libmysql doesn't reset charset, insert_id after succ.
16768
mysql_change_user() call row insertions.
16771
static void bug30472_retrieve_charset_info(MYSQL *con,
16772
char *character_set_name,
16773
char *character_set_client,
16774
char *character_set_results,
16775
char *collation_connection)
16780
/* Get the cached client character set name. */
16782
strcpy(character_set_name, mysql_character_set_name(con));
16784
/* Retrieve server character set information. */
16786
DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'character_set_client'"));
16787
DIE_UNLESS(rs= mysql_store_result(con));
16788
DIE_UNLESS(row= mysql_fetch_row(rs));
16789
strcpy(character_set_client, row[1]);
16790
mysql_free_result(rs);
16792
DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'character_set_results'"));
16793
DIE_UNLESS(rs= mysql_store_result(con));
16794
DIE_UNLESS(row= mysql_fetch_row(rs));
16795
strcpy(character_set_results, row[1]);
16796
mysql_free_result(rs);
16798
DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'collation_connection'"));
16799
DIE_UNLESS(rs= mysql_store_result(con));
16800
DIE_UNLESS(row= mysql_fetch_row(rs));
16801
strcpy(collation_connection, row[1]);
16802
mysql_free_result(rs);
16805
static void test_bug30472()
16809
char character_set_name_1[MY_CS_NAME_SIZE];
16810
char character_set_client_1[MY_CS_NAME_SIZE];
16811
char character_set_results_1[MY_CS_NAME_SIZE];
16812
char collation_connnection_1[MY_CS_NAME_SIZE];
16814
char character_set_name_2[MY_CS_NAME_SIZE];
16815
char character_set_client_2[MY_CS_NAME_SIZE];
16816
char character_set_results_2[MY_CS_NAME_SIZE];
16817
char collation_connnection_2[MY_CS_NAME_SIZE];
16819
char character_set_name_3[MY_CS_NAME_SIZE];
16820
char character_set_client_3[MY_CS_NAME_SIZE];
16821
char character_set_results_3[MY_CS_NAME_SIZE];
16822
char collation_connnection_3[MY_CS_NAME_SIZE];
16824
char character_set_name_4[MY_CS_NAME_SIZE];
16825
char character_set_client_4[MY_CS_NAME_SIZE];
16826
char character_set_results_4[MY_CS_NAME_SIZE];
16827
char collation_connnection_4[MY_CS_NAME_SIZE];
16829
/* Create a new connection. */
16831
DIE_UNLESS(mysql_init(&con));
16833
DIE_UNLESS(mysql_real_connect(&con,
16837
opt_db ? opt_db : "test",
16840
CLIENT_FOUND_ROWS));
16842
/* Retrieve character set information. */
16844
bug30472_retrieve_charset_info(&con,
16845
character_set_name_1,
16846
character_set_client_1,
16847
character_set_results_1,
16848
collation_connnection_1);
16850
/* Switch client character set. */
16852
DIE_IF(mysql_set_character_set(&con, "utf8"));
16854
/* Retrieve character set information. */
16856
bug30472_retrieve_charset_info(&con,
16857
character_set_name_2,
16858
character_set_client_2,
16859
character_set_results_2,
16860
collation_connnection_2);
16864
1) character set has been switched and
16865
2) new character set is different from the original one.
16868
DIE_UNLESS(strcmp(character_set_name_2, "utf8") == 0);
16869
DIE_UNLESS(strcmp(character_set_client_2, "utf8") == 0);
16870
DIE_UNLESS(strcmp(character_set_results_2, "utf8") == 0);
16871
DIE_UNLESS(strcmp(collation_connnection_2, "utf8_general_ci") == 0);
16873
DIE_UNLESS(strcmp(character_set_name_1, character_set_name_2) != 0);
16874
DIE_UNLESS(strcmp(character_set_client_1, character_set_client_2) != 0);
16875
DIE_UNLESS(strcmp(character_set_results_1, character_set_results_2) != 0);
16876
DIE_UNLESS(strcmp(collation_connnection_1, collation_connnection_2) != 0);
16878
/* Call mysql_change_user() with the same username, password, database. */
16880
DIE_IF(mysql_change_user(&con,
16883
opt_db ? opt_db : "test"));
16885
/* Retrieve character set information. */
16887
bug30472_retrieve_charset_info(&con,
16888
character_set_name_3,
16889
character_set_client_3,
16890
character_set_results_3,
16891
collation_connnection_3);
16893
/* Check that character set information has been reset. */
16895
DIE_UNLESS(strcmp(character_set_name_1, character_set_name_3) == 0);
16896
DIE_UNLESS(strcmp(character_set_client_1, character_set_client_3) == 0);
16897
DIE_UNLESS(strcmp(character_set_results_1, character_set_results_3) == 0);
16898
DIE_UNLESS(strcmp(collation_connnection_1, collation_connnection_3) == 0);
16900
/* Change connection-default character set in the client. */
16902
mysql_options(&con, MYSQL_SET_CHARSET_NAME, "utf8");
16905
Call mysql_change_user() in order to check that new connection will
16906
have UTF8 character set on the client and on the server.
16909
DIE_IF(mysql_change_user(&con,
16912
opt_db ? opt_db : "test"));
16914
/* Retrieve character set information. */
16916
bug30472_retrieve_charset_info(&con,
16917
character_set_name_4,
16918
character_set_client_4,
16919
character_set_results_4,
16920
collation_connnection_4);
16922
/* Check that we have UTF8 on the server and on the client. */
16924
DIE_UNLESS(strcmp(character_set_name_4, "utf8") == 0);
16925
DIE_UNLESS(strcmp(character_set_client_4, "utf8") == 0);
16926
DIE_UNLESS(strcmp(character_set_results_4, "utf8") == 0);
16927
DIE_UNLESS(strcmp(collation_connnection_4, "utf8_general_ci") == 0);
16929
/* That's it. Cleanup. */
16934
static void bug20023_change_user(MYSQL *con)
16936
DIE_IF(mysql_change_user(con,
16939
opt_db ? opt_db : "test"));
16942
static my_bool query_int_variable(MYSQL *con,
16943
const char *var_name,
16949
char query_buffer[MAX_TEST_QUERY_LENGTH];
16953
my_snprintf(query_buffer,
16954
sizeof (query_buffer),
16956
(const char *) var_name);
16958
DIE_IF(mysql_query(con, query_buffer));
16959
DIE_UNLESS(rs= mysql_store_result(con));
16960
DIE_UNLESS(row= mysql_fetch_row(rs));
16962
is_null= row[0] == NULL;
16965
*var_value= atoi(row[0]);
16967
mysql_free_result(rs);
16972
static void test_bug20023()
16976
int sql_big_selects_orig;
16977
int max_join_size_orig;
16979
int sql_big_selects_2;
16980
int sql_big_selects_3;
16981
int sql_big_selects_4;
16982
int sql_big_selects_5;
16985
char query_buffer[MAX_TEST_QUERY_LENGTH];
16988
/* Create a new connection. */
16990
DIE_UNLESS(mysql_init(&con));
16992
DIE_UNLESS(mysql_real_connect(&con,
16996
opt_db ? opt_db : "test",
16999
CLIENT_FOUND_ROWS));
17001
/***********************************************************************
17002
Remember original SQL_BIG_SELECTS, MAX_JOIN_SIZE values.
17003
***********************************************************************/
17005
query_int_variable(&con,
17006
"@@session.sql_big_selects",
17007
&sql_big_selects_orig);
17009
query_int_variable(&con,
17010
"@@global.max_join_size",
17011
&max_join_size_orig);
17013
/***********************************************************************
17014
Test that COM_CHANGE_USER resets the SQL_BIG_SELECTS to the initial value.
17015
***********************************************************************/
17017
/* Issue COM_CHANGE_USER. */
17019
bug20023_change_user(&con);
17021
/* Query SQL_BIG_SELECTS. */
17023
query_int_variable(&con,
17024
"@@session.sql_big_selects",
17025
&sql_big_selects_2);
17027
/* Check that SQL_BIG_SELECTS is reset properly. */
17029
DIE_UNLESS(sql_big_selects_orig == sql_big_selects_2);
17031
/***********************************************************************
17032
Test that if MAX_JOIN_SIZE set to non-default value,
17033
SQL_BIG_SELECTS will be 0.
17034
***********************************************************************/
17036
/* Set MAX_JOIN_SIZE to some non-default value. */
17038
DIE_IF(mysql_query(&con, "SET @@global.max_join_size = 10000"));
17039
DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
17041
/* Issue COM_CHANGE_USER. */
17043
bug20023_change_user(&con);
17045
/* Query SQL_BIG_SELECTS. */
17047
query_int_variable(&con,
17048
"@@session.sql_big_selects",
17049
&sql_big_selects_3);
17051
/* Check that SQL_BIG_SELECTS is 0. */
17053
DIE_UNLESS(sql_big_selects_3 == 0);
17055
/***********************************************************************
17056
Test that if MAX_JOIN_SIZE set to default value,
17057
SQL_BIG_SELECTS will be 1.
17058
***********************************************************************/
17060
/* Set MAX_JOIN_SIZE to the default value (-1). */
17062
DIE_IF(mysql_query(&con, "SET @@global.max_join_size = -1"));
17063
DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
17065
/* Issue COM_CHANGE_USER. */
17067
bug20023_change_user(&con);
17069
/* Query SQL_BIG_SELECTS. */
17071
query_int_variable(&con,
17072
"@@session.sql_big_selects",
17073
&sql_big_selects_4);
17075
/* Check that SQL_BIG_SELECTS is 1. */
17077
DIE_UNLESS(sql_big_selects_4 == 1);
17079
/***********************************************************************
17080
Restore MAX_JOIN_SIZE.
17081
Check that SQL_BIG_SELECTS will be the original one.
17082
***********************************************************************/
17086
max_join_size is a ulong or better.
17087
my_snprintf() only goes up to ul.
17090
/* Restore MAX_JOIN_SIZE. */
17092
my_snprintf(query_buffer,
17093
sizeof (query_buffer),
17094
"SET @@global.max_join_size = %d",
17095
(int) max_join_size_orig);
17097
DIE_IF(mysql_query(&con, query_buffer));
17100
DIE_IF(mysql_query(&con, "SET @@global.max_join_size = -1"));
17103
DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
17105
/* Issue COM_CHANGE_USER. */
17107
bug20023_change_user(&con);
17109
/* Query SQL_BIG_SELECTS. */
17111
query_int_variable(&con,
17112
"@@session.sql_big_selects",
17113
&sql_big_selects_5);
17115
/* Check that SQL_BIG_SELECTS is 1. */
17117
DIE_UNLESS(sql_big_selects_5 == sql_big_selects_orig);
17119
/***********************************************************************
17120
That's it. Cleanup.
17121
***********************************************************************/
17126
static void bug31418_impl()
17133
/* Create a new connection. */
17135
DIE_UNLESS(mysql_init(&con));
17137
DIE_UNLESS(mysql_real_connect(&con,
17141
opt_db ? opt_db : "test",
17144
CLIENT_FOUND_ROWS));
17146
/***********************************************************************
17147
Check that lock is free:
17148
- IS_FREE_LOCK() should return 1;
17149
- IS_USED_LOCK() should return NULL;
17150
***********************************************************************/
17152
is_null= query_int_variable(&con,
17153
"IS_FREE_LOCK('bug31418')",
17155
DIE_UNLESS(!is_null && rc);
17157
is_null= query_int_variable(&con,
17158
"IS_USED_LOCK('bug31418')",
17160
DIE_UNLESS(is_null);
17162
/***********************************************************************
17163
Acquire lock and check the lock status (the lock must be in use):
17164
- IS_FREE_LOCK() should return 0;
17165
- IS_USED_LOCK() should return non-zero thread id;
17166
***********************************************************************/
17168
query_int_variable(&con, "GET_LOCK('bug31418', 1)", &rc);
17171
is_null= query_int_variable(&con,
17172
"IS_FREE_LOCK('bug31418')",
17174
DIE_UNLESS(!is_null && !rc);
17176
is_null= query_int_variable(&con,
17177
"IS_USED_LOCK('bug31418')",
17179
DIE_UNLESS(!is_null && rc);
17181
/***********************************************************************
17182
Issue COM_CHANGE_USER command and check the lock status
17183
(the lock must be free):
17184
- IS_FREE_LOCK() should return 1;
17185
- IS_USED_LOCK() should return NULL;
17186
**********************************************************************/
17188
bug20023_change_user(&con);
17190
is_null= query_int_variable(&con,
17191
"IS_FREE_LOCK('bug31418')",
17193
DIE_UNLESS(!is_null && rc);
17195
is_null= query_int_variable(&con,
17196
"IS_USED_LOCK('bug31418')",
17198
DIE_UNLESS(is_null);
17200
/***********************************************************************
17201
That's it. Cleanup.
17202
***********************************************************************/
17207
static void test_bug31418()
17209
/* Run test case for BUG#31418 for three different connections. */
17221
Bug#31669 Buffer overflow in mysql_change_user()
17224
#define LARGE_BUFFER_SIZE 2048
17226
static void test_bug31669()
17229
static char buff[LARGE_BUFFER_SIZE+1];
17230
#ifndef EMBEDDED_LIBRARY
17231
static char user[USERNAME_CHAR_LENGTH+1];
17232
static char db[NAME_CHAR_LEN+1];
17233
static char query[LARGE_BUFFER_SIZE*2];
17236
DBUG_ENTER("test_bug31669");
17237
myheader("test_bug31669");
17239
rc= mysql_change_user(mysql, NULL, NULL, NULL);
17242
rc= mysql_change_user(mysql, "", "", "");
17245
memset(buff, 'a', sizeof(buff));
17247
rc= mysql_change_user(mysql, buff, buff, buff);
17250
rc = mysql_change_user(mysql, opt_user, opt_password, current_db);
17253
#ifndef EMBEDDED_LIBRARY
17254
memset(db, 'a', sizeof(db));
17255
db[NAME_CHAR_LEN]= 0;
17256
strxmov(query, "CREATE DATABASE IF NOT EXISTS ", db, NullS);
17257
rc= mysql_query(mysql, query);
17260
memset(user, 'b', sizeof(user));
17261
user[USERNAME_CHAR_LENGTH]= 0;
17262
memset(buff, 'c', sizeof(buff));
17263
buff[LARGE_BUFFER_SIZE]= 0;
17264
strxmov(query, "GRANT ALL PRIVILEGES ON *.* TO '", user, "'@'%' IDENTIFIED BY "
17265
"'", buff, "' WITH GRANT OPTION", NullS);
17266
rc= mysql_query(mysql, query);
17269
rc= mysql_query(mysql, "FLUSH PRIVILEGES");
17272
rc= mysql_change_user(mysql, user, buff, db);
17275
user[USERNAME_CHAR_LENGTH-1]= 'a';
17276
rc= mysql_change_user(mysql, user, buff, db);
17279
user[USERNAME_CHAR_LENGTH-1]= 'b';
17280
buff[LARGE_BUFFER_SIZE-1]= 'd';
17281
rc= mysql_change_user(mysql, user, buff, db);
17284
buff[LARGE_BUFFER_SIZE-1]= 'c';
17285
db[NAME_CHAR_LEN-1]= 'e';
17286
rc= mysql_change_user(mysql, user, buff, db);
17289
db[NAME_CHAR_LEN-1]= 'a';
17290
rc= mysql_change_user(mysql, user, buff, db);
17293
rc= mysql_change_user(mysql, user + 1, buff + 1, db + 1);
17296
rc = mysql_change_user(mysql, opt_user, opt_password, current_db);
17299
strxmov(query, "DROP DATABASE ", db, NullS);
17300
rc= mysql_query(mysql, query);
17303
strxmov(query, "DELETE FROM mysql.user WHERE User='", user, "'", NullS);
17304
rc= mysql_query(mysql, query);
17306
DIE_UNLESS(mysql_affected_rows(mysql) == 1);
17314
Bug#28386 the general log is incomplete
17317
static void test_bug28386()
17324
const char hello[]= "hello world!";
17326
DBUG_ENTER("test_bug28386");
17327
myheader("test_bug28386");
17329
rc= mysql_query(mysql, "select @@global.log_output");
17332
result= mysql_store_result(mysql);
17333
DIE_UNLESS(result);
17335
row= mysql_fetch_row(result);
17336
if (! strstr(row[0], "TABLE"))
17338
mysql_free_result(result);
17340
printf("Skipping the test since logging to tables is not enabled\n");
17341
/* Log output is not to tables */
17344
mysql_free_result(result);
17346
enable_general_log(1);
17348
stmt= mysql_simple_prepare(mysql, "SELECT ?");
17351
memset(&bind, 0, sizeof(bind));
17353
bind.buffer_type= MYSQL_TYPE_STRING;
17354
bind.buffer= (void *) hello;
17355
bind.buffer_length= sizeof(hello);
17357
mysql_stmt_bind_param(stmt, &bind);
17358
mysql_stmt_send_long_data(stmt, 0, hello, sizeof(hello));
17360
rc= mysql_stmt_execute(stmt);
17361
check_execute(stmt, rc);
17363
rc= my_process_stmt_result(stmt);
17364
DIE_UNLESS(rc == 1);
17366
rc= mysql_stmt_reset(stmt);
17367
check_execute(stmt, rc);
17369
rc= mysql_stmt_close(stmt);
17372
rc= mysql_query(mysql, "select * from mysql.general_log where "
17373
"command_type='Close stmt' or "
17374
"command_type='Reset stmt' or "
17375
"command_type='Long Data'");
17378
result= mysql_store_result(mysql);
17381
DIE_UNLESS(mysql_num_rows(result) == 3);
17383
mysql_free_result(result);
17385
restore_general_log();
17392
Bug#36004 mysql_stmt_prepare resets the list of warnings
17395
static void test_bug36004()
17397
int rc, warning_count= 0;
17400
DBUG_ENTER("test_bug36004");
17401
myheader("test_bug36004");
17403
rc= mysql_query(mysql, "drop table if exists inexistant");
17406
DIE_UNLESS(mysql_warning_count(mysql) == 1);
17407
query_int_variable(mysql, "@@warning_count", &warning_count);
17408
DIE_UNLESS(warning_count);
17410
stmt= mysql_simple_prepare(mysql, "select 1");
17413
DIE_UNLESS(mysql_warning_count(mysql) == 0);
17414
query_int_variable(mysql, "@@warning_count", &warning_count);
17415
DIE_UNLESS(warning_count);
17417
rc= mysql_stmt_execute(stmt);
17418
check_execute(stmt, rc);
17420
DIE_UNLESS(mysql_warning_count(mysql) == 0);
17421
mysql_stmt_close(stmt);
17423
query_int_variable(mysql, "@@warning_count", &warning_count);
17424
DIE_UNLESS(warning_count);
17426
stmt= mysql_simple_prepare(mysql, "drop table if exists inexistant");
17429
query_int_variable(mysql, "@@warning_count", &warning_count);
17430
DIE_UNLESS(warning_count == 0);
17436
Read and parse arguments and MySQL options from my.cnf
17439
static const char *client_test_load_default_groups[]= { "client", 0 };
17440
static char **defaults_argv;
17442
static struct my_option client_test_long_options[] =
17444
{"basedir", 'b', "Basedir for tests.", (uchar**) &opt_basedir,
17445
(uchar**) &opt_basedir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
17446
{"count", 't', "Number of times test to be executed", (uchar **) &opt_count,
17447
(uchar **) &opt_count, 0, GET_UINT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0},
17448
{"database", 'D', "Database to use", (uchar **) &opt_db, (uchar **) &opt_db,
17449
0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
17450
{"debug", '#', "Output debug log", (uchar**) &default_dbug_option,
17451
(uchar**) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
17452
{"help", '?', "Display this help and exit", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
17454
{"host", 'h', "Connect to host", (uchar **) &opt_host, (uchar **) &opt_host,
17455
0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
17457
"Password to use when connecting to server. If password is not given it's asked from the tty.",
17458
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
17459
{"port", 'P', "Port number to use for connection or 0 for default to, in "
17460
"order of preference, my.cnf, $MYSQL_TCP_PORT, "
17461
#if MYSQL_PORT_DEFAULT == 0
17464
"built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
17465
(uchar **) &opt_port,
17466
(uchar **) &opt_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
17467
{"server-arg", 'A', "Send embedded server this as a parameter.",
17468
0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
17469
{"show-tests", 'T', "Show all tests' names", 0, 0, 0, GET_NO_ARG, NO_ARG,
17471
{"silent", 's', "Be more silent", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0,
17473
{"socket", 'S', "Socket file to use for connection",
17474
(uchar **) &opt_unix_socket, (uchar **) &opt_unix_socket, 0, GET_STR,
17475
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
17477
"May disable some code when runs as mysql-test-run testcase.",
17478
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
17479
#ifndef DONT_ALLOW_USER_CHANGE
17480
{"user", 'u', "User for login if not current user", (uchar **) &opt_user,
17481
(uchar **) &opt_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
17483
{"vardir", 'v', "Data dir for tests.", (uchar**) &opt_vardir,
17484
(uchar**) &opt_vardir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
17485
{"getopt-ll-test", 'g', "Option for testing bug in getopt library",
17486
(uchar **) &opt_getopt_ll_test, (uchar **) &opt_getopt_ll_test, 0,
17487
GET_LL, REQUIRED_ARG, 0, 0, LONGLONG_MAX, 0, 0, 0},
17488
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
17492
static void usage(void)
17494
/* show the usage string when the user asks for this */
17495
putc('\n', stdout);
17496
printf("%s Ver %s Distrib %s, for %s (%s)\n",
17497
my_progname, VER, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
17498
puts("By Monty, Venu, Kent and others\n");
17500
Copyright (C) 2002-2004 MySQL AB\n\
17501
This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
17502
and you are welcome to modify and redistribute it under the GPL license\n");
17503
printf("Usage: %s [OPTIONS] [TESTNAME1 TESTNAME2...]\n", my_progname);
17504
my_print_help(client_test_long_options);
17505
print_defaults("my", client_test_load_default_groups);
17506
my_print_variables(client_test_long_options);
17510
static struct my_tests_st my_tests[]= {
17511
{ "disable_general_log", disable_general_log },
17512
{ "test_view_sp_list_fields", test_view_sp_list_fields },
17513
{ "client_query", client_query },
17514
{ "test_prepare_insert_update", test_prepare_insert_update},
17515
#if NOT_YET_WORKING
17516
{ "test_drop_temp", test_drop_temp },
17518
{ "test_fetch_seek", test_fetch_seek },
17519
{ "test_fetch_nobuffs", test_fetch_nobuffs },
17520
{ "test_open_direct", test_open_direct },
17521
{ "test_fetch_null", test_fetch_null },
17522
{ "test_ps_null_param", test_ps_null_param },
17523
{ "test_fetch_date", test_fetch_date },
17524
{ "test_fetch_str", test_fetch_str },
17525
{ "test_fetch_long", test_fetch_long },
17526
{ "test_fetch_short", test_fetch_short },
17527
{ "test_fetch_tiny", test_fetch_tiny },
17528
{ "test_fetch_bigint", test_fetch_bigint },
17529
{ "test_fetch_float", test_fetch_float },
17530
{ "test_fetch_double", test_fetch_double },
17531
{ "test_bind_result_ext", test_bind_result_ext },
17532
{ "test_bind_result_ext1", test_bind_result_ext1 },
17533
{ "test_select_direct", test_select_direct },
17534
{ "test_select_prepare", test_select_prepare },
17535
{ "test_select", test_select },
17536
{ "test_select_version", test_select_version },
17537
{ "test_ps_conj_select", test_ps_conj_select },
17538
{ "test_select_show_table", test_select_show_table },
17539
{ "test_func_fields", test_func_fields },
17540
{ "test_long_data", test_long_data },
17541
{ "test_insert", test_insert },
17542
{ "test_set_variable", test_set_variable },
17543
{ "test_select_show", test_select_show },
17544
{ "test_prepare_noparam", test_prepare_noparam },
17545
{ "test_bind_result", test_bind_result },
17546
{ "test_prepare_simple", test_prepare_simple },
17547
{ "test_prepare", test_prepare },
17548
{ "test_null", test_null },
17549
{ "test_debug_example", test_debug_example },
17550
{ "test_update", test_update },
17551
{ "test_simple_update", test_simple_update },
17552
{ "test_simple_delete", test_simple_delete },
17553
{ "test_double_compare", test_double_compare },
17554
{ "client_store_result", client_store_result },
17555
{ "client_use_result", client_use_result },
17556
{ "test_tran_bdb", test_tran_bdb },
17557
{ "test_tran_innodb", test_tran_innodb },
17558
{ "test_prepare_ext", test_prepare_ext },
17559
{ "test_prepare_syntax", test_prepare_syntax },
17560
{ "test_field_names", test_field_names },
17561
{ "test_field_flags", test_field_flags },
17562
{ "test_long_data_str", test_long_data_str },
17563
{ "test_long_data_str1", test_long_data_str1 },
17564
{ "test_long_data_bin", test_long_data_bin },
17565
{ "test_warnings", test_warnings },
17566
{ "test_errors", test_errors },
17567
{ "test_prepare_resultset", test_prepare_resultset },
17568
{ "test_stmt_close", test_stmt_close },
17569
{ "test_prepare_field_result", test_prepare_field_result },
17570
{ "test_multi_stmt", test_multi_stmt },
17571
{ "test_multi_statements", test_multi_statements },
17572
{ "test_prepare_multi_statements", test_prepare_multi_statements },
17573
{ "test_store_result", test_store_result },
17574
{ "test_store_result1", test_store_result1 },
17575
{ "test_store_result2", test_store_result2 },
17576
{ "test_subselect", test_subselect },
17577
{ "test_date", test_date },
17578
{ "test_date_date", test_date_date },
17579
{ "test_date_time", test_date_time },
17580
{ "test_date_ts", test_date_ts },
17581
{ "test_date_dt", test_date_dt },
17582
{ "test_prepare_alter", test_prepare_alter },
17583
{ "test_manual_sample", test_manual_sample },
17584
{ "test_pure_coverage", test_pure_coverage },
17585
{ "test_buffers", test_buffers },
17586
{ "test_ushort_bug", test_ushort_bug },
17587
{ "test_sshort_bug", test_sshort_bug },
17588
{ "test_stiny_bug", test_stiny_bug },
17589
{ "test_field_misc", test_field_misc },
17590
{ "test_set_option", test_set_option },
17591
#ifndef EMBEDDED_LIBRARY
17592
{ "test_prepare_grant", test_prepare_grant },
17594
{ "test_frm_bug", test_frm_bug },
17595
{ "test_explain_bug", test_explain_bug },
17596
{ "test_decimal_bug", test_decimal_bug },
17597
{ "test_nstmts", test_nstmts },
17598
{ "test_logs;", test_logs },
17599
{ "test_cuted_rows", test_cuted_rows },
17600
{ "test_fetch_offset", test_fetch_offset },
17601
{ "test_fetch_column", test_fetch_column },
17602
{ "test_mem_overun", test_mem_overun },
17603
{ "test_list_fields", test_list_fields },
17604
{ "test_free_result", test_free_result },
17605
{ "test_free_store_result", test_free_store_result },
17606
{ "test_sqlmode", test_sqlmode },
17607
{ "test_ts", test_ts },
17608
{ "test_bug1115", test_bug1115 },
17609
{ "test_bug1180", test_bug1180 },
17610
{ "test_bug1500", test_bug1500 },
17611
{ "test_bug1644", test_bug1644 },
17612
{ "test_bug1946", test_bug1946 },
17613
{ "test_bug2248", test_bug2248 },
17614
{ "test_parse_error_and_bad_length", test_parse_error_and_bad_length },
17615
{ "test_bug2247", test_bug2247 },
17616
{ "test_subqueries", test_subqueries },
17617
{ "test_bad_union", test_bad_union },
17618
{ "test_distinct", test_distinct },
17619
{ "test_subqueries_ref", test_subqueries_ref },
17620
{ "test_union", test_union },
17621
{ "test_bug3117", test_bug3117 },
17622
{ "test_join", test_join },
17623
{ "test_selecttmp", test_selecttmp },
17624
{ "test_create_drop", test_create_drop },
17625
{ "test_rename", test_rename },
17626
{ "test_do_set", test_do_set },
17627
{ "test_multi", test_multi },
17628
{ "test_insert_select", test_insert_select },
17629
{ "test_bind_nagative", test_bind_nagative },
17630
{ "test_derived", test_derived },
17631
{ "test_xjoin", test_xjoin },
17632
{ "test_bug3035", test_bug3035 },
17633
{ "test_union2", test_union2 },
17634
{ "test_bug1664", test_bug1664 },
17635
{ "test_union_param", test_union_param },
17636
{ "test_order_param", test_order_param },
17637
{ "test_ps_i18n", test_ps_i18n },
17638
{ "test_bug3796", test_bug3796 },
17639
{ "test_bug4026", test_bug4026 },
17640
{ "test_bug4079", test_bug4079 },
17641
{ "test_bug4236", test_bug4236 },
17642
{ "test_bug4030", test_bug4030 },
17643
{ "test_bug5126", test_bug5126 },
17644
{ "test_bug4231", test_bug4231 },
17645
{ "test_bug5399", test_bug5399 },
17646
{ "test_bug5194", test_bug5194 },
17647
{ "test_bug5315", test_bug5315 },
17648
{ "test_bug6049", test_bug6049 },
17649
{ "test_bug6058", test_bug6058 },
17650
{ "test_bug6059", test_bug6059 },
17651
{ "test_bug6046", test_bug6046 },
17652
{ "test_bug6081", test_bug6081 },
17653
{ "test_bug6096", test_bug6096 },
17654
{ "test_datetime_ranges", test_datetime_ranges },
17655
{ "test_bug4172", test_bug4172 },
17656
{ "test_conversion", test_conversion },
17657
{ "test_rewind", test_rewind },
17658
{ "test_bug6761", test_bug6761 },
17659
{ "test_view", test_view },
17660
{ "test_view_where", test_view_where },
17661
{ "test_view_2where", test_view_2where },
17662
{ "test_view_star", test_view_star },
17663
{ "test_view_insert", test_view_insert },
17664
{ "test_left_join_view", test_left_join_view },
17665
{ "test_view_insert_fields", test_view_insert_fields },
17666
{ "test_basic_cursors", test_basic_cursors },
17667
{ "test_cursors_with_union", test_cursors_with_union },
17668
{ "test_cursors_with_procedure", test_cursors_with_procedure },
17669
{ "test_truncation", test_truncation },
17670
{ "test_truncation_option", test_truncation_option },
17671
{ "test_client_character_set", test_client_character_set },
17672
{ "test_bug8330", test_bug8330 },
17673
{ "test_bug7990", test_bug7990 },
17674
{ "test_bug8378", test_bug8378 },
17675
{ "test_bug8722", test_bug8722 },
17676
{ "test_bug8880", test_bug8880 },
17677
{ "test_bug9159", test_bug9159 },
17678
{ "test_bug9520", test_bug9520 },
17679
{ "test_bug9478", test_bug9478 },
17680
{ "test_bug9643", test_bug9643 },
17681
{ "test_bug10729", test_bug10729 },
17682
{ "test_bug11111", test_bug11111 },
17683
{ "test_bug9992", test_bug9992 },
17684
{ "test_bug10736", test_bug10736 },
17685
{ "test_bug10794", test_bug10794 },
17686
{ "test_bug11172", test_bug11172 },
17687
{ "test_bug11656", test_bug11656 },
17688
{ "test_bug10214", test_bug10214 },
17689
{ "test_bug9735", test_bug9735 },
17690
{ "test_bug11183", test_bug11183 },
17691
{ "test_bug11037", test_bug11037 },
17692
{ "test_bug10760", test_bug10760 },
17693
{ "test_bug12001", test_bug12001 },
17694
{ "test_bug11718", test_bug11718 },
17695
{ "test_bug12925", test_bug12925 },
17696
{ "test_bug11909", test_bug11909 },
17697
{ "test_bug11901", test_bug11901 },
17698
{ "test_bug11904", test_bug11904 },
17699
{ "test_bug12243", test_bug12243 },
17700
{ "test_bug14210", test_bug14210 },
17701
{ "test_bug13488", test_bug13488 },
17702
{ "test_bug13524", test_bug13524 },
17703
{ "test_bug14845", test_bug14845 },
17704
{ "test_opt_reconnect", test_opt_reconnect },
17705
{ "test_bug15510", test_bug15510},
17706
#ifndef EMBEDDED_LIBRARY
17707
{ "test_bug12744", test_bug12744 },
17709
{ "test_bug16143", test_bug16143 },
17710
{ "test_bug16144", test_bug16144 },
17711
{ "test_bug15613", test_bug15613 },
17712
{ "test_bug20152", test_bug20152 },
17713
{ "test_bug14169", test_bug14169 },
17714
{ "test_bug17667", test_bug17667 },
17715
{ "test_bug15752", test_bug15752 },
17716
{ "test_mysql_insert_id", test_mysql_insert_id },
17717
{ "test_bug19671", test_bug19671 },
17718
{ "test_bug21206", test_bug21206 },
17719
{ "test_bug21726", test_bug21726 },
17720
{ "test_bug15518", test_bug15518 },
17721
{ "test_bug23383", test_bug23383 },
17722
{ "test_bug32265", test_bug32265 },
17723
{ "test_bug21635", test_bug21635 },
17724
{ "test_status", test_status },
17725
{ "test_bug24179", test_bug24179 },
17726
{ "test_ps_query_cache", test_ps_query_cache },
17727
{ "test_bug28075", test_bug28075 },
17728
{ "test_bug27876", test_bug27876 },
17729
{ "test_bug28505", test_bug28505 },
17730
{ "test_bug28934", test_bug28934 },
17731
{ "test_bug27592", test_bug27592 },
17732
{ "test_bug29687", test_bug29687 },
17733
{ "test_bug29692", test_bug29692 },
17734
{ "test_bug29306", test_bug29306 },
17735
{ "test_change_user", test_change_user },
17736
{ "test_bug30472", test_bug30472 },
17737
{ "test_bug20023", test_bug20023 },
17738
{ "test_bug31418", test_bug31418 },
17739
{ "test_bug31669", test_bug31669 },
17740
{ "test_bug28386", test_bug28386 },
17741
{ "test_bug36004", test_bug36004 },
17747
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
17752
DBUG_PUSH(argument ? argument : default_dbug_option);
17760
char *start=argument;
17761
my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
17762
opt_password= my_strdup(argument, MYF(MY_FAE));
17763
while (*argument) *argument++= 'x'; /* Destroy argument */
17771
if (argument == disabled_my_option)
17778
When the embedded server is being tested, the test suite needs to be
17779
able to pass command-line arguments to the embedded server so it can
17780
locate the language files and data directory. The test suite
17781
(mysql-test-run) never uses config files, just command-line options.
17783
if (!embedded_server_arg_count)
17785
embedded_server_arg_count= 1;
17786
embedded_server_args[0]= (char*) "";
17788
if (embedded_server_arg_count == MAX_SERVER_ARGS-1 ||
17789
!(embedded_server_args[embedded_server_arg_count++]=
17790
my_strdup(argument, MYF(MY_FAE))))
17792
DIE("Can't use server argument");
17797
struct my_tests_st *fptr;
17799
printf("All possible test names:\n\n");
17800
for (fptr= my_tests; fptr->name; fptr++)
17801
printf("%s\n", fptr->name);
17806
case 'I': /* Info */
17814
static void get_options(int *argc, char ***argv)
17818
if ((ho_error= handle_options(argc, argv, client_test_long_options,
17823
opt_password= get_tty_password(NullS);
17828
Print the test output on successful execution before exiting
17831
static void print_test_output()
17833
if (opt_silent < 3)
17835
fprintf(stdout, "\n\n");
17836
fprintf(stdout, "All '%d' tests were successful (in '%d' iterations)",
17837
test_count-1, opt_count);
17838
fprintf(stdout, "\n Total execution time: %g SECS", total_time);
17840
fprintf(stdout, " (Avg: %g SECS)", total_time/opt_count);
17842
fprintf(stdout, "\n\n!!! SUCCESS !!!\n");
17846
/***************************************************************************
17848
***************************************************************************/
17851
int main(int argc, char **argv)
17853
struct my_tests_st *fptr;
17857
load_defaults("my", client_test_load_default_groups, &argc, &argv);
17858
defaults_argv= argv;
17859
get_options(&argc, &argv);
17861
if (mysql_server_init(embedded_server_arg_count,
17862
embedded_server_args,
17863
(char**) embedded_server_groups))
17864
DIE("Can't initialize MySQL server");
17866
client_connect(0); /* connect to server */
17869
for (iter_count= 1; iter_count <= opt_count; iter_count++)
17871
/* Start of tests */
17873
start_time= time((time_t *)0);
17876
for (fptr= my_tests; fptr->name; fptr++)
17877
(*fptr->function)();
17881
for ( ; *argv ; argv++)
17883
for (fptr= my_tests; fptr->name; fptr++)
17885
if (!strcmp(fptr->name, *argv))
17887
(*fptr->function)();
17893
fprintf(stderr, "\n\nGiven test not found: '%s'\n", *argv);
17894
fprintf(stderr, "See legal test names with %s -T\n\nAborting!\n",
17896
client_disconnect();
17897
free_defaults(defaults_argv);
17903
end_time= time((time_t *)0);
17904
total_time+= difftime(end_time, start_time);
17909
client_disconnect(); /* disconnect from server */
17911
free_defaults(defaults_argv);
17912
print_test_output();
17914
while (embedded_server_arg_count > 1)
17915
my_free(embedded_server_args[--embedded_server_arg_count],MYF(0));
17917
mysql_server_end();