-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
181 lines (124 loc) · 4.28 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <regex.h>
#include <mysql.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include "hedis.h"
#define HEDIS_COMMAND_PATTERN ".+"
#define MAX_ERROR_MSG 0x1000
hedisConfigEntry **hedis_entries;
int hedis_entry_count;
char *connector_username;
char *connector_password;
char *connector_host;
char *connector_database;
int init(hedisConfigEntry **entries, int entry_count) {
hedis_entries = entries;
hedis_entry_count = entry_count;
for (int i = 0; i < entry_count; i++) {
if(!strcasecmp(entries[i]->key, "username")){
connector_username = malloc(sizeof(char) * (strlen(entries[i]->value) + 1));
strcpy(connector_username, entries[i]->value);
}
if(!strcasecmp(entries[i]->key, "password")){
connector_password = malloc(sizeof(char) * (strlen(entries[i]->value) + 1));
strcpy(connector_password, entries[i]->value);
}
if(!strcasecmp(entries[i]->key, "host")){
connector_host = malloc(sizeof(char) * (strlen(entries[i]->value) + 1));
strcpy(connector_host, entries[i]->value);
}
if(!strcasecmp(entries[i]->key, "database")){
connector_database = malloc(sizeof(char) * (strlen(entries[i]->value) + 1));
strcpy(connector_database, entries[i]->value);
}
}
return 0;
}
void strip_last_char(char *str) {
int length = strlen(str);
memmove(&str[length - 1], &str[length], 1);
}
char *get_value(const char *str) {
MYSQL *MySQLConRet;
MYSQL *MySQLConnection = NULL;
MySQLConnection = mysql_init(NULL);
MySQLConRet = mysql_real_connect(MySQLConnection, connector_host, connector_username, connector_password, connector_database, 0, NULL, 0);
if (MySQLConRet == NULL) {
printf("Connection fail\n");
return NULL;
}
printf("MySQL Connection Info: %s\n", mysql_get_host_info(MySQLConnection));
printf("MySQL Client Info: %s\n", mysql_get_client_info());
printf("MySQL Server Info: %s\n", mysql_get_server_info(MySQLConnection));
int mysqlStatus = 0;
MYSQL_RES *mysqlResult = NULL;
if (mysqlResult) {
mysql_free_result(mysqlResult);
mysqlResult = NULL;
}
MYSQL_ROW mysqlRow;
MYSQL_FIELD *mysqlFields;
my_ulonglong numRows;
unsigned int numFields;
const char *sqlSelStatement = str;
mysqlStatus = mysql_query(MySQLConnection, sqlSelStatement);
if (mysqlStatus) {
printf("%s\n", (char *)mysql_error(MySQLConnection));
return NULL;
}
mysqlResult = mysql_store_result(MySQLConnection);
if (mysqlResult) {
numRows = mysql_num_rows(mysqlResult);
numFields = mysql_field_count(MySQLConnection);
numFields = mysql_num_fields(mysqlResult);
printf("Number of rows=%u Number of fields=%u\n", numRows, numFields);
} else {
printf("Result set is empty\n");
return NULL;
}
mysqlFields = mysql_fetch_fields(mysqlResult);
for (int i = 0; i < numFields; i++) {
printf("%s\t", mysqlFields[i].name);
}
printf("\n");
char *value = NULL;
value = malloc(sizeof(char) * 200);
strcpy(value, "{\"columns\":{");
char **columns = NULL;
columns = malloc(sizeof(char *) * numFields);
for (int i = 0; i < numFields; i++) {
columns[i] = malloc(sizeof(char) * 100);
strcpy(columns[i], "\"");
strcat(columns[i], mysqlFields[i].name);
strcat(columns[i], "\":[");
}
while (mysqlRow = mysql_fetch_row(mysqlResult)) {
for (int i = 0; i < numFields; i++) {
char *column = mysqlRow[i] ? mysqlRow[i] : "NULL";
strcat(columns[i], "\"");
strcat(columns[i], column);
strcat(columns[i], "\",");
printf("%s\t", column);
}
printf("\n");
}
for (int i = 0; i < numFields; i++) {
strip_last_char(columns[i]);
strcat(value, columns[i]);
strcat(value, "],");
}
strip_last_char(value);
strcat(value, "}}");
if (mysqlResult) {
mysql_free_result(mysqlResult);
mysqlResult = NULL;
}
mysql_close(MySQLConnection);
for (int i = 0; i < numFields; i++) {
free(columns[i]);
}
free(columns);
return value;
}