16 #include <my_global.h>
18 #include "my_default.h"
21 #include <my_getopt.h>
23 #include <mysqld_error.h>
24 #include <sql_common.h>
28 #define MAX_TEST_QUERY_LENGTH 300
29 #define MAX_KEY MAX_INDEXES
30 #define MAX_SERVER_ARGS 64
33 static int opt_testcase = 0;
34 static char *opt_db= 0;
35 static char *opt_user= 0;
36 static char *opt_password= 0;
37 static char *opt_host= 0;
38 static char *opt_unix_socket= 0;
40 static char *shared_memory_base_name= 0;
42 static unsigned int opt_port;
43 static my_bool tty_password= 0, opt_silent= 0;
45 static MYSQL *mysql= 0;
46 static char current_db[]=
"client_test_db";
47 static unsigned int test_count= 0;
48 static unsigned int opt_count= 0;
49 static unsigned int opt_count_read= 0;
50 static unsigned int iter_count= 0;
51 static my_bool have_innodb= FALSE;
52 static char *opt_plugin_dir= 0, *opt_default_auth= 0;
53 static unsigned int opt_drop_db= 1;
55 static const char *opt_basedir=
"./";
56 static const char *opt_vardir=
"mysql-test/var";
58 static longlong opt_getopt_ll_test= 0;
60 static char **defaults_argv;
61 static int original_argc;
62 static char **original_argv;
63 static int embedded_server_arg_count= 0;
64 static char *embedded_server_args[MAX_SERVER_ARGS];
66 static const char *embedded_server_groups[]= {
69 "mysql_client_test_SERVER",
73 static time_t start_time, end_time;
74 static double total_time;
76 const char *default_dbug_option=
"d:t:o,/tmp/mysql_client_test.trace";
81 static const char *client_test_load_default_groups[]= {
"client", 0 };
89 #define myheader(str) \
90 DBUG_PRINT("test", ("name: %s", str)); \
93 fprintf(stdout, "\n\n#####################################\n"); \
94 fprintf(stdout, "%u of (%u/%u): %s", test_count++, iter_count, \
96 fprintf(stdout, " \n#####################################\n"); \
99 #define myheader_r(str) \
100 DBUG_PRINT("test", ("name: %s", str)); \
103 fprintf(stdout, "\n\n#####################################\n"); \
104 fprintf(stdout, "%s", str); \
105 fprintf(stdout, " \n#####################################\n"); \
108 static void print_error(
MYSQL * l_mysql,
const char *
msg);
109 static void print_st_error(
MYSQL_STMT *stmt,
const char *
msg);
110 static void client_disconnect(
MYSQL* mysql);
111 static void get_options(
int *argc,
char ***argv);
124 #define DIE_UNLESS(expr) \
125 ((void) ((expr) ? 0 : (die(__FILE__, __LINE__, #expr), 0)))
126 #define DIE_IF(expr) \
127 ((void) ((expr) ? (die(__FILE__, __LINE__, #expr), 0) : 0))
129 die(__FILE__, __LINE__, #expr)
131 static void die(
const char *
file,
int line,
const char *expr)
134 fprintf(stderr,
"%s:%d: check failed: '%s'\n", file, line, expr);
140 #define myerror(msg) print_error(mysql,msg)
141 #define myerror2(l_mysql, msg) print_error(l_mysql,msg)
142 #define mysterror(stmt, msg) print_st_error(stmt, msg)
144 #define myquery(RES) \
149 DIE_UNLESS(r == 0); \
152 #define myquery2(L_MYSQL,RES) \
156 myerror2(L_MYSQL,NULL); \
157 DIE_UNLESS(r == 0); \
160 #define myquery_r(r) \
164 DIE_UNLESS(r != 0); \
167 #define check_execute(stmt, r) \
170 mysterror(stmt, NULL); \
171 DIE_UNLESS(r == 0); \
174 #define check_execute_r(stmt, r) \
177 mysterror(stmt, NULL); \
178 DIE_UNLESS(r != 0); \
181 #define check_stmt(stmt) \
185 DIE_UNLESS(stmt != 0); \
188 #define check_stmt_r(stmt) \
192 DIE_UNLESS(stmt == 0); \
195 #define mytest(x) if (!(x)) {myerror(NULL);DIE_UNLESS(FALSE);}
196 #define mytest_r(x) if ((x)) {myerror(NULL);DIE_UNLESS(FALSE);}
201 static int cmp_double(
double *a,
double *b)
209 static void print_error(
MYSQL *l_mysql,
const char *
msg)
213 if (l_mysql && mysql_errno(l_mysql))
215 if (l_mysql->server_version)
216 fprintf(stdout,
"\n [MySQL-%s]", l_mysql->server_version);
218 fprintf(stdout,
"\n [MySQL]");
219 fprintf(stdout,
"[%d] %s\n", mysql_errno(l_mysql), mysql_error(l_mysql));
222 fprintf(stderr,
" [MySQL] %s\n", msg);
227 static void print_st_error(
MYSQL_STMT *stmt,
const char *msg)
231 if (stmt && mysql_stmt_errno(stmt))
233 if (stmt->mysql && stmt->mysql->server_version)
234 fprintf(stdout,
"\n [MySQL-%s]", stmt->mysql->server_version);
236 fprintf(stdout,
"\n [MySQL]");
238 fprintf(stdout,
"[%d] %s\n", mysql_stmt_errno(stmt),
239 mysql_stmt_error(stmt));
242 fprintf(stderr,
" [MySQL] %s\n", msg);
252 MYSQL* res = mysql_init(con);
254 if (res && shared_memory_base_name)
255 mysql_options(res, MYSQL_SHARED_MEMORY_BASE_NAME, shared_memory_base_name);
257 if (opt_plugin_dir && *opt_plugin_dir)
258 mysql_options(res, MYSQL_PLUGIN_DIR, opt_plugin_dir);
260 if (opt_default_auth && *opt_default_auth)
261 mysql_options(res, MYSQL_DEFAULT_AUTH, opt_default_auth);
268 #define mysql_init(A) Please use mysql_client_init instead of mysql_init
273 static my_bool check_have_innodb(
MYSQL *
conn)
278 my_bool result= FALSE;
280 rc= mysql_query(conn,
281 "SELECT (support = 'YES' or support = 'DEFAULT' or support = 'ENABLED') "
282 "AS `TRUE` FROM information_schema.engines WHERE engine = 'innodb'");
284 res= mysql_use_result(conn);
287 row= mysql_fetch_row(res);
290 if (row[0] && row[1])
291 result= strcmp(row[1],
"1") == 0;
292 mysql_free_result(res);
303 mysql_simple_prepare(
MYSQL *mysql_arg,
const char *
query)
306 if (stmt && mysql_stmt_prepare(stmt, query, (uint) strlen(query)))
308 mysql_stmt_close(stmt);
326 static MYSQL* client_connect(ulong flag, uint protocol, my_bool auto_reconnect)
330 static char query[MAX_TEST_QUERY_LENGTH];
331 myheader_r(
"client_connect");
334 fprintf(stdout,
"\n Establishing a connection to '%s' ...",
335 opt_host ? opt_host :
"");
337 if (!(mysql= mysql_client_init(NULL)))
340 myerror(
"mysql_client_init() failed");
344 mysql_options(mysql, MYSQL_OPT_LOCAL_INFILE, 0);
345 mysql_options(mysql, MYSQL_OPT_PROTOCOL, &protocol);
346 if (opt_plugin_dir && *opt_plugin_dir)
347 mysql_options(mysql, MYSQL_PLUGIN_DIR, opt_plugin_dir);
349 if (opt_default_auth && *opt_default_auth)
350 mysql_options(mysql, MYSQL_DEFAULT_AUTH, opt_default_auth);
352 if (!(mysql_real_connect(mysql, opt_host, opt_user,
353 opt_password, opt_db ? opt_db:
"test", opt_port,
354 opt_unix_socket, flag)))
357 myerror(
"connection failed");
359 fprintf(stdout,
"\n Check the connection options using --help or -?\n");
362 mysql->reconnect= auto_reconnect;
365 fprintf(stdout,
"OK");
368 mysql_autocommit(mysql, TRUE);
372 fprintf(stdout,
"\nConnected to MySQL server version: %s (%lu)\n",
373 mysql_get_server_info(mysql),
374 (ulong) mysql_get_server_version(mysql));
375 fprintf(stdout,
"\n Creating a test database '%s' ...", current_db);
377 strxmov(query,
"CREATE DATABASE IF NOT EXISTS ", current_db, NullS);
379 rc= mysql_query(mysql, query);
382 strxmov(query,
"USE ", current_db, NullS);
383 rc= mysql_query(mysql, query);
385 have_innodb= check_have_innodb(mysql);
388 fprintf(stdout,
"OK");
396 static void client_disconnect(
MYSQL* mysql)
398 static char query[MAX_TEST_QUERY_LENGTH];
400 myheader_r(
"client_disconnect");
407 fprintf(stdout,
"\n dropping the test database '%s' ...", current_db);
408 strxmov(query,
"DROP DATABASE IF EXISTS ", current_db, NullS);
410 mysql_query(mysql, query);
412 fprintf(stdout,
"OK");
416 fprintf(stdout,
"\n closing the connection ...");
419 fprintf(stdout,
"OK\n");
426 static void my_print_dashes(
MYSQL_RES *result)
431 mysql_field_seek(result, 0);
435 for(i= 0; i< mysql_num_fields(result); i++)
437 field= mysql_fetch_field(result);
438 for(j= 0; j < field->max_length+2; j++)
448 static void my_print_result_metadata(
MYSQL_RES *result)
452 unsigned int field_count;
454 mysql_field_seek(result, 0);
461 field_count= mysql_num_fields(result);
462 for(i= 0; i< field_count; i++)
464 field= mysql_fetch_field(result);
465 j= strlen(field->name);
466 if (j < field->max_length)
467 j= field->max_length;
468 if (j < 4 && !IS_NOT_NULL(field->flags))
470 field->max_length= j;
474 my_print_dashes(result);
479 mysql_field_seek(result, 0);
480 for(i= 0; i< field_count; i++)
482 field= mysql_fetch_field(result);
484 fprintf(stdout,
" %-*s |", (
int) field->max_length, field->name);
489 my_print_dashes(result);
496 static int my_process_result_set(
MYSQL_RES *result)
501 unsigned int row_count= 0;
506 my_print_result_metadata(result);
508 while ((row= mysql_fetch_row(result)) != NULL)
510 mysql_field_seek(result, 0);
517 for(i= 0; i< mysql_num_fields(result); i++)
519 field= mysql_fetch_field(result);
523 fprintf(stdout,
" %-*s |", (
int) field->max_length,
"NULL");
524 else if (IS_NUM(field->type))
525 fprintf(stdout,
" %*s |", (
int) field->max_length, row[i]);
527 fprintf(stdout,
" %-*s |", (
int) field->max_length, row[i]);
540 my_print_dashes(result);
542 if (mysql_errno(mysql) != 0)
543 fprintf(stderr,
"\n\tmysql_fetch_row() failed\n");
545 fprintf(stdout,
"\n\t%d %s returned\n", row_count,
546 row_count == 1 ?
"row" :
"rows");
552 static int my_process_result(
MYSQL *mysql_arg)
557 if (!(result= mysql_store_result(mysql_arg)))
560 row_count= my_process_result_set(result);
562 mysql_free_result(result);
569 #define MAX_RES_FIELDS 50
570 #define MAX_FIELD_DATA_SIZE 255
572 static int my_process_stmt_result(
MYSQL_STMT *stmt)
579 char data[MAX_RES_FIELDS][MAX_FIELD_DATA_SIZE];
580 ulong length[MAX_RES_FIELDS];
581 my_bool is_null[MAX_RES_FIELDS];
584 if (!(result= mysql_stmt_result_metadata(stmt)))
586 while (!mysql_stmt_fetch(stmt))
591 field_count= MY_MIN(mysql_num_fields(result), MAX_RES_FIELDS);
593 memset(buffer, 0,
sizeof(buffer));
594 memset(length, 0,
sizeof(length));
595 memset(is_null, 0,
sizeof(is_null));
597 for(i= 0; i < field_count; i++)
599 buffer[
i].buffer_type= MYSQL_TYPE_STRING;
600 buffer[
i].buffer_length= MAX_FIELD_DATA_SIZE;
601 buffer[
i].length= &length[
i];
602 buffer[
i].buffer= (
void *) data[i];
603 buffer[
i].is_null= &is_null[
i];
606 rc= mysql_stmt_bind_result(stmt, buffer);
607 check_execute(stmt, rc);
610 mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (
void*)&rc);
611 rc= mysql_stmt_store_result(stmt);
612 check_execute(stmt, rc);
613 my_print_result_metadata(result);
615 mysql_field_seek(result, 0);
616 while ((rc= mysql_stmt_fetch(stmt)) == 0)
623 mysql_field_seek(result, 0);
624 for (i= 0; i < field_count; i++)
626 field= mysql_fetch_field(result);
630 fprintf(stdout,
" %-*s |", (
int) field->max_length,
"NULL");
631 else if (length[i] == 0)
634 fprintf(stdout,
" %*s |", (
int) field->max_length, data[i]);
636 else if (IS_NUM(field->type))
637 fprintf(stdout,
" %*s |", (
int) field->max_length, data[i]);
639 fprintf(stdout,
" %-*s |", (
int) field->max_length, data[i]);
649 DIE_UNLESS(rc == MYSQL_NO_DATA);
653 my_print_dashes(result);
654 fprintf(stdout,
"\n\t%d %s returned\n", row_count,
655 row_count == 1 ?
"row" :
"rows");
657 mysql_free_result(result);
664 int my_stmt_result(
const char *buff)
671 fprintf(stdout,
"\n\n %s", buff);
672 stmt= mysql_simple_prepare(mysql, buff);
675 rc= mysql_stmt_execute(stmt);
676 check_execute(stmt, rc);
678 row_count= my_process_stmt_result(stmt);
679 mysql_stmt_close(stmt);
686 void my_process_warnings(
MYSQL *conn,
unsigned expected_warning_count)
692 fprintf(stdout,
"\n total warnings: %u (expected: %u)\n",
693 mysql_warning_count(conn), expected_warning_count);
695 DIE_UNLESS(mysql_warning_count(mysql) == expected_warning_count);
697 rc= mysql_query(conn,
"SHOW WARNINGS");
700 result= mysql_store_result(conn);
703 rc= my_process_result_set(result);
704 mysql_free_result(result);
710 static void verify_col_data(
const char *
table,
const char *col,
711 const char *exp_data)
713 static char query[MAX_TEST_QUERY_LENGTH];
720 strxmov(query,
"SELECT ", col,
" FROM ", table,
" LIMIT 1", NullS);
722 fprintf(stdout,
"\n %s", query);
723 rc= mysql_query(mysql, query);
729 result= mysql_use_result(mysql);
732 if (!(row= mysql_fetch_row(result)) || !row[field])
734 fprintf(stdout,
"\n *** ERROR: FAILED TO GET THE RESULT ***");
737 if (strcmp(row[field], exp_data))
739 fprintf(stdout,
"\n obtained: `%s` (expected: `%s`)",
740 row[field], exp_data);
743 mysql_free_result(result);
749 #define verify_prepare_field(result,no,name,org_name,type,table, \
750 org_table,db,length,def) \
751 do_verify_prepare_field((result),(no),(name),(org_name),(type), \
752 (table),(org_table),(db),(length),(def), \
755 static void do_verify_prepare_field(
MYSQL_RES *result,
756 unsigned int no,
const char *
name,
757 const char *org_name,
758 enum enum_field_types
type,
760 const char *org_table,
const char *db,
761 unsigned long length,
const char *def,
762 const char *
file,
int line)
766 ulonglong expected_field_length;
768 if (!(field= mysql_fetch_field_direct(result, no)))
770 fprintf(stdout,
"\n *** ERROR: FAILED TO GET THE RESULT ***");
773 cs= get_charset(field->charsetnr, 0);
775 if ((expected_field_length= length * cs->mbmaxlen) > UINT_MAX32)
776 expected_field_length= UINT_MAX32;
779 fprintf(stdout,
"\n field[%d]:", no);
780 fprintf(stdout,
"\n name :`%s`\t(expected: `%s`)", field->name, name);
781 fprintf(stdout,
"\n org_name :`%s`\t(expected: `%s`)",
782 field->org_name, org_name);
783 fprintf(stdout,
"\n type :`%d`\t(expected: `%d`)", field->type, type);
785 fprintf(stdout,
"\n table :`%s`\t(expected: `%s`)",
786 field->table, table);
788 fprintf(stdout,
"\n org_table:`%s`\t(expected: `%s`)",
789 field->org_table, org_table);
790 fprintf(stdout,
"\n database :`%s`\t(expected: `%s`)", field->db, db);
791 fprintf(stdout,
"\n length :`%lu`\t(expected: `%llu`)",
792 field->length, expected_field_length);
793 fprintf(stdout,
"\n maxlength:`%ld`", field->max_length);
794 fprintf(stdout,
"\n charsetnr:`%d`", field->charsetnr);
795 fprintf(stdout,
"\n default :`%s`\t(expected: `%s`)",
796 field->def ? field->def :
"(null)", def ? def:
"(null)");
797 fprintf(stdout,
"\n");
799 DIE_UNLESS(strcmp(field->name, name) == 0);
800 DIE_UNLESS(strcmp(field->org_name, org_name) == 0);
808 if (cs->mbmaxlen == 1)
810 if (field->type != type)
813 "Expected field type: %d, got type: %d in file %s, line %d\n",
814 (
int) type, (
int) field->type, file, line);
815 DIE_UNLESS(field->type == type);
819 DIE_UNLESS(strcmp(field->table, table) == 0);
821 DIE_UNLESS(strcmp(field->org_table, org_table) == 0);
822 DIE_UNLESS(strcmp(field->db, db) == 0);
828 if (length && (field->length != expected_field_length))
830 fprintf(stderr,
"Expected field length: %llu, got length: %lu\n",
831 expected_field_length, field->length);
832 DIE_UNLESS(field->length == expected_field_length);
835 DIE_UNLESS(strcmp(field->def, def) == 0);
841 static void verify_param_count(
MYSQL_STMT *stmt,
long exp_count)
843 long param_count= mysql_stmt_param_count(stmt);
845 fprintf(stdout,
"\n total parameters in stmt: `%ld` (expected: `%ld`)",
846 param_count, exp_count);
847 DIE_UNLESS(param_count == exp_count);
853 static void verify_st_affected_rows(
MYSQL_STMT *stmt, ulonglong exp_count)
855 ulonglong affected_rows= mysql_stmt_affected_rows(stmt);
857 fprintf(stdout,
"\n total affected rows: `%ld` (expected: `%ld`)",
858 (
long) affected_rows, (
long) exp_count);
859 DIE_UNLESS(affected_rows == exp_count);
865 static void verify_affected_rows(ulonglong exp_count)
867 ulonglong affected_rows= mysql_affected_rows(mysql);
869 fprintf(stdout,
"\n total affected rows: `%ld` (expected: `%ld`)",
870 (
long) affected_rows, (
long) exp_count);
871 DIE_UNLESS(affected_rows == exp_count);
877 static void verify_field_count(
MYSQL_RES *result, uint exp_count)
879 uint field_count= mysql_num_fields(result);
881 fprintf(stdout,
"\n total fields in the result set: `%d` (expected: `%d`)",
882 field_count, exp_count);
883 DIE_UNLESS(field_count == exp_count);
889 #ifndef EMBEDDED_LIBRARY
890 static void execute_prepare_query(
const char *query, ulonglong exp_count)
893 ulonglong affected_rows;
896 stmt= mysql_simple_prepare(mysql, query);
899 rc= mysql_stmt_execute(stmt);
902 affected_rows= mysql_stmt_affected_rows(stmt);
904 fprintf(stdout,
"\n total affected rows: `%ld` (expected: `%ld`)",
905 (
long) affected_rows, (
long) exp_count);
907 DIE_UNLESS(affected_rows == exp_count);
908 mysql_stmt_close(stmt);
917 void fill_tables(
const char **query_list,
unsigned query_count)
921 DBUG_ENTER(
"fill_tables");
922 for (query= query_list; query < query_list + query_count;
925 rc= mysql_query(mysql, *query);
937 enum { MAX_COLUMN_LENGTH= 255 };
947 unsigned long *out_data_length;
948 unsigned column_count;
958 void stmt_fetch_init(
Stmt_fetch *fetch,
unsigned stmt_no_arg,
959 const char *query_arg)
961 unsigned long type= CURSOR_TYPE_READ_ONLY;
965 DBUG_ENTER(
"stmt_fetch_init");
968 fetch->stmt_no= stmt_no_arg;
969 fetch->query= query_arg;
971 fetch->handle= mysql_stmt_init(mysql);
973 rc= mysql_stmt_prepare(fetch->handle, fetch->query, strlen(fetch->query));
974 check_execute(fetch->handle, rc);
980 mysql_stmt_attr_set(fetch->handle, STMT_ATTR_CURSOR_TYPE,
981 (
const void*) &type);
983 rc= mysql_stmt_execute(fetch->handle);
984 check_execute(fetch->handle, rc);
987 metadata= mysql_stmt_result_metadata(fetch->handle);
988 fetch->column_count= mysql_num_fields(metadata);
989 mysql_free_result(metadata);
998 fetch->column_count);
999 fetch->out_data= (
char**) calloc(1,
sizeof(
char*) * fetch->column_count);
1000 fetch->out_data_length= (ulong*) calloc(1,
sizeof(ulong) *
1001 fetch->column_count);
1002 for (i= 0; i < fetch->column_count; ++
i)
1004 fetch->out_data[
i]= (
char*) calloc(1, MAX_COLUMN_LENGTH);
1005 fetch->bind_array[
i].buffer_type= MYSQL_TYPE_STRING;
1006 fetch->bind_array[
i].buffer= fetch->out_data[
i];
1007 fetch->bind_array[
i].buffer_length= MAX_COLUMN_LENGTH;
1008 fetch->bind_array[
i].length= fetch->out_data_length +
i;
1011 mysql_stmt_bind_result(fetch->handle, fetch->bind_array);
1013 fetch->row_count= 0;
1014 fetch->is_open= TRUE;
1027 DBUG_ENTER(
"stmt_fetch_fetch_row");
1029 if ((rc= mysql_stmt_fetch(fetch->handle)) == 0)
1033 printf(
"Stmt %d fetched row %d:\n", fetch->stmt_no, fetch->row_count);
1034 for (i= 0; i < fetch->column_count; ++
i)
1036 fetch->out_data[
i][fetch->out_data_length[
i]]=
'\0';
1038 printf(
"column %d: %s\n", i+1, fetch->out_data[i]);
1042 fetch->is_open= FALSE;
1050 DBUG_ENTER(
"stmt_fetch_close");
1052 for (i= 0; i < fetch->column_count; ++
i)
1053 free(fetch->out_data[i]);
1054 free(fetch->out_data);
1055 free(fetch->out_data_length);
1056 free(fetch->bind_array);
1057 mysql_stmt_close(fetch->handle);
1068 enum fetch_type { USE_ROW_BY_ROW_FETCH= 0, USE_STORE_RESULT= 1 };
1070 my_bool fetch_n(
const char **query_list,
unsigned query_count,
1071 enum fetch_type fetch_type)
1073 unsigned open_statements= query_count;
1074 int rc, error_count= 0;
1078 DBUG_ENTER(
"fetch_n");
1080 for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1083 stmt_fetch_init(fetch, fetch - fetch_array,
1084 query_list[fetch - fetch_array]);
1087 if (fetch_type == USE_STORE_RESULT)
1089 for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1091 rc= mysql_stmt_store_result(fetch->handle);
1092 check_execute(fetch->handle, rc);
1096 while (open_statements)
1098 for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1100 if (fetch->is_open && (rc= stmt_fetch_fetch_row(fetch)))
1107 if (rc != MYSQL_NO_DATA)
1110 "Got error reading rows from statement %d,\n"
1112 "error message: %s", (
int) (fetch - fetch_array),
1114 mysql_stmt_error(fetch->handle));
1121 fprintf(stderr,
"Fetch FAILED");
1124 unsigned total_row_count= 0;
1125 for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1126 total_row_count+= fetch->row_count;
1128 printf(
"Success, total rows fetched: %d\n", total_row_count);
1130 for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
1131 stmt_fetch_close(fetch);
1133 DBUG_RETURN(error_count != 0);
1138 static my_bool thread_query(
const char *query)
1145 fprintf(stdout,
"\n in thread_query(%s)", query);
1146 if (!(l_mysql= mysql_client_init(NULL)))
1148 myerror(
"mysql_client_init() failed");
1151 if (!(mysql_real_connect(l_mysql, opt_host, opt_user,
1152 opt_password, current_db, opt_port,
1153 opt_unix_socket, 0)))
1155 myerror(
"connection failed");
1159 l_mysql->reconnect= 1;
1160 if (mysql_query(l_mysql, query))
1162 fprintf(stderr,
"Query failed (%s)\n", mysql_error(l_mysql));
1166 mysql_commit(l_mysql);
1168 mysql_close(l_mysql);
1173 static struct my_option client_test_long_options[] =
1175 {
"basedir",
'b',
"Basedir for tests.", &opt_basedir,
1176 &opt_basedir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1177 {
"count",
't',
"Number of times test to be executed", &opt_count_read,
1178 &opt_count_read, 0, GET_UINT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0},
1179 {
"database",
'D',
"Database to use", &opt_db, &opt_db,
1180 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1181 {
"do-not-drop-database",
'd',
"Do not drop database while disconnecting",
1182 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1183 {
"debug",
'#',
"Output debug log", &default_dbug_option,
1184 &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1185 {
"help",
'?',
"Display this help and exit", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
1187 {
"host",
'h',
"Connect to host", &opt_host, &opt_host,
1188 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1190 "Password to use when connecting to server. If password is not given it's asked from the tty.",
1191 0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1192 {
"port",
'P',
"Port number to use for connection or 0 for default to, in "
1193 "order of preference, my.cnf, $MYSQL_TCP_PORT, "
1194 #if MYSQL_PORT_DEFAULT == 0
1197 "built-in default (" STRINGIFY_ARG(MYSQL_PORT)
").",
1198 &opt_port, &opt_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1199 {
"server-arg",
'A',
"Send embedded server this as a parameter.",
1200 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1201 {
"show-tests",
'T',
"Show all tests' names", 0, 0, 0, GET_NO_ARG, NO_ARG,
1203 {
"silent",
's',
"Be more silent", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0,
1206 {
"shared-memory-base-name",
'm',
"Base name of shared memory.",
1207 &shared_memory_base_name, (uchar**)&shared_memory_base_name, 0,
1208 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1210 {
"socket",
'S',
"Socket file to use for connection",
1211 &opt_unix_socket, &opt_unix_socket, 0, GET_STR,
1212 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1214 "May disable some code when runs as mysql-test-run testcase.",
1215 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1216 #ifndef DONT_ALLOW_USER_CHANGE
1217 {
"user",
'u',
"User for login if not current user", &opt_user,
1218 &opt_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1220 {
"vardir",
'v',
"Data dir for tests.", &opt_vardir,
1221 &opt_vardir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1222 {
"getopt-ll-test",
'g',
"Option for testing bug in getopt library",
1223 &opt_getopt_ll_test, &opt_getopt_ll_test, 0,
1224 GET_LL, REQUIRED_ARG, 0, 0, LONGLONG_MAX, 0, 0, 0},
1225 {
"plugin_dir", 0,
"Directory for client-side plugins.",
1226 &opt_plugin_dir, &opt_plugin_dir, 0,
1227 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1228 {
"default_auth", 0,
"Default authentication client-side plugin to use.",
1229 &opt_default_auth, &opt_default_auth, 0,
1230 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1231 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1235 static void usage(
void)
1239 printf(
"%s Ver %s Distrib %s, for %s (%s)\n",
1240 my_progname, VER, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
1241 puts(
"By Monty, Venu, Kent and others\n");
1243 Copyright (C) 2002-2004 MySQL AB\n\
1244 This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
1245 and you are welcome to modify and redistribute it under the GPL license\n");
1246 printf(
"Usage: %s [OPTIONS] [TESTNAME1 TESTNAME2...]\n", my_progname);
1247 my_print_help(client_test_long_options);
1248 print_defaults(
"my", client_test_load_default_groups);
1249 my_print_variables(client_test_long_options);
1257 get_one_option(
int optid,
const struct my_option *opt __attribute__((unused)),
1262 DBUG_PUSH(argument ? argument : default_dbug_option);
1270 char *start=argument;
1271 my_free(opt_password);
1272 opt_password= my_strdup(argument, MYF(MY_FAE));
1273 while (*argument) *argument++=
'x';
1281 if (argument == disabled_my_option)
1296 if (!embedded_server_arg_count)
1298 embedded_server_arg_count= 1;
1299 embedded_server_args[0]= (
char*)
"";
1301 if (embedded_server_arg_count == MAX_SERVER_ARGS-1 ||
1302 !(embedded_server_args[embedded_server_arg_count++]=
1303 my_strdup(argument, MYF(MY_FAE))))
1305 DIE(
"Can't use server argument");
1312 printf(
"All possible test names:\n\n");
1313 for (fptr= my_testlist; fptr->name; fptr++)
1314 printf(
"%s\n", fptr->name);
1327 static void get_options(
int *argc,
char ***argv)
1332 defaults_argv= *argv;
1336 if ((ho_error= handle_options(argc, argv, client_test_long_options,
1341 opt_password= get_tty_password(NullS);
1349 static void print_test_output()
1353 fprintf(stdout,
"\n\n");
1354 fprintf(stdout,
"All '%d' tests were successful (in '%d' iterations)",
1355 test_count-1, opt_count);
1358 fprintf(stdout,
"\n Total execution time: %g SECS", total_time);
1360 fprintf(stdout,
" (Avg: %g SECS)", total_time/opt_count);
1363 fprintf(stdout,
"\n\n!!! SUCCESS !!!\n");
1372 int main(
int argc,
char **argv)
1375 char **tests_to_run= NULL, **curr_test;
1377 my_testlist= get_my_tests();
1382 original_argc= argc;
1383 original_argv= malloc(argc *
sizeof(
char*));
1384 if (argc && !original_argv)
1386 for (i= 0; i < argc; i++)
1387 original_argv[i]= strdup(argv[i]);
1389 if (load_defaults(
"my", client_test_load_default_groups, &argc, &argv))
1392 get_options(&argc, &argv);
1395 opt_count= opt_count_read;
1400 tests_to_run= malloc((argc + 1) *
sizeof(
char*));
1403 for (i= 0; i < argc; i++)
1404 tests_to_run[i]= strdup(argv[i]);
1405 tests_to_run[
i]= NULL;
1408 if (mysql_server_init(embedded_server_arg_count,
1409 embedded_server_args,
1410 (
char**) embedded_server_groups))
1411 DIE(
"Can't initialize MySQL server");
1414 mysql= client_connect(0, MYSQL_PROTOCOL_DEFAULT, 1);
1417 for (iter_count= 1; iter_count <= opt_count; iter_count++)
1421 start_time= time((time_t *)0);
1424 for (fptr= my_testlist; fptr->name; fptr++)
1425 (*fptr->function)();
1429 for (curr_test= tests_to_run ; *curr_test ; curr_test++)
1431 for (fptr= my_testlist; fptr->name; fptr++)
1433 if (!strcmp(fptr->name, *curr_test))
1435 (*fptr->function)();
1441 fprintf(stderr,
"\n\nGiven test not found: '%s'\n", *argv);
1442 fprintf(stderr,
"See legal test names with %s -T\n\nAborting!\n",
1444 client_disconnect(mysql);
1445 free_defaults(defaults_argv);
1452 end_time= time((time_t *)0);
1453 total_time+= difftime(end_time, start_time);
1458 client_disconnect(mysql);
1460 free_defaults(defaults_argv);
1461 print_test_output();
1463 while (embedded_server_arg_count > 1)
1464 my_free(embedded_server_args[--embedded_server_arg_count]);
1470 for (i= 0; i < original_argc; i++)
1471 free(original_argv[i]);
1473 free(original_argv);
1476 for (curr_test= tests_to_run ; *curr_test ; curr_test++)
1480 my_free(opt_password);