forked from Ideonella-sakaiensis/lib_mysqludf_redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_mysqludf_redis.c
334 lines (276 loc) · 7.27 KB
/
lib_mysqludf_redis.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <stdbool.h>
#include <mysql.h>
#include <cjson/cJSON.h>
#include <hiredis/hiredis.h>
#define REDIS_HOST "127.0.0.1"
#define REDIS_PORT 6379
#define ushort unsigned short
#define uint unsigned int
#define TRUE 1
#define FALSE 0
typedef struct st_foreign_server {
bool parsed;
char *connection_string;
char *scheme;
char *hostname;
char *username;
char *password;
char *database;
char *table_name;
ushort port;
uint table_name_length, connect_string_length;
} FOREIGN_SERVER;
static void
freeServerShare(FOREIGN_SERVER *share) {
if (share != NULL) {
if (share->connection_string != NULL) {
free(share->connection_string);
}
free(share);
}
}
static int
parse_url(FOREIGN_SERVER *share) {
share->port= 0;
/*
No :// or @ in connection string. Must be a straight connection name of
either "servername" or "servername/tablename"
*/
if ( (!strstr(share->connection_string, "://") &&
(!strchr(share->connection_string, '@'))) ) {
goto error;
} else {
share->parsed= TRUE;
// Add a null for later termination of table name
share->connection_string[share->connect_string_length]= 0;
share->scheme= share->connection_string;
/*
Remove addition of null terminator and store length
for each string in share
*/
if (!(share->username= (char *)strstr(share->scheme, "://")))
goto error;
share->scheme[share->username - share->scheme]= '\0';
if (strcmp(share->scheme, "redis"))
goto error;
share->username+= 3;
if (!(share->hostname= (char *)strchr(share->username, '@')))
goto error;
*share->hostname++= '\0';
if ((share->password= (char *)strchr(share->username, ':'))) {
*share->password++= '\0';
/* make sure there isn't an extra / or @ */
if ((strchr(share->password, '/') || strchr(share->hostname, '@')))
goto error;
/*
Found that if the string is:
user:@hostname:port/db/table
Then password is a null string, so set to NULL
*/
if (share->password[0] == '\0')
share->password= NULL;
}
/* make sure there isn't an extra / or @ */
if ((strchr(share->username, '/')) || (strchr(share->hostname, '@')))
goto error;
if (!(share->database= (char *)strchr(share->hostname, '/')))
goto error;
*share->database++= '\0';
char* port = NULL;
if ((port= (char *)strchr(share->hostname, ':'))) {
*port++= '\0';
share->port= atoi(port);
}
if (!(share->table_name= (char *)strchr(share->database, '/')))
goto error;
*share->table_name++= '\0';
share->table_name_length= strlen(share->table_name);
/* make sure there's not an extra / */
if ((strchr(share->table_name, '/')))
goto error;
if (share->hostname[0] == '\0')
share->hostname= NULL;
}
if (!share->port) {
share->port= REDIS_PORT;
}
if (!share->hostname) {
share->hostname= REDIS_HOST;
}
return 0;
error:
return -1;
}
static cJSON*
getJsonReply(redisReply *reply, cJSON *json) {
cJSON *array = NULL;
if (json == NULL) {
json = cJSON_CreateObject();
}
switch (reply->type) {
case REDIS_REPLY_STRING:
cJSON_AddStringToObject(json, "out", reply->str);
break;
case REDIS_REPLY_ARRAY:
array = cJSON_CreateArray();
cJSON_AddItemToObject(json, "out", array);
unsigned int i;
for (i = 0; i < reply->elements; i++) {
getJsonReply(reply->element[i], array);
}
break;
case REDIS_REPLY_INTEGER:
cJSON_AddNumberToObject(json, "out", reply->integer);
break;
case REDIS_REPLY_NIL:
cJSON_AddNullToObject(json, "out");
break;
case REDIS_REPLY_STATUS:
cJSON_AddStringToObject(json, "out", reply->str);
break;
case REDIS_REPLY_ERROR:
cJSON_AddStringToObject(json, "err", reply->str);
break;
}
return json;
}
static char*
getResultFromRedisReply(redisReply *reply) {
char *result;
cJSON *jsonReply = getJsonReply(reply, NULL);
{
result = cJSON_Print(jsonReply);
}
cJSON_Delete(jsonReply);
return result;
}
static char*
getErrorResult(char *message) {
char *result;
cJSON *json = cJSON_CreateObject();
cJSON_AddStringToObject(json, "err", message);
{
result = cJSON_Print(json);
}
cJSON_Delete(json);
return result;
}
/**********************************
redis
redis(server, command, args...);
RETURN json
*/
my_bool
redis_init(UDF_INIT *initid,
UDF_ARGS *args,
char *message)
{
if (args->arg_count < 2) {
strcpy(message, "requires at last two arguments.");
return EXIT_FAILURE;
}
if (args->args[0] == NULL || args->args[1] == NULL) {
initid->ptr = NULL;
} else {
if (args->arg_type[0] != STRING_RESULT ||
args->arg_type[1] != STRING_RESULT) {
strcpy(message, "invalid arguments.");
return EXIT_FAILURE;
}
}
initid->maybe_null = 1;
return EXIT_SUCCESS;
}
void
redis_deinit(UDF_INIT *initid)
{
if (initid->ptr != NULL) {
free(initid->ptr);
}
}
char*
redis(UDF_INIT *initid,
UDF_ARGS *args,
char *result,
unsigned long *length,
char *is_null,
char *error)
{
// check arguments
if (args->args[0] == NULL || args->args[1] == NULL) {
*is_null = 1;
return NULL;
} else {
if (args->arg_type[0] != STRING_RESULT ||
args->arg_type[1] != STRING_RESULT) {
// invalid arguments
*error = 1;
return NULL;
}
}
FOREIGN_SERVER *server = NULL;
redisContext *ctx = NULL;
redisReply *reply = NULL;
server = malloc(sizeof(FOREIGN_SERVER));
if (server == NULL) {
// out of memory
*error = 1;
goto final;
}
// import argument server
server->connection_string = strdup(args->args[0]);
server->connect_string_length = (unsigned short)args->lengths[0];
if (parse_url(server) != 0) {
result = getErrorResult("invalid connection string");
goto final;
}
// connection to redis
ctx = redisConnect(server->hostname, server->port);
if (ctx != NULL && ctx->err) {
result = getErrorResult(ctx->errstr);
goto final;
}
// send AUTH command to redis
if (server->password != NULL) {
reply = redisCommand(ctx, "AUTH %s", server->password);
if (reply != NULL) {
if (reply->type == REDIS_REPLY_ERROR) {
result = getResultFromRedisReply(reply);
goto final;
}
freeReplyObject(reply);
}
}
// send SELECT dbnum to redis
if (server->database != 0) {
reply = redisCommand(ctx, "SELECT %s", server->database);
if (reply != NULL) {
if (reply->type == REDIS_REPLY_ERROR) {
result = getResultFromRedisReply(reply);
goto final;
}
freeReplyObject(reply);
}
}
int argc = args->arg_count - 1;
char **argv = ++args->args;
size_t *argvlen = ++args->lengths;
reply = redisCommandArgv(ctx, argc, (const char**)argv, (const size_t*)argvlen);
result = getResultFromRedisReply(reply);
final:
if (reply != NULL) freeReplyObject(reply);
if (ctx != NULL) redisFree(ctx);
if (server != NULL) freeServerShare(server);
if (result != NULL) {
*length = strlen(result);
initid->max_length = *length;
initid->ptr = result;
} else {
*is_null = 1;
}
return result;
}