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;
50
static char *shared_memory_base_name= 0;
52
static unsigned int opt_port;
53
static my_bool tty_password= 0, opt_silent= 0;
55
static MYSQL *mysql= 0;
56
static char current_db[]= "client_test_db";
57
static unsigned int test_count= 0;
58
static unsigned int opt_count= 0;
59
static unsigned int iter_count= 0;
60
static my_bool have_innodb= FALSE;
62
static const char *opt_basedir= "./";
63
static const char *opt_vardir= "mysql-test/var";
65
static longlong opt_getopt_ll_test= 0;
67
static int embedded_server_arg_count= 0;
68
static char *embedded_server_args[MAX_SERVER_ARGS];
70
static const char *embedded_server_groups[]= {
73
"mysql_client_test_SERVER",
77
static time_t start_time, end_time;
78
static double total_time;
80
const char *default_dbug_option= "d:t:o,/tmp/mysql_client_test.trace";
88
#define myheader(str) \
89
DBUG_PRINT("test", ("name: %s", str)); \
92
fprintf(stdout, "\n\n#####################################\n"); \
93
fprintf(stdout, "%u of (%u/%u): %s", test_count++, iter_count, \
95
fprintf(stdout, " \n#####################################\n"); \
98
#define myheader_r(str) \
99
DBUG_PRINT("test", ("name: %s", str)); \
102
fprintf(stdout, "\n\n#####################################\n"); \
103
fprintf(stdout, "%s", str); \
104
fprintf(stdout, " \n#####################################\n"); \
107
static void print_error(const char *msg);
108
static void print_st_error(MYSQL_STMT *stmt, const char *msg);
109
static void client_disconnect(MYSQL* mysql, my_bool drop_db);
113
Abort unless given experssion is non-zero.
119
We can't use any kind of system assert as we need to
120
preserve tested invariants in release builds as well.
123
#define DIE_UNLESS(expr) \
124
((void) ((expr) ? 0 : (die(__FILE__, __LINE__, #expr), 0)))
125
#define DIE_IF(expr) \
126
((void) ((expr) ? (die(__FILE__, __LINE__, #expr), 0) : 0))
128
die(__FILE__, __LINE__, #expr)
130
static void die(const char *file, int line, const char *expr)
133
fprintf(stderr, "%s:%d: check failed: '%s'\n", file, line, expr);
139
#define myerror(msg) print_error(msg)
140
#define mysterror(stmt, msg) print_st_error(stmt, msg)
142
#define myquery(RES) \
147
DIE_UNLESS(r == 0); \
150
#define myquery_r(r) \
154
DIE_UNLESS(r != 0); \
157
#define check_execute(stmt, r) \
160
mysterror(stmt, NULL); \
164
#define check_execute_r(stmt, r) \
167
mysterror(stmt, NULL); \
171
#define check_stmt(stmt) \
175
DIE_UNLESS(stmt != 0); \
178
#define check_stmt_r(stmt) \
182
DIE_UNLESS(stmt == 0);\
185
#define mytest(x) if (!(x)) {myerror(NULL);DIE_UNLESS(FALSE);}
186
#define mytest_r(x) if ((x)) {myerror(NULL);DIE_UNLESS(FALSE);}
189
/* A workaround for Sun Forte 5.6 on Solaris x86 */
191
static int cmp_double(double *a, double *b)
197
/* Print the error message */
199
static void print_error(const char *msg)
203
if (mysql && mysql_errno(mysql))
205
if (mysql->server_version)
206
fprintf(stdout, "\n [MySQL-%s]", mysql->server_version);
208
fprintf(stdout, "\n [MySQL]");
209
fprintf(stdout, "[%d] %s\n", mysql_errno(mysql), mysql_error(mysql));
212
fprintf(stderr, " [MySQL] %s\n", msg);
217
static void print_st_error(MYSQL_STMT *stmt, const char *msg)
221
if (stmt && mysql_stmt_errno(stmt))
223
if (stmt->mysql && stmt->mysql->server_version)
224
fprintf(stdout, "\n [MySQL-%s]", stmt->mysql->server_version);
226
fprintf(stdout, "\n [MySQL]");
228
fprintf(stdout, "[%d] %s\n", mysql_stmt_errno(stmt),
229
mysql_stmt_error(stmt));
232
fprintf(stderr, " [MySQL] %s\n", msg);
237
Enhanced version of mysql_client_init(), which may also set shared memory
240
static MYSQL *mysql_client_init(MYSQL* con)
242
MYSQL* res = mysql_init(con);
244
if (res && shared_memory_base_name)
245
mysql_options(res, MYSQL_SHARED_MEMORY_BASE_NAME, shared_memory_base_name);
251
Disable direct calls of mysql_init, as it disregards shared memory base.
253
#define mysql_init(A) Please use mysql_client_init instead of mysql_init
256
/* Check if the connection has InnoDB tables */
258
static my_bool check_have_innodb(MYSQL *conn)
265
rc= mysql_query(conn, "show variables like 'have_innodb'");
267
res= mysql_use_result(conn);
270
row= mysql_fetch_row(res);
273
result= strcmp(row[1], "YES") == 0;
274
mysql_free_result(res);
280
This is to be what mysql_query() is for mysql_real_query(), for
281
mysql_simple_prepare(): a variant without the 'length' parameter.
284
static MYSQL_STMT *STDCALL
285
mysql_simple_prepare(MYSQL *mysql_arg, const char *query)
287
MYSQL_STMT *stmt= mysql_stmt_init(mysql_arg);
288
if (stmt && mysql_stmt_prepare(stmt, query, (uint) strlen(query)))
290
mysql_stmt_close(stmt);
298
Connect to the server with options given by arguments to this application,
299
stored in global variables opt_host, opt_user, opt_password, opt_db,
300
opt_port and opt_unix_socket.
302
@param flag[in] client_flag passed on to mysql_real_connect
303
@param protocol[in] MYSQL_PROTOCOL_* to use for this connection
304
@param auto_reconnect[in] set to 1 for auto reconnect
306
@return pointer to initialized and connected MYSQL object
308
static MYSQL* client_connect(ulong flag, uint protocol, my_bool auto_reconnect)
312
static char query[MAX_TEST_QUERY_LENGTH];
313
myheader_r("client_connect");
316
fprintf(stdout, "\n Establishing a connection to '%s' ...",
317
opt_host ? opt_host : "");
319
if (!(mysql= mysql_client_init(NULL)))
322
myerror("mysql_client_init() failed");
325
/* enable local infile, in non-binary builds often disabled by default */
326
mysql_options(mysql, MYSQL_OPT_LOCAL_INFILE, 0);
327
mysql_options(mysql, MYSQL_OPT_PROTOCOL, &protocol);
329
if (!(mysql_real_connect(mysql, opt_host, opt_user,
330
opt_password, opt_db ? opt_db:"test", opt_port,
331
opt_unix_socket, flag)))
334
myerror("connection failed");
336
fprintf(stdout, "\n Check the connection options using --help or -?\n");
339
mysql->reconnect= auto_reconnect;
342
fprintf(stdout, "OK");
344
/* set AUTOCOMMIT to ON*/
345
mysql_autocommit(mysql, TRUE);
349
fprintf(stdout, "\nConnected to MySQL server version: %s (%lu)\n",
350
mysql_get_server_info(mysql),
351
(ulong) mysql_get_server_version(mysql));
352
fprintf(stdout, "\n Creating a test database '%s' ...", current_db);
354
strxmov(query, "CREATE DATABASE IF NOT EXISTS ", current_db, NullS);
356
rc= mysql_query(mysql, query);
359
strxmov(query, "USE ", current_db, NullS);
360
rc= mysql_query(mysql, query);
362
have_innodb= check_have_innodb(mysql);
365
fprintf(stdout, "OK");
371
/* Close the connection */
373
static void client_disconnect(MYSQL* mysql, my_bool drop_db)
375
static char query[MAX_TEST_QUERY_LENGTH];
377
myheader_r("client_disconnect");
384
fprintf(stdout, "\n dropping the test database '%s' ...", current_db);
385
strxmov(query, "DROP DATABASE IF EXISTS ", current_db, NullS);
387
mysql_query(mysql, query);
389
fprintf(stdout, "OK");
393
fprintf(stdout, "\n closing the connection ...");
396
fprintf(stdout, "OK\n");
401
/* Query processing */
403
static void client_query()
407
myheader("client_query");
409
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
412
rc= mysql_query(mysql, "CREATE TABLE t1("
413
"id int primary key auto_increment, "
414
"name varchar(20))");
417
rc= mysql_query(mysql, "CREATE TABLE t1(id int, name varchar(20))");
420
rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('mysql')");
423
rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('monty')");
426
rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('venu')");
429
rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
432
rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
435
rc= mysql_query(mysql, "UPDATE t1 SET name= 'updated' "
436
"WHERE name= 'deleted'");
439
rc= mysql_query(mysql, "UPDATE t1 SET id= 3 WHERE name= 'updated'");
442
myquery(mysql_query(mysql, "drop table t1"));
448
static void my_print_dashes(MYSQL_RES *result)
453
mysql_field_seek(result, 0);
457
for(i= 0; i< mysql_num_fields(result); i++)
459
field= mysql_fetch_field(result);
460
for(j= 0; j < field->max_length+2; j++)
468
/* Print resultset metadata information */
470
static void my_print_result_metadata(MYSQL_RES *result)
474
unsigned int field_count;
476
mysql_field_seek(result, 0);
483
field_count= mysql_num_fields(result);
484
for(i= 0; i< field_count; i++)
486
field= mysql_fetch_field(result);
487
j= strlen(field->name);
488
if (j < field->max_length)
489
j= field->max_length;
490
if (j < 4 && !IS_NOT_NULL(field->flags))
492
field->max_length= j;
496
my_print_dashes(result);
501
mysql_field_seek(result, 0);
502
for(i= 0; i< field_count; i++)
504
field= mysql_fetch_field(result);
506
fprintf(stdout, " %-*s |", (int) field->max_length, field->name);
511
my_print_dashes(result);
516
/* Process the result set */
518
static int my_process_result_set(MYSQL_RES *result)
523
unsigned int row_count= 0;
528
my_print_result_metadata(result);
530
while ((row= mysql_fetch_row(result)) != NULL)
532
mysql_field_seek(result, 0);
539
for(i= 0; i< mysql_num_fields(result); i++)
541
field= mysql_fetch_field(result);
545
fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
546
else if (IS_NUM(field->type))
547
fprintf(stdout, " %*s |", (int) field->max_length, row[i]);
549
fprintf(stdout, " %-*s |", (int) field->max_length, row[i]);
562
my_print_dashes(result);
564
if (mysql_errno(mysql) != 0)
565
fprintf(stderr, "\n\tmysql_fetch_row() failed\n");
567
fprintf(stdout, "\n\t%d %s returned\n", row_count,
568
row_count == 1 ? "row" : "rows");
574
static int my_process_result(MYSQL *mysql_arg)
579
if (!(result= mysql_store_result(mysql_arg)))
582
row_count= my_process_result_set(result);
584
mysql_free_result(result);
589
/* Process the statement result set */
591
#define MAX_RES_FIELDS 50
592
#define MAX_FIELD_DATA_SIZE 255
594
static int my_process_stmt_result(MYSQL_STMT *stmt)
598
MYSQL_BIND buffer[MAX_RES_FIELDS];
601
char data[MAX_RES_FIELDS][MAX_FIELD_DATA_SIZE];
602
ulong length[MAX_RES_FIELDS];
603
my_bool is_null[MAX_RES_FIELDS];
606
if (!(result= mysql_stmt_result_metadata(stmt))) /* No meta info */
608
while (!mysql_stmt_fetch(stmt))
613
field_count= min(mysql_num_fields(result), MAX_RES_FIELDS);
615
bzero((char*) buffer, sizeof(buffer));
616
bzero((char*) length, sizeof(length));
617
bzero((char*) is_null, sizeof(is_null));
619
for(i= 0; i < field_count; i++)
621
buffer[i].buffer_type= MYSQL_TYPE_STRING;
622
buffer[i].buffer_length= MAX_FIELD_DATA_SIZE;
623
buffer[i].length= &length[i];
624
buffer[i].buffer= (void *) data[i];
625
buffer[i].is_null= &is_null[i];
628
rc= mysql_stmt_bind_result(stmt, buffer);
629
check_execute(stmt, rc);
632
mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*)&rc);
633
rc= mysql_stmt_store_result(stmt);
634
check_execute(stmt, rc);
635
my_print_result_metadata(result);
637
mysql_field_seek(result, 0);
638
while ((rc= mysql_stmt_fetch(stmt)) == 0)
645
mysql_field_seek(result, 0);
646
for (i= 0; i < field_count; i++)
648
field= mysql_fetch_field(result);
652
fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
653
else if (length[i] == 0)
655
data[i][0]= '\0'; /* unmodified buffer */
656
fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
658
else if (IS_NUM(field->type))
659
fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
661
fprintf(stdout, " %-*s |", (int) field->max_length, data[i]);
671
DIE_UNLESS(rc == MYSQL_NO_DATA);
675
my_print_dashes(result);
676
fprintf(stdout, "\n\t%d %s returned\n", row_count,
677
row_count == 1 ? "row" : "rows");
679
mysql_free_result(result);
684
/* Prepare statement, execute, and process result set for given query */
686
int my_stmt_result(const char *buff)
693
fprintf(stdout, "\n\n %s", buff);
694
stmt= mysql_simple_prepare(mysql, buff);
697
rc= mysql_stmt_execute(stmt);
698
check_execute(stmt, rc);
700
row_count= my_process_stmt_result(stmt);
701
mysql_stmt_close(stmt);
707
/* Utility function to verify a particular column data */
709
static void verify_col_data(const char *table, const char *col,
710
const char *exp_data)
712
static char query[MAX_TEST_QUERY_LENGTH];
719
strxmov(query, "SELECT ", col, " FROM ", table, " LIMIT 1", NullS);
721
fprintf(stdout, "\n %s", query);
722
rc= mysql_query(mysql, query);
728
result= mysql_use_result(mysql);
731
if (!(row= mysql_fetch_row(result)) || !row[field])
733
fprintf(stdout, "\n *** ERROR: FAILED TO GET THE RESULT ***");
736
if (strcmp(row[field], exp_data))
738
fprintf(stdout, "\n obtained: `%s` (expected: `%s`)",
739
row[field], exp_data);
742
mysql_free_result(result);
746
/* Utility function to verify the field members */
748
#define verify_prepare_field(result,no,name,org_name,type,table,\
749
org_table,db,length,def) \
750
do_verify_prepare_field((result),(no),(name),(org_name),(type), \
751
(table),(org_table),(db),(length),(def), \
754
static void do_verify_prepare_field(MYSQL_RES *result,
755
unsigned int no, const char *name,
756
const char *org_name,
757
enum enum_field_types type,
759
const char *org_table, const char *db,
760
unsigned long length, const char *def,
761
const char *file, int line)
765
ulonglong expected_field_length;
767
if (!(field= mysql_fetch_field_direct(result, no)))
769
fprintf(stdout, "\n *** ERROR: FAILED TO GET THE RESULT ***");
772
cs= get_charset(field->charsetnr, 0);
774
if ((expected_field_length= length * cs->mbmaxlen) > UINT_MAX32)
775
expected_field_length= UINT_MAX32;
778
fprintf(stdout, "\n field[%d]:", no);
779
fprintf(stdout, "\n name :`%s`\t(expected: `%s`)", field->name, name);
780
fprintf(stdout, "\n org_name :`%s`\t(expected: `%s`)",
781
field->org_name, org_name);
782
fprintf(stdout, "\n type :`%d`\t(expected: `%d`)", field->type, type);
784
fprintf(stdout, "\n table :`%s`\t(expected: `%s`)",
785
field->table, table);
787
fprintf(stdout, "\n org_table:`%s`\t(expected: `%s`)",
788
field->org_table, org_table);
789
fprintf(stdout, "\n database :`%s`\t(expected: `%s`)", field->db, db);
790
fprintf(stdout, "\n length :`%lu`\t(expected: `%llu`)",
791
field->length, expected_field_length);
792
fprintf(stdout, "\n maxlength:`%ld`", field->max_length);
793
fprintf(stdout, "\n charsetnr:`%d`", field->charsetnr);
794
fprintf(stdout, "\n default :`%s`\t(expected: `%s`)",
795
field->def ? field->def : "(null)", def ? def: "(null)");
796
fprintf(stdout, "\n");
798
DIE_UNLESS(strcmp(field->name, name) == 0);
799
DIE_UNLESS(strcmp(field->org_name, org_name) == 0);
801
XXX: silent column specification change works based on number of
802
bytes a column occupies. So CHAR -> VARCHAR upgrade is possible even
803
for CHAR(2) column if its character set is multibyte.
804
VARCHAR -> CHAR downgrade won't work for VARCHAR(3) as one would
807
if (cs->mbmaxlen == 1)
809
if (field->type != type)
812
"Expected field type: %d, got type: %d in file %s, line %d\n",
813
(int) type, (int) field->type, file, line);
814
DIE_UNLESS(field->type == type);
818
DIE_UNLESS(strcmp(field->table, table) == 0);
820
DIE_UNLESS(strcmp(field->org_table, org_table) == 0);
821
DIE_UNLESS(strcmp(field->db, db) == 0);
823
Character set should be taken into account for multibyte encodings, such
824
as utf8. Field length is calculated as number of characters * maximum
825
number of bytes a character can occupy.
827
if (length && (field->length != expected_field_length))
829
fprintf(stderr, "Expected field length: %llu, got length: %lu\n",
830
expected_field_length, field->length);
831
DIE_UNLESS(field->length == expected_field_length);
834
DIE_UNLESS(strcmp(field->def, def) == 0);
838
/* Utility function to verify the parameter count */
840
static void verify_param_count(MYSQL_STMT *stmt, long exp_count)
842
long param_count= mysql_stmt_param_count(stmt);
844
fprintf(stdout, "\n total parameters in stmt: `%ld` (expected: `%ld`)",
845
param_count, exp_count);
846
DIE_UNLESS(param_count == exp_count);
850
/* Utility function to verify the total affected rows */
852
static void verify_st_affected_rows(MYSQL_STMT *stmt, ulonglong exp_count)
854
ulonglong affected_rows= mysql_stmt_affected_rows(stmt);
856
fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
857
(long) affected_rows, (long) exp_count);
858
DIE_UNLESS(affected_rows == exp_count);
862
/* Utility function to verify the total affected rows */
864
static void verify_affected_rows(ulonglong exp_count)
866
ulonglong affected_rows= mysql_affected_rows(mysql);
868
fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
869
(long) affected_rows, (long) exp_count);
870
DIE_UNLESS(affected_rows == exp_count);
874
/* Utility function to verify the total fields count */
876
static void verify_field_count(MYSQL_RES *result, uint exp_count)
878
uint field_count= mysql_num_fields(result);
880
fprintf(stdout, "\n total fields in the result set: `%d` (expected: `%d`)",
881
field_count, exp_count);
882
DIE_UNLESS(field_count == exp_count);
886
/* Utility function to execute a query using prepare-execute */
888
#ifndef EMBEDDED_LIBRARY
889
static void execute_prepare_query(const char *query, ulonglong exp_count)
892
ulonglong affected_rows;
895
stmt= mysql_simple_prepare(mysql, query);
898
rc= mysql_stmt_execute(stmt);
901
affected_rows= mysql_stmt_affected_rows(stmt);
903
fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
904
(long) affected_rows, (long) exp_count);
906
DIE_UNLESS(affected_rows == exp_count);
907
mysql_stmt_close(stmt);
911
/* Store result processing */
913
static void client_store_result()
918
myheader("client_store_result");
920
rc= mysql_query(mysql, "SELECT * FROM t1");
924
result= mysql_store_result(mysql);
927
(void) my_process_result_set(result);
928
mysql_free_result(result);
932
/* Fetch the results */
934
static void client_use_result()
938
myheader("client_use_result");
940
rc= mysql_query(mysql, "SELECT * FROM t1");
944
result= mysql_use_result(mysql);
947
(void) my_process_result_set(result);
948
mysql_free_result(result);
953
Accepts arbitrary number of queries and runs them against the database.
954
Used to fill tables for each test.
957
void fill_tables(const char **query_list, unsigned query_count)
961
DBUG_ENTER("fill_tables");
962
for (query= query_list; query < query_list + query_count;
965
rc= mysql_query(mysql, *query);
972
All state of fetch from one statement: statement handle, out buffers,
974
See fetch_n for for the only use case.
977
enum { MAX_COLUMN_LENGTH= 255 };
979
typedef struct st_stmt_fetch
985
MYSQL_BIND *bind_array;
987
unsigned long *out_data_length;
988
unsigned column_count;
994
Create statement handle, prepare it with statement, execute and allocate
998
void stmt_fetch_init(Stmt_fetch *fetch, unsigned stmt_no_arg,
999
const char *query_arg)
1001
unsigned long type= CURSOR_TYPE_READ_ONLY;
1004
MYSQL_RES *metadata;
1005
DBUG_ENTER("stmt_fetch_init");
1007
/* Save query and statement number for error messages */
1008
fetch->stmt_no= stmt_no_arg;
1009
fetch->query= query_arg;
1011
fetch->handle= mysql_stmt_init(mysql);
1013
rc= mysql_stmt_prepare(fetch->handle, fetch->query, strlen(fetch->query));
1014
check_execute(fetch->handle, rc);
1017
The attribute is sent to server on execute and asks to open read-only
1020
mysql_stmt_attr_set(fetch->handle, STMT_ATTR_CURSOR_TYPE,
1021
(const void*) &type);
1023
rc= mysql_stmt_execute(fetch->handle);
1024
check_execute(fetch->handle, rc);
1026
/* Find out total number of columns in result set */
1027
metadata= mysql_stmt_result_metadata(fetch->handle);
1028
fetch->column_count= mysql_num_fields(metadata);
1029
mysql_free_result(metadata);
1032
Now allocate bind handles and buffers for output data:
1033
calloc memory to reduce number of MYSQL_BIND members we need to
1037
fetch->bind_array= (MYSQL_BIND *) calloc(1, sizeof(MYSQL_BIND) *
1038
fetch->column_count);
1039
fetch->out_data= (char**) calloc(1, sizeof(char*) * fetch->column_count);
1040
fetch->out_data_length= (ulong*) calloc(1, sizeof(ulong) *
1041
fetch->column_count);
1042
for (i= 0; i < fetch->column_count; ++i)
1044
fetch->out_data[i]= (char*) calloc(1, MAX_COLUMN_LENGTH);
1045
fetch->bind_array[i].buffer_type= MYSQL_TYPE_STRING;
1046
fetch->bind_array[i].buffer= fetch->out_data[i];
1047
fetch->bind_array[i].buffer_length= MAX_COLUMN_LENGTH;
1048
fetch->bind_array[i].length= fetch->out_data_length + i;
1051
mysql_stmt_bind_result(fetch->handle, fetch->bind_array);
1053
fetch->row_count= 0;
1054
fetch->is_open= TRUE;
1056
/* Ready for reading rows */
1061
/* Fetch and print one row from cursor */
1063
int stmt_fetch_fetch_row(Stmt_fetch *fetch)
1067
DBUG_ENTER("stmt_fetch_fetch_row");
1069
if ((rc= mysql_stmt_fetch(fetch->handle)) == 0)
1073
printf("Stmt %d fetched row %d:\n", fetch->stmt_no, fetch->row_count);
1074
for (i= 0; i < fetch->column_count; ++i)
1076
fetch->out_data[i][fetch->out_data_length[i]]= '\0';
1078
printf("column %d: %s\n", i+1, fetch->out_data[i]);
1082
fetch->is_open= FALSE;
1087
void stmt_fetch_close(Stmt_fetch *fetch)
1090
DBUG_ENTER("stmt_fetch_close");
1092
for (i= 0; i < fetch->column_count; ++i)
1093
free(fetch->out_data[i]);
1094
free(fetch->out_data);
1095
free(fetch->out_data_length);
1096
free(fetch->bind_array);
1097
mysql_stmt_close(fetch->handle);
1102
For given array of queries, open query_count cursors and fetch
1103
from them in simultaneous manner.
1104
In case there was an error in one of the cursors, continue
1105
reading from the rest.
1108
enum fetch_type { USE_ROW_BY_ROW_FETCH= 0, USE_STORE_RESULT= 1 };
1110
my_bool fetch_n(const char **query_list, unsigned query_count,
1111
enum fetch_type fetch_type)
1113
unsigned open_statements= query_count;
1114
int rc, error_count= 0;
1115
Stmt_fetch *fetch_array= (Stmt_fetch*) calloc(1, sizeof(Stmt_fetch) *
1118
DBUG_ENTER("fetch_n");
1120
for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1122
/* Init will exit(1) in case of error */
1123
stmt_fetch_init(fetch, fetch - fetch_array,
1124
query_list[fetch - fetch_array]);
1127
if (fetch_type == USE_STORE_RESULT)
1129
for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1131
rc= mysql_stmt_store_result(fetch->handle);
1132
check_execute(fetch->handle, rc);
1136
while (open_statements)
1138
for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1140
if (fetch->is_open && (rc= stmt_fetch_fetch_row(fetch)))
1144
We try to fetch from the rest of the statements in case of
1147
if (rc != MYSQL_NO_DATA)
1150
"Got error reading rows from statement %d,\n"
1152
"error message: %s", (int) (fetch - fetch_array),
1154
mysql_stmt_error(fetch->handle));
1161
fprintf(stderr, "Fetch FAILED");
1164
unsigned total_row_count= 0;
1165
for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1166
total_row_count+= fetch->row_count;
1168
printf("Success, total rows fetched: %d\n", total_row_count);
1170
for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1171
stmt_fetch_close(fetch);
1173
DBUG_RETURN(error_count != 0);
1176
/* Separate thread query to test some cases */
1178
static my_bool thread_query(char *query)
1185
fprintf(stdout, "\n in thread_query(%s)", query);
1186
if (!(l_mysql= mysql_client_init(NULL)))
1188
myerror("mysql_client_init() failed");
1191
if (!(mysql_real_connect(l_mysql, opt_host, opt_user,
1192
opt_password, current_db, opt_port,
1193
opt_unix_socket, 0)))
1195
myerror("connection failed");
1199
l_mysql->reconnect= 1;
1200
if (mysql_query(l_mysql, (char *)query))
1202
fprintf(stderr, "Query failed (%s)\n", mysql_error(l_mysql));
1206
mysql_commit(l_mysql);
1208
mysql_close(l_mysql);
1213
/* Query processing */
1215
static void test_debug_example()
1220
myheader("test_debug_example");
1222
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_debug_example");
1225
rc= mysql_query(mysql, "CREATE TABLE test_debug_example("
1226
"id INT PRIMARY KEY AUTO_INCREMENT, "
1227
"name VARCHAR(20), xxx INT)");
1230
rc= mysql_query(mysql, "INSERT INTO test_debug_example (name) "
1231
"VALUES ('mysql')");
1234
rc= mysql_query(mysql, "UPDATE test_debug_example SET name='updated' "
1235
"WHERE name='deleted'");
1238
rc= mysql_query(mysql, "SELECT * FROM test_debug_example where name='mysql'");
1241
result= mysql_use_result(mysql);
1244
(void) my_process_result_set(result);
1245
mysql_free_result(result);
1247
rc= mysql_query(mysql, "DROP TABLE test_debug_example");
1252
/* Test autocommit feature for BDB tables */
1254
static void test_tran_bdb()
1260
myheader("test_tran_bdb");
1262
/* set AUTOCOMMIT to OFF */
1263
rc= mysql_autocommit(mysql, FALSE);
1266
rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction");
1270
/* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
1271
rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction( "
1272
"col1 int , col2 varchar(30)) ENGINE= BDB");
1275
/* insert a row and commit the transaction */
1276
rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')");
1279
rc= mysql_commit(mysql);
1282
/* now insert the second row, and roll back the transaction */
1283
rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')");
1286
rc= mysql_rollback(mysql);
1289
/* delete first row, and roll it back */
1290
rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10");
1293
rc= mysql_rollback(mysql);
1296
/* test the results now, only one row should exist */
1297
rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
1300
/* get the result */
1301
result= mysql_store_result(mysql);
1304
(void) my_process_result_set(result);
1305
mysql_free_result(result);
1307
/* test the results now, only one row should exist */
1308
rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
1311
/* get the result */
1312
result= mysql_use_result(mysql);
1315
row= mysql_fetch_row(result);
1318
row= mysql_fetch_row(result);
1321
mysql_free_result(result);
1322
mysql_autocommit(mysql, TRUE);
1326
/* Test autocommit feature for InnoDB tables */
1328
static void test_tran_innodb()
1334
myheader("test_tran_innodb");
1336
/* set AUTOCOMMIT to OFF */
1337
rc= mysql_autocommit(mysql, FALSE);
1340
rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction");
1343
/* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
1344
rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction(col1 int, "
1345
"col2 varchar(30)) ENGINE= InnoDB");
1348
/* insert a row and commit the transaction */
1349
rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')");
1352
rc= mysql_commit(mysql);
1355
/* now insert the second row, and roll back the transaction */
1356
rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')");
1359
rc= mysql_rollback(mysql);
1362
/* delete first row, and roll it back */
1363
rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10");
1366
rc= mysql_rollback(mysql);
1369
/* test the results now, only one row should exist */
1370
rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
1373
/* get the result */
1374
result= mysql_store_result(mysql);
1377
(void) my_process_result_set(result);
1378
mysql_free_result(result);
1380
/* test the results now, only one row should exist */
1381
rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
1384
/* get the result */
1385
result= mysql_use_result(mysql);
1388
row= mysql_fetch_row(result);
1391
row= mysql_fetch_row(result);
1394
mysql_free_result(result);
1395
mysql_autocommit(mysql, TRUE);
1399
/* Test for BUG#7242 */
1401
static void test_prepare_insert_update()
1406
const char *testcase[]= {
1407
"CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B))",
1408
"INSERT t1 VALUES (1,2,10), (3,4,20)",
1409
"INSERT t1 VALUES (5,6,30), (7,4,40), (8,9,60) ON DUPLICATE KEY UPDATE c=c+100",
1411
"INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0",
1413
"INSERT t1 VALUES (2,1,11), (7,4,40) ON DUPLICATE KEY UPDATE c=c+VALUES(a)",
1415
const char **cur_query;
1417
myheader("test_prepare_insert_update");
1419
for (cur_query= testcase; *cur_query; cur_query++)
1421
char query[MAX_TEST_QUERY_LENGTH];
1422
printf("\nRunning query: %s", *cur_query);
1423
strmov(query, *cur_query);
1424
stmt= mysql_simple_prepare(mysql, query);
1427
verify_param_count(stmt, 0);
1428
rc= mysql_stmt_execute(stmt);
1430
check_execute(stmt, rc);
1431
/* try the last query several times */
1434
for (i=0; i < 3;i++)
1436
printf("\nExecuting last statement again");
1437
rc= mysql_stmt_execute(stmt);
1438
check_execute(stmt, rc);
1439
rc= mysql_stmt_execute(stmt);
1440
check_execute(stmt, rc);
1443
mysql_stmt_close(stmt);
1446
rc= mysql_commit(mysql);
1450
/* Test simple prepares of all DML statements */
1452
static void test_prepare_simple()
1456
char query[MAX_TEST_QUERY_LENGTH];
1458
myheader("test_prepare_simple");
1460
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_simple");
1463
rc= mysql_query(mysql, "CREATE TABLE test_prepare_simple("
1464
"id int, name varchar(50))");
1468
strmov(query, "INSERT INTO test_prepare_simple VALUES(?, ?)");
1469
stmt= mysql_simple_prepare(mysql, query);
1472
verify_param_count(stmt, 2);
1473
mysql_stmt_close(stmt);
1476
strmov(query, "UPDATE test_prepare_simple SET id=? "
1477
"WHERE id=? AND CONVERT(name USING utf8)= ?");
1478
stmt= mysql_simple_prepare(mysql, query);
1481
verify_param_count(stmt, 3);
1482
mysql_stmt_close(stmt);
1485
strmov(query, "DELETE FROM test_prepare_simple WHERE id=10");
1486
stmt= mysql_simple_prepare(mysql, query);
1489
verify_param_count(stmt, 0);
1491
rc= mysql_stmt_execute(stmt);
1492
check_execute(stmt, rc);
1493
mysql_stmt_close(stmt);
1496
strmov(query, "DELETE FROM test_prepare_simple WHERE id=?");
1497
stmt= mysql_simple_prepare(mysql, query);
1500
verify_param_count(stmt, 1);
1502
mysql_stmt_close(stmt);
1505
strmov(query, "SELECT * FROM test_prepare_simple WHERE id=? "
1506
"AND CONVERT(name USING utf8)= ?");
1507
stmt= mysql_simple_prepare(mysql, query);
1510
verify_param_count(stmt, 2);
1512
mysql_stmt_close(stmt);
1514
/* now fetch the results ..*/
1515
rc= mysql_commit(mysql);
1520
/* Test simple prepare field results */
1522
static void test_prepare_field_result()
1527
char query[MAX_TEST_QUERY_LENGTH];
1529
myheader("test_prepare_field_result");
1531
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_field_result");
1534
rc= mysql_query(mysql, "CREATE TABLE test_prepare_field_result(int_c int, "
1535
"var_c varchar(50), ts_c timestamp(14), "
1536
"char_c char(4), date_c date, extra tinyint)");
1540
strmov(query, "SELECT int_c, var_c, date_c as date, ts_c, char_c FROM "
1541
" test_prepare_field_result as t1 WHERE int_c=?");
1542
stmt= mysql_simple_prepare(mysql, query);
1545
verify_param_count(stmt, 1);
1547
result= mysql_stmt_result_metadata(stmt);
1550
my_print_result_metadata(result);
1553
fprintf(stdout, "\n\n field attributes:\n");
1554
verify_prepare_field(result, 0, "int_c", "int_c", MYSQL_TYPE_LONG,
1555
"t1", "test_prepare_field_result", current_db, 11, 0);
1556
verify_prepare_field(result, 1, "var_c", "var_c", MYSQL_TYPE_VAR_STRING,
1557
"t1", "test_prepare_field_result", current_db, 50, 0);
1558
verify_prepare_field(result, 2, "date", "date_c", MYSQL_TYPE_DATE,
1559
"t1", "test_prepare_field_result", current_db, 10, 0);
1560
verify_prepare_field(result, 3, "ts_c", "ts_c", MYSQL_TYPE_TIMESTAMP,
1561
"t1", "test_prepare_field_result", current_db, 19, 0);
1562
verify_prepare_field(result, 4, "char_c", "char_c",
1563
(mysql_get_server_version(mysql) <= 50000 ?
1564
MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING),
1565
"t1", "test_prepare_field_result", current_db, 4, 0);
1567
verify_field_count(result, 5);
1568
mysql_free_result(result);
1569
mysql_stmt_close(stmt);
1573
/* Test simple prepare field results */
1575
static void test_prepare_syntax()
1579
char query[MAX_TEST_QUERY_LENGTH];
1581
myheader("test_prepare_syntax");
1583
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_syntax");
1586
rc= mysql_query(mysql, "CREATE TABLE test_prepare_syntax("
1587
"id int, name varchar(50), extra int)");
1590
strmov(query, "INSERT INTO test_prepare_syntax VALUES(?");
1591
stmt= mysql_simple_prepare(mysql, query);
1594
strmov(query, "SELECT id, name FROM test_prepare_syntax WHERE id=? AND WHERE");
1595
stmt= mysql_simple_prepare(mysql, query);
1598
/* now fetch the results ..*/
1599
rc= mysql_commit(mysql);
1604
/* Test a simple prepare */
1606
static void test_prepare()
1610
int int_data, o_int_data;
1611
char str_data[50], data[50];
1612
char tiny_data, o_tiny_data;
1613
short small_data, o_small_data;
1614
longlong big_data, o_big_data;
1615
float real_data, o_real_data;
1616
double double_data, o_double_data;
1617
ulong length[7], len;
1620
MYSQL_BIND my_bind[7];
1621
char query[MAX_TEST_QUERY_LENGTH];
1623
myheader("test_prepare");
1625
rc= mysql_autocommit(mysql, TRUE);
1628
rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
1631
rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 tinyint, "
1632
"col2 varchar(15), col3 int, "
1633
"col4 smallint, col5 bigint, "
1634
"col6 float, col7 double )");
1637
/* insert by prepare */
1638
strxmov(query, "INSERT INTO my_prepare VALUES(?, ?, ?, ?, ?, ?, ?)", NullS);
1639
stmt= mysql_simple_prepare(mysql, query);
1642
verify_param_count(stmt, 7);
1644
bzero((char*) my_bind, sizeof(my_bind));
1647
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
1648
my_bind[0].buffer= (void *)&tiny_data;
1650
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
1651
my_bind[1].buffer= (void *)str_data;
1652
my_bind[1].buffer_length= 1000; /* Max string length */
1654
my_bind[2].buffer_type= MYSQL_TYPE_LONG;
1655
my_bind[2].buffer= (void *)&int_data;
1657
my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
1658
my_bind[3].buffer= (void *)&small_data;
1660
my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
1661
my_bind[4].buffer= (void *)&big_data;
1663
my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
1664
my_bind[5].buffer= (void *)&real_data;
1666
my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
1667
my_bind[6].buffer= (void *)&double_data;
1669
for (i= 0; i < (int) array_elements(my_bind); i++)
1671
my_bind[i].length= &length[i];
1672
my_bind[i].is_null= &is_null[i];
1676
rc= mysql_stmt_bind_param(stmt, my_bind);
1677
check_execute(stmt, rc);
1683
double_data= 6578.001;
1685
/* now, execute the prepared statement to insert 10 records.. */
1686
for (tiny_data= 0; tiny_data < 100; tiny_data++)
1688
length[1]= my_sprintf(str_data, (str_data, "MySQL%d", int_data));
1689
rc= mysql_stmt_execute(stmt);
1690
check_execute(stmt, rc);
1695
double_data += 10.09;
1698
mysql_stmt_close(stmt);
1700
/* now fetch the results ..*/
1701
rc= mysql_commit(mysql);
1704
/* test the results now, only one row should exist */
1705
rc= my_stmt_result("SELECT * FROM my_prepare");
1706
DIE_UNLESS(tiny_data == (char) rc);
1708
stmt= mysql_simple_prepare(mysql, "SELECT * FROM my_prepare");
1711
rc= mysql_stmt_bind_result(stmt, my_bind);
1712
check_execute(stmt, rc);
1714
/* get the result */
1715
rc= mysql_stmt_execute(stmt);
1716
check_execute(stmt, rc);
1722
o_double_data= 6578.001;
1724
/* now, execute the prepared statement to insert 10 records.. */
1725
for (o_tiny_data= 0; o_tiny_data < 100; o_tiny_data++)
1727
len= my_sprintf(data, (data, "MySQL%d", o_int_data));
1729
rc= mysql_stmt_fetch(stmt);
1730
check_execute(stmt, rc);
1734
fprintf(stdout, "\n");
1735
fprintf(stdout, "\n\t tiny : %d (%lu)", tiny_data, length[0]);
1736
fprintf(stdout, "\n\t short : %d (%lu)", small_data, length[3]);
1737
fprintf(stdout, "\n\t int : %d (%lu)", int_data, length[2]);
1738
fprintf(stdout, "\n\t big : %s (%lu)", llstr(big_data, llbuf),
1741
fprintf(stdout, "\n\t float : %f (%lu)", real_data, length[5]);
1742
fprintf(stdout, "\n\t double : %f (%lu)", double_data, length[6]);
1744
fprintf(stdout, "\n\t str : %s (%lu)", str_data, length[1]);
1747
DIE_UNLESS(tiny_data == o_tiny_data);
1748
DIE_UNLESS(is_null[0] == 0);
1749
DIE_UNLESS(length[0] == 1);
1751
DIE_UNLESS(int_data == o_int_data);
1752
DIE_UNLESS(length[2] == 4);
1754
DIE_UNLESS(small_data == o_small_data);
1755
DIE_UNLESS(length[3] == 2);
1757
DIE_UNLESS(big_data == o_big_data);
1758
DIE_UNLESS(length[4] == 8);
1760
DIE_UNLESS(real_data == o_real_data);
1761
DIE_UNLESS(length[5] == 4);
1763
DIE_UNLESS(cmp_double(&double_data, &o_double_data));
1764
DIE_UNLESS(length[6] == 8);
1766
DIE_UNLESS(strcmp(data, str_data) == 0);
1767
DIE_UNLESS(length[1] == len);
1773
o_double_data += 10.09;
1776
rc= mysql_stmt_fetch(stmt);
1777
DIE_UNLESS(rc == MYSQL_NO_DATA);
1779
mysql_stmt_close(stmt);
1784
/* Test double comparision */
1786
static void test_double_compare()
1790
char real_data[10], tiny_data;
1793
MYSQL_BIND my_bind[3];
1795
char query[MAX_TEST_QUERY_LENGTH];
1797
myheader("test_double_compare");
1799
rc= mysql_autocommit(mysql, TRUE);
1802
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_double_compare");
1805
rc= mysql_query(mysql, "CREATE TABLE test_double_compare(col1 tinyint, "
1806
" col2 float, col3 double )");
1809
rc= mysql_query(mysql, "INSERT INTO test_double_compare "
1810
"VALUES (1, 10.2, 34.5)");
1813
strmov(query, "UPDATE test_double_compare SET col1=100 "
1814
"WHERE col1 = ? AND col2 = ? AND COL3 = ?");
1815
stmt= mysql_simple_prepare(mysql, query);
1818
verify_param_count(stmt, 3);
1820
/* Always bzero bind array because there can be internal members */
1821
bzero((char*) my_bind, sizeof(my_bind));
1824
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
1825
my_bind[0].buffer= (void *)&tiny_data;
1828
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
1829
my_bind[1].buffer= (void *)&real_data;
1830
my_bind[1].buffer_length= sizeof(real_data);
1831
my_bind[1].length= &length[1];
1835
my_bind[2].buffer_type= MYSQL_TYPE_DOUBLE;
1836
my_bind[2].buffer= (void *)&double_data;
1839
strmov(real_data, "10.2");
1841
rc= mysql_stmt_bind_param(stmt, my_bind);
1842
check_execute(stmt, rc);
1844
rc= mysql_stmt_execute(stmt);
1845
check_execute(stmt, rc);
1847
verify_affected_rows(0);
1849
mysql_stmt_close(stmt);
1851
/* now fetch the results ..*/
1852
rc= mysql_commit(mysql);
1855
/* test the results now, only one row should exist */
1856
rc= mysql_query(mysql, "SELECT * FROM test_double_compare");
1859
/* get the result */
1860
result= mysql_store_result(mysql);
1863
rc= my_process_result_set(result);
1864
DIE_UNLESS((int)tiny_data == rc);
1865
mysql_free_result(result);
1869
/* Test simple null */
1871
static void test_null()
1876
MYSQL_BIND my_bind[2];
1878
char query[MAX_TEST_QUERY_LENGTH];
1880
myheader("test_null");
1882
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_null");
1885
rc= mysql_query(mysql, "CREATE TABLE test_null(col1 int, col2 varchar(50))");
1888
/* insert by prepare, wrong column name */
1889
strmov(query, "INSERT INTO test_null(col3, col2) VALUES(?, ?)");
1890
stmt= mysql_simple_prepare(mysql, query);
1893
strmov(query, "INSERT INTO test_null(col1, col2) VALUES(?, ?)");
1894
stmt= mysql_simple_prepare(mysql, query);
1897
verify_param_count(stmt, 2);
1899
/* Always bzero all members of bind parameter */
1900
bzero((char*) my_bind, sizeof(my_bind));
1902
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1903
my_bind[0].is_null= &is_null[0];
1905
my_bind[1]= my_bind[0];
1907
rc= mysql_stmt_bind_param(stmt, my_bind);
1908
check_execute(stmt, rc);
1910
/* now, execute the prepared statement to insert 10 records.. */
1911
for (nData= 0; nData<10; nData++)
1913
rc= mysql_stmt_execute(stmt);
1914
check_execute(stmt, rc);
1917
/* Re-bind with MYSQL_TYPE_NULL */
1918
my_bind[0].buffer_type= MYSQL_TYPE_NULL;
1919
is_null[0]= 0; /* reset */
1920
my_bind[1]= my_bind[0];
1922
rc= mysql_stmt_bind_param(stmt, my_bind);
1923
check_execute(stmt, rc);
1925
for (nData= 0; nData<10; nData++)
1927
rc= mysql_stmt_execute(stmt);
1928
check_execute(stmt, rc);
1931
mysql_stmt_close(stmt);
1933
/* now fetch the results ..*/
1934
rc= mysql_commit(mysql);
1938
rc= my_stmt_result("SELECT * FROM test_null");;
1939
DIE_UNLESS((int) nData == rc);
1942
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1943
my_bind[0].buffer= (void *)&nData; /* this buffer won't be altered */
1944
my_bind[0].length= 0;
1945
my_bind[1]= my_bind[0];
1946
my_bind[0].is_null= &is_null[0];
1947
my_bind[1].is_null= &is_null[1];
1949
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_null");
1952
rc= mysql_stmt_execute(stmt);
1953
check_execute(stmt, rc);
1955
rc= mysql_stmt_bind_result(stmt, my_bind);
1956
check_execute(stmt, rc);
1959
is_null[0]= is_null[1]= 0;
1960
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
1962
DIE_UNLESS(is_null[0]);
1963
DIE_UNLESS(is_null[1]);
1965
is_null[0]= is_null[1]= 0;
1967
DIE_UNLESS(rc == (int) nData);
1968
mysql_stmt_close(stmt);
1972
/* Test for NULL as PS parameter (BUG#3367, BUG#3371) */
1974
static void test_ps_null_param()
1983
MYSQL_BIND out_bind;
1985
my_bool out_is_null;
1986
char out_str_data[20];
1988
const char *queries[]= {"select ?", "select ?+1",
1989
"select col1 from test_ps_nulls where col1 <=> ?",
1992
const char **cur_query= queries;
1994
myheader("test_null_ps_param_in_result");
1996
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ps_nulls");
1999
rc= mysql_query(mysql, "CREATE TABLE test_ps_nulls(col1 int)");
2002
rc= mysql_query(mysql, "INSERT INTO test_ps_nulls values (1), (null)");
2005
/* Always bzero all members of bind parameter */
2006
bzero((char*) &in_bind, sizeof(in_bind));
2007
bzero((char*) &out_bind, sizeof(out_bind));
2009
in_bind.buffer_type= MYSQL_TYPE_LONG;
2010
in_bind.is_null= &in_is_null;
2012
in_bind.buffer= (void *)&in_long;
2016
out_bind.buffer_type= MYSQL_TYPE_STRING;
2017
out_bind.is_null= &out_is_null;
2018
out_bind.length= &out_length;
2019
out_bind.buffer= out_str_data;
2020
out_bind.buffer_length= array_elements(out_str_data);
2022
/* Execute several queries, all returning NULL in result. */
2023
for(cur_query= queries; *cur_query; cur_query++)
2025
char query[MAX_TEST_QUERY_LENGTH];
2026
strmov(query, *cur_query);
2027
stmt= mysql_simple_prepare(mysql, query);
2029
verify_param_count(stmt, 1);
2031
rc= mysql_stmt_bind_param(stmt, &in_bind);
2032
check_execute(stmt, rc);
2033
rc= mysql_stmt_bind_result(stmt, &out_bind);
2034
check_execute(stmt, rc);
2035
rc= mysql_stmt_execute(stmt);
2036
check_execute(stmt, rc);
2037
rc= mysql_stmt_fetch(stmt);
2038
DIE_UNLESS(rc != MYSQL_NO_DATA);
2039
DIE_UNLESS(out_is_null);
2040
rc= mysql_stmt_fetch(stmt);
2041
DIE_UNLESS(rc == MYSQL_NO_DATA);
2042
mysql_stmt_close(stmt);
2047
/* Test fetch null */
2049
static void test_fetch_null()
2054
MYSQL_BIND my_bind[11];
2056
my_bool is_null[11];
2057
char query[MAX_TEST_QUERY_LENGTH];
2059
myheader("test_fetch_null");
2061
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_fetch_null");
2064
rc= mysql_query(mysql, "CREATE TABLE test_fetch_null("
2065
" col1 tinyint, col2 smallint, "
2066
" col3 int, col4 bigint, "
2067
" col5 float, col6 double, "
2068
" col7 date, col8 time, "
2069
" col9 varbinary(10), "
2070
" col10 varchar(50), "
2071
" col11 char(20))");
2074
rc= mysql_query(mysql, "INSERT INTO test_fetch_null (col11) "
2075
"VALUES (1000), (88), (389789)");
2078
rc= mysql_commit(mysql);
2082
bzero((char*) my_bind, sizeof(my_bind));
2083
for (i= 0; i < (int) array_elements(my_bind); i++)
2085
my_bind[i].buffer_type= MYSQL_TYPE_LONG;
2086
my_bind[i].is_null= &is_null[i];
2087
my_bind[i].length= &length[i];
2089
my_bind[i-1].buffer= (void *)&nData; /* Last column is not null */
2091
strmov((char *)query , "SELECT * FROM test_fetch_null");
2093
rc= my_stmt_result(query);
2094
DIE_UNLESS(rc == 3);
2096
stmt= mysql_simple_prepare(mysql, query);
2099
rc= mysql_stmt_bind_result(stmt, my_bind);
2100
check_execute(stmt, rc);
2102
rc= mysql_stmt_execute(stmt);
2103
check_execute(stmt, rc);
2106
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
2109
for (i= 0; i < 10; i++)
2112
fprintf(stdout, "\n data[%d] : %s", i,
2113
is_null[i] ? "NULL" : "NOT NULL");
2114
DIE_UNLESS(is_null[i]);
2117
fprintf(stdout, "\n data[%d]: %d", i, nData);
2118
DIE_UNLESS(nData == 1000 || nData == 88 || nData == 389789);
2119
DIE_UNLESS(is_null[i] == 0);
2120
DIE_UNLESS(length[i] == 4);
2122
DIE_UNLESS(rc == 3);
2123
mysql_stmt_close(stmt);
2127
/* Test simple select */
2129
static void test_select_version()
2134
myheader("test_select_version");
2136
stmt= mysql_simple_prepare(mysql, "SELECT @@version");
2139
verify_param_count(stmt, 0);
2141
rc= mysql_stmt_execute(stmt);
2142
check_execute(stmt, rc);
2144
my_process_stmt_result(stmt);
2145
mysql_stmt_close(stmt);
2149
/* Test simple show */
2151
static void test_select_show_table()
2156
myheader("test_select_show_table");
2158
stmt= mysql_simple_prepare(mysql, "SHOW TABLES FROM mysql");
2161
verify_param_count(stmt, 0);
2163
for (i= 1; i < 3; i++)
2165
rc= mysql_stmt_execute(stmt);
2166
check_execute(stmt, rc);
2169
my_process_stmt_result(stmt);
2170
mysql_stmt_close(stmt);
2174
/* Test simple select to debug */
2176
static void test_select_direct()
2181
myheader("test_select_direct");
2183
rc= mysql_autocommit(mysql, TRUE);
2186
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
2189
rc= mysql_query(mysql, "CREATE TABLE test_select(id int, id1 tinyint, "
2192
" name varchar(50))");
2195
/* insert a row and commit the transaction */
2196
rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 5, 2.3, 4.5, 'venu')");
2199
rc= mysql_commit(mysql);
2202
rc= mysql_query(mysql, "SELECT * FROM test_select");
2205
/* get the result */
2206
result= mysql_store_result(mysql);
2209
(void) my_process_result_set(result);
2210
mysql_free_result(result);
2214
/* Test simple select with prepare */
2216
static void test_select_prepare()
2221
myheader("test_select_prepare");
2223
rc= mysql_autocommit(mysql, TRUE);
2226
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
2229
rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
2232
/* insert a row and commit the transaction */
2233
rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
2236
rc= mysql_commit(mysql);
2239
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
2242
rc= mysql_stmt_execute(stmt);
2243
check_execute(stmt, rc);
2245
rc= my_process_stmt_result(stmt);
2246
DIE_UNLESS(rc == 1);
2247
mysql_stmt_close(stmt);
2249
rc= mysql_query(mysql, "DROP TABLE test_select");
2252
rc= mysql_query(mysql, "CREATE TABLE test_select(id tinyint, id1 int, "
2253
" id2 float, id3 float, "
2254
" name varchar(50))");
2257
/* insert a row and commit the transaction */
2258
rc= mysql_query(mysql, "INSERT INTO test_select(id, id1, id2, name) VALUES(10, 5, 2.3, 'venu')");
2261
rc= mysql_commit(mysql);
2264
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
2267
rc= mysql_stmt_execute(stmt);
2268
check_execute(stmt, rc);
2270
rc= my_process_stmt_result(stmt);
2271
DIE_UNLESS(rc == 1);
2272
mysql_stmt_close(stmt);
2276
/* Test simple select */
2278
static void test_select()
2284
MYSQL_BIND my_bind[2];
2286
char query[MAX_TEST_QUERY_LENGTH];
2288
myheader("test_select");
2290
rc= mysql_autocommit(mysql, TRUE);
2293
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
2296
rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
2299
/* insert a row and commit the transaction */
2300
rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
2303
/* now insert the second row, and roll back the transaction */
2304
rc= mysql_query(mysql, "INSERT INTO test_select VALUES(20, 'mysql')");
2307
rc= mysql_commit(mysql);
2310
strmov(query, "SELECT * FROM test_select WHERE id= ? "
2311
"AND CONVERT(name USING utf8) =?");
2312
stmt= mysql_simple_prepare(mysql, query);
2315
verify_param_count(stmt, 2);
2317
/* Always bzero all members of bind parameter */
2318
bzero((char*) my_bind, sizeof(my_bind));
2322
strmov(szData, (char *)"venu");
2323
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
2324
my_bind[1].buffer= (void *)szData;
2325
my_bind[1].buffer_length= 4;
2326
my_bind[1].length= &length[1];
2329
my_bind[0].buffer= (void *)&nData;
2330
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2332
rc= mysql_stmt_bind_param(stmt, my_bind);
2333
check_execute(stmt, rc);
2335
rc= mysql_stmt_execute(stmt);
2336
check_execute(stmt, rc);
2338
rc= my_process_stmt_result(stmt);
2339
DIE_UNLESS(rc == 1);
2341
mysql_stmt_close(stmt);
2346
Test for BUG#3420 ("select id1, value1 from t where id= ? or value= ?"
2347
returns all rows in the table)
2350
static void test_ps_conj_select()
2354
MYSQL_BIND my_bind[2];
2357
unsigned long str_length;
2358
char query[MAX_TEST_QUERY_LENGTH];
2359
myheader("test_ps_conj_select");
2361
rc= mysql_query(mysql, "drop table if exists t1");
2364
rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
2365
"value2 varchar(100), value1 varchar(100))");
2368
rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
2369
"(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
2372
strmov(query, "select id1, value1 from t1 where id1= ? or "
2373
"CONVERT(value1 USING utf8)= ?");
2374
stmt= mysql_simple_prepare(mysql, query);
2377
verify_param_count(stmt, 2);
2379
/* Always bzero all members of bind parameter */
2380
bzero((char*) my_bind, sizeof(my_bind));
2382
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2383
my_bind[0].buffer= (void *)&int_data;
2385
my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
2386
my_bind[1].buffer= (void *)str_data;
2387
my_bind[1].buffer_length= array_elements(str_data);
2388
my_bind[1].length= &str_length;
2390
rc= mysql_stmt_bind_param(stmt, my_bind);
2391
check_execute(stmt, rc);
2394
strmov(str_data, "hh");
2395
str_length= strlen(str_data);
2397
rc= mysql_stmt_execute(stmt);
2398
check_execute(stmt, rc);
2400
rc= my_process_stmt_result(stmt);
2401
DIE_UNLESS(rc == 3);
2403
mysql_stmt_close(stmt);
2407
/* reads Qcache_hits from server and returns its value */
2408
static uint query_cache_hits(MYSQL *conn)
2415
rc= mysql_query(conn, "show status like 'qcache_hits'");
2417
res= mysql_use_result(conn);
2420
row= mysql_fetch_row(res);
2423
result= atoi(row[1]);
2424
mysql_free_result(res);
2430
utility for the next test; expects 3 rows in the result from a SELECT,
2431
compares each row/field with an expected value.
2433
#define test_ps_query_cache_result(i1,s1,l1,i2,s2,l2,i3,s3,l3) \
2434
r_metadata= mysql_stmt_result_metadata(stmt); \
2435
DIE_UNLESS(r_metadata != NULL); \
2436
rc= mysql_stmt_fetch(stmt); \
2437
check_execute(stmt, rc); \
2439
fprintf(stdout, "\n row 1: %d, %s(%lu)", r_int_data, \
2440
r_str_data, r_str_length); \
2441
DIE_UNLESS((r_int_data == i1) && (r_str_length == l1) && \
2442
(strcmp(r_str_data, s1) == 0)); \
2443
rc= mysql_stmt_fetch(stmt); \
2444
check_execute(stmt, rc); \
2446
fprintf(stdout, "\n row 2: %d, %s(%lu)", r_int_data, \
2447
r_str_data, r_str_length); \
2448
DIE_UNLESS((r_int_data == i2) && (r_str_length == l2) && \
2449
(strcmp(r_str_data, s2) == 0)); \
2450
rc= mysql_stmt_fetch(stmt); \
2451
check_execute(stmt, rc); \
2453
fprintf(stdout, "\n row 3: %d, %s(%lu)", r_int_data, \
2454
r_str_data, r_str_length); \
2455
DIE_UNLESS((r_int_data == i3) && (r_str_length == l3) && \
2456
(strcmp(r_str_data, s3) == 0)); \
2457
rc= mysql_stmt_fetch(stmt); \
2458
DIE_UNLESS(rc == MYSQL_NO_DATA); \
2459
mysql_free_result(r_metadata);
2463
Test that prepared statements make use of the query cache just as normal
2464
statements (BUG#735).
2466
static void test_ps_query_cache()
2468
MYSQL *lmysql= mysql;
2471
MYSQL_BIND p_bind[2],r_bind[2]; /* p: param bind; r: result bind */
2472
int32 p_int_data, r_int_data;
2473
char p_str_data[32], r_str_data[32];
2474
unsigned long p_str_length, r_str_length;
2475
MYSQL_RES *r_metadata;
2476
char query[MAX_TEST_QUERY_LENGTH];
2478
enum enum_test_ps_query_cache
2481
We iterate the same prepare/executes block, but have iterations where
2482
we vary the query cache conditions.
2484
/* the query cache is enabled for the duration of prep&execs: */
2487
same but using a new connection (to see if qcache serves results from
2488
the previous connection as it should):
2490
TEST_QCACHE_ON_WITH_OTHER_CONN,
2492
First border case: disables the query cache before prepare and
2493
re-enables it before execution (to test if we have no bug then):
2497
Second border case: enables the query cache before prepare and
2498
disables it before execution:
2502
enum enum_test_ps_query_cache iteration;
2504
myheader("test_ps_query_cache");
2506
rc= mysql_query(mysql, "SET SQL_MODE=''");
2509
/* prepare the table */
2511
rc= mysql_query(mysql, "drop table if exists t1");
2514
rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
2515
"value2 varchar(100), value1 varchar(100))");
2518
rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
2519
"(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
2522
for (iteration= TEST_QCACHE_ON; iteration <= TEST_QCACHE_ON_OFF; iteration++)
2525
switch (iteration) {
2526
case TEST_QCACHE_ON:
2527
case TEST_QCACHE_ON_OFF:
2528
rc= mysql_query(lmysql, "set global query_cache_size=1000000");
2531
case TEST_QCACHE_OFF_ON:
2532
rc= mysql_query(lmysql, "set global query_cache_size=0");
2535
case TEST_QCACHE_ON_WITH_OTHER_CONN:
2537
fprintf(stdout, "\n Establishing a test connection ...");
2538
if (!(lmysql= mysql_client_init(NULL)))
2540
printf("mysql_client_init() failed");
2543
if (!(mysql_real_connect(lmysql, opt_host, opt_user,
2544
opt_password, current_db, opt_port,
2545
opt_unix_socket, 0)))
2547
printf("connection failed");
2548
mysql_close(lmysql);
2551
rc= mysql_query(lmysql, "SET SQL_MODE=''");
2555
fprintf(stdout, "OK");
2558
strmov(query, "select id1, value1 from t1 where id1= ? or "
2559
"CONVERT(value1 USING utf8)= ?");
2560
stmt= mysql_simple_prepare(lmysql, query);
2563
verify_param_count(stmt, 2);
2565
switch (iteration) {
2566
case TEST_QCACHE_OFF_ON:
2567
rc= mysql_query(lmysql, "set global query_cache_size=1000000");
2570
case TEST_QCACHE_ON_OFF:
2571
rc= mysql_query(lmysql, "set global query_cache_size=0");
2577
bzero((char*) p_bind, sizeof(p_bind));
2578
p_bind[0].buffer_type= MYSQL_TYPE_LONG;
2579
p_bind[0].buffer= (void *)&p_int_data;
2580
p_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
2581
p_bind[1].buffer= (void *)p_str_data;
2582
p_bind[1].buffer_length= array_elements(p_str_data);
2583
p_bind[1].length= &p_str_length;
2585
rc= mysql_stmt_bind_param(stmt, p_bind);
2586
check_execute(stmt, rc);
2589
strmov(p_str_data, "hh");
2590
p_str_length= strlen(p_str_data);
2592
bzero((char*) r_bind, sizeof(r_bind));
2593
r_bind[0].buffer_type= MYSQL_TYPE_LONG;
2594
r_bind[0].buffer= (void *)&r_int_data;
2595
r_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
2596
r_bind[1].buffer= (void *)r_str_data;
2597
r_bind[1].buffer_length= array_elements(r_str_data);
2598
r_bind[1].length= &r_str_length;
2600
rc= mysql_stmt_bind_result(stmt, r_bind);
2601
check_execute(stmt, rc);
2603
rc= mysql_stmt_execute(stmt);
2604
check_execute(stmt, rc);
2606
test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
2608
/* now retry with the same parameter values and see qcache hits */
2609
hits1= query_cache_hits(lmysql);
2610
rc= mysql_stmt_execute(stmt);
2611
check_execute(stmt, rc);
2612
test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
2613
hits2= query_cache_hits(lmysql);
2615
case TEST_QCACHE_ON_WITH_OTHER_CONN:
2616
case TEST_QCACHE_ON: /* should have hit */
2617
DIE_UNLESS(hits2-hits1 == 1);
2619
case TEST_QCACHE_OFF_ON:
2620
case TEST_QCACHE_ON_OFF: /* should not have hit */
2621
DIE_UNLESS(hits2-hits1 == 0);
2625
/* now modify parameter values and see qcache hits */
2626
strmov(p_str_data, "ii");
2627
p_str_length= strlen(p_str_data);
2628
rc= mysql_stmt_execute(stmt);
2629
check_execute(stmt, rc);
2630
test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
2631
hits1= query_cache_hits(lmysql);
2634
case TEST_QCACHE_ON:
2635
case TEST_QCACHE_OFF_ON:
2636
case TEST_QCACHE_ON_OFF: /* should not have hit */
2637
DIE_UNLESS(hits2-hits1 == 0);
2639
case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
2640
DIE_UNLESS(hits1-hits2 == 1);
2644
rc= mysql_stmt_execute(stmt);
2645
check_execute(stmt, rc);
2647
test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
2648
hits2= query_cache_hits(lmysql);
2650
mysql_stmt_close(stmt);
2653
case TEST_QCACHE_ON: /* should have hit */
2654
DIE_UNLESS(hits2-hits1 == 1);
2656
case TEST_QCACHE_OFF_ON:
2657
case TEST_QCACHE_ON_OFF: /* should not have hit */
2658
DIE_UNLESS(hits2-hits1 == 0);
2660
case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
2661
DIE_UNLESS(hits2-hits1 == 1);
2665
} /* for(iteration=...) */
2667
if (lmysql != mysql)
2668
mysql_close(lmysql);
2670
rc= mysql_query(mysql, "set global query_cache_size=0");
2675
/* Test BUG#1115 (incorrect string parameter value allocation) */
2677
static void test_bug1115()
2681
MYSQL_BIND my_bind[1];
2684
char query[MAX_TEST_QUERY_LENGTH];
2686
myheader("test_bug1115");
2688
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
2691
rc= mysql_query(mysql, "CREATE TABLE test_select(\
2692
session_id char(9) NOT NULL, \
2693
a int(8) unsigned NOT NULL, \
2694
b int(5) NOT NULL, \
2695
c int(5) NOT NULL, \
2696
d datetime NOT NULL)");
2698
rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
2699
"(\"abc\", 1, 2, 3, 2003-08-30), "
2700
"(\"abd\", 1, 2, 3, 2003-08-30), "
2701
"(\"abf\", 1, 2, 3, 2003-08-30), "
2702
"(\"abg\", 1, 2, 3, 2003-08-30), "
2703
"(\"abh\", 1, 2, 3, 2003-08-30), "
2704
"(\"abj\", 1, 2, 3, 2003-08-30), "
2705
"(\"abk\", 1, 2, 3, 2003-08-30), "
2706
"(\"abl\", 1, 2, 3, 2003-08-30), "
2707
"(\"abq\", 1, 2, 3, 2003-08-30) ");
2709
rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
2710
"(\"abw\", 1, 2, 3, 2003-08-30), "
2711
"(\"abe\", 1, 2, 3, 2003-08-30), "
2712
"(\"abr\", 1, 2, 3, 2003-08-30), "
2713
"(\"abt\", 1, 2, 3, 2003-08-30), "
2714
"(\"aby\", 1, 2, 3, 2003-08-30), "
2715
"(\"abu\", 1, 2, 3, 2003-08-30), "
2716
"(\"abi\", 1, 2, 3, 2003-08-30), "
2717
"(\"abo\", 1, 2, 3, 2003-08-30), "
2718
"(\"abp\", 1, 2, 3, 2003-08-30), "
2719
"(\"abz\", 1, 2, 3, 2003-08-30), "
2720
"(\"abx\", 1, 2, 3, 2003-08-30)");
2723
strmov(query, "SELECT * FROM test_select WHERE "
2724
"CONVERT(session_id USING utf8)= ?");
2725
stmt= mysql_simple_prepare(mysql, query);
2728
verify_param_count(stmt, 1);
2730
/* Always bzero all members of bind parameter */
2731
bzero((char*) my_bind, sizeof(my_bind));
2733
strmov(szData, (char *)"abc");
2734
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2735
my_bind[0].buffer= (void *)szData;
2736
my_bind[0].buffer_length= 10;
2737
my_bind[0].length= &length[0];
2740
rc= mysql_stmt_bind_param(stmt, my_bind);
2741
check_execute(stmt, rc);
2743
rc= mysql_stmt_execute(stmt);
2744
check_execute(stmt, rc);
2746
rc= my_process_stmt_result(stmt);
2747
DIE_UNLESS(rc == 1);
2749
strmov(szData, (char *)"venu");
2750
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2751
my_bind[0].buffer= (void *)szData;
2752
my_bind[0].buffer_length= 10;
2753
my_bind[0].length= &length[0];
2755
my_bind[0].is_null= 0;
2757
rc= mysql_stmt_bind_param(stmt, my_bind);
2758
check_execute(stmt, rc);
2760
rc= mysql_stmt_execute(stmt);
2761
check_execute(stmt, rc);
2763
rc= my_process_stmt_result(stmt);
2764
DIE_UNLESS(rc == 0);
2766
strmov(szData, (char *)"abc");
2767
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2768
my_bind[0].buffer= (void *)szData;
2769
my_bind[0].buffer_length= 10;
2770
my_bind[0].length= &length[0];
2772
my_bind[0].is_null= 0;
2774
rc= mysql_stmt_bind_param(stmt, my_bind);
2775
check_execute(stmt, rc);
2777
rc= mysql_stmt_execute(stmt);
2778
check_execute(stmt, rc);
2780
rc= my_process_stmt_result(stmt);
2781
DIE_UNLESS(rc == 1);
2783
mysql_stmt_close(stmt);
2787
/* Test BUG#1180 (optimized away part of WHERE clause) */
2789
static void test_bug1180()
2793
MYSQL_BIND my_bind[1];
2796
char query[MAX_TEST_QUERY_LENGTH];
2798
myheader("test_select_bug");
2800
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
2803
rc= mysql_query(mysql, "CREATE TABLE test_select(session_id char(9) NOT NULL)");
2805
rc= mysql_query(mysql, "INSERT INTO test_select VALUES (\"abc\")");
2808
strmov(query, "SELECT * FROM test_select WHERE ?= \"1111\" and "
2809
"session_id= \"abc\"");
2810
stmt= mysql_simple_prepare(mysql, query);
2813
verify_param_count(stmt, 1);
2815
/* Always bzero all members of bind parameter */
2816
bzero((char*) my_bind, sizeof(my_bind));
2818
strmov(szData, (char *)"abc");
2819
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2820
my_bind[0].buffer= (void *)szData;
2821
my_bind[0].buffer_length= 10;
2822
my_bind[0].length= &length[0];
2824
my_bind[0].is_null= 0;
2826
rc= mysql_stmt_bind_param(stmt, my_bind);
2827
check_execute(stmt, rc);
2829
rc= mysql_stmt_execute(stmt);
2830
check_execute(stmt, rc);
2832
rc= my_process_stmt_result(stmt);
2833
DIE_UNLESS(rc == 0);
2835
strmov(szData, (char *)"1111");
2836
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2837
my_bind[0].buffer= (void *)szData;
2838
my_bind[0].buffer_length= 10;
2839
my_bind[0].length= &length[0];
2841
my_bind[0].is_null= 0;
2843
rc= mysql_stmt_bind_param(stmt, my_bind);
2844
check_execute(stmt, rc);
2846
rc= mysql_stmt_execute(stmt);
2847
check_execute(stmt, rc);
2849
rc= my_process_stmt_result(stmt);
2850
DIE_UNLESS(rc == 1);
2852
strmov(szData, (char *)"abc");
2853
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2854
my_bind[0].buffer= (void *)szData;
2855
my_bind[0].buffer_length= 10;
2856
my_bind[0].length= &length[0];
2858
my_bind[0].is_null= 0;
2860
rc= mysql_stmt_bind_param(stmt, my_bind);
2861
check_execute(stmt, rc);
2863
rc= mysql_stmt_execute(stmt);
2864
check_execute(stmt, rc);
2866
rc= my_process_stmt_result(stmt);
2867
DIE_UNLESS(rc == 0);
2869
mysql_stmt_close(stmt);
2874
Test BUG#1644 (Insertion of more than 3 NULL columns with parameter
2878
static void test_bug1644()
2883
MYSQL_BIND my_bind[4];
2887
char query[MAX_TEST_QUERY_LENGTH];
2889
myheader("test_bug1644");
2891
rc= mysql_query(mysql, "DROP TABLE IF EXISTS foo_dfr");
2894
rc= mysql_query(mysql,
2895
"CREATE TABLE foo_dfr(col1 int, col2 int, col3 int, col4 int);");
2898
strmov(query, "INSERT INTO foo_dfr VALUES (?, ?, ?, ? )");
2899
stmt= mysql_simple_prepare(mysql, query);
2902
verify_param_count(stmt, 4);
2904
/* Always bzero all members of bind parameter */
2905
bzero((char*) my_bind, sizeof(my_bind));
2909
for (i= 0 ; i < 4 ; i++)
2911
my_bind[i].buffer_type= MYSQL_TYPE_LONG;
2912
my_bind[i].buffer= (void *)#
2913
my_bind[i].is_null= &isnull;
2916
rc= mysql_stmt_bind_param(stmt, my_bind);
2917
check_execute(stmt, rc);
2919
rc= mysql_stmt_execute(stmt);
2920
check_execute(stmt, rc);
2923
for (i= 0 ; i < 4 ; i++)
2924
my_bind[i].is_null= &isnull;
2926
rc= mysql_stmt_bind_param(stmt, my_bind);
2927
check_execute(stmt, rc);
2929
rc= mysql_stmt_execute(stmt);
2930
check_execute(stmt, rc);
2934
for (i= 0 ; i < 4 ; i++)
2935
my_bind[i].is_null= &isnull;
2937
rc= mysql_stmt_bind_param(stmt, my_bind);
2938
check_execute(stmt, rc);
2940
rc= mysql_stmt_execute(stmt);
2941
check_execute(stmt, rc);
2943
mysql_stmt_close(stmt);
2945
rc= mysql_query(mysql, "SELECT * FROM foo_dfr");
2948
result= mysql_store_result(mysql);
2951
rc= my_process_result_set(result);
2952
DIE_UNLESS(rc == 3);
2954
mysql_data_seek(result, 0);
2956
row= mysql_fetch_row(result);
2958
for (i= 0 ; i < 4 ; i++)
2960
DIE_UNLESS(strcmp(row[i], "22") == 0);
2962
row= mysql_fetch_row(result);
2964
for (i= 0 ; i < 4 ; i++)
2966
DIE_UNLESS(row[i] == 0);
2968
row= mysql_fetch_row(result);
2970
for (i= 0 ; i < 4 ; i++)
2972
DIE_UNLESS(strcmp(row[i], "88") == 0);
2974
row= mysql_fetch_row(result);
2977
mysql_free_result(result);
2981
/* Test simple select show */
2983
static void test_select_show()
2987
char query[MAX_TEST_QUERY_LENGTH];
2989
myheader("test_select_show");
2991
mysql_autocommit(mysql, TRUE);
2993
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_show");
2996
rc= mysql_query(mysql, "CREATE TABLE test_show(id int(4) NOT NULL primary "
2997
" key, name char(2))");
3000
stmt= mysql_simple_prepare(mysql, "show columns from test_show");
3003
verify_param_count(stmt, 0);
3005
rc= mysql_stmt_execute(stmt);
3006
check_execute(stmt, rc);
3008
my_process_stmt_result(stmt);
3009
mysql_stmt_close(stmt);
3011
stmt= mysql_simple_prepare(mysql, "show tables from mysql like ?");
3014
strxmov(query, "show tables from ", current_db, " like \'test_show\'", NullS);
3015
stmt= mysql_simple_prepare(mysql, query);
3018
rc= mysql_stmt_execute(stmt);
3019
check_execute(stmt, rc);
3021
my_process_stmt_result(stmt);
3022
mysql_stmt_close(stmt);
3024
stmt= mysql_simple_prepare(mysql, "describe test_show");
3027
rc= mysql_stmt_execute(stmt);
3028
check_execute(stmt, rc);
3030
my_process_stmt_result(stmt);
3031
mysql_stmt_close(stmt);
3033
stmt= mysql_simple_prepare(mysql, "show keys from test_show");
3036
rc= mysql_stmt_execute(stmt);
3037
check_execute(stmt, rc);
3039
rc= my_process_stmt_result(stmt);
3040
DIE_UNLESS(rc == 1);
3041
mysql_stmt_close(stmt);
3045
/* Test simple update */
3047
static void test_simple_update()
3054
MYSQL_BIND my_bind[2];
3056
char query[MAX_TEST_QUERY_LENGTH];
3058
myheader("test_simple_update");
3060
rc= mysql_autocommit(mysql, TRUE);
3063
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
3066
rc= mysql_query(mysql, "CREATE TABLE test_update(col1 int, "
3067
" col2 varchar(50), col3 int )");
3070
rc= mysql_query(mysql, "INSERT INTO test_update VALUES(1, 'MySQL', 100)");
3073
verify_affected_rows(1);
3075
rc= mysql_commit(mysql);
3078
/* insert by prepare */
3079
strmov(query, "UPDATE test_update SET col2= ? WHERE col1= ?");
3080
stmt= mysql_simple_prepare(mysql, query);
3083
verify_param_count(stmt, 2);
3085
/* Always bzero all members of bind parameter */
3086
bzero((char*) my_bind, sizeof(my_bind));
3089
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
3090
my_bind[0].buffer= szData; /* string data */
3091
my_bind[0].buffer_length= sizeof(szData);
3092
my_bind[0].length= &length[0];
3093
length[0]= my_sprintf(szData, (szData, "updated-data"));
3095
my_bind[1].buffer= (void *) &nData;
3096
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
3098
rc= mysql_stmt_bind_param(stmt, my_bind);
3099
check_execute(stmt, rc);
3101
rc= mysql_stmt_execute(stmt);
3102
check_execute(stmt, rc);
3103
verify_affected_rows(1);
3105
mysql_stmt_close(stmt);
3107
/* now fetch the results ..*/
3108
rc= mysql_commit(mysql);
3111
/* test the results now, only one row should exist */
3112
rc= mysql_query(mysql, "SELECT * FROM test_update");
3115
/* get the result */
3116
result= mysql_store_result(mysql);
3119
rc= my_process_result_set(result);
3120
DIE_UNLESS(rc == 1);
3121
mysql_free_result(result);
3125
/* Test simple long data handling */
3127
static void test_long_data()
3133
MYSQL_BIND my_bind[3];
3134
char query[MAX_TEST_QUERY_LENGTH];
3136
myheader("test_long_data");
3138
rc= mysql_autocommit(mysql, TRUE);
3141
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
3144
rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, "
3145
" col2 long varchar, col3 long varbinary)");
3148
strmov(query, "INSERT INTO test_long_data(col1, col2) VALUES(?)");
3149
stmt= mysql_simple_prepare(mysql, query);
3152
strmov(query, "INSERT INTO test_long_data(col1, col2, col3) VALUES(?, ?, ?)");
3153
stmt= mysql_simple_prepare(mysql, query);
3156
verify_param_count(stmt, 3);
3158
/* Always bzero all members of bind parameter */
3159
bzero((char*) my_bind, sizeof(my_bind));
3161
my_bind[0].buffer= (void *)&int_data;
3162
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3164
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
3166
my_bind[2]= my_bind[1];
3167
rc= mysql_stmt_bind_param(stmt, my_bind);
3168
check_execute(stmt, rc);
3171
data= (char *)"Michael";
3173
/* supply data in pieces */
3174
rc= mysql_stmt_send_long_data(stmt, 1, data, strlen(data));
3175
data= (char *)" 'Monty' Widenius";
3176
rc= mysql_stmt_send_long_data(stmt, 1, data, strlen(data));
3177
check_execute(stmt, rc);
3178
rc= mysql_stmt_send_long_data(stmt, 2, "Venu (venu@mysql.com)", 4);
3179
check_execute(stmt, rc);
3182
rc= mysql_stmt_execute(stmt);
3184
fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
3185
check_execute(stmt, rc);
3187
rc= mysql_commit(mysql);
3190
/* now fetch the results ..*/
3191
rc= mysql_query(mysql, "SELECT * FROM test_long_data");
3194
/* get the result */
3195
result= mysql_store_result(mysql);
3198
rc= my_process_result_set(result);
3199
DIE_UNLESS(rc == 1);
3200
mysql_free_result(result);
3202
verify_col_data("test_long_data", "col1", "999");
3203
verify_col_data("test_long_data", "col2", "Michael 'Monty' Widenius");
3204
verify_col_data("test_long_data", "col3", "Venu");
3205
mysql_stmt_close(stmt);
3209
/* Test long data (string) handling */
3211
static void test_long_data_str()
3219
MYSQL_BIND my_bind[2];
3221
char query[MAX_TEST_QUERY_LENGTH];
3223
myheader("test_long_data_str");
3225
rc= mysql_autocommit(mysql, TRUE);
3228
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
3231
rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(id int, longstr long varchar)");
3234
strmov(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
3235
stmt= mysql_simple_prepare(mysql, query);
3238
verify_param_count(stmt, 2);
3240
/* Always bzero all members of bind parameter */
3241
bzero((char*) my_bind, sizeof(my_bind));
3243
my_bind[0].buffer= (void *)&length;
3244
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3245
my_bind[0].is_null= &is_null[0];
3249
my_bind[1].buffer= data; /* string data */
3250
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
3251
my_bind[1].length= &length1;
3252
my_bind[1].is_null= &is_null[1];
3254
rc= mysql_stmt_bind_param(stmt, my_bind);
3255
check_execute(stmt, rc);
3258
strmov(data, "MySQL AB");
3260
/* supply data in pieces */
3261
for(i= 0; i < 4; i++)
3263
rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 5);
3264
check_execute(stmt, rc);
3267
rc= mysql_stmt_execute(stmt);
3269
fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
3270
check_execute(stmt, rc);
3272
mysql_stmt_close(stmt);
3274
rc= mysql_commit(mysql);
3277
/* now fetch the results ..*/
3278
rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr FROM test_long_data_str");
3281
/* get the result */
3282
result= mysql_store_result(mysql);
3285
rc= my_process_result_set(result);
3286
DIE_UNLESS(rc == 1);
3287
mysql_free_result(result);
3289
my_sprintf(data, (data, "%d", i*5));
3290
verify_col_data("test_long_data_str", "LENGTH(longstr)", data);
3293
strxmov(data, data, "MySQL", NullS);
3294
verify_col_data("test_long_data_str", "longstr", data);
3296
rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
3301
/* Test long data (string) handling */
3303
static void test_long_data_str1()
3309
ulong max_blob_length, blob_length, length1;
3312
MYSQL_BIND my_bind[2];
3314
char query[MAX_TEST_QUERY_LENGTH];
3316
myheader("test_long_data_str1");
3318
rc= mysql_autocommit(mysql, TRUE);
3321
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
3324
rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(longstr long varchar, blb long varbinary)");
3327
strmov(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
3328
stmt= mysql_simple_prepare(mysql, query);
3331
verify_param_count(stmt, 2);
3333
/* Always bzero all members of bind parameter */
3334
bzero((char*) my_bind, sizeof(my_bind));
3336
my_bind[0].buffer= data; /* string data */
3337
my_bind[0].buffer_length= sizeof(data);
3338
my_bind[0].length= &length1;
3339
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
3342
my_bind[1]= my_bind[0];
3343
my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
3345
rc= mysql_stmt_bind_param(stmt, my_bind);
3346
check_execute(stmt, rc);
3347
length= my_sprintf(data, (data, "MySQL AB"));
3349
/* supply data in pieces */
3350
for (i= 0; i < 3; i++)
3352
rc= mysql_stmt_send_long_data(stmt, 0, data, length);
3353
check_execute(stmt, rc);
3355
rc= mysql_stmt_send_long_data(stmt, 1, data, 2);
3356
check_execute(stmt, rc);
3360
rc= mysql_stmt_execute(stmt);
3362
fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
3363
check_execute(stmt, rc);
3365
mysql_stmt_close(stmt);
3367
rc= mysql_commit(mysql);
3370
/* now fetch the results ..*/
3371
rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr, LENGTH(blb), blb FROM test_long_data_str");
3374
/* get the result */
3375
result= mysql_store_result(mysql);
3377
mysql_field_seek(result, 1);
3378
field= mysql_fetch_field(result);
3379
max_blob_length= field->max_length;
3383
rc= my_process_result_set(result);
3384
DIE_UNLESS(rc == 1);
3385
mysql_free_result(result);
3387
my_sprintf(data, (data, "%ld", (long)i*length));
3388
verify_col_data("test_long_data_str", "length(longstr)", data);
3390
my_sprintf(data, (data, "%d", i*2));
3391
verify_col_data("test_long_data_str", "length(blb)", data);
3393
/* Test length of field->max_length */
3394
stmt= mysql_simple_prepare(mysql, "SELECT * from test_long_data_str");
3396
verify_param_count(stmt, 0);
3398
rc= mysql_stmt_execute(stmt);
3399
check_execute(stmt, rc);
3401
rc= mysql_stmt_store_result(stmt);
3402
check_execute(stmt, rc);
3404
result= mysql_stmt_result_metadata(stmt);
3405
field= mysql_fetch_fields(result);
3407
/* First test what happens if STMT_ATTR_UPDATE_MAX_LENGTH is not used */
3408
DIE_UNLESS(field->max_length == 0);
3409
mysql_free_result(result);
3411
/* Enable updating of field->max_length */
3413
mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &true_value);
3414
rc= mysql_stmt_execute(stmt);
3415
check_execute(stmt, rc);
3417
rc= mysql_stmt_store_result(stmt);
3418
check_execute(stmt, rc);
3420
result= mysql_stmt_result_metadata(stmt);
3421
field= mysql_fetch_fields(result);
3423
DIE_UNLESS(field->max_length == max_blob_length);
3425
/* Fetch results into a data buffer that is smaller than data */
3426
bzero((char*) my_bind, sizeof(*my_bind));
3427
my_bind[0].buffer_type= MYSQL_TYPE_BLOB;
3428
my_bind[0].buffer= (void *) &data; /* this buffer won't be altered */
3429
my_bind[0].buffer_length= 16;
3430
my_bind[0].length= &blob_length;
3431
my_bind[0].error= &my_bind[0].error_value;
3432
rc= mysql_stmt_bind_result(stmt, my_bind);
3435
rc= mysql_stmt_fetch(stmt);
3436
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
3437
DIE_UNLESS(my_bind[0].error_value);
3438
DIE_UNLESS(strlen(data) == 16);
3439
DIE_UNLESS(blob_length == max_blob_length);
3441
/* Fetch all data */
3442
bzero((char*) (my_bind+1), sizeof(*my_bind));
3443
my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
3444
my_bind[1].buffer= (void *) &data; /* this buffer won't be altered */
3445
my_bind[1].buffer_length= sizeof(data);
3446
my_bind[1].length= &blob_length;
3447
bzero(data, sizeof(data));
3448
mysql_stmt_fetch_column(stmt, my_bind+1, 0, 0);
3449
DIE_UNLESS(strlen(data) == max_blob_length);
3451
mysql_free_result(result);
3452
mysql_stmt_close(stmt);
3454
/* Drop created table */
3455
rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
3460
/* Test long data (binary) handling */
3462
static void test_long_data_bin()
3469
MYSQL_BIND my_bind[2];
3470
char query[MAX_TEST_QUERY_LENGTH];
3473
myheader("test_long_data_bin");
3475
rc= mysql_autocommit(mysql, TRUE);
3478
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_bin");
3481
rc= mysql_query(mysql, "CREATE TABLE test_long_data_bin(id int, longbin long varbinary)");
3484
strmov(query, "INSERT INTO test_long_data_bin VALUES(?, ?)");
3485
stmt= mysql_simple_prepare(mysql, query);
3488
verify_param_count(stmt, 2);
3490
/* Always bzero all members of bind parameter */
3491
bzero((char*) my_bind, sizeof(my_bind));
3493
my_bind[0].buffer= (void *)&length;
3494
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3497
my_bind[1].buffer= data; /* string data */
3498
my_bind[1].buffer_type= MYSQL_TYPE_LONG_BLOB;
3499
rc= mysql_stmt_bind_param(stmt, my_bind);
3500
check_execute(stmt, rc);
3503
strmov(data, "MySQL AB");
3505
/* supply data in pieces */
3508
for (i= 0; i < 100; i++)
3510
rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 4);
3511
check_execute(stmt, rc);
3515
rc= mysql_stmt_execute(stmt);
3517
fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
3518
check_execute(stmt, rc);
3520
mysql_stmt_close(stmt);
3522
rc= mysql_commit(mysql);
3525
/* now fetch the results ..*/
3526
rc= mysql_query(mysql, "SELECT LENGTH(longbin), longbin FROM test_long_data_bin");
3529
/* get the result */
3530
result= mysql_store_result(mysql);
3533
rc= my_process_result_set(result);
3534
DIE_UNLESS(rc == 1);
3535
mysql_free_result(result);
3539
/* Test simple delete */
3541
static void test_simple_delete()
3545
char szData[30]= {0};
3548
MYSQL_BIND my_bind[2];
3550
char query[MAX_TEST_QUERY_LENGTH];
3552
myheader("test_simple_delete");
3554
rc= mysql_autocommit(mysql, TRUE);
3557
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_simple_delete");
3560
rc= mysql_query(mysql, "CREATE TABLE test_simple_delete(col1 int, \
3561
col2 varchar(50), col3 int )");
3564
rc= mysql_query(mysql, "INSERT INTO test_simple_delete VALUES(1, 'MySQL', 100)");
3567
verify_affected_rows(1);
3569
rc= mysql_commit(mysql);
3572
/* insert by prepare */
3573
strmov(query, "DELETE FROM test_simple_delete WHERE col1= ? AND "
3574
"CONVERT(col2 USING utf8)= ? AND col3= 100");
3575
stmt= mysql_simple_prepare(mysql, query);
3578
verify_param_count(stmt, 2);
3580
/* Always bzero all members of bind parameter */
3581
bzero((char*) my_bind, sizeof(my_bind));
3584
strmov(szData, "MySQL");
3585
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
3586
my_bind[1].buffer= szData; /* string data */
3587
my_bind[1].buffer_length= sizeof(szData);
3588
my_bind[1].length= &length[1];
3591
my_bind[0].buffer= (void *)&nData;
3592
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3594
rc= mysql_stmt_bind_param(stmt, my_bind);
3595
check_execute(stmt, rc);
3597
rc= mysql_stmt_execute(stmt);
3598
check_execute(stmt, rc);
3600
verify_affected_rows(1);
3602
mysql_stmt_close(stmt);
3604
/* now fetch the results ..*/
3605
rc= mysql_commit(mysql);
3608
/* test the results now, only one row should exist */
3609
rc= mysql_query(mysql, "SELECT * FROM test_simple_delete");
3612
/* get the result */
3613
result= mysql_store_result(mysql);
3616
rc= my_process_result_set(result);
3617
DIE_UNLESS(rc == 0);
3618
mysql_free_result(result);
3622
/* Test simple update */
3624
static void test_update()
3631
MYSQL_BIND my_bind[2];
3633
char query[MAX_TEST_QUERY_LENGTH];
3635
myheader("test_update");
3637
rc= mysql_autocommit(mysql, TRUE);
3640
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
3643
rc= mysql_query(mysql, "CREATE TABLE test_update("
3644
"col1 int primary key auto_increment, "
3645
"col2 varchar(50), col3 int )");
3648
strmov(query, "INSERT INTO test_update(col2, col3) VALUES(?, ?)");
3649
stmt= mysql_simple_prepare(mysql, query);
3652
verify_param_count(stmt, 2);
3654
/* Always bzero all members of bind parameter */
3655
bzero((char*) my_bind, sizeof(my_bind));
3658
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
3659
my_bind[0].buffer= szData;
3660
my_bind[0].buffer_length= sizeof(szData);
3661
my_bind[0].length= &length[0];
3662
length[0]= my_sprintf(szData, (szData, "inserted-data"));
3664
my_bind[1].buffer= (void *)&nData;
3665
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
3667
rc= mysql_stmt_bind_param(stmt, my_bind);
3668
check_execute(stmt, rc);
3671
rc= mysql_stmt_execute(stmt);
3672
check_execute(stmt, rc);
3674
verify_affected_rows(1);
3675
mysql_stmt_close(stmt);
3677
strmov(query, "UPDATE test_update SET col2= ? WHERE col3= ?");
3678
stmt= mysql_simple_prepare(mysql, query);
3681
verify_param_count(stmt, 2);
3684
/* Always bzero all members of bind parameter */
3685
bzero((char*) my_bind, sizeof(my_bind));
3687
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
3688
my_bind[0].buffer= szData;
3689
my_bind[0].buffer_length= sizeof(szData);
3690
my_bind[0].length= &length[0];
3691
length[0]= my_sprintf(szData, (szData, "updated-data"));
3693
my_bind[1].buffer= (void *)&nData;
3694
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
3696
rc= mysql_stmt_bind_param(stmt, my_bind);
3697
check_execute(stmt, rc);
3699
rc= mysql_stmt_execute(stmt);
3700
check_execute(stmt, rc);
3701
verify_affected_rows(1);
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 test_update");
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 prepare without parameters */
3725
static void test_prepare_noparam()
3730
char query[MAX_TEST_QUERY_LENGTH];
3732
myheader("test_prepare_noparam");
3734
rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
3738
rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 int, col2 varchar(50))");
3741
/* insert by prepare */
3742
strmov(query, "INSERT INTO my_prepare VALUES(10, 'venu')");
3743
stmt= mysql_simple_prepare(mysql, query);
3746
verify_param_count(stmt, 0);
3748
rc= mysql_stmt_execute(stmt);
3749
check_execute(stmt, rc);
3751
mysql_stmt_close(stmt);
3753
/* now fetch the results ..*/
3754
rc= mysql_commit(mysql);
3757
/* test the results now, only one row should exist */
3758
rc= mysql_query(mysql, "SELECT * FROM my_prepare");
3761
/* get the result */
3762
result= mysql_store_result(mysql);
3765
rc= my_process_result_set(result);
3766
DIE_UNLESS(rc == 1);
3767
mysql_free_result(result);
3771
/* Test simple bind result */
3773
static void test_bind_result()
3780
MYSQL_BIND my_bind[2];
3783
myheader("test_bind_result");
3785
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
3788
rc= mysql_query(mysql, "CREATE TABLE test_bind_result(col1 int , col2 varchar(50))");
3791
rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(10, 'venu')");
3794
rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(20, 'MySQL')");
3797
rc= mysql_query(mysql, "INSERT INTO test_bind_result(col2) VALUES('monty')");
3800
rc= mysql_commit(mysql);
3805
bzero((char*) my_bind, sizeof(my_bind));
3806
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3807
my_bind[0].buffer= (void *) &nData; /* integer data */
3808
my_bind[0].is_null= &is_null[0];
3810
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
3811
my_bind[1].buffer= szData; /* string data */
3812
my_bind[1].buffer_length= sizeof(szData);
3813
my_bind[1].length= &length1;
3814
my_bind[1].is_null= &is_null[1];
3816
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_result");
3819
rc= mysql_stmt_bind_result(stmt, my_bind);
3820
check_execute(stmt, rc);
3822
rc= mysql_stmt_execute(stmt);
3823
check_execute(stmt, rc);
3825
rc= mysql_stmt_fetch(stmt);
3826
check_execute(stmt, rc);
3829
fprintf(stdout, "\n row 1: %d, %s(%lu)", nData, szData, length1);
3830
DIE_UNLESS(nData == 10);
3831
DIE_UNLESS(strcmp(szData, "venu") == 0);
3832
DIE_UNLESS(length1 == 4);
3834
rc= mysql_stmt_fetch(stmt);
3835
check_execute(stmt, rc);
3838
fprintf(stdout, "\n row 2: %d, %s(%lu)", nData, szData, length1);
3839
DIE_UNLESS(nData == 20);
3840
DIE_UNLESS(strcmp(szData, "MySQL") == 0);
3841
DIE_UNLESS(length1 == 5);
3843
rc= mysql_stmt_fetch(stmt);
3844
check_execute(stmt, rc);
3846
if (!opt_silent && is_null[0])
3847
fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
3848
DIE_UNLESS(is_null[0]);
3849
DIE_UNLESS(strcmp(szData, "monty") == 0);
3850
DIE_UNLESS(length1 == 5);
3852
rc= mysql_stmt_fetch(stmt);
3853
DIE_UNLESS(rc == MYSQL_NO_DATA);
3855
mysql_stmt_close(stmt);
3859
/* Test ext bind result */
3861
static void test_bind_result_ext()
3871
char szData[20], bData[20];
3872
ulong szLength, bLength;
3873
MYSQL_BIND my_bind[8];
3877
myheader("test_bind_result_ext");
3879
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
3882
rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, "
3884
" c3 int, c4 bigint, "
3885
" c5 float, c6 double, "
3886
" c7 varbinary(10), "
3887
" c8 varchar(50))");
3890
rc= mysql_query(mysql, "INSERT INTO test_bind_result "
3891
"VALUES (19, 2999, 3999, 4999999, "
3892
" 2345.6, 5678.89563, 'venu', 'mysql')");
3895
rc= mysql_commit(mysql);
3898
bzero((char*) my_bind, sizeof(my_bind));
3899
for (i= 0; i < (int) array_elements(my_bind); i++)
3901
my_bind[i].length= &length[i];
3902
my_bind[i].is_null= &is_null[i];
3905
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
3906
my_bind[0].buffer= (void *)&t_data;
3908
my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
3909
my_bind[2].buffer_type= MYSQL_TYPE_LONG;
3911
my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
3912
my_bind[1].buffer= (void *)&s_data;
3914
my_bind[2].buffer= (void *)&i_data;
3915
my_bind[3].buffer= (void *)&b_data;
3917
my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
3918
my_bind[4].buffer= (void *)&f_data;
3920
my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
3921
my_bind[5].buffer= (void *)&d_data;
3923
my_bind[6].buffer_type= MYSQL_TYPE_STRING;
3924
my_bind[6].buffer= (void *)szData;
3925
my_bind[6].buffer_length= sizeof(szData);
3926
my_bind[6].length= &szLength;
3928
my_bind[7].buffer_type= MYSQL_TYPE_TINY_BLOB;
3929
my_bind[7].buffer= (void *)&bData;
3930
my_bind[7].length= &bLength;
3931
my_bind[7].buffer_length= sizeof(bData);
3933
stmt= mysql_simple_prepare(mysql, "select * from test_bind_result");
3936
rc= mysql_stmt_bind_result(stmt, my_bind);
3937
check_execute(stmt, rc);
3939
rc= mysql_stmt_execute(stmt);
3940
check_execute(stmt, rc);
3942
rc= mysql_stmt_fetch(stmt);
3943
check_execute(stmt, rc);
3947
fprintf(stdout, "\n data (tiny) : %d", t_data);
3948
fprintf(stdout, "\n data (short) : %d", s_data);
3949
fprintf(stdout, "\n data (int) : %d", i_data);
3950
fprintf(stdout, "\n data (big) : %s", llstr(b_data, llbuf));
3952
fprintf(stdout, "\n data (float) : %f", f_data);
3953
fprintf(stdout, "\n data (double) : %f", d_data);
3955
fprintf(stdout, "\n data (str) : %s(%lu)", szData, szLength);
3957
bData[bLength]= '\0'; /* bData is binary */
3958
fprintf(stdout, "\n data (bin) : %s(%lu)", bData, bLength);
3961
DIE_UNLESS(t_data == 19);
3962
DIE_UNLESS(s_data == 2999);
3963
DIE_UNLESS(i_data == 3999);
3964
DIE_UNLESS(b_data == 4999999);
3965
/*DIE_UNLESS(f_data == 2345.60);*/
3966
/*DIE_UNLESS(d_data == 5678.89563);*/
3967
DIE_UNLESS(strcmp(szData, "venu") == 0);
3968
DIE_UNLESS(strncmp(bData, "mysql", 5) == 0);
3969
DIE_UNLESS(szLength == 4);
3970
DIE_UNLESS(bLength == 5);
3972
rc= mysql_stmt_fetch(stmt);
3973
DIE_UNLESS(rc == MYSQL_NO_DATA);
3975
mysql_stmt_close(stmt);
3979
/* Test ext bind result */
3981
static void test_bind_result_ext1()
3994
MYSQL_BIND my_bind[8];
3997
myheader("test_bind_result_ext1");
3999
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
4002
rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, c2 smallint, \
4003
c3 int, c4 bigint, \
4004
c5 float, c6 double, \
4009
rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(120, 2999, 3999, 54, \
4014
rc= mysql_commit(mysql);
4017
bzero((char*) my_bind, sizeof(my_bind));
4018
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
4019
my_bind[0].buffer= (void *) t_data;
4020
my_bind[0].buffer_length= sizeof(t_data);
4021
my_bind[0].error= &my_bind[0].error_value;
4023
my_bind[1].buffer_type= MYSQL_TYPE_FLOAT;
4024
my_bind[1].buffer= (void *)&s_data;
4025
my_bind[1].buffer_length= 0;
4026
my_bind[1].error= &my_bind[1].error_value;
4028
my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
4029
my_bind[2].buffer= (void *)&i_data;
4030
my_bind[2].buffer_length= 0;
4031
my_bind[2].error= &my_bind[2].error_value;
4033
my_bind[3].buffer_type= MYSQL_TYPE_TINY;
4034
my_bind[3].buffer= (void *)&b_data;
4035
my_bind[3].buffer_length= 0;
4036
my_bind[3].error= &my_bind[3].error_value;
4038
my_bind[4].buffer_type= MYSQL_TYPE_LONG;
4039
my_bind[4].buffer= (void *)&f_data;
4040
my_bind[4].buffer_length= 0;
4041
my_bind[4].error= &my_bind[4].error_value;
4043
my_bind[5].buffer_type= MYSQL_TYPE_STRING;
4044
my_bind[5].buffer= (void *)d_data;
4045
my_bind[5].buffer_length= sizeof(d_data);
4046
my_bind[5].error= &my_bind[5].error_value;
4048
my_bind[6].buffer_type= MYSQL_TYPE_LONG;
4049
my_bind[6].buffer= (void *)&bData;
4050
my_bind[6].buffer_length= 0;
4051
my_bind[6].error= &my_bind[6].error_value;
4053
my_bind[7].buffer_type= MYSQL_TYPE_DOUBLE;
4054
my_bind[7].buffer= (void *)&szData;
4055
my_bind[7].buffer_length= 0;
4056
my_bind[7].error= &my_bind[7].error_value;
4058
for (i= 0; i < array_elements(my_bind); i++)
4060
my_bind[i].is_null= &is_null[i];
4061
my_bind[i].length= &length[i];
4064
stmt= mysql_simple_prepare(mysql, "select * from test_bind_result");
4067
rc= mysql_stmt_bind_result(stmt, my_bind);
4068
check_execute(stmt, rc);
4070
rc= mysql_stmt_execute(stmt);
4071
check_execute(stmt, rc);
4073
rc= mysql_stmt_fetch(stmt);
4074
printf("rc=%d\n", rc);
4075
DIE_UNLESS(rc == 0);
4079
fprintf(stdout, "\n data (tiny) : %s(%lu)", t_data, length[0]);
4080
fprintf(stdout, "\n data (short) : %f(%lu)", s_data, length[1]);
4081
fprintf(stdout, "\n data (int) : %d(%lu)", i_data, length[2]);
4082
fprintf(stdout, "\n data (big) : %d(%lu)", b_data, length[3]);
4084
fprintf(stdout, "\n data (float) : %d(%lu)", f_data, length[4]);
4085
fprintf(stdout, "\n data (double) : %s(%lu)", d_data, length[5]);
4087
fprintf(stdout, "\n data (bin) : %ld(%lu)", bData, length[6]);
4088
fprintf(stdout, "\n data (str) : %g(%lu)", szData, length[7]);
4091
DIE_UNLESS(strcmp(t_data, "120") == 0);
4092
DIE_UNLESS(i_data == 3999);
4093
DIE_UNLESS(f_data == 2);
4094
DIE_UNLESS(strcmp(d_data, "58.89") == 0);
4095
DIE_UNLESS(b_data == 54);
4097
DIE_UNLESS(length[0] == 3);
4098
DIE_UNLESS(length[1] == 4);
4099
DIE_UNLESS(length[2] == 2);
4100
DIE_UNLESS(length[3] == 1);
4101
DIE_UNLESS(length[4] == 4);
4102
DIE_UNLESS(length[5] == 5);
4103
DIE_UNLESS(length[6] == 4);
4104
DIE_UNLESS(length[7] == 8);
4106
rc= mysql_stmt_fetch(stmt);
4107
DIE_UNLESS(rc == MYSQL_NO_DATA);
4109
mysql_stmt_close(stmt);
4113
/* Generalized fetch conversion routine for all basic types */
4115
static void bind_fetch(int row_count)
4118
int rc, i, count= row_count;
4128
MYSQL_BIND my_bind[7];
4131
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_bind_fetch VALUES "
4132
"(?, ?, ?, ?, ?, ?, ?)");
4135
verify_param_count(stmt, 7);
4137
/* Always bzero all members of bind parameter */
4138
bzero((char*) my_bind, sizeof(my_bind));
4140
for (i= 0; i < (int) array_elements(my_bind); i++)
4142
my_bind[i].buffer_type= MYSQL_TYPE_LONG;
4143
my_bind[i].buffer= (void *) &data[i];
4145
rc= mysql_stmt_bind_param(stmt, my_bind);
4146
check_execute(stmt, rc);
4151
for (i= 0; i < (int) array_elements(my_bind); i++)
4156
rc= mysql_stmt_execute(stmt);
4157
check_execute(stmt, rc);
4160
rc= mysql_commit(mysql);
4163
mysql_stmt_close(stmt);
4165
rc= my_stmt_result("SELECT * FROM test_bind_fetch");
4166
DIE_UNLESS(row_count == rc);
4168
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_fetch");
4171
for (i= 0; i < (int) array_elements(my_bind); i++)
4173
my_bind[i].buffer= (void *) &data[i];
4174
my_bind[i].length= &length[i];
4175
my_bind[i].is_null= &is_null[i];
4178
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
4179
my_bind[0].buffer= (void *)&i8_data;
4181
my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
4182
my_bind[1].buffer= (void *)&i16_data;
4184
my_bind[2].buffer_type= MYSQL_TYPE_LONG;
4185
my_bind[2].buffer= (void *)&i32_data;
4187
my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
4188
my_bind[3].buffer= (void *)&i64_data;
4190
my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
4191
my_bind[4].buffer= (void *)&f_data;
4193
my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
4194
my_bind[5].buffer= (void *)&d_data;
4196
my_bind[6].buffer_type= MYSQL_TYPE_STRING;
4197
my_bind[6].buffer= (void *)&s_data;
4198
my_bind[6].buffer_length= sizeof(s_data);
4200
rc= mysql_stmt_bind_result(stmt, my_bind);
4201
check_execute(stmt, rc);
4203
rc= mysql_stmt_execute(stmt);
4204
check_execute(stmt, rc);
4206
rc= mysql_stmt_store_result(stmt);
4207
check_execute(stmt, rc);
4211
rc= mysql_stmt_fetch(stmt);
4212
check_execute(stmt, rc);
4216
fprintf(stdout, "\n");
4217
fprintf(stdout, "\n tiny : %ld(%lu)", (ulong) i8_data, length[0]);
4218
fprintf(stdout, "\n short : %ld(%lu)", (ulong) i16_data, length[1]);
4219
fprintf(stdout, "\n int : %ld(%lu)", (ulong) i32_data, length[2]);
4220
fprintf(stdout, "\n longlong : %ld(%lu)", (ulong) i64_data, length[3]);
4221
fprintf(stdout, "\n float : %f(%lu)", f_data, length[4]);
4222
fprintf(stdout, "\n double : %g(%lu)", d_data, length[5]);
4223
fprintf(stdout, "\n char : %s(%lu)", s_data, length[6]);
4228
DIE_UNLESS((int) i8_data == rc);
4229
DIE_UNLESS(length[0] == 1);
4233
DIE_UNLESS((int) i16_data == rc);
4234
DIE_UNLESS(length[1] == 2);
4238
DIE_UNLESS((int) i32_data == rc);
4239
DIE_UNLESS(length[2] == 4);
4243
DIE_UNLESS((int) i64_data == rc);
4244
DIE_UNLESS(length[3] == 8);
4248
DIE_UNLESS((int)f_data == rc);
4249
DIE_UNLESS(length[4] == 4);
4253
DIE_UNLESS((int)d_data == rc);
4254
DIE_UNLESS(length[5] == 8);
4260
long len= my_sprintf(buff, (buff, "%d", rc));
4261
DIE_UNLESS(strcmp(s_data, buff) == 0);
4262
DIE_UNLESS(length[6] == (ulong) len);
4265
rc= mysql_stmt_fetch(stmt);
4266
DIE_UNLESS(rc == MYSQL_NO_DATA);
4268
mysql_stmt_close(stmt);
4272
/* Test fetching of date, time and ts */
4274
static void test_fetch_date()
4279
char date[25], my_time[25], ts[25], ts_4[25], ts_6[20], dt[20];
4280
ulong d_length, t_length, ts_length, ts4_length, ts6_length,
4281
dt_length, y_length;
4282
MYSQL_BIND my_bind[8];
4286
myheader("test_fetch_date");
4288
/* Will not work if sql_mode is NO_ZERO_DATE (implicit if TRADITIONAL) */
4289
rc= mysql_query(mysql, "SET SQL_MODE=''");
4292
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
4295
rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 date, c2 time, \
4303
rc= mysql_query(mysql, "SET SQL_MODE=''");
4304
rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES('2002-01-02', \
4306
'2002-01-02 17:46:59', \
4309
'2020', '1999-12-29')");
4312
rc= mysql_commit(mysql);
4315
bzero((char*) my_bind, sizeof(my_bind));
4316
for (i= 0; i < array_elements(my_bind); i++)
4318
my_bind[i].is_null= &is_null[i];
4319
my_bind[i].length= &length[i];
4322
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
4323
my_bind[1]= my_bind[2]= my_bind[0];
4325
my_bind[0].buffer= (void *)&date;
4326
my_bind[0].buffer_length= sizeof(date);
4327
my_bind[0].length= &d_length;
4329
my_bind[1].buffer= (void *)&my_time;
4330
my_bind[1].buffer_length= sizeof(my_time);
4331
my_bind[1].length= &t_length;
4333
my_bind[2].buffer= (void *)&ts;
4334
my_bind[2].buffer_length= sizeof(ts);
4335
my_bind[2].length= &ts_length;
4337
my_bind[3].buffer_type= MYSQL_TYPE_LONG;
4338
my_bind[3].buffer= (void *)&year;
4339
my_bind[3].length= &y_length;
4341
my_bind[4].buffer_type= MYSQL_TYPE_STRING;
4342
my_bind[4].buffer= (void *)&dt;
4343
my_bind[4].buffer_length= sizeof(dt);
4344
my_bind[4].length= &dt_length;
4346
my_bind[5].buffer_type= MYSQL_TYPE_STRING;
4347
my_bind[5].buffer= (void *)&ts_4;
4348
my_bind[5].buffer_length= sizeof(ts_4);
4349
my_bind[5].length= &ts4_length;
4351
my_bind[6].buffer_type= MYSQL_TYPE_STRING;
4352
my_bind[6].buffer= (void *)&ts_6;
4353
my_bind[6].buffer_length= sizeof(ts_6);
4354
my_bind[6].length= &ts6_length;
4356
rc= my_stmt_result("SELECT * FROM test_bind_result");
4357
DIE_UNLESS(rc == 1);
4359
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_result");
4362
rc= mysql_stmt_bind_result(stmt, my_bind);
4363
check_execute(stmt, rc);
4365
rc= mysql_stmt_execute(stmt);
4366
check_execute(stmt, rc);
4369
rc= mysql_stmt_fetch(stmt);
4370
check_execute(stmt, rc);
4374
fprintf(stdout, "\n date : %s(%lu)", date, d_length);
4375
fprintf(stdout, "\n time : %s(%lu)", my_time, t_length);
4376
fprintf(stdout, "\n ts : %s(%lu)", ts, ts_length);
4377
fprintf(stdout, "\n year : %d(%lu)", year, y_length);
4378
fprintf(stdout, "\n dt : %s(%lu)", dt, dt_length);
4379
fprintf(stdout, "\n ts(4) : %s(%lu)", ts_4, ts4_length);
4380
fprintf(stdout, "\n ts(6) : %s(%lu)", ts_6, ts6_length);
4383
DIE_UNLESS(strcmp(date, "2002-01-02") == 0);
4384
DIE_UNLESS(d_length == 10);
4386
DIE_UNLESS(strcmp(my_time, "12:49:00") == 0);
4387
DIE_UNLESS(t_length == 8);
4389
DIE_UNLESS(strcmp(ts, "2002-01-02 17:46:59") == 0);
4390
DIE_UNLESS(ts_length == 19);
4392
DIE_UNLESS(year == 2010);
4393
DIE_UNLESS(y_length == 4);
4395
DIE_UNLESS(strcmp(dt, "2010-07-10 00:00:00") == 0);
4396
DIE_UNLESS(dt_length == 19);
4398
DIE_UNLESS(strcmp(ts_4, "0000-00-00 00:00:00") == 0);
4399
DIE_UNLESS(ts4_length == strlen("0000-00-00 00:00:00"));
4401
DIE_UNLESS(strcmp(ts_6, "1999-12-29 00:00:00") == 0);
4402
DIE_UNLESS(ts6_length == 19);
4404
rc= mysql_stmt_fetch(stmt);
4405
DIE_UNLESS(rc == MYSQL_NO_DATA);
4407
mysql_stmt_close(stmt);
4411
/* Test fetching of str to all types */
4413
static void test_fetch_str()
4417
myheader("test_fetch_str");
4419
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4422
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 char(10), \
4435
/* Test fetching of long to all types */
4437
static void test_fetch_long()
4441
myheader("test_fetch_long");
4443
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4446
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 int unsigned, \
4459
/* Test fetching of short to all types */
4461
static void test_fetch_short()
4465
myheader("test_fetch_short");
4467
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4470
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 smallint unsigned, \
4472
c3 smallint unsigned, \
4476
c7 smallint unsigned)");
4483
/* Test fetching of tiny to all types */
4485
static void test_fetch_tiny()
4489
myheader("test_fetch_tiny");
4491
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4494
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 tinyint unsigned, \
4496
c3 tinyint unsigned, \
4500
c7 tinyint unsigned)");
4508
/* Test fetching of longlong to all types */
4510
static void test_fetch_bigint()
4514
myheader("test_fetch_bigint");
4516
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4519
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 bigint, \
4521
c3 bigint unsigned, \
4522
c4 bigint unsigned, \
4523
c5 bigint unsigned, \
4524
c6 bigint unsigned, \
4525
c7 bigint unsigned)");
4533
/* Test fetching of float to all types */
4535
static void test_fetch_float()
4539
myheader("test_fetch_float");
4541
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4544
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 float(3), \
4546
c3 float unsigned, \
4550
c7 float(10) unsigned)");
4558
/* Test fetching of double to all types */
4560
static void test_fetch_double()
4564
myheader("test_fetch_double");
4566
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
4569
rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 double(5, 2), "
4570
"c2 double unsigned, c3 double unsigned, "
4571
"c4 double unsigned, c5 double unsigned, "
4572
"c6 double unsigned, c7 double unsigned)");
4580
/* Test simple prepare with all possible types */
4582
static void test_prepare_ext()
4591
MYSQL_BIND my_bind[6];
4592
char query[MAX_TEST_QUERY_LENGTH];
4593
myheader("test_prepare_ext");
4595
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_ext");
4598
sql= (char *)"CREATE TABLE test_prepare_ext"
4608
" c9 double precision,"
4610
" c11 decimal(7, 4),"
4611
" c12 numeric(8, 4),"
4614
" c15 timestamp(14),"
4630
" c31 enum('one', 'two', 'three'),"
4631
" c32 set('monday', 'tuesday', 'wednesday'))";
4633
rc= mysql_query(mysql, sql);
4636
/* insert by prepare - all integers */
4637
strmov(query, (char *)"INSERT INTO test_prepare_ext(c1, c2, c3, c4, c5, c6) VALUES(?, ?, ?, ?, ?, ?)");
4638
stmt= mysql_simple_prepare(mysql, query);
4641
verify_param_count(stmt, 6);
4643
/* Always bzero all members of bind parameter */
4644
bzero((char*) my_bind, sizeof(my_bind));
4647
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
4648
my_bind[0].buffer= (void *)&tData;
4651
my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
4652
my_bind[1].buffer= (void *)&sData;
4655
my_bind[2].buffer_type= MYSQL_TYPE_LONG;
4656
my_bind[2].buffer= (void *)&nData;
4659
my_bind[3].buffer_type= MYSQL_TYPE_LONG;
4660
my_bind[3].buffer= (void *)&nData;
4663
my_bind[4].buffer_type= MYSQL_TYPE_LONG;
4664
my_bind[4].buffer= (void *)&nData;
4667
my_bind[5].buffer_type= MYSQL_TYPE_LONGLONG;
4668
my_bind[5].buffer= (void *)&bData;
4670
rc= mysql_stmt_bind_param(stmt, my_bind);
4671
check_execute(stmt, rc);
4674
* integer to integer
4676
for (nData= 0; nData<10; nData++, tData++, sData++, bData++)
4678
rc= mysql_stmt_execute(stmt);
4679
check_execute(stmt, rc);
4681
mysql_stmt_close(stmt);
4683
/* now fetch the results ..*/
4685
stmt= mysql_simple_prepare(mysql, "SELECT c1, c2, c3, c4, c5, c6 "
4686
"FROM test_prepare_ext");
4689
/* get the result */
4690
rc= mysql_stmt_execute(stmt);
4691
check_execute(stmt, rc);
4693
rc= my_process_stmt_result(stmt);
4694
DIE_UNLESS(nData == rc);
4696
mysql_stmt_close(stmt);
4700
/* Test real and alias names */
4702
static void test_field_names()
4707
myheader("test_field_names");
4710
fprintf(stdout, "\n %d, %d, %d", MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NEWDATE, MYSQL_TYPE_ENUM);
4711
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names1");
4714
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names2");
4717
rc= mysql_query(mysql, "CREATE TABLE test_field_names1(id int, name varchar(50))");
4720
rc= mysql_query(mysql, "CREATE TABLE test_field_names2(id int, name varchar(50))");
4723
/* with table name included with TRUE column name */
4724
rc= mysql_query(mysql, "SELECT id as 'id-alias' FROM test_field_names1");
4727
result= mysql_use_result(mysql);
4730
rc= my_process_result_set(result);
4731
DIE_UNLESS(rc == 0);
4732
mysql_free_result(result);
4734
/* with table name included with TRUE column name */
4735
rc= mysql_query(mysql, "SELECT t1.id as 'id-alias', test_field_names2.name FROM test_field_names1 t1, test_field_names2");
4738
result= mysql_use_result(mysql);
4741
rc= my_process_result_set(result);
4742
DIE_UNLESS(rc == 0);
4743
mysql_free_result(result);
4749
static void test_warnings()
4754
myheader("test_warnings");
4756
mysql_query(mysql, "DROP TABLE if exists test_non_exists");
4758
rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
4762
fprintf(stdout, "\n total warnings: %d", mysql_warning_count(mysql));
4763
rc= mysql_query(mysql, "SHOW WARNINGS");
4766
result= mysql_store_result(mysql);
4769
rc= my_process_result_set(result);
4770
DIE_UNLESS(rc == 1);
4771
mysql_free_result(result);
4777
static void test_errors()
4782
myheader("test_errors");
4784
mysql_query(mysql, "DROP TABLE if exists test_non_exists");
4786
rc= mysql_query(mysql, "DROP TABLE test_non_exists");
4789
rc= mysql_query(mysql, "SHOW ERRORS");
4792
result= mysql_store_result(mysql);
4795
(void) my_process_result_set(result);
4796
mysql_free_result(result);
4800
/* Test simple prepare-insert */
4802
static void test_insert()
4809
MYSQL_BIND my_bind[2];
4812
myheader("test_insert");
4814
rc= mysql_autocommit(mysql, TRUE);
4817
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
4820
rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
4821
col2 varchar(50))");
4824
/* insert by prepare */
4825
stmt= mysql_simple_prepare(mysql,
4826
"INSERT INTO test_prep_insert VALUES(?, ?)");
4829
verify_param_count(stmt, 2);
4832
We need to bzero bind structure because mysql_stmt_bind_param checks all
4835
bzero((char*) my_bind, sizeof(my_bind));
4838
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
4839
my_bind[0].buffer= (void *)&tiny_data;
4842
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
4843
my_bind[1].buffer= str_data;
4844
my_bind[1].buffer_length= sizeof(str_data);;
4845
my_bind[1].length= &length;
4847
rc= mysql_stmt_bind_param(stmt, my_bind);
4848
check_execute(stmt, rc);
4850
/* now, execute the prepared statement to insert 10 records.. */
4851
for (tiny_data= 0; tiny_data < 3; tiny_data++)
4853
length= my_sprintf(str_data, (str_data, "MySQL%d", tiny_data));
4854
rc= mysql_stmt_execute(stmt);
4855
check_execute(stmt, rc);
4858
mysql_stmt_close(stmt);
4860
/* now fetch the results ..*/
4861
rc= mysql_commit(mysql);
4864
/* test the results now, only one row should exist */
4865
rc= mysql_query(mysql, "SELECT * FROM test_prep_insert");
4868
/* get the result */
4869
result= mysql_store_result(mysql);
4872
rc= my_process_result_set(result);
4873
DIE_UNLESS((int) tiny_data == rc);
4874
mysql_free_result(result);
4879
/* Test simple prepare-resultset info */
4881
static void test_prepare_resultset()
4887
myheader("test_prepare_resultset");
4889
rc= mysql_autocommit(mysql, TRUE);
4892
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_resultset");
4895
rc= mysql_query(mysql, "CREATE TABLE test_prepare_resultset(id int, \
4896
name varchar(50), extra double)");
4899
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_prepare_resultset");
4902
verify_param_count(stmt, 0);
4904
result= mysql_stmt_result_metadata(stmt);
4906
my_print_result_metadata(result);
4907
mysql_free_result(result);
4908
mysql_stmt_close(stmt);
4912
/* Test field flags (verify .NET provider) */
4914
static void test_field_flags()
4922
myheader("test_field_flags");
4924
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_flags");
4927
rc= mysql_query(mysql, "CREATE TABLE test_field_flags(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, \
4936
/* with table name included with TRUE column name */
4937
rc= mysql_query(mysql, "SELECT * FROM test_field_flags");
4940
result= mysql_use_result(mysql);
4943
mysql_field_seek(result, 0);
4945
fputc('\n', stdout);
4947
for(i= 0; i< mysql_num_fields(result); i++)
4949
field= mysql_fetch_field(result);
4952
fprintf(stdout, "\n field:%d", i);
4953
if (field->flags & NOT_NULL_FLAG)
4954
fprintf(stdout, "\n NOT_NULL_FLAG");
4955
if (field->flags & PRI_KEY_FLAG)
4956
fprintf(stdout, "\n PRI_KEY_FLAG");
4957
if (field->flags & UNIQUE_KEY_FLAG)
4958
fprintf(stdout, "\n UNIQUE_KEY_FLAG");
4959
if (field->flags & MULTIPLE_KEY_FLAG)
4960
fprintf(stdout, "\n MULTIPLE_KEY_FLAG");
4961
if (field->flags & AUTO_INCREMENT_FLAG)
4962
fprintf(stdout, "\n AUTO_INCREMENT_FLAG");
4966
mysql_free_result(result);
4970
/* Test mysql_stmt_close for open stmts */
4972
static void test_stmt_close()
4975
MYSQL_STMT *stmt1, *stmt2, *stmt3, *stmt_x;
4976
MYSQL_BIND my_bind[1];
4980
char query[MAX_TEST_QUERY_LENGTH];
4982
myheader("test_stmt_close");
4985
fprintf(stdout, "\n Establishing a test connection ...");
4986
if (!(lmysql= mysql_client_init(NULL)))
4988
myerror("mysql_client_init() failed");
4991
if (!(mysql_real_connect(lmysql, opt_host, opt_user,
4992
opt_password, current_db, opt_port,
4993
opt_unix_socket, 0)))
4995
myerror("connection failed");
4998
lmysql->reconnect= 1;
5000
fprintf(stdout, "OK");
5003
/* set AUTOCOMMIT to ON*/
5004
mysql_autocommit(lmysql, TRUE);
5006
rc= mysql_query(lmysql, "SET SQL_MODE = ''");
5009
rc= mysql_query(lmysql, "DROP TABLE IF EXISTS test_stmt_close");
5012
rc= mysql_query(lmysql, "CREATE TABLE test_stmt_close(id int)");
5015
strmov(query, "DO \"nothing\"");
5016
stmt1= mysql_simple_prepare(lmysql, query);
5019
verify_param_count(stmt1, 0);
5021
strmov(query, "INSERT INTO test_stmt_close(id) VALUES(?)");
5022
stmt_x= mysql_simple_prepare(mysql, query);
5025
verify_param_count(stmt_x, 1);
5027
strmov(query, "UPDATE test_stmt_close SET id= ? WHERE id= ?");
5028
stmt3= mysql_simple_prepare(lmysql, query);
5031
verify_param_count(stmt3, 2);
5033
strmov(query, "SELECT * FROM test_stmt_close WHERE id= ?");
5034
stmt2= mysql_simple_prepare(lmysql, query);
5037
verify_param_count(stmt2, 1);
5039
rc= mysql_stmt_close(stmt1);
5041
fprintf(stdout, "\n mysql_close_stmt(1) returned: %d", rc);
5042
DIE_UNLESS(rc == 0);
5045
Originally we were going to close all statements automatically in
5046
mysql_close(). This proved to not work well - users weren't able to
5047
close statements by hand once mysql_close() had been called.
5048
Now mysql_close() doesn't free any statements, so this test doesn't
5049
serve its original designation any more.
5050
Here we free stmt2 and stmt3 by hand to avoid memory leaks.
5052
mysql_stmt_close(stmt2);
5053
mysql_stmt_close(stmt3);
5054
mysql_close(lmysql);
5057
We need to bzero bind structure because mysql_stmt_bind_param checks all
5060
bzero((char*) my_bind, sizeof(my_bind));
5062
my_bind[0].buffer= (void *)&count;
5063
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
5066
rc= mysql_stmt_bind_param(stmt_x, my_bind);
5067
check_execute(stmt_x, rc);
5069
rc= mysql_stmt_execute(stmt_x);
5070
check_execute(stmt_x, rc);
5072
verify_st_affected_rows(stmt_x, 1);
5074
rc= mysql_stmt_close(stmt_x);
5076
fprintf(stdout, "\n mysql_close_stmt(x) returned: %d", rc);
5077
DIE_UNLESS( rc == 0);
5079
rc= mysql_query(mysql, "SELECT id FROM test_stmt_close");
5082
result= mysql_store_result(mysql);
5085
rc= my_process_result_set(result);
5086
DIE_UNLESS(rc == 1);
5087
mysql_free_result(result);
5091
/* Test simple set-variable prepare */
5093
static void test_set_variable()
5095
MYSQL_STMT *stmt, *stmt1;
5097
int set_count, def_count, get_count;
5099
char var[NAME_LEN+1];
5100
MYSQL_BIND set_bind[1], get_bind[2];
5102
myheader("test_set_variable");
5104
mysql_autocommit(mysql, TRUE);
5106
stmt1= mysql_simple_prepare(mysql, "show variables like 'max_error_count'");
5110
We need to bzero bind structure because mysql_stmt_bind_param checks all
5113
bzero((char*) get_bind, sizeof(get_bind));
5115
get_bind[0].buffer_type= MYSQL_TYPE_STRING;
5116
get_bind[0].buffer= (void *)var;
5117
get_bind[0].length= &length;
5118
get_bind[0].buffer_length= (int)NAME_LEN;
5121
get_bind[1].buffer_type= MYSQL_TYPE_LONG;
5122
get_bind[1].buffer= (void *)&get_count;
5124
rc= mysql_stmt_execute(stmt1);
5125
check_execute(stmt1, rc);
5127
rc= mysql_stmt_bind_result(stmt1, get_bind);
5128
check_execute(stmt1, rc);
5130
rc= mysql_stmt_fetch(stmt1);
5131
check_execute(stmt1, rc);
5134
fprintf(stdout, "\n max_error_count(default): %d", get_count);
5135
def_count= get_count;
5137
DIE_UNLESS(strcmp(var, "max_error_count") == 0);
5138
rc= mysql_stmt_fetch(stmt1);
5139
DIE_UNLESS(rc == MYSQL_NO_DATA);
5141
stmt= mysql_simple_prepare(mysql, "set max_error_count= ?");
5144
bzero((char*) set_bind, sizeof(set_bind));
5146
set_bind[0].buffer_type= MYSQL_TYPE_LONG;
5147
set_bind[0].buffer= (void *)&set_count;
5149
rc= mysql_stmt_bind_param(stmt, set_bind);
5150
check_execute(stmt, rc);
5153
rc= mysql_stmt_execute(stmt);
5154
check_execute(stmt, rc);
5156
mysql_commit(mysql);
5158
rc= mysql_stmt_execute(stmt1);
5159
check_execute(stmt1, rc);
5161
rc= mysql_stmt_fetch(stmt1);
5162
check_execute(stmt1, rc);
5165
fprintf(stdout, "\n max_error_count : %d", get_count);
5166
DIE_UNLESS(get_count == set_count);
5168
rc= mysql_stmt_fetch(stmt1);
5169
DIE_UNLESS(rc == MYSQL_NO_DATA);
5171
/* restore back to default */
5172
set_count= def_count;
5173
rc= mysql_stmt_execute(stmt);
5174
check_execute(stmt, rc);
5176
rc= mysql_stmt_execute(stmt1);
5177
check_execute(stmt1, rc);
5179
rc= mysql_stmt_fetch(stmt1);
5180
check_execute(stmt1, rc);
5183
fprintf(stdout, "\n max_error_count(default): %d", get_count);
5184
DIE_UNLESS(get_count == set_count);
5186
rc= mysql_stmt_fetch(stmt1);
5187
DIE_UNLESS(rc == MYSQL_NO_DATA);
5189
mysql_stmt_close(stmt);
5190
mysql_stmt_close(stmt1);
5195
/* Insert meta info .. */
5197
static void test_insert_meta()
5204
myheader("test_insert_meta");
5206
rc= mysql_autocommit(mysql, TRUE);
5209
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
5212
rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
5213
col2 varchar(50), col3 varchar(30))");
5216
strmov(query, "INSERT INTO test_prep_insert VALUES(10, 'venu1', 'test')");
5217
stmt= mysql_simple_prepare(mysql, query);
5220
verify_param_count(stmt, 0);
5222
result= mysql_param_result(stmt);
5225
mysql_stmt_close(stmt);
5227
strmov(query, "INSERT INTO test_prep_insert VALUES(?, 'venu', ?)");
5228
stmt= mysql_simple_prepare(mysql, query);
5231
verify_param_count(stmt, 2);
5233
result= mysql_param_result(stmt);
5236
my_print_result_metadata(result);
5238
mysql_field_seek(result, 0);
5239
field= mysql_fetch_field(result);
5242
fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col1");
5243
DIE_UNLESS(strcmp(field->name, "col1") == 0);
5245
field= mysql_fetch_field(result);
5248
fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col3");
5249
DIE_UNLESS(strcmp(field->name, "col3") == 0);
5251
field= mysql_fetch_field(result);
5254
mysql_free_result(result);
5255
mysql_stmt_close(stmt);
5259
/* Update meta info .. */
5261
static void test_update_meta()
5268
myheader("test_update_meta");
5270
rc= mysql_autocommit(mysql, TRUE);
5273
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_update");
5276
rc= mysql_query(mysql, "CREATE TABLE test_prep_update(col1 tinyint, \
5277
col2 varchar(50), col3 varchar(30))");
5280
strmov(query, "UPDATE test_prep_update SET col1=10, col2='venu1' WHERE col3='test'");
5281
stmt= mysql_simple_prepare(mysql, query);
5284
verify_param_count(stmt, 0);
5286
result= mysql_param_result(stmt);
5289
mysql_stmt_close(stmt);
5291
strmov(query, "UPDATE test_prep_update SET col1=?, col2='venu' WHERE col3=?");
5292
stmt= mysql_simple_prepare(mysql, query);
5295
verify_param_count(stmt, 2);
5297
result= mysql_param_result(stmt);
5300
my_print_result_metadata(result);
5302
mysql_field_seek(result, 0);
5303
field= mysql_fetch_field(result);
5307
fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
5308
fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
5310
DIE_UNLESS(strcmp(field->name, "col1") == 0);
5311
DIE_UNLESS(strcmp(field->table, "test_prep_update") == 0);
5313
field= mysql_fetch_field(result);
5317
fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col3");
5318
fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
5320
DIE_UNLESS(strcmp(field->name, "col3") == 0);
5321
DIE_UNLESS(strcmp(field->table, "test_prep_update") == 0);
5323
field= mysql_fetch_field(result);
5326
mysql_free_result(result);
5327
mysql_stmt_close(stmt);
5331
/* Select meta info .. */
5333
static void test_select_meta()
5340
myheader("test_select_meta");
5342
rc= mysql_autocommit(mysql, TRUE);
5345
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_select");
5348
rc= mysql_query(mysql, "CREATE TABLE test_prep_select(col1 tinyint, \
5349
col2 varchar(50), col3 varchar(30))");
5352
strmov(query, "SELECT * FROM test_prep_select WHERE col1=10");
5353
stmt= mysql_simple_prepare(mysql, query);
5356
verify_param_count(stmt, 0);
5358
result= mysql_param_result(stmt);
5361
strmov(query, "SELECT col1, col3 from test_prep_select WHERE col1=? AND col3='test' AND col2= ?");
5362
stmt= mysql_simple_prepare(mysql, query);
5365
verify_param_count(stmt, 2);
5367
result= mysql_param_result(stmt);
5370
my_print_result_metadata(result);
5372
mysql_field_seek(result, 0);
5373
field= mysql_fetch_field(result);
5377
fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
5378
fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
5380
DIE_UNLESS(strcmp(field->name, "col1") == 0);
5381
DIE_UNLESS(strcmp(field->table, "test_prep_select") == 0);
5383
field= mysql_fetch_field(result);
5387
fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col2");
5388
fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
5390
DIE_UNLESS(strcmp(field->name, "col2") == 0);
5391
DIE_UNLESS(strcmp(field->table, "test_prep_select") == 0);
5393
field= mysql_fetch_field(result);
5396
mysql_free_result(result);
5397
mysql_stmt_close(stmt);
5402
/* Test FUNCTION field info / DATE_FORMAT() table_name . */
5404
static void test_func_fields()
5410
myheader("test_func_fields");
5412
rc= mysql_autocommit(mysql, TRUE);
5415
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_dateformat");
5418
rc= mysql_query(mysql, "CREATE TABLE test_dateformat(id int, \
5422
rc= mysql_query(mysql, "INSERT INTO test_dateformat(id) values(10)");
5425
rc= mysql_query(mysql, "SELECT ts FROM test_dateformat");
5428
result= mysql_store_result(mysql);
5431
field= mysql_fetch_field(result);
5434
fprintf(stdout, "\n table name: `%s` (expected: `%s`)", field->table,
5436
DIE_UNLESS(strcmp(field->table, "test_dateformat") == 0);
5438
field= mysql_fetch_field(result);
5439
mytest_r(field); /* no more fields */
5441
mysql_free_result(result);
5444
rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'venu' FROM test_dateformat");
5447
result= mysql_store_result(mysql);
5450
field= mysql_fetch_field(result);
5453
fprintf(stdout, "\n table name: `%s` (expected: `%s`)", field->table, "");
5454
DIE_UNLESS(field->table[0] == '\0');
5456
field= mysql_fetch_field(result);
5457
mytest_r(field); /* no more fields */
5459
mysql_free_result(result);
5461
/* FIELD ALIAS TEST */
5462
rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'YEAR' FROM test_dateformat");
5465
result= mysql_store_result(mysql);
5468
field= mysql_fetch_field(result);
5472
printf("\n field name: `%s` (expected: `%s`)", field->name, "YEAR");
5473
printf("\n field org name: `%s` (expected: `%s`)", field->org_name, "");
5475
DIE_UNLESS(strcmp(field->name, "YEAR") == 0);
5476
DIE_UNLESS(field->org_name[0] == '\0');
5478
field= mysql_fetch_field(result);
5479
mytest_r(field); /* no more fields */
5481
mysql_free_result(result);
5485
/* Multiple stmts .. */
5487
static void test_multi_stmt()
5490
MYSQL_STMT *stmt, *stmt1, *stmt2;
5494
MYSQL_BIND my_bind[2];
5497
myheader("test_multi_stmt");
5499
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_multi_table");
5502
rc= mysql_query(mysql, "CREATE TABLE test_multi_table(id int, name char(20))");
5505
rc= mysql_query(mysql, "INSERT INTO test_multi_table values(10, 'mysql')");
5508
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_multi_table "
5512
stmt2= mysql_simple_prepare(mysql, "UPDATE test_multi_table "
5513
"SET name='updated' WHERE id=10");
5516
verify_param_count(stmt, 1);
5519
We need to bzero bind structure because mysql_stmt_bind_param checks all
5522
bzero((char*) my_bind, sizeof(my_bind));
5524
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
5525
my_bind[0].buffer= (void *)&id;
5526
my_bind[0].is_null= &is_null[0];
5527
my_bind[0].length= &length[0];
5531
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
5532
my_bind[1].buffer= (void *)name;
5533
my_bind[1].buffer_length= sizeof(name);
5534
my_bind[1].length= &length[1];
5535
my_bind[1].is_null= &is_null[1];
5537
rc= mysql_stmt_bind_param(stmt, my_bind);
5538
check_execute(stmt, rc);
5540
rc= mysql_stmt_bind_result(stmt, my_bind);
5541
check_execute(stmt, rc);
5544
rc= mysql_stmt_execute(stmt);
5545
check_execute(stmt, rc);
5548
rc= mysql_stmt_fetch(stmt);
5549
check_execute(stmt, rc);
5553
fprintf(stdout, "\n int_data: %lu(%lu)", (ulong) id, length[0]);
5554
fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
5556
DIE_UNLESS(id == 10);
5557
DIE_UNLESS(strcmp(name, "mysql") == 0);
5559
rc= mysql_stmt_fetch(stmt);
5560
DIE_UNLESS(rc == MYSQL_NO_DATA);
5562
/* alter the table schema now */
5563
stmt1= mysql_simple_prepare(mysql, "DELETE FROM test_multi_table "
5565
"CONVERT(name USING utf8)=?");
5568
verify_param_count(stmt1, 2);
5570
rc= mysql_stmt_bind_param(stmt1, my_bind);
5571
check_execute(stmt1, rc);
5573
rc= mysql_stmt_execute(stmt2);
5574
check_execute(stmt2, rc);
5576
verify_st_affected_rows(stmt2, 1);
5578
rc= mysql_stmt_execute(stmt);
5579
check_execute(stmt, rc);
5581
rc= mysql_stmt_fetch(stmt);
5582
check_execute(stmt, rc);
5586
fprintf(stdout, "\n int_data: %lu(%lu)", (ulong) id, length[0]);
5587
fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
5589
DIE_UNLESS(id == 10);
5590
DIE_UNLESS(strcmp(name, "updated") == 0);
5592
rc= mysql_stmt_fetch(stmt);
5593
DIE_UNLESS(rc == MYSQL_NO_DATA);
5595
rc= mysql_stmt_execute(stmt1);
5596
check_execute(stmt1, rc);
5598
verify_st_affected_rows(stmt1, 1);
5600
mysql_stmt_close(stmt1);
5602
rc= mysql_stmt_execute(stmt);
5603
check_execute(stmt, rc);
5605
rc= mysql_stmt_fetch(stmt);
5606
DIE_UNLESS(rc == MYSQL_NO_DATA);
5608
rc= my_stmt_result("SELECT * FROM test_multi_table");
5609
DIE_UNLESS(rc == 0);
5611
mysql_stmt_close(stmt);
5612
mysql_stmt_close(stmt2);
5617
/* Test simple sample - manual */
5619
static void test_manual_sample()
5621
unsigned int param_count;
5627
ulonglong affected_rows;
5628
MYSQL_BIND my_bind[3];
5630
char query[MAX_TEST_QUERY_LENGTH];
5632
myheader("test_manual_sample");
5635
Sample which is incorporated directly in the manual under Prepared
5636
statements section (Example from mysql_stmt_execute()
5639
mysql_autocommit(mysql, 1);
5640
if (mysql_query(mysql, "DROP TABLE IF EXISTS test_table"))
5642
fprintf(stderr, "\n drop table failed");
5643
fprintf(stderr, "\n %s", mysql_error(mysql));
5646
if (mysql_query(mysql, "CREATE TABLE test_table(col1 int, col2 varchar(50), \
5648
col4 timestamp(14))"))
5650
fprintf(stderr, "\n create table failed");
5651
fprintf(stderr, "\n %s", mysql_error(mysql));
5655
/* Prepare a insert query with 3 parameters */
5656
strmov(query, "INSERT INTO test_table(col1, col2, col3) values(?, ?, ?)");
5657
if (!(stmt= mysql_simple_prepare(mysql, query)))
5659
fprintf(stderr, "\n prepare, insert failed");
5660
fprintf(stderr, "\n %s", mysql_error(mysql));
5664
fprintf(stdout, "\n prepare, insert successful");
5666
/* Get the parameter count from the statement */
5667
param_count= mysql_stmt_param_count(stmt);
5670
fprintf(stdout, "\n total parameters in insert: %d", param_count);
5671
if (param_count != 3) /* validate parameter count */
5673
fprintf(stderr, "\n invalid parameter count returned by MySQL");
5677
/* Bind the data for the parameters */
5680
We need to bzero bind structure because mysql_stmt_bind_param checks all
5683
bzero((char*) my_bind, sizeof(my_bind));
5686
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
5687
my_bind[0].buffer= (void *)&int_data;
5690
my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
5691
my_bind[1].buffer= (void *)str_data;
5692
my_bind[1].buffer_length= sizeof(str_data);
5695
my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
5696
my_bind[2].buffer= (void *)&small_data;
5697
my_bind[2].is_null= &is_null;
5700
/* Bind the buffers */
5701
if (mysql_stmt_bind_param(stmt, my_bind))
5703
fprintf(stderr, "\n param bind failed");
5704
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
5708
/* Specify the data */
5709
int_data= 10; /* integer */
5710
strmov(str_data, "MySQL"); /* string */
5712
/* INSERT SMALLINT data as NULL */
5715
/* Execute the insert statement - 1*/
5716
if (mysql_stmt_execute(stmt))
5718
fprintf(stderr, "\n execute 1 failed");
5719
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
5723
/* Get the total rows affected */
5724
affected_rows= mysql_stmt_affected_rows(stmt);
5727
fprintf(stdout, "\n total affected rows: %ld", (ulong) affected_rows);
5728
if (affected_rows != 1) /* validate affected rows */
5730
fprintf(stderr, "\n invalid affected rows by MySQL");
5734
/* Re-execute the insert, by changing the values */
5736
strmov(str_data, "The most popular open source database");
5737
small_data= 1000; /* smallint */
5738
is_null= 0; /* reset */
5740
/* Execute the insert statement - 2*/
5741
if (mysql_stmt_execute(stmt))
5743
fprintf(stderr, "\n execute 2 failed");
5744
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
5748
/* Get the total rows affected */
5749
affected_rows= mysql_stmt_affected_rows(stmt);
5752
fprintf(stdout, "\n total affected rows: %ld", (ulong) affected_rows);
5753
if (affected_rows != 1) /* validate affected rows */
5755
fprintf(stderr, "\n invalid affected rows by MySQL");
5759
/* Close the statement */
5760
if (mysql_stmt_close(stmt))
5762
fprintf(stderr, "\n failed while closing the statement");
5763
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
5766
rc= my_stmt_result("SELECT * FROM test_table");
5767
DIE_UNLESS(rc == 2);
5769
/* DROP THE TABLE */
5770
if (mysql_query(mysql, "DROP TABLE test_table"))
5772
fprintf(stderr, "\n drop table failed");
5773
fprintf(stderr, "\n %s", mysql_error(mysql));
5777
fprintf(stdout, "Success !!!");
5781
/* Test alter table scenario in the middle of prepare */
5783
static void test_prepare_alter()
5787
MYSQL_BIND my_bind[1];
5790
myheader("test_prepare_alter");
5792
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_alter");
5795
rc= mysql_query(mysql, "CREATE TABLE test_prep_alter(id int, name char(20))");
5798
rc= mysql_query(mysql, "INSERT INTO test_prep_alter values(10, 'venu'), (20, 'mysql')");
5801
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_prep_alter VALUES(?, 'monty')");
5804
verify_param_count(stmt, 1);
5807
We need to bzero bind structure because mysql_stmt_bind_param checks all
5810
bzero((char*) my_bind, sizeof(my_bind));
5813
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
5814
my_bind[0].buffer= (void *)&id;
5815
my_bind[0].is_null= &is_null;
5817
rc= mysql_stmt_bind_param(stmt, my_bind);
5818
check_execute(stmt, rc);
5821
rc= mysql_stmt_execute(stmt);
5822
check_execute(stmt, rc);
5824
if (thread_query((char *)"ALTER TABLE test_prep_alter change id id_new varchar(20)"))
5828
rc= mysql_stmt_execute(stmt);
5829
check_execute(stmt, rc);
5831
rc= my_stmt_result("SELECT * FROM test_prep_alter");
5832
DIE_UNLESS(rc == 4);
5834
mysql_stmt_close(stmt);
5838
/* Test the support of multi-statement executions */
5840
static void test_multi_statements()
5846
const char *query= "\
5847
DROP TABLE IF EXISTS test_multi_tab;\
5848
CREATE TABLE test_multi_tab(id int, name char(20));\
5849
INSERT INTO test_multi_tab(id) VALUES(10), (20);\
5850
INSERT INTO test_multi_tab VALUES(20, 'insert;comma');\
5851
SELECT * FROM test_multi_tab;\
5852
UPDATE test_multi_tab SET name='new;name' WHERE id=20;\
5853
DELETE FROM test_multi_tab WHERE name='new;name';\
5854
SELECT * FROM test_multi_tab;\
5855
DELETE FROM test_multi_tab WHERE id=10;\
5856
SELECT * FROM test_multi_tab;\
5857
DROP TABLE test_multi_tab;\
5859
DROP TABLE IF EXISTS test_multi_tab";
5860
uint count, exp_value;
5861
uint rows[]= {0, 0, 2, 1, 3, 2, 2, 1, 1, 0, 0, 1, 0};
5863
myheader("test_multi_statements");
5866
First test that we get an error for multi statements
5867
(Because default connection is not opened with CLIENT_MULTI_STATEMENTS)
5869
rc= mysql_query(mysql, query); /* syntax error */
5872
rc= mysql_next_result(mysql);
5873
DIE_UNLESS(rc == -1);
5874
rc= mysql_more_results(mysql);
5875
DIE_UNLESS(rc == 0);
5877
if (!(mysql_local= mysql_client_init(NULL)))
5879
fprintf(stdout, "\n mysql_client_init() failed");
5883
/* Create connection that supports multi statements */
5884
if (!(mysql_real_connect(mysql_local, opt_host, opt_user,
5885
opt_password, current_db, opt_port,
5886
opt_unix_socket, CLIENT_MULTI_STATEMENTS)))
5888
fprintf(stdout, "\n connection failed(%s)", mysql_error(mysql_local));
5891
mysql_local->reconnect= 1;
5893
rc= mysql_query(mysql_local, query);
5896
for (count= 0 ; count < array_elements(rows) ; count++)
5899
fprintf(stdout, "\n Query %d: ", count);
5900
if ((result= mysql_store_result(mysql_local)))
5902
(void) my_process_result_set(result);
5903
mysql_free_result(result);
5905
else if (!opt_silent)
5906
fprintf(stdout, "OK, %ld row(s) affected, %ld warning(s)\n",
5907
(ulong) mysql_affected_rows(mysql_local),
5908
(ulong) mysql_warning_count(mysql_local));
5910
exp_value= (uint) mysql_affected_rows(mysql_local);
5911
if (rows[count] != exp_value)
5913
fprintf(stderr, "row %d had affected rows: %d, should be %d\n",
5914
count, exp_value, rows[count]);
5917
if (count != array_elements(rows) -1)
5919
if (!(rc= mysql_more_results(mysql_local)))
5922
"mysql_more_result returned wrong value: %d for row %d\n",
5926
if ((rc= mysql_next_result(mysql_local)))
5928
exp_value= mysql_errno(mysql_local);
5935
rc= mysql_more_results(mysql_local);
5936
DIE_UNLESS(rc == 0);
5937
rc= mysql_next_result(mysql_local);
5938
DIE_UNLESS(rc == -1);
5942
/* check that errors abort multi statements */
5944
rc= mysql_query(mysql_local, "select 1+1+a;select 1+1");
5946
rc= mysql_more_results(mysql_local);
5947
DIE_UNLESS(rc == 0);
5948
rc= mysql_next_result(mysql_local);
5949
DIE_UNLESS(rc == -1);
5951
rc= mysql_query(mysql_local, "select 1+1;select 1+1+a;select 1");
5953
result= mysql_store_result(mysql_local);
5955
mysql_free_result(result);
5956
rc= mysql_more_results(mysql_local);
5957
DIE_UNLESS(rc == 1);
5958
rc= mysql_next_result(mysql_local);
5962
Ensure that we can now do a simple query (this checks that the server is
5963
not trying to send us the results for the last 'select 1'
5965
rc= mysql_query(mysql_local, "select 1+1+1");
5967
result= mysql_store_result(mysql_local);
5969
(void) my_process_result_set(result);
5970
mysql_free_result(result);
5973
Check if errors in one of the queries handled properly.
5975
rc= mysql_query(mysql_local, "select 1; select * from not_existing_table");
5977
result= mysql_store_result(mysql_local);
5978
mysql_free_result(result);
5980
rc= mysql_next_result(mysql_local);
5983
rc= mysql_next_result(mysql_local);
5986
mysql_close(mysql_local);
5991
Check that Prepared statement cannot contain several
5995
static void test_prepare_multi_statements()
5999
char query[MAX_TEST_QUERY_LENGTH];
6000
myheader("test_prepare_multi_statements");
6002
if (!(mysql_local= mysql_client_init(NULL)))
6004
fprintf(stderr, "\n mysql_client_init() failed");
6008
if (!(mysql_real_connect(mysql_local, opt_host, opt_user,
6009
opt_password, current_db, opt_port,
6010
opt_unix_socket, CLIENT_MULTI_STATEMENTS)))
6012
fprintf(stderr, "\n connection failed(%s)", mysql_error(mysql_local));
6015
mysql_local->reconnect= 1;
6016
strmov(query, "select 1; select 'another value'");
6017
stmt= mysql_simple_prepare(mysql_local, query);
6019
mysql_close(mysql_local);
6023
/* Test simple bind store result */
6025
static void test_store_result()
6031
MYSQL_BIND my_bind[2];
6032
ulong length, length1;
6035
myheader("test_store_result");
6037
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
6040
rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
6043
rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
6046
rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
6049
rc= mysql_commit(mysql);
6053
bzero((char*) my_bind, sizeof(my_bind));
6054
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
6055
my_bind[0].buffer= (void *) &nData; /* integer data */
6056
my_bind[0].length= &length;
6057
my_bind[0].is_null= &is_null[0];
6060
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
6061
my_bind[1].buffer= szData; /* string data */
6062
my_bind[1].buffer_length= sizeof(szData);
6063
my_bind[1].length= &length1;
6064
my_bind[1].is_null= &is_null[1];
6067
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_store_result");
6070
rc= mysql_stmt_bind_result(stmt, my_bind);
6071
check_execute(stmt, rc);
6073
rc= mysql_stmt_execute(stmt);
6074
check_execute(stmt, rc);
6076
rc= mysql_stmt_store_result(stmt);
6077
check_execute(stmt, rc);
6079
rc= mysql_stmt_fetch(stmt);
6080
check_execute(stmt, rc);
6083
fprintf(stdout, "\n row 1: %ld, %s(%lu)", (long) nData, szData, length1);
6084
DIE_UNLESS(nData == 10);
6085
DIE_UNLESS(strcmp(szData, "venu") == 0);
6086
DIE_UNLESS(length1 == 4);
6088
rc= mysql_stmt_fetch(stmt);
6089
check_execute(stmt, rc);
6092
fprintf(stdout, "\n row 2: %ld, %s(%lu)", (long) nData, szData, length1);
6093
DIE_UNLESS(nData == 20);
6094
DIE_UNLESS(strcmp(szData, "mysql") == 0);
6095
DIE_UNLESS(length1 == 5);
6098
rc= mysql_stmt_fetch(stmt);
6099
check_execute(stmt, rc);
6101
if (!opt_silent && is_null[0])
6102
fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
6103
DIE_UNLESS(is_null[0]);
6104
DIE_UNLESS(strcmp(szData, "monty") == 0);
6105
DIE_UNLESS(length1 == 5);
6107
rc= mysql_stmt_fetch(stmt);
6108
DIE_UNLESS(rc == MYSQL_NO_DATA);
6110
rc= mysql_stmt_execute(stmt);
6111
check_execute(stmt, rc);
6113
rc= mysql_stmt_store_result(stmt);
6114
check_execute(stmt, rc);
6116
rc= mysql_stmt_fetch(stmt);
6117
check_execute(stmt, rc);
6120
fprintf(stdout, "\n row 1: %ld, %s(%lu)", (long) nData, szData, length1);
6121
DIE_UNLESS(nData == 10);
6122
DIE_UNLESS(strcmp(szData, "venu") == 0);
6123
DIE_UNLESS(length1 == 4);
6125
rc= mysql_stmt_fetch(stmt);
6126
check_execute(stmt, rc);
6129
fprintf(stdout, "\n row 2: %ld, %s(%lu)", (long) nData, szData, length1);
6130
DIE_UNLESS(nData == 20);
6131
DIE_UNLESS(strcmp(szData, "mysql") == 0);
6132
DIE_UNLESS(length1 == 5);
6135
rc= mysql_stmt_fetch(stmt);
6136
check_execute(stmt, rc);
6138
if (!opt_silent && is_null[0])
6139
fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
6140
DIE_UNLESS(is_null[0]);
6141
DIE_UNLESS(strcmp(szData, "monty") == 0);
6142
DIE_UNLESS(length1 == 5);
6144
rc= mysql_stmt_fetch(stmt);
6145
DIE_UNLESS(rc == MYSQL_NO_DATA);
6147
mysql_stmt_close(stmt);
6151
/* Test simple bind store result */
6153
static void test_store_result1()
6158
myheader("test_store_result1");
6160
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
6163
rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
6166
rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
6169
rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
6172
rc= mysql_commit(mysql);
6175
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_store_result");
6178
rc= mysql_stmt_execute(stmt);
6179
check_execute(stmt, rc);
6181
rc= mysql_stmt_store_result(stmt);
6182
check_execute(stmt, rc);
6185
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
6188
fprintf(stdout, "\n total rows: %d", rc);
6189
DIE_UNLESS(rc == 3);
6191
rc= mysql_stmt_execute(stmt);
6192
check_execute(stmt, rc);
6194
rc= mysql_stmt_store_result(stmt);
6195
check_execute(stmt, rc);
6198
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
6201
fprintf(stdout, "\n total rows: %d", rc);
6202
DIE_UNLESS(rc == 3);
6204
mysql_stmt_close(stmt);
6208
/* Another test for bind and store result */
6210
static void test_store_result2()
6216
MYSQL_BIND my_bind[1];
6217
char query[MAX_TEST_QUERY_LENGTH];
6219
myheader("test_store_result2");
6221
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
6224
rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
6227
rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
6230
rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
6233
rc= mysql_commit(mysql);
6237
We need to bzero bind structure because mysql_stmt_bind_param checks all
6240
bzero((char*) my_bind, sizeof(my_bind));
6242
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
6243
my_bind[0].buffer= (void *) &nData; /* integer data */
6244
my_bind[0].length= &length;
6245
my_bind[0].is_null= 0;
6247
strmov((char *)query , "SELECT col1 FROM test_store_result where col1= ?");
6248
stmt= mysql_simple_prepare(mysql, query);
6251
rc= mysql_stmt_bind_param(stmt, my_bind);
6252
check_execute(stmt, rc);
6254
rc= mysql_stmt_bind_result(stmt, my_bind);
6255
check_execute(stmt, rc);
6257
nData= 10; length= 0;
6258
rc= mysql_stmt_execute(stmt);
6259
check_execute(stmt, rc);
6262
rc= mysql_stmt_store_result(stmt);
6263
check_execute(stmt, rc);
6265
rc= mysql_stmt_fetch(stmt);
6266
check_execute(stmt, rc);
6269
fprintf(stdout, "\n row 1: %d", nData);
6270
DIE_UNLESS(nData == 10);
6272
rc= mysql_stmt_fetch(stmt);
6273
DIE_UNLESS(rc == MYSQL_NO_DATA);
6276
rc= mysql_stmt_execute(stmt);
6277
check_execute(stmt, rc);
6280
rc= mysql_stmt_store_result(stmt);
6281
check_execute(stmt, rc);
6283
rc= mysql_stmt_fetch(stmt);
6284
check_execute(stmt, rc);
6287
fprintf(stdout, "\n row 1: %d", nData);
6288
DIE_UNLESS(nData == 20);
6290
rc= mysql_stmt_fetch(stmt);
6291
DIE_UNLESS(rc == MYSQL_NO_DATA);
6292
mysql_stmt_close(stmt);
6296
/* Test simple subselect prepare */
6298
static void test_subselect()
6303
MYSQL_BIND my_bind[1];
6304
DBUG_ENTER("test_subselect");
6306
myheader("test_subselect");
6308
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sub1");
6311
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sub2");
6314
rc= mysql_query(mysql, "CREATE TABLE test_sub1(id int)");
6317
rc= mysql_query(mysql, "CREATE TABLE test_sub2(id int, id1 int)");
6320
rc= mysql_query(mysql, "INSERT INTO test_sub1 values(2)");
6323
rc= mysql_query(mysql, "INSERT INTO test_sub2 VALUES(1, 7), (2, 7)");
6326
rc= mysql_commit(mysql);
6331
We need to bzero bind structure because mysql_stmt_bind_param checks all
6334
bzero((char*) my_bind, sizeof(my_bind));
6336
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
6337
my_bind[0].buffer= (void *) &id;
6338
my_bind[0].length= 0;
6339
my_bind[0].is_null= 0;
6341
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_sub2(id) SELECT * FROM test_sub1 WHERE id= ?");
6344
rc= mysql_stmt_bind_param(stmt, my_bind);
6345
check_execute(stmt, rc);
6348
rc= mysql_stmt_execute(stmt);
6349
check_execute(stmt, rc);
6351
verify_st_affected_rows(stmt, 1);
6354
rc= mysql_stmt_execute(stmt);
6355
check_execute(stmt, rc);
6357
verify_st_affected_rows(stmt, 0);
6359
mysql_stmt_close(stmt);
6361
rc= my_stmt_result("SELECT * FROM test_sub2");
6362
DIE_UNLESS(rc == 3);
6364
rc= my_stmt_result("SELECT ROW(1, 7) IN (select id, id1 "
6365
"from test_sub2 WHERE id1= 8)");
6366
DIE_UNLESS(rc == 1);
6367
rc= my_stmt_result("SELECT ROW(1, 7) IN (select id, id1 "
6368
"from test_sub2 WHERE id1= 7)");
6369
DIE_UNLESS(rc == 1);
6371
stmt= mysql_simple_prepare(mysql, ("SELECT ROW(1, 7) IN (select id, id1 "
6372
"from test_sub2 WHERE id1= ?)"));
6375
rc= mysql_stmt_bind_param(stmt, my_bind);
6376
check_execute(stmt, rc);
6378
rc= mysql_stmt_bind_result(stmt, my_bind);
6379
check_execute(stmt, rc);
6382
rc= mysql_stmt_execute(stmt);
6383
check_execute(stmt, rc);
6385
rc= mysql_stmt_fetch(stmt);
6386
check_execute(stmt, rc);
6389
fprintf(stdout, "\n row 1: %d", id);
6390
DIE_UNLESS(id == 1);
6392
rc= mysql_stmt_fetch(stmt);
6393
DIE_UNLESS(rc == MYSQL_NO_DATA);
6396
rc= mysql_stmt_execute(stmt);
6397
check_execute(stmt, rc);
6399
rc= mysql_stmt_fetch(stmt);
6400
check_execute(stmt, rc);
6403
fprintf(stdout, "\n row 1: %d", id);
6404
DIE_UNLESS(id == 0);
6406
rc= mysql_stmt_fetch(stmt);
6407
DIE_UNLESS(rc == MYSQL_NO_DATA);
6409
mysql_stmt_close(stmt);
6415
Generalized conversion routine to handle DATE, TIME and DATETIME
6416
conversion using MYSQL_TIME structure
6419
static void test_bind_date_conv(uint row_count)
6421
MYSQL_STMT *stmt= 0;
6422
uint rc, i, count= row_count;
6424
MYSQL_BIND my_bind[4];
6425
my_bool is_null[4]= {0};
6428
uint year, month, day, hour, minute, sec;
6430
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_date VALUES(?, ?, ?, ?)");
6433
verify_param_count(stmt, 4);
6436
We need to bzero bind structure because mysql_stmt_bind_param checks all
6439
bzero((char*) my_bind, sizeof(my_bind));
6441
my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
6442
my_bind[1].buffer_type= MYSQL_TYPE_TIME;
6443
my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
6444
my_bind[3].buffer_type= MYSQL_TYPE_DATE;
6446
for (i= 0; i < (int) array_elements(my_bind); i++)
6448
my_bind[i].buffer= (void *) &tm[i];
6449
my_bind[i].is_null= &is_null[i];
6450
my_bind[i].length= &length[i];
6451
my_bind[i].buffer_length= 30;
6465
rc= mysql_stmt_bind_param(stmt, my_bind);
6466
check_execute(stmt, rc);
6468
for (count= 0; count < row_count; count++)
6470
for (i= 0; i < (int) array_elements(my_bind); i++)
6473
tm[i].second_part= second_part+count;
6474
if (my_bind[i].buffer_type != MYSQL_TYPE_TIME)
6476
tm[i].year= year+count;
6477
tm[i].month= month+count;
6478
tm[i].day= day+count;
6481
tm[i].year= tm[i].month= tm[i].day= 0;
6482
if (my_bind[i].buffer_type != MYSQL_TYPE_DATE)
6484
tm[i].hour= hour+count;
6485
tm[i].minute= minute+count;
6486
tm[i].second= sec+count;
6489
tm[i].hour= tm[i].minute= tm[i].second= 0;
6491
rc= mysql_stmt_execute(stmt);
6492
check_execute(stmt, rc);
6495
rc= mysql_commit(mysql);
6498
mysql_stmt_close(stmt);
6500
rc= my_stmt_result("SELECT * FROM test_date");
6501
DIE_UNLESS(row_count == rc);
6503
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_date");
6506
rc= mysql_stmt_bind_result(stmt, my_bind);
6507
check_execute(stmt, rc);
6509
rc= mysql_stmt_execute(stmt);
6510
check_execute(stmt, rc);
6512
rc= mysql_stmt_store_result(stmt);
6513
check_execute(stmt, rc);
6515
for (count= 0; count < row_count; count++)
6517
rc= mysql_stmt_fetch(stmt);
6518
DIE_UNLESS(rc == 0 || rc == MYSQL_DATA_TRUNCATED);
6521
fprintf(stdout, "\n");
6522
for (i= 0; i < array_elements(my_bind); i++)
6525
fprintf(stdout, "\ntime[%d]: %02d-%02d-%02d %02d:%02d:%02d.%02lu",
6526
i, tm[i].year, tm[i].month, tm[i].day,
6527
tm[i].hour, tm[i].minute, tm[i].second,
6529
DIE_UNLESS(tm[i].year == 0 || tm[i].year == year+count);
6530
DIE_UNLESS(tm[i].month == 0 || tm[i].month == month+count);
6531
DIE_UNLESS(tm[i].day == 0 || tm[i].day == day+count);
6533
DIE_UNLESS(tm[i].hour == 0 || tm[i].hour == hour+count);
6534
DIE_UNLESS(tm[i].minute == 0 || tm[i].minute == minute+count);
6535
DIE_UNLESS(tm[i].second == 0 || tm[i].second == sec+count);
6536
DIE_UNLESS(tm[i].second_part == 0 ||
6537
tm[i].second_part == second_part+count);
6540
rc= mysql_stmt_fetch(stmt);
6541
DIE_UNLESS(rc == MYSQL_NO_DATA);
6543
mysql_stmt_close(stmt);
6547
/* Test DATE, TIME, DATETIME and TS with MYSQL_TIME conversion */
6549
static void test_date()
6553
myheader("test_date");
6555
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
6558
rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP(14), \
6565
test_bind_date_conv(5);
6569
/* Test all time types to DATE and DATE to all types */
6571
static void test_date_date()
6575
myheader("test_date_date");
6577
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
6580
rc= mysql_query(mysql, "CREATE TABLE test_date(c1 DATE, \
6587
test_bind_date_conv(3);
6591
/* Test all time types to TIME and TIME to all types */
6593
static void test_date_time()
6597
myheader("test_date_time");
6599
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
6602
rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIME, \
6609
test_bind_date_conv(3);
6613
/* Test all time types to TIMESTAMP and TIMESTAMP to all types */
6615
static void test_date_ts()
6619
myheader("test_date_ts");
6621
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
6624
rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP(10), \
6631
test_bind_date_conv(2);
6635
/* Test all time types to DATETIME and DATETIME to all types */
6637
static void test_date_dt()
6641
myheader("test_date_dt");
6643
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
6646
rc= mysql_query(mysql, "CREATE TABLE test_date(c1 datetime, "
6647
" c2 datetime, c3 datetime, c4 date)");
6650
test_bind_date_conv(2);
6654
/* Misc tests to keep pure coverage happy */
6656
static void test_pure_coverage()
6659
MYSQL_BIND my_bind[1];
6663
myheader("test_pure_coverage");
6665
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_pure");
6668
rc= mysql_query(mysql, "CREATE TABLE test_pure(c1 int, c2 varchar(20))");
6671
stmt= mysql_simple_prepare(mysql, "insert into test_pure(c67788) values(10)");
6674
/* Query without params and result should allow to bind 0 arrays */
6675
stmt= mysql_simple_prepare(mysql, "insert into test_pure(c2) values(10)");
6678
rc= mysql_stmt_bind_param(stmt, (MYSQL_BIND*)0);
6679
check_execute(stmt, rc);
6681
rc= mysql_stmt_execute(stmt);
6682
check_execute(stmt, rc);
6684
rc= mysql_stmt_bind_result(stmt, (MYSQL_BIND*)0);
6685
DIE_UNLESS(rc == 1);
6687
mysql_stmt_close(stmt);
6689
stmt= mysql_simple_prepare(mysql, "insert into test_pure(c2) values(?)");
6693
We need to bzero bind structure because mysql_stmt_bind_param checks all
6696
bzero((char*) my_bind, sizeof(my_bind));
6698
my_bind[0].length= &length;
6699
my_bind[0].is_null= 0;
6700
my_bind[0].buffer_length= 0;
6702
my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
6703
rc= mysql_stmt_bind_param(stmt, my_bind);
6704
check_execute_r(stmt, rc); /* unsupported buffer type */
6706
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
6707
rc= mysql_stmt_bind_param(stmt, my_bind);
6708
check_execute(stmt, rc);
6710
rc= mysql_stmt_store_result(stmt);
6711
check_execute(stmt, rc);
6713
mysql_stmt_close(stmt);
6715
stmt= mysql_simple_prepare(mysql, "select * from test_pure");
6716
check_execute(stmt, rc);
6718
rc= mysql_stmt_execute(stmt);
6719
check_execute(stmt, rc);
6721
my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
6722
rc= mysql_stmt_bind_result(stmt, my_bind);
6723
check_execute_r(stmt, rc); /* unsupported buffer type */
6725
rc= mysql_stmt_store_result(stmt);
6728
rc= mysql_stmt_store_result(stmt);
6729
DIE_UNLESS(rc); /* Old error must be reset first */
6731
mysql_stmt_close(stmt);
6733
mysql_query(mysql, "DROP TABLE test_pure");
6737
/* Test for string buffer fetch */
6739
static void test_buffers()
6742
MYSQL_BIND my_bind[1];
6748
myheader("test_buffers");
6750
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_buffer");
6753
rc= mysql_query(mysql, "CREATE TABLE test_buffer(str varchar(20))");
6756
rc= mysql_query(mysql, "insert into test_buffer values('MySQL')\
6757
, ('Database'), ('Open-Source'), ('Popular')");
6760
stmt= mysql_simple_prepare(mysql, "select str from test_buffer");
6763
rc= mysql_stmt_execute(stmt);
6764
check_execute(stmt, rc);
6766
bzero(buffer, sizeof(buffer)); /* Avoid overruns in printf() */
6768
bzero((char*) my_bind, sizeof(my_bind));
6769
my_bind[0].length= &length;
6770
my_bind[0].is_null= &is_null;
6771
my_bind[0].buffer_length= 1;
6772
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
6773
my_bind[0].buffer= (void *)buffer;
6774
my_bind[0].error= &my_bind[0].error_value;
6776
rc= mysql_stmt_bind_result(stmt, my_bind);
6777
check_execute(stmt, rc);
6779
rc= mysql_stmt_store_result(stmt);
6780
check_execute(stmt, rc);
6783
rc= mysql_stmt_fetch(stmt);
6784
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
6785
DIE_UNLESS(my_bind[0].error_value);
6787
fprintf(stdout, "\n data: %s (%lu)", buffer, length);
6788
DIE_UNLESS(buffer[0] == 'M');
6789
DIE_UNLESS(buffer[1] == 'X');
6790
DIE_UNLESS(length == 5);
6792
my_bind[0].buffer_length= 8;
6793
rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
6794
check_execute(stmt, rc);
6796
rc= mysql_stmt_fetch(stmt);
6797
check_execute(stmt, rc);
6799
fprintf(stdout, "\n data: %s (%lu)", buffer, length);
6800
DIE_UNLESS(strncmp(buffer, "Database", 8) == 0);
6801
DIE_UNLESS(length == 8);
6803
my_bind[0].buffer_length= 12;
6804
rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
6805
check_execute(stmt, rc);
6807
rc= mysql_stmt_fetch(stmt);
6808
check_execute(stmt, rc);
6810
fprintf(stdout, "\n data: %s (%lu)", buffer, length);
6811
DIE_UNLESS(strcmp(buffer, "Open-Source") == 0);
6812
DIE_UNLESS(length == 11);
6814
my_bind[0].buffer_length= 6;
6815
rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
6816
check_execute(stmt, rc);
6818
rc= mysql_stmt_fetch(stmt);
6819
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
6820
DIE_UNLESS(my_bind[0].error_value);
6822
fprintf(stdout, "\n data: %s (%lu)", buffer, length);
6823
DIE_UNLESS(strncmp(buffer, "Popula", 6) == 0);
6824
DIE_UNLESS(length == 7);
6826
mysql_stmt_close(stmt);
6830
/* Test the direct query execution in the middle of open stmts */
6832
static void test_open_direct()
6838
myheader("test_open_direct");
6840
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_open_direct");
6843
rc= mysql_query(mysql, "CREATE TABLE test_open_direct(id int, name char(6))");
6846
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_open_direct values(10, 'mysql')");
6849
rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
6852
result= mysql_store_result(mysql);
6855
rc= my_process_result_set(result);
6856
DIE_UNLESS(rc == 0);
6857
mysql_free_result(result);
6859
rc= mysql_stmt_execute(stmt);
6860
check_execute(stmt, rc);
6862
verify_st_affected_rows(stmt, 1);
6864
rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
6867
result= mysql_store_result(mysql);
6870
rc= my_process_result_set(result);
6871
DIE_UNLESS(rc == 1);
6872
mysql_free_result(result);
6874
rc= mysql_stmt_execute(stmt);
6875
check_execute(stmt, rc);
6877
verify_st_affected_rows(stmt, 1);
6879
rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
6882
result= mysql_store_result(mysql);
6885
rc= my_process_result_set(result);
6886
DIE_UNLESS(rc == 2);
6887
mysql_free_result(result);
6889
mysql_stmt_close(stmt);
6891
/* run a direct query in the middle of a fetch */
6892
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_open_direct");
6895
rc= mysql_stmt_execute(stmt);
6896
check_execute(stmt, rc);
6898
rc= mysql_stmt_fetch(stmt);
6899
check_execute(stmt, rc);
6901
rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
6904
rc= mysql_stmt_close(stmt);
6905
check_execute(stmt, rc);
6907
rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
6910
/* run a direct query with store result */
6911
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_open_direct");
6914
rc= mysql_stmt_execute(stmt);
6915
check_execute(stmt, rc);
6917
rc= mysql_stmt_store_result(stmt);
6918
check_execute(stmt, rc);
6920
rc= mysql_stmt_fetch(stmt);
6921
check_execute(stmt, rc);
6923
rc= mysql_query(mysql, "drop table test_open_direct");
6926
rc= mysql_stmt_close(stmt);
6927
check_execute(stmt, rc);
6931
/* Test fetch without prior bound buffers */
6933
static void test_fetch_nobuffs()
6936
MYSQL_BIND my_bind[4];
6940
myheader("test_fetch_nobuffs");
6942
stmt= mysql_simple_prepare(mysql, "SELECT DATABASE(), CURRENT_USER(), \
6943
CURRENT_DATE(), CURRENT_TIME()");
6946
rc= mysql_stmt_execute(stmt);
6947
check_execute(stmt, rc);
6950
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
6954
fprintf(stdout, "\n total rows : %d", rc);
6955
DIE_UNLESS(rc == 1);
6957
bzero((char*) my_bind, sizeof(MYSQL_BIND));
6958
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
6959
my_bind[0].buffer= (void *)str[0];
6960
my_bind[0].buffer_length= sizeof(str[0]);
6961
my_bind[1]= my_bind[2]= my_bind[3]= my_bind[0];
6962
my_bind[1].buffer= (void *)str[1];
6963
my_bind[2].buffer= (void *)str[2];
6964
my_bind[3].buffer= (void *)str[3];
6966
rc= mysql_stmt_bind_result(stmt, my_bind);
6967
check_execute(stmt, rc);
6969
rc= mysql_stmt_execute(stmt);
6970
check_execute(stmt, rc);
6973
while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
6978
fprintf(stdout, "\n CURRENT_DATABASE(): %s", str[0]);
6979
fprintf(stdout, "\n CURRENT_USER() : %s", str[1]);
6980
fprintf(stdout, "\n CURRENT_DATE() : %s", str[2]);
6981
fprintf(stdout, "\n CURRENT_TIME() : %s", str[3]);
6985
fprintf(stdout, "\n total rows : %d", rc);
6986
DIE_UNLESS(rc == 1);
6988
mysql_stmt_close(stmt);
6992
/* Test a misc bug */
6994
static void test_ushort_bug()
6997
MYSQL_BIND my_bind[4];
7000
ulong s_length, l_length, ll_length, t_length;
7001
ulonglong longlong_value;
7005
myheader("test_ushort_bug");
7007
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ushort");
7010
rc= mysql_query(mysql, "CREATE TABLE test_ushort(a smallint unsigned, \
7011
b smallint unsigned, \
7012
c smallint unsigned, \
7013
d smallint unsigned)");
7016
rc= mysql_query(mysql,
7017
"INSERT INTO test_ushort VALUES(35999, 35999, 35999, 200)");
7021
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_ushort");
7024
rc= mysql_stmt_execute(stmt);
7025
check_execute(stmt, rc);
7027
bzero((char*) my_bind, sizeof(my_bind));
7028
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
7029
my_bind[0].buffer= (void *)&short_value;
7030
my_bind[0].is_unsigned= TRUE;
7031
my_bind[0].length= &s_length;
7033
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
7034
my_bind[1].buffer= (void *)&long_value;
7035
my_bind[1].length= &l_length;
7037
my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
7038
my_bind[2].buffer= (void *)&longlong_value;
7039
my_bind[2].length= &ll_length;
7041
my_bind[3].buffer_type= MYSQL_TYPE_TINY;
7042
my_bind[3].buffer= (void *)&tiny_value;
7043
my_bind[3].is_unsigned= TRUE;
7044
my_bind[3].length= &t_length;
7046
rc= mysql_stmt_bind_result(stmt, my_bind);
7047
check_execute(stmt, rc);
7049
rc= mysql_stmt_fetch(stmt);
7050
check_execute(stmt, rc);
7054
fprintf(stdout, "\n ushort : %d (%ld)", short_value, s_length);
7055
fprintf(stdout, "\n ulong : %lu (%ld)", (ulong) long_value, l_length);
7056
fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
7058
fprintf(stdout, "\n tinyint : %d (%ld)", tiny_value, t_length);
7061
DIE_UNLESS(short_value == 35999);
7062
DIE_UNLESS(s_length == 2);
7064
DIE_UNLESS(long_value == 35999);
7065
DIE_UNLESS(l_length == 4);
7067
DIE_UNLESS(longlong_value == 35999);
7068
DIE_UNLESS(ll_length == 8);
7070
DIE_UNLESS(tiny_value == 200);
7071
DIE_UNLESS(t_length == 1);
7073
rc= mysql_stmt_fetch(stmt);
7074
DIE_UNLESS(rc == MYSQL_NO_DATA);
7076
mysql_stmt_close(stmt);
7080
/* Test a misc smallint-signed conversion bug */
7082
static void test_sshort_bug()
7085
MYSQL_BIND my_bind[4];
7088
ulong s_length, l_length, ll_length, t_length;
7089
ulonglong longlong_value;
7094
myheader("test_sshort_bug");
7096
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sshort");
7099
rc= mysql_query(mysql, "CREATE TABLE test_sshort(a smallint signed, \
7100
b smallint signed, \
7101
c smallint unsigned, \
7102
d smallint unsigned)");
7105
rc= mysql_query(mysql, "INSERT INTO test_sshort VALUES(-5999, -5999, 35999, 200)");
7109
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_sshort");
7112
rc= mysql_stmt_execute(stmt);
7113
check_execute(stmt, rc);
7115
bzero((char*) my_bind, sizeof(my_bind));
7116
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
7117
my_bind[0].buffer= (void *)&short_value;
7118
my_bind[0].length= &s_length;
7120
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
7121
my_bind[1].buffer= (void *)&long_value;
7122
my_bind[1].length= &l_length;
7124
my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
7125
my_bind[2].buffer= (void *)&longlong_value;
7126
my_bind[2].length= &ll_length;
7128
my_bind[3].buffer_type= MYSQL_TYPE_TINY;
7129
my_bind[3].buffer= (void *)&tiny_value;
7130
my_bind[3].is_unsigned= TRUE;
7131
my_bind[3].length= &t_length;
7133
rc= mysql_stmt_bind_result(stmt, my_bind);
7134
check_execute(stmt, rc);
7136
rc= mysql_stmt_fetch(stmt);
7137
check_execute(stmt, rc);
7141
fprintf(stdout, "\n sshort : %d (%ld)", short_value, s_length);
7142
fprintf(stdout, "\n slong : %ld (%ld)", (long) long_value, l_length);
7143
fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
7145
fprintf(stdout, "\n tinyint : %d (%ld)", tiny_value, t_length);
7148
DIE_UNLESS(short_value == -5999);
7149
DIE_UNLESS(s_length == 2);
7151
DIE_UNLESS(long_value == -5999);
7152
DIE_UNLESS(l_length == 4);
7154
DIE_UNLESS(longlong_value == 35999);
7155
DIE_UNLESS(ll_length == 8);
7157
DIE_UNLESS(tiny_value == 200);
7158
DIE_UNLESS(t_length == 1);
7160
rc= mysql_stmt_fetch(stmt);
7161
DIE_UNLESS(rc == MYSQL_NO_DATA);
7163
mysql_stmt_close(stmt);
7167
/* Test a misc tinyint-signed conversion bug */
7169
static void test_stiny_bug()
7172
MYSQL_BIND my_bind[4];
7175
ulong s_length, l_length, ll_length, t_length;
7176
ulonglong longlong_value;
7181
myheader("test_stiny_bug");
7183
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stiny");
7186
rc= mysql_query(mysql, "CREATE TABLE test_stiny(a tinyint signed, \
7188
c tinyint unsigned, \
7189
d tinyint unsigned)");
7192
rc= mysql_query(mysql, "INSERT INTO test_stiny VALUES(-128, -127, 255, 0)");
7196
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_stiny");
7199
rc= mysql_stmt_execute(stmt);
7200
check_execute(stmt, rc);
7202
bzero((char*) my_bind, sizeof(my_bind));
7203
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
7204
my_bind[0].buffer= (void *)&short_value;
7205
my_bind[0].length= &s_length;
7207
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
7208
my_bind[1].buffer= (void *)&long_value;
7209
my_bind[1].length= &l_length;
7211
my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
7212
my_bind[2].buffer= (void *)&longlong_value;
7213
my_bind[2].length= &ll_length;
7215
my_bind[3].buffer_type= MYSQL_TYPE_TINY;
7216
my_bind[3].buffer= (void *)&tiny_value;
7217
my_bind[3].length= &t_length;
7219
rc= mysql_stmt_bind_result(stmt, my_bind);
7220
check_execute(stmt, rc);
7222
rc= mysql_stmt_fetch(stmt);
7223
check_execute(stmt, rc);
7227
fprintf(stdout, "\n sshort : %d (%ld)", short_value, s_length);
7228
fprintf(stdout, "\n slong : %ld (%ld)", (long) long_value, l_length);
7229
fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
7231
fprintf(stdout, "\n tinyint : %d (%ld)", tiny_value, t_length);
7234
DIE_UNLESS(short_value == -128);
7235
DIE_UNLESS(s_length == 2);
7237
DIE_UNLESS(long_value == -127);
7238
DIE_UNLESS(l_length == 4);
7240
DIE_UNLESS(longlong_value == 255);
7241
DIE_UNLESS(ll_length == 8);
7243
DIE_UNLESS(tiny_value == 0);
7244
DIE_UNLESS(t_length == 1);
7246
rc= mysql_stmt_fetch(stmt);
7247
DIE_UNLESS(rc == MYSQL_NO_DATA);
7249
mysql_stmt_close(stmt);
7253
/* Test misc field information, bug: #74 */
7255
static void test_field_misc()
7261
myheader("test_field_misc");
7263
rc= mysql_query(mysql, "SELECT @@autocommit");
7266
result= mysql_store_result(mysql);
7269
rc= my_process_result_set(result);
7270
DIE_UNLESS(rc == 1);
7272
verify_prepare_field(result, 0,
7273
"@@autocommit", "", /* field and its org name */
7274
MYSQL_TYPE_LONGLONG, /* field type */
7275
"", "", /* table and its org name */
7276
"", 1, 0); /* db name, length(its bool flag)*/
7278
mysql_free_result(result);
7280
stmt= mysql_simple_prepare(mysql, "SELECT @@autocommit");
7283
rc= mysql_stmt_execute(stmt);
7284
check_execute(stmt, rc);
7286
result= mysql_stmt_result_metadata(stmt);
7289
rc= my_process_stmt_result(stmt);
7290
DIE_UNLESS(rc == 1);
7292
verify_prepare_field(result, 0,
7293
"@@autocommit", "", /* field and its org name */
7294
MYSQL_TYPE_LONGLONG, /* field type */
7295
"", "", /* table and its org name */
7296
"", 1, 0); /* db name, length(its bool flag)*/
7298
mysql_free_result(result);
7299
mysql_stmt_close(stmt);
7301
stmt= mysql_simple_prepare(mysql, "SELECT @@max_error_count");
7304
result= mysql_stmt_result_metadata(stmt);
7307
rc= mysql_stmt_execute(stmt);
7308
check_execute(stmt, rc);
7310
rc= my_process_stmt_result(stmt);
7311
DIE_UNLESS(rc == 1);
7313
verify_prepare_field(result, 0,
7314
"@@max_error_count", "", /* field and its org name */
7315
MYSQL_TYPE_LONGLONG, /* field type */
7316
"", "", /* table and its org name */
7317
/* db name, length */
7318
"", MY_INT64_NUM_DECIMAL_DIGITS , 0);
7320
mysql_free_result(result);
7321
mysql_stmt_close(stmt);
7323
stmt= mysql_simple_prepare(mysql, "SELECT @@max_allowed_packet");
7326
result= mysql_stmt_result_metadata(stmt);
7329
rc= mysql_stmt_execute(stmt);
7330
check_execute(stmt, rc);
7332
DIE_UNLESS(1 == my_process_stmt_result(stmt));
7334
verify_prepare_field(result, 0,
7335
"@@max_allowed_packet", "", /* field and its org name */
7336
MYSQL_TYPE_LONGLONG, /* field type */
7337
"", "", /* table and its org name */
7338
/* db name, length */
7339
"", MY_INT64_NUM_DECIMAL_DIGITS, 0);
7341
mysql_free_result(result);
7342
mysql_stmt_close(stmt);
7344
stmt= mysql_simple_prepare(mysql, "SELECT @@sql_warnings");
7347
result= mysql_stmt_result_metadata(stmt);
7350
rc= mysql_stmt_execute(stmt);
7351
check_execute(stmt, rc);
7353
rc= my_process_stmt_result(stmt);
7354
DIE_UNLESS(rc == 1);
7356
verify_prepare_field(result, 0,
7357
"@@sql_warnings", "", /* field and its org name */
7358
MYSQL_TYPE_LONGLONG, /* field type */
7359
"", "", /* table and its org name */
7360
"", 1, 0); /* db name, length */
7362
mysql_free_result(result);
7363
mysql_stmt_close(stmt);
7368
Test SET OPTION feature with prepare stmts
7369
bug #85 (reported by mark@mysql.com)
7372
static void test_set_option()
7378
myheader("test_set_option");
7380
mysql_autocommit(mysql, TRUE);
7382
/* LIMIT the rows count to 2 */
7383
rc= mysql_query(mysql, "SET OPTION SQL_SELECT_LIMIT= 2");
7386
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_limit");
7389
rc= mysql_query(mysql, "CREATE TABLE test_limit(a tinyint)");
7392
rc= mysql_query(mysql, "INSERT INTO test_limit VALUES(10), (20), (30), (40)");
7396
fprintf(stdout, "\n with SQL_SELECT_LIMIT= 2 (direct)");
7397
rc= mysql_query(mysql, "SELECT * FROM test_limit");
7400
result= mysql_store_result(mysql);
7403
rc= my_process_result_set(result);
7404
DIE_UNLESS(rc == 2);
7406
mysql_free_result(result);
7409
fprintf(stdout, "\n with SQL_SELECT_LIMIT=2 (prepare)");
7410
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_limit");
7413
rc= mysql_stmt_execute(stmt);
7414
check_execute(stmt, rc);
7416
rc= my_process_stmt_result(stmt);
7417
DIE_UNLESS(rc == 2);
7419
mysql_stmt_close(stmt);
7421
/* RESET the LIMIT the rows count to 0 */
7423
fprintf(stdout, "\n with SQL_SELECT_LIMIT=DEFAULT (prepare)");
7424
rc= mysql_query(mysql, "SET OPTION SQL_SELECT_LIMIT=DEFAULT");
7427
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_limit");
7430
rc= mysql_stmt_execute(stmt);
7431
check_execute(stmt, rc);
7433
rc= my_process_stmt_result(stmt);
7434
DIE_UNLESS(rc == 4);
7436
mysql_stmt_close(stmt);
7441
Test a misc GRANT option
7442
bug #89 (reported by mark@mysql.com)
7445
#ifndef EMBEDDED_LIBRARY
7446
static void test_prepare_grant()
7449
char query[MAX_TEST_QUERY_LENGTH];
7451
myheader("test_prepare_grant");
7453
mysql_autocommit(mysql, TRUE);
7455
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_grant");
7458
rc= mysql_query(mysql, "CREATE TABLE test_grant(a tinyint primary key auto_increment)");
7461
strxmov(query, "GRANT INSERT, UPDATE, SELECT ON ", current_db,
7462
".test_grant TO 'test_grant'@",
7463
opt_host ? opt_host : "'localhost'", NullS);
7465
if (mysql_query(mysql, query))
7467
myerror("GRANT failed");
7470
If server started with --skip-grant-tables, skip this test, else
7471
exit to indicate an error
7473
ER_UNKNOWN_COM_ERROR= 1047
7475
if (mysql_errno(mysql) != 1047)
7480
MYSQL *org_mysql= mysql, *lmysql;
7484
fprintf(stdout, "\n Establishing a test connection ...");
7485
if (!(lmysql= mysql_client_init(NULL)))
7487
myerror("mysql_client_init() failed");
7490
if (!(mysql_real_connect(lmysql, opt_host, "test_grant",
7491
"", current_db, opt_port,
7492
opt_unix_socket, 0)))
7494
myerror("connection failed");
7495
mysql_close(lmysql);
7498
lmysql->reconnect= 1;
7500
fprintf(stdout, "OK");
7503
rc= mysql_query(mysql, "INSERT INTO test_grant VALUES(NULL)");
7506
rc= mysql_query(mysql, "INSERT INTO test_grant(a) VALUES(NULL)");
7509
execute_prepare_query("INSERT INTO test_grant(a) VALUES(NULL)", 1);
7510
execute_prepare_query("INSERT INTO test_grant VALUES(NULL)", 1);
7511
execute_prepare_query("UPDATE test_grant SET a=9 WHERE a=1", 1);
7512
rc= my_stmt_result("SELECT a FROM test_grant");
7513
DIE_UNLESS(rc == 4);
7515
/* Both DELETE expected to fail as user does not have DELETE privs */
7517
rc= mysql_query(mysql, "DELETE FROM test_grant");
7520
stmt= mysql_simple_prepare(mysql, "DELETE FROM test_grant");
7523
rc= my_stmt_result("SELECT * FROM test_grant");
7524
DIE_UNLESS(rc == 4);
7526
mysql_close(lmysql);
7529
rc= mysql_query(mysql, "delete from mysql.user where User='test_grant'");
7531
DIE_UNLESS(1 == mysql_affected_rows(mysql));
7533
rc= mysql_query(mysql, "delete from mysql.tables_priv where User='test_grant'");
7535
DIE_UNLESS(1 == mysql_affected_rows(mysql));
7539
#endif /* EMBEDDED_LIBRARY */
7542
Test a crash when invalid/corrupted .frm is used in the
7544
bug #93 (reported by serg@mysql.com).
7547
static void test_frm_bug()
7550
MYSQL_BIND my_bind[2];
7554
char data_dir[FN_REFLEN];
7555
char test_frm[FN_REFLEN];
7558
myheader("test_frm_bug");
7560
mysql_autocommit(mysql, TRUE);
7562
rc= mysql_query(mysql, "drop table if exists test_frm_bug");
7565
rc= mysql_query(mysql, "flush tables");
7568
stmt= mysql_simple_prepare(mysql, "show variables like 'datadir'");
7571
rc= mysql_stmt_execute(stmt);
7572
check_execute(stmt, rc);
7574
bzero((char*) my_bind, sizeof(my_bind));
7575
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
7576
my_bind[0].buffer= data_dir;
7577
my_bind[0].buffer_length= FN_REFLEN;
7578
my_bind[1]= my_bind[0];
7580
rc= mysql_stmt_bind_result(stmt, my_bind);
7581
check_execute(stmt, rc);
7583
rc= mysql_stmt_fetch(stmt);
7584
check_execute(stmt, rc);
7587
fprintf(stdout, "\n data directory: %s", data_dir);
7589
rc= mysql_stmt_fetch(stmt);
7590
DIE_UNLESS(rc == MYSQL_NO_DATA);
7592
strxmov(test_frm, data_dir, "/", current_db, "/", "test_frm_bug.frm", NullS);
7595
fprintf(stdout, "\n test_frm: %s", test_frm);
7597
if (!(test_file= my_fopen(test_frm, (int) (O_RDWR | O_CREAT), MYF(MY_WME))))
7599
fprintf(stdout, "\n ERROR: my_fopen failed for '%s'", test_frm);
7600
fprintf(stdout, "\n test cancelled");
7604
fprintf(test_file, "this is a junk file for test");
7606
rc= mysql_query(mysql, "SHOW TABLE STATUS like 'test_frm_bug'");
7609
result= mysql_store_result(mysql);
7610
mytest(result);/* It can't be NULL */
7612
rc= my_process_result_set(result);
7613
DIE_UNLESS(rc == 1);
7615
mysql_data_seek(result, 0);
7617
row= mysql_fetch_row(result);
7621
fprintf(stdout, "\n Comment: %s", row[17]);
7622
DIE_UNLESS(row[17] != 0);
7624
mysql_free_result(result);
7625
mysql_stmt_close(stmt);
7627
my_fclose(test_file, MYF(0));
7628
mysql_query(mysql, "drop table if exists test_frm_bug");
7632
/* Test DECIMAL conversion */
7634
static void test_decimal_bug()
7637
MYSQL_BIND my_bind[1];
7642
myheader("test_decimal_bug");
7644
mysql_autocommit(mysql, TRUE);
7646
rc= mysql_query(mysql, "drop table if exists test_decimal_bug");
7649
rc= mysql_query(mysql, "create table test_decimal_bug(c1 decimal(10, 2))");
7652
rc= mysql_query(mysql, "insert into test_decimal_bug value(8), (10.22), (5.61)");
7655
stmt= mysql_simple_prepare(mysql, "select c1 from test_decimal_bug where c1= ?");
7659
We need to bzero bind structure because mysql_stmt_bind_param checks all
7662
bzero((char*) my_bind, sizeof(my_bind));
7664
my_bind[0].buffer_type= MYSQL_TYPE_NEWDECIMAL;
7665
my_bind[0].buffer= (void *)data;
7666
my_bind[0].buffer_length= 25;
7667
my_bind[0].is_null= &is_null;
7670
rc= mysql_stmt_bind_param(stmt, my_bind);
7671
check_execute(stmt, rc);
7673
strmov(data, "8.0");
7674
rc= mysql_stmt_execute(stmt);
7675
check_execute(stmt, rc);
7678
rc= mysql_stmt_bind_result(stmt, my_bind);
7679
check_execute(stmt, rc);
7681
rc= mysql_stmt_fetch(stmt);
7682
check_execute(stmt, rc);
7685
fprintf(stdout, "\n data: %s", data);
7686
DIE_UNLESS(strcmp(data, "8.00") == 0);
7688
rc= mysql_stmt_fetch(stmt);
7689
DIE_UNLESS(rc == MYSQL_NO_DATA);
7691
strmov(data, "5.61");
7692
rc= mysql_stmt_execute(stmt);
7693
check_execute(stmt, rc);
7696
rc= mysql_stmt_bind_result(stmt, my_bind);
7697
check_execute(stmt, rc);
7699
rc= mysql_stmt_fetch(stmt);
7700
check_execute(stmt, rc);
7703
fprintf(stdout, "\n data: %s", data);
7704
DIE_UNLESS(strcmp(data, "5.61") == 0);
7706
rc= mysql_stmt_fetch(stmt);
7707
DIE_UNLESS(rc == MYSQL_NO_DATA);
7710
rc= mysql_stmt_execute(stmt);
7711
check_execute(stmt, rc);
7713
rc= mysql_stmt_fetch(stmt);
7714
DIE_UNLESS(rc == MYSQL_NO_DATA);
7716
strmov(data, "10.22"); is_null= 0;
7717
rc= mysql_stmt_execute(stmt);
7718
check_execute(stmt, rc);
7721
rc= mysql_stmt_bind_result(stmt, my_bind);
7722
check_execute(stmt, rc);
7724
rc= mysql_stmt_fetch(stmt);
7725
check_execute(stmt, rc);
7728
fprintf(stdout, "\n data: %s", data);
7729
DIE_UNLESS(strcmp(data, "10.22") == 0);
7731
rc= mysql_stmt_fetch(stmt);
7732
DIE_UNLESS(rc == MYSQL_NO_DATA);
7734
mysql_stmt_close(stmt);
7738
/* Test EXPLAIN bug (#115, reported by mark@mysql.com & georg@php.net). */
7740
static void test_explain_bug()
7746
myheader("test_explain_bug");
7748
mysql_autocommit(mysql, TRUE);
7750
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_explain");
7753
rc= mysql_query(mysql, "CREATE TABLE test_explain(id int, name char(2))");
7756
stmt= mysql_simple_prepare(mysql, "explain test_explain");
7759
rc= mysql_stmt_execute(stmt);
7760
check_execute(stmt, rc);
7762
rc= my_process_stmt_result(stmt);
7763
DIE_UNLESS(rc == 2);
7765
result= mysql_stmt_result_metadata(stmt);
7769
fprintf(stdout, "\n total fields in the result: %d",
7770
mysql_num_fields(result));
7771
DIE_UNLESS(6 == mysql_num_fields(result));
7773
verify_prepare_field(result, 0, "Field", "COLUMN_NAME",
7774
mysql_get_server_version(mysql) <= 50000 ?
7775
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7778
verify_prepare_field(result, 1, "Type", "COLUMN_TYPE", MYSQL_TYPE_BLOB,
7781
verify_prepare_field(result, 2, "Null", "IS_NULLABLE",
7782
mysql_get_server_version(mysql) <= 50000 ?
7783
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7786
verify_prepare_field(result, 3, "Key", "COLUMN_KEY",
7787
mysql_get_server_version(mysql) <= 50000 ?
7788
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7791
if ( mysql_get_server_version(mysql) >= 50027 )
7793
/* The patch for bug#23037 changes column type of DEAULT to blob */
7794
verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
7795
MYSQL_TYPE_BLOB, 0, 0, "", 0, 0);
7799
verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
7800
mysql_get_server_version(mysql) >= 50027 ?
7802
mysql_get_server_version(mysql) <= 50000 ?
7803
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7805
mysql_get_server_version(mysql) >= 50027 ? 0 :64, 0);
7808
verify_prepare_field(result, 5, "Extra", "EXTRA",
7809
mysql_get_server_version(mysql) <= 50000 ?
7810
MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
7813
mysql_free_result(result);
7814
mysql_stmt_close(stmt);
7816
stmt= mysql_simple_prepare(mysql, "explain select id, name FROM test_explain");
7819
rc= mysql_stmt_execute(stmt);
7820
check_execute(stmt, rc);
7822
rc= my_process_stmt_result(stmt);
7823
DIE_UNLESS(rc == 1);
7825
result= mysql_stmt_result_metadata(stmt);
7829
fprintf(stdout, "\n total fields in the result: %d",
7830
mysql_num_fields(result));
7831
DIE_UNLESS(10 == mysql_num_fields(result));
7833
verify_prepare_field(result, 0, "id", "", MYSQL_TYPE_LONGLONG,
7836
verify_prepare_field(result, 1, "select_type", "", MYSQL_TYPE_VAR_STRING,
7839
verify_prepare_field(result, 2, "table", "", MYSQL_TYPE_VAR_STRING,
7840
"", "", "", NAME_CHAR_LEN, 0);
7842
verify_prepare_field(result, 3, "type", "", MYSQL_TYPE_VAR_STRING,
7845
verify_prepare_field(result, 4, "possible_keys", "", MYSQL_TYPE_VAR_STRING,
7846
"", "", "", NAME_CHAR_LEN*MAX_KEY, 0);
7848
verify_prepare_field(result, 5, "key", "", MYSQL_TYPE_VAR_STRING,
7849
"", "", "", NAME_CHAR_LEN, 0);
7851
if (mysql_get_server_version(mysql) <= 50000)
7853
verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_LONGLONG, "",
7858
verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_VAR_STRING, "",
7859
"", "", NAME_CHAR_LEN*MAX_KEY, 0);
7862
verify_prepare_field(result, 7, "ref", "", MYSQL_TYPE_VAR_STRING,
7863
"", "", "", NAME_CHAR_LEN*16, 0);
7865
verify_prepare_field(result, 8, "rows", "", MYSQL_TYPE_LONGLONG,
7868
verify_prepare_field(result, 9, "Extra", "", MYSQL_TYPE_VAR_STRING,
7869
"", "", "", 255, 0);
7871
mysql_free_result(result);
7872
mysql_stmt_close(stmt);
7875
#ifdef NOT_YET_WORKING
7878
Test math functions.
7879
Bug #148 (reported by salle@mysql.com).
7882
#define myerrno(n) check_errcode(n)
7884
static void check_errcode(const unsigned int err)
7886
if (!opt_silent || mysql_errno(mysql) != err)
7888
if (mysql->server_version)
7889
fprintf(stdout, "\n [MySQL-%s]", mysql->server_version);
7891
fprintf(stdout, "\n [MySQL]");
7892
fprintf(stdout, "[%d] %s\n", mysql_errno(mysql), mysql_error(mysql));
7894
DIE_UNLESS(mysql_errno(mysql) == err);
7898
static void test_drop_temp()
7902
myheader("test_drop_temp");
7904
rc= mysql_query(mysql, "DROP DATABASE IF EXISTS test_drop_temp_db");
7907
rc= mysql_query(mysql, "CREATE DATABASE test_drop_temp_db");
7910
rc= mysql_query(mysql, "CREATE TABLE test_drop_temp_db.t1(c1 int, c2 char(1))");
7913
rc= mysql_query(mysql, "delete from mysql.db where Db='test_drop_temp_db'");
7916
rc= mysql_query(mysql, "delete from mysql.db where Db='test_drop_temp_db'");
7919
strxmov(query, "GRANT SELECT, USAGE, DROP ON test_drop_temp_db.* TO test_temp@",
7920
opt_host ? opt_host : "localhost", NullS);
7922
if (mysql_query(mysql, query))
7924
myerror("GRANT failed");
7927
If server started with --skip-grant-tables, skip this test, else
7928
exit to indicate an error
7930
ER_UNKNOWN_COM_ERROR= 1047
7932
if (mysql_errno(mysql) != 1047)
7937
MYSQL *org_mysql= mysql, *lmysql;
7940
fprintf(stdout, "\n Establishing a test connection ...");
7941
if (!(lmysql= mysql_client_init(NULL)))
7943
myerror("mysql_client_init() failed");
7947
rc= mysql_query(mysql, "flush privileges");
7950
if (!(mysql_real_connect(lmysql, opt_host ? opt_host : "localhost", "test_temp",
7951
"", "test_drop_temp_db", opt_port,
7952
opt_unix_socket, 0)))
7955
myerror("connection failed");
7956
mysql_close(lmysql);
7959
lmysql->reconnect= 1;
7961
fprintf(stdout, "OK");
7964
rc= mysql_query(mysql, "INSERT INTO t1 VALUES(10, 'C')");
7965
myerrno((uint)1142);
7967
rc= mysql_query(mysql, "DROP TABLE t1");
7968
myerrno((uint)1142);
7971
rc= mysql_query(mysql, "CREATE TEMPORARY TABLE test_drop_temp_db.t1(c1 int)");
7974
rc= mysql_query(mysql, "CREATE TEMPORARY TABLE test_drop_temp_db.t2 LIKE test_drop_temp_db.t1");
7979
rc= mysql_query(mysql, "DROP TABLE t1, t2");
7982
rc= mysql_query(mysql, "DROP TEMPORARY TABLE t1");
7985
rc= mysql_query(mysql, "DROP TEMPORARY TABLE t2");
7988
mysql_close(lmysql);
7991
rc= mysql_query(mysql, "drop database test_drop_temp_db");
7993
DIE_UNLESS(1 == mysql_affected_rows(mysql));
7995
rc= mysql_query(mysql, "delete from mysql.user where User='test_temp'");
7997
DIE_UNLESS(1 == mysql_affected_rows(mysql));
8000
rc= mysql_query(mysql, "delete from mysql.tables_priv where User='test_temp'");
8002
DIE_UNLESS(1 == mysql_affected_rows(mysql));
8008
/* Test warnings for cuted rows */
8010
static void test_cuted_rows()
8015
myheader("test_cuted_rows");
8017
mysql_query(mysql, "DROP TABLE if exists t1");
8018
mysql_query(mysql, "DROP TABLE if exists t2");
8020
rc= mysql_query(mysql, "CREATE TABLE t1(c1 tinyint)");
8023
rc= mysql_query(mysql, "CREATE TABLE t2(c1 int not null)");
8026
rc= mysql_query(mysql, "INSERT INTO t1 values(10), (NULL), (NULL)");
8029
count= mysql_warning_count(mysql);
8031
fprintf(stdout, "\n total warnings: %d", count);
8032
DIE_UNLESS(count == 0);
8034
rc= mysql_query(mysql, "INSERT INTO t2 SELECT * FROM t1");
8037
count= mysql_warning_count(mysql);
8039
fprintf(stdout, "\n total warnings: %d", count);
8040
DIE_UNLESS(count == 2);
8042
rc= mysql_query(mysql, "SHOW WARNINGS");
8045
result= mysql_store_result(mysql);
8048
rc= my_process_result_set(result);
8049
DIE_UNLESS(rc == 2);
8050
mysql_free_result(result);
8052
rc= mysql_query(mysql, "INSERT INTO t1 VALUES('junk'), (876789)");
8055
count= mysql_warning_count(mysql);
8057
fprintf(stdout, "\n total warnings: %d", count);
8058
DIE_UNLESS(count == 2);
8060
rc= mysql_query(mysql, "SHOW WARNINGS");
8063
result= mysql_store_result(mysql);
8066
rc= my_process_result_set(result);
8067
DIE_UNLESS(rc == 2);
8068
mysql_free_result(result);
8072
/* Test update/binary logs */
8074
static void test_logs()
8077
MYSQL_BIND my_bind[2];
8083
myheader("test_logs");
8086
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_logs");
8089
rc= mysql_query(mysql, "CREATE TABLE test_logs(id smallint, name varchar(20))");
8092
strmov((char *)data, "INSERT INTO test_logs VALUES(?, ?)");
8093
stmt= mysql_simple_prepare(mysql, data);
8097
We need to bzero bind structure because mysql_stmt_bind_param checks all
8100
bzero((char*) my_bind, sizeof(my_bind));
8102
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
8103
my_bind[0].buffer= (void *)&id;
8105
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
8106
my_bind[1].buffer= (void *)&data;
8107
my_bind[1].buffer_length= 255;
8108
my_bind[1].length= &length;
8111
length= (ulong)(strmov((char *)data, "MySQL - Open Source Database")- data);
8113
rc= mysql_stmt_bind_param(stmt, my_bind);
8114
check_execute(stmt, rc);
8116
rc= mysql_stmt_execute(stmt);
8117
check_execute(stmt, rc);
8119
strmov((char *)data, "'");
8122
rc= mysql_stmt_execute(stmt);
8123
check_execute(stmt, rc);
8125
strmov((char *)data, "\"");
8128
rc= mysql_stmt_execute(stmt);
8129
check_execute(stmt, rc);
8131
length= (ulong)(strmov((char *)data, "my\'sql\'")-data);
8132
rc= mysql_stmt_execute(stmt);
8133
check_execute(stmt, rc);
8135
length= (ulong)(strmov((char *)data, "my\"sql\"")-data);
8136
rc= mysql_stmt_execute(stmt);
8137
check_execute(stmt, rc);
8139
mysql_stmt_close(stmt);
8141
strmov((char *)data, "INSERT INTO test_logs VALUES(20, 'mysql')");
8142
stmt= mysql_simple_prepare(mysql, data);
8145
rc= mysql_stmt_execute(stmt);
8146
check_execute(stmt, rc);
8148
rc= mysql_stmt_execute(stmt);
8149
check_execute(stmt, rc);
8151
mysql_stmt_close(stmt);
8153
strmov((char *)data, "SELECT * FROM test_logs WHERE id=?");
8154
stmt= mysql_simple_prepare(mysql, data);
8157
rc= mysql_stmt_bind_param(stmt, my_bind);
8158
check_execute(stmt, rc);
8160
rc= mysql_stmt_execute(stmt);
8161
check_execute(stmt, rc);
8163
my_bind[1].buffer_length= 255;
8164
rc= mysql_stmt_bind_result(stmt, my_bind);
8165
check_execute(stmt, rc);
8167
rc= mysql_stmt_fetch(stmt);
8168
check_execute(stmt, rc);
8172
fprintf(stdout, "id : %d\n", id);
8173
fprintf(stdout, "name : %s(%ld)\n", data, length);
8176
DIE_UNLESS(id == 9876);
8177
DIE_UNLESS(length == 19 || length == 20); /* Due to VARCHAR(20) */
8178
DIE_UNLESS(is_prefix(data, "MySQL - Open Source") == 1);
8180
rc= mysql_stmt_fetch(stmt);
8181
check_execute(stmt, rc);
8184
fprintf(stdout, "\n name : %s(%ld)", data, length);
8186
DIE_UNLESS(length == 1);
8187
DIE_UNLESS(strcmp(data, "'") == 0);
8189
rc= mysql_stmt_fetch(stmt);
8190
check_execute(stmt, rc);
8193
fprintf(stdout, "\n name : %s(%ld)", data, length);
8195
DIE_UNLESS(length == 1);
8196
DIE_UNLESS(strcmp(data, "\"") == 0);
8198
rc= mysql_stmt_fetch(stmt);
8199
check_execute(stmt, rc);
8202
fprintf(stdout, "\n name : %s(%ld)", data, length);
8204
DIE_UNLESS(length == 7);
8205
DIE_UNLESS(strcmp(data, "my\'sql\'") == 0);
8207
rc= mysql_stmt_fetch(stmt);
8208
check_execute(stmt, rc);
8211
fprintf(stdout, "\n name : %s(%ld)", data, length);
8213
DIE_UNLESS(length == 7);
8214
/*DIE_UNLESS(strcmp(data, "my\"sql\"") == 0); */
8216
rc= mysql_stmt_fetch(stmt);
8217
DIE_UNLESS(rc == MYSQL_NO_DATA);
8219
mysql_stmt_close(stmt);
8221
rc= mysql_query(mysql, "DROP TABLE test_logs");
8226
/* Test 'n' statements create and close */
8228
static void test_nstmts()
8233
static uint i, total_stmts= 2000;
8234
MYSQL_BIND my_bind[1];
8236
myheader("test_nstmts");
8238
mysql_autocommit(mysql, TRUE);
8240
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_nstmts");
8243
rc= mysql_query(mysql, "CREATE TABLE test_nstmts(id int)");
8247
We need to bzero bind structure because mysql_stmt_bind_param checks all
8250
bzero((char*) my_bind, sizeof(my_bind));
8252
my_bind[0].buffer= (void *)&i;
8253
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8255
for (i= 0; i < total_stmts; i++)
8258
fprintf(stdout, "\r stmt: %d", i);
8260
strmov(query, "insert into test_nstmts values(?)");
8261
stmt= mysql_simple_prepare(mysql, query);
8264
rc= mysql_stmt_bind_param(stmt, my_bind);
8265
check_execute(stmt, rc);
8267
rc= mysql_stmt_execute(stmt);
8268
check_execute(stmt, rc);
8270
mysql_stmt_close(stmt);
8273
stmt= mysql_simple_prepare(mysql, " select count(*) from test_nstmts");
8276
rc= mysql_stmt_execute(stmt);
8277
check_execute(stmt, rc);
8280
rc= mysql_stmt_bind_result(stmt, my_bind);
8281
check_execute(stmt, rc);
8283
rc= mysql_stmt_fetch(stmt);
8284
check_execute(stmt, rc);
8286
fprintf(stdout, "\n total rows: %d", i);
8287
DIE_UNLESS( i == total_stmts);
8289
rc= mysql_stmt_fetch(stmt);
8290
DIE_UNLESS(rc == MYSQL_NO_DATA);
8292
mysql_stmt_close(stmt);
8294
rc= mysql_query(mysql, "DROP TABLE test_nstmts");
8299
/* Test stmt seek() functions */
8301
static void test_fetch_seek()
8304
MYSQL_BIND my_bind[3];
8305
MYSQL_ROW_OFFSET row;
8308
char c2[11], c3[20];
8310
myheader("test_fetch_seek");
8311
rc= mysql_query(mysql, "drop table if exists t1");
8315
rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10), c3 timestamp(14))");
8318
rc= mysql_query(mysql, "insert into t1(c2) values('venu'), ('mysql'), ('open'), ('source')");
8321
stmt= mysql_simple_prepare(mysql, "select * from t1");
8324
bzero((char*) my_bind, sizeof(my_bind));
8325
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8326
my_bind[0].buffer= (void *)&c1;
8328
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
8329
my_bind[1].buffer= (void *)c2;
8330
my_bind[1].buffer_length= sizeof(c2);
8332
my_bind[2]= my_bind[1];
8333
my_bind[2].buffer= (void *)c3;
8334
my_bind[2].buffer_length= sizeof(c3);
8336
rc= mysql_stmt_execute(stmt);
8337
check_execute(stmt, rc);
8339
rc= mysql_stmt_bind_result(stmt, my_bind);
8340
check_execute(stmt, rc);
8342
rc= mysql_stmt_store_result(stmt);
8343
check_execute(stmt, rc);
8345
rc= mysql_stmt_fetch(stmt);
8346
check_execute(stmt, rc);
8349
fprintf(stdout, "\n row 0: %ld, %s, %s", (long) c1, c2, c3);
8351
row= mysql_stmt_row_tell(stmt);
8353
row= mysql_stmt_row_seek(stmt, row);
8355
rc= mysql_stmt_fetch(stmt);
8356
check_execute(stmt, rc);
8359
fprintf(stdout, "\n row 2: %ld, %s, %s", (long) c1, c2, c3);
8361
row= mysql_stmt_row_seek(stmt, row);
8363
rc= mysql_stmt_fetch(stmt);
8364
check_execute(stmt, rc);
8367
fprintf(stdout, "\n row 2: %ld, %s, %s", (long) c1, c2, c3);
8369
mysql_stmt_data_seek(stmt, 0);
8371
rc= mysql_stmt_fetch(stmt);
8372
check_execute(stmt, rc);
8375
fprintf(stdout, "\n row 0: %ld, %s, %s", (long) c1, c2, c3);
8377
rc= mysql_stmt_fetch(stmt);
8378
check_execute(stmt, rc);
8380
rc= mysql_stmt_fetch(stmt);
8381
check_execute(stmt, rc);
8383
rc= mysql_stmt_fetch(stmt);
8384
check_execute(stmt, rc);
8386
rc= mysql_stmt_fetch(stmt);
8387
DIE_UNLESS(rc == MYSQL_NO_DATA);
8389
mysql_stmt_close(stmt);
8390
myquery(mysql_query(mysql, "drop table t1"));
8394
/* Test mysql_stmt_fetch_column() with offset */
8396
static void test_fetch_offset()
8399
MYSQL_BIND my_bind[1];
8406
myheader("test_fetch_offset");
8408
rc= mysql_query(mysql, "drop table if exists t1");
8411
rc= mysql_query(mysql, "create table t1(a char(10))");
8414
rc= mysql_query(mysql, "insert into t1 values('abcdefghij'), (null)");
8417
stmt= mysql_simple_prepare(mysql, "select * from t1");
8420
bzero((char*) my_bind, sizeof(my_bind));
8421
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8422
my_bind[0].buffer= (void *)data;
8423
my_bind[0].buffer_length= 11;
8424
my_bind[0].is_null= &is_null;
8425
my_bind[0].length= &length;
8427
rc= mysql_stmt_execute(stmt);
8428
check_execute(stmt, rc);
8430
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8431
check_execute_r(stmt, rc);
8433
rc= mysql_stmt_execute(stmt);
8434
check_execute(stmt, rc);
8436
rc= mysql_stmt_bind_result(stmt, my_bind);
8437
check_execute(stmt, rc);
8439
rc= mysql_stmt_store_result(stmt);
8440
check_execute(stmt, rc);
8442
rc= mysql_stmt_fetch(stmt);
8443
check_execute(stmt, rc);
8446
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8447
check_execute(stmt, rc);
8449
fprintf(stdout, "\n col 1: %s (%ld)", data, length);
8450
DIE_UNLESS(strncmp(data, "abcd", 4) == 0 && length == 10);
8452
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 5);
8453
check_execute(stmt, rc);
8455
fprintf(stdout, "\n col 1: %s (%ld)", data, length);
8456
DIE_UNLESS(strncmp(data, "fg", 2) == 0 && length == 10);
8458
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 9);
8459
check_execute(stmt, rc);
8461
fprintf(stdout, "\n col 0: %s (%ld)", data, length);
8462
DIE_UNLESS(strncmp(data, "j", 1) == 0 && length == 10);
8464
rc= mysql_stmt_fetch(stmt);
8465
check_execute(stmt, rc);
8469
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8470
check_execute(stmt, rc);
8472
DIE_UNLESS(is_null == 1);
8474
rc= mysql_stmt_fetch(stmt);
8475
DIE_UNLESS(rc == MYSQL_NO_DATA);
8477
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8478
check_execute_r(stmt, rc);
8480
mysql_stmt_close(stmt);
8482
myquery(mysql_query(mysql, "drop table t1"));
8486
/* Test mysql_stmt_fetch_column() */
8488
static void test_fetch_column()
8491
MYSQL_BIND my_bind[2];
8492
char c2[20], bc2[20];
8493
ulong l1, l2, bl1, bl2;
8496
myheader("test_fetch_column");
8498
rc= mysql_query(mysql, "drop table if exists t1");
8501
rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10))");
8504
rc= mysql_query(mysql, "insert into t1(c2) values('venu'), ('mysql')");
8507
stmt= mysql_simple_prepare(mysql, "select * from t1 order by c2 desc");
8510
bzero((char*) my_bind, sizeof(my_bind));
8511
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8512
my_bind[0].buffer= (void *)&bc1;
8513
my_bind[0].buffer_length= 0;
8514
my_bind[0].is_null= 0;
8515
my_bind[0].length= &bl1;
8516
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
8517
my_bind[1].buffer= (void *)bc2;
8518
my_bind[1].buffer_length= 7;
8519
my_bind[1].is_null= 0;
8520
my_bind[1].length= &bl2;
8522
rc= mysql_stmt_execute(stmt);
8523
check_execute(stmt, rc);
8525
rc= mysql_stmt_bind_result(stmt, my_bind);
8526
check_execute(stmt, rc);
8528
rc= mysql_stmt_store_result(stmt);
8529
check_execute(stmt, rc);
8531
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0); /* No-op at this point */
8532
check_execute_r(stmt, rc);
8534
rc= mysql_stmt_fetch(stmt);
8535
check_execute(stmt, rc);
8538
fprintf(stdout, "\n row 0: %d, %s", bc1, bc2);
8541
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8542
my_bind[0].buffer= (void *)c2;
8543
my_bind[0].buffer_length= 7;
8544
my_bind[0].is_null= 0;
8545
my_bind[0].length= &l2;
8547
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8548
check_execute(stmt, rc);
8550
fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
8551
DIE_UNLESS(strncmp(c2, "venu", 4) == 0 && l2 == 4);
8554
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8555
check_execute(stmt, rc);
8557
fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
8558
DIE_UNLESS(strcmp(c2, "venu") == 0 && l2 == 4);
8561
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8562
my_bind[0].buffer= (void *)&c1;
8563
my_bind[0].buffer_length= 0;
8564
my_bind[0].is_null= 0;
8565
my_bind[0].length= &l1;
8567
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8568
check_execute(stmt, rc);
8570
fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
8571
DIE_UNLESS(c1 == 1 && l1 == 4);
8573
rc= mysql_stmt_fetch(stmt);
8574
check_execute(stmt, rc);
8577
fprintf(stdout, "\n row 1: %d, %s", bc1, bc2);
8580
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8581
my_bind[0].buffer= (void *)c2;
8582
my_bind[0].buffer_length= 7;
8583
my_bind[0].is_null= 0;
8584
my_bind[0].length= &l2;
8586
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8587
check_execute(stmt, rc);
8589
fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
8590
DIE_UNLESS(strncmp(c2, "mysq", 4) == 0 && l2 == 5);
8593
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8594
check_execute(stmt, rc);
8596
fprintf(stdout, "\n col 1: %si(%ld)", c2, l2);
8597
DIE_UNLESS(strcmp(c2, "mysql") == 0 && l2 == 5);
8600
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8601
my_bind[0].buffer= (void *)&c1;
8602
my_bind[0].buffer_length= 0;
8603
my_bind[0].is_null= 0;
8604
my_bind[0].length= &l1;
8606
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8607
check_execute(stmt, rc);
8609
fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
8610
DIE_UNLESS(c1 == 2 && l1 == 4);
8612
rc= mysql_stmt_fetch(stmt);
8613
DIE_UNLESS(rc == MYSQL_NO_DATA);
8615
rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
8616
check_execute_r(stmt, rc);
8618
mysql_stmt_close(stmt);
8619
myquery(mysql_query(mysql, "drop table t1"));
8623
/* Test mysql_list_fields() */
8625
static void test_list_fields()
8629
myheader("test_list_fields");
8631
rc= mysql_query(mysql, "drop table if exists t1");
8634
rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10) default 'mysql')");
8637
result= mysql_list_fields(mysql, "t1", NULL);
8640
rc= my_process_result_set(result);
8641
DIE_UNLESS(rc == 0);
8643
verify_prepare_field(result, 0, "c1", "c1", MYSQL_TYPE_LONG,
8645
current_db, 11, "0");
8647
verify_prepare_field(result, 1, "c2", "c2", MYSQL_TYPE_STRING,
8649
current_db, 10, "mysql");
8651
mysql_free_result(result);
8652
myquery(mysql_query(mysql, "drop table t1"));
8656
static void test_bug19671()
8660
myheader("test_bug19671");
8662
mysql_query(mysql, "set sql_mode=''");
8663
rc= mysql_query(mysql, "drop table if exists t1");
8666
rc= mysql_query(mysql, "drop view if exists v1");
8669
rc= mysql_query(mysql, "create table t1(f1 int)");
8672
rc= mysql_query(mysql, "create view v1 as select va.* from t1 va");
8675
result= mysql_list_fields(mysql, "v1", NULL);
8678
rc= my_process_result_set(result);
8679
DIE_UNLESS(rc == 0);
8681
verify_prepare_field(result, 0, "f1", "f1", MYSQL_TYPE_LONG,
8682
"v1", "v1", current_db, 11, "0");
8684
mysql_free_result(result);
8685
myquery(mysql_query(mysql, "drop view v1"));
8686
myquery(mysql_query(mysql, "drop table t1"));
8690
/* Test a memory ovverun bug */
8692
static void test_mem_overun()
8694
char buffer[10000], field[10];
8696
MYSQL_RES *field_res;
8699
myheader("test_mem_overun");
8702
Test a memory ovverun bug when a table had 1000 fields with
8705
rc= mysql_query(mysql, "drop table if exists t_mem_overun");
8708
strxmov(buffer, "create table t_mem_overun(", NullS);
8709
for (i= 0; i < 1000; i++)
8711
sprintf(field, "c%d int", i);
8712
strxmov(buffer, buffer, field, ", ", NullS);
8714
length= strlen(buffer);
8715
buffer[length-2]= ')';
8716
buffer[--length]= '\0';
8718
rc= mysql_real_query(mysql, buffer, length);
8721
strxmov(buffer, "insert into t_mem_overun values(", NullS);
8722
for (i= 0; i < 1000; i++)
8724
strxmov(buffer, buffer, "1, ", NullS);
8726
length= strlen(buffer);
8727
buffer[length-2]= ')';
8728
buffer[--length]= '\0';
8730
rc= mysql_real_query(mysql, buffer, length);
8733
rc= mysql_query(mysql, "select * from t_mem_overun");
8736
rc= my_process_result(mysql);
8737
DIE_UNLESS(rc == 1);
8739
stmt= mysql_simple_prepare(mysql, "select * from t_mem_overun");
8742
rc= mysql_stmt_execute(stmt);
8743
check_execute(stmt, rc);
8745
field_res= mysql_stmt_result_metadata(stmt);
8749
fprintf(stdout, "\n total fields : %d", mysql_num_fields(field_res));
8750
DIE_UNLESS( 1000 == mysql_num_fields(field_res));
8752
rc= mysql_stmt_store_result(stmt);
8753
check_execute(stmt, rc);
8755
rc= mysql_stmt_fetch(stmt);
8756
check_execute(stmt, rc);
8758
rc= mysql_stmt_fetch(stmt);
8759
DIE_UNLESS(rc == MYSQL_NO_DATA);
8761
mysql_free_result(field_res);
8763
mysql_stmt_close(stmt);
8767
/* Test mysql_stmt_free_result() */
8769
static void test_free_result()
8772
MYSQL_BIND my_bind[1];
8777
myheader("test_free_result");
8779
rc= mysql_query(mysql, "drop table if exists test_free_result");
8782
rc= mysql_query(mysql, "create table test_free_result("
8783
"c1 int primary key auto_increment)");
8786
rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
8789
stmt= mysql_simple_prepare(mysql, "select * from test_free_result");
8792
bzero((char*) my_bind, sizeof(my_bind));
8793
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8794
my_bind[0].buffer= (void *)&bc1;
8795
my_bind[0].length= &bl1;
8797
rc= mysql_stmt_execute(stmt);
8798
check_execute(stmt, rc);
8800
rc= mysql_stmt_bind_result(stmt, my_bind);
8801
check_execute(stmt, rc);
8803
rc= mysql_stmt_fetch(stmt);
8804
check_execute(stmt, rc);
8807
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8808
my_bind[0].buffer= (void *)c2;
8809
my_bind[0].buffer_length= 7;
8810
my_bind[0].is_null= 0;
8811
my_bind[0].length= &l2;
8813
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8814
check_execute(stmt, rc);
8816
fprintf(stdout, "\n col 0: %s(%ld)", c2, l2);
8817
DIE_UNLESS(strncmp(c2, "1", 1) == 0 && l2 == 1);
8819
rc= mysql_stmt_fetch(stmt);
8820
check_execute(stmt, rc);
8823
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8824
my_bind[0].buffer= (void *)&c1;
8825
my_bind[0].buffer_length= 0;
8826
my_bind[0].is_null= 0;
8827
my_bind[0].length= &l2;
8829
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8830
check_execute(stmt, rc);
8832
fprintf(stdout, "\n col 0: %d(%ld)", c1, l2);
8833
DIE_UNLESS(c1 == 2 && l2 == 4);
8835
rc= mysql_query(mysql, "drop table test_free_result");
8836
myquery_r(rc); /* error should be, COMMANDS OUT OF SYNC */
8838
rc= mysql_stmt_free_result(stmt);
8839
check_execute(stmt, rc);
8841
rc= mysql_query(mysql, "drop table test_free_result");
8842
myquery(rc); /* should be successful */
8844
mysql_stmt_close(stmt);
8848
/* Test mysql_stmt_store_result() */
8850
static void test_free_store_result()
8853
MYSQL_BIND my_bind[1];
8858
myheader("test_free_store_result");
8860
rc= mysql_query(mysql, "drop table if exists test_free_result");
8863
rc= mysql_query(mysql, "create table test_free_result(c1 int primary key auto_increment)");
8866
rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
8869
stmt= mysql_simple_prepare(mysql, "select * from test_free_result");
8872
bzero((char*) my_bind, sizeof(my_bind));
8873
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8874
my_bind[0].buffer= (void *)&bc1;
8875
my_bind[0].buffer_length= 0;
8876
my_bind[0].is_null= 0;
8877
my_bind[0].length= &bl1;
8879
rc= mysql_stmt_execute(stmt);
8880
check_execute(stmt, rc);
8882
rc= mysql_stmt_bind_result(stmt, my_bind);
8883
check_execute(stmt, rc);
8885
rc= mysql_stmt_store_result(stmt);
8886
check_execute(stmt, rc);
8888
rc= mysql_stmt_fetch(stmt);
8889
check_execute(stmt, rc);
8892
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8893
my_bind[0].buffer= (void *)c2;
8894
my_bind[0].buffer_length= 7;
8895
my_bind[0].is_null= 0;
8896
my_bind[0].length= &l2;
8898
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8899
check_execute(stmt, rc);
8901
fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
8902
DIE_UNLESS(strncmp(c2, "1", 1) == 0 && l2 == 1);
8904
rc= mysql_stmt_fetch(stmt);
8905
check_execute(stmt, rc);
8908
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
8909
my_bind[0].buffer= (void *)&c1;
8910
my_bind[0].buffer_length= 0;
8911
my_bind[0].is_null= 0;
8912
my_bind[0].length= &l2;
8914
rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
8915
check_execute(stmt, rc);
8917
fprintf(stdout, "\n col 0: %d(%ld)", c1, l2);
8918
DIE_UNLESS(c1 == 2 && l2 == 4);
8920
rc= mysql_stmt_free_result(stmt);
8921
check_execute(stmt, rc);
8923
rc= mysql_query(mysql, "drop table test_free_result");
8926
mysql_stmt_close(stmt);
8932
static void test_sqlmode()
8935
MYSQL_BIND my_bind[2];
8938
char query[MAX_TEST_QUERY_LENGTH];
8940
myheader("test_sqlmode");
8942
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_piping");
8945
rc= mysql_query(mysql, "CREATE TABLE test_piping(name varchar(10))");
8948
/* PIPES_AS_CONCAT */
8949
strmov(query, "SET SQL_MODE= \"PIPES_AS_CONCAT\"");
8951
fprintf(stdout, "\n With %s", query);
8952
rc= mysql_query(mysql, query);
8955
strmov(query, "INSERT INTO test_piping VALUES(?||?)");
8957
fprintf(stdout, "\n query: %s", query);
8958
stmt= mysql_simple_prepare(mysql, query);
8962
fprintf(stdout, "\n total parameters: %ld", mysql_stmt_param_count(stmt));
8965
We need to bzero bind structure because mysql_stmt_bind_param checks all
8968
bzero((char*) my_bind, sizeof(my_bind));
8970
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
8971
my_bind[0].buffer= (void *)c1;
8972
my_bind[0].buffer_length= 2;
8974
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
8975
my_bind[1].buffer= (void *)c2;
8976
my_bind[1].buffer_length= 3;
8978
rc= mysql_stmt_bind_param(stmt, my_bind);
8979
check_execute(stmt, rc);
8981
strmov(c1, "My"); strmov(c2, "SQL");
8982
rc= mysql_stmt_execute(stmt);
8983
check_execute(stmt, rc);
8984
mysql_stmt_close(stmt);
8986
verify_col_data("test_piping", "name", "MySQL");
8988
rc= mysql_query(mysql, "DELETE FROM test_piping");
8991
strmov(query, "SELECT connection_id ()");
8993
fprintf(stdout, "\n query: %s", query);
8994
stmt= mysql_simple_prepare(mysql, query);
8996
mysql_stmt_close(stmt);
8999
strmov(query, "SET SQL_MODE= \"ANSI\"");
9001
fprintf(stdout, "\n With %s", query);
9002
rc= mysql_query(mysql, query);
9005
strmov(query, "INSERT INTO test_piping VALUES(?||?)");
9007
fprintf(stdout, "\n query: %s", query);
9008
stmt= mysql_simple_prepare(mysql, query);
9011
fprintf(stdout, "\n total parameters: %ld", mysql_stmt_param_count(stmt));
9013
rc= mysql_stmt_bind_param(stmt, my_bind);
9014
check_execute(stmt, rc);
9016
strmov(c1, "My"); strmov(c2, "SQL");
9017
rc= mysql_stmt_execute(stmt);
9018
check_execute(stmt, rc);
9020
mysql_stmt_close(stmt);
9021
verify_col_data("test_piping", "name", "MySQL");
9023
/* ANSI mode spaces ... */
9024
strmov(query, "SELECT connection_id ()");
9026
fprintf(stdout, "\n query: %s", query);
9027
stmt= mysql_simple_prepare(mysql, query);
9030
rc= mysql_stmt_execute(stmt);
9031
check_execute(stmt, rc);
9033
rc= mysql_stmt_fetch(stmt);
9034
check_execute(stmt, rc);
9036
rc= mysql_stmt_fetch(stmt);
9037
DIE_UNLESS(rc == MYSQL_NO_DATA);
9039
fprintf(stdout, "\n returned 1 row\n");
9041
mysql_stmt_close(stmt);
9043
/* IGNORE SPACE MODE */
9044
strmov(query, "SET SQL_MODE= \"IGNORE_SPACE\"");
9046
fprintf(stdout, "\n With %s", query);
9047
rc= mysql_query(mysql, query);
9050
strmov(query, "SELECT connection_id ()");
9052
fprintf(stdout, "\n query: %s", query);
9053
stmt= mysql_simple_prepare(mysql, query);
9056
rc= mysql_stmt_execute(stmt);
9057
check_execute(stmt, rc);
9059
rc= mysql_stmt_fetch(stmt);
9060
check_execute(stmt, rc);
9062
rc= mysql_stmt_fetch(stmt);
9063
DIE_UNLESS(rc == MYSQL_NO_DATA);
9065
fprintf(stdout, "\n returned 1 row");
9067
mysql_stmt_close(stmt);
9071
/* Test for timestamp handling */
9073
static void test_ts()
9076
MYSQL_BIND my_bind[6];
9078
MYSQL_RES *prep_res;
9081
int rc, field_count;
9083
char query[MAX_TEST_QUERY_LENGTH];
9084
const char *queries [3]= {"SELECT a, b, c FROM test_ts WHERE %c=?",
9085
"SELECT a, b, c FROM test_ts WHERE %c=?",
9086
"SELECT a, b, c FROM test_ts WHERE %c=CAST(? AS DATE)"};
9087
myheader("test_ts");
9089
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ts");
9092
rc= mysql_query(mysql, "CREATE TABLE test_ts(a DATE, b TIME, c TIMESTAMP)");
9095
stmt= mysql_simple_prepare(mysql, "INSERT INTO test_ts VALUES(?, ?, ?), (?, ?, ?)");
9105
length= (long)(strmov(strts, "2003-07-12 21:07:46") - strts);
9108
We need to bzero bind structure because mysql_stmt_bind_param checks all
9111
bzero((char*) my_bind, sizeof(my_bind));
9113
my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
9114
my_bind[0].buffer= (void *)&ts;
9115
my_bind[0].buffer_length= sizeof(ts);
9117
my_bind[2]= my_bind[1]= my_bind[0];
9119
my_bind[3].buffer_type= MYSQL_TYPE_STRING;
9120
my_bind[3].buffer= (void *)strts;
9121
my_bind[3].buffer_length= sizeof(strts);
9122
my_bind[3].length= &length;
9124
my_bind[5]= my_bind[4]= my_bind[3];
9126
rc= mysql_stmt_bind_param(stmt, my_bind);
9127
check_execute(stmt, rc);
9129
rc= mysql_stmt_execute(stmt);
9130
check_execute(stmt, rc);
9132
mysql_stmt_close(stmt);
9134
verify_col_data("test_ts", "a", "2003-07-12");
9135
verify_col_data("test_ts", "b", "21:07:46");
9136
verify_col_data("test_ts", "c", "2003-07-12 21:07:46");
9138
stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_ts");
9141
prep_res= mysql_stmt_result_metadata(stmt);
9144
rc= mysql_stmt_execute(stmt);
9145
check_execute(stmt, rc);
9147
rc= my_process_stmt_result(stmt);
9148
DIE_UNLESS(rc == 2);
9149
field_count= mysql_num_fields(prep_res);
9151
mysql_free_result(prep_res);
9152
mysql_stmt_close(stmt);
9154
for (name= 'a'; field_count--; name++)
9158
sprintf(query, queries[field_count], name);
9161
fprintf(stdout, "\n %s", query);
9162
stmt= mysql_simple_prepare(mysql, query);
9165
rc= mysql_stmt_bind_param(stmt, my_bind);
9166
check_execute(stmt, rc);
9168
rc= mysql_stmt_execute(stmt);
9169
check_execute(stmt, rc);
9171
while (mysql_stmt_fetch(stmt) == 0)
9175
fprintf(stdout, "\n returned '%d' rows", row_count);
9176
DIE_UNLESS(row_count == 2);
9177
mysql_stmt_close(stmt);
9182
/* Test for bug #1500. */
9184
static void test_bug1500()
9187
MYSQL_BIND my_bind[3];
9189
int32 int_data[3]= {2, 3, 4};
9192
myheader("test_bug1500");
9194
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bg1500");
9197
rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (i INT)");
9200
rc= mysql_query(mysql, "INSERT INTO test_bg1500 VALUES (1), (2)");
9203
rc= mysql_commit(mysql);
9206
stmt= mysql_simple_prepare(mysql, "SELECT i FROM test_bg1500 WHERE i IN (?, ?, ?)");
9208
verify_param_count(stmt, 3);
9211
We need to bzero bind structure because mysql_stmt_bind_param checks all
9214
bzero((char*) my_bind, sizeof(my_bind));
9216
my_bind[0].buffer= (void *)int_data;
9217
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
9218
my_bind[2]= my_bind[1]= my_bind[0];
9219
my_bind[1].buffer= (void *)(int_data + 1);
9220
my_bind[2].buffer= (void *)(int_data + 2);
9222
rc= mysql_stmt_bind_param(stmt, my_bind);
9223
check_execute(stmt, rc);
9225
rc= mysql_stmt_execute(stmt);
9226
check_execute(stmt, rc);
9228
rc= my_process_stmt_result(stmt);
9229
DIE_UNLESS(rc == 1);
9231
mysql_stmt_close(stmt);
9233
rc= mysql_query(mysql, "DROP TABLE test_bg1500");
9236
rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (s VARCHAR(25), FULLTEXT(s)) engine=MyISAM");
9239
rc= mysql_query(mysql,
9240
"INSERT INTO test_bg1500 VALUES ('Gravedigger'), ('Greed'), ('Hollow Dogs')");
9243
rc= mysql_commit(mysql);
9246
stmt= mysql_simple_prepare(mysql,
9247
"SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (?)");
9250
verify_param_count(stmt, 1);
9253
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
9254
my_bind[0].buffer= (void *) data;
9255
my_bind[0].buffer_length= strlen(data);
9256
my_bind[0].is_null= 0;
9257
my_bind[0].length= 0;
9259
rc= mysql_stmt_bind_param(stmt, my_bind);
9260
check_execute(stmt, rc);
9262
rc= mysql_stmt_execute(stmt);
9263
check_execute(stmt, rc);
9265
rc= my_process_stmt_result(stmt);
9266
DIE_UNLESS(rc == 1);
9268
mysql_stmt_close(stmt);
9270
/* This should work too */
9271
stmt= mysql_simple_prepare(mysql,
9272
"SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (CONCAT(?, 'digger'))");
9275
verify_param_count(stmt, 1);
9278
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
9279
my_bind[0].buffer= (void *) data;
9280
my_bind[0].buffer_length= strlen(data);
9282
rc= mysql_stmt_bind_param(stmt, my_bind);
9283
check_execute(stmt, rc);
9285
rc= mysql_stmt_execute(stmt);
9286
check_execute(stmt, rc);
9288
rc= my_process_stmt_result(stmt);
9289
DIE_UNLESS(rc == 1);
9291
mysql_stmt_close(stmt);
9295
static void test_bug1946()
9299
const char *query= "INSERT INTO prepare_command VALUES (?)";
9301
myheader("test_bug1946");
9303
rc= mysql_query(mysql, "DROP TABLE IF EXISTS prepare_command");
9306
rc= mysql_query(mysql, "CREATE TABLE prepare_command(ID INT)");
9309
stmt= mysql_simple_prepare(mysql, query);
9311
rc= mysql_real_query(mysql, query, strlen(query));
9312
DIE_UNLESS(rc != 0);
9314
fprintf(stdout, "Got error (as expected):\n");
9317
mysql_stmt_close(stmt);
9318
rc= mysql_query(mysql, "DROP TABLE prepare_command");
9322
static void test_parse_error_and_bad_length()
9327
/* check that we get 4 syntax errors over the 4 calls */
9328
myheader("test_parse_error_and_bad_length");
9330
rc= mysql_query(mysql, "SHOW DATABAAAA");
9333
fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
9334
rc= mysql_real_query(mysql, "SHOW DATABASES", 100);
9337
fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
9339
stmt= mysql_simple_prepare(mysql, "SHOW DATABAAAA");
9342
fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
9343
stmt= mysql_stmt_init(mysql);
9345
rc= mysql_stmt_prepare(stmt, "SHOW DATABASES", 100);
9346
DIE_UNLESS(rc != 0);
9348
fprintf(stdout, "Got error (as expected): '%s'\n", mysql_stmt_error(stmt));
9349
mysql_stmt_close(stmt);
9353
static void test_bug2247()
9359
const char *create= "CREATE TABLE bug2247(id INT UNIQUE AUTO_INCREMENT)";
9360
const char *insert= "INSERT INTO bug2247 VALUES (NULL)";
9361
const char *SELECT= "SELECT id FROM bug2247";
9362
const char *update= "UPDATE bug2247 SET id=id+10";
9363
const char *drop= "DROP TABLE IF EXISTS bug2247";
9364
ulonglong exp_count;
9365
enum { NUM_ROWS= 5 };
9367
myheader("test_bug2247");
9370
fprintf(stdout, "\nChecking if stmt_affected_rows is not affected by\n"
9371
"mysql_query ... ");
9372
/* create table and insert few rows */
9373
rc= mysql_query(mysql, drop);
9376
rc= mysql_query(mysql, create);
9379
stmt= mysql_simple_prepare(mysql, insert);
9381
for (i= 0; i < NUM_ROWS; ++i)
9383
rc= mysql_stmt_execute(stmt);
9384
check_execute(stmt, rc);
9386
exp_count= mysql_stmt_affected_rows(stmt);
9387
DIE_UNLESS(exp_count == 1);
9389
rc= mysql_query(mysql, SELECT);
9392
mysql_store_result overwrites mysql->affected_rows. Check that
9393
mysql_stmt_affected_rows() returns the same value, whereas
9394
mysql_affected_rows() value is correct.
9396
res= mysql_store_result(mysql);
9399
DIE_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS);
9400
DIE_UNLESS(exp_count == mysql_stmt_affected_rows(stmt));
9402
rc= mysql_query(mysql, update);
9404
DIE_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS);
9405
DIE_UNLESS(exp_count == mysql_stmt_affected_rows(stmt));
9407
mysql_free_result(res);
9408
mysql_stmt_close(stmt);
9410
/* check that mysql_stmt_store_result modifies mysql_stmt_affected_rows */
9411
stmt= mysql_simple_prepare(mysql, SELECT);
9414
rc= mysql_stmt_execute(stmt);
9415
check_execute(stmt, rc);
9416
rc= mysql_stmt_store_result(stmt);
9417
check_execute(stmt, rc);
9418
exp_count= mysql_stmt_affected_rows(stmt);
9419
DIE_UNLESS(exp_count == NUM_ROWS);
9421
rc= mysql_query(mysql, insert);
9423
DIE_UNLESS(mysql_affected_rows(mysql) == 1);
9424
DIE_UNLESS(mysql_stmt_affected_rows(stmt) == exp_count);
9426
mysql_stmt_close(stmt);
9428
fprintf(stdout, "OK");
9432
static void test_subqueries()
9436
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";
9438
myheader("test_subqueries");
9440
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
9443
rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
9446
rc= mysql_query(mysql,
9447
"insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
9450
rc= mysql_query(mysql, "create table t2 select * from t1;");
9453
stmt= mysql_simple_prepare(mysql, query);
9455
for (i= 0; i < 3; i++)
9457
rc= mysql_stmt_execute(stmt);
9458
check_execute(stmt, rc);
9459
rc= my_process_stmt_result(stmt);
9460
DIE_UNLESS(rc == 5);
9462
mysql_stmt_close(stmt);
9464
rc= mysql_query(mysql, "DROP TABLE t1, t2");
9469
static void test_bad_union()
9472
const char *query= "SELECT 1, 2 union SELECT 1";
9474
myheader("test_bad_union");
9476
stmt= mysql_simple_prepare(mysql, query);
9477
DIE_UNLESS(stmt == 0);
9482
static void test_distinct()
9487
"SELECT 2+count(distinct b), group_concat(a) FROM t1 group by a";
9489
myheader("test_distinct");
9491
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
9494
rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
9497
rc= mysql_query(mysql,
9498
"insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), \
9499
(1, 10), (2, 20), (3, 30), (4, 40), (5, 50);");
9502
for (i= 0; i < 3; i++)
9504
stmt= mysql_simple_prepare(mysql, query);
9506
rc= mysql_stmt_execute(stmt);
9507
check_execute(stmt, rc);
9508
rc= my_process_stmt_result(stmt);
9509
DIE_UNLESS(rc == 5);
9510
mysql_stmt_close(stmt);
9513
rc= mysql_query(mysql, "DROP TABLE t1");
9519
Test for bug#2248 "mysql_fetch without prior mysql_stmt_execute hangs"
9522
static void test_bug2248()
9526
const char *query1= "SELECT DATABASE()";
9527
const char *query2= "INSERT INTO test_bug2248 VALUES (10)";
9529
myheader("test_bug2248");
9531
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bug2248");
9534
rc= mysql_query(mysql, "CREATE TABLE test_bug2248 (id int)");
9537
stmt= mysql_simple_prepare(mysql, query1);
9540
/* This should not hang */
9541
rc= mysql_stmt_fetch(stmt);
9542
check_execute_r(stmt, rc);
9545
rc= mysql_stmt_store_result(stmt);
9546
check_execute_r(stmt, rc);
9548
mysql_stmt_close(stmt);
9550
stmt= mysql_simple_prepare(mysql, query2);
9553
rc= mysql_stmt_execute(stmt);
9554
check_execute(stmt, rc);
9556
/* This too should not hang but should return proper error */
9557
rc= mysql_stmt_fetch(stmt);
9558
DIE_UNLESS(rc == 1);
9560
/* This too should not hang but should not bark */
9561
rc= mysql_stmt_store_result(stmt);
9562
check_execute(stmt, rc);
9564
/* This should return proper error */
9565
rc= mysql_stmt_fetch(stmt);
9566
check_execute_r(stmt, rc);
9567
DIE_UNLESS(rc == 1);
9569
mysql_stmt_close(stmt);
9571
rc= mysql_query(mysql, "DROP TABLE test_bug2248");
9576
static void test_subqueries_ref()
9580
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)";
9582
myheader("test_subqueries_ref");
9584
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
9587
rc= mysql_query(mysql, "CREATE TABLE t1 (a int);");
9590
rc= mysql_query(mysql,
9591
"insert into t1 values (1), (2), (3), (4), (5);");
9594
stmt= mysql_simple_prepare(mysql, query);
9596
for (i= 0; i < 3; i++)
9598
rc= mysql_stmt_execute(stmt);
9599
check_execute(stmt, rc);
9600
rc= my_process_stmt_result(stmt);
9601
DIE_UNLESS(rc == 1);
9603
mysql_stmt_close(stmt);
9605
rc= mysql_query(mysql, "DROP TABLE t1");
9610
static void test_union()
9615
myheader("test_union");
9617
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
9620
rc= mysql_query(mysql,
9622
"(id INTEGER NOT NULL PRIMARY KEY, "
9623
" name VARCHAR(20) NOT NULL)");
9625
rc= mysql_query(mysql,
9626
"INSERT INTO t1 (id, name) VALUES "
9627
"(2, 'Ja'), (3, 'Ede'), "
9628
"(4, 'Haag'), (5, 'Kabul'), "
9629
"(6, 'Almere'), (7, 'Utrecht'), "
9630
"(8, 'Qandahar'), (9, 'Amsterdam'), "
9631
"(10, 'Amersfoort'), (11, 'Constantine')");
9633
rc= mysql_query(mysql,
9635
"(id INTEGER NOT NULL PRIMARY KEY, "
9636
" name VARCHAR(20) NOT NULL)");
9638
rc= mysql_query(mysql,
9639
"INSERT INTO t2 (id, name) VALUES "
9640
"(4, 'Guam'), (5, 'Aruba'), "
9641
"(6, 'Angola'), (7, 'Albania'), "
9642
"(8, 'Anguilla'), (9, 'Argentina'), "
9643
"(10, 'Azerbaijan'), (11, 'Afghanistan'), "
9644
"(12, 'Burkina Faso'), (13, 'Faroe Islands')");
9647
stmt= mysql_simple_prepare(mysql,
9648
"SELECT t1.name FROM t1 UNION "
9649
"SELECT t2.name FROM t2");
9652
rc= mysql_stmt_execute(stmt);
9653
check_execute(stmt, rc);
9654
rc= my_process_stmt_result(stmt);
9655
DIE_UNLESS(rc == 20);
9656
mysql_stmt_close(stmt);
9658
rc= mysql_query(mysql, "DROP TABLE t1, t2");
9663
static void test_bug3117()
9672
myheader("test_bug3117");
9674
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
9677
rc= mysql_query(mysql, "CREATE TABLE t1 (id int auto_increment primary key)");
9680
stmt= mysql_simple_prepare(mysql, "SELECT LAST_INSERT_ID()");
9683
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
9686
rc= mysql_stmt_execute(stmt);
9687
check_execute(stmt, rc);
9689
bzero((char*) &buffer, sizeof(buffer));
9690
buffer.buffer_type= MYSQL_TYPE_LONGLONG;
9691
buffer.buffer_length= sizeof(lii);
9692
buffer.buffer= (void *)&lii;
9693
buffer.length= &length;
9694
buffer.is_null= &is_null;
9696
rc= mysql_stmt_bind_result(stmt, &buffer);
9697
check_execute(stmt, rc);
9699
rc= mysql_stmt_store_result(stmt);
9700
check_execute(stmt, rc);
9702
rc= mysql_stmt_fetch(stmt);
9703
check_execute(stmt, rc);
9705
DIE_UNLESS(is_null == 0 && lii == 1);
9707
fprintf(stdout, "\n\tLAST_INSERT_ID()= 1 ok\n");
9709
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
9712
rc= mysql_stmt_execute(stmt);
9713
check_execute(stmt, rc);
9715
rc= mysql_stmt_fetch(stmt);
9716
check_execute(stmt, rc);
9718
DIE_UNLESS(is_null == 0 && lii == 2);
9720
fprintf(stdout, "\tLAST_INSERT_ID()= 2 ok\n");
9722
mysql_stmt_close(stmt);
9724
rc= mysql_query(mysql, "DROP TABLE t1");
9729
static void test_join()
9733
const char *query[]= {"SELECT * FROM t2 join t1 on (t1.a=t2.a)",
9734
"SELECT * FROM t2 natural join t1",
9735
"SELECT * FROM t2 join t1 using(a)",
9736
"SELECT * FROM t2 left join t1 on(t1.a=t2.a)",
9737
"SELECT * FROM t2 natural left join t1",
9738
"SELECT * FROM t2 left join t1 using(a)",
9739
"SELECT * FROM t2 right join t1 on(t1.a=t2.a)",
9740
"SELECT * FROM t2 natural right join t1",
9741
"SELECT * FROM t2 right join t1 using(a)"};
9743
myheader("test_join");
9745
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
9748
rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
9751
rc= mysql_query(mysql,
9752
"insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
9755
rc= mysql_query(mysql, "CREATE TABLE t2 (a int , c int);");
9758
rc= mysql_query(mysql,
9759
"insert into t2 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
9762
for (j= 0; j < 9; j++)
9764
stmt= mysql_simple_prepare(mysql, query[j]);
9766
for (i= 0; i < 3; i++)
9768
rc= mysql_stmt_execute(stmt);
9769
check_execute(stmt, rc);
9770
rc= my_process_stmt_result(stmt);
9771
DIE_UNLESS(rc == 5);
9773
mysql_stmt_close(stmt);
9776
rc= mysql_query(mysql, "DROP TABLE t1, t2");
9781
static void test_selecttmp()
9785
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";
9787
myheader("test_select_tmp");
9789
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3");
9792
rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
9795
rc= mysql_query(mysql, "create table t2 (a int, b int);");
9798
rc= mysql_query(mysql, "create table t3 (a int, b int);");
9801
rc= mysql_query(mysql,
9802
"insert into t1 values (0, 100), (1, 2), (1, 3), (2, 2), (2, 7), \
9803
(2, -1), (3, 10);");
9805
rc= mysql_query(mysql,
9806
"insert into t2 values (0, 0), (1, 1), (2, 1), (3, 1), (4, 1);");
9808
rc= mysql_query(mysql,
9809
"insert into t3 values (3, 3), (2, 2), (1, 1);");
9812
stmt= mysql_simple_prepare(mysql, query);
9814
for (i= 0; i < 3; i++)
9816
rc= mysql_stmt_execute(stmt);
9817
check_execute(stmt, rc);
9818
rc= my_process_stmt_result(stmt);
9819
DIE_UNLESS(rc == 3);
9821
mysql_stmt_close(stmt);
9823
rc= mysql_query(mysql, "DROP TABLE t1, t2, t3");
9828
static void test_create_drop()
9830
MYSQL_STMT *stmt_create, *stmt_drop, *stmt_select, *stmt_create_select;
9833
myheader("test_table_manipulation");
9835
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
9838
rc= mysql_query(mysql, "create table t2 (a int);");
9841
rc= mysql_query(mysql, "create table t1 (a int);");
9844
rc= mysql_query(mysql, "insert into t2 values (3), (2), (1);");
9847
query= (char*)"create table t1 (a int)";
9848
stmt_create= mysql_simple_prepare(mysql, query);
9849
check_stmt(stmt_create);
9851
query= (char*)"drop table t1";
9852
stmt_drop= mysql_simple_prepare(mysql, query);
9853
check_stmt(stmt_drop);
9855
query= (char*)"select a in (select a from t2) from t1";
9856
stmt_select= mysql_simple_prepare(mysql, query);
9857
check_stmt(stmt_select);
9859
rc= mysql_query(mysql, "DROP TABLE t1");
9862
query= (char*)"create table t1 select a from t2";
9863
stmt_create_select= mysql_simple_prepare(mysql, query);
9864
check_stmt(stmt_create_select);
9866
for (i= 0; i < 3; i++)
9868
rc= mysql_stmt_execute(stmt_create);
9869
check_execute(stmt_create, rc);
9871
fprintf(stdout, "created %i\n", i);
9873
rc= mysql_stmt_execute(stmt_select);
9874
check_execute(stmt_select, rc);
9875
rc= my_process_stmt_result(stmt_select);
9876
DIE_UNLESS(rc == 0);
9878
rc= mysql_stmt_execute(stmt_drop);
9879
check_execute(stmt_drop, rc);
9881
fprintf(stdout, "dropped %i\n", i);
9883
rc= mysql_stmt_execute(stmt_create_select);
9884
check_execute(stmt_create, rc);
9886
fprintf(stdout, "created select %i\n", i);
9888
rc= mysql_stmt_execute(stmt_select);
9889
check_execute(stmt_select, rc);
9890
rc= my_process_stmt_result(stmt_select);
9891
DIE_UNLESS(rc == 3);
9893
rc= mysql_stmt_execute(stmt_drop);
9894
check_execute(stmt_drop, rc);
9896
fprintf(stdout, "dropped %i\n", i);
9899
mysql_stmt_close(stmt_create);
9900
mysql_stmt_close(stmt_drop);
9901
mysql_stmt_close(stmt_select);
9902
mysql_stmt_close(stmt_create_select);
9904
rc= mysql_query(mysql, "DROP TABLE t2");
9909
static void test_rename()
9912
const char *query= "rename table t1 to t2, t3 to t4";
9914
myheader("test_table_rename");
9916
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
9919
stmt= mysql_simple_prepare(mysql, query);
9922
rc= mysql_query(mysql, "create table t1 (a int)");
9925
rc= mysql_stmt_execute(stmt);
9926
check_execute_r(stmt, rc);
9928
fprintf(stdout, "rename without t3\n");
9930
rc= mysql_query(mysql, "create table t3 (a int)");
9933
rc= mysql_stmt_execute(stmt);
9934
check_execute(stmt, rc);
9936
fprintf(stdout, "rename with t3\n");
9938
rc= mysql_stmt_execute(stmt);
9939
check_execute_r(stmt, rc);
9941
fprintf(stdout, "rename renamed\n");
9943
rc= mysql_query(mysql, "rename table t2 to t1, t4 to t3");
9946
rc= mysql_stmt_execute(stmt);
9947
check_execute(stmt, rc);
9949
fprintf(stdout, "rename reverted\n");
9951
mysql_stmt_close(stmt);
9953
rc= mysql_query(mysql, "DROP TABLE t2, t4");
9958
static void test_do_set()
9960
MYSQL_STMT *stmt_do, *stmt_set;
9963
myheader("test_do_set");
9965
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
9968
rc= mysql_query(mysql, "create table t1 (a int)");
9971
query= (char*)"do @var:=(1 in (select * from t1))";
9972
stmt_do= mysql_simple_prepare(mysql, query);
9973
check_stmt(stmt_do);
9975
query= (char*)"set @var=(1 in (select * from t1))";
9976
stmt_set= mysql_simple_prepare(mysql, query);
9977
check_stmt(stmt_set);
9979
for (i= 0; i < 3; i++)
9981
rc= mysql_stmt_execute(stmt_do);
9982
check_execute(stmt_do, rc);
9984
fprintf(stdout, "do %i\n", i);
9985
rc= mysql_stmt_execute(stmt_set);
9986
check_execute(stmt_set, rc);
9988
fprintf(stdout, "set %i\n", i);
9991
mysql_stmt_close(stmt_do);
9992
mysql_stmt_close(stmt_set);
9996
static void test_multi()
9998
MYSQL_STMT *stmt_delete, *stmt_update, *stmt_select1, *stmt_select2;
10000
MYSQL_BIND my_bind[1];
10004
myheader("test_multi");
10007
We need to bzero bind structure because mysql_stmt_bind_param checks all
10010
bzero((char*) my_bind, sizeof(my_bind));
10012
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
10013
my_bind[0].buffer= (void *)¶m;
10014
my_bind[0].length= &length;
10016
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
10019
rc= mysql_query(mysql, "create table t1 (a int, b int)");
10022
rc= mysql_query(mysql, "create table t2 (a int, b int)");
10025
rc= mysql_query(mysql, "insert into t1 values (3, 3), (2, 2), (1, 1)");
10028
rc= mysql_query(mysql, "insert into t2 values (3, 3), (2, 2), (1, 1)");
10031
query= (char*)"delete t1, t2 from t1, t2 where t1.a=t2.a and t1.b=10";
10032
stmt_delete= mysql_simple_prepare(mysql, query);
10033
check_stmt(stmt_delete);
10035
query= (char*)"update t1, t2 set t1.b=10, t2.b=10 where t1.a=t2.a and t1.b=?";
10036
stmt_update= mysql_simple_prepare(mysql, query);
10037
check_stmt(stmt_update);
10039
query= (char*)"select * from t1";
10040
stmt_select1= mysql_simple_prepare(mysql, query);
10041
check_stmt(stmt_select1);
10043
query= (char*)"select * from t2";
10044
stmt_select2= mysql_simple_prepare(mysql, query);
10045
check_stmt(stmt_select2);
10047
for(i= 0; i < 3; i++)
10049
rc= mysql_stmt_bind_param(stmt_update, my_bind);
10050
check_execute(stmt_update, rc);
10052
rc= mysql_stmt_execute(stmt_update);
10053
check_execute(stmt_update, rc);
10055
fprintf(stdout, "update %ld\n", (long) param);
10057
rc= mysql_stmt_execute(stmt_delete);
10058
check_execute(stmt_delete, rc);
10060
fprintf(stdout, "delete %ld\n", (long) param);
10062
rc= mysql_stmt_execute(stmt_select1);
10063
check_execute(stmt_select1, rc);
10064
rc= my_process_stmt_result(stmt_select1);
10065
DIE_UNLESS(rc == 3-param);
10067
rc= mysql_stmt_execute(stmt_select2);
10068
check_execute(stmt_select2, rc);
10069
rc= my_process_stmt_result(stmt_select2);
10070
DIE_UNLESS(rc == 3-param);
10075
mysql_stmt_close(stmt_delete);
10076
mysql_stmt_close(stmt_update);
10077
mysql_stmt_close(stmt_select1);
10078
mysql_stmt_close(stmt_select2);
10079
rc= mysql_query(mysql, "drop table t1, t2");
10084
static void test_insert_select()
10086
MYSQL_STMT *stmt_insert, *stmt_select;
10090
myheader("test_insert_select");
10092
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
10095
rc= mysql_query(mysql, "create table t1 (a int)");
10098
rc= mysql_query(mysql, "create table t2 (a int)");
10101
rc= mysql_query(mysql, "insert into t2 values (1)");
10104
query= (char*)"insert into t1 select a from t2";
10105
stmt_insert= mysql_simple_prepare(mysql, query);
10106
check_stmt(stmt_insert);
10108
query= (char*)"select * from t1";
10109
stmt_select= mysql_simple_prepare(mysql, query);
10110
check_stmt(stmt_select);
10112
for(i= 0; i < 3; i++)
10114
rc= mysql_stmt_execute(stmt_insert);
10115
check_execute(stmt_insert, rc);
10117
fprintf(stdout, "insert %u\n", i);
10119
rc= mysql_stmt_execute(stmt_select);
10120
check_execute(stmt_select, rc);
10121
rc= my_process_stmt_result(stmt_select);
10122
DIE_UNLESS(rc == (int)(i+1));
10125
mysql_stmt_close(stmt_insert);
10126
mysql_stmt_close(stmt_select);
10127
rc= mysql_query(mysql, "drop table t1, t2");
10132
static void test_bind_nagative()
10134
MYSQL_STMT *stmt_insert;
10137
MYSQL_BIND my_bind[1];
10139
ulong my_length= 0L;
10140
my_bool my_null= FALSE;
10141
myheader("test_insert_select");
10143
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
10146
rc= mysql_query(mysql, "create temporary table t1 (c1 int unsigned)");
10149
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1), (-1)");
10152
query= (char*)"INSERT INTO t1 VALUES (?)";
10153
stmt_insert= mysql_simple_prepare(mysql, query);
10154
check_stmt(stmt_insert);
10156
/* bind parameters */
10157
bzero((char*) my_bind, sizeof(my_bind));
10159
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
10160
my_bind[0].buffer= (void *)&my_val;
10161
my_bind[0].length= &my_length;
10162
my_bind[0].is_null= (char*)&my_null;
10164
rc= mysql_stmt_bind_param(stmt_insert, my_bind);
10165
check_execute(stmt_insert, rc);
10168
rc= mysql_stmt_execute(stmt_insert);
10169
check_execute(stmt_insert, rc);
10171
mysql_stmt_close(stmt_insert);
10172
rc= mysql_query(mysql, "drop table t1");
10177
static void test_derived()
10181
MYSQL_BIND my_bind[1];
10183
ulong my_length= 0L;
10184
my_bool my_null= FALSE;
10186
"select count(1) from (select f.id from t1 f where f.id=?) as x";
10188
myheader("test_derived");
10190
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
10193
rc= mysql_query(mysql, "create table t1 (id int(8), primary key (id)) \
10194
ENGINE=InnoDB DEFAULT CHARSET=utf8");
10197
rc= mysql_query(mysql, "insert into t1 values (1)");
10200
stmt= mysql_simple_prepare(mysql, query);
10203
We need to bzero bind structure because mysql_stmt_bind_param checks all
10206
bzero((char*) my_bind, sizeof(my_bind));
10208
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
10209
my_bind[0].buffer= (void *)&my_val;
10210
my_bind[0].length= &my_length;
10211
my_bind[0].is_null= (char*)&my_null;
10213
rc= mysql_stmt_bind_param(stmt, my_bind);
10214
check_execute(stmt, rc);
10216
for (i= 0; i < 3; i++)
10218
rc= mysql_stmt_execute(stmt);
10219
check_execute(stmt, rc);
10220
rc= my_process_stmt_result(stmt);
10221
DIE_UNLESS(rc == 1);
10223
mysql_stmt_close(stmt);
10225
rc= mysql_query(mysql, "DROP TABLE t1");
10230
static void test_xjoin()
10235
"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";
10237
myheader("test_xjoin");
10239
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
10242
rc= mysql_query(mysql, "create table t3 (id int(8), param1_id int(8), param2_id int(8)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
10245
rc= mysql_query(mysql, "create table t1 ( id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
10248
rc= mysql_query(mysql, "create table t2 (id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
10251
rc= mysql_query(mysql, "create table t4(id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
10254
rc= mysql_query(mysql, "insert into t3 values (1, 1, 1), (2, 2, null)");
10257
rc= mysql_query(mysql, "insert into t1 values (1, 1, 'aaa'), (2, null, 'bbb')");
10260
rc= mysql_query(mysql, "insert into t2 values (1, 2, 'ccc')");
10263
rc= mysql_query(mysql, "insert into t4 values (1, 'Name1'), (2, null)");
10266
stmt= mysql_simple_prepare(mysql, query);
10269
for (i= 0; i < 3; i++)
10271
rc= mysql_stmt_execute(stmt);
10272
check_execute(stmt, rc);
10273
rc= my_process_stmt_result(stmt);
10274
DIE_UNLESS(rc == 1);
10276
mysql_stmt_close(stmt);
10278
rc= mysql_query(mysql, "DROP TABLE t1, t2, t3, t4");
10283
static void test_bug3035()
10287
MYSQL_BIND bind_array[12], *my_bind= bind_array, *bind_end= my_bind + 12;
10294
longlong int64_val;
10295
ulonglong uint64_val;
10296
double double_val, udouble_val, double_tmp;
10297
char longlong_as_string[22], ulonglong_as_string[22];
10299
/* mins and maxes */
10300
const int8 int8_min= -128;
10301
const int8 int8_max= 127;
10302
const uint8 uint8_min= 0;
10303
const uint8 uint8_max= 255;
10305
const int16 int16_min= -32768;
10306
const int16 int16_max= 32767;
10307
const uint16 uint16_min= 0;
10308
const uint16 uint16_max= 65535;
10310
const int32 int32_max= 2147483647L;
10311
const int32 int32_min= -int32_max - 1;
10312
const uint32 uint32_min= 0;
10313
const uint32 uint32_max= 4294967295U;
10315
/* it might not work okay everyplace */
10316
const longlong int64_max= LL(9223372036854775807);
10317
const longlong int64_min= -int64_max - 1;
10319
const ulonglong uint64_min= 0U;
10320
const ulonglong uint64_max= ULL(18446744073709551615);
10322
const char *stmt_text;
10324
myheader("test_bug3035");
10326
stmt_text= "DROP TABLE IF EXISTS t1";
10327
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10330
stmt_text= "CREATE TABLE t1 (i8 TINYINT, ui8 TINYINT UNSIGNED, "
10331
"i16 SMALLINT, ui16 SMALLINT UNSIGNED, "
10332
"i32 INT, ui32 INT UNSIGNED, "
10333
"i64 BIGINT, ui64 BIGINT UNSIGNED, "
10334
"id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT)";
10335
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10338
bzero((char*) bind_array, sizeof(bind_array));
10340
for (my_bind= bind_array; my_bind < bind_end; my_bind++)
10341
my_bind->error= &my_bind->error_value;
10343
bind_array[0].buffer_type= MYSQL_TYPE_TINY;
10344
bind_array[0].buffer= (void *) &int8_val;
10346
bind_array[1].buffer_type= MYSQL_TYPE_TINY;
10347
bind_array[1].buffer= (void *) &uint8_val;
10348
bind_array[1].is_unsigned= 1;
10350
bind_array[2].buffer_type= MYSQL_TYPE_SHORT;
10351
bind_array[2].buffer= (void *) &int16_val;
10353
bind_array[3].buffer_type= MYSQL_TYPE_SHORT;
10354
bind_array[3].buffer= (void *) &uint16_val;
10355
bind_array[3].is_unsigned= 1;
10357
bind_array[4].buffer_type= MYSQL_TYPE_LONG;
10358
bind_array[4].buffer= (void *) &int32_val;
10360
bind_array[5].buffer_type= MYSQL_TYPE_LONG;
10361
bind_array[5].buffer= (void *) &uint32_val;
10362
bind_array[5].is_unsigned= 1;
10364
bind_array[6].buffer_type= MYSQL_TYPE_LONGLONG;
10365
bind_array[6].buffer= (void *) &int64_val;
10367
bind_array[7].buffer_type= MYSQL_TYPE_LONGLONG;
10368
bind_array[7].buffer= (void *) &uint64_val;
10369
bind_array[7].is_unsigned= 1;
10371
stmt= mysql_stmt_init(mysql);
10374
stmt_text= "INSERT INTO t1 (i8, ui8, i16, ui16, i32, ui32, i64, ui64) "
10375
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
10376
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10377
check_execute(stmt, rc);
10379
mysql_stmt_bind_param(stmt, bind_array);
10381
int8_val= int8_min;
10382
uint8_val= uint8_min;
10383
int16_val= int16_min;
10384
uint16_val= uint16_min;
10385
int32_val= int32_min;
10386
uint32_val= uint32_min;
10387
int64_val= int64_min;
10388
uint64_val= uint64_min;
10390
rc= mysql_stmt_execute(stmt);
10391
check_execute(stmt, rc);
10393
int8_val= int8_max;
10394
uint8_val= uint8_max;
10395
int16_val= int16_max;
10396
uint16_val= uint16_max;
10397
int32_val= int32_max;
10398
uint32_val= uint32_max;
10399
int64_val= int64_max;
10400
uint64_val= uint64_max;
10402
rc= mysql_stmt_execute(stmt);
10403
check_execute(stmt, rc);
10405
stmt_text= "SELECT i8, ui8, i16, ui16, i32, ui32, i64, ui64, ui64, "
10406
"cast(ui64 as signed), ui64, cast(ui64 as signed)"
10407
"FROM t1 ORDER BY id ASC";
10409
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10410
check_execute(stmt, rc);
10412
rc= mysql_stmt_execute(stmt);
10413
check_execute(stmt, rc);
10415
bind_array[8].buffer_type= MYSQL_TYPE_DOUBLE;
10416
bind_array[8].buffer= (void *) &udouble_val;
10418
bind_array[9].buffer_type= MYSQL_TYPE_DOUBLE;
10419
bind_array[9].buffer= (void *) &double_val;
10421
bind_array[10].buffer_type= MYSQL_TYPE_STRING;
10422
bind_array[10].buffer= (void *) &ulonglong_as_string;
10423
bind_array[10].buffer_length= sizeof(ulonglong_as_string);
10425
bind_array[11].buffer_type= MYSQL_TYPE_STRING;
10426
bind_array[11].buffer= (void *) &longlong_as_string;
10427
bind_array[11].buffer_length= sizeof(longlong_as_string);
10429
mysql_stmt_bind_result(stmt, bind_array);
10431
rc= mysql_stmt_fetch(stmt);
10432
check_execute(stmt, rc);
10434
DIE_UNLESS(int8_val == int8_min);
10435
DIE_UNLESS(uint8_val == uint8_min);
10436
DIE_UNLESS(int16_val == int16_min);
10437
DIE_UNLESS(uint16_val == uint16_min);
10438
DIE_UNLESS(int32_val == int32_min);
10439
DIE_UNLESS(uint32_val == uint32_min);
10440
DIE_UNLESS(int64_val == int64_min);
10441
DIE_UNLESS(uint64_val == uint64_min);
10442
DIE_UNLESS(double_val == (longlong) uint64_min);
10443
double_tmp= ulonglong2double(uint64_val);
10444
DIE_UNLESS(cmp_double(&udouble_val, &double_tmp));
10445
DIE_UNLESS(!strcmp(longlong_as_string, "0"));
10446
DIE_UNLESS(!strcmp(ulonglong_as_string, "0"));
10448
rc= mysql_stmt_fetch(stmt);
10452
printf("Truncation mask: ");
10453
for (my_bind= bind_array; my_bind < bind_end; my_bind++)
10454
printf("%d", (int) my_bind->error_value);
10457
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED || rc == 0);
10459
DIE_UNLESS(int8_val == int8_max);
10460
DIE_UNLESS(uint8_val == uint8_max);
10461
DIE_UNLESS(int16_val == int16_max);
10462
DIE_UNLESS(uint16_val == uint16_max);
10463
DIE_UNLESS(int32_val == int32_max);
10464
DIE_UNLESS(uint32_val == uint32_max);
10465
DIE_UNLESS(int64_val == int64_max);
10466
DIE_UNLESS(uint64_val == uint64_max);
10467
DIE_UNLESS(double_val == (longlong) uint64_val);
10468
double_tmp= ulonglong2double(uint64_val);
10469
DIE_UNLESS(cmp_double(&udouble_val, &double_tmp));
10470
DIE_UNLESS(!strcmp(longlong_as_string, "-1"));
10471
DIE_UNLESS(!strcmp(ulonglong_as_string, "18446744073709551615"));
10473
rc= mysql_stmt_fetch(stmt);
10474
DIE_UNLESS(rc == MYSQL_NO_DATA);
10476
mysql_stmt_close(stmt);
10478
stmt_text= "DROP TABLE t1";
10479
mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10483
static void test_union2()
10488
myheader("test_union2");
10490
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
10493
rc= mysql_query(mysql, "CREATE TABLE t1(col1 INT, \
10494
col2 VARCHAR(40), \
10499
stmt= mysql_simple_prepare(mysql,
10500
"select col1 FROM t1 where col1=1 union distinct "
10501
"select col1 FROM t1 where col1=2");
10504
for (i= 0; i < 3; i++)
10506
rc= mysql_stmt_execute(stmt);
10507
check_execute(stmt, rc);
10508
rc= my_process_stmt_result(stmt);
10509
DIE_UNLESS(rc == 0);
10512
mysql_stmt_close(stmt);
10514
rc= mysql_query(mysql, "DROP TABLE t1");
10520
This tests for various mysql_stmt_send_long_data bugs described in #1664
10523
static void test_bug1664()
10528
const char *str_data= "Simple string";
10529
MYSQL_BIND my_bind[2];
10530
const char *query= "INSERT INTO test_long_data(col2, col1) VALUES(?, ?)";
10532
myheader("test_bug1664");
10534
rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
10537
rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, col2 long varchar)");
10540
stmt= mysql_stmt_init(mysql);
10542
rc= mysql_stmt_prepare(stmt, query, strlen(query));
10543
check_execute(stmt, rc);
10545
verify_param_count(stmt, 2);
10547
bzero((char*) my_bind, sizeof(my_bind));
10549
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
10550
my_bind[0].buffer= (void *)str_data;
10551
my_bind[0].buffer_length= strlen(str_data);
10553
my_bind[1].buffer= (void *)&int_data;
10554
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
10556
rc= mysql_stmt_bind_param(stmt, my_bind);
10557
check_execute(stmt, rc);
10562
Let us supply empty long_data. This should work and should
10563
not break following execution.
10566
rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
10567
check_execute(stmt, rc);
10569
rc= mysql_stmt_execute(stmt);
10570
check_execute(stmt, rc);
10572
verify_col_data("test_long_data", "col1", "1");
10573
verify_col_data("test_long_data", "col2", "");
10575
rc= mysql_query(mysql, "DELETE FROM test_long_data");
10578
/* This should pass OK */
10579
data= (char *)"Data";
10580
rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
10581
check_execute(stmt, rc);
10583
rc= mysql_stmt_execute(stmt);
10584
check_execute(stmt, rc);
10586
verify_col_data("test_long_data", "col1", "1");
10587
verify_col_data("test_long_data", "col2", "Data");
10590
rc= mysql_query(mysql, "DELETE FROM test_long_data");
10594
Now we are changing int parameter and don't do anything
10595
with first parameter. Second mysql_stmt_execute() should run
10596
OK treating this first parameter as string parameter.
10601
rc= mysql_stmt_execute(stmt);
10602
check_execute(stmt, rc);
10604
verify_col_data("test_long_data", "col1", "2");
10605
verify_col_data("test_long_data", "col2", str_data);
10608
rc= mysql_query(mysql, "DELETE FROM test_long_data");
10612
Now we are sending other long data. It should not be
10613
concatened to previous.
10616
data= (char *)"SomeOtherData";
10617
rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
10618
check_execute(stmt, rc);
10620
rc= mysql_stmt_execute(stmt);
10621
check_execute(stmt, rc);
10623
verify_col_data("test_long_data", "col1", "2");
10624
verify_col_data("test_long_data", "col2", "SomeOtherData");
10626
mysql_stmt_close(stmt);
10629
rc= mysql_query(mysql, "DELETE FROM test_long_data");
10632
/* Now let us test how mysql_stmt_reset works. */
10633
stmt= mysql_stmt_init(mysql);
10635
rc= mysql_stmt_prepare(stmt, query, strlen(query));
10636
check_execute(stmt, rc);
10637
rc= mysql_stmt_bind_param(stmt, my_bind);
10638
check_execute(stmt, rc);
10640
data= (char *)"SomeData";
10641
rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
10642
check_execute(stmt, rc);
10644
rc= mysql_stmt_reset(stmt);
10645
check_execute(stmt, rc);
10647
rc= mysql_stmt_execute(stmt);
10648
check_execute(stmt, rc);
10650
verify_col_data("test_long_data", "col1", "2");
10651
verify_col_data("test_long_data", "col2", str_data);
10653
mysql_stmt_close(stmt);
10655
/* Final clean up */
10656
rc= mysql_query(mysql, "DROP TABLE test_long_data");
10661
static void test_order_param()
10666
myheader("test_order_param");
10668
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
10671
rc= mysql_query(mysql, "CREATE TABLE t1(a INT, b char(10))");
10674
stmt= mysql_simple_prepare(mysql,
10675
"select sum(a) + 200, 1 from t1 "
10677
"select sum(a) + 200, 1 from t1 group by b ");
10679
mysql_stmt_close(stmt);
10681
stmt= mysql_simple_prepare(mysql,
10682
"select sum(a) + 200, ? from t1 group by b "
10684
"select sum(a) + 200, 1 from t1 group by b ");
10686
mysql_stmt_close(stmt);
10688
stmt= mysql_simple_prepare(mysql,
10689
"select sum(a) + 200, ? from t1 "
10691
"select sum(a) + 200, 1 from t1 group by b ");
10693
mysql_stmt_close(stmt);
10695
rc= mysql_query(mysql, "DROP TABLE t1");
10700
static void test_union_param()
10705
MYSQL_BIND my_bind[2];
10707
ulong my_length= 3L;
10708
my_bool my_null= FALSE;
10709
myheader("test_union_param");
10711
strmov(my_val, "abc");
10713
query= (char*)"select ? as my_col union distinct select ?";
10714
stmt= mysql_simple_prepare(mysql, query);
10718
We need to bzero bind structure because mysql_stmt_bind_param checks all
10721
bzero((char*) my_bind, sizeof(my_bind));
10723
/* bind parameters */
10724
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
10725
my_bind[0].buffer= (char*) &my_val;
10726
my_bind[0].buffer_length= 4;
10727
my_bind[0].length= &my_length;
10728
my_bind[0].is_null= (char*)&my_null;
10729
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
10730
my_bind[1].buffer= (char*) &my_val;
10731
my_bind[1].buffer_length= 4;
10732
my_bind[1].length= &my_length;
10733
my_bind[1].is_null= (char*)&my_null;
10735
rc= mysql_stmt_bind_param(stmt, my_bind);
10736
check_execute(stmt, rc);
10738
for (i= 0; i < 3; i++)
10740
rc= mysql_stmt_execute(stmt);
10741
check_execute(stmt, rc);
10742
rc= my_process_stmt_result(stmt);
10743
DIE_UNLESS(rc == 1);
10746
mysql_stmt_close(stmt);
10750
static void test_ps_i18n()
10754
const char *stmt_text;
10755
MYSQL_BIND bind_array[2];
10757
/* Represented as numbers to keep UTF8 tools from clobbering them. */
10758
const char *koi8= "\xee\xd5\x2c\x20\xda\xc1\x20\xd2\xd9\xc2\xc1\xcc\xcb\xd5";
10759
const char *cp1251= "\xcd\xf3\x2c\x20\xe7\xe0\x20\xf0\xfb\xe1\xe0\xeb\xea\xf3";
10760
char buf1[16], buf2[16];
10761
ulong buf1_len, buf2_len;
10764
myheader("test_ps_i18n");
10766
stmt_text= "DROP TABLE IF EXISTS t1";
10767
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10771
Create table with binary columns, set session character set to cp1251,
10772
client character set to koi8, and make sure that there is conversion
10773
on insert and no conversion on select
10776
stmt_text= "CREATE TABLE t1 (c1 VARBINARY(255), c2 VARBINARY(255))";
10778
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10781
stmt_text= "SET CHARACTER_SET_CLIENT=koi8r, "
10782
"CHARACTER_SET_CONNECTION=cp1251, "
10783
"CHARACTER_SET_RESULTS=koi8r";
10785
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10788
bzero((char*) bind_array, sizeof(bind_array));
10790
bind_array[0].buffer_type= MYSQL_TYPE_STRING;
10791
bind_array[0].buffer= (void *) koi8;
10792
bind_array[0].buffer_length= strlen(koi8);
10794
bind_array[1].buffer_type= MYSQL_TYPE_STRING;
10795
bind_array[1].buffer= (void *) koi8;
10796
bind_array[1].buffer_length= strlen(koi8);
10798
stmt= mysql_stmt_init(mysql);
10801
stmt_text= "INSERT INTO t1 (c1, c2) VALUES (?, ?)";
10803
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10804
check_execute(stmt, rc);
10806
mysql_stmt_bind_param(stmt, bind_array);
10808
mysql_stmt_send_long_data(stmt, 0, koi8, strlen(koi8));
10810
rc= mysql_stmt_execute(stmt);
10811
check_execute(stmt, rc);
10813
stmt_text= "SELECT c1, c2 FROM t1";
10815
/* c1 and c2 are binary so no conversion will be done on select */
10816
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10817
check_execute(stmt, rc);
10819
rc= mysql_stmt_execute(stmt);
10820
check_execute(stmt, rc);
10822
bind_array[0].buffer= buf1;
10823
bind_array[0].buffer_length= sizeof(buf1);
10824
bind_array[0].length= &buf1_len;
10826
bind_array[1].buffer= buf2;
10827
bind_array[1].buffer_length= sizeof(buf2);
10828
bind_array[1].length= &buf2_len;
10830
mysql_stmt_bind_result(stmt, bind_array);
10832
rc= mysql_stmt_fetch(stmt);
10833
check_execute(stmt, rc);
10835
DIE_UNLESS(buf1_len == strlen(cp1251));
10836
DIE_UNLESS(buf2_len == strlen(cp1251));
10837
DIE_UNLESS(!memcmp(buf1, cp1251, buf1_len));
10838
DIE_UNLESS(!memcmp(buf2, cp1251, buf1_len));
10840
rc= mysql_stmt_fetch(stmt);
10841
DIE_UNLESS(rc == MYSQL_NO_DATA);
10843
stmt_text= "DROP TABLE IF EXISTS t1";
10844
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10848
Now create table with two cp1251 columns, set client character
10849
set to koi8 and supply columns of one row as string and another as
10850
binary data. Binary data must not be converted on insert, and both
10851
columns must be converted to client character set on select.
10854
stmt_text= "CREATE TABLE t1 (c1 VARCHAR(255) CHARACTER SET cp1251, "
10855
"c2 VARCHAR(255) CHARACTER SET cp1251)";
10857
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10860
stmt_text= "INSERT INTO t1 (c1, c2) VALUES (?, ?)";
10862
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10863
check_execute(stmt, rc);
10865
/* this data must be converted */
10866
bind_array[0].buffer_type= MYSQL_TYPE_STRING;
10867
bind_array[0].buffer= (void *) koi8;
10868
bind_array[0].buffer_length= strlen(koi8);
10870
bind_array[1].buffer_type= MYSQL_TYPE_STRING;
10871
bind_array[1].buffer= (void *) koi8;
10872
bind_array[1].buffer_length= strlen(koi8);
10874
mysql_stmt_bind_param(stmt, bind_array);
10876
mysql_stmt_send_long_data(stmt, 0, koi8, strlen(koi8));
10878
rc= mysql_stmt_execute(stmt);
10879
check_execute(stmt, rc);
10881
/* this data must not be converted */
10882
bind_array[0].buffer_type= MYSQL_TYPE_BLOB;
10883
bind_array[0].buffer= (void *) cp1251;
10884
bind_array[0].buffer_length= strlen(cp1251);
10886
bind_array[1].buffer_type= MYSQL_TYPE_BLOB;
10887
bind_array[1].buffer= (void *) cp1251;
10888
bind_array[1].buffer_length= strlen(cp1251);
10890
mysql_stmt_bind_param(stmt, bind_array);
10892
mysql_stmt_send_long_data(stmt, 0, cp1251, strlen(cp1251));
10894
rc= mysql_stmt_execute(stmt);
10895
check_execute(stmt, rc);
10897
/* Fetch data and verify that rows are in koi8 */
10899
stmt_text= "SELECT c1, c2 FROM t1";
10901
/* c1 and c2 are binary so no conversion will be done on select */
10902
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10903
check_execute(stmt, rc);
10905
rc= mysql_stmt_execute(stmt);
10906
check_execute(stmt, rc);
10908
bind_array[0].buffer= buf1;
10909
bind_array[0].buffer_length= sizeof(buf1);
10910
bind_array[0].length= &buf1_len;
10912
bind_array[1].buffer= buf2;
10913
bind_array[1].buffer_length= sizeof(buf2);
10914
bind_array[1].length= &buf2_len;
10916
mysql_stmt_bind_result(stmt, bind_array);
10918
while ((rc= mysql_stmt_fetch(stmt)) == 0)
10920
DIE_UNLESS(buf1_len == strlen(koi8));
10921
DIE_UNLESS(buf2_len == strlen(koi8));
10922
DIE_UNLESS(!memcmp(buf1, koi8, buf1_len));
10923
DIE_UNLESS(!memcmp(buf2, koi8, buf1_len));
10925
DIE_UNLESS(rc == MYSQL_NO_DATA);
10926
mysql_stmt_close(stmt);
10928
stmt_text= "DROP TABLE t1";
10929
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10931
stmt_text= "SET NAMES DEFAULT";
10932
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10937
static void test_bug3796()
10940
MYSQL_BIND my_bind[1];
10941
const char *concat_arg0= "concat_with_";
10942
enum { OUT_BUFF_SIZE= 30 };
10943
char out_buff[OUT_BUFF_SIZE];
10944
char canonical_buff[OUT_BUFF_SIZE];
10946
const char *stmt_text;
10949
myheader("test_bug3796");
10951
/* Create and fill test table */
10952
stmt_text= "DROP TABLE IF EXISTS t1";
10953
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10956
stmt_text= "CREATE TABLE t1 (a INT, b VARCHAR(30))";
10957
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10960
stmt_text= "INSERT INTO t1 VALUES(1, 'ONE'), (2, 'TWO')";
10961
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
10964
/* Create statement handle and prepare it with select */
10965
stmt= mysql_stmt_init(mysql);
10966
stmt_text= "SELECT concat(?, b) FROM t1";
10968
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
10969
check_execute(stmt, rc);
10971
/* Bind input buffers */
10972
bzero((char*) my_bind, sizeof(my_bind));
10974
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
10975
my_bind[0].buffer= (void *) concat_arg0;
10976
my_bind[0].buffer_length= strlen(concat_arg0);
10978
mysql_stmt_bind_param(stmt, my_bind);
10980
/* Execute the select statement */
10981
rc= mysql_stmt_execute(stmt);
10982
check_execute(stmt, rc);
10984
my_bind[0].buffer= (void *) out_buff;
10985
my_bind[0].buffer_length= OUT_BUFF_SIZE;
10986
my_bind[0].length= &out_length;
10988
mysql_stmt_bind_result(stmt, my_bind);
10990
rc= mysql_stmt_fetch(stmt);
10992
printf("Concat result: '%s'\n", out_buff);
10993
check_execute(stmt, rc);
10994
strmov(canonical_buff, concat_arg0);
10995
strcat(canonical_buff, "ONE");
10996
DIE_UNLESS(strlen(canonical_buff) == out_length &&
10997
strncmp(out_buff, canonical_buff, out_length) == 0);
10999
rc= mysql_stmt_fetch(stmt);
11000
check_execute(stmt, rc);
11001
strmov(canonical_buff + strlen(concat_arg0), "TWO");
11002
DIE_UNLESS(strlen(canonical_buff) == out_length &&
11003
strncmp(out_buff, canonical_buff, out_length) == 0);
11005
printf("Concat result: '%s'\n", out_buff);
11007
rc= mysql_stmt_fetch(stmt);
11008
DIE_UNLESS(rc == MYSQL_NO_DATA);
11010
mysql_stmt_close(stmt);
11012
stmt_text= "DROP TABLE IF EXISTS t1";
11013
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11018
static void test_bug4026()
11021
MYSQL_BIND my_bind[2];
11022
MYSQL_TIME time_in, time_out;
11023
MYSQL_TIME datetime_in, datetime_out;
11024
const char *stmt_text;
11027
myheader("test_bug4026");
11029
/* Check that microseconds are inserted and selected successfully */
11031
/* Create a statement handle and prepare it with select */
11032
stmt= mysql_stmt_init(mysql);
11033
stmt_text= "SELECT ?, ?";
11035
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11036
check_execute(stmt, rc);
11038
/* Bind input buffers */
11039
bzero((char*) my_bind, sizeof(my_bind));
11040
bzero((char*) &time_in, sizeof(time_in));
11041
bzero((char*) &time_out, sizeof(time_out));
11042
bzero((char*) &datetime_in, sizeof(datetime_in));
11043
bzero((char*) &datetime_out, sizeof(datetime_out));
11045
my_bind[0].buffer_type= MYSQL_TYPE_TIME;
11046
my_bind[0].buffer= (void *) &time_in;
11047
my_bind[1].buffer_type= MYSQL_TYPE_DATETIME;
11048
my_bind[1].buffer= (void *) &datetime_in;
11051
time_in.minute= 59;
11052
time_in.second= 59;
11053
time_in.second_part= 123456;
11055
This is not necessary, just to make DIE_UNLESS below work: this field
11056
is filled in when time is received from server
11058
time_in.time_type= MYSQL_TIMESTAMP_TIME;
11060
datetime_in= time_in;
11061
datetime_in.year= 2003;
11062
datetime_in.month= 12;
11063
datetime_in.day= 31;
11064
datetime_in.time_type= MYSQL_TIMESTAMP_DATETIME;
11066
mysql_stmt_bind_param(stmt, my_bind);
11068
/* Execute the select statement */
11069
rc= mysql_stmt_execute(stmt);
11070
check_execute(stmt, rc);
11072
my_bind[0].buffer= (void *) &time_out;
11073
my_bind[1].buffer= (void *) &datetime_out;
11075
mysql_stmt_bind_result(stmt, my_bind);
11077
rc= mysql_stmt_fetch(stmt);
11078
DIE_UNLESS(rc == 0);
11081
printf("%d:%d:%d.%lu\n", time_out.hour, time_out.minute, time_out.second,
11082
time_out.second_part);
11083
printf("%d-%d-%d %d:%d:%d.%lu\n", datetime_out.year, datetime_out.month,
11084
datetime_out.day, datetime_out.hour,
11085
datetime_out.minute, datetime_out.second,
11086
datetime_out.second_part);
11088
DIE_UNLESS(memcmp(&time_in, &time_out, sizeof(time_in)) == 0);
11089
DIE_UNLESS(memcmp(&datetime_in, &datetime_out, sizeof(datetime_in)) == 0);
11090
mysql_stmt_close(stmt);
11094
static void test_bug4079()
11097
MYSQL_BIND my_bind[1];
11098
const char *stmt_text;
11102
myheader("test_bug4079");
11104
/* Create and fill table */
11105
mysql_query(mysql, "DROP TABLE IF EXISTS t1");
11106
mysql_query(mysql, "CREATE TABLE t1 (a int)");
11107
mysql_query(mysql, "INSERT INTO t1 VALUES (1), (2)");
11109
/* Prepare erroneous statement */
11110
stmt= mysql_stmt_init(mysql);
11111
stmt_text= "SELECT 1 < (SELECT a FROM t1)";
11113
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11114
check_execute(stmt, rc);
11116
/* Execute the select statement */
11117
rc= mysql_stmt_execute(stmt);
11118
check_execute(stmt, rc);
11120
/* Bind input buffers */
11121
bzero((char*) my_bind, sizeof(my_bind));
11123
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
11124
my_bind[0].buffer= (void *) &res;
11126
mysql_stmt_bind_result(stmt, my_bind);
11128
rc= mysql_stmt_fetch(stmt);
11129
DIE_UNLESS(rc != 0 && rc != MYSQL_NO_DATA);
11131
printf("Got error from mysql_stmt_fetch (as expected):\n%s\n",
11132
mysql_stmt_error(stmt));
11133
/* buggy version of libmysql hanged up here */
11134
mysql_stmt_close(stmt);
11138
static void test_bug4236()
11141
const char *stmt_text;
11145
myheader("test_bug4236");
11147
stmt= mysql_stmt_init(mysql);
11149
/* mysql_stmt_execute() of statement with statement id= 0 crashed server */
11150
stmt_text= "SELECT 1";
11151
/* We need to prepare statement to pass by possible check in libmysql */
11152
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11153
check_execute(stmt, rc);
11154
/* Hack to check that server works OK if statement wasn't found */
11155
backup.stmt_id= stmt->stmt_id;
11157
rc= mysql_stmt_execute(stmt);
11159
/* Restore original statement id to be able to reprepare it */
11160
stmt->stmt_id= backup.stmt_id;
11162
mysql_stmt_close(stmt);
11166
static void test_bug4030()
11169
MYSQL_BIND my_bind[3];
11170
MYSQL_TIME time_canonical, time_out;
11171
MYSQL_TIME date_canonical, date_out;
11172
MYSQL_TIME datetime_canonical, datetime_out;
11173
const char *stmt_text;
11176
myheader("test_bug4030");
11178
/* Check that microseconds are inserted and selected successfully */
11180
/* Execute a query with time values in prepared mode */
11181
stmt= mysql_stmt_init(mysql);
11182
stmt_text= "SELECT '23:59:59.123456', '2003-12-31', "
11183
"'2003-12-31 23:59:59.123456'";
11184
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11185
check_execute(stmt, rc);
11186
rc= mysql_stmt_execute(stmt);
11187
check_execute(stmt, rc);
11189
/* Bind output buffers */
11190
bzero((char*) my_bind, sizeof(my_bind));
11191
bzero((char*) &time_canonical, sizeof(time_canonical));
11192
bzero((char*) &time_out, sizeof(time_out));
11193
bzero((char*) &date_canonical, sizeof(date_canonical));
11194
bzero((char*) &date_out, sizeof(date_out));
11195
bzero((char*) &datetime_canonical, sizeof(datetime_canonical));
11196
bzero((char*) &datetime_out, sizeof(datetime_out));
11198
my_bind[0].buffer_type= MYSQL_TYPE_TIME;
11199
my_bind[0].buffer= (void *) &time_out;
11200
my_bind[1].buffer_type= MYSQL_TYPE_DATE;
11201
my_bind[1].buffer= (void *) &date_out;
11202
my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
11203
my_bind[2].buffer= (void *) &datetime_out;
11205
time_canonical.hour= 23;
11206
time_canonical.minute= 59;
11207
time_canonical.second= 59;
11208
time_canonical.second_part= 123456;
11209
time_canonical.time_type= MYSQL_TIMESTAMP_TIME;
11211
date_canonical.year= 2003;
11212
date_canonical.month= 12;
11213
date_canonical.day= 31;
11214
date_canonical.time_type= MYSQL_TIMESTAMP_DATE;
11216
datetime_canonical= time_canonical;
11217
datetime_canonical.year= 2003;
11218
datetime_canonical.month= 12;
11219
datetime_canonical.day= 31;
11220
datetime_canonical.time_type= MYSQL_TIMESTAMP_DATETIME;
11222
mysql_stmt_bind_result(stmt, my_bind);
11224
rc= mysql_stmt_fetch(stmt);
11225
DIE_UNLESS(rc == 0);
11228
printf("%d:%d:%d.%lu\n", time_out.hour, time_out.minute, time_out.second,
11229
time_out.second_part);
11230
printf("%d-%d-%d\n", date_out.year, date_out.month, date_out.day);
11231
printf("%d-%d-%d %d:%d:%d.%lu\n", datetime_out.year, datetime_out.month,
11232
datetime_out.day, datetime_out.hour,
11233
datetime_out.minute, datetime_out.second,
11234
datetime_out.second_part);
11236
DIE_UNLESS(memcmp(&time_canonical, &time_out, sizeof(time_out)) == 0);
11237
DIE_UNLESS(memcmp(&date_canonical, &date_out, sizeof(date_out)) == 0);
11238
DIE_UNLESS(memcmp(&datetime_canonical, &datetime_out, sizeof(datetime_out)) == 0);
11239
mysql_stmt_close(stmt);
11242
static void test_view()
11246
MYSQL_BIND my_bind[1];
11251
"SELECT COUNT(*) FROM v1 WHERE SERVERNAME=?";
11253
myheader("test_view");
11255
rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,t2,t3,v1");
11258
rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1,t2,t3");
11260
rc= mysql_query(mysql,"CREATE TABLE t1 ("
11261
" SERVERGRP varchar(20) NOT NULL default '', "
11262
" DBINSTANCE varchar(20) NOT NULL default '', "
11263
" PRIMARY KEY (SERVERGRP)) "
11264
" CHARSET=latin1 collate=latin1_bin");
11266
rc= mysql_query(mysql,"CREATE TABLE t2 ("
11267
" SERVERNAME varchar(20) NOT NULL, "
11268
" SERVERGRP varchar(20) NOT NULL, "
11269
" PRIMARY KEY (SERVERNAME)) "
11270
" CHARSET=latin1 COLLATE latin1_bin");
11272
rc= mysql_query(mysql,
11273
"CREATE TABLE t3 ("
11274
" SERVERGRP varchar(20) BINARY NOT NULL, "
11275
" TABNAME varchar(30) NOT NULL, MAPSTATE char(1) NOT NULL, "
11276
" ACTSTATE char(1) NOT NULL , "
11277
" LOCAL_NAME varchar(30) NOT NULL, "
11278
" CHG_DATE varchar(8) NOT NULL default '00000000', "
11279
" CHG_TIME varchar(6) NOT NULL default '000000', "
11280
" MXUSER varchar(12) NOT NULL default '', "
11281
" PRIMARY KEY (SERVERGRP, TABNAME, MAPSTATE, ACTSTATE, "
11282
" LOCAL_NAME)) CHARSET=latin1 COLLATE latin1_bin");
11284
rc= mysql_query(mysql,"CREATE VIEW v1 AS select sql_no_cache"
11285
" T0001.SERVERNAME AS SERVERNAME, T0003.TABNAME AS"
11286
" TABNAME,T0003.LOCAL_NAME AS LOCAL_NAME,T0002.DBINSTANCE AS"
11287
" DBINSTANCE from t2 T0001 join t1 T0002 join t3 T0003 where"
11288
" ((T0002.SERVERGRP = T0001.SERVERGRP) and"
11289
" (T0002.SERVERGRP = T0003.SERVERGRP)"
11290
" and (T0003.MAPSTATE = _latin1'A') and"
11291
" (T0003.ACTSTATE = _latin1' '))");
11294
stmt= mysql_stmt_init(mysql);
11295
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11296
check_execute(stmt, rc);
11298
strmov(str_data, "TEST");
11299
bzero((char*) my_bind, sizeof(my_bind));
11300
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
11301
my_bind[0].buffer= (char *)&str_data;
11302
my_bind[0].buffer_length= 50;
11303
my_bind[0].length= &length;
11305
my_bind[0].is_null= (char*)&is_null;
11306
rc= mysql_stmt_bind_param(stmt, my_bind);
11307
check_execute(stmt,rc);
11309
for (i= 0; i < 3; i++)
11311
rc= mysql_stmt_execute(stmt);
11312
check_execute(stmt, rc);
11313
rc= my_process_stmt_result(stmt);
11314
DIE_UNLESS(1 == rc);
11316
mysql_stmt_close(stmt);
11318
rc= mysql_query(mysql, "DROP TABLE t1,t2,t3");
11320
rc= mysql_query(mysql, "DROP VIEW v1");
11325
static void test_view_where()
11330
"select v1.c,v2.c from v1, v2";
11332
myheader("test_view_where");
11334
rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1,v2");
11337
rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,v2,t1");
11339
rc= mysql_query(mysql,"CREATE TABLE t1 (a int, b int)");
11341
rc= mysql_query(mysql,"insert into t1 values (1,2), (1,3), (2,4), (2,5), (3,10)");
11343
rc= mysql_query(mysql,"create view v1 (c) as select b from t1 where a<3");
11345
rc= mysql_query(mysql,"create view v2 (c) as select b from t1 where a>=3");
11348
stmt= mysql_stmt_init(mysql);
11349
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11350
check_execute(stmt, rc);
11352
for (i= 0; i < 3; i++)
11354
rc= mysql_stmt_execute(stmt);
11355
check_execute(stmt, rc);
11356
rc= my_process_stmt_result(stmt);
11357
DIE_UNLESS(4 == rc);
11359
mysql_stmt_close(stmt);
11361
rc= mysql_query(mysql, "DROP TABLE t1");
11363
rc= mysql_query(mysql, "DROP VIEW v1, v2");
11368
static void test_view_2where()
11372
MYSQL_BIND my_bind[8];
11373
char parms[8][100];
11376
"select relid, report, handle, log_group, username, variant, type, "
11377
"version, erfdat, erftime, erfname, aedat, aetime, aename, dependvars, "
11378
"inactive from V_LTDX where mandt = ? and relid = ? and report = ? and "
11379
"handle = ? and log_group = ? and username in ( ? , ? ) and type = ?";
11381
myheader("test_view_2where");
11383
rc= mysql_query(mysql, "DROP TABLE IF EXISTS LTDX");
11385
rc= mysql_query(mysql, "DROP VIEW IF EXISTS V_LTDX");
11387
rc= mysql_query(mysql,
11388
"CREATE TABLE LTDX (MANDT char(3) NOT NULL default '000', "
11389
" RELID char(2) NOT NULL, REPORT varchar(40) NOT NULL,"
11390
" HANDLE varchar(4) NOT NULL, LOG_GROUP varchar(4) NOT NULL,"
11391
" USERNAME varchar(12) NOT NULL,"
11392
" VARIANT varchar(12) NOT NULL,"
11393
" TYPE char(1) NOT NULL, SRTF2 int(11) NOT NULL,"
11394
" VERSION varchar(6) NOT NULL default '000000',"
11395
" ERFDAT varchar(8) NOT NULL default '00000000',"
11396
" ERFTIME varchar(6) NOT NULL default '000000',"
11397
" ERFNAME varchar(12) NOT NULL,"
11398
" AEDAT varchar(8) NOT NULL default '00000000',"
11399
" AETIME varchar(6) NOT NULL default '000000',"
11400
" AENAME varchar(12) NOT NULL,"
11401
" DEPENDVARS varchar(10) NOT NULL,"
11402
" INACTIVE char(1) NOT NULL, CLUSTR smallint(6) NOT NULL,"
11404
" PRIMARY KEY (MANDT, RELID, REPORT, HANDLE, LOG_GROUP, "
11405
"USERNAME, VARIANT, TYPE, SRTF2))"
11406
" CHARSET=latin1 COLLATE latin1_bin");
11408
rc= mysql_query(mysql,
11409
"CREATE VIEW V_LTDX AS select T0001.MANDT AS "
11410
" MANDT,T0001.RELID AS RELID,T0001.REPORT AS "
11411
" REPORT,T0001.HANDLE AS HANDLE,T0001.LOG_GROUP AS "
11412
" LOG_GROUP,T0001.USERNAME AS USERNAME,T0001.VARIANT AS "
11413
" VARIANT,T0001.TYPE AS TYPE,T0001.VERSION AS "
11414
" VERSION,T0001.ERFDAT AS ERFDAT,T0001.ERFTIME AS "
11415
" ERFTIME,T0001.ERFNAME AS ERFNAME,T0001.AEDAT AS "
11416
" AEDAT,T0001.AETIME AS AETIME,T0001.AENAME AS "
11417
" AENAME,T0001.DEPENDVARS AS DEPENDVARS,T0001.INACTIVE AS "
11418
" INACTIVE from LTDX T0001 where (T0001.SRTF2 = 0)");
11420
bzero((char*) my_bind, sizeof(my_bind));
11421
for (i=0; i < 8; i++) {
11422
strmov(parms[i], "1");
11423
my_bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
11424
my_bind[i].buffer = (char *)&parms[i];
11425
my_bind[i].buffer_length = 100;
11426
my_bind[i].is_null = 0;
11427
my_bind[i].length = &length[i];
11430
stmt= mysql_stmt_init(mysql);
11431
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11432
check_execute(stmt, rc);
11434
rc= mysql_stmt_bind_param(stmt, my_bind);
11435
check_execute(stmt,rc);
11437
rc= mysql_stmt_execute(stmt);
11438
check_execute(stmt, rc);
11439
rc= my_process_stmt_result(stmt);
11440
DIE_UNLESS(0 == rc);
11442
mysql_stmt_close(stmt);
11444
rc= mysql_query(mysql, "DROP VIEW V_LTDX");
11446
rc= mysql_query(mysql, "DROP TABLE LTDX");
11451
static void test_view_star()
11455
MYSQL_BIND my_bind[8];
11456
char parms[8][100];
11458
const char *query= "SELECT * FROM vt1 WHERE a IN (?,?)";
11460
myheader("test_view_star");
11462
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, vt1");
11464
rc= mysql_query(mysql, "DROP VIEW IF EXISTS t1, vt1");
11466
rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
11468
rc= mysql_query(mysql, "CREATE VIEW vt1 AS SELECT a FROM t1");
11470
bzero((char*) my_bind, sizeof(my_bind));
11471
for (i= 0; i < 2; i++) {
11472
sprintf((char *)&parms[i], "%d", i);
11473
my_bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
11474
my_bind[i].buffer = (char *)&parms[i];
11475
my_bind[i].buffer_length = 100;
11476
my_bind[i].is_null = 0;
11477
my_bind[i].length = &length[i];
11481
stmt= mysql_stmt_init(mysql);
11482
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11483
check_execute(stmt, rc);
11485
rc= mysql_stmt_bind_param(stmt, my_bind);
11486
check_execute(stmt,rc);
11488
for (i= 0; i < 3; i++)
11490
rc= mysql_stmt_execute(stmt);
11491
check_execute(stmt, rc);
11492
rc= my_process_stmt_result(stmt);
11493
DIE_UNLESS(0 == rc);
11496
mysql_stmt_close(stmt);
11498
rc= mysql_query(mysql, "DROP TABLE t1");
11500
rc= mysql_query(mysql, "DROP VIEW vt1");
11505
static void test_view_insert()
11507
MYSQL_STMT *insert_stmt, *select_stmt;
11509
MYSQL_BIND my_bind[1];
11511
ulong my_length = 0L;
11514
"insert into v1 values (?)";
11516
myheader("test_view_insert");
11518
rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
11520
rc = mysql_query(mysql, "DROP VIEW IF EXISTS t1,v1");
11523
rc= mysql_query(mysql,"create table t1 (a int, primary key (a))");
11526
rc= mysql_query(mysql, "create view v1 as select a from t1 where a>=1");
11529
insert_stmt= mysql_stmt_init(mysql);
11530
rc= mysql_stmt_prepare(insert_stmt, query, strlen(query));
11531
check_execute(insert_stmt, rc);
11532
query= "select * from t1";
11533
select_stmt= mysql_stmt_init(mysql);
11534
rc= mysql_stmt_prepare(select_stmt, query, strlen(query));
11535
check_execute(select_stmt, rc);
11537
bzero((char*) my_bind, sizeof(my_bind));
11538
my_bind[0].buffer_type = MYSQL_TYPE_LONG;
11539
my_bind[0].buffer = (char *)&my_val;
11540
my_bind[0].length = &my_length;
11541
my_bind[0].is_null = (char*)&my_null;
11542
rc= mysql_stmt_bind_param(insert_stmt, my_bind);
11543
check_execute(insert_stmt, rc);
11545
for (i= 0; i < 3; i++)
11550
rc= mysql_stmt_execute(insert_stmt);
11551
check_execute(insert_stmt, rc);
11553
rc= mysql_stmt_execute(select_stmt);
11554
check_execute(select_stmt, rc);
11555
rowcount= (int)my_process_stmt_result(select_stmt);
11556
DIE_UNLESS((i+1) == rowcount);
11558
mysql_stmt_close(insert_stmt);
11559
mysql_stmt_close(select_stmt);
11561
rc= mysql_query(mysql, "DROP VIEW v1");
11563
rc= mysql_query(mysql, "DROP TABLE t1");
11568
static void test_left_join_view()
11573
"select t1.a, v1.x from t1 left join v1 on (t1.a= v1.x);";
11575
myheader("test_left_join_view");
11577
rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
11580
rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1");
11582
rc= mysql_query(mysql,"CREATE TABLE t1 (a int)");
11584
rc= mysql_query(mysql,"insert into t1 values (1), (2), (3)");
11586
rc= mysql_query(mysql,"create view v1 (x) as select a from t1 where a > 1");
11588
stmt= mysql_stmt_init(mysql);
11589
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11590
check_execute(stmt, rc);
11592
for (i= 0; i < 3; i++)
11594
rc= mysql_stmt_execute(stmt);
11595
check_execute(stmt, rc);
11596
rc= my_process_stmt_result(stmt);
11597
DIE_UNLESS(3 == rc);
11599
mysql_stmt_close(stmt);
11601
rc= mysql_query(mysql, "DROP VIEW v1");
11603
rc= mysql_query(mysql, "DROP TABLE t1");
11608
static void test_view_insert_fields()
11611
char parm[11][1000];
11614
MYSQL_BIND my_bind[11];
11615
const char *query= "INSERT INTO `v1` ( `K1C4` ,`K2C4` ,`K3C4` ,`K4N4` ,`F1C4` ,`F2I4` ,`F3N5` ,`F7F8` ,`F6N4` ,`F5C8` ,`F9D8` ) VALUES( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )";
11617
myheader("test_view_insert_fields");
11619
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, v1");
11621
rc= mysql_query(mysql, "DROP VIEW IF EXISTS t1, v1");
11623
rc= mysql_query(mysql,
11624
"CREATE TABLE t1 (K1C4 varchar(4) NOT NULL,"
11625
"K2C4 varchar(4) NOT NULL, K3C4 varchar(4) NOT NULL,"
11626
"K4N4 varchar(4) NOT NULL default '0000',"
11627
"F1C4 varchar(4) NOT NULL, F2I4 int(11) NOT NULL,"
11628
"F3N5 varchar(5) NOT NULL default '00000',"
11629
"F4I4 int(11) NOT NULL default '0', F5C8 varchar(8) NOT NULL,"
11630
"F6N4 varchar(4) NOT NULL default '0000',"
11631
"F7F8 double NOT NULL default '0',"
11632
"F8F8 double NOT NULL default '0',"
11633
"F9D8 decimal(8,2) NOT NULL default '0.00',"
11634
"PRIMARY KEY (K1C4,K2C4,K3C4,K4N4)) "
11635
"CHARSET=latin1 COLLATE latin1_bin");
11637
rc= mysql_query(mysql,
11638
"CREATE VIEW v1 AS select sql_no_cache "
11639
" K1C4 AS K1C4, K2C4 AS K2C4, K3C4 AS K3C4, K4N4 AS K4N4, "
11640
" F1C4 AS F1C4, F2I4 AS F2I4, F3N5 AS F3N5,"
11641
" F7F8 AS F7F8, F6N4 AS F6N4, F5C8 AS F5C8, F9D8 AS F9D8"
11644
bzero((char*) my_bind, sizeof(my_bind));
11645
for (i= 0; i < 11; i++)
11648
my_bind[i].buffer_type= MYSQL_TYPE_STRING;
11649
my_bind[i].is_null= 0;
11650
my_bind[i].buffer= (char *)&parm[i];
11652
strmov(parm[i], "1");
11653
my_bind[i].buffer_length= 2;
11654
my_bind[i].length= &l[i];
11656
stmt= mysql_stmt_init(mysql);
11657
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11658
check_execute(stmt, rc);
11659
rc= mysql_stmt_bind_param(stmt, my_bind);
11660
check_execute(stmt, rc);
11662
rc= mysql_stmt_execute(stmt);
11663
check_execute(stmt, rc);
11664
mysql_stmt_close(stmt);
11666
query= "select * from t1";
11667
stmt= mysql_stmt_init(mysql);
11668
rc= mysql_stmt_prepare(stmt, query, strlen(query));
11669
check_execute(stmt, rc);
11670
rc= mysql_stmt_execute(stmt);
11671
check_execute(stmt, rc);
11672
rc= my_process_stmt_result(stmt);
11673
DIE_UNLESS(1 == rc);
11675
mysql_stmt_close(stmt);
11676
rc= mysql_query(mysql, "DROP VIEW v1");
11678
rc= mysql_query(mysql, "DROP TABLE t1");
11683
static void test_bug5126()
11686
MYSQL_BIND my_bind[2];
11688
const char *stmt_text;
11691
myheader("test_bug5126");
11693
stmt_text= "DROP TABLE IF EXISTS t1";
11694
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11697
stmt_text= "CREATE TABLE t1 (a mediumint, b int)";
11698
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11701
stmt_text= "INSERT INTO t1 VALUES (8386608, 1)";
11702
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11705
stmt= mysql_stmt_init(mysql);
11706
stmt_text= "SELECT a, b FROM t1";
11707
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11708
check_execute(stmt, rc);
11709
rc= mysql_stmt_execute(stmt);
11710
check_execute(stmt, rc);
11712
/* Bind output buffers */
11713
bzero((char*) my_bind, sizeof(my_bind));
11715
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
11716
my_bind[0].buffer= &c1;
11717
my_bind[1].buffer_type= MYSQL_TYPE_LONG;
11718
my_bind[1].buffer= &c2;
11720
mysql_stmt_bind_result(stmt, my_bind);
11722
rc= mysql_stmt_fetch(stmt);
11723
DIE_UNLESS(rc == 0);
11724
DIE_UNLESS(c1 == 8386608 && c2 == 1);
11726
printf("%ld, %ld\n", (long) c1, (long) c2);
11727
mysql_stmt_close(stmt);
11731
static void test_bug4231()
11734
MYSQL_BIND my_bind[2];
11736
const char *stmt_text;
11739
myheader("test_bug4231");
11741
stmt_text= "DROP TABLE IF EXISTS t1";
11742
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11745
stmt_text= "CREATE TABLE t1 (a int)";
11746
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11749
stmt_text= "INSERT INTO t1 VALUES (1)";
11750
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11753
stmt= mysql_stmt_init(mysql);
11754
stmt_text= "SELECT a FROM t1 WHERE ? = ?";
11755
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
11756
check_execute(stmt, rc);
11758
/* Bind input buffers */
11759
bzero((char*) my_bind, sizeof(my_bind));
11760
bzero((char*) tm, sizeof(tm));
11762
my_bind[0].buffer_type= MYSQL_TYPE_DATE;
11763
my_bind[0].buffer= &tm[0];
11764
my_bind[1].buffer_type= MYSQL_TYPE_DATE;
11765
my_bind[1].buffer= &tm[1];
11767
mysql_stmt_bind_param(stmt, my_bind);
11768
check_execute(stmt, rc);
11771
First set server-side params to some non-zero non-equal values:
11772
then we will check that they are not used when client sends
11775
tm[0].time_type = MYSQL_TIMESTAMP_DATE;
11780
--tm[1].year; /* tm[0] != tm[1] */
11782
rc= mysql_stmt_execute(stmt);
11783
check_execute(stmt, rc);
11785
rc= mysql_stmt_fetch(stmt);
11787
/* binds are unequal, no rows should be returned */
11788
DIE_UNLESS(rc == MYSQL_NO_DATA);
11790
/* Set one of the dates to zero */
11791
tm[0].year= tm[0].month= tm[0].day= 0;
11793
mysql_stmt_execute(stmt);
11794
rc= mysql_stmt_fetch(stmt);
11795
DIE_UNLESS(rc == 0);
11797
mysql_stmt_close(stmt);
11798
stmt_text= "DROP TABLE t1";
11799
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11804
static void test_bug5399()
11807
Ascii 97 is 'a', which gets mapped to Ascii 65 'A' unless internal
11808
statement id hash in the server uses binary collation.
11810
#define NUM_OF_USED_STMT 97
11811
MYSQL_STMT *stmt_list[NUM_OF_USED_STMT];
11813
MYSQL_BIND my_bind[1];
11818
myheader("test_bug5399");
11820
bzero((char*) my_bind, sizeof(my_bind));
11821
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
11822
my_bind[0].buffer= &no;
11824
for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
11826
sprintf(buff, "select %d", (int) (stmt - stmt_list));
11827
*stmt= mysql_stmt_init(mysql);
11828
rc= mysql_stmt_prepare(*stmt, buff, strlen(buff));
11829
check_execute(*stmt, rc);
11830
mysql_stmt_bind_result(*stmt, my_bind);
11833
printf("%d statements prepared.\n", NUM_OF_USED_STMT);
11835
for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
11837
rc= mysql_stmt_execute(*stmt);
11838
check_execute(*stmt, rc);
11839
rc= mysql_stmt_store_result(*stmt);
11840
check_execute(*stmt, rc);
11841
rc= mysql_stmt_fetch(*stmt);
11842
DIE_UNLESS(rc == 0);
11843
DIE_UNLESS((int32) (stmt - stmt_list) == no);
11846
for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
11847
mysql_stmt_close(*stmt);
11848
#undef NUM_OF_USED_STMT
11852
static void test_bug5194()
11855
MYSQL_BIND *my_bind;
11858
int param_str_length;
11859
const char *stmt_text;
11861
float float_array[250] =
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.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11867
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11868
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11869
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11870
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11871
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11872
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11873
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11874
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
11875
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
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,
11878
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11879
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11880
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11881
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11882
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11883
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11884
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11885
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11886
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
11887
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25
11889
float *fa_ptr= float_array;
11890
/* Number of columns per row */
11891
const int COLUMN_COUNT= sizeof(float_array)/sizeof(*float_array);
11892
/* Number of rows per bulk insert to start with */
11893
const int MIN_ROWS_PER_INSERT= 262;
11894
/* Max number of rows per bulk insert to end with */
11895
const int MAX_ROWS_PER_INSERT= 300;
11896
const int MAX_PARAM_COUNT= COLUMN_COUNT*MAX_ROWS_PER_INSERT;
11897
const char *query_template= "insert into t1 values %s";
11898
const int CHARS_PER_PARAM= 5; /* space needed to place ", ?" in the query */
11899
const int uint16_max= 65535;
11902
myheader("test_bug5194");
11904
stmt_text= "drop table if exists t1";
11905
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11907
stmt_text= "create table if not exists t1"
11908
"(c1 float, c2 float, c3 float, c4 float, c5 float, c6 float, "
11909
"c7 float, c8 float, c9 float, c10 float, c11 float, c12 float, "
11910
"c13 float, c14 float, c15 float, c16 float, c17 float, c18 float, "
11911
"c19 float, c20 float, c21 float, c22 float, c23 float, c24 float, "
11912
"c25 float, c26 float, c27 float, c28 float, c29 float, c30 float, "
11913
"c31 float, c32 float, c33 float, c34 float, c35 float, c36 float, "
11914
"c37 float, c38 float, c39 float, c40 float, c41 float, c42 float, "
11915
"c43 float, c44 float, c45 float, c46 float, c47 float, c48 float, "
11916
"c49 float, c50 float, c51 float, c52 float, c53 float, c54 float, "
11917
"c55 float, c56 float, c57 float, c58 float, c59 float, c60 float, "
11918
"c61 float, c62 float, c63 float, c64 float, c65 float, c66 float, "
11919
"c67 float, c68 float, c69 float, c70 float, c71 float, c72 float, "
11920
"c73 float, c74 float, c75 float, c76 float, c77 float, c78 float, "
11921
"c79 float, c80 float, c81 float, c82 float, c83 float, c84 float, "
11922
"c85 float, c86 float, c87 float, c88 float, c89 float, c90 float, "
11923
"c91 float, c92 float, c93 float, c94 float, c95 float, c96 float, "
11924
"c97 float, c98 float, c99 float, c100 float, c101 float, c102 float, "
11925
"c103 float, c104 float, c105 float, c106 float, c107 float, c108 float, "
11926
"c109 float, c110 float, c111 float, c112 float, c113 float, c114 float, "
11927
"c115 float, c116 float, c117 float, c118 float, c119 float, c120 float, "
11928
"c121 float, c122 float, c123 float, c124 float, c125 float, c126 float, "
11929
"c127 float, c128 float, c129 float, c130 float, c131 float, c132 float, "
11930
"c133 float, c134 float, c135 float, c136 float, c137 float, c138 float, "
11931
"c139 float, c140 float, c141 float, c142 float, c143 float, c144 float, "
11932
"c145 float, c146 float, c147 float, c148 float, c149 float, c150 float, "
11933
"c151 float, c152 float, c153 float, c154 float, c155 float, c156 float, "
11934
"c157 float, c158 float, c159 float, c160 float, c161 float, c162 float, "
11935
"c163 float, c164 float, c165 float, c166 float, c167 float, c168 float, "
11936
"c169 float, c170 float, c171 float, c172 float, c173 float, c174 float, "
11937
"c175 float, c176 float, c177 float, c178 float, c179 float, c180 float, "
11938
"c181 float, c182 float, c183 float, c184 float, c185 float, c186 float, "
11939
"c187 float, c188 float, c189 float, c190 float, c191 float, c192 float, "
11940
"c193 float, c194 float, c195 float, c196 float, c197 float, c198 float, "
11941
"c199 float, c200 float, c201 float, c202 float, c203 float, c204 float, "
11942
"c205 float, c206 float, c207 float, c208 float, c209 float, c210 float, "
11943
"c211 float, c212 float, c213 float, c214 float, c215 float, c216 float, "
11944
"c217 float, c218 float, c219 float, c220 float, c221 float, c222 float, "
11945
"c223 float, c224 float, c225 float, c226 float, c227 float, c228 float, "
11946
"c229 float, c230 float, c231 float, c232 float, c233 float, c234 float, "
11947
"c235 float, c236 float, c237 float, c238 float, c239 float, c240 float, "
11948
"c241 float, c242 float, c243 float, c244 float, c245 float, c246 float, "
11949
"c247 float, c248 float, c249 float, c250 float)";
11950
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
11953
my_bind= (MYSQL_BIND*) malloc(MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
11954
query= (char*) malloc(strlen(query_template) +
11955
MAX_PARAM_COUNT * CHARS_PER_PARAM + 1);
11956
param_str= (char*) malloc(COLUMN_COUNT * CHARS_PER_PARAM);
11958
if (my_bind == 0 || query == 0 || param_str == 0)
11960
fprintf(stderr, "Can't allocate enough memory for query structs\n");
11970
stmt= mysql_stmt_init(mysql);
11972
/* setup a template for one row of parameters */
11973
sprintf(param_str, "(");
11974
for (i= 1; i < COLUMN_COUNT; ++i)
11975
strcat(param_str, "?, ");
11976
strcat(param_str, "?)");
11977
param_str_length= strlen(param_str);
11979
/* setup bind array */
11980
bzero((char*) my_bind, MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
11981
for (i= 0; i < MAX_PARAM_COUNT; ++i)
11983
my_bind[i].buffer_type= MYSQL_TYPE_FLOAT;
11984
my_bind[i].buffer= fa_ptr;
11985
if (++fa_ptr == float_array + COLUMN_COUNT)
11986
fa_ptr= float_array;
11990
Test each number of rows per bulk insert, so that we can see where
11993
for (nrows= MIN_ROWS_PER_INSERT; nrows <= MAX_ROWS_PER_INSERT; ++nrows)
11996
/* Create statement text for current number of rows */
11997
sprintf(query, query_template, param_str);
11998
query_ptr= query + strlen(query);
11999
for (i= 1; i < nrows; ++i)
12001
memcpy(query_ptr, ", ", 2);
12003
memcpy(query_ptr, param_str, param_str_length);
12004
query_ptr+= param_str_length;
12008
rc= mysql_stmt_prepare(stmt, query, query_ptr - query);
12009
if (rc && nrows * COLUMN_COUNT > uint16_max)
12012
printf("Failed to prepare a statement with %d placeholders "
12013
"(as expected).\n", nrows * COLUMN_COUNT);
12017
check_execute(stmt, rc);
12020
printf("Insert: query length= %d, row count= %d, param count= %lu\n",
12021
(int) strlen(query), nrows, mysql_stmt_param_count(stmt));
12023
/* bind the parameter array and execute the query */
12024
rc= mysql_stmt_bind_param(stmt, my_bind);
12025
check_execute(stmt, rc);
12027
rc= mysql_stmt_execute(stmt);
12028
check_execute(stmt, rc);
12029
mysql_stmt_reset(stmt);
12032
mysql_stmt_close(stmt);
12036
stmt_text= "drop table t1";
12037
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12042
static void test_bug5315()
12045
const char *stmt_text;
12048
myheader("test_bug5315");
12050
stmt_text= "SELECT 1";
12051
stmt= mysql_stmt_init(mysql);
12052
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12053
DIE_UNLESS(rc == 0);
12055
printf("Excuting mysql_change_user\n");
12056
mysql_change_user(mysql, opt_user, opt_password, current_db);
12058
printf("Excuting mysql_stmt_execute\n");
12059
rc= mysql_stmt_execute(stmt);
12060
DIE_UNLESS(rc != 0);
12064
printf("Got error (as expected): '%s'\n", mysql_stmt_error(stmt));
12066
/* check that connection is OK */
12068
printf("Excuting mysql_stmt_close\n");
12069
mysql_stmt_close(stmt);
12071
printf("Excuting mysql_stmt_init\n");
12072
stmt= mysql_stmt_init(mysql);
12073
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12074
DIE_UNLESS(rc == 0);
12075
rc= mysql_stmt_execute(stmt);
12076
DIE_UNLESS(rc == 0);
12077
mysql_stmt_close(stmt);
12081
static void test_bug6049()
12084
MYSQL_BIND my_bind[1];
12087
const char *stmt_text;
12092
myheader("test_bug6049");
12094
stmt_text= "SELECT MAKETIME(-25, 12, 12)";
12096
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12098
res= mysql_store_result(mysql);
12099
row= mysql_fetch_row(res);
12101
stmt= mysql_stmt_init(mysql);
12102
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12103
check_execute(stmt, rc);
12104
rc= mysql_stmt_execute(stmt);
12105
check_execute(stmt, rc);
12107
bzero((char*) my_bind, sizeof(my_bind));
12108
my_bind[0].buffer_type = MYSQL_TYPE_STRING;
12109
my_bind[0].buffer = &buffer;
12110
my_bind[0].buffer_length = sizeof(buffer);
12111
my_bind[0].length = &length;
12113
mysql_stmt_bind_result(stmt, my_bind);
12114
rc= mysql_stmt_fetch(stmt);
12115
DIE_UNLESS(rc == 0);
12119
printf("Result from query: %s\n", row[0]);
12120
printf("Result from prepared statement: %s\n", (char*) buffer);
12123
DIE_UNLESS(strcmp(row[0], (char*) buffer) == 0);
12125
mysql_free_result(res);
12126
mysql_stmt_close(stmt);
12130
static void test_bug6058()
12133
MYSQL_BIND my_bind[1];
12136
const char *stmt_text;
12141
myheader("test_bug6058");
12143
rc= mysql_query(mysql, "SET SQL_MODE=''");
12146
stmt_text= "SELECT CAST('0000-00-00' AS DATE)";
12148
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12150
res= mysql_store_result(mysql);
12151
row= mysql_fetch_row(res);
12153
stmt= mysql_stmt_init(mysql);
12154
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12155
check_execute(stmt, rc);
12156
rc= mysql_stmt_execute(stmt);
12157
check_execute(stmt, rc);
12159
bzero((char*) my_bind, sizeof(my_bind));
12160
my_bind[0].buffer_type = MYSQL_TYPE_STRING;
12161
my_bind[0].buffer = &buffer;
12162
my_bind[0].buffer_length = sizeof(buffer);
12163
my_bind[0].length = &length;
12165
mysql_stmt_bind_result(stmt, my_bind);
12166
rc= mysql_stmt_fetch(stmt);
12167
DIE_UNLESS(rc == 0);
12171
printf("Result from query: %s\n", row[0]);
12172
printf("Result from prepared statement: %s\n", buffer);
12175
DIE_UNLESS(strcmp(row[0], buffer) == 0);
12177
mysql_free_result(res);
12178
mysql_stmt_close(stmt);
12182
static void test_bug6059()
12185
const char *stmt_text;
12187
myheader("test_bug6059");
12189
stmt_text= "SELECT 'foo' INTO OUTFILE 'x.3'";
12191
stmt= mysql_stmt_init(mysql);
12192
(void) mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12193
DIE_UNLESS(mysql_stmt_field_count(stmt) == 0);
12194
mysql_stmt_close(stmt);
12198
static void test_bug6046()
12201
const char *stmt_text;
12204
MYSQL_BIND my_bind[1];
12206
myheader("test_bug6046");
12208
stmt_text= "DROP TABLE IF EXISTS t1";
12209
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12211
stmt_text= "CREATE TABLE t1 (a int, b int)";
12212
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12214
stmt_text= "INSERT INTO t1 VALUES (1,1),(2,2),(3,1),(4,2)";
12215
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12218
stmt= mysql_stmt_init(mysql);
12220
stmt_text= "SELECT t1.a FROM t1 NATURAL JOIN t1 as X1 "
12221
"WHERE t1.b > ? ORDER BY t1.a";
12223
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12224
check_execute(stmt, rc);
12227
bzero((char*) my_bind, sizeof(my_bind));
12228
my_bind[0].buffer= &b;
12229
my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
12231
mysql_stmt_bind_param(stmt, my_bind);
12233
rc= mysql_stmt_execute(stmt);
12234
check_execute(stmt, rc);
12235
mysql_stmt_store_result(stmt);
12237
rc= mysql_stmt_execute(stmt);
12238
check_execute(stmt, rc);
12240
mysql_stmt_close(stmt);
12245
static void test_basic_cursors()
12247
const char *basic_tables[]=
12249
"DROP TABLE IF EXISTS t1, t2",
12252
"(id INTEGER NOT NULL PRIMARY KEY, "
12253
" name VARCHAR(20) NOT NULL)",
12255
"INSERT INTO t1 (id, name) VALUES "
12256
" (2, 'Ja'), (3, 'Ede'), "
12257
" (4, 'Haag'), (5, 'Kabul'), "
12258
" (6, 'Almere'), (7, 'Utrecht'), "
12259
" (8, 'Qandahar'), (9, 'Amsterdam'), "
12260
" (10, 'Amersfoort'), (11, 'Constantine')",
12263
"(id INTEGER NOT NULL PRIMARY KEY, "
12264
" name VARCHAR(20) NOT NULL)",
12266
"INSERT INTO t2 (id, name) VALUES "
12267
" (4, 'Guam'), (5, 'Aruba'), "
12268
" (6, 'Angola'), (7, 'Albania'), "
12269
" (8, 'Anguilla'), (9, 'Argentina'), "
12270
" (10, 'Azerbaijan'), (11, 'Afghanistan'), "
12271
" (12, 'Burkina Faso'), (13, 'Faroe Islands')"
12273
const char *queries[]=
12275
"SELECT * FROM t1",
12279
DBUG_ENTER("test_basic_cursors");
12280
myheader("test_basic_cursors");
12282
fill_tables(basic_tables, sizeof(basic_tables)/sizeof(*basic_tables));
12284
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
12285
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
12290
static void test_cursors_with_union()
12292
const char *queries[]=
12294
"SELECT t1.name FROM t1 UNION SELECT t2.name FROM t2",
12295
"SELECT t1.id FROM t1 WHERE t1.id < 5"
12297
myheader("test_cursors_with_union");
12298
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
12299
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
12303
static void test_cursors_with_procedure()
12305
const char *queries[]=
12307
"SELECT * FROM t1 procedure analyse()"
12309
myheader("test_cursors_with_procedure");
12310
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
12311
fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
12316
Altough mysql_create_db(), mysql_rm_db() are deprecated since 4.0 they
12317
should not crash server and should not hang in case of errors.
12319
Since those functions can't be seen in modern API (unless client library
12320
was compiled with USE_OLD_FUNCTIONS define) we use simple_command() macro.
12322
static void test_bug6081()
12325
myheader("test_bug6081");
12327
rc= simple_command(mysql, COM_DROP_DB, (uchar*) current_db,
12328
(ulong)strlen(current_db), 0);
12329
if (rc == 0 && mysql_errno(mysql) != ER_UNKNOWN_COM_ERROR)
12331
myerror(NULL); /* purecov: inspected */
12332
die(__FILE__, __LINE__, "COM_DROP_DB failed"); /* purecov: inspected */
12334
rc= simple_command(mysql, COM_DROP_DB, (uchar*) current_db,
12335
(ulong)strlen(current_db), 0);
12337
rc= simple_command(mysql, COM_CREATE_DB, (uchar*) current_db,
12338
(ulong)strlen(current_db), 0);
12339
if (rc == 0 && mysql_errno(mysql) != ER_UNKNOWN_COM_ERROR)
12341
myerror(NULL); /* purecov: inspected */
12342
die(__FILE__, __LINE__, "COM_CREATE_DB failed"); /* purecov: inspected */
12344
rc= simple_command(mysql, COM_CREATE_DB, (uchar*) current_db,
12345
(ulong)strlen(current_db), 0);
12347
rc= mysql_select_db(mysql, current_db);
12352
static void test_bug6096()
12355
MYSQL_RES *query_result, *stmt_metadata;
12356
const char *stmt_text;
12357
MYSQL_BIND my_bind[12];
12358
MYSQL_FIELD *query_field_list, *stmt_field_list;
12359
ulong query_field_count, stmt_field_count;
12361
my_bool update_max_length= TRUE;
12364
myheader("test_bug6096");
12366
stmt_text= "drop table if exists t1";
12367
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12370
mysql_query(mysql, "set sql_mode=''");
12371
stmt_text= "create table t1 (c_tinyint tinyint, c_smallint smallint, "
12372
" c_mediumint mediumint, c_int int, "
12373
" c_bigint bigint, c_float float, "
12374
" c_double double, c_varchar varchar(20), "
12375
" c_char char(20), c_time time, c_date date, "
12376
" c_datetime datetime)";
12377
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12379
stmt_text= "insert into t1 values (-100, -20000, 30000000, 4, 8, 1.0, "
12380
"2.0, 'abc', 'def', now(), now(), now())";
12381
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12384
stmt_text= "select * from t1";
12386
/* Run select in prepared and non-prepared mode and compare metadata */
12387
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12389
query_result= mysql_store_result(mysql);
12390
query_field_list= mysql_fetch_fields(query_result);
12391
query_field_count= mysql_num_fields(query_result);
12393
stmt= mysql_stmt_init(mysql);
12394
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12395
check_execute(stmt, rc);
12396
rc= mysql_stmt_execute(stmt);
12397
check_execute(stmt, rc);
12398
mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH,
12399
(void*) &update_max_length);
12400
mysql_stmt_store_result(stmt);
12401
stmt_metadata= mysql_stmt_result_metadata(stmt);
12402
stmt_field_list= mysql_fetch_fields(stmt_metadata);
12403
stmt_field_count= mysql_num_fields(stmt_metadata);
12404
DIE_UNLESS(stmt_field_count == query_field_count);
12406
/* Print out and check the metadata */
12410
printf(" ------------------------------------------------------------\n");
12411
printf(" | Metadata \n");
12412
printf(" ------------------------------------------------------------\n");
12413
printf(" | Query | Prepared statement \n");
12414
printf(" ------------------------------------------------------------\n");
12415
printf(" field name | length | max_length | length | max_length\n");
12416
printf(" ------------------------------------------------------------\n");
12418
for (i= 0; i < query_field_count; ++i)
12420
MYSQL_FIELD *f1= &query_field_list[i], *f2= &stmt_field_list[i];
12421
printf(" %-11s | %9lu | %10lu | %9lu | %10lu \n",
12422
f1->name, f1->length, f1->max_length, f2->length, f2->max_length);
12423
DIE_UNLESS(f1->length == f2->length);
12425
printf(" ---------------------------------------------------------------\n");
12428
/* Bind and fetch the data */
12430
bzero((char*) my_bind, sizeof(my_bind));
12431
for (i= 0; i < stmt_field_count; ++i)
12433
my_bind[i].buffer_type= MYSQL_TYPE_STRING;
12434
my_bind[i].buffer_length= stmt_field_list[i].max_length + 1;
12435
my_bind[i].buffer= malloc(my_bind[i].buffer_length);
12437
mysql_stmt_bind_result(stmt, my_bind);
12438
rc= mysql_stmt_fetch(stmt);
12439
check_execute(stmt, rc);
12440
rc= mysql_stmt_fetch(stmt);
12441
DIE_UNLESS(rc == MYSQL_NO_DATA);
12445
for (i= 0; i < stmt_field_count; ++i)
12446
free(my_bind[i].buffer);
12447
mysql_stmt_close(stmt);
12448
mysql_free_result(query_result);
12449
mysql_free_result(stmt_metadata);
12450
stmt_text= "drop table t1";
12451
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12457
Test of basic checks that are performed in server for components
12458
of MYSQL_TIME parameters.
12461
static void test_datetime_ranges()
12463
const char *stmt_text;
12466
MYSQL_BIND my_bind[6];
12469
myheader("test_datetime_ranges");
12471
stmt_text= "drop table if exists t1";
12472
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12475
stmt_text= "create table t1 (year datetime, month datetime, day datetime, "
12476
"hour datetime, min datetime, sec datetime)";
12477
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12480
stmt= mysql_simple_prepare(mysql,
12481
"INSERT INTO t1 VALUES (?, ?, ?, ?, ?, ?)");
12483
verify_param_count(stmt, 6);
12485
bzero((char*) my_bind, sizeof(my_bind));
12486
for (i= 0; i < 6; i++)
12488
my_bind[i].buffer_type= MYSQL_TYPE_DATETIME;
12489
my_bind[i].buffer= &tm[i];
12491
rc= mysql_stmt_bind_param(stmt, my_bind);
12492
check_execute(stmt, rc);
12494
tm[0].year= 2004; tm[0].month= 11; tm[0].day= 10;
12495
tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
12496
tm[0].second_part= 0; tm[0].neg= 0;
12498
tm[5]= tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
12499
tm[0].year= 10000; tm[1].month= 13; tm[2].day= 32;
12500
tm[3].hour= 24; tm[4].minute= 60; tm[5].second= 60;
12502
rc= mysql_stmt_execute(stmt);
12503
check_execute(stmt, rc);
12504
DIE_UNLESS(mysql_warning_count(mysql) != 6);
12506
verify_col_data("t1", "year", "0000-00-00 00:00:00");
12507
verify_col_data("t1", "month", "0000-00-00 00:00:00");
12508
verify_col_data("t1", "day", "0000-00-00 00:00:00");
12509
verify_col_data("t1", "hour", "0000-00-00 00:00:00");
12510
verify_col_data("t1", "min", "0000-00-00 00:00:00");
12511
verify_col_data("t1", "sec", "0000-00-00 00:00:00");
12513
mysql_stmt_close(stmt);
12515
stmt_text= "delete from t1";
12516
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12519
stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 (year, month, day) "
12520
"VALUES (?, ?, ?)");
12522
verify_param_count(stmt, 3);
12525
We reuse contents of bind and tm arrays left from previous part of test.
12527
for (i= 0; i < 3; i++)
12528
my_bind[i].buffer_type= MYSQL_TYPE_DATE;
12530
rc= mysql_stmt_bind_param(stmt, my_bind);
12531
check_execute(stmt, rc);
12533
rc= mysql_stmt_execute(stmt);
12534
check_execute(stmt, rc);
12535
DIE_UNLESS(mysql_warning_count(mysql) != 3);
12537
verify_col_data("t1", "year", "0000-00-00 00:00:00");
12538
verify_col_data("t1", "month", "0000-00-00 00:00:00");
12539
verify_col_data("t1", "day", "0000-00-00 00:00:00");
12541
mysql_stmt_close(stmt);
12543
stmt_text= "drop table t1";
12544
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12547
stmt_text= "create table t1 (day_ovfl time, day time, hour time, min time, sec time)";
12548
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12551
stmt= mysql_simple_prepare(mysql,
12552
"INSERT INTO t1 VALUES (?, ?, ?, ?, ?)");
12554
verify_param_count(stmt, 5);
12557
Again we reuse what we can from previous part of test.
12559
for (i= 0; i < 5; i++)
12560
my_bind[i].buffer_type= MYSQL_TYPE_TIME;
12562
rc= mysql_stmt_bind_param(stmt, my_bind);
12563
check_execute(stmt, rc);
12565
tm[0].year= 0; tm[0].month= 0; tm[0].day= 10;
12566
tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
12567
tm[0].second_part= 0; tm[0].neg= 0;
12569
tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
12570
tm[0].day= 35; tm[1].day= 34; tm[2].hour= 30; tm[3].minute= 60; tm[4].second= 60;
12572
rc= mysql_stmt_execute(stmt);
12573
check_execute(stmt, rc);
12574
DIE_UNLESS(mysql_warning_count(mysql) == 2);
12576
verify_col_data("t1", "day_ovfl", "838:59:59");
12577
verify_col_data("t1", "day", "828:30:30");
12578
verify_col_data("t1", "hour", "270:30:30");
12579
verify_col_data("t1", "min", "00:00:00");
12580
verify_col_data("t1", "sec", "00:00:00");
12582
mysql_stmt_close(stmt);
12584
stmt_text= "drop table t1";
12585
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12590
static void test_bug4172()
12593
MYSQL_BIND my_bind[3];
12594
const char *stmt_text;
12598
char f[100], d[100], e[100];
12599
ulong f_len, d_len, e_len;
12601
myheader("test_bug4172");
12603
mysql_query(mysql, "DROP TABLE IF EXISTS t1");
12604
mysql_query(mysql, "CREATE TABLE t1 (f float, d double, e decimal(10,4))");
12605
mysql_query(mysql, "INSERT INTO t1 VALUES (12345.1234, 123456.123456, "
12608
stmt= mysql_stmt_init(mysql);
12609
stmt_text= "SELECT f, d, e FROM t1";
12611
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12612
check_execute(stmt, rc);
12613
rc= mysql_stmt_execute(stmt);
12614
check_execute(stmt, rc);
12616
bzero((char*) my_bind, sizeof(my_bind));
12617
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
12618
my_bind[0].buffer= f;
12619
my_bind[0].buffer_length= sizeof(f);
12620
my_bind[0].length= &f_len;
12621
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
12622
my_bind[1].buffer= d;
12623
my_bind[1].buffer_length= sizeof(d);
12624
my_bind[1].length= &d_len;
12625
my_bind[2].buffer_type= MYSQL_TYPE_STRING;
12626
my_bind[2].buffer= e;
12627
my_bind[2].buffer_length= sizeof(e);
12628
my_bind[2].length= &e_len;
12630
mysql_stmt_bind_result(stmt, my_bind);
12632
mysql_stmt_store_result(stmt);
12633
rc= mysql_stmt_fetch(stmt);
12634
check_execute(stmt, rc);
12636
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12638
res= mysql_store_result(mysql);
12639
row= mysql_fetch_row(res);
12643
printf("Binary protocol: float=%s, double=%s, decimal(10,4)=%s\n",
12645
printf("Text protocol: float=%s, double=%s, decimal(10,4)=%s\n",
12646
row[0], row[1], row[2]);
12648
DIE_UNLESS(!strcmp(f, row[0]) && !strcmp(d, row[1]) && !strcmp(e, row[2]));
12650
mysql_free_result(res);
12651
mysql_stmt_close(stmt);
12655
static void test_conversion()
12658
const char *stmt_text;
12660
MYSQL_BIND my_bind[1];
12664
myheader("test_conversion");
12666
stmt_text= "DROP TABLE IF EXISTS t1";
12667
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12669
stmt_text= "CREATE TABLE t1 (a TEXT) DEFAULT CHARSET latin1";
12670
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12672
stmt_text= "SET character_set_connection=utf8, character_set_client=utf8, "
12673
" character_set_results=latin1";
12674
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12677
stmt= mysql_stmt_init(mysql);
12679
stmt_text= "INSERT INTO t1 (a) VALUES (?)";
12680
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12681
check_execute(stmt, rc);
12683
bzero((char*) my_bind, sizeof(my_bind));
12684
my_bind[0].buffer= buff;
12685
my_bind[0].length= &length;
12686
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
12688
mysql_stmt_bind_param(stmt, my_bind);
12690
buff[0]= (uchar) 0xC3;
12691
buff[1]= (uchar) 0xA0;
12694
rc= mysql_stmt_execute(stmt);
12695
check_execute(stmt, rc);
12697
stmt_text= "SELECT a FROM t1";
12698
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12699
check_execute(stmt, rc);
12700
rc= mysql_stmt_execute(stmt);
12701
check_execute(stmt, rc);
12703
my_bind[0].buffer_length= sizeof(buff);
12704
mysql_stmt_bind_result(stmt, my_bind);
12706
rc= mysql_stmt_fetch(stmt);
12707
DIE_UNLESS(rc == 0);
12708
DIE_UNLESS(length == 1);
12709
DIE_UNLESS((uchar) buff[0] == 0xE0);
12710
rc= mysql_stmt_fetch(stmt);
12711
DIE_UNLESS(rc == MYSQL_NO_DATA);
12713
mysql_stmt_close(stmt);
12714
stmt_text= "DROP TABLE t1";
12715
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12717
stmt_text= "SET NAMES DEFAULT";
12718
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12722
static void test_rewind(void)
12725
MYSQL_BIND my_bind;
12727
const char *stmt_text;
12728
long unsigned int length=4, Data=0;
12731
myheader("test_rewind");
12733
stmt_text= "CREATE TABLE t1 (a int)";
12734
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12736
stmt_text= "INSERT INTO t1 VALUES(2),(3),(4)";
12737
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12740
stmt= mysql_stmt_init(mysql);
12742
stmt_text= "SELECT * FROM t1";
12743
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12744
check_execute(stmt, rc);
12746
bzero((char*) &my_bind, sizeof(MYSQL_BIND));
12747
my_bind.buffer_type= MYSQL_TYPE_LONG;
12748
my_bind.buffer= (void *)&Data; /* this buffer won't be altered */
12749
my_bind.length= &length;
12750
my_bind.is_null= &isnull;
12752
rc= mysql_stmt_execute(stmt);
12753
check_execute(stmt, rc);
12755
rc= mysql_stmt_store_result(stmt);
12756
DIE_UNLESS(rc == 0);
12758
rc= mysql_stmt_bind_result(stmt, &my_bind);
12759
DIE_UNLESS(rc == 0);
12761
/* retreive all result sets till we are at the end */
12762
while(!mysql_stmt_fetch(stmt))
12764
printf("fetched result:%ld\n", Data);
12766
DIE_UNLESS(rc != MYSQL_NO_DATA);
12768
/* seek to the first row */
12769
mysql_stmt_data_seek(stmt, 0);
12771
/* now we should be able to fetch the results again */
12772
/* but mysql_stmt_fetch returns MYSQL_NO_DATA */
12773
while(!(rc= mysql_stmt_fetch(stmt)))
12775
printf("fetched result after seek:%ld\n", Data);
12777
DIE_UNLESS(rc == MYSQL_NO_DATA);
12779
stmt_text= "DROP TABLE t1";
12780
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12782
rc= mysql_stmt_free_result(stmt);
12783
rc= mysql_stmt_close(stmt);
12787
static void test_truncation()
12790
const char *stmt_text;
12793
MYSQL_BIND *bind_array, *my_bind;
12795
myheader("test_truncation");
12797
/* Prepare the test table */
12798
rc= mysql_query(mysql, "drop table if exists t1");
12801
stmt_text= "create table t1 ("
12802
"i8 tinyint, ui8 tinyint unsigned, "
12803
"i16 smallint, i16_1 smallint, "
12804
"ui16 smallint unsigned, i32 int, i32_1 int, "
12805
"d double, d_1 double, ch char(30), ch_1 char(30), "
12806
"tx text, tx_1 text, ch_2 char(30) "
12808
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12810
stmt_text= "insert into t1 VALUES ("
12813
"32000, " /* i16 */
12814
"-32767, " /* i16_1 */
12815
"64000, " /* ui16 */
12816
"1073741824, " /* i32 */
12817
"1073741825, " /* i32_1 */
12818
"123.456, " /* d */
12819
"-12345678910, " /* d_1 */
12820
"'111111111111111111111111111111',"/* ch */
12821
"'abcdef', " /* ch_1 */
12822
"'12345 ', " /* tx */
12823
"'12345.67 ', " /* tx_1 */
12824
"'12345.67abc'" /* ch_2 */
12826
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
12829
stmt_text= "select i8 c1, i8 c2, ui8 c3, i16_1 c4, ui16 c5, "
12830
" i16 c6, ui16 c7, i32 c8, i32_1 c9, i32_1 c10, "
12831
" d c11, d_1 c12, d_1 c13, ch c14, ch_1 c15, tx c16, "
12832
" tx_1 c17, ch_2 c18 "
12835
stmt= mysql_stmt_init(mysql);
12836
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
12837
check_execute(stmt, rc);
12838
rc= mysql_stmt_execute(stmt);
12839
check_execute(stmt, rc);
12840
bind_count= (uint) mysql_stmt_field_count(stmt);
12842
/*************** Fill in the bind structure and bind it **************/
12843
bind_array= malloc(sizeof(MYSQL_BIND) * bind_count);
12844
bzero((char*) bind_array, sizeof(MYSQL_BIND) * bind_count);
12845
for (my_bind= bind_array; my_bind < bind_array + bind_count; my_bind++)
12846
my_bind->error= &my_bind->error_value;
12847
my_bind= bind_array;
12849
my_bind->buffer= malloc(sizeof(uint8));
12850
my_bind->buffer_type= MYSQL_TYPE_TINY;
12851
my_bind->is_unsigned= TRUE;
12853
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12854
my_bind->buffer= malloc(sizeof(uint32));
12855
my_bind->buffer_type= MYSQL_TYPE_LONG;
12856
my_bind->is_unsigned= TRUE;
12858
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12859
my_bind->buffer= malloc(sizeof(int8));
12860
my_bind->buffer_type= MYSQL_TYPE_TINY;
12862
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12863
my_bind->buffer= malloc(sizeof(uint16));
12864
my_bind->buffer_type= MYSQL_TYPE_SHORT;
12865
my_bind->is_unsigned= TRUE;
12867
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12868
my_bind->buffer= malloc(sizeof(int16));
12869
my_bind->buffer_type= MYSQL_TYPE_SHORT;
12871
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12872
my_bind->buffer= malloc(sizeof(uint16));
12873
my_bind->buffer_type= MYSQL_TYPE_SHORT;
12874
my_bind->is_unsigned= TRUE;
12876
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12877
my_bind->buffer= malloc(sizeof(int8));
12878
my_bind->buffer_type= MYSQL_TYPE_TINY;
12879
my_bind->is_unsigned= TRUE;
12881
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12882
my_bind->buffer= malloc(sizeof(float));
12883
my_bind->buffer_type= MYSQL_TYPE_FLOAT;
12885
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12886
my_bind->buffer= malloc(sizeof(float));
12887
my_bind->buffer_type= MYSQL_TYPE_FLOAT;
12889
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12890
my_bind->buffer= malloc(sizeof(double));
12891
my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
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(ulonglong));
12899
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12900
my_bind->is_unsigned= TRUE;
12902
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12903
my_bind->buffer= malloc(sizeof(longlong));
12904
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12906
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12907
my_bind->buffer= malloc(sizeof(longlong));
12908
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12910
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12911
my_bind->buffer= malloc(sizeof(longlong));
12912
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12914
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12915
my_bind->buffer= malloc(sizeof(longlong));
12916
my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
12918
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12919
my_bind->buffer= malloc(sizeof(double));
12920
my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
12922
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12923
my_bind->buffer= malloc(sizeof(double));
12924
my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
12926
rc= mysql_stmt_bind_result(stmt, bind_array);
12927
check_execute(stmt, rc);
12928
rc= mysql_stmt_fetch(stmt);
12929
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
12931
/*************** Verify truncation results ***************************/
12932
my_bind= bind_array;
12934
/* signed tiny -> tiny */
12935
DIE_UNLESS(*my_bind->error && * (int8*) my_bind->buffer == -10);
12937
/* signed tiny -> uint32 */
12938
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12939
DIE_UNLESS(*my_bind->error && * (int32*) my_bind->buffer == -10);
12941
/* unsigned tiny -> tiny */
12942
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12943
DIE_UNLESS(*my_bind->error && * (uint8*) my_bind->buffer == 200);
12945
/* short -> ushort */
12946
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12947
DIE_UNLESS(*my_bind->error && * (int16*) my_bind->buffer == -32767);
12949
/* ushort -> short */
12950
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12951
DIE_UNLESS(*my_bind->error && * (uint16*) my_bind->buffer == 64000);
12953
/* short -> ushort (no truncation, data is in the range of target type) */
12954
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12955
DIE_UNLESS(! *my_bind->error && * (uint16*) my_bind->buffer == 32000);
12957
/* ushort -> utiny */
12958
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12959
DIE_UNLESS(*my_bind->error && * (int8*) my_bind->buffer == 0);
12961
/* int -> float: no truncation, the number is a power of two */
12962
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12963
DIE_UNLESS(! *my_bind->error && * (float*) my_bind->buffer == 1073741824);
12965
/* int -> float: truncation, not enough bits in float */
12966
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12967
DIE_UNLESS(*my_bind->error);
12969
/* int -> double: no truncation */
12970
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12971
DIE_UNLESS(! *my_bind->error && * (double*) my_bind->buffer == 1073741825);
12973
/* double -> longlong: fractional part is lost */
12974
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12976
/* double -> ulonglong, negative fp number to unsigned integer */
12977
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12978
/* Value in the buffer is not defined: don't test it */
12979
DIE_UNLESS(*my_bind->error);
12981
/* double -> longlong, negative fp number to signed integer: no loss */
12982
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12983
DIE_UNLESS(! *my_bind->error && * (longlong*) my_bind->buffer == LL(-12345678910));
12985
/* big numeric string -> number */
12986
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12987
DIE_UNLESS(*my_bind->error);
12989
/* junk string -> number */
12990
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12991
DIE_UNLESS(*my_bind->error && *(longlong*) my_bind->buffer == 0);
12993
/* string with trailing spaces -> number */
12994
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12995
DIE_UNLESS(! *my_bind->error && *(longlong*) my_bind->buffer == 12345);
12997
/* string with trailing spaces -> double */
12998
DIE_UNLESS(my_bind++ < bind_array + bind_count);
12999
DIE_UNLESS(! *my_bind->error && *(double*) my_bind->buffer == 12345.67);
13001
/* string with trailing junk -> double */
13002
DIE_UNLESS(my_bind++ < bind_array + bind_count);
13004
XXX: There must be a truncation error: but it's not the way the server
13005
behaves, so let's leave it for now.
13007
DIE_UNLESS(*(double*) my_bind->buffer == 12345.67);
13009
TODO: string -> double, double -> time, double -> string (truncation
13010
errors are not supported here yet)
13011
longlong -> time/date/datetime
13012
date -> time, date -> timestamp, date -> number
13013
time -> string, time -> date, time -> timestamp,
13014
number -> date string -> date
13016
/*************** Cleanup *********************************************/
13018
mysql_stmt_close(stmt);
13020
for (my_bind= bind_array; my_bind < bind_array + bind_count; my_bind++)
13021
free(my_bind->buffer);
13024
rc= mysql_query(mysql, "drop table t1");
13028
static void test_truncation_option()
13031
const char *stmt_text;
13036
MYSQL_BIND my_bind;
13038
myheader("test_truncation_option");
13040
/* Prepare the test table */
13041
stmt_text= "select -1";
13043
stmt= mysql_stmt_init(mysql);
13044
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13045
check_execute(stmt, rc);
13046
rc= mysql_stmt_execute(stmt);
13047
check_execute(stmt, rc);
13049
bzero((char*) &my_bind, sizeof(my_bind));
13051
my_bind.buffer= (void*) &buf;
13052
my_bind.buffer_type= MYSQL_TYPE_TINY;
13053
my_bind.is_unsigned= TRUE;
13054
my_bind.error= &error;
13056
rc= mysql_stmt_bind_result(stmt, &my_bind);
13057
check_execute(stmt, rc);
13058
rc= mysql_stmt_fetch(stmt);
13059
DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
13061
rc= mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option);
13063
/* need to rebind for the new setting to take effect */
13064
rc= mysql_stmt_bind_result(stmt, &my_bind);
13065
check_execute(stmt, rc);
13066
rc= mysql_stmt_execute(stmt);
13067
check_execute(stmt, rc);
13068
rc= mysql_stmt_fetch(stmt);
13069
check_execute(stmt, rc);
13070
/* The only change is rc - error pointers are still filled in */
13071
DIE_UNLESS(error == 1);
13072
/* restore back the defaults */
13074
mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option);
13076
mysql_stmt_close(stmt);
13080
/* Bug#6761 - mysql_list_fields doesn't work */
13082
static void test_bug6761(void)
13084
const char *stmt_text;
13087
myheader("test_bug6761");
13089
stmt_text= "CREATE TABLE t1 (a int, b char(255), c decimal)";
13090
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13093
res= mysql_list_fields(mysql, "t1", "%");
13094
DIE_UNLESS(res && mysql_num_fields(res) == 3);
13095
mysql_free_result(res);
13097
stmt_text= "DROP TABLE t1";
13098
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13103
/* Bug#8330 - mysql_stmt_execute crashes (libmysql) */
13105
static void test_bug8330()
13107
const char *stmt_text;
13108
MYSQL_STMT *stmt[2];
13110
const char *query= "select a,b from t1 where a=?";
13111
MYSQL_BIND my_bind[2];
13114
myheader("test_bug8330");
13116
stmt_text= "drop table if exists t1";
13117
/* in case some previos test failed */
13118
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13120
stmt_text= "create table t1 (a int, b int)";
13121
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13124
bzero((char*) my_bind, sizeof(my_bind));
13125
for (i=0; i < 2; i++)
13127
stmt[i]= mysql_stmt_init(mysql);
13128
rc= mysql_stmt_prepare(stmt[i], query, strlen(query));
13129
check_execute(stmt[i], rc);
13131
my_bind[i].buffer_type= MYSQL_TYPE_LONG;
13132
my_bind[i].buffer= (void*) &lval[i];
13133
my_bind[i].is_null= 0;
13134
mysql_stmt_bind_param(stmt[i], &my_bind[i]);
13137
rc= mysql_stmt_execute(stmt[0]);
13138
check_execute(stmt[0], rc);
13140
rc= mysql_stmt_execute(stmt[1]);
13141
DIE_UNLESS(rc && mysql_stmt_errno(stmt[1]) == CR_COMMANDS_OUT_OF_SYNC);
13142
rc= mysql_stmt_execute(stmt[0]);
13143
check_execute(stmt[0], rc);
13145
mysql_stmt_close(stmt[0]);
13146
mysql_stmt_close(stmt[1]);
13148
stmt_text= "drop table t1";
13149
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13154
/* Bug#7990 - mysql_stmt_close doesn't reset mysql->net.last_error */
13156
static void test_bug7990()
13160
myheader("test_bug7990");
13162
stmt= mysql_stmt_init(mysql);
13163
rc= mysql_stmt_prepare(stmt, "foo", 3);
13165
XXX: the fact that we store errno both in STMT and in
13166
MYSQL is not documented and is subject to change in 5.0
13168
DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql));
13169
mysql_stmt_close(stmt);
13170
DIE_UNLESS(!mysql_errno(mysql));
13174
Bug #15518 - Reusing a stmt that has failed during prepare
13175
does not clear error
13178
static void test_bug15518()
13183
myheader("test_bug15518");
13185
mysql1= mysql_client_init(NULL);
13187
if (!mysql_real_connect(mysql1, opt_host, opt_user, opt_password,
13188
opt_db ? opt_db : "test", opt_port, opt_unix_socket,
13189
CLIENT_MULTI_STATEMENTS))
13191
fprintf(stderr, "Failed to connect to the database\n");
13195
stmt= mysql_stmt_init(mysql1);
13198
The prepare of foo should fail with errno 1064 since
13199
it's not a valid query
13201
rc= mysql_stmt_prepare(stmt, "foo", 3);
13203
fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
13204
rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
13205
DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql1));
13208
Use the same stmt and reprepare with another query that
13211
rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
13213
fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
13214
rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
13215
DIE_UNLESS(!rc || mysql_stmt_errno(stmt) || mysql_errno(mysql1));
13217
mysql_stmt_close(stmt);
13218
DIE_UNLESS(!mysql_errno(mysql1));
13221
part2, when connection to server has been closed
13222
after first prepare
13224
stmt= mysql_stmt_init(mysql1);
13225
rc= mysql_stmt_prepare(stmt, "foo", 3);
13227
fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
13228
rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
13229
DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql1));
13231
/* Close connection to server */
13232
mysql_close(mysql1);
13235
Use the same stmt and reprepare with another query that
13236
suceeds. The prepare should fail with error 2013 since
13237
connection to server has been closed.
13239
rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
13241
fprintf(stdout, "rc: %d, mysql_stmt_errno: %d\n",
13242
rc, mysql_stmt_errno(stmt));
13243
DIE_UNLESS(rc && mysql_stmt_errno(stmt));
13245
mysql_stmt_close(stmt);
13249
static void disable_general_log()
13252
rc= mysql_query(mysql, "set @@global.general_log=off");
13257
static void enable_general_log(int truncate)
13261
rc= mysql_query(mysql, "set @save_global_general_log=@@global.general_log");
13264
rc= mysql_query(mysql, "set @@global.general_log=on");
13269
rc= mysql_query(mysql, "truncate mysql.general_log");
13275
static void restore_general_log()
13278
rc= mysql_query(mysql, "set @@global.general_log=@save_global_general_log");
13283
static void test_view_sp_list_fields()
13288
myheader("test_view_sp_list_fields");
13290
rc= mysql_query(mysql, "DROP FUNCTION IF EXISTS f1");
13292
rc= mysql_query(mysql, "DROP TABLE IF EXISTS v1, t1, t2");
13294
rc= mysql_query(mysql, "DROP VIEW IF EXISTS v1, t1, t2");
13296
rc= mysql_query(mysql, "create function f1 () returns int return 5");
13298
rc= mysql_query(mysql, "create table t1 (s1 char,s2 char)");
13300
rc= mysql_query(mysql, "create table t2 (s1 int);");
13302
rc= mysql_query(mysql, "create view v1 as select s2,sum(s1) - \
13303
count(s2) as vx from t1 group by s2 having sum(s1) - count(s2) < (select f1() \
13306
res= mysql_list_fields(mysql, "v1", NullS);
13307
DIE_UNLESS(res != 0 && mysql_num_fields(res) != 0);
13308
rc= mysql_query(mysql, "DROP FUNCTION f1");
13310
rc= mysql_query(mysql, "DROP VIEW v1");
13312
rc= mysql_query(mysql, "DROP TABLE t1, t2");
13313
mysql_free_result(res);
13320
Test mysql_real_escape_string() with gbk charset
13322
The important part is that 0x27 (') is the second-byte in a invalid
13323
two-byte GBK character here. But 0xbf5c is a valid GBK character, so
13324
it needs to be escaped as 0x5cbf27
13326
#define TEST_BUG8378_IN "\xef\xbb\xbf\x27\xbf\x10"
13327
#define TEST_BUG8378_OUT "\xef\xbb\x5c\xbf\x5c\x27\x5c\xbf\x10"
13329
static void test_bug8378()
13331
#if defined(HAVE_CHARSET_gbk) && !defined(EMBEDDED_LIBRARY)
13333
char out[9]; /* strlen(TEST_BUG8378)*2+1 */
13337
myheader("test_bug8378");
13340
fprintf(stdout, "\n Establishing a test connection ...");
13341
if (!(lmysql= mysql_client_init(NULL)))
13343
myerror("mysql_client_init() failed");
13346
if (mysql_options(lmysql, MYSQL_SET_CHARSET_NAME, "gbk"))
13348
myerror("mysql_options() failed");
13351
if (!(mysql_real_connect(lmysql, opt_host, opt_user,
13352
opt_password, current_db, opt_port,
13353
opt_unix_socket, 0)))
13355
myerror("connection failed");
13359
fprintf(stdout, "OK");
13361
rc= mysql_query(lmysql, "SET SQL_MODE=''");
13364
len= mysql_real_escape_string(lmysql, out, TEST_BUG8378_IN, 4);
13366
/* No escaping should have actually happened. */
13367
DIE_UNLESS(memcmp(out, TEST_BUG8378_OUT, len) == 0);
13369
sprintf(buf, "SELECT '%s'", out);
13371
rc=mysql_real_query(lmysql, buf, strlen(buf));
13374
mysql_close(lmysql);
13379
static void test_bug8722()
13383
const char *stmt_text;
13385
myheader("test_bug8722");
13386
/* Prepare test data */
13387
stmt_text= "drop table if exists t1, v1";
13388
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13390
stmt_text= "CREATE TABLE t1 (c1 varchar(10), c2 varchar(10), c3 varchar(10),"
13391
" c4 varchar(10), c5 varchar(10), c6 varchar(10),"
13392
" c7 varchar(10), c8 varchar(10), c9 varchar(10),"
13393
"c10 varchar(10))";
13394
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13396
stmt_text= "INSERT INTO t1 VALUES (1,2,3,4,5,6,7,8,9,10)";
13397
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13399
stmt_text= "CREATE VIEW v1 AS SELECT * FROM t1";
13400
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13402
/* Note: if you uncomment following block everything works fine */
13404
rc= mysql_query(mysql, "sellect * from v1");
13406
mysql_free_result(mysql_store_result(mysql));
13409
stmt= mysql_stmt_init(mysql);
13410
stmt_text= "select * from v1";
13411
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13412
check_execute(stmt, rc);
13413
mysql_stmt_close(stmt);
13414
stmt_text= "drop table if exists t1, v1";
13415
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
13420
MYSQL_STMT *open_cursor(const char *query)
13423
const ulong type= (ulong)CURSOR_TYPE_READ_ONLY;
13425
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
13426
rc= mysql_stmt_prepare(stmt, query, strlen(query));
13427
check_execute(stmt, rc);
13429
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
13434
static void test_bug8880()
13436
MYSQL_STMT *stmt_list[2], **stmt;
13437
MYSQL_STMT **stmt_list_end= (MYSQL_STMT**) stmt_list + 2;
13440
myheader("test_bug8880");
13442
mysql_query(mysql, "drop table if exists t1");
13443
mysql_query(mysql, "create table t1 (a int not null primary key, b int)");
13444
rc= mysql_query(mysql, "insert into t1 values (1,1)");
13445
myquery(rc); /* one check is enough */
13447
when inserting 2 rows everything works well
13448
mysql_query(mysql, "INSERT INTO t1 VALUES (1,1),(2,2)");
13450
for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
13451
*stmt= open_cursor("select a from t1");
13452
for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
13454
rc= mysql_stmt_execute(*stmt);
13455
check_execute(*stmt, rc);
13457
for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
13458
mysql_stmt_close(*stmt);
13462
static void test_bug9159()
13466
const char *stmt_text= "select a, b from t1";
13467
const unsigned long type= CURSOR_TYPE_READ_ONLY;
13469
myheader("test_bug9159");
13471
mysql_query(mysql, "drop table if exists t1");
13472
mysql_query(mysql, "create table t1 (a int not null primary key, b int)");
13473
rc= mysql_query(mysql, "insert into t1 values (1,1)");
13476
stmt= mysql_stmt_init(mysql);
13477
mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13478
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void *)&type);
13480
mysql_stmt_execute(stmt);
13481
mysql_stmt_close(stmt);
13482
rc= mysql_query(mysql, "drop table if exists t1");
13487
/* Crash when opening a cursor to a query with DISTICNT and no key */
13489
static void test_bug9520()
13492
MYSQL_BIND my_bind[1];
13495
int rc, row_count= 0;
13497
myheader("test_bug9520");
13499
mysql_query(mysql, "drop table if exists t1");
13500
mysql_query(mysql, "create table t1 (a char(5), b char(5), c char(5),"
13501
" primary key (a, b, c))");
13502
rc= mysql_query(mysql, "insert into t1 values ('x', 'y', 'z'), "
13503
" ('a', 'b', 'c'), ('k', 'l', 'm')");
13506
stmt= open_cursor("select distinct b from t1");
13510
stmt= open_cursor("select distinct a from t1");
13513
rc= mysql_stmt_execute(stmt);
13514
check_execute(stmt, rc);
13516
bzero((char*) my_bind, sizeof(my_bind));
13517
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
13518
my_bind[0].buffer= (char*) a;
13519
my_bind[0].buffer_length= sizeof(a);
13520
my_bind[0].length= &a_len;
13522
mysql_stmt_bind_result(stmt, my_bind);
13524
while (!(rc= mysql_stmt_fetch(stmt)))
13527
DIE_UNLESS(rc == MYSQL_NO_DATA);
13530
printf("Fetched %d rows\n", row_count);
13531
DBUG_ASSERT(row_count == 3);
13533
mysql_stmt_close(stmt);
13535
rc= mysql_query(mysql, "drop table t1");
13541
We can't have more than one cursor open for a prepared statement.
13542
Test re-executions of a PS with cursor; mysql_stmt_reset must close
13543
the cursor attached to the statement, if there is one.
13546
static void test_bug9478()
13549
MYSQL_BIND my_bind[1];
13553
DBUG_ENTER("test_bug9478");
13555
myheader("test_bug9478");
13557
mysql_query(mysql, "drop table if exists t1");
13558
mysql_query(mysql, "create table t1 (id integer not null primary key, "
13559
" name varchar(20) not null)");
13560
rc= mysql_query(mysql, "insert into t1 (id, name) values "
13561
" (1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
13564
stmt= open_cursor("select name from t1 where id=2");
13566
bzero((char*) my_bind, sizeof(my_bind));
13567
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
13568
my_bind[0].buffer= (char*) a;
13569
my_bind[0].buffer_length= sizeof(a);
13570
my_bind[0].length= &a_len;
13571
mysql_stmt_bind_result(stmt, my_bind);
13573
for (i= 0; i < 5; i++)
13575
rc= mysql_stmt_execute(stmt);
13576
check_execute(stmt, rc);
13577
rc= mysql_stmt_fetch(stmt);
13578
check_execute(stmt, rc);
13579
if (!opt_silent && i == 0)
13580
printf("Fetched row: %s\n", a);
13583
The query above is a one-row result set. Therefore, there is no
13584
cursor associated with it, as the server won't bother with opening
13585
a cursor for a one-row result set. The first row was read from the
13586
server in the fetch above. But there is eof packet pending in the
13587
network. mysql_stmt_execute will flush the packet and successfully
13588
execute the statement.
13591
rc= mysql_stmt_execute(stmt);
13592
check_execute(stmt, rc);
13594
rc= mysql_stmt_fetch(stmt);
13595
check_execute(stmt, rc);
13596
if (!opt_silent && i == 0)
13597
printf("Fetched row: %s\n", a);
13598
rc= mysql_stmt_fetch(stmt);
13599
DIE_UNLESS(rc == MYSQL_NO_DATA);
13603
/* Fill in the fetch packet */
13604
int4store(buff, stmt->stmt_id);
13605
buff[4]= 1; /* prefetch rows */
13606
rc= ((*mysql->methods->advanced_command)(mysql, COM_STMT_FETCH,
13608
sizeof(buff), 0,0,1,NULL) ||
13609
(*mysql->methods->read_query_result)(mysql));
13611
if (!opt_silent && i == 0)
13612
printf("Got error (as expected): %s\n", mysql_error(mysql));
13615
rc= mysql_stmt_execute(stmt);
13616
check_execute(stmt, rc);
13618
rc= mysql_stmt_fetch(stmt);
13619
check_execute(stmt, rc);
13620
if (!opt_silent && i == 0)
13621
printf("Fetched row: %s\n", a);
13623
rc= mysql_stmt_reset(stmt);
13624
check_execute(stmt, rc);
13625
rc= mysql_stmt_fetch(stmt);
13626
DIE_UNLESS(rc && mysql_stmt_errno(stmt));
13627
if (!opt_silent && i == 0)
13628
printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
13630
rc= mysql_stmt_close(stmt);
13631
DIE_UNLESS(rc == 0);
13633
/* Test the case with a server side cursor */
13634
stmt= open_cursor("select name from t1");
13636
mysql_stmt_bind_result(stmt, my_bind);
13638
for (i= 0; i < 5; i++)
13640
DBUG_PRINT("loop",("i: %d", i));
13641
rc= mysql_stmt_execute(stmt);
13642
check_execute(stmt, rc);
13643
rc= mysql_stmt_fetch(stmt);
13644
check_execute(stmt, rc);
13645
if (!opt_silent && i == 0)
13646
printf("Fetched row: %s\n", a);
13647
rc= mysql_stmt_execute(stmt);
13648
check_execute(stmt, rc);
13650
while (! (rc= mysql_stmt_fetch(stmt)))
13652
if (!opt_silent && i == 0)
13653
printf("Fetched row: %s\n", a);
13655
DIE_UNLESS(rc == MYSQL_NO_DATA);
13657
rc= mysql_stmt_execute(stmt);
13658
check_execute(stmt, rc);
13660
rc= mysql_stmt_fetch(stmt);
13661
check_execute(stmt, rc);
13662
if (!opt_silent && i == 0)
13663
printf("Fetched row: %s\n", a);
13665
rc= mysql_stmt_reset(stmt);
13666
check_execute(stmt, rc);
13667
rc= mysql_stmt_fetch(stmt);
13668
DIE_UNLESS(rc && mysql_stmt_errno(stmt));
13669
if (!opt_silent && i == 0)
13670
printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
13673
rc= mysql_stmt_close(stmt);
13674
DIE_UNLESS(rc == 0);
13676
rc= mysql_query(mysql, "drop table t1");
13683
Error message is returned for unsupported features.
13684
Test also cursors with non-default PREFETCH_ROWS
13687
static void test_bug9643()
13690
MYSQL_BIND my_bind[1];
13693
const char *stmt_text;
13696
ulong prefetch_rows= 5;
13698
myheader("test_bug9643");
13700
mysql_query(mysql, "drop table if exists t1");
13701
mysql_query(mysql, "create table t1 (id integer not null primary key)");
13702
rc= mysql_query(mysql, "insert into t1 (id) values "
13703
" (1), (2), (3), (4), (5), (6), (7), (8), (9)");
13706
stmt= mysql_stmt_init(mysql);
13707
/* Not implemented in 5.0 */
13708
type= (ulong) CURSOR_TYPE_SCROLLABLE;
13709
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
13712
printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
13714
type= (ulong) CURSOR_TYPE_READ_ONLY;
13715
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
13716
check_execute(stmt, rc);
13717
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS,
13718
(void*) &prefetch_rows);
13719
check_execute(stmt, rc);
13720
stmt_text= "select * from t1";
13721
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13722
check_execute(stmt, rc);
13724
bzero((char*) my_bind, sizeof(my_bind));
13725
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
13726
my_bind[0].buffer= (void*) &a;
13727
my_bind[0].buffer_length= sizeof(a);
13728
mysql_stmt_bind_result(stmt, my_bind);
13730
rc= mysql_stmt_execute(stmt);
13731
check_execute(stmt, rc);
13733
while ((rc= mysql_stmt_fetch(stmt)) == 0)
13735
DIE_UNLESS(num_rows == 9);
13737
rc= mysql_stmt_close(stmt);
13738
DIE_UNLESS(rc == 0);
13740
rc= mysql_query(mysql, "drop table t1");
13745
Bug#11111: fetch from view returns wrong data
13748
static void test_bug11111()
13751
MYSQL_BIND my_bind[2];
13756
const char *query= "SELECT DISTINCT f1,ff2 FROM v1";
13758
myheader("test_bug11111");
13760
rc= mysql_query(mysql, "drop table if exists t1, t2, v1");
13762
rc= mysql_query(mysql, "drop view if exists t1, t2, v1");
13764
rc= mysql_query(mysql, "create table t1 (f1 int, f2 int)");
13766
rc= mysql_query(mysql, "create table t2 (ff1 int, ff2 int)");
13768
rc= mysql_query(mysql, "create view v1 as select * from t1, t2 where f1=ff1");
13770
rc= mysql_query(mysql, "insert into t1 values (1,1), (2,2), (3,3)");
13772
rc= mysql_query(mysql, "insert into t2 values (1,1), (2,2), (3,3)");
13775
stmt= mysql_stmt_init(mysql);
13777
mysql_stmt_prepare(stmt, query, strlen(query));
13778
mysql_stmt_execute(stmt);
13780
bzero((char*) my_bind, sizeof(my_bind));
13781
for (i=0; i < 2; i++)
13783
my_bind[i].buffer_type= MYSQL_TYPE_STRING;
13784
my_bind[i].buffer= (uchar* *)&buf[i];
13785
my_bind[i].buffer_length= 20;
13786
my_bind[i].length= &len[i];
13789
rc= mysql_stmt_bind_result(stmt, my_bind);
13790
check_execute(stmt, rc);
13792
rc= mysql_stmt_fetch(stmt);
13793
check_execute(stmt, rc);
13795
printf("return: %s", buf[1]);
13796
DIE_UNLESS(!strcmp(buf[1],"1"));
13797
mysql_stmt_close(stmt);
13798
rc= mysql_query(mysql, "drop view v1");
13800
rc= mysql_query(mysql, "drop table t1, t2");
13805
Check that proper cleanups are done for prepared statement when
13806
fetching thorugh a cursor.
13809
static void test_bug10729()
13812
MYSQL_BIND my_bind[1];
13815
const char *stmt_text;
13817
const char *name_array[3]= { "aaa", "bbb", "ccc" };
13820
myheader("test_bug10729");
13822
mysql_query(mysql, "drop table if exists t1");
13823
mysql_query(mysql, "create table t1 (id integer not null primary key,"
13824
"name VARCHAR(20) NOT NULL)");
13825
rc= mysql_query(mysql, "insert into t1 (id, name) values "
13826
"(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
13829
stmt= mysql_stmt_init(mysql);
13831
type= (ulong) CURSOR_TYPE_READ_ONLY;
13832
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
13833
check_execute(stmt, rc);
13834
stmt_text= "select name from t1";
13835
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13836
check_execute(stmt, rc);
13838
bzero((char*) my_bind, sizeof(my_bind));
13839
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
13840
my_bind[0].buffer= (void*) a;
13841
my_bind[0].buffer_length= sizeof(a);
13842
mysql_stmt_bind_result(stmt, my_bind);
13844
for (i= 0; i < 3; i++)
13847
rc= mysql_stmt_execute(stmt);
13848
check_execute(stmt, rc);
13849
while ((rc= mysql_stmt_fetch(stmt)) == 0)
13851
DIE_UNLESS(strcmp(a, name_array[row_no]) == 0);
13853
printf("%d: %s\n", row_no, a);
13856
DIE_UNLESS(rc == MYSQL_NO_DATA);
13858
rc= mysql_stmt_close(stmt);
13859
DIE_UNLESS(rc == 0);
13861
rc= mysql_query(mysql, "drop table t1");
13867
Check that mysql_next_result works properly in case when one of
13868
the statements used in a multi-statement query is erroneous
13871
static void test_bug9992()
13877
myheader("test_bug9992");
13880
printf("Establishing a connection with option CLIENT_MULTI_STATEMENTS..\n");
13882
mysql1= mysql_client_init(NULL);
13884
if (!mysql_real_connect(mysql1, opt_host, opt_user, opt_password,
13885
opt_db ? opt_db : "test", opt_port, opt_unix_socket,
13886
CLIENT_MULTI_STATEMENTS))
13888
fprintf(stderr, "Failed to connect to the database\n");
13893
/* Sic: SHOW DATABASE is incorrect syntax. */
13894
rc= mysql_query(mysql1, "SHOW TABLES; SHOW DATABASE; SELECT 1;");
13898
fprintf(stderr, "[%d] %s\n", mysql_errno(mysql1), mysql_error(mysql1));
13903
printf("Testing mysql_store_result/mysql_next_result..\n");
13905
res= mysql_store_result(mysql1);
13907
mysql_free_result(res);
13908
rc= mysql_next_result(mysql1);
13909
DIE_UNLESS(rc == 1); /* Got errors, as expected */
13912
fprintf(stdout, "Got error, as expected:\n [%d] %s\n",
13913
mysql_errno(mysql1), mysql_error(mysql1));
13915
mysql_close(mysql1);
13918
/* Bug#10736: cursors and subqueries, memroot management */
13920
static void test_bug10736()
13923
MYSQL_BIND my_bind[1];
13926
const char *stmt_text;
13930
myheader("test_bug10736");
13932
mysql_query(mysql, "drop table if exists t1");
13933
mysql_query(mysql, "create table t1 (id integer not null primary key,"
13934
"name VARCHAR(20) NOT NULL)");
13935
rc= mysql_query(mysql, "insert into t1 (id, name) values "
13936
"(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
13939
stmt= mysql_stmt_init(mysql);
13941
type= (ulong) CURSOR_TYPE_READ_ONLY;
13942
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
13943
check_execute(stmt, rc);
13944
stmt_text= "select name from t1 where name=(select name from t1 where id=2)";
13945
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13946
check_execute(stmt, rc);
13948
bzero((char*) my_bind, sizeof(my_bind));
13949
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
13950
my_bind[0].buffer= (void*) a;
13951
my_bind[0].buffer_length= sizeof(a);
13952
mysql_stmt_bind_result(stmt, my_bind);
13954
for (i= 0; i < 3; i++)
13957
rc= mysql_stmt_execute(stmt);
13958
check_execute(stmt, rc);
13959
while ((rc= mysql_stmt_fetch(stmt)) == 0)
13962
printf("%d: %s\n", row_no, a);
13965
DIE_UNLESS(rc == MYSQL_NO_DATA);
13967
rc= mysql_stmt_close(stmt);
13968
DIE_UNLESS(rc == 0);
13970
rc= mysql_query(mysql, "drop table t1");
13974
/* Bug#10794: cursors, packets out of order */
13976
static void test_bug10794()
13978
MYSQL_STMT *stmt, *stmt1;
13979
MYSQL_BIND my_bind[2];
13984
const char *stmt_text;
13988
myheader("test_bug10794");
13990
mysql_query(mysql, "drop table if exists t1");
13991
mysql_query(mysql, "create table t1 (id integer not null primary key,"
13992
"name varchar(20) not null)");
13993
stmt= mysql_stmt_init(mysql);
13994
stmt_text= "insert into t1 (id, name) values (?, ?)";
13995
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
13996
check_execute(stmt, rc);
13997
bzero((char*) my_bind, sizeof(my_bind));
13998
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
13999
my_bind[0].buffer= (void*) &id_val;
14000
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
14001
my_bind[1].buffer= (void*) a;
14002
my_bind[1].length= &a_len;
14003
rc= mysql_stmt_bind_param(stmt, my_bind);
14004
check_execute(stmt, rc);
14005
for (i= 0; i < 42; i++)
14008
sprintf(a, "a%d", i);
14009
a_len= strlen(a); /* safety against broken sprintf */
14010
rc= mysql_stmt_execute(stmt);
14011
check_execute(stmt, rc);
14013
stmt_text= "select name from t1";
14014
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14015
type= (ulong) CURSOR_TYPE_READ_ONLY;
14016
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14017
stmt1= mysql_stmt_init(mysql);
14018
mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14019
bzero((char*) my_bind, sizeof(my_bind));
14020
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
14021
my_bind[0].buffer= (void*) a;
14022
my_bind[0].buffer_length= sizeof(a);
14023
my_bind[0].length= &a_len;
14024
rc= mysql_stmt_bind_result(stmt, my_bind);
14025
check_execute(stmt, rc);
14026
rc= mysql_stmt_execute(stmt);
14027
check_execute(stmt, rc);
14028
rc= mysql_stmt_fetch(stmt);
14029
check_execute(stmt, rc);
14031
printf("Fetched row from stmt: %s\n", a);
14032
/* Don't optimize: an attribute of the original test case */
14033
mysql_stmt_free_result(stmt);
14034
mysql_stmt_reset(stmt);
14035
stmt_text= "select name from t1 where id=10";
14036
rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
14037
check_execute(stmt1, rc);
14038
rc= mysql_stmt_bind_result(stmt1, my_bind);
14039
check_execute(stmt1, rc);
14040
rc= mysql_stmt_execute(stmt1);
14043
rc= mysql_stmt_fetch(stmt1);
14044
if (rc == MYSQL_NO_DATA)
14047
printf("End of data in stmt1\n");
14050
check_execute(stmt1, rc);
14052
printf("Fetched row from stmt1: %s\n", a);
14054
mysql_stmt_close(stmt);
14055
mysql_stmt_close(stmt1);
14057
rc= mysql_query(mysql, "drop table t1");
14062
/* Bug#11172: cursors, crash on a fetch from a datetime column */
14064
static void test_bug11172()
14067
MYSQL_BIND bind_in[1], bind_out[2];
14070
const char *stmt_text;
14074
myheader("test_bug11172");
14076
mysql_query(mysql, "drop table if exists t1");
14077
mysql_query(mysql, "create table t1 (id integer not null primary key,"
14078
"hired date not null)");
14079
rc= mysql_query(mysql,
14080
"insert into t1 (id, hired) values (1, '1933-08-24'), "
14081
"(2, '1965-01-01'), (3, '1949-08-17'), (4, '1945-07-07'), "
14082
"(5, '1941-05-15'), (6, '1978-09-15'), (7, '1936-03-28')");
14084
stmt= mysql_stmt_init(mysql);
14085
stmt_text= "SELECT id, hired FROM t1 WHERE hired=?";
14086
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14087
check_execute(stmt, rc);
14089
type= (ulong) CURSOR_TYPE_READ_ONLY;
14090
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14092
bzero((char*) bind_in, sizeof(bind_in));
14093
bzero((char*) bind_out, sizeof(bind_out));
14094
bzero((char*) &hired, sizeof(hired));
14098
bind_in[0].buffer_type= MYSQL_TYPE_DATE;
14099
bind_in[0].buffer= (void*) &hired;
14100
bind_in[0].buffer_length= sizeof(hired);
14101
bind_out[0].buffer_type= MYSQL_TYPE_LONG;
14102
bind_out[0].buffer= (void*) &id;
14103
bind_out[1]= bind_in[0];
14105
for (i= 0; i < 3; i++)
14107
rc= mysql_stmt_bind_param(stmt, bind_in);
14108
check_execute(stmt, rc);
14109
rc= mysql_stmt_bind_result(stmt, bind_out);
14110
check_execute(stmt, rc);
14111
rc= mysql_stmt_execute(stmt);
14112
check_execute(stmt, rc);
14113
while ((rc= mysql_stmt_fetch(stmt)) == 0)
14116
printf("fetched data %d:%d-%d-%d\n", id,
14117
hired.year, hired.month, hired.day);
14119
DIE_UNLESS(rc == MYSQL_NO_DATA);
14120
if (!mysql_stmt_free_result(stmt))
14121
mysql_stmt_reset(stmt);
14123
mysql_stmt_close(stmt);
14124
mysql_rollback(mysql);
14125
mysql_rollback(mysql);
14127
rc= mysql_query(mysql, "drop table t1");
14132
/* Bug#11656: cursors, crash on a fetch from a query with distinct. */
14134
static void test_bug11656()
14137
MYSQL_BIND my_bind[2];
14139
const char *stmt_text;
14144
myheader("test_bug11656");
14146
mysql_query(mysql, "drop table if exists t1");
14148
rc= mysql_query(mysql, "create table t1 ("
14149
"server varchar(40) not null, "
14150
"test_kind varchar(1) not null, "
14151
"test_id varchar(30) not null , "
14152
"primary key (server,test_kind,test_id))");
14155
stmt_text= "select distinct test_kind, test_id from t1 "
14156
"where server in (?, ?)";
14157
stmt= mysql_stmt_init(mysql);
14158
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14159
check_execute(stmt, rc);
14160
type= (ulong) CURSOR_TYPE_READ_ONLY;
14161
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14163
bzero((char*) my_bind, sizeof(my_bind));
14164
strmov(buf[0], "pcint502_MY2");
14165
strmov(buf[1], "*");
14166
for (i=0; i < 2; i++)
14168
my_bind[i].buffer_type= MYSQL_TYPE_STRING;
14169
my_bind[i].buffer= (uchar* *)&buf[i];
14170
my_bind[i].buffer_length= strlen(buf[i]);
14172
mysql_stmt_bind_param(stmt, my_bind);
14174
rc= mysql_stmt_execute(stmt);
14175
check_execute(stmt, rc);
14177
rc= mysql_stmt_fetch(stmt);
14178
DIE_UNLESS(rc == MYSQL_NO_DATA);
14180
mysql_stmt_close(stmt);
14181
rc= mysql_query(mysql, "drop table t1");
14187
Check that the server signals when NO_BACKSLASH_ESCAPES mode is in effect,
14188
and mysql_real_escape_string() does the right thing as a result.
14191
static void test_bug10214()
14196
myheader("test_bug10214");
14198
DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES));
14200
len= mysql_real_escape_string(mysql, out, "a'b\\c", 5);
14201
DIE_UNLESS(memcmp(out, "a\\'b\\\\c", len) == 0);
14203
mysql_query(mysql, "set sql_mode='NO_BACKSLASH_ESCAPES'");
14204
DIE_UNLESS(mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES);
14206
len= mysql_real_escape_string(mysql, out, "a'b\\c", 5);
14207
DIE_UNLESS(memcmp(out, "a''b\\c", len) == 0);
14209
mysql_query(mysql, "set sql_mode=''");
14212
static void test_client_character_set()
14214
MY_CHARSET_INFO cs;
14215
char *csname= (char*) "utf8";
14216
char *csdefault= (char*)mysql_character_set_name(mysql);
14219
myheader("test_client_character_set");
14221
rc= mysql_set_character_set(mysql, csname);
14222
DIE_UNLESS(rc == 0);
14224
mysql_get_character_set_info(mysql, &cs);
14225
DIE_UNLESS(!strcmp(cs.csname, "utf8"));
14226
DIE_UNLESS(!strcmp(cs.name, "utf8_general_ci"));
14227
/* Restore the default character set */
14228
rc= mysql_set_character_set(mysql, csdefault);
14232
/* Test correct max length for MEDIUMTEXT and LONGTEXT columns */
14234
static void test_bug9735()
14239
myheader("test_bug9735");
14241
rc= mysql_query(mysql, "drop table if exists t1");
14243
rc= mysql_query(mysql, "create table t1 (a mediumtext, b longtext) "
14244
"character set latin1");
14246
rc= mysql_query(mysql, "select * from t1");
14248
res= mysql_store_result(mysql);
14249
verify_prepare_field(res, 0, "a", "a", MYSQL_TYPE_BLOB,
14250
"t1", "t1", current_db, (1U << 24)-1, 0);
14251
verify_prepare_field(res, 1, "b", "b", MYSQL_TYPE_BLOB,
14252
"t1", "t1", current_db, ~0U, 0);
14253
mysql_free_result(res);
14254
rc= mysql_query(mysql, "drop table t1");
14259
/* Bug#11183 "mysql_stmt_reset() doesn't reset information about error" */
14261
static void test_bug11183()
14265
char bug_statement[]= "insert into t1 values (1)";
14267
myheader("test_bug11183");
14269
mysql_query(mysql, "drop table t1 if exists");
14270
mysql_query(mysql, "create table t1 (a int)");
14272
stmt= mysql_stmt_init(mysql);
14273
DIE_UNLESS(stmt != 0);
14275
rc= mysql_stmt_prepare(stmt, bug_statement, strlen(bug_statement));
14276
check_execute(stmt, rc);
14278
rc= mysql_query(mysql, "drop table t1");
14281
/* Trying to execute statement that should fail on execute stage */
14282
rc= mysql_stmt_execute(stmt);
14285
mysql_stmt_reset(stmt);
14286
DIE_UNLESS(mysql_stmt_errno(stmt) == 0);
14288
mysql_query(mysql, "create table t1 (a int)");
14290
/* Trying to execute statement that should pass ok */
14291
if (mysql_stmt_execute(stmt))
14293
mysql_stmt_reset(stmt);
14294
DIE_UNLESS(mysql_stmt_errno(stmt) == 0);
14297
mysql_stmt_close(stmt);
14299
rc= mysql_query(mysql, "drop table t1");
14303
static void test_bug11037()
14307
const char *stmt_text;
14309
myheader("test_bug11037");
14311
mysql_query(mysql, "drop table if exists t1");
14313
rc= mysql_query(mysql, "create table t1 (id int not null)");
14316
rc= mysql_query(mysql, "insert into t1 values (1)");
14319
stmt_text= "select id FROM t1";
14320
stmt= mysql_stmt_init(mysql);
14321
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14323
/* expected error */
14324
rc = mysql_stmt_fetch(stmt);
14327
fprintf(stdout, "Got error, as expected:\n [%d] %s\n",
14328
mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
14330
rc= mysql_stmt_execute(stmt);
14331
check_execute(stmt, rc);
14333
rc= mysql_stmt_fetch(stmt);
14336
rc= mysql_stmt_fetch(stmt);
14337
DIE_UNLESS(rc==MYSQL_NO_DATA);
14339
rc= mysql_stmt_fetch(stmt);
14340
DIE_UNLESS(rc==MYSQL_NO_DATA);
14342
mysql_stmt_close(stmt);
14343
rc= mysql_query(mysql, "drop table t1");
14347
/* Bug#10760: cursors, crash in a fetch after rollback. */
14349
static void test_bug10760()
14352
MYSQL_BIND my_bind[1];
14354
const char *stmt_text;
14360
myheader("test_bug10760");
14362
mysql_query(mysql, "drop table if exists t1, t2");
14364
/* create tables */
14365
rc= mysql_query(mysql, "create table t1 (id integer not null primary key)"
14368
for (; i < 42; ++i)
14371
sprintf(buf, "insert into t1 (id) values (%d)", i+1);
14372
rc= mysql_query(mysql, buf);
14375
mysql_autocommit(mysql, FALSE);
14376
/* create statement */
14377
stmt= mysql_stmt_init(mysql);
14378
type= (ulong) CURSOR_TYPE_READ_ONLY;
14379
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14382
1: check that a deadlock within the same connection
14383
is resolved and an error is returned. The deadlock is modelled
14385
con1: open cursor for select * from t1;
14386
con1: insert into t1 (id) values (1)
14388
stmt_text= "select id from t1 order by 1";
14389
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14390
check_execute(stmt, rc);
14391
rc= mysql_stmt_execute(stmt);
14392
check_execute(stmt, rc);
14393
rc= mysql_query(mysql, "update t1 set id=id+100");
14395
If cursors are not materialized, the update will return an error;
14396
we mainly test that it won't deadlock.
14398
if (rc && !opt_silent)
14399
printf("Got error (as expected): %s\n", mysql_error(mysql));
14401
2: check that MyISAM tables used in cursors survive
14404
rc= mysql_rollback(mysql); /* should not close the cursor */
14406
rc= mysql_stmt_fetch(stmt);
14407
check_execute(stmt, rc);
14410
3: check that cursors to InnoDB tables are closed (for now) by
14416
printf("Testing that cursors are closed at COMMIT/ROLLBACK requires "
14421
stmt_text= "select id from t1 order by 1";
14422
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14423
check_execute(stmt, rc);
14425
rc= mysql_query(mysql, "alter table t1 engine=InnoDB");
14428
bzero(my_bind, sizeof(my_bind));
14429
my_bind[0].buffer_type= MYSQL_TYPE_STRING;
14430
my_bind[0].buffer= (void*) id_buf;
14431
my_bind[0].buffer_length= sizeof(id_buf);
14432
my_bind[0].length= &id_len;
14433
check_execute(stmt, rc);
14434
mysql_stmt_bind_result(stmt, my_bind);
14436
rc= mysql_stmt_execute(stmt);
14437
rc= mysql_stmt_fetch(stmt);
14438
DIE_UNLESS(rc == 0);
14440
printf("Fetched row %s\n", id_buf);
14441
rc= mysql_rollback(mysql); /* should close the cursor */
14444
rc= mysql_stmt_fetch(stmt);
14447
printf("Got error (as expected): %s\n", mysql_error(mysql));
14451
mysql_stmt_close(stmt);
14452
rc= mysql_query(mysql, "drop table t1");
14454
mysql_autocommit(mysql, TRUE); /* restore default */
14457
static void test_bug12001()
14459
MYSQL *mysql_local;
14461
const char *query= "DROP TABLE IF EXISTS test_table;"
14462
"CREATE TABLE test_table(id INT);"
14463
"INSERT INTO test_table VALUES(10);"
14464
"UPDATE test_table SET id=20 WHERE id=10;"
14465
"SELECT * FROM test_table;"
14466
"INSERT INTO non_existent_table VALUES(11);";
14469
myheader("test_bug12001");
14471
if (!(mysql_local= mysql_client_init(NULL)))
14473
fprintf(stdout, "\n mysql_client_init() failed");
14477
/* Create connection that supports multi statements */
14478
if (!mysql_real_connect(mysql_local, opt_host, opt_user,
14479
opt_password, current_db, opt_port,
14480
opt_unix_socket, CLIENT_MULTI_STATEMENTS |
14481
CLIENT_MULTI_RESULTS))
14483
fprintf(stdout, "\n mysql_real_connect() failed");
14487
rc= mysql_query(mysql_local, query);
14492
if (mysql_field_count(mysql_local) &&
14493
(result= mysql_use_result(mysql_local)))
14495
mysql_free_result(result);
14498
while (!(res= mysql_next_result(mysql_local)));
14500
rc= mysql_query(mysql_local, "DROP TABLE IF EXISTS test_table");
14503
mysql_close(mysql_local);
14504
DIE_UNLESS(res==1);
14508
/* Bug#11909: wrong metadata if fetching from two cursors */
14510
static void test_bug11909()
14512
MYSQL_STMT *stmt1, *stmt2;
14513
MYSQL_BIND my_bind[7];
14515
char firstname[20], midinit[20], lastname[20], workdept[20];
14516
ulong firstname_len, midinit_len, lastname_len, workdept_len;
14520
const char *stmt_text;
14522
myheader("test_bug11909");
14524
stmt_text= "drop table if exists t1";
14525
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14528
stmt_text= "create table t1 ("
14529
" empno int(11) not null, firstname varchar(20) not null,"
14530
" midinit varchar(20) not null, lastname varchar(20) not null,"
14531
" workdept varchar(6) not null, salary double not null,"
14532
" bonus float not null, primary key (empno)"
14533
") default charset=latin1 collate=latin1_bin";
14534
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14537
stmt_text= "insert into t1 values "
14538
"(10, 'CHRISTINE', 'I', 'HAAS', 'A00', 52750, 1000), "
14539
"(20, 'MICHAEL', 'L', 'THOMPSON', 'B01', 41250, 800),"
14540
"(30, 'SALLY', 'A', 'KWAN', 'C01', 38250, 800),"
14541
"(50, 'JOHN', 'B', 'GEYER', 'E01', 40175, 800), "
14542
"(60, 'IRVING', 'F', 'STERN', 'D11', 32250, 500)";
14543
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14546
/* ****** Begin of trace ****** */
14548
stmt1= open_cursor("SELECT empno, firstname, midinit, lastname,"
14549
"workdept, salary, bonus FROM t1");
14551
bzero(my_bind, sizeof(my_bind));
14552
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
14553
my_bind[0].buffer= (void*) &empno;
14555
my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
14556
my_bind[1].buffer= (void*) firstname;
14557
my_bind[1].buffer_length= sizeof(firstname);
14558
my_bind[1].length= &firstname_len;
14560
my_bind[2].buffer_type= MYSQL_TYPE_VAR_STRING;
14561
my_bind[2].buffer= (void*) midinit;
14562
my_bind[2].buffer_length= sizeof(midinit);
14563
my_bind[2].length= &midinit_len;
14565
my_bind[3].buffer_type= MYSQL_TYPE_VAR_STRING;
14566
my_bind[3].buffer= (void*) lastname;
14567
my_bind[3].buffer_length= sizeof(lastname);
14568
my_bind[3].length= &lastname_len;
14570
my_bind[4].buffer_type= MYSQL_TYPE_VAR_STRING;
14571
my_bind[4].buffer= (void*) workdept;
14572
my_bind[4].buffer_length= sizeof(workdept);
14573
my_bind[4].length= &workdept_len;
14575
my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
14576
my_bind[5].buffer= (void*) &salary;
14578
my_bind[6].buffer_type= MYSQL_TYPE_FLOAT;
14579
my_bind[6].buffer= (void*) &bonus;
14580
rc= mysql_stmt_bind_result(stmt1, my_bind);
14581
check_execute(stmt1, rc);
14583
rc= mysql_stmt_execute(stmt1);
14584
check_execute(stmt1, rc);
14586
rc= mysql_stmt_fetch(stmt1);
14587
DIE_UNLESS(rc == 0);
14588
DIE_UNLESS(empno == 10);
14589
DIE_UNLESS(strcmp(firstname, "CHRISTINE") == 0);
14590
DIE_UNLESS(strcmp(midinit, "I") == 0);
14591
DIE_UNLESS(strcmp(lastname, "HAAS") == 0);
14592
DIE_UNLESS(strcmp(workdept, "A00") == 0);
14593
DIE_UNLESS(salary == (double) 52750.0);
14594
DIE_UNLESS(bonus == (float) 1000.0);
14596
stmt2= open_cursor("SELECT empno, firstname FROM t1");
14597
rc= mysql_stmt_bind_result(stmt2, my_bind);
14598
check_execute(stmt2, rc);
14600
rc= mysql_stmt_execute(stmt2);
14601
check_execute(stmt2, rc);
14603
rc= mysql_stmt_fetch(stmt2);
14604
DIE_UNLESS(rc == 0);
14606
DIE_UNLESS(empno == 10);
14607
DIE_UNLESS(strcmp(firstname, "CHRISTINE") == 0);
14609
rc= mysql_stmt_reset(stmt2);
14610
check_execute(stmt2, rc);
14612
/* ERROR: next statement should return 0 */
14614
rc= mysql_stmt_fetch(stmt1);
14615
DIE_UNLESS(rc == 0);
14617
mysql_stmt_close(stmt1);
14618
mysql_stmt_close(stmt2);
14619
rc= mysql_rollback(mysql);
14622
rc= mysql_query(mysql, "drop table t1");
14626
/* Cursors: opening a cursor to a compilicated query with ORDER BY */
14628
static void test_bug11901()
14631
MYSQL_BIND my_bind[2];
14634
ulong workdept_len;
14636
const char *stmt_text;
14638
myheader("test_bug11901");
14640
stmt_text= "drop table if exists t1, t2";
14641
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14644
stmt_text= "create table t1 ("
14645
" empno int(11) not null, firstname varchar(20) not null,"
14646
" midinit varchar(20) not null, lastname varchar(20) not null,"
14647
" workdept varchar(6) not null, salary double not null,"
14648
" bonus float not null, primary key (empno), "
14649
" unique key (workdept, empno) "
14650
") default charset=latin1 collate=latin1_bin";
14651
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14654
stmt_text= "insert into t1 values "
14655
"(10, 'CHRISTINE', 'I', 'HAAS', 'A00', 52750, 1000),"
14656
"(20, 'MICHAEL', 'L', 'THOMPSON', 'B01', 41250, 800), "
14657
"(30, 'SALLY', 'A', 'KWAN', 'C01', 38250, 800), "
14658
"(50, 'JOHN', 'B', 'GEYER', 'E01', 40175, 800), "
14659
"(60, 'IRVING', 'F', 'STERN', 'D11', 32250, 500), "
14660
"(70, 'EVA', 'D', 'PULASKI', 'D21', 36170, 700), "
14661
"(90, 'EILEEN', 'W', 'HENDERSON', 'E11', 29750, 600), "
14662
"(100, 'THEODORE', 'Q', 'SPENSER', 'E21', 26150, 500), "
14663
"(110, 'VINCENZO', 'G', 'LUCCHESSI', 'A00', 46500, 900), "
14664
"(120, 'SEAN', '', 'O\\'CONNELL', 'A00', 29250, 600), "
14665
"(130, 'DOLORES', 'M', 'QUINTANA', 'C01', 23800, 500), "
14666
"(140, 'HEATHER', 'A', 'NICHOLLS', 'C01', 28420, 600), "
14667
"(150, 'BRUCE', '', 'ADAMSON', 'D11', 25280, 500), "
14668
"(160, 'ELIZABETH', 'R', 'PIANKA', 'D11', 22250, 400), "
14669
"(170, 'MASATOSHI', 'J', 'YOSHIMURA', 'D11', 24680, 500), "
14670
"(180, 'MARILYN', 'S', 'SCOUTTEN', 'D11', 21340, 500), "
14671
"(190, 'JAMES', 'H', 'WALKER', 'D11', 20450, 400), "
14672
"(200, 'DAVID', '', 'BROWN', 'D11', 27740, 600), "
14673
"(210, 'WILLIAM', 'T', 'JONES', 'D11', 18270, 400), "
14674
"(220, 'JENNIFER', 'K', 'LUTZ', 'D11', 29840, 600), "
14675
"(230, 'JAMES', 'J', 'JEFFERSON', 'D21', 22180, 400), "
14676
"(240, 'SALVATORE', 'M', 'MARINO', 'D21', 28760, 600), "
14677
"(250, 'DANIEL', 'S', 'SMITH', 'D21', 19180, 400), "
14678
"(260, 'SYBIL', 'P', 'JOHNSON', 'D21', 17250, 300), "
14679
"(270, 'MARIA', 'L', 'PEREZ', 'D21', 27380, 500), "
14680
"(280, 'ETHEL', 'R', 'SCHNEIDER', 'E11', 26250, 500), "
14681
"(290, 'JOHN', 'R', 'PARKER', 'E11', 15340, 300), "
14682
"(300, 'PHILIP', 'X', 'SMITH', 'E11', 17750, 400), "
14683
"(310, 'MAUDE', 'F', 'SETRIGHT', 'E11', 15900, 300), "
14684
"(320, 'RAMLAL', 'V', 'MEHTA', 'E21', 19950, 400), "
14685
"(330, 'WING', '', 'LEE', 'E21', 25370, 500), "
14686
"(340, 'JASON', 'R', 'GOUNOT', 'E21', 23840, 500)";
14688
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14691
stmt_text= "create table t2 ("
14692
" deptno varchar(6) not null, deptname varchar(20) not null,"
14693
" mgrno int(11) not null, location varchar(20) not null,"
14694
" admrdept varchar(6) not null, refcntd int(11) not null,"
14695
" refcntu int(11) not null, primary key (deptno)"
14696
") default charset=latin1 collate=latin1_bin";
14697
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14700
stmt_text= "insert into t2 values "
14701
"('A00', 'SPIFFY COMPUTER SERV', 10, '', 'A00', 0, 0), "
14702
"('B01', 'PLANNING', 20, '', 'A00', 0, 0), "
14703
"('C01', 'INFORMATION CENTER', 30, '', 'A00', 0, 0), "
14704
"('D01', 'DEVELOPMENT CENTER', 0, '', 'A00', 0, 0),"
14705
"('D11', 'MANUFACTURING SYSTEM', 60, '', 'D01', 0, 0), "
14706
"('D21', 'ADMINISTRATION SYSTE', 70, '', 'D01', 0, 0), "
14707
"('E01', 'SUPPORT SERVICES', 50, '', 'A00', 0, 0), "
14708
"('E11', 'OPERATIONS', 90, '', 'E01', 0, 0), "
14709
"('E21', 'SOFTWARE SUPPORT', 100,'', 'E01', 0, 0)";
14710
rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
14713
/* ****** Begin of trace ****** */
14715
stmt= open_cursor("select t1.empno, t1.workdept "
14716
"from (t1 left join t2 on t2.deptno = t1.workdept) "
14717
"where t2.deptno in "
14718
" (select t2.deptno "
14719
" from (t1 left join t2 on t2.deptno = t1.workdept) "
14720
" where t1.empno = ?) "
14722
bzero(my_bind, sizeof(my_bind));
14724
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
14725
my_bind[0].buffer= &empno;
14726
rc= mysql_stmt_bind_param(stmt, my_bind);
14727
check_execute(stmt, rc);
14729
my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
14730
my_bind[1].buffer= (void*) workdept;
14731
my_bind[1].buffer_length= sizeof(workdept);
14732
my_bind[1].length= &workdept_len;
14734
rc= mysql_stmt_bind_result(stmt, my_bind);
14735
check_execute(stmt, rc);
14738
/* ERROR: next statement causes a server crash */
14739
rc= mysql_stmt_execute(stmt);
14740
check_execute(stmt, rc);
14742
mysql_stmt_close(stmt);
14744
rc= mysql_query(mysql, "drop table t1, t2");
14748
/* Bug#11904: mysql_stmt_attr_set CURSOR_TYPE_READ_ONLY grouping wrong result */
14750
static void test_bug11904()
14754
const char *stmt_text;
14755
const ulong type= (ulong)CURSOR_TYPE_READ_ONLY;
14756
MYSQL_BIND my_bind[2];
14758
char row_data[11]= {0};
14760
myheader("test_bug11904");
14762
/* create tables */
14763
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bug11904b");
14765
rc= mysql_query(mysql, "CREATE TABLE bug11904b (id int, name char(10), primary key(id, name))");
14768
rc= mysql_query(mysql, "INSERT INTO bug11904b VALUES (1, 'sofia'), (1,'plovdiv'),"
14769
" (1,'varna'), (2,'LA'), (2,'new york'), (3,'heidelberg'),"
14770
" (3,'berlin'), (3, 'frankfurt')");
14773
mysql_commit(mysql);
14774
/* create statement */
14775
stmt1= mysql_stmt_init(mysql);
14776
mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14778
stmt_text= "SELECT id, MIN(name) FROM bug11904b GROUP BY id";
14780
rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
14781
check_execute(stmt1, rc);
14783
memset(my_bind, 0, sizeof(my_bind));
14784
my_bind[0].buffer_type= MYSQL_TYPE_LONG;
14785
my_bind[0].buffer=& country_id;
14786
my_bind[0].buffer_length= 0;
14787
my_bind[0].length= 0;
14789
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
14790
my_bind[1].buffer=& row_data;
14791
my_bind[1].buffer_length= sizeof(row_data) - 1;
14792
my_bind[1].length= 0;
14794
rc= mysql_stmt_bind_result(stmt1, my_bind);
14795
check_execute(stmt1, rc);
14797
rc= mysql_stmt_execute(stmt1);
14798
check_execute(stmt1, rc);
14800
rc= mysql_stmt_fetch(stmt1);
14801
check_execute(stmt1, rc);
14802
DIE_UNLESS(country_id == 1);
14803
DIE_UNLESS(memcmp(row_data, "plovdiv", 7) == 0);
14805
rc= mysql_stmt_fetch(stmt1);
14806
check_execute(stmt1, rc);
14807
DIE_UNLESS(country_id == 2);
14808
DIE_UNLESS(memcmp(row_data, "LA", 2) == 0);
14810
rc= mysql_stmt_fetch(stmt1);
14811
check_execute(stmt1, rc);
14812
DIE_UNLESS(country_id == 3);
14813
DIE_UNLESS(memcmp(row_data, "berlin", 6) == 0);
14815
rc= mysql_stmt_close(stmt1);
14816
check_execute(stmt1, rc);
14818
rc= mysql_query(mysql, "drop table bug11904b");
14823
/* Bug#12243: multiple cursors, crash in a fetch after commit. */
14825
static void test_bug12243()
14827
MYSQL_STMT *stmt1, *stmt2;
14829
const char *stmt_text;
14832
myheader("test_bug12243");
14837
printf("This test requires InnoDB.\n");
14841
/* create tables */
14842
mysql_query(mysql, "drop table if exists t1");
14843
mysql_query(mysql, "create table t1 (a int) engine=InnoDB");
14844
rc= mysql_query(mysql, "insert into t1 (a) values (1), (2)");
14846
mysql_autocommit(mysql, FALSE);
14847
/* create statement */
14848
stmt1= mysql_stmt_init(mysql);
14849
stmt2= mysql_stmt_init(mysql);
14850
type= (ulong) CURSOR_TYPE_READ_ONLY;
14851
mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14852
mysql_stmt_attr_set(stmt2, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14854
stmt_text= "select a from t1";
14856
rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
14857
check_execute(stmt1, rc);
14858
rc= mysql_stmt_execute(stmt1);
14859
check_execute(stmt1, rc);
14860
rc= mysql_stmt_fetch(stmt1);
14861
check_execute(stmt1, rc);
14863
rc= mysql_stmt_prepare(stmt2, stmt_text, strlen(stmt_text));
14864
check_execute(stmt2, rc);
14865
rc= mysql_stmt_execute(stmt2);
14866
check_execute(stmt2, rc);
14867
rc= mysql_stmt_fetch(stmt2);
14868
check_execute(stmt2, rc);
14870
rc= mysql_stmt_close(stmt1);
14871
check_execute(stmt1, rc);
14872
rc= mysql_commit(mysql);
14874
rc= mysql_stmt_fetch(stmt2);
14875
check_execute(stmt2, rc);
14877
mysql_stmt_close(stmt2);
14878
rc= mysql_query(mysql, "drop table t1");
14880
mysql_autocommit(mysql, TRUE); /* restore default */
14885
Bug#11718: query with function, join and order by returns wrong type
14888
static void test_bug11718()
14892
const char *query= "select str_to_date(concat(f3),'%Y%m%d') from t1,t2 "
14893
"where f1=f2 order by f1";
14895
myheader("test_bug11718");
14897
rc= mysql_query(mysql, "drop table if exists t1, t2");
14899
rc= mysql_query(mysql, "create table t1 (f1 int)");
14901
rc= mysql_query(mysql, "create table t2 (f2 int, f3 numeric(8))");
14903
rc= mysql_query(mysql, "insert into t1 values (1), (2)");
14905
rc= mysql_query(mysql, "insert into t2 values (1,20050101), (2,20050202)");
14907
rc= mysql_query(mysql, query);
14909
res = mysql_store_result(mysql);
14912
printf("return type: %s", (res->fields[0].type == MYSQL_TYPE_DATE)?"DATE":
14914
DIE_UNLESS(res->fields[0].type == MYSQL_TYPE_DATE);
14915
mysql_free_result(res);
14916
rc= mysql_query(mysql, "drop table t1, t2");
14922
Bug #12925: Bad handling of maximum values in getopt
14924
static void test_bug12925()
14926
myheader("test_bug12925");
14927
if (opt_getopt_ll_test)
14928
DIE_UNLESS(opt_getopt_ll_test == LL(25600*1024*1024));
14933
Bug#14210 "Simple query with > operator on large table gives server
14937
static void test_bug14210()
14941
const char *stmt_text;
14944
myheader("test_bug14210");
14946
mysql_query(mysql, "drop table if exists t1");
14948
To trigger the problem the table must be InnoDB, although the problem
14949
itself is not InnoDB related. In case the table is MyISAM this test
14952
mysql_query(mysql, "create table t1 (a varchar(255)) engine=InnoDB");
14953
rc= mysql_query(mysql, "insert into t1 (a) values (repeat('a', 256))");
14955
rc= mysql_query(mysql, "set @@session.max_heap_table_size=16384");
14956
/* Create a big enough table (more than max_heap_table_size) */
14957
for (i= 0; i < 8; i++)
14959
rc= mysql_query(mysql, "insert into t1 (a) select a from t1");
14962
/* create statement */
14963
stmt= mysql_stmt_init(mysql);
14964
type= (ulong) CURSOR_TYPE_READ_ONLY;
14965
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
14967
stmt_text= "select a from t1";
14969
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
14970
check_execute(stmt, rc);
14971
rc= mysql_stmt_execute(stmt);
14972
while ((rc= mysql_stmt_fetch(stmt)) == 0)
14974
DIE_UNLESS(rc == MYSQL_NO_DATA);
14976
rc= mysql_stmt_close(stmt);
14978
rc= mysql_query(mysql, "drop table t1");
14980
rc= mysql_query(mysql, "set @@session.max_heap_table_size=default");
14984
/* Bug#13488: wrong column metadata when fetching from cursor */
14986
static void test_bug13488()
14988
MYSQL_BIND my_bind[3];
14990
int rc, f1, f2, f3, i;
14991
const ulong type= CURSOR_TYPE_READ_ONLY;
14992
const char *query= "select * from t1 left join t2 on f1=f2 where f1=1";
14994
myheader("test_bug13488");
14996
rc= mysql_query(mysql, "drop table if exists t1, t2");
14998
rc= mysql_query(mysql, "create table t1 (f1 int not null primary key)");
15000
rc= mysql_query(mysql, "create table t2 (f2 int not null primary key, "
15001
"f3 int not null)");
15003
rc= mysql_query(mysql, "insert into t1 values (1), (2)");
15005
rc= mysql_query(mysql, "insert into t2 values (1,2), (2,4)");
15008
memset(my_bind, 0, sizeof(my_bind));
15009
for (i= 0; i < 3; i++)
15011
my_bind[i].buffer_type= MYSQL_TYPE_LONG;
15012
my_bind[i].buffer_length= 4;
15013
my_bind[i].length= 0;
15015
my_bind[0].buffer=&f1;
15016
my_bind[1].buffer=&f2;
15017
my_bind[2].buffer=&f3;
15019
stmt1= mysql_stmt_init(mysql);
15020
rc= mysql_stmt_attr_set(stmt1,STMT_ATTR_CURSOR_TYPE, (const void *)&type);
15021
check_execute(stmt1, rc);
15023
rc= mysql_stmt_prepare(stmt1, query, strlen(query));
15024
check_execute(stmt1, rc);
15026
rc= mysql_stmt_execute(stmt1);
15027
check_execute(stmt1, rc);
15029
rc= mysql_stmt_bind_result(stmt1, my_bind);
15030
check_execute(stmt1, rc);
15032
rc= mysql_stmt_fetch(stmt1);
15033
check_execute(stmt1, rc);
15035
rc= mysql_stmt_free_result(stmt1);
15036
check_execute(stmt1, rc);
15038
rc= mysql_stmt_reset(stmt1);
15039
check_execute(stmt1, rc);
15041
rc= mysql_stmt_close(stmt1);
15042
check_execute(stmt1, rc);
15045
printf("data is: %s", (f1 == 1 && f2 == 1 && f3 == 2)?"OK":
15047
DIE_UNLESS(f1 == 1 && f2 == 1 && f3 == 2);
15048
rc= mysql_query(mysql, "drop table t1, t2");
15053
Bug#13524: warnings of a previous command are not reset when fetching
15057
static void test_bug13524()
15061
unsigned int warning_count;
15062
const ulong type= CURSOR_TYPE_READ_ONLY;
15063
const char *query= "select * from t1";
15065
myheader("test_bug13524");
15067
rc= mysql_query(mysql, "drop table if exists t1, t2");
15069
rc= mysql_query(mysql, "create table t1 (a int not null primary key)");
15071
rc= mysql_query(mysql, "insert into t1 values (1), (2), (3), (4)");
15074
stmt= mysql_stmt_init(mysql);
15075
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
15076
check_execute(stmt, rc);
15078
rc= mysql_stmt_prepare(stmt, query, strlen(query));
15079
check_execute(stmt, rc);
15081
rc= mysql_stmt_execute(stmt);
15082
check_execute(stmt, rc);
15084
rc= mysql_stmt_fetch(stmt);
15085
check_execute(stmt, rc);
15087
warning_count= mysql_warning_count(mysql);
15088
DIE_UNLESS(warning_count == 0);
15090
/* Check that DROP TABLE produced a warning (no such table) */
15091
rc= mysql_query(mysql, "drop table if exists t2");
15093
warning_count= mysql_warning_count(mysql);
15094
DIE_UNLESS(warning_count == 1);
15097
Check that fetch from a cursor cleared the warning from the previous
15100
rc= mysql_stmt_fetch(stmt);
15101
check_execute(stmt, rc);
15102
warning_count= mysql_warning_count(mysql);
15103
DIE_UNLESS(warning_count == 0);
15106
mysql_stmt_close(stmt);
15107
rc= mysql_query(mysql, "drop table t1");
15112
Bug#14845 "mysql_stmt_fetch returns MYSQL_NO_DATA when COUNT(*) is 0"
15115
static void test_bug14845()
15119
const ulong type= CURSOR_TYPE_READ_ONLY;
15120
const char *query= "select count(*) from t1 where 1 = 0";
15122
myheader("test_bug14845");
15124
rc= mysql_query(mysql, "drop table if exists t1");
15126
rc= mysql_query(mysql, "create table t1 (id int(11) default null, "
15127
"name varchar(20) default null)"
15128
"engine=MyISAM DEFAULT CHARSET=utf8");
15130
rc= mysql_query(mysql, "insert into t1 values (1,'abc'),(2,'def')");
15133
stmt= mysql_stmt_init(mysql);
15134
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
15135
check_execute(stmt, rc);
15137
rc= mysql_stmt_prepare(stmt, query, strlen(query));
15138
check_execute(stmt, rc);
15140
rc= mysql_stmt_execute(stmt);
15141
check_execute(stmt, rc);
15143
rc= mysql_stmt_fetch(stmt);
15144
DIE_UNLESS(rc == 0);
15146
rc= mysql_stmt_fetch(stmt);
15147
DIE_UNLESS(rc == MYSQL_NO_DATA);
15150
mysql_stmt_close(stmt);
15151
rc= mysql_query(mysql, "drop table t1");
15157
Bug #15510: mysql_warning_count returns 0 after mysql_stmt_fetch which
15160
static void test_bug15510()
15164
const char *query= "select 1 from dual where 1/0";
15166
myheader("test_bug15510");
15168
rc= mysql_query(mysql, "set @@sql_mode='ERROR_FOR_DIVISION_BY_ZERO'");
15171
stmt= mysql_stmt_init(mysql);
15173
rc= mysql_stmt_prepare(stmt, query, strlen(query));
15174
check_execute(stmt, rc);
15176
rc= mysql_stmt_execute(stmt);
15177
check_execute(stmt, rc);
15179
rc= mysql_stmt_fetch(stmt);
15180
DIE_UNLESS(mysql_warning_count(mysql));
15183
mysql_stmt_close(stmt);
15184
rc= mysql_query(mysql, "set @@sql_mode=''");
15189
/* Test MYSQL_OPT_RECONNECT, Bug#15719 */
15191
static void test_opt_reconnect()
15194
my_bool my_true= TRUE;
15196
myheader("test_opt_reconnect");
15198
if (!(lmysql= mysql_client_init(NULL)))
15200
myerror("mysql_client_init() failed");
15205
fprintf(stdout, "reconnect before mysql_options: %d\n", lmysql->reconnect);
15206
DIE_UNLESS(lmysql->reconnect == 0);
15208
if (mysql_options(lmysql, MYSQL_OPT_RECONNECT, &my_true))
15210
myerror("mysql_options failed: unknown option MYSQL_OPT_RECONNECT\n");
15214
/* reconnect should be 1 */
15216
fprintf(stdout, "reconnect after mysql_options: %d\n", lmysql->reconnect);
15217
DIE_UNLESS(lmysql->reconnect == 1);
15219
if (!(mysql_real_connect(lmysql, opt_host, opt_user,
15220
opt_password, current_db, opt_port,
15221
opt_unix_socket, 0)))
15223
myerror("connection failed");
15227
/* reconnect should still be 1 */
15229
fprintf(stdout, "reconnect after mysql_real_connect: %d\n",
15230
lmysql->reconnect);
15231
DIE_UNLESS(lmysql->reconnect == 1);
15233
mysql_close(lmysql);
15235
if (!(lmysql= mysql_client_init(NULL)))
15237
myerror("mysql_client_init() failed");
15242
fprintf(stdout, "reconnect before mysql_real_connect: %d\n", lmysql->reconnect);
15243
DIE_UNLESS(lmysql->reconnect == 0);
15245
if (!(mysql_real_connect(lmysql, opt_host, opt_user,
15246
opt_password, current_db, opt_port,
15247
opt_unix_socket, 0)))
15249
myerror("connection failed");
15253
/* reconnect should still be 0 */
15255
fprintf(stdout, "reconnect after mysql_real_connect: %d\n",
15256
lmysql->reconnect);
15257
DIE_UNLESS(lmysql->reconnect == 0);
15259
mysql_close(lmysql);
15263
#ifndef EMBEDDED_LIBRARY
15265
static void test_bug12744()
15267
MYSQL_STMT *prep_stmt = NULL;
15270
myheader("test_bug12744");
15272
lmysql= mysql_client_init(NULL);
15273
DIE_UNLESS(lmysql);
15275
if (!mysql_real_connect(lmysql, opt_host, opt_user, opt_password,
15276
current_db, opt_port, opt_unix_socket, 0))
15278
fprintf(stderr, "Failed to connect to the database\n");
15282
prep_stmt= mysql_stmt_init(lmysql);
15283
rc= mysql_stmt_prepare(prep_stmt, "SELECT 1", 8);
15284
DIE_UNLESS(rc == 0);
15286
mysql_close(lmysql);
15288
rc= mysql_stmt_execute(prep_stmt);
15290
rc= mysql_stmt_reset(prep_stmt);
15292
rc= mysql_stmt_close(prep_stmt);
15293
DIE_UNLESS(rc == 0);
15296
#endif /* EMBEDDED_LIBRARY */
15298
/* Bug #16143: mysql_stmt_sqlstate returns an empty string instead of '00000' */
15300
static void test_bug16143()
15303
myheader("test_bug16143");
15305
stmt= mysql_stmt_init(mysql);
15306
/* Check mysql_stmt_sqlstate return "no error" */
15307
DIE_UNLESS(strcmp(mysql_stmt_sqlstate(stmt), "00000") == 0);
15309
mysql_stmt_close(stmt);
15313
/* Bug #16144: mysql_stmt_attr_get type error */
15315
static void test_bug16144()
15317
const my_bool flag_orig= (my_bool) 0xde;
15318
my_bool flag= flag_orig;
15320
myheader("test_bug16144");
15322
/* Check that attr_get returns correct data on little and big endian CPUs */
15323
stmt= mysql_stmt_init(mysql);
15324
mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (const void*) &flag);
15325
mysql_stmt_attr_get(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &flag);
15326
DIE_UNLESS(flag == flag_orig);
15328
mysql_stmt_close(stmt);
15332
Bug #15613: "libmysqlclient API function mysql_stmt_prepare returns wrong
15336
static void test_bug15613()
15339
const char *stmt_text;
15340
MYSQL_RES *metadata;
15341
MYSQL_FIELD *field;
15343
myheader("test_bug15613");
15345
/* I. Prepare the table */
15346
rc= mysql_query(mysql, "set names latin1");
15348
mysql_query(mysql, "drop table if exists t1");
15349
rc= mysql_query(mysql,
15350
"create table t1 (t text character set utf8, "
15351
"tt tinytext character set utf8, "
15352
"mt mediumtext character set utf8, "
15353
"lt longtext character set utf8, "
15354
"vl varchar(255) character set latin1,"
15355
"vb varchar(255) character set binary,"
15356
"vu varchar(255) character set utf8)");
15359
stmt= mysql_stmt_init(mysql);
15361
/* II. Check SELECT metadata */
15362
stmt_text= ("select t, tt, mt, lt, vl, vb, vu from t1");
15363
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
15364
metadata= mysql_stmt_result_metadata(stmt);
15365
field= mysql_fetch_fields(metadata);
15368
printf("Field lengths (client character set is latin1):\n"
15369
"text character set utf8:\t\t%lu\n"
15370
"tinytext character set utf8:\t\t%lu\n"
15371
"mediumtext character set utf8:\t\t%lu\n"
15372
"longtext character set utf8:\t\t%lu\n"
15373
"varchar(255) character set latin1:\t%lu\n"
15374
"varchar(255) character set binary:\t%lu\n"
15375
"varchar(255) character set utf8:\t%lu\n",
15376
field[0].length, field[1].length, field[2].length, field[3].length,
15377
field[4].length, field[5].length, field[6].length);
15379
DIE_UNLESS(field[0].length == 65535);
15380
DIE_UNLESS(field[1].length == 255);
15381
DIE_UNLESS(field[2].length == 16777215);
15382
DIE_UNLESS(field[3].length == 4294967295UL);
15383
DIE_UNLESS(field[4].length == 255);
15384
DIE_UNLESS(field[5].length == 255);
15385
DIE_UNLESS(field[6].length == 255);
15386
mysql_free_result(metadata);
15387
mysql_stmt_free_result(stmt);
15390
rc= mysql_query(mysql, "drop table t1");
15392
rc= mysql_query(mysql, "set names default");
15394
mysql_stmt_close(stmt);
15398
Bug#17667: An attacker has the opportunity to bypass query logging.
15400
Note! Also tests Bug#21813, where prepared statements are used to
15403
static void test_bug17667()
15407
enum query_type { QT_NORMAL, QT_PREPARED};
15408
struct buffer_and_length {
15409
enum query_type qt;
15410
const char *buffer;
15413
{ QT_NORMAL, "drop table if exists bug17667", 29 },
15414
{ QT_NORMAL, "create table bug17667 (c varchar(20))", 37 },
15415
{ QT_NORMAL, "insert into bug17667 (c) values ('regular') /* NUL=\0 with comment */", 68 },
15417
"insert into bug17667 (c) values ('prepared') /* NUL=\0 with comment */", 69, },
15418
{ QT_NORMAL, "insert into bug17667 (c) values ('NUL=\0 in value')", 50 },
15419
{ QT_NORMAL, "insert into bug17667 (c) values ('5 NULs=\0\0\0\0\0')", 48 },
15420
{ QT_PREPARED, "insert into bug17667 (c) values ('6 NULs=\0\0\0\0\0\0')", 50 },
15421
{ QT_NORMAL, "/* NUL=\0 with comment */ insert into bug17667 (c) values ('encore')", 67 },
15422
{ QT_NORMAL, "drop table bug17667", 19 },
15423
{ QT_NORMAL, NULL, 0 } };
15425
struct buffer_and_length *statement_cursor;
15427
char *master_log_filename;
15429
myheader("test_bug17667");
15431
master_log_filename = (char *) malloc(strlen(opt_vardir) + strlen("/log/master.log") + 1);
15432
strxmov(master_log_filename, opt_vardir, "/log/master.log", NullS);
15434
printf("Opening '%s'\n", master_log_filename);
15435
log_file= my_fopen(master_log_filename, (int) (O_RDONLY | O_BINARY), MYF(0));
15436
free(master_log_filename);
15438
if (log_file == NULL)
15442
printf("Could not find the log file, VARDIR/log/master.log, so "
15443
"test_bug17667 is not run.\n"
15444
"Run test from the mysql-test/mysql-test-run* program to set up "
15445
"correct environment for this test.\n\n");
15450
enable_general_log(1);
15452
for (statement_cursor= statements; statement_cursor->buffer != NULL;
15453
statement_cursor++)
15455
if (statement_cursor->qt == QT_NORMAL)
15457
/* Run statement as normal query */
15458
rc= mysql_real_query(mysql, statement_cursor->buffer,
15459
statement_cursor->length);
15462
else if (statement_cursor->qt == QT_PREPARED)
15465
Run as prepared statement
15467
NOTE! All these queries should be in the log twice,
15468
one time for prepare and one time for execute
15470
stmt= mysql_stmt_init(mysql);
15472
rc= mysql_stmt_prepare(stmt, statement_cursor->buffer,
15473
statement_cursor->length);
15474
check_execute(stmt, rc);
15476
rc= mysql_stmt_execute(stmt);
15477
check_execute(stmt, rc);
15479
mysql_stmt_close(stmt);
15487
/* Make sure the server has written the logs to disk before reading it */
15488
rc= mysql_query(mysql, "flush logs");
15491
for (statement_cursor= statements; statement_cursor->buffer != NULL;
15492
statement_cursor++)
15494
int expected_hits= 1, hits= 0;
15495
char line_buffer[MAX_TEST_QUERY_LENGTH*2];
15496
/* more than enough room for the query and some marginalia. */
15498
/* Prepared statments always occurs twice in log */
15499
if (statement_cursor->qt == QT_PREPARED)
15502
/* Loop until we found expected number of log entries */
15504
/* Loop until statement is found in log */
15506
memset(line_buffer, '/', MAX_TEST_QUERY_LENGTH*2);
15508
if(fgets(line_buffer, MAX_TEST_QUERY_LENGTH*2, log_file) == NULL)
15510
/* If fgets returned NULL, it indicates either error or EOF */
15511
if (feof(log_file))
15512
DIE("Found EOF before all statements where found");
15514
fprintf(stderr, "Got error %d while reading from file\n",
15519
} while (my_memmem(line_buffer, MAX_TEST_QUERY_LENGTH*2,
15520
statement_cursor->buffer,
15521
statement_cursor->length) == NULL);
15523
} while (hits < expected_hits);
15526
printf("Found statement starting with \"%s\"\n",
15527
statement_cursor->buffer);
15530
restore_general_log();
15533
printf("success. All queries found intact in the log.\n");
15535
my_fclose(log_file, MYF(0));
15540
Bug#14169: type of group_concat() result changed to blob if tmp_table was
15543
static void test_bug14169()
15546
const char *stmt_text;
15548
MYSQL_FIELD *field;
15551
myheader("test_bug14169");
15553
rc= mysql_query(mysql, "drop table if exists t1");
15555
rc= mysql_query(mysql, "set session group_concat_max_len=1024");
15557
rc= mysql_query(mysql, "create table t1 (f1 int unsigned, f2 varchar(255))");
15559
rc= mysql_query(mysql, "insert into t1 values (1,repeat('a',255)),"
15560
"(2,repeat('b',255))");
15562
stmt= mysql_stmt_init(mysql);
15563
stmt_text= "select f2,group_concat(f1) from t1 group by f2";
15564
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
15566
res= mysql_stmt_result_metadata(stmt);
15567
field= mysql_fetch_fields(res);
15569
printf("GROUP_CONCAT() result type %i", field[1].type);
15570
DIE_UNLESS(field[1].type == MYSQL_TYPE_BLOB);
15571
mysql_free_result(res);
15572
mysql_stmt_free_result(stmt);
15573
mysql_stmt_close(stmt);
15575
rc= mysql_query(mysql, "drop table t1");
15580
Test that mysql_insert_id() behaves as documented in our manual
15582
static void test_mysql_insert_id()
15587
myheader("test_mysql_insert_id");
15589
rc= mysql_query(mysql, "drop table if exists t1");
15591
/* table without auto_increment column */
15592
rc= mysql_query(mysql, "create table t1 (f1 int, f2 varchar(255), key(f1))");
15594
rc= mysql_query(mysql, "insert into t1 values (1,'a')");
15596
res= mysql_insert_id(mysql);
15597
DIE_UNLESS(res == 0);
15598
rc= mysql_query(mysql, "insert into t1 values (null,'b')");
15600
res= mysql_insert_id(mysql);
15601
DIE_UNLESS(res == 0);
15602
rc= mysql_query(mysql, "insert into t1 select 5,'c'");
15604
res= mysql_insert_id(mysql);
15605
DIE_UNLESS(res == 0);
15608
Test for bug #34889: mysql_client_test::test_mysql_insert_id test fails
15611
rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
15613
rc= mysql_query(mysql, "insert into t2 values (null,'b')");
15615
rc= mysql_query(mysql, "insert into t1 select 5,'c'");
15617
res= mysql_insert_id(mysql);
15618
DIE_UNLESS(res == 0);
15619
rc= mysql_query(mysql, "drop table t2");
15622
rc= mysql_query(mysql, "insert into t1 select null,'d'");
15624
res= mysql_insert_id(mysql);
15625
DIE_UNLESS(res == 0);
15626
rc= mysql_query(mysql, "insert into t1 values (null,last_insert_id(300))");
15628
res= mysql_insert_id(mysql);
15629
DIE_UNLESS(res == 300);
15630
rc= mysql_query(mysql, "insert into t1 select null,last_insert_id(400)");
15632
res= mysql_insert_id(mysql);
15634
Behaviour change: old code used to return 0; but 400 is consistent
15635
with INSERT VALUES, and the manual's section of mysql_insert_id() does not
15636
say INSERT SELECT should be different.
15638
DIE_UNLESS(res == 400);
15640
/* table with auto_increment column */
15641
rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
15643
rc= mysql_query(mysql, "insert into t2 values (1,'a')");
15645
res= mysql_insert_id(mysql);
15646
DIE_UNLESS(res == 1);
15647
/* this should not influence next INSERT if it doesn't have auto_inc */
15648
rc= mysql_query(mysql, "insert into t1 values (10,'e')");
15650
res= mysql_insert_id(mysql);
15651
DIE_UNLESS(res == 0);
15653
rc= mysql_query(mysql, "insert into t2 values (null,'b')");
15655
res= mysql_insert_id(mysql);
15656
DIE_UNLESS(res == 2);
15657
rc= mysql_query(mysql, "insert into t2 select 5,'c'");
15659
res= mysql_insert_id(mysql);
15661
Manual says that for multirow insert this should have been 5, but does not
15662
say for INSERT SELECT. This is a behaviour change: old code used to return
15663
0. We try to be consistent with INSERT VALUES.
15665
DIE_UNLESS(res == 5);
15666
rc= mysql_query(mysql, "insert into t2 select null,'d'");
15668
res= mysql_insert_id(mysql);
15669
DIE_UNLESS(res == 6);
15670
/* with more than one row */
15671
rc= mysql_query(mysql, "insert into t2 values (10,'a'),(11,'b')");
15673
res= mysql_insert_id(mysql);
15674
DIE_UNLESS(res == 11);
15675
rc= mysql_query(mysql, "insert into t2 select 12,'a' union select 13,'b'");
15677
res= mysql_insert_id(mysql);
15679
Manual says that for multirow insert this should have been 13, but does
15680
not say for INSERT SELECT. This is a behaviour change: old code used to
15681
return 0. We try to be consistent with INSERT VALUES.
15683
DIE_UNLESS(res == 13);
15684
rc= mysql_query(mysql, "insert into t2 values (null,'a'),(null,'b')");
15686
res= mysql_insert_id(mysql);
15687
DIE_UNLESS(res == 14);
15688
rc= mysql_query(mysql, "insert into t2 select null,'a' union select null,'b'");
15690
res= mysql_insert_id(mysql);
15691
DIE_UNLESS(res == 16);
15692
rc= mysql_query(mysql, "insert into t2 select 12,'a' union select 13,'b'");
15694
rc= mysql_query(mysql, "insert ignore into t2 select 12,'a' union select 13,'b'");
15696
res= mysql_insert_id(mysql);
15697
DIE_UNLESS(res == 0);
15698
rc= mysql_query(mysql, "insert into t2 values (12,'a'),(13,'b')");
15700
res= mysql_insert_id(mysql);
15701
DIE_UNLESS(res == 0);
15702
rc= mysql_query(mysql, "insert ignore into t2 values (12,'a'),(13,'b')");
15704
res= mysql_insert_id(mysql);
15705
DIE_UNLESS(res == 0);
15706
/* mixing autogenerated and explicit values */
15707
rc= mysql_query(mysql, "insert into t2 values (null,'e'),(12,'a'),(13,'b')");
15709
rc= mysql_query(mysql, "insert into t2 values (null,'e'),(12,'a'),(13,'b'),(25,'g')");
15711
rc= mysql_query(mysql, "insert into t2 values (null,last_insert_id(300))");
15713
res= mysql_insert_id(mysql);
15715
according to the manual, this might be 20 or 300, but it looks like
15716
auto_increment column takes priority over last_insert_id().
15718
DIE_UNLESS(res == 20);
15719
/* If first autogenerated number fails and 2nd works: */
15720
rc= mysql_query(mysql, "drop table t2");
15722
rc= mysql_query(mysql, "create table t2 (f1 int not null primary key "
15723
"auto_increment, f2 varchar(255), unique (f2))");
15725
rc= mysql_query(mysql, "insert into t2 values (null,'e')");
15726
res= mysql_insert_id(mysql);
15727
DIE_UNLESS(res == 1);
15728
rc= mysql_query(mysql, "insert ignore into t2 values (null,'e'),(null,'a'),(null,'e')");
15730
res= mysql_insert_id(mysql);
15731
DIE_UNLESS(res == 2);
15732
/* If autogenerated fails and explicit works: */
15733
rc= mysql_query(mysql, "insert ignore into t2 values (null,'e'),(12,'c'),(null,'d')");
15735
res= mysql_insert_id(mysql);
15737
Behaviour change: old code returned 3 (first autogenerated, even if it
15738
fails); we now return first successful autogenerated.
15740
DIE_UNLESS(res == 13);
15741
/* UPDATE may update mysql_insert_id() if it uses LAST_INSERT_ID(#) */
15742
rc= mysql_query(mysql, "update t2 set f1=14 where f1=12");
15744
res= mysql_insert_id(mysql);
15745
DIE_UNLESS(res == 0);
15746
rc= mysql_query(mysql, "update t2 set f1=0 where f1=14");
15748
res= mysql_insert_id(mysql);
15749
DIE_UNLESS(res == 0);
15750
rc= mysql_query(mysql, "update t2 set f2=last_insert_id(372) where f1=0");
15752
res= mysql_insert_id(mysql);
15753
DIE_UNLESS(res == 372);
15754
/* check that LAST_INSERT_ID() does not update mysql_insert_id(): */
15755
rc= mysql_query(mysql, "insert into t2 values (null,'g')");
15757
res= mysql_insert_id(mysql);
15758
DIE_UNLESS(res == 15);
15759
rc= mysql_query(mysql, "update t2 set f2=(@li:=last_insert_id()) where f1=15");
15761
res= mysql_insert_id(mysql);
15762
DIE_UNLESS(res == 0);
15764
Behaviour change: now if ON DUPLICATE KEY UPDATE updates a row,
15765
mysql_insert_id() returns the id of the row, instead of not being
15768
rc= mysql_query(mysql, "insert into t2 values (null,@li) on duplicate key "
15769
"update f2=concat('we updated ',f2)");
15771
res= mysql_insert_id(mysql);
15772
DIE_UNLESS(res == 15);
15774
rc= mysql_query(mysql, "drop table t1,t2");
15779
Bug#20152: mysql_stmt_execute() writes to MYSQL_TYPE_DATE buffer
15782
static void test_bug20152()
15784
MYSQL_BIND my_bind[1];
15788
const char *query= "INSERT INTO t1 (f1) VALUES (?)";
15790
myheader("test_bug20152");
15792
memset(my_bind, 0, sizeof(my_bind));
15793
my_bind[0].buffer_type= MYSQL_TYPE_DATE;
15794
my_bind[0].buffer= (void*)&tm;
15803
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
15805
rc= mysql_query(mysql, "CREATE TABLE t1 (f1 DATE)");
15808
stmt= mysql_stmt_init(mysql);
15809
rc= mysql_stmt_prepare(stmt, query, strlen(query));
15810
check_execute(stmt, rc);
15811
rc= mysql_stmt_bind_param(stmt, my_bind);
15812
check_execute(stmt, rc);
15813
rc= mysql_stmt_execute(stmt);
15814
check_execute(stmt, rc);
15815
rc= mysql_stmt_close(stmt);
15816
check_execute(stmt, rc);
15817
rc= mysql_query(mysql, "DROP TABLE t1");
15820
if (tm.hour == 14 && tm.minute == 9 && tm.second == 42) {
15824
printf("[14:09:42] != [%02d:%02d:%02d]\n", tm.hour, tm.minute, tm.second);
15829
/* Bug#15752 "Lost connection to MySQL server when calling a SP from C API" */
15831
static void test_bug15752()
15835
const int ITERATION_COUNT= 100;
15836
const char *query= "CALL p1()";
15838
myheader("test_bug15752");
15840
rc= mysql_query(mysql, "drop procedure if exists p1");
15842
rc= mysql_query(mysql, "create procedure p1() select 1");
15845
mysql_client_init(&mysql_local);
15846
if (! mysql_real_connect(&mysql_local, opt_host, opt_user,
15847
opt_password, current_db, opt_port,
15849
CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS))
15851
printf("Unable connect to MySQL server: %s\n", mysql_error(&mysql_local));
15854
rc= mysql_real_query(&mysql_local, query, strlen(query));
15856
mysql_free_result(mysql_store_result(&mysql_local));
15858
rc= mysql_real_query(&mysql_local, query, strlen(query));
15859
DIE_UNLESS(rc && mysql_errno(&mysql_local) == CR_COMMANDS_OUT_OF_SYNC);
15862
printf("Got error (as expected): %s\n", mysql_error(&mysql_local));
15864
/* Check some other commands too */
15866
DIE_UNLESS(mysql_next_result(&mysql_local) == 0);
15867
mysql_free_result(mysql_store_result(&mysql_local));
15868
DIE_UNLESS(mysql_next_result(&mysql_local) == -1);
15870
/* The second problem is not reproducible: add the test case */
15871
for (i = 0; i < ITERATION_COUNT; i++)
15873
if (mysql_real_query(&mysql_local, query, strlen(query)))
15875
printf("\ni=%d %s failed: %s\n", i, query, mysql_error(&mysql_local));
15878
mysql_free_result(mysql_store_result(&mysql_local));
15879
DIE_UNLESS(mysql_next_result(&mysql_local) == 0);
15880
mysql_free_result(mysql_store_result(&mysql_local));
15881
DIE_UNLESS(mysql_next_result(&mysql_local) == -1);
15884
mysql_close(&mysql_local);
15885
rc= mysql_query(mysql, "drop procedure p1");
15890
Bug#21206: memory corruption when too many cursors are opened at once
15892
Memory corruption happens when more than 1024 cursors are open
15895
static void test_bug21206()
15897
const size_t cursor_count= 1025;
15899
const char *create_table[]=
15901
"DROP TABLE IF EXISTS t1",
15902
"CREATE TABLE t1 (i INT)",
15903
"INSERT INTO t1 VALUES (1), (2), (3)"
15905
const char *query= "SELECT * FROM t1";
15907
Stmt_fetch *fetch_array=
15908
(Stmt_fetch*) calloc(cursor_count, sizeof(Stmt_fetch));
15912
DBUG_ENTER("test_bug21206");
15913
myheader("test_bug21206");
15915
fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
15917
for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
15919
/* Init will exit(1) in case of error */
15920
stmt_fetch_init(fetch, fetch - fetch_array, query);
15923
for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
15924
stmt_fetch_close(fetch);
15932
Ensure we execute the status code while testing
15935
static void test_status()
15937
const char *status;
15938
DBUG_ENTER("test_status");
15939
myheader("test_status");
15941
if (!(status= mysql_stat(mysql)))
15943
myerror("mysql_stat failed"); /* purecov: inspected */
15944
die(__FILE__, __LINE__, "mysql_stat failed"); /* purecov: inspected */
15950
Bug#21726: Incorrect result with multiple invocations of
15953
Test that client gets updated value of insert_id on UPDATE that uses
15954
LAST_INSERT_ID(expr).
15955
select_query added to test for bug
15956
#26921 Problem in mysql_insert_id() Embedded C API function
15958
static void test_bug21726()
15960
const char *create_table[]=
15962
"DROP TABLE IF EXISTS t1",
15963
"CREATE TABLE t1 (i INT)",
15964
"INSERT INTO t1 VALUES (1)",
15966
const char *update_query= "UPDATE t1 SET i= LAST_INSERT_ID(i + 1)";
15968
my_ulonglong insert_id;
15969
const char *select_query= "SELECT * FROM t1";
15972
DBUG_ENTER("test_bug21726");
15973
myheader("test_bug21726");
15975
fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
15977
rc= mysql_query(mysql, update_query);
15979
insert_id= mysql_insert_id(mysql);
15980
DIE_UNLESS(insert_id == 2);
15982
rc= mysql_query(mysql, update_query);
15984
insert_id= mysql_insert_id(mysql);
15985
DIE_UNLESS(insert_id == 3);
15987
rc= mysql_query(mysql, select_query);
15989
insert_id= mysql_insert_id(mysql);
15990
DIE_UNLESS(insert_id == 3);
15991
result= mysql_store_result(mysql);
15992
mysql_free_result(result);
15999
BUG#23383: mysql_affected_rows() returns different values than
16000
mysql_stmt_affected_rows()
16002
Test that both mysql_affected_rows() and mysql_stmt_affected_rows()
16003
return -1 on error, 0 when no rows were affected, and (positive) row
16004
count when some rows were affected.
16006
static void test_bug23383()
16008
const char *insert_query= "INSERT INTO t1 VALUES (1), (2)";
16009
const char *update_query= "UPDATE t1 SET i= 4 WHERE i = 3";
16011
my_ulonglong row_count;
16014
DBUG_ENTER("test_bug23383");
16015
myheader("test_bug23383");
16017
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
16020
rc= mysql_query(mysql, "CREATE TABLE t1 (i INT UNIQUE)");
16023
rc= mysql_query(mysql, insert_query);
16025
row_count= mysql_affected_rows(mysql);
16026
DIE_UNLESS(row_count == 2);
16028
rc= mysql_query(mysql, insert_query);
16029
DIE_UNLESS(rc != 0);
16030
row_count= mysql_affected_rows(mysql);
16031
DIE_UNLESS(row_count == (my_ulonglong)-1);
16033
rc= mysql_query(mysql, update_query);
16035
row_count= mysql_affected_rows(mysql);
16036
DIE_UNLESS(row_count == 0);
16038
rc= mysql_query(mysql, "DELETE FROM t1");
16041
stmt= mysql_stmt_init(mysql);
16042
DIE_UNLESS(stmt != 0);
16044
rc= mysql_stmt_prepare(stmt, insert_query, strlen(insert_query));
16045
check_execute(stmt, rc);
16047
rc= mysql_stmt_execute(stmt);
16048
check_execute(stmt, rc);
16049
row_count= mysql_stmt_affected_rows(stmt);
16050
DIE_UNLESS(row_count == 2);
16052
rc= mysql_stmt_execute(stmt);
16053
DIE_UNLESS(rc != 0);
16054
row_count= mysql_stmt_affected_rows(stmt);
16055
DIE_UNLESS(row_count == (my_ulonglong)-1);
16057
rc= mysql_stmt_prepare(stmt, update_query, strlen(update_query));
16058
check_execute(stmt, rc);
16060
rc= mysql_stmt_execute(stmt);
16061
check_execute(stmt, rc);
16062
row_count= mysql_stmt_affected_rows(stmt);
16063
DIE_UNLESS(row_count == 0);
16065
rc= mysql_stmt_close(stmt);
16066
check_execute(stmt, rc);
16068
rc= mysql_query(mysql, "DROP TABLE t1");
16076
BUG#21635: MYSQL_FIELD struct's member strings seem to misbehave for
16079
Check that for MIN(), MAX(), COUNT() only MYSQL_FIELD::name is set
16080
to either expression or its alias, and db, org_table, table,
16081
org_name fields are empty strings.
16083
static void test_bug21635()
16085
const char *expr[]=
16087
"MIN(i)", "MIN(i)",
16088
"MIN(i) AS A1", "A1",
16089
"MAX(i)", "MAX(i)",
16090
"MAX(i) AS A2", "A2",
16091
"COUNT(i)", "COUNT(i)",
16092
"COUNT(i) AS A3", "A3",
16094
char query[MAX_TEST_QUERY_LENGTH];
16097
MYSQL_FIELD *field;
16098
unsigned int field_count, i, j;
16101
DBUG_ENTER("test_bug21635");
16102
myheader("test_bug21635");
16104
query_end= strxmov(query, "SELECT ", NullS);
16105
for (i= 0; i < sizeof(expr) / sizeof(*expr) / 2; ++i)
16106
query_end= strxmov(query_end, expr[i * 2], ", ", NullS);
16107
query_end= strxmov(query_end - 2, " FROM t1 GROUP BY i", NullS);
16108
DIE_UNLESS(query_end - query < MAX_TEST_QUERY_LENGTH);
16110
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
16112
rc= mysql_query(mysql, "CREATE TABLE t1 (i INT)");
16115
We need this loop to ensure correct behavior with both constant and
16116
non-constant tables.
16118
for (j= 0; j < 2 ; j++)
16120
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
16123
rc= mysql_real_query(mysql, query, query_end - query);
16126
result= mysql_use_result(mysql);
16127
DIE_UNLESS(result);
16129
field_count= mysql_field_count(mysql);
16130
for (i= 0; i < field_count; ++i)
16132
field= mysql_fetch_field_direct(result, i);
16135
printf("%s -> %s ... ", expr[i * 2], field->name);
16137
DIE_UNLESS(field->db[0] == 0 && field->org_table[0] == 0 &&
16138
field->table[0] == 0 && field->org_name[0] == 0);
16139
DIE_UNLESS(strcmp(field->name, expr[i * 2 + 1]) == 0);
16145
mysql_free_result(result);
16147
rc= mysql_query(mysql, "DROP TABLE t1");
16154
Bug#24179 "select b into $var" fails with --cursor_protocol"
16155
The failure is correct, check that the returned message is meaningful.
16158
static void test_bug24179()
16163
DBUG_ENTER("test_bug24179");
16164
myheader("test_bug24179");
16166
stmt= open_cursor("select 1 into @a");
16167
rc= mysql_stmt_execute(stmt);
16171
printf("Got error (as expected): %d %s\n",
16172
mysql_stmt_errno(stmt),
16173
mysql_stmt_error(stmt));
16175
DIE_UNLESS(mysql_stmt_errno(stmt) == 1323);
16176
mysql_stmt_close(stmt);
16183
Bug#32265 Server returns different metadata if prepared statement is used
16186
static void test_bug32265()
16190
MYSQL_FIELD *field;
16191
MYSQL_RES *metadata;
16193
DBUG_ENTER("test_bug32265");
16194
myheader("test_bug32265");
16196
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
16198
rc= mysql_query(mysql, "CREATE TABLE t1 (a INTEGER)");
16200
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
16202
rc= mysql_query(mysql, "CREATE VIEW v1 AS SELECT * FROM t1");
16205
stmt= open_cursor("SELECT * FROM t1");
16206
rc= mysql_stmt_execute(stmt);
16207
check_execute(stmt, rc);
16209
metadata= mysql_stmt_result_metadata(stmt);
16210
field= mysql_fetch_field(metadata);
16212
DIE_UNLESS(strcmp(field->table, "t1") == 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 a '' FROM t1 ``");
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, "") == 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");
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, "v1") == 0);
16250
DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
16251
mysql_free_result(metadata);
16252
mysql_stmt_close(stmt);
16254
stmt= open_cursor("SELECT * FROM v1 /* SIC */ GROUP BY 1");
16255
rc= mysql_stmt_execute(stmt);
16256
check_execute(stmt, rc);
16258
metadata= mysql_stmt_result_metadata(stmt);
16259
field= mysql_fetch_field(metadata);
16260
DIE_UNLESS(strcmp(field->table, "v1") == 0);
16261
DIE_UNLESS(strcmp(field->org_table, "v1") == 0);
16262
DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
16263
mysql_free_result(metadata);
16264
mysql_stmt_close(stmt);
16266
rc= mysql_query(mysql, "DROP VIEW v1");
16268
rc= mysql_query(mysql, "DROP TABLE t1");
16275
Bug#28075 "COM_DEBUG crashes mysqld"
16278
static void test_bug28075()
16282
DBUG_ENTER("test_bug28075");
16283
myheader("test_bug28075");
16285
rc= mysql_dump_debug_info(mysql);
16286
DIE_UNLESS(rc == 0);
16288
rc= mysql_ping(mysql);
16289
DIE_UNLESS(rc == 0);
16296
Bug#27876 (SF with cyrillic variable name fails during execution (regression))
16299
static void test_bug27876()
16304
uchar utf8_func[] =
16306
0xd1, 0x84, 0xd1, 0x83, 0xd0, 0xbd, 0xd0, 0xba,
16307
0xd1, 0x86, 0xd0, 0xb8, 0xd0, 0xb9, 0xd0, 0xba,
16312
uchar utf8_param[] =
16314
0xd0, 0xbf, 0xd0, 0xb0, 0xd1, 0x80, 0xd0, 0xb0,
16315
0xd0, 0xbc, 0xd0, 0xb5, 0xd1, 0x82, 0xd1, 0x8a,
16316
0xd1, 0x80, 0x5f, 0xd0, 0xb2, 0xd0, 0xb5, 0xd1,
16317
0x80, 0xd1, 0x81, 0xd0, 0xb8, 0xd1, 0x8f,
16323
DBUG_ENTER("test_bug27876");
16324
myheader("test_bug27876");
16326
rc= mysql_query(mysql, "set names utf8");
16329
rc= mysql_query(mysql, "select version()");
16331
result= mysql_store_result(mysql);
16333
mysql_free_result(result);
16335
sprintf(query, "DROP FUNCTION IF EXISTS %s", (char*) utf8_func);
16336
rc= mysql_query(mysql, query);
16340
"CREATE FUNCTION %s( %s VARCHAR(25))"
16341
" RETURNS VARCHAR(25) DETERMINISTIC RETURN %s",
16342
(char*) utf8_func, (char*) utf8_param, (char*) utf8_param);
16343
rc= mysql_query(mysql, query);
16345
sprintf(query, "SELECT %s(VERSION())", (char*) utf8_func);
16346
rc= mysql_query(mysql, query);
16348
result= mysql_store_result(mysql);
16350
mysql_free_result(result);
16352
sprintf(query, "DROP FUNCTION %s", (char*) utf8_func);
16353
rc= mysql_query(mysql, query);
16356
rc= mysql_query(mysql, "set names default");
16362
Bug#28505: mysql_affected_rows() returns wrong value if CLIENT_FOUND_ROWS
16366
static void test_bug28505()
16370
myquery(mysql_query(mysql, "drop table if exists t1"));
16371
myquery(mysql_query(mysql, "create table t1(f1 int primary key)"));
16372
myquery(mysql_query(mysql, "insert into t1 values(1)"));
16373
myquery(mysql_query(mysql,
16374
"insert into t1 values(1) on duplicate key update f1=1"));
16375
res= mysql_affected_rows(mysql);
16377
myquery(mysql_query(mysql, "drop table t1"));
16382
Bug#28934: server crash when receiving malformed com_execute packets
16385
static void test_bug28934()
16388
MYSQL_BIND bind[5];
16392
myquery(mysql_query(mysql, "drop table if exists t1"));
16393
myquery(mysql_query(mysql, "create table t1(id int)"));
16395
myquery(mysql_query(mysql, "insert into t1 values(1),(2),(3),(4),(5)"));
16396
stmt= mysql_simple_prepare(mysql,"select * from t1 where id in(?,?,?,?,?)");
16399
memset (&bind, 0, sizeof (bind));
16400
for (cnt= 0; cnt < 5; cnt++)
16402
bind[cnt].buffer_type= MYSQL_TYPE_LONG;
16403
bind[cnt].buffer= (char*)&cnt;
16404
bind[cnt].buffer_length= 0;
16406
myquery(mysql_stmt_bind_param(stmt, bind));
16408
stmt->param_count=2;
16409
error= mysql_stmt_execute(stmt);
16410
DIE_UNLESS(error != 0);
16412
mysql_stmt_close(stmt);
16414
myquery(mysql_query(mysql, "drop table t1"));
16418
Test mysql_change_user() C API and COM_CHANGE_USER
16421
static void test_change_user()
16424
const char *user_pw= "mysqltest_pw";
16425
const char *user_no_pw= "mysqltest_no_pw";
16426
const char *pw= "password";
16427
const char *db= "mysqltest_user_test_database";
16430
DBUG_ENTER("test_change_user");
16431
myheader("test_change_user");
16433
/* Prepare environment */
16434
sprintf(buff, "drop database if exists %s", db);
16435
rc= mysql_query(mysql, buff);
16438
sprintf(buff, "create database %s", db);
16439
rc= mysql_query(mysql, buff);
16443
"grant select on %s.* to %s@'%%' identified by '%s'",
16447
rc= mysql_query(mysql, buff);
16451
"grant select on %s.* to %s@'localhost' identified by '%s'",
16455
rc= mysql_query(mysql, buff);
16459
"grant select on %s.* to %s@'%%'",
16462
rc= mysql_query(mysql, buff);
16466
"grant select on %s.* to %s@'localhost'",
16469
rc= mysql_query(mysql, buff);
16473
/* Try some combinations */
16474
rc= mysql_change_user(mysql, NULL, NULL, NULL);
16477
printf("Got error (as expected): %s\n", mysql_error(mysql));
16480
rc= mysql_change_user(mysql, "", NULL, NULL);
16483
printf("Got error (as expected): %s\n", mysql_error(mysql));
16485
rc= mysql_change_user(mysql, "", "", NULL);
16488
printf("Got error (as expected): %s\n", mysql_error(mysql));
16490
rc= mysql_change_user(mysql, "", "", "");
16493
printf("Got error (as expected): %s\n", mysql_error(mysql));
16495
rc= mysql_change_user(mysql, NULL, "", "");
16498
printf("Got error (as expected): %s\n", mysql_error(mysql));
16501
rc= mysql_change_user(mysql, NULL, NULL, "");
16504
printf("Got error (as expected): %s\n", mysql_error(mysql));
16506
rc= mysql_change_user(mysql, "", NULL, "");
16509
printf("Got error (as expected): %s\n", mysql_error(mysql));
16511
rc= mysql_change_user(mysql, user_pw, NULL, "");
16514
printf("Got error (as expected): %s\n", mysql_error(mysql));
16516
rc= mysql_change_user(mysql, user_pw, "", "");
16519
printf("Got error (as expected): %s\n", mysql_error(mysql));
16521
rc= mysql_change_user(mysql, user_pw, "", NULL);
16524
printf("Got error (as expected): %s\n", mysql_error(mysql));
16526
rc= mysql_change_user(mysql, user_pw, NULL, NULL);
16529
printf("Got error (as expected): %s\n", mysql_error(mysql));
16531
rc= mysql_change_user(mysql, user_pw, "", db);
16534
printf("Got error (as expected): %s\n", mysql_error(mysql));
16536
rc= mysql_change_user(mysql, user_pw, NULL, db);
16539
printf("Got error (as expected): %s\n", mysql_error(mysql));
16541
rc= mysql_change_user(mysql, user_pw, pw, db);
16544
rc= mysql_change_user(mysql, user_pw, pw, NULL);
16547
rc= mysql_change_user(mysql, user_pw, pw, "");
16550
rc= mysql_change_user(mysql, user_no_pw, pw, db);
16553
printf("Got error (as expected): %s\n", mysql_error(mysql));
16555
rc= mysql_change_user(mysql, user_no_pw, pw, "");
16558
printf("Got error (as expected): %s\n", mysql_error(mysql));
16560
rc= mysql_change_user(mysql, user_no_pw, pw, NULL);
16563
printf("Got error (as expected): %s\n", mysql_error(mysql));
16565
rc= mysql_change_user(mysql, user_no_pw, "", NULL);
16568
rc= mysql_change_user(mysql, user_no_pw, "", "");
16571
rc= mysql_change_user(mysql, user_no_pw, "", db);
16574
rc= mysql_change_user(mysql, user_no_pw, NULL, db);
16577
rc= mysql_change_user(mysql, "", pw, db);
16580
printf("Got error (as expected): %s\n", mysql_error(mysql));
16582
rc= mysql_change_user(mysql, "", pw, "");
16585
printf("Got error (as expected): %s\n", mysql_error(mysql));
16587
rc= mysql_change_user(mysql, "", pw, NULL);
16590
printf("Got error (as expected): %s\n", mysql_error(mysql));
16592
rc= mysql_change_user(mysql, NULL, pw, NULL);
16595
printf("Got error (as expected): %s\n", mysql_error(mysql));
16597
rc= mysql_change_user(mysql, NULL, NULL, db);
16600
printf("Got error (as expected): %s\n", mysql_error(mysql));
16602
rc= mysql_change_user(mysql, NULL, "", db);
16605
printf("Got error (as expected): %s\n", mysql_error(mysql));
16607
rc= mysql_change_user(mysql, "", "", db);
16610
printf("Got error (as expected): %s\n", mysql_error(mysql));
16612
/* Cleanup the environment */
16614
mysql_change_user(mysql, opt_user, opt_password, current_db);
16616
sprintf(buff, "drop database %s", db);
16617
rc= mysql_query(mysql, buff);
16620
sprintf(buff, "drop user %s@'%%'", user_pw);
16621
rc= mysql_query(mysql, buff);
16624
sprintf(buff, "drop user %s@'%%'", user_no_pw);
16625
rc= mysql_query(mysql, buff);
16628
sprintf(buff, "drop user %s@'localhost'", user_pw);
16629
rc= mysql_query(mysql, buff);
16632
sprintf(buff, "drop user %s@'localhost'", user_no_pw);
16633
rc= mysql_query(mysql, buff);
16640
Bug#27592 (stack overrun when storing datetime value using prepared statements)
16643
static void test_bug27592()
16645
const int NUM_ITERATIONS= 40;
16648
MYSQL_STMT *stmt= NULL;
16649
MYSQL_BIND bind[1];
16650
MYSQL_TIME time_val;
16652
DBUG_ENTER("test_bug27592");
16653
myheader("test_bug27592");
16655
mysql_query(mysql, "DROP TABLE IF EXISTS t1");
16656
mysql_query(mysql, "CREATE TABLE t1(c2 DATETIME)");
16658
stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 VALUES (?)");
16661
memset(bind, 0, sizeof(bind));
16663
bind[0].buffer_type= MYSQL_TYPE_DATETIME;
16664
bind[0].buffer= (char *) &time_val;
16665
bind[0].length= NULL;
16667
for (i= 0; i < NUM_ITERATIONS; i++)
16669
time_val.year= 2007;
16673
time_val.minute= 41;
16674
time_val.second= 3;
16676
time_val.second_part=0;
16679
rc= mysql_stmt_bind_param(stmt, bind);
16680
check_execute(stmt, rc);
16682
rc= mysql_stmt_execute(stmt);
16683
check_execute(stmt, rc);
16686
mysql_stmt_close(stmt);
16692
Bug#29687 mysql_stmt_store_result memory leak in libmysqld
16695
static void test_bug29687()
16697
const int NUM_ITERATIONS= 40;
16700
MYSQL_STMT *stmt= NULL;
16702
DBUG_ENTER("test_bug29687");
16703
myheader("test_bug29687");
16705
stmt= mysql_simple_prepare(mysql, "SELECT 1 FROM dual WHERE 0=2");
16708
for (i= 0; i < NUM_ITERATIONS; i++)
16710
rc= mysql_stmt_execute(stmt);
16711
check_execute(stmt, rc);
16712
mysql_stmt_store_result(stmt);
16713
while (mysql_stmt_fetch(stmt)==0);
16714
mysql_stmt_free_result(stmt);
16717
mysql_stmt_close(stmt);
16723
Bug #29692 Single row inserts can incorrectly report a huge number of
16727
static void test_bug29692()
16731
if (!(conn= mysql_client_init(NULL)))
16733
myerror("test_bug29692 init failed");
16737
if (!(mysql_real_connect(conn, opt_host, opt_user,
16738
opt_password, opt_db ? opt_db:"test", opt_port,
16739
opt_unix_socket, CLIENT_FOUND_ROWS)))
16741
myerror("test_bug29692 connection failed");
16742
mysql_close(mysql);
16745
myquery(mysql_query(conn, "drop table if exists t1"));
16746
myquery(mysql_query(conn, "create table t1(f1 int)"));
16747
myquery(mysql_query(conn, "insert into t1 values(1)"));
16748
DIE_UNLESS(1 == mysql_affected_rows(conn));
16749
myquery(mysql_query(conn, "drop table t1"));
16754
Bug#29306 Truncated data in MS Access with decimal (3,1) columns in a VIEW
16757
static void test_bug29306()
16759
MYSQL_FIELD *field;
16763
DBUG_ENTER("test_bug29306");
16764
myheader("test_bug29306");
16766
rc= mysql_query(mysql, "DROP TABLE IF EXISTS tab17557");
16768
rc= mysql_query(mysql, "DROP VIEW IF EXISTS view17557");
16770
rc= mysql_query(mysql, "CREATE TABLE tab17557 (dd decimal (3,1))");
16772
rc= mysql_query(mysql, "CREATE VIEW view17557 as SELECT dd FROM tab17557");
16774
rc= mysql_query(mysql, "INSERT INTO tab17557 VALUES (7.6)");
16777
/* Checking the view */
16778
res= mysql_list_fields(mysql, "view17557", NULL);
16779
while ((field= mysql_fetch_field(res)))
16783
printf("field name %s\n", field->name);
16784
printf("field table %s\n", field->table);
16785
printf("field decimals %d\n", field->decimals);
16786
if (field->decimals < 1)
16787
printf("Error! No decimals! \n");
16790
DIE_UNLESS(field->decimals == 1);
16792
mysql_free_result(res);
16794
rc= mysql_query(mysql, "DROP TABLE tab17557");
16796
rc= mysql_query(mysql, "DROP VIEW view17557");
16802
Bug#30472: libmysql doesn't reset charset, insert_id after succ.
16803
mysql_change_user() call row insertions.
16806
static void bug30472_retrieve_charset_info(MYSQL *con,
16807
char *character_set_name,
16808
char *character_set_client,
16809
char *character_set_results,
16810
char *collation_connection)
16815
/* Get the cached client character set name. */
16817
strcpy(character_set_name, mysql_character_set_name(con));
16819
/* Retrieve server character set information. */
16821
DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'character_set_client'"));
16822
DIE_UNLESS(rs= mysql_store_result(con));
16823
DIE_UNLESS(row= mysql_fetch_row(rs));
16824
strcpy(character_set_client, row[1]);
16825
mysql_free_result(rs);
16827
DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'character_set_results'"));
16828
DIE_UNLESS(rs= mysql_store_result(con));
16829
DIE_UNLESS(row= mysql_fetch_row(rs));
16830
strcpy(character_set_results, row[1]);
16831
mysql_free_result(rs);
16833
DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'collation_connection'"));
16834
DIE_UNLESS(rs= mysql_store_result(con));
16835
DIE_UNLESS(row= mysql_fetch_row(rs));
16836
strcpy(collation_connection, row[1]);
16837
mysql_free_result(rs);
16840
static void test_bug30472()
16844
char character_set_name_1[MY_CS_NAME_SIZE];
16845
char character_set_client_1[MY_CS_NAME_SIZE];
16846
char character_set_results_1[MY_CS_NAME_SIZE];
16847
char collation_connnection_1[MY_CS_NAME_SIZE];
16849
char character_set_name_2[MY_CS_NAME_SIZE];
16850
char character_set_client_2[MY_CS_NAME_SIZE];
16851
char character_set_results_2[MY_CS_NAME_SIZE];
16852
char collation_connnection_2[MY_CS_NAME_SIZE];
16854
char character_set_name_3[MY_CS_NAME_SIZE];
16855
char character_set_client_3[MY_CS_NAME_SIZE];
16856
char character_set_results_3[MY_CS_NAME_SIZE];
16857
char collation_connnection_3[MY_CS_NAME_SIZE];
16859
char character_set_name_4[MY_CS_NAME_SIZE];
16860
char character_set_client_4[MY_CS_NAME_SIZE];
16861
char character_set_results_4[MY_CS_NAME_SIZE];
16862
char collation_connnection_4[MY_CS_NAME_SIZE];
16864
/* Create a new connection. */
16866
DIE_UNLESS(mysql_client_init(&con));
16868
DIE_UNLESS(mysql_real_connect(&con,
16872
opt_db ? opt_db : "test",
16875
CLIENT_FOUND_ROWS));
16877
/* Retrieve character set information. */
16879
bug30472_retrieve_charset_info(&con,
16880
character_set_name_1,
16881
character_set_client_1,
16882
character_set_results_1,
16883
collation_connnection_1);
16885
/* Switch client character set. */
16887
DIE_IF(mysql_set_character_set(&con, "utf8"));
16889
/* Retrieve character set information. */
16891
bug30472_retrieve_charset_info(&con,
16892
character_set_name_2,
16893
character_set_client_2,
16894
character_set_results_2,
16895
collation_connnection_2);
16899
1) character set has been switched and
16900
2) new character set is different from the original one.
16903
DIE_UNLESS(strcmp(character_set_name_2, "utf8") == 0);
16904
DIE_UNLESS(strcmp(character_set_client_2, "utf8") == 0);
16905
DIE_UNLESS(strcmp(character_set_results_2, "utf8") == 0);
16906
DIE_UNLESS(strcmp(collation_connnection_2, "utf8_general_ci") == 0);
16908
DIE_UNLESS(strcmp(character_set_name_1, character_set_name_2) != 0);
16909
DIE_UNLESS(strcmp(character_set_client_1, character_set_client_2) != 0);
16910
DIE_UNLESS(strcmp(character_set_results_1, character_set_results_2) != 0);
16911
DIE_UNLESS(strcmp(collation_connnection_1, collation_connnection_2) != 0);
16913
/* Call mysql_change_user() with the same username, password, database. */
16915
DIE_IF(mysql_change_user(&con,
16918
opt_db ? opt_db : "test"));
16920
/* Retrieve character set information. */
16922
bug30472_retrieve_charset_info(&con,
16923
character_set_name_3,
16924
character_set_client_3,
16925
character_set_results_3,
16926
collation_connnection_3);
16928
/* Check that character set information has been reset. */
16930
DIE_UNLESS(strcmp(character_set_name_1, character_set_name_3) == 0);
16931
DIE_UNLESS(strcmp(character_set_client_1, character_set_client_3) == 0);
16932
DIE_UNLESS(strcmp(character_set_results_1, character_set_results_3) == 0);
16933
DIE_UNLESS(strcmp(collation_connnection_1, collation_connnection_3) == 0);
16935
/* Change connection-default character set in the client. */
16937
mysql_options(&con, MYSQL_SET_CHARSET_NAME, "utf8");
16940
Call mysql_change_user() in order to check that new connection will
16941
have UTF8 character set on the client and on the server.
16944
DIE_IF(mysql_change_user(&con,
16947
opt_db ? opt_db : "test"));
16949
/* Retrieve character set information. */
16951
bug30472_retrieve_charset_info(&con,
16952
character_set_name_4,
16953
character_set_client_4,
16954
character_set_results_4,
16955
collation_connnection_4);
16957
/* Check that we have UTF8 on the server and on the client. */
16959
DIE_UNLESS(strcmp(character_set_name_4, "utf8") == 0);
16960
DIE_UNLESS(strcmp(character_set_client_4, "utf8") == 0);
16961
DIE_UNLESS(strcmp(character_set_results_4, "utf8") == 0);
16962
DIE_UNLESS(strcmp(collation_connnection_4, "utf8_general_ci") == 0);
16964
/* That's it. Cleanup. */
16969
static void bug20023_change_user(MYSQL *con)
16971
DIE_IF(mysql_change_user(con,
16974
opt_db ? opt_db : "test"));
16977
static my_bool query_str_variable(MYSQL *con,
16978
const char *var_name,
16985
char query_buffer[MAX_TEST_QUERY_LENGTH];
16989
my_snprintf(query_buffer, sizeof (query_buffer),
16990
"SELECT %s", var_name);
16992
DIE_IF(mysql_query(con, query_buffer));
16993
DIE_UNLESS(rs= mysql_store_result(con));
16994
DIE_UNLESS(row= mysql_fetch_row(rs));
16996
is_null= row[0] == NULL;
16999
my_snprintf(str, len, "%s", row[0]);
17001
mysql_free_result(rs);
17006
static my_bool query_int_variable(MYSQL *con,
17007
const char *var_name,
17011
my_bool is_null= query_str_variable(con, var_name, str, sizeof(str));
17014
*var_value= atoi(str);
17019
static void test_bug20023()
17023
int sql_big_selects_orig= 0;
17025
Type of max_join_size is ha_rows, which might be ulong or off_t
17026
depending on the platform or configure options. Preserve the string
17027
to avoid type overflow pitfalls.
17029
char max_join_size_orig[32];
17031
int sql_big_selects_2= 0;
17032
int sql_big_selects_3= 0;
17033
int sql_big_selects_4= 0;
17034
int sql_big_selects_5= 0;
17036
char query_buffer[MAX_TEST_QUERY_LENGTH];
17038
/* Create a new connection. */
17040
DIE_UNLESS(mysql_client_init(&con));
17042
DIE_UNLESS(mysql_real_connect(&con,
17046
opt_db ? opt_db : "test",
17049
CLIENT_FOUND_ROWS));
17051
/***********************************************************************
17052
Remember original SQL_BIG_SELECTS, MAX_JOIN_SIZE values.
17053
***********************************************************************/
17055
query_int_variable(&con,
17056
"@@session.sql_big_selects",
17057
&sql_big_selects_orig);
17059
query_str_variable(&con,
17060
"@@global.max_join_size",
17061
max_join_size_orig,
17062
sizeof(max_join_size_orig));
17064
/***********************************************************************
17065
Test that COM_CHANGE_USER resets the SQL_BIG_SELECTS to the initial value.
17066
***********************************************************************/
17068
/* Issue COM_CHANGE_USER. */
17070
bug20023_change_user(&con);
17072
/* Query SQL_BIG_SELECTS. */
17074
query_int_variable(&con,
17075
"@@session.sql_big_selects",
17076
&sql_big_selects_2);
17078
/* Check that SQL_BIG_SELECTS is reset properly. */
17080
DIE_UNLESS(sql_big_selects_orig == sql_big_selects_2);
17082
/***********************************************************************
17083
Test that if MAX_JOIN_SIZE set to non-default value,
17084
SQL_BIG_SELECTS will be 0.
17085
***********************************************************************/
17087
/* Set MAX_JOIN_SIZE to some non-default value. */
17089
DIE_IF(mysql_query(&con, "SET @@global.max_join_size = 10000"));
17090
DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
17092
/* Issue COM_CHANGE_USER. */
17094
bug20023_change_user(&con);
17096
/* Query SQL_BIG_SELECTS. */
17098
query_int_variable(&con,
17099
"@@session.sql_big_selects",
17100
&sql_big_selects_3);
17102
/* Check that SQL_BIG_SELECTS is 0. */
17104
DIE_UNLESS(sql_big_selects_3 == 0);
17106
/***********************************************************************
17107
Test that if MAX_JOIN_SIZE set to default value,
17108
SQL_BIG_SELECTS will be 1.
17109
***********************************************************************/
17111
/* Set MAX_JOIN_SIZE to the default value (-1). */
17113
DIE_IF(mysql_query(&con, "SET @@global.max_join_size = -1"));
17114
DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
17116
/* Issue COM_CHANGE_USER. */
17118
bug20023_change_user(&con);
17120
/* Query SQL_BIG_SELECTS. */
17122
query_int_variable(&con,
17123
"@@session.sql_big_selects",
17124
&sql_big_selects_4);
17126
/* Check that SQL_BIG_SELECTS is 1. */
17128
DIE_UNLESS(sql_big_selects_4 == 1);
17130
/***********************************************************************
17131
Restore MAX_JOIN_SIZE.
17132
Check that SQL_BIG_SELECTS will be the original one.
17133
***********************************************************************/
17135
/* Restore MAX_JOIN_SIZE. */
17137
my_snprintf(query_buffer,
17138
sizeof (query_buffer),
17139
"SET @@global.max_join_size = %s",
17140
max_join_size_orig);
17142
DIE_IF(mysql_query(&con, query_buffer));
17144
DIE_IF(mysql_query(&con, "SET @@global.max_join_size = -1"));
17145
DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
17147
/* Issue COM_CHANGE_USER. */
17149
bug20023_change_user(&con);
17151
/* Query SQL_BIG_SELECTS. */
17153
query_int_variable(&con,
17154
"@@session.sql_big_selects",
17155
&sql_big_selects_5);
17157
/* Check that SQL_BIG_SELECTS is 1. */
17159
DIE_UNLESS(sql_big_selects_5 == sql_big_selects_orig);
17161
/***********************************************************************
17162
That's it. Cleanup.
17163
***********************************************************************/
17168
static void bug31418_impl()
17175
/* Create a new connection. */
17177
DIE_UNLESS(mysql_client_init(&con));
17179
DIE_UNLESS(mysql_real_connect(&con,
17183
opt_db ? opt_db : "test",
17186
CLIENT_FOUND_ROWS));
17188
/***********************************************************************
17189
Check that lock is free:
17190
- IS_FREE_LOCK() should return 1;
17191
- IS_USED_LOCK() should return NULL;
17192
***********************************************************************/
17194
is_null= query_int_variable(&con,
17195
"IS_FREE_LOCK('bug31418')",
17197
DIE_UNLESS(!is_null && rc);
17199
is_null= query_int_variable(&con,
17200
"IS_USED_LOCK('bug31418')",
17202
DIE_UNLESS(is_null);
17204
/***********************************************************************
17205
Acquire lock and check the lock status (the lock must be in use):
17206
- IS_FREE_LOCK() should return 0;
17207
- IS_USED_LOCK() should return non-zero thread id;
17208
***********************************************************************/
17210
query_int_variable(&con, "GET_LOCK('bug31418', 1)", &rc);
17213
is_null= query_int_variable(&con,
17214
"IS_FREE_LOCK('bug31418')",
17216
DIE_UNLESS(!is_null && !rc);
17218
is_null= query_int_variable(&con,
17219
"IS_USED_LOCK('bug31418')",
17221
DIE_UNLESS(!is_null && rc);
17223
/***********************************************************************
17224
Issue COM_CHANGE_USER command and check the lock status
17225
(the lock must be free):
17226
- IS_FREE_LOCK() should return 1;
17227
- IS_USED_LOCK() should return NULL;
17228
**********************************************************************/
17230
bug20023_change_user(&con);
17232
is_null= query_int_variable(&con,
17233
"IS_FREE_LOCK('bug31418')",
17235
DIE_UNLESS(!is_null && rc);
17237
is_null= query_int_variable(&con,
17238
"IS_USED_LOCK('bug31418')",
17240
DIE_UNLESS(is_null);
17242
/***********************************************************************
17243
That's it. Cleanup.
17244
***********************************************************************/
17249
static void test_bug31418()
17251
/* Run test case for BUG#31418 for three different connections. */
17263
Bug#31669 Buffer overflow in mysql_change_user()
17266
#define LARGE_BUFFER_SIZE 2048
17268
static void test_bug31669()
17271
static char buff[LARGE_BUFFER_SIZE+1];
17272
#ifndef EMBEDDED_LIBRARY
17273
static char user[USERNAME_CHAR_LENGTH+1];
17274
static char db[NAME_CHAR_LEN+1];
17275
static char query[LARGE_BUFFER_SIZE*2];
17278
DBUG_ENTER("test_bug31669");
17279
myheader("test_bug31669");
17281
rc= mysql_change_user(mysql, NULL, NULL, NULL);
17284
rc= mysql_change_user(mysql, "", "", "");
17287
memset(buff, 'a', sizeof(buff));
17289
rc= mysql_change_user(mysql, buff, buff, buff);
17292
rc = mysql_change_user(mysql, opt_user, opt_password, current_db);
17295
#ifndef EMBEDDED_LIBRARY
17296
memset(db, 'a', sizeof(db));
17297
db[NAME_CHAR_LEN]= 0;
17298
strxmov(query, "CREATE DATABASE IF NOT EXISTS ", db, NullS);
17299
rc= mysql_query(mysql, query);
17302
memset(user, 'b', sizeof(user));
17303
user[USERNAME_CHAR_LENGTH]= 0;
17304
memset(buff, 'c', sizeof(buff));
17305
buff[LARGE_BUFFER_SIZE]= 0;
17306
strxmov(query, "GRANT ALL PRIVILEGES ON *.* TO '", user, "'@'%' IDENTIFIED BY "
17307
"'", buff, "' WITH GRANT OPTION", NullS);
17308
rc= mysql_query(mysql, query);
17311
strxmov(query, "GRANT ALL PRIVILEGES ON *.* TO '", user, "'@'localhost' IDENTIFIED BY "
17312
"'", buff, "' WITH GRANT OPTION", NullS);
17313
rc= mysql_query(mysql, query);
17316
rc= mysql_query(mysql, "FLUSH PRIVILEGES");
17319
rc= mysql_change_user(mysql, user, buff, db);
17322
user[USERNAME_CHAR_LENGTH-1]= 'a';
17323
rc= mysql_change_user(mysql, user, buff, db);
17326
user[USERNAME_CHAR_LENGTH-1]= 'b';
17327
buff[LARGE_BUFFER_SIZE-1]= 'd';
17328
rc= mysql_change_user(mysql, user, buff, db);
17331
buff[LARGE_BUFFER_SIZE-1]= 'c';
17332
db[NAME_CHAR_LEN-1]= 'e';
17333
rc= mysql_change_user(mysql, user, buff, db);
17336
db[NAME_CHAR_LEN-1]= 'a';
17337
rc= mysql_change_user(mysql, user, buff, db);
17340
rc= mysql_change_user(mysql, user + 1, buff + 1, db + 1);
17343
rc = mysql_change_user(mysql, opt_user, opt_password, current_db);
17346
strxmov(query, "DROP DATABASE ", db, NullS);
17347
rc= mysql_query(mysql, query);
17350
strxmov(query, "DELETE FROM mysql.user WHERE User='", user, "'", NullS);
17351
rc= mysql_query(mysql, query);
17353
DIE_UNLESS(mysql_affected_rows(mysql) == 2);
17361
Bug#28386 the general log is incomplete
17364
static void test_bug28386()
17371
const char hello[]= "hello world!";
17373
DBUG_ENTER("test_bug28386");
17374
myheader("test_bug28386");
17376
rc= mysql_query(mysql, "select @@global.log_output");
17379
result= mysql_store_result(mysql);
17380
DIE_UNLESS(result);
17382
row= mysql_fetch_row(result);
17383
if (! strstr(row[0], "TABLE"))
17385
mysql_free_result(result);
17387
printf("Skipping the test since logging to tables is not enabled\n");
17388
/* Log output is not to tables */
17391
mysql_free_result(result);
17393
enable_general_log(1);
17395
stmt= mysql_simple_prepare(mysql, "SELECT ?");
17398
memset(&bind, 0, sizeof(bind));
17400
bind.buffer_type= MYSQL_TYPE_STRING;
17401
bind.buffer= (void *) hello;
17402
bind.buffer_length= sizeof(hello);
17404
mysql_stmt_bind_param(stmt, &bind);
17405
mysql_stmt_send_long_data(stmt, 0, hello, sizeof(hello));
17407
rc= mysql_stmt_execute(stmt);
17408
check_execute(stmt, rc);
17410
rc= my_process_stmt_result(stmt);
17411
DIE_UNLESS(rc == 1);
17413
rc= mysql_stmt_reset(stmt);
17414
check_execute(stmt, rc);
17416
rc= mysql_stmt_close(stmt);
17419
rc= mysql_query(mysql, "select * from mysql.general_log where "
17420
"command_type='Close stmt' or "
17421
"command_type='Reset stmt' or "
17422
"command_type='Long Data'");
17425
result= mysql_store_result(mysql);
17428
DIE_UNLESS(mysql_num_rows(result) == 3);
17430
mysql_free_result(result);
17432
restore_general_log();
17437
static void test_wl4166_1()
17446
double double_data;
17448
my_bool is_null[7];
17449
MYSQL_BIND my_bind[7];
17453
myheader("test_wl4166_1");
17455
rc= mysql_query(mysql, "DROP TABLE IF EXISTS table_4166");
17458
rc= mysql_query(mysql, "CREATE TABLE table_4166(col1 tinyint NOT NULL, "
17459
"col2 varchar(15), col3 int, "
17460
"col4 smallint, col5 bigint, "
17461
"col6 float, col7 double, "
17462
"colX varchar(10) default NULL)");
17465
stmt= mysql_simple_prepare(mysql,
17466
"INSERT INTO table_4166(col1, col2, col3, col4, col5, col6, col7) "
17467
"VALUES(?, ?, ?, ?, ?, ?, ?)");
17470
verify_param_count(stmt, 7);
17472
bzero(my_bind, sizeof(my_bind));
17474
my_bind[0].buffer_type= MYSQL_TYPE_TINY;
17475
my_bind[0].buffer= (void *)&tiny_data;
17477
my_bind[1].buffer_type= MYSQL_TYPE_STRING;
17478
my_bind[1].buffer= (void *)str_data;
17479
my_bind[1].buffer_length= 1000; /* Max string length */
17481
my_bind[2].buffer_type= MYSQL_TYPE_LONG;
17482
my_bind[2].buffer= (void *)&int_data;
17484
my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
17485
my_bind[3].buffer= (void *)&small_data;
17487
my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
17488
my_bind[4].buffer= (void *)&big_data;
17490
my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
17491
my_bind[5].buffer= (void *)&real_data;
17493
my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
17494
my_bind[6].buffer= (void *)&double_data;
17496
for (i= 0; i < (int) array_elements(my_bind); i++)
17498
my_bind[i].length= &length[i];
17499
my_bind[i].is_null= &is_null[i];
17503
rc= mysql_stmt_bind_param(stmt, my_bind);
17504
check_execute(stmt, rc);
17510
double_data= 6578.001;
17512
/* now, execute the prepared statement to insert 10 records.. */
17513
for (tiny_data= 0; tiny_data < 10; tiny_data++)
17515
length[1]= my_sprintf(str_data, (str_data, "MySQL%d", int_data));
17516
rc= mysql_stmt_execute(stmt);
17517
check_execute(stmt, rc);
17522
double_data += 10.09;
17525
/* force a re-prepare with some DDL */
17527
rc= mysql_query(mysql,
17528
"ALTER TABLE table_4166 change colX colX varchar(20) default NULL");
17532
execute the prepared statement again,
17533
without changing the types of parameters already bound.
17536
for (tiny_data= 50; tiny_data < 60; tiny_data++)
17538
length[1]= my_sprintf(str_data, (str_data, "MySQL%d", int_data));
17539
rc= mysql_stmt_execute(stmt);
17540
check_execute(stmt, rc);
17545
double_data += 10.09;
17548
mysql_stmt_close(stmt);
17550
rc= mysql_query(mysql, "DROP TABLE table_4166");
17555
static void test_wl4166_2()
17560
MYSQL_BIND bind_out[2];
17563
myheader("test_wl4166_2");
17565
rc= mysql_query(mysql, "SET SQL_MODE=''");
17568
rc= mysql_query(mysql, "drop table if exists t1");
17570
rc= mysql_query(mysql, "create table t1 (c_int int, d_date date)");
17572
rc= mysql_query(mysql,
17573
"insert into t1 (c_int, d_date) values (42, '1948-05-15')");
17576
stmt= mysql_simple_prepare(mysql, "select * from t1");
17579
bzero(bind_out, sizeof(bind_out));
17580
bind_out[0].buffer_type= MYSQL_TYPE_LONG;
17581
bind_out[0].buffer= (void*) &c_int;
17583
bind_out[1].buffer_type= MYSQL_TYPE_DATE;
17584
bind_out[1].buffer= (void*) &d_date;
17586
rc= mysql_stmt_bind_result(stmt, bind_out);
17587
check_execute(stmt, rc);
17589
/* int -> varchar transition */
17591
rc= mysql_query(mysql,
17592
"alter table t1 change column c_int c_int varchar(11)");
17595
rc= mysql_stmt_execute(stmt);
17596
check_execute(stmt, rc);
17598
rc= mysql_stmt_fetch(stmt);
17599
check_execute(stmt, rc);
17601
DIE_UNLESS(c_int == 42);
17602
DIE_UNLESS(d_date.year == 1948);
17603
DIE_UNLESS(d_date.month == 5);
17604
DIE_UNLESS(d_date.day == 15);
17606
rc= mysql_stmt_fetch(stmt);
17607
DIE_UNLESS(rc == MYSQL_NO_DATA);
17609
/* varchar to int retrieval with truncation */
17611
rc= mysql_query(mysql, "update t1 set c_int='abcde'");
17614
rc= mysql_stmt_execute(stmt);
17615
check_execute(stmt, rc);
17617
rc= mysql_stmt_fetch(stmt);
17618
check_execute_r(stmt, rc);
17620
DIE_UNLESS(c_int == 0);
17622
rc= mysql_stmt_fetch(stmt);
17623
DIE_UNLESS(rc == MYSQL_NO_DATA);
17625
/* alter table and increase the number of columns */
17626
rc= mysql_query(mysql, "alter table t1 add column d_int int");
17629
rc= mysql_stmt_execute(stmt);
17630
check_execute_r(stmt, rc);
17632
rc= mysql_stmt_reset(stmt);
17633
check_execute(stmt, rc);
17635
/* decrease the number of columns */
17636
rc= mysql_query(mysql, "alter table t1 drop d_date, drop d_int");
17639
rc= mysql_stmt_execute(stmt);
17640
check_execute_r(stmt, rc);
17642
mysql_stmt_close(stmt);
17643
rc= mysql_query(mysql, "drop table t1");
17649
Bug#38486 Crash when using cursor protocol
17652
static void test_bug38486(void)
17655
const char *stmt_text;
17656
unsigned long type= CURSOR_TYPE_READ_ONLY;
17658
DBUG_ENTER("test_bug38486");
17659
myheader("test_bug38486");
17661
stmt= mysql_stmt_init(mysql);
17662
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*)&type);
17663
stmt_text= "CREATE TABLE t1 (a INT)";
17664
mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
17665
mysql_stmt_execute(stmt);
17666
mysql_stmt_close(stmt);
17668
stmt= mysql_stmt_init(mysql);
17669
mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*)&type);
17670
stmt_text= "INSERT INTO t1 VALUES (1)";
17671
mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
17672
mysql_stmt_execute(stmt);
17673
mysql_stmt_close(stmt);
17678
static void test_bug40365(void)
17681
MYSQL_STMT *stmt= 0;
17682
MYSQL_BIND my_bind[2];
17683
my_bool is_null[2]= {0};
17686
DBUG_ENTER("test_bug40365");
17688
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
17690
rc= mysql_query(mysql, "CREATE TABLE t1(c1 DATETIME, \
17694
stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 VALUES(?, ?)");
17696
verify_param_count(stmt, 2);
17698
bzero((char*) my_bind, sizeof(my_bind));
17699
my_bind[0].buffer_type= MYSQL_TYPE_DATETIME;
17700
my_bind[1].buffer_type= MYSQL_TYPE_DATE;
17701
for (i= 0; i < (int) array_elements(my_bind); i++)
17703
my_bind[i].buffer= (void *) &tm[i];
17704
my_bind[i].is_null= &is_null[i];
17707
rc= mysql_stmt_bind_param(stmt, my_bind);
17708
check_execute(stmt, rc);
17710
for (i= 0; i < (int) array_elements(my_bind); i++)
17713
tm[i].second_part= 0;
17721
rc= mysql_stmt_execute(stmt);
17722
check_execute(stmt, rc);
17724
rc= mysql_commit(mysql);
17726
mysql_stmt_close(stmt);
17728
stmt= mysql_simple_prepare(mysql, "SELECT * FROM t1");
17731
rc= mysql_stmt_bind_result(stmt, my_bind);
17732
check_execute(stmt, rc);
17734
rc= mysql_stmt_execute(stmt);
17735
check_execute(stmt, rc);
17737
rc= mysql_stmt_store_result(stmt);
17738
check_execute(stmt, rc);
17740
rc= mysql_stmt_fetch(stmt);
17741
check_execute(stmt, rc);
17744
fprintf(stdout, "\n");
17746
for (i= 0; i < array_elements(my_bind); i++)
17749
fprintf(stdout, "\ntime[%d]: %02d-%02d-%02d ",
17750
i, tm[i].year, tm[i].month, tm[i].day);
17751
DIE_UNLESS(tm[i].year == 0);
17752
DIE_UNLESS(tm[i].month == 0);
17753
DIE_UNLESS(tm[i].day == 0);
17755
mysql_stmt_close(stmt);
17762
Subtest for Bug#43560. Verifies that a loss of connection on the server side
17763
is handled well by the mysql_stmt_execute() call, i.e., no SIGSEGV due to
17764
a vio socket that is cleared upon closed connection.
17766
Assumes the presence of the close_conn_after_stmt_execute debug feature in
17767
the server. Verifies that it is connected to a debug server before proceeding
17770
static void test_bug43560(void)
17774
MYSQL_STMT *stmt= 0;
17776
my_bool is_null= 0;
17778
const uint BUFSIZE= sizeof(buffer);
17779
const char* values[] = {"eins", "zwei", "drei", "viele", NULL};
17780
const char insert_str[] = "INSERT INTO t1 (c2) VALUES (?)";
17781
unsigned long length;
17783
DBUG_ENTER("test_bug43560");
17784
myheader("test_bug43560");
17786
/* Make sure we only run against a debug server. */
17787
if (!strstr(mysql->server_version, "debug"))
17789
fprintf(stdout, "Skipping test_bug43560: server not DEBUG version\n");
17794
Set up a separate connection for this test to avoid messing up the
17795
general MYSQL object used in other subtests. Use TCP protocol to avoid
17796
problems with the buffer semantics of AF_UNIX, and turn off auto reconnect.
17798
conn= client_connect(0, MYSQL_PROTOCOL_TCP, 0);
17800
rc= mysql_query(conn, "DROP TABLE IF EXISTS t1");
17802
rc= mysql_query(conn,
17803
"CREATE TABLE t1 (c1 INT PRIMARY KEY AUTO_INCREMENT, c2 CHAR(10))");
17806
stmt= mysql_stmt_init(conn);
17808
rc= mysql_stmt_prepare(stmt, insert_str, strlen(insert_str));
17809
check_execute(stmt, rc);
17811
bind.buffer_type= MYSQL_TYPE_STRING;
17812
bind.buffer_length= BUFSIZE;
17813
bind.buffer= buffer;
17814
bind.is_null= &is_null;
17815
bind.length= &length;
17816
rc= mysql_stmt_bind_param(stmt, &bind);
17817
check_execute(stmt, rc);
17819
/* First execute; should succeed. */
17820
strncpy(buffer, values[0], BUFSIZE);
17821
length= strlen(buffer);
17822
rc= mysql_stmt_execute(stmt);
17823
check_execute(stmt, rc);
17826
Set up the server to close this session's server-side socket after
17827
next execution of prep statement.
17829
rc= mysql_query(conn,"SET SESSION debug='+d,close_conn_after_stmt_execute'");
17832
/* Second execute; should fail due to socket closed during execution. */
17833
strncpy(buffer, values[1], BUFSIZE);
17834
length= strlen(buffer);
17835
rc= mysql_stmt_execute(stmt);
17836
DIE_UNLESS(rc && mysql_stmt_errno(stmt) == CR_SERVER_LOST);
17839
Third execute; should fail (connection already closed), or SIGSEGV in
17840
case of a Bug#43560 type regression in which case the whole test fails.
17842
strncpy(buffer, values[2], BUFSIZE);
17843
length= strlen(buffer);
17844
rc= mysql_stmt_execute(stmt);
17845
DIE_UNLESS(rc && mysql_stmt_errno(stmt) == CR_SERVER_LOST);
17847
client_disconnect(conn, 0);
17848
rc= mysql_query(mysql, "DROP TABLE t1");
17856
Bug#36326: nested transaction and select
17859
#ifdef HAVE_QUERY_CACHE
17861
static void test_bug36326()
17865
DBUG_ENTER("test_bug36326");
17866
myheader("test_bug36326");
17868
rc= mysql_autocommit(mysql, TRUE);
17870
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
17872
rc= mysql_query(mysql, "CREATE TABLE t1 (a INTEGER)");
17874
rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
17876
rc= mysql_query(mysql, "SET GLOBAL query_cache_type = 1");
17878
rc= mysql_query(mysql, "SET GLOBAL query_cache_size = 1048576");
17880
DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
17881
DIE_UNLESS(mysql->server_status & SERVER_STATUS_AUTOCOMMIT);
17882
rc= mysql_query(mysql, "BEGIN");
17884
DIE_UNLESS(mysql->server_status & SERVER_STATUS_IN_TRANS);
17885
rc= mysql_query(mysql, "SELECT * FROM t1");
17887
rc= my_process_result(mysql);
17888
DIE_UNLESS(rc == 1);
17889
rc= mysql_rollback(mysql);
17891
rc= mysql_query(mysql, "ROLLBACK");
17893
DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
17894
rc= mysql_query(mysql, "SELECT * FROM t1");
17896
DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
17897
rc= my_process_result(mysql);
17898
DIE_UNLESS(rc == 1);
17899
rc= mysql_query(mysql, "DROP TABLE t1");
17901
rc= mysql_query(mysql, "SET GLOBAL query_cache_size = 0");
17910
Bug#41078: With CURSOR_TYPE_READ_ONLY mysql_stmt_fetch() returns short
17914
static void test_bug41078(void)
17917
MYSQL_STMT *stmt= 0;
17918
MYSQL_BIND param, result;
17919
ulong cursor_type= CURSOR_TYPE_READ_ONLY;
17922
const char param_str[]= "abcdefghijklmn";
17923
my_bool is_null, error;
17925
DBUG_ENTER("test_bug41078");
17927
rc= mysql_query(mysql, "SET NAMES UTF8");
17930
stmt= mysql_simple_prepare(mysql, "SELECT ?");
17932
verify_param_count(stmt, 1);
17934
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, &cursor_type);
17935
check_execute(stmt, rc);
17937
bzero(¶m, sizeof(param));
17938
param.buffer_type= MYSQL_TYPE_STRING;
17939
param.buffer= (void *) param_str;
17940
len= sizeof(param_str) - 1;
17941
param.length= &len;
17943
rc= mysql_stmt_bind_param(stmt, ¶m);
17944
check_execute(stmt, rc);
17946
rc= mysql_stmt_execute(stmt);
17947
check_execute(stmt, rc);
17949
bzero(&result, sizeof(result));
17950
result.buffer_type= MYSQL_TYPE_STRING;
17951
result.buffer= str;
17952
result.buffer_length= sizeof(str);
17953
result.is_null= &is_null;
17954
result.length= &len;
17955
result.error= &error;
17957
rc= mysql_stmt_bind_result(stmt, &result);
17958
check_execute(stmt, rc);
17960
rc= mysql_stmt_store_result(stmt);
17961
check_execute(stmt, rc);
17963
rc= mysql_stmt_fetch(stmt);
17964
check_execute(stmt, rc);
17966
DIE_UNLESS(len == sizeof(param_str) - 1 && !strcmp(str, param_str));
17968
mysql_stmt_close(stmt);
17974
Bug#45010: invalid memory reads during parsing some strange statements
17976
static void test_bug45010()
17979
const char query1[]= "select a.\x80",
17980
query2[]= "describe `table\xef";
17982
DBUG_ENTER("test_bug45010");
17983
myheader("test_bug45010");
17985
rc= mysql_query(mysql, "set names utf8");
17988
/* \x80 (-128) could be used as a index of ident_map. */
17989
rc= mysql_real_query(mysql, query1, sizeof(query1) - 1);
17992
/* \xef (-17) could be used to skip 3 bytes past the buffer end. */
17993
rc= mysql_real_query(mysql, query2, sizeof(query2) - 1);
17996
rc= mysql_query(mysql, "set names default");
18003
Bug#44495: Prepared Statement:
18004
CALL p(<x>) - `thd->protocol == &thd->protocol_text' failed
18007
static void test_bug44495()
18013
DBUG_ENTER("test_bug44495");
18014
myheader("test_44495");
18016
rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
18019
rc= mysql_query(mysql, "CREATE PROCEDURE p1(IN arg VARCHAR(25))"
18020
" BEGIN SET @stmt = CONCAT('SELECT \"', arg, '\"');"
18021
" PREPARE ps1 FROM @stmt;"
18023
" DROP PREPARE ps1;"
18027
DIE_UNLESS(mysql_client_init(&con));
18029
DIE_UNLESS(mysql_real_connect(&con, opt_host, opt_user, opt_password,
18030
current_db, opt_port, opt_unix_socket,
18031
CLIENT_MULTI_RESULTS));
18033
stmt= mysql_simple_prepare(&con, "CALL p1('abc')");
18036
rc= mysql_stmt_execute(stmt);
18037
check_execute(stmt, rc);
18039
rc= my_process_stmt_result(stmt);
18040
DIE_UNLESS(rc == 1);
18042
mysql_stmt_close(stmt);
18046
rc= mysql_query(mysql, "DROP PROCEDURE p1");
18053
Read and parse arguments and MySQL options from my.cnf
18056
static const char *client_test_load_default_groups[]= { "client", 0 };
18057
static char **defaults_argv;
18059
static struct my_option client_test_long_options[] =
18061
{"basedir", 'b', "Basedir for tests.", (uchar**) &opt_basedir,
18062
(uchar**) &opt_basedir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
18063
{"count", 't', "Number of times test to be executed", (uchar **) &opt_count,
18064
(uchar **) &opt_count, 0, GET_UINT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0},
18065
{"database", 'D', "Database to use", (uchar **) &opt_db, (uchar **) &opt_db,
18066
0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
18067
{"debug", '#', "Output debug log", (uchar**) &default_dbug_option,
18068
(uchar**) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
18069
{"help", '?', "Display this help and exit", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
18071
{"host", 'h', "Connect to host", (uchar **) &opt_host, (uchar **) &opt_host,
18072
0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
18074
"Password to use when connecting to server. If password is not given it's asked from the tty.",
18075
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
18076
{"port", 'P', "Port number to use for connection or 0 for default to, in "
18077
"order of preference, my.cnf, $MYSQL_TCP_PORT, "
18078
#if MYSQL_PORT_DEFAULT == 0
18081
"built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
18082
(uchar **) &opt_port,
18083
(uchar **) &opt_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
18084
{"server-arg", 'A', "Send embedded server this as a parameter.",
18085
0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
18086
{"show-tests", 'T', "Show all tests' names", 0, 0, 0, GET_NO_ARG, NO_ARG,
18088
{"silent", 's', "Be more silent", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0,
18091
{"shared-memory-base-name", 'm', "Base name of shared memory.",
18092
(uchar**) &shared_memory_base_name, (uchar**)&shared_memory_base_name, 0,
18093
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
18095
{"socket", 'S', "Socket file to use for connection",
18096
(uchar **) &opt_unix_socket, (uchar **) &opt_unix_socket, 0, GET_STR,
18097
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
18099
"May disable some code when runs as mysql-test-run testcase.",
18100
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
18101
#ifndef DONT_ALLOW_USER_CHANGE
18102
{"user", 'u', "User for login if not current user", (uchar **) &opt_user,
18103
(uchar **) &opt_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
18105
{"vardir", 'v', "Data dir for tests.", (uchar**) &opt_vardir,
18106
(uchar**) &opt_vardir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
18107
{"getopt-ll-test", 'g', "Option for testing bug in getopt library",
18108
(uchar **) &opt_getopt_ll_test, (uchar **) &opt_getopt_ll_test, 0,
18109
GET_LL, REQUIRED_ARG, 0, 0, LONGLONG_MAX, 0, 0, 0},
18110
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
18114
static void usage(void)
18116
/* show the usage string when the user asks for this */
18117
putc('\n', stdout);
18118
printf("%s Ver %s Distrib %s, for %s (%s)\n",
18119
my_progname, VER, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
18120
puts("By Monty, Venu, Kent and others\n");
18122
Copyright (C) 2002-2004 MySQL AB\n\
18123
This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
18124
and you are welcome to modify and redistribute it under the GPL license\n");
18125
printf("Usage: %s [OPTIONS] [TESTNAME1 TESTNAME2...]\n", my_progname);
18126
my_print_help(client_test_long_options);
18127
print_defaults("my", client_test_load_default_groups);
18128
my_print_variables(client_test_long_options);
18132
static struct my_tests_st my_tests[]= {
18133
{ "disable_general_log", disable_general_log },
18134
{ "test_view_sp_list_fields", test_view_sp_list_fields },
18135
{ "client_query", client_query },
18136
{ "test_prepare_insert_update", test_prepare_insert_update},
18137
#if NOT_YET_WORKING
18138
{ "test_drop_temp", test_drop_temp },
18140
{ "test_fetch_seek", test_fetch_seek },
18141
{ "test_fetch_nobuffs", test_fetch_nobuffs },
18142
{ "test_open_direct", test_open_direct },
18143
{ "test_fetch_null", test_fetch_null },
18144
{ "test_ps_null_param", test_ps_null_param },
18145
{ "test_fetch_date", test_fetch_date },
18146
{ "test_fetch_str", test_fetch_str },
18147
{ "test_fetch_long", test_fetch_long },
18148
{ "test_fetch_short", test_fetch_short },
18149
{ "test_fetch_tiny", test_fetch_tiny },
18150
{ "test_fetch_bigint", test_fetch_bigint },
18151
{ "test_fetch_float", test_fetch_float },
18152
{ "test_fetch_double", test_fetch_double },
18153
{ "test_bind_result_ext", test_bind_result_ext },
18154
{ "test_bind_result_ext1", test_bind_result_ext1 },
18155
{ "test_select_direct", test_select_direct },
18156
{ "test_select_prepare", test_select_prepare },
18157
{ "test_select", test_select },
18158
{ "test_select_version", test_select_version },
18159
{ "test_ps_conj_select", test_ps_conj_select },
18160
{ "test_select_show_table", test_select_show_table },
18161
{ "test_func_fields", test_func_fields },
18162
{ "test_long_data", test_long_data },
18163
{ "test_insert", test_insert },
18164
{ "test_set_variable", test_set_variable },
18165
{ "test_select_show", test_select_show },
18166
{ "test_prepare_noparam", test_prepare_noparam },
18167
{ "test_bind_result", test_bind_result },
18168
{ "test_prepare_simple", test_prepare_simple },
18169
{ "test_prepare", test_prepare },
18170
{ "test_null", test_null },
18171
{ "test_debug_example", test_debug_example },
18172
{ "test_update", test_update },
18173
{ "test_simple_update", test_simple_update },
18174
{ "test_simple_delete", test_simple_delete },
18175
{ "test_double_compare", test_double_compare },
18176
{ "client_store_result", client_store_result },
18177
{ "client_use_result", client_use_result },
18178
{ "test_tran_bdb", test_tran_bdb },
18179
{ "test_tran_innodb", test_tran_innodb },
18180
{ "test_prepare_ext", test_prepare_ext },
18181
{ "test_prepare_syntax", test_prepare_syntax },
18182
{ "test_field_names", test_field_names },
18183
{ "test_field_flags", test_field_flags },
18184
{ "test_long_data_str", test_long_data_str },
18185
{ "test_long_data_str1", test_long_data_str1 },
18186
{ "test_long_data_bin", test_long_data_bin },
18187
{ "test_warnings", test_warnings },
18188
{ "test_errors", test_errors },
18189
{ "test_prepare_resultset", test_prepare_resultset },
18190
{ "test_stmt_close", test_stmt_close },
18191
{ "test_prepare_field_result", test_prepare_field_result },
18192
{ "test_multi_stmt", test_multi_stmt },
18193
{ "test_multi_statements", test_multi_statements },
18194
{ "test_prepare_multi_statements", test_prepare_multi_statements },
18195
{ "test_store_result", test_store_result },
18196
{ "test_store_result1", test_store_result1 },
18197
{ "test_store_result2", test_store_result2 },
18198
{ "test_subselect", test_subselect },
18199
{ "test_date", test_date },
18200
{ "test_date_date", test_date_date },
18201
{ "test_date_time", test_date_time },
18202
{ "test_date_ts", test_date_ts },
18203
{ "test_date_dt", test_date_dt },
18204
{ "test_prepare_alter", test_prepare_alter },
18205
{ "test_manual_sample", test_manual_sample },
18206
{ "test_pure_coverage", test_pure_coverage },
18207
{ "test_buffers", test_buffers },
18208
{ "test_ushort_bug", test_ushort_bug },
18209
{ "test_sshort_bug", test_sshort_bug },
18210
{ "test_stiny_bug", test_stiny_bug },
18211
{ "test_field_misc", test_field_misc },
18212
{ "test_set_option", test_set_option },
18213
#ifndef EMBEDDED_LIBRARY
18214
{ "test_prepare_grant", test_prepare_grant },
18216
{ "test_frm_bug", test_frm_bug },
18217
{ "test_explain_bug", test_explain_bug },
18218
{ "test_decimal_bug", test_decimal_bug },
18219
{ "test_nstmts", test_nstmts },
18220
{ "test_logs;", test_logs },
18221
{ "test_cuted_rows", test_cuted_rows },
18222
{ "test_fetch_offset", test_fetch_offset },
18223
{ "test_fetch_column", test_fetch_column },
18224
{ "test_mem_overun", test_mem_overun },
18225
{ "test_list_fields", test_list_fields },
18226
{ "test_free_result", test_free_result },
18227
{ "test_free_store_result", test_free_store_result },
18228
{ "test_sqlmode", test_sqlmode },
18229
{ "test_ts", test_ts },
18230
{ "test_bug1115", test_bug1115 },
18231
{ "test_bug1180", test_bug1180 },
18232
{ "test_bug1500", test_bug1500 },
18233
{ "test_bug1644", test_bug1644 },
18234
{ "test_bug1946", test_bug1946 },
18235
{ "test_bug2248", test_bug2248 },
18236
{ "test_parse_error_and_bad_length", test_parse_error_and_bad_length },
18237
{ "test_bug2247", test_bug2247 },
18238
{ "test_subqueries", test_subqueries },
18239
{ "test_bad_union", test_bad_union },
18240
{ "test_distinct", test_distinct },
18241
{ "test_subqueries_ref", test_subqueries_ref },
18242
{ "test_union", test_union },
18243
{ "test_bug3117", test_bug3117 },
18244
{ "test_join", test_join },
18245
{ "test_selecttmp", test_selecttmp },
18246
{ "test_create_drop", test_create_drop },
18247
{ "test_rename", test_rename },
18248
{ "test_do_set", test_do_set },
18249
{ "test_multi", test_multi },
18250
{ "test_insert_select", test_insert_select },
18251
{ "test_bind_nagative", test_bind_nagative },
18252
{ "test_derived", test_derived },
18253
{ "test_xjoin", test_xjoin },
18254
{ "test_bug3035", test_bug3035 },
18255
{ "test_union2", test_union2 },
18256
{ "test_bug1664", test_bug1664 },
18257
{ "test_union_param", test_union_param },
18258
{ "test_order_param", test_order_param },
18259
{ "test_ps_i18n", test_ps_i18n },
18260
{ "test_bug3796", test_bug3796 },
18261
{ "test_bug4026", test_bug4026 },
18262
{ "test_bug4079", test_bug4079 },
18263
{ "test_bug4236", test_bug4236 },
18264
{ "test_bug4030", test_bug4030 },
18265
{ "test_bug5126", test_bug5126 },
18266
{ "test_bug4231", test_bug4231 },
18267
{ "test_bug5399", test_bug5399 },
18268
{ "test_bug5194", test_bug5194 },
18269
{ "test_bug5315", test_bug5315 },
18270
{ "test_bug6049", test_bug6049 },
18271
{ "test_bug6058", test_bug6058 },
18272
{ "test_bug6059", test_bug6059 },
18273
{ "test_bug6046", test_bug6046 },
18274
{ "test_bug6081", test_bug6081 },
18275
{ "test_bug6096", test_bug6096 },
18276
{ "test_datetime_ranges", test_datetime_ranges },
18277
{ "test_bug4172", test_bug4172 },
18278
{ "test_conversion", test_conversion },
18279
{ "test_rewind", test_rewind },
18280
{ "test_bug6761", test_bug6761 },
18281
{ "test_view", test_view },
18282
{ "test_view_where", test_view_where },
18283
{ "test_view_2where", test_view_2where },
18284
{ "test_view_star", test_view_star },
18285
{ "test_view_insert", test_view_insert },
18286
{ "test_left_join_view", test_left_join_view },
18287
{ "test_view_insert_fields", test_view_insert_fields },
18288
{ "test_basic_cursors", test_basic_cursors },
18289
{ "test_cursors_with_union", test_cursors_with_union },
18290
{ "test_cursors_with_procedure", test_cursors_with_procedure },
18291
{ "test_truncation", test_truncation },
18292
{ "test_truncation_option", test_truncation_option },
18293
{ "test_client_character_set", test_client_character_set },
18294
{ "test_bug8330", test_bug8330 },
18295
{ "test_bug7990", test_bug7990 },
18296
{ "test_bug8378", test_bug8378 },
18297
{ "test_bug8722", test_bug8722 },
18298
{ "test_bug8880", test_bug8880 },
18299
{ "test_bug9159", test_bug9159 },
18300
{ "test_bug9520", test_bug9520 },
18301
{ "test_bug9478", test_bug9478 },
18302
{ "test_bug9643", test_bug9643 },
18303
{ "test_bug10729", test_bug10729 },
18304
{ "test_bug11111", test_bug11111 },
18305
{ "test_bug9992", test_bug9992 },
18306
{ "test_bug10736", test_bug10736 },
18307
{ "test_bug10794", test_bug10794 },
18308
{ "test_bug11172", test_bug11172 },
18309
{ "test_bug11656", test_bug11656 },
18310
{ "test_bug10214", test_bug10214 },
18311
{ "test_bug9735", test_bug9735 },
18312
{ "test_bug11183", test_bug11183 },
18313
{ "test_bug11037", test_bug11037 },
18314
{ "test_bug10760", test_bug10760 },
18315
{ "test_bug12001", test_bug12001 },
18316
{ "test_bug11718", test_bug11718 },
18317
{ "test_bug12925", test_bug12925 },
18318
{ "test_bug11909", test_bug11909 },
18319
{ "test_bug11901", test_bug11901 },
18320
{ "test_bug11904", test_bug11904 },
18321
{ "test_bug12243", test_bug12243 },
18322
{ "test_bug14210", test_bug14210 },
18323
{ "test_bug13488", test_bug13488 },
18324
{ "test_bug13524", test_bug13524 },
18325
{ "test_bug14845", test_bug14845 },
18326
{ "test_opt_reconnect", test_opt_reconnect },
18327
{ "test_bug15510", test_bug15510},
18328
#ifndef EMBEDDED_LIBRARY
18329
{ "test_bug12744", test_bug12744 },
18331
{ "test_bug16143", test_bug16143 },
18332
{ "test_bug16144", test_bug16144 },
18333
{ "test_bug15613", test_bug15613 },
18334
{ "test_bug20152", test_bug20152 },
18335
{ "test_bug14169", test_bug14169 },
18336
{ "test_bug17667", test_bug17667 },
18337
{ "test_bug15752", test_bug15752 },
18338
{ "test_mysql_insert_id", test_mysql_insert_id },
18339
{ "test_bug19671", test_bug19671 },
18340
{ "test_bug21206", test_bug21206 },
18341
{ "test_bug21726", test_bug21726 },
18342
{ "test_bug15518", test_bug15518 },
18343
{ "test_bug23383", test_bug23383 },
18344
{ "test_bug32265", test_bug32265 },
18345
{ "test_bug21635", test_bug21635 },
18346
{ "test_status", test_status },
18347
{ "test_bug24179", test_bug24179 },
18348
{ "test_ps_query_cache", test_ps_query_cache },
18349
{ "test_bug28075", test_bug28075 },
18350
{ "test_bug27876", test_bug27876 },
18351
{ "test_bug28505", test_bug28505 },
18352
{ "test_bug28934", test_bug28934 },
18353
{ "test_bug27592", test_bug27592 },
18354
{ "test_bug29687", test_bug29687 },
18355
{ "test_bug29692", test_bug29692 },
18356
{ "test_bug29306", test_bug29306 },
18357
{ "test_change_user", test_change_user },
18358
{ "test_bug30472", test_bug30472 },
18359
{ "test_bug20023", test_bug20023 },
18360
{ "test_bug45010", test_bug45010 },
18361
{ "test_bug31418", test_bug31418 },
18362
{ "test_bug31669", test_bug31669 },
18363
{ "test_bug28386", test_bug28386 },
18364
{ "test_wl4166_1", test_wl4166_1 },
18365
{ "test_wl4166_2", test_wl4166_2 },
18366
{ "test_bug38486", test_bug38486 },
18367
{ "test_bug40365", test_bug40365 },
18368
{ "test_bug43560", test_bug43560 },
18369
#ifdef HAVE_QUERY_CACHE
18370
{ "test_bug36326", test_bug36326 },
18372
{ "test_bug41078", test_bug41078 },
18373
{ "test_bug44495", test_bug44495 },
18379
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
18384
DBUG_PUSH(argument ? argument : default_dbug_option);
18392
char *start=argument;
18393
my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
18394
opt_password= my_strdup(argument, MYF(MY_FAE));
18395
while (*argument) *argument++= 'x'; /* Destroy argument */
18403
if (argument == disabled_my_option)
18410
When the embedded server is being tested, the test suite needs to be
18411
able to pass command-line arguments to the embedded server so it can
18412
locate the language files and data directory. The test suite
18413
(mysql-test-run) never uses config files, just command-line options.
18415
if (!embedded_server_arg_count)
18417
embedded_server_arg_count= 1;
18418
embedded_server_args[0]= (char*) "";
18420
if (embedded_server_arg_count == MAX_SERVER_ARGS-1 ||
18421
!(embedded_server_args[embedded_server_arg_count++]=
18422
my_strdup(argument, MYF(MY_FAE))))
18424
DIE("Can't use server argument");
18429
struct my_tests_st *fptr;
18431
printf("All possible test names:\n\n");
18432
for (fptr= my_tests; fptr->name; fptr++)
18433
printf("%s\n", fptr->name);
18438
case 'I': /* Info */
18446
static void get_options(int *argc, char ***argv)
18450
if ((ho_error= handle_options(argc, argv, client_test_long_options,
18455
opt_password= get_tty_password(NullS);
18460
Print the test output on successful execution before exiting
18463
static void print_test_output()
18465
if (opt_silent < 3)
18467
fprintf(stdout, "\n\n");
18468
fprintf(stdout, "All '%d' tests were successful (in '%d' iterations)",
18469
test_count-1, opt_count);
18470
fprintf(stdout, "\n Total execution time: %g SECS", total_time);
18472
fprintf(stdout, " (Avg: %g SECS)", total_time/opt_count);
18474
fprintf(stdout, "\n\n!!! SUCCESS !!!\n");
18478
/***************************************************************************
18480
***************************************************************************/
18483
int main(int argc, char **argv)
18485
struct my_tests_st *fptr;
18489
load_defaults("my", client_test_load_default_groups, &argc, &argv);
18490
defaults_argv= argv;
18491
get_options(&argc, &argv);
18493
if (mysql_server_init(embedded_server_arg_count,
18494
embedded_server_args,
18495
(char**) embedded_server_groups))
18496
DIE("Can't initialize MySQL server");
18498
/* connect to server with no flags, default protocol, auto reconnect true */
18499
mysql= client_connect(0, MYSQL_PROTOCOL_DEFAULT, 1);
18502
for (iter_count= 1; iter_count <= opt_count; iter_count++)
18504
/* Start of tests */
18506
start_time= time((time_t *)0);
18509
for (fptr= my_tests; fptr->name; fptr++)
18510
(*fptr->function)();
18514
for ( ; *argv ; argv++)
18516
for (fptr= my_tests; fptr->name; fptr++)
18518
if (!strcmp(fptr->name, *argv))
18520
(*fptr->function)();
18526
fprintf(stderr, "\n\nGiven test not found: '%s'\n", *argv);
18527
fprintf(stderr, "See legal test names with %s -T\n\nAborting!\n",
18529
client_disconnect(mysql, 1);
18530
free_defaults(defaults_argv);
18536
end_time= time((time_t *)0);
18537
total_time+= difftime(end_time, start_time);
18542
client_disconnect(mysql, 1); /* disconnect from server */
18544
free_defaults(defaults_argv);
18545
print_test_output();
18547
while (embedded_server_arg_count > 1)
18548
my_free(embedded_server_args[--embedded_server_arg_count],MYF(0));
18550
mysql_server_end();