Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ SELECT CLUSTER_COUNTKEYSINSLOT(<key_id>)
SELECT DBSIZE()
```

### HEXISTS

```sql
SELECT HEXISTS('some-key','some-field')
```

### LLEN

```sql
Expand Down
2 changes: 1 addition & 1 deletion commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | 🚧
Expand Down
39 changes: 39 additions & 0 deletions internal/redis/hexists/hexists.go
Original file line number Diff line number Diff line change
@@ -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}
}
46 changes: 46 additions & 0 deletions internal/redis/hexists/hexists_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 5 additions & 0 deletions pkg/ext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down