Makes all other databases superfluous
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.6 KiB

// $ gcc redis-sql.c -I ../ -fPIC -lsqlite3 -std=gnu99 -shared -o sql.o
// $ ../redis-server --loadmodule ./rsql.o
// 127.0.0.1:6379> sql.version
// "3.14.2"
#include <sqlite3.h>
#include <string.h>
#include "redismodule.h"
sqlite3 *db;
sqlite3_stmt *res;
int rc;
// sql.version
int sqlVersion (RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 1) return RedisModule_WrongArity (ctx);
const char *version = sqlite3_libversion ();
RedisModuleString *strings_in_c_are_a_peace_of_shit = RedisModule_CreateString (ctx, version, strlen (version));
return RedisModule_ReplyWithString (ctx, strings_in_c_are_a_peace_of_shit);
}
// sql.cmd
int sqlCmd (RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2) return RedisModule_WrongArity (ctx);
size_t cmdlen;
int cmdidx = 1;
const char *cmd = RedisModule_StringPtrLen(argv[cmdidx], &cmdlen);
rc = sqlite3_prepare_v2(db, cmd, -1, &res, 0);
rc = sqlite3_step(res);
const char *version = sqlite3_column_text(res, 0);
RedisModuleString *strings_in_c_are_a_peace_of_shit = RedisModule_CreateString (ctx, version, strlen (version));
return RedisModule_ReplyWithString(ctx, strings_in_c_are_a_peace_of_shit);
}
int staticArrayTest (RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 1) return RedisModule_WrongArity (ctx);
RedisModule_ReplyWithArray(ctx, REDISMODULE_POSTPONED_ARRAY_LEN);
RedisModule_ReplyWithLongLong(ctx, 1);
RedisModule_ReplyWithArray(ctx, REDISMODULE_POSTPONED_ARRAY_LEN);
RedisModule_ReplyWithLongLong(ctx, 10);
RedisModule_ReplyWithLongLong(ctx, 20);
RedisModule_ReplyWithLongLong(ctx, 30);
RedisModule_ReplySetArrayLength(ctx, 3); // Set len of 10,20,30 array.
RedisModule_ReplySetArrayLength(ctx ,2); // Set len of top array
}
// Redis Module
int RedisModule_OnLoad(RedisModuleCtx *ctx) {
if (RedisModule_Init(ctx, "sql", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
// init in memory sqlite
rc = sqlite3_open (":memory:", &db);
if (rc != SQLITE_OK) {
RedisModule_ReplyWithError(ctx, "FIRE!!");
sqlite3_close(db);
return REDISMODULE_ERR;
}
// register sql.version command
if (RedisModule_CreateCommand (ctx, "sql.version", sqlVersion, "readonly", 1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
// register sql.cmd command
if (RedisModule_CreateCommand (ctx, "sql.cmd", sqlCmd, "no-cluster", 1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand (ctx, "sql.test", staticArrayTest, "no-cluster", 1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
}