From 2629dd5f276b521013e1df3c66d789d0c03473fc Mon Sep 17 00:00:00 2001 From: vialeon Date: Sun, 18 Jul 2021 10:39:58 -0500 Subject: [PATCH 1/4] implement hexists --- internal/redis/hexists/hexists.go | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 internal/redis/hexists/hexists.go diff --git a/internal/redis/hexists/hexists.go b/internal/redis/hexists/hexists.go new file mode 100644 index 0000000..14b5d5e --- /dev/null +++ b/internal/redis/hexists/hexists.go @@ -0,0 +1,39 @@ +package hexists + +import ( + "context" + "fmt" + + "github.com/go-redis/redis/v8" + "go.riyazali.net/sqlite" +) + +type hexists struct { + rdb *redis.Client +} + +func (f *hexists) Args() int { return -1 } +func (f *hexists) Deterministic() bool { return false } +func (f *hexists) Apply(ctx *sqlite.Context, values ...sqlite.Value) { + var ( + key string + field string + ) + + if len(values) >= 2 { + key = values[0].Text() + field = values[1].Text() + } else { + ctx.ResultError(fmt.Errorf("must supply argument to redis hexists command (key,field) ")) + return + } + + result := f.rdb.HExists(context.TODO(), key, field) + + 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 &hexists{rdb} +} From 521321a0588ea45a3a8c04f42b6b33f89c2284ca Mon Sep 17 00:00:00 2001 From: vialeon Date: Sun, 18 Jul 2021 10:40:05 -0500 Subject: [PATCH 2/4] testing for hexists --- internal/redis/hexists/hexists_test.go | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 internal/redis/hexists/hexists_test.go diff --git a/internal/redis/hexists/hexists_test.go b/internal/redis/hexists/hexists_test.go new file mode 100644 index 0000000..070289e --- /dev/null +++ b/internal/redis/hexists/hexists_test.go @@ -0,0 +1,46 @@ +package hexists_test + +import ( + "testing" + + "github.com/augmentable-dev/reqlite/internal/redis/hexists" + _ "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 TestHExists(t *testing.T) { + rdb, mock := redismock.NewClientMock() + + sqlite.Register(func(api *sqlite.ExtensionApi) (sqlite.ErrorCode, error) { + if err := api.CreateFunction("hexists", hexists.New(rdb)); err != nil { + return sqlite.SQLITE_ERROR, err + } + return sqlite.SQLITE_OK, nil + }) + + mock.ExpectHExists("mykey", "myfield").SetVal(true) + db, err := sqlx.Open("sqlite3", ":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + row := db.QueryRow("SELECT hexists('mykey','myfield')") + 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 6a8863a31e74a9e864fd06b807dd13869fa51a05 Mon Sep 17 00:00:00 2001 From: vialeon Date: Sun, 18 Jul 2021 10:41:21 -0500 Subject: [PATCH 3/4] add hexists to commands 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..df5b937 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,12 @@ SELECT CLUSTER_COUNTKEYSINSLOT() SELECT DBSIZE() ``` +### HEXISTS + +```sql +SELECT HEXISTS('some-key','some-field') +``` + ### LLEN ```sql diff --git a/commands.md b/commands.md index 03d1a3b..c2296b0 100644 --- a/commands.md +++ b/commands.md @@ -108,7 +108,7 @@ | GETSET | 🚧 | HDEL | 🚧 | HELLO | 🚧 -| HEXISTS | 🚧 +| HEXISTS | [`HEXISTS`](https://github.com/augmentable-dev/reqlite/tree/main/internal/redis/hexists) | HGET | 🚧 | HGETALL | ✅ [`HGETALL`](https://github.com/augmentable-dev/reqlite/tree/main/internal/redis/hgetall) | HINCRBY | 🚧 From b95b5e2fa2c2e7ecb56615f610fe366ca519ca9b Mon Sep 17 00:00:00 2001 From: vialeon Date: Sun, 18 Jul 2021 10:41:56 -0500 Subject: [PATCH 4/4] add hexists 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..9e4412b 100644 --- a/pkg/ext/ext.go +++ b/pkg/ext/ext.go @@ -16,6 +16,7 @@ import ( "github.com/augmentable-dev/reqlite/internal/redis/dbsize" "github.com/augmentable-dev/reqlite/internal/redis/dump" "github.com/augmentable-dev/reqlite/internal/redis/echo" + "github.com/augmentable-dev/reqlite/internal/redis/hexists" "github.com/augmentable-dev/reqlite/internal/redis/hgetall" "github.com/augmentable-dev/reqlite/internal/redis/llen" "github.com/augmentable-dev/reqlite/internal/redis/lrange" @@ -98,6 +99,10 @@ func init() { return sqlite.SQLITE_ERROR, err } + if err := api.CreateFunction("hexists", hexists.New(rdb)); err != nil { + return sqlite.SQLITE_ERROR, err + } + if err := api.CreateFunction("llen", llen.New(rdb)); err != nil { return sqlite.SQLITE_ERROR, err }