From 5733da941983cc4571fcf3dc77b4d4e7c846edb9 Mon Sep 17 00:00:00 2001 From: vialeon Date: Mon, 19 Jul 2021 17:44:53 -0500 Subject: [PATCH 1/3] create hkeys and testing --- internal/redis/hkeys/hkeys.go | 35 +++++++++++++++++++++++ internal/redis/hkeys/hkeys_test.go | 46 ++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 internal/redis/hkeys/hkeys.go create mode 100644 internal/redis/hkeys/hkeys_test.go diff --git a/internal/redis/hkeys/hkeys.go b/internal/redis/hkeys/hkeys.go new file mode 100644 index 0000000..a898124 --- /dev/null +++ b/internal/redis/hkeys/hkeys.go @@ -0,0 +1,35 @@ +package hkeys + +import ( + "context" + "fmt" + + "github.com/go-redis/redis/v8" + "go.riyazali.net/sqlite" +) + +type hkeys struct { + rdb *redis.Client +} + +func (f *hkeys) Args() int { return -1 } +func (f *hkeys) Deterministic() bool { return false } +func (f *hkeys) Apply(ctx *sqlite.Context, values ...sqlite.Value) { + var key string + + if len(values) >= 1 { + key = values[0].Text() + } else { + ctx.ResultError(fmt.Errorf("must supply argument to redis hkeys command")) + return + } + + result := f.rdb.HKeys(context.TODO(), key) + + ctx.ResultText(fmt.Sprintf("%+q", result)) +} + +// New returns a sqlite function for reading the contents of a file +func New(rdb *redis.Client) sqlite.Function { + return &hkeys{rdb} +} diff --git a/internal/redis/hkeys/hkeys_test.go b/internal/redis/hkeys/hkeys_test.go new file mode 100644 index 0000000..ad85cdb --- /dev/null +++ b/internal/redis/hkeys/hkeys_test.go @@ -0,0 +1,46 @@ +package hkeys_test + +import ( + "testing" + + "github.com/augmentable-dev/reqlite/internal/redis/hkeys" + _ "github.com/augmentable-dev/reqlite/internal/sqlite" + "github.com/go-redis/redismock/v8" + "github.com/jmoiron/sqlx" + _ "github.com/mattn/go-sqlite3" + "go.riyazali.net/sqlite" +) + +func TestDump(t *testing.T) { + rdb, mock := redismock.NewClientMock() + myFields := []string{"field1", "field2", "field3"} + sqlite.Register(func(api *sqlite.ExtensionApi) (sqlite.ErrorCode, error) { + if err := api.CreateFunction("hkeys", hkeys.New(rdb)); err != nil { + return sqlite.SQLITE_ERROR, err + } + return sqlite.SQLITE_OK, nil + }) + + mock.ExpectHKeys("mykey").SetVal(myFields) + db, err := sqlx.Open("sqlite3", ":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + row := db.QueryRow("SELECT hkeys('mykey')") + err = row.Err() + if err != nil { + t.Fatal(err) + } + + var s string + err = row.Scan(&s) + if err != nil { + t.Fatal(err) + } + + if err := mock.ExpectationsWereMet(); err != nil { + t.Error(err) + } +} From 7efcfde1a4acbbf653954067380081819ae867f7 Mon Sep 17 00:00:00 2001 From: vialeon Date: Mon, 19 Jul 2021 17:45:06 -0500 Subject: [PATCH 2/3] add hkeys to commands list and readme --- README.md | 6 ++++++ commands.md | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1d3d419..49c52fa 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,12 @@ SELECT CLUSTER_COUNTKEYSINSLOT() SELECT DBSIZE() ``` +### HKEYS + +```sql +SELECT HKEYS("some-key") +``` + ### LLEN ```sql diff --git a/commands.md b/commands.md index 03d1a3b..718e257 100644 --- a/commands.md +++ b/commands.md @@ -113,7 +113,7 @@ | HGETALL | ✅ [`HGETALL`](https://github.com/augmentable-dev/reqlite/tree/main/internal/redis/hgetall) | HINCRBY | 🚧 | HINCRBYFLOAT | 🚧 -| HKEYS | 🚧 +| HKEYS | ✅ [`HKEYS`](https://github.com/augmentable-dev/reqlite/tree/main/internal/redis/hkeys) | HLEN | 🚧 | HMGET | 🚧 | HMSET | 🚧 From 13620c647330e46efa81f390954231f2e6e7768e Mon Sep 17 00:00:00 2001 From: vialeon Date: Mon, 19 Jul 2021 17:45:13 -0500 Subject: [PATCH 3/3] add hkeys to ext.go --- pkg/ext/ext.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/ext/ext.go b/pkg/ext/ext.go index 77850c5..305a23c 100644 --- a/pkg/ext/ext.go +++ b/pkg/ext/ext.go @@ -17,6 +17,7 @@ import ( "github.com/augmentable-dev/reqlite/internal/redis/dump" "github.com/augmentable-dev/reqlite/internal/redis/echo" "github.com/augmentable-dev/reqlite/internal/redis/hgetall" + "github.com/augmentable-dev/reqlite/internal/redis/hkeys" "github.com/augmentable-dev/reqlite/internal/redis/llen" "github.com/augmentable-dev/reqlite/internal/redis/lrange" "github.com/go-redis/redis/v8" @@ -98,6 +99,10 @@ func init() { return sqlite.SQLITE_ERROR, err } + if err := api.CreateFunction("hkeys", hkeys.New(rdb)); err != nil { + return sqlite.SQLITE_ERROR, err + } + if err := api.CreateFunction("llen", llen.New(rdb)); err != nil { return sqlite.SQLITE_ERROR, err }