-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis.c
100 lines (86 loc) · 2.77 KB
/
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
/*
* gcc -fPIC -Wall -I/usr/include/mysql/ -I/usr/local/include/hiredis -lhiredis -shared -o redis.so redis.c
* cp redis.so /usr/lib64/mysql/plugin/ && ldconfig
* mysql -uroot -p -e "drop function if exists redis;create function redis returns string soname 'redis.so';"
*/
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
#include <string.h>
#include <hiredis.h>
#define UNIXSOCKET "/var/tmp/redis.sock"
my_bool redis_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
redisContext *rdsCon;
if(args->arg_count!=1 || args->lengths[0]==0 || args->arg_type[0]!=STRING_RESULT || args->args[0]==NULL || args->args[0][0]==' '){
strcpy(message, "redis() requires ONE string arguments");
return 1;
}
initid->maybe_null = 1;
initid->ptr = NULL;
/* Connect to Redis */
rdsCon = redisConnectUnix(UNIXSOCKET);
if(rdsCon->err){
redisFree(rdsCon);
strcpy(message, "redis() connect to db failed");
return 1;
}
initid->ptr = (char*)rdsCon;
return 0;
}
void redis_deinit(UDF_INIT *initid) {
if(initid->ptr!=NULL){
redisContext *rdsCon;
rdsCon = (redisContext*)initid->ptr;
redisFree(rdsCon);
}
}
char *redis(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *is_null, char *error)
{
redisContext* rdsCon = (redisContext*)initid->ptr;
result[0] = '\0';
*length = 0;
redisReply *rdsRply = redisCommand(rdsCon, args->args[0]);
if(!rdsRply){
*is_null=1;
}else{
switch(rdsRply->type) {
case REDIS_REPLY_ERROR:
*error = 1;
break;
case REDIS_REPLY_ARRAY: {
int elmLen;
int bufIdx = 0;
int i;
for(i=0; i<rdsRply->elements; i++){
elmLen = rdsRply->element[i]->len;
memcpy(&result[bufIdx], rdsRply->element[i]->str, elmLen);
result[bufIdx + elmLen] = '\n';
bufIdx += (elmLen + 1);
}
result[bufIdx - 1] = '\0';
*length = bufIdx - 1;
break;
}
case REDIS_REPLY_STRING:
case REDIS_REPLY_STATUS:
if(rdsRply->len > 0){
strcpy(result, rdsRply->str);
*length = rdsRply->len;
}else{
*is_null = 1;
}
break;
case REDIS_REPLY_INTEGER:
sprintf(result, "%lld", rdsRply->integer);
*length = strlen(result);
break;
case REDIS_REPLY_NIL:
*is_null = 1;
break;
}
}
freeReplyObject(rdsRply);
return result;
}