0

I want to connect to Redis with ServiceStack.Redis package with c# with below statement.

        var redisManager = new RedisManagerPool("127.0.0.1:6380");
        var redis = redisManager.GetClient();

        redis.Custom("DEL", "DB");

        redis.Custom("REDISQL.CREATE_DB", "DB");
        redis.Custom("REDISQL.EXEC", "DB", "CREATE TABLE TABLE1(A INT, B TEXT);");
        redis.Custom("REDISQL.CREATE_STATEMENT", "DB", "INSERTINTOTABLE1STMT", "INSERT INTO TABLE1 VALUES(?1,?2)");
        redis.Custom("REDISQL.EXEC_STATEMENT", "DB", "INSERTINTOTABLE1STMT", 1, "Value1");
        redis.Custom("REDISQL.EXEC_STATEMENT", "DB", "INSERTINTOTABLE1STMT", 2, "Value2");
        var res = redis.Custom("REDISQL.EXEC", "DB", "SELECT * FROM TABLE1");

Queries are executed fine. Column 'A' of Table1 is defined as an integer. But final command that retrieves all rows from TABLE1 return data of 'A' column as Text because result is RedisText type.

Is there a way to get the results with the same data type of the column ?

Serhat Önal
  • 63
  • 1
  • 8
  • Not sure if it's useful for this module but you can [execute RawCommand instead](https://github.com/ServiceStack/ServiceStack.Redis#generic-apis-for-calling-custom-redis-commands) which captures the raw `byte[]` into a `RedisData`. Can you update your question with your `RedisText` to serialize json, i.e. `res.ToJson()`. Not familiar with the library, want to know if the Type info is captured in the `RedisText` response. – mythz Feb 20 '21 at 23:46
  • 1
    I am also using ServiceStack.Redis and RediSQL / zeeSQL and have worked out most thing I need. Yes, the RedisText returned in the RawCommand isn't easy to work with, it is possible. I wrote a very simple "ORM" for communicating with the SqLite db in zeeSQL, which uses RedisText and Reflection to interpret correct values. Please also note that in the latest version of zeeSQL, you can append "json" at the end, to get the result of the query back as a JSON blob. – Ted Apr 24 '21 at 08:01

1 Answers1

1
127.0.0.1:6379> REDISQL.V2.CREATE_DB DB
1) 1) "OK"
127.0.0.1:6379> REDISQL.EXEC DB COMMAND "CREATE TABLE TABLE1(A INT, B TEXT);"
1) 1) "DONE"
2) 1) (integer) 0
127.0.0.1:6379> REDISQL.EXEC DB COMMAND "INSERT INTO TABLE1 VALUES (1,'A'),(2,'B');"
1) 1) "DONE"
2) 1) (integer) 2
127.0.0.1:6379> REDISQL.EXEC DB COMMAND "SELECT  * FROM TABLE1;"
1) 1) "RESULT"
2) 1) "A"
   2) "B"
3) 1) "INT"
   2) "TEXT"
4) 1) (integer) 1
   2) "A"
5) 1) (integer) 2
   2) "B"
127.0.0.1:6379> REDISQL.EXEC DB COMMAND "SELECT  * FROM TABLE1;" json
"{\"rows\":[{\"A\":1,\"B\":\"A\"},{\"A\":2,\"B\":\"B\"}],\"number_of_rows\":2,\"columns\":{\"A\":\"INT\",\"B\":\"TEXT\"}}"

So as you mention, the problem is ServiceStack that returns a simple RedisText instead of interpreter the correct data in the redis protocol.

In the example above I am using RediSQL V2 (now know as zeeSQL https://zeesql.com) that returns also the type of the columns.

Moreover, we can return directly JSON, with the correct typing.

More info about all the commands in zeeSQL are here: https://doc.zeesql.com/references

Unfortunately I don't believe there is much we can do, beside a suggestion to the ServiceStack maintainers.

The obvious suggestion is to switch to RediSQL V2 / zeeSQL. We maintain perfect backward compatibility with RediSQL V1.

You just have to prefix your commands with REDISQL.V1 instead of just REDISQL.

Siscia
  • 1,421
  • 1
  • 12
  • 29