1 #include <mysql.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
get_evar(char ** hostname,char ** port,char ** username,char ** password)5 int get_evar(char **hostname, char **port, char** username, char ** password)
6 {
7
8 if (!((*hostname)= getenv("MYSQL_TEST_HOST")))
9 (*hostname)= (char *)"127.0.0.1";
10
11 if (!((*port)= getenv("MASTER_MYPORT")))
12 {
13 if (!((*port)= getenv("MYSQL_TEST_PORT")))
14 return 1;
15 }
16
17 if (!((*username)= getenv("MYSQL_TEST_USER")))
18 (*username)= (char *)"root";
19
20 if (!((*password)= getenv("MYSQL_TEST_PASSWD")))
21 (*password)= (char *)"";
22
23 return 0;
24 }
25
main(int argc,char * argv[])26 int main(int argc, char *argv[])
27 {
28 MYSQL *mysql;
29 char *host;
30 char *user;
31 char *passwd;
32 char *porta;
33 unsigned int port;
34
35 if (get_evar(&host, &porta, &user, &passwd))
36 {
37 printf("set environment variable MASTER_MYPORT\n");
38 return 1;
39 }
40
41 port = atoi(porta);
42
43 mysql_thread_init();
44
45 if (mysql_server_init(-1, NULL, NULL) != 0) {
46 printf("mysql_library_init failed");
47 return 1;
48 }
49
50
51 mysql = mysql_init(NULL);
52
53 if (!mysql) {
54 printf("mysql_init failed");
55 return 1;
56 }
57
58 if (mysql_options(mysql, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL) != 0) {
59 printf("mysql_options MYSQL_OPT_USE_REMOTE_CONNECTION failed: %s\n", mysql_error(mysql));
60 return 1;
61 }
62
63 if (mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "utf8mb4") != 0) {
64 printf("mysql_options MYSQL_SET_CHARSET_NAME utf8mb4 failed: %s\n", mysql_error(mysql));
65 return 1;
66 }
67
68 if (!mysql_real_connect(mysql, host, user, passwd, NULL, port, NULL, CLIENT_FOUND_ROWS | CLIENT_MULTI_RESULTS | CLIENT_REMEMBER_OPTIONS)) {
69 printf("mysql_real_connect failed: %s\n", mysql_error(mysql));
70 return 1;
71 }
72 mysql_close(mysql);
73 mysql_thread_end();
74 mysql_library_end();
75
76 return 0;
77
78 }
79