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.

77 lines
2.6KB

  1. // $ gcc redis-sql.c -I ../ -fPIC -lsqlite3 -std=gnu99 -shared -o sql.o
  2. // $ ../redis-server --loadmodule ./rsql.o
  3. // 127.0.0.1:6379> sql.version
  4. // "3.14.2"
  5. #include <sqlite3.h>
  6. #include <string.h>
  7. #include "redismodule.h"
  8. sqlite3 *db;
  9. sqlite3_stmt *res;
  10. int rc;
  11. // sql.version
  12. int sqlVersion (RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  13. if (argc != 1) return RedisModule_WrongArity (ctx);
  14. const char *version = sqlite3_libversion ();
  15. RedisModuleString *strings_in_c_are_a_peace_of_shit = RedisModule_CreateString (ctx, version, strlen (version));
  16. return RedisModule_ReplyWithString (ctx, strings_in_c_are_a_peace_of_shit);
  17. }
  18. // sql.cmd
  19. int sqlCmd (RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  20. if (argc != 2) return RedisModule_WrongArity (ctx);
  21. size_t cmdlen;
  22. int cmdidx = 1;
  23. const char *cmd = RedisModule_StringPtrLen(argv[cmdidx], &cmdlen);
  24. rc = sqlite3_prepare_v2(db, cmd, -1, &res, 0);
  25. rc = sqlite3_step(res);
  26. const char *version = sqlite3_column_text(res, 0);
  27. RedisModuleString *strings_in_c_are_a_peace_of_shit = RedisModule_CreateString (ctx, version, strlen (version));
  28. return RedisModule_ReplyWithString(ctx, strings_in_c_are_a_peace_of_shit);
  29. }
  30. int staticArrayTest (RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  31. if (argc != 1) return RedisModule_WrongArity (ctx);
  32. RedisModule_ReplyWithArray(ctx, REDISMODULE_POSTPONED_ARRAY_LEN);
  33. RedisModule_ReplyWithLongLong(ctx, 1);
  34. RedisModule_ReplyWithArray(ctx, REDISMODULE_POSTPONED_ARRAY_LEN);
  35. RedisModule_ReplyWithLongLong(ctx, 10);
  36. RedisModule_ReplyWithLongLong(ctx, 20);
  37. RedisModule_ReplyWithLongLong(ctx, 30);
  38. RedisModule_ReplySetArrayLength(ctx, 3); // Set len of 10,20,30 array.
  39. RedisModule_ReplySetArrayLength(ctx ,2); // Set len of top array
  40. }
  41. // Redis Module
  42. int RedisModule_OnLoad(RedisModuleCtx *ctx) {
  43. if (RedisModule_Init(ctx, "sql", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) {
  44. return REDISMODULE_ERR;
  45. }
  46. // init in memory sqlite
  47. rc = sqlite3_open (":memory:", &db);
  48. if (rc != SQLITE_OK) {
  49. RedisModule_ReplyWithError(ctx, "FIRE!!");
  50. sqlite3_close(db);
  51. return REDISMODULE_ERR;
  52. }
  53. // register sql.version command
  54. if (RedisModule_CreateCommand (ctx, "sql.version", sqlVersion, "readonly", 1, 1, 1) == REDISMODULE_ERR) {
  55. return REDISMODULE_ERR;
  56. }
  57. // register sql.cmd command
  58. if (RedisModule_CreateCommand (ctx, "sql.cmd", sqlCmd, "no-cluster", 1, 1, 1) == REDISMODULE_ERR) {
  59. return REDISMODULE_ERR;
  60. }
  61. if (RedisModule_CreateCommand (ctx, "sql.test", staticArrayTest, "no-cluster", 1, 1, 1) == REDISMODULE_ERR) {
  62. return REDISMODULE_ERR;
  63. }
  64. }