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.

75 lines
2.5KB

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