diff --git a/README.md b/README.md index 1d3d419..f45110e 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,12 @@ SELECT CLUSTER_COUNTKEYSINSLOT() SELECT DBSIZE() ``` +### GETRANGE + +```sql +SELECT GETRANGE('key-id',,) +``` + ### LLEN ```sql diff --git a/commands.md b/commands.md index 03d1a3b..f10f5a4 100644 --- a/commands.md +++ b/commands.md @@ -104,7 +104,7 @@ | GETBIT | 🚧 | GETDEL | 🚧 | GETEX | 🚧 -| GETRANGE | 🚧 +| GETRANGE | ✅ [`GETRANGE`](https://github.com/augmentable-dev/reqlite/tree/main/internal/redis/getrange) | GETSET | 🚧 | HDEL | 🚧 | HELLO | 🚧 diff --git a/internal/redis/getrange/getrange.go b/internal/redis/getrange/getrange.go new file mode 100644 index 0000000..7c5a319 --- /dev/null +++ b/internal/redis/getrange/getrange.go @@ -0,0 +1,41 @@ +package getrange + +import ( + "context" + "fmt" + + "github.com/go-redis/redis/v8" + "go.riyazali.net/sqlite" +) + +type getrange struct { + rdb *redis.Client +} + +func (f *getrange) Args() int { return -1 } +func (f *getrange) Deterministic() bool { return false } +func (f *getrange) Apply(ctx *sqlite.Context, values ...sqlite.Value) { + var ( + key string + start int64 + end int64 + ) + + if len(values) >= 3 { + key = values[0].Text() + start = values[1].Int64() + end = values[2].Int64() + } else { + ctx.ResultError(fmt.Errorf("must supply (key,start,end) to getrange")) + return + } + + result := f.rdb.GetRange(context.TODO(), key, start, end) + + 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 &getrange{rdb} +} diff --git a/internal/redis/getrange/getrange_test.go b/internal/redis/getrange/getrange_test.go new file mode 100644 index 0000000..45833d1 --- /dev/null +++ b/internal/redis/getrange/getrange_test.go @@ -0,0 +1,69 @@ +package getrange_test + +import ( + "testing" + + getrange "github.com/augmentable-dev/reqlite/internal/redis/getrange" + _ "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 TestGetrange(t *testing.T) { + rdb, mock := redismock.NewClientMock() + + sqlite.Register(func(api *sqlite.ExtensionApi) (sqlite.ErrorCode, error) { + if err := api.CreateFunction("getrange", getrange.New(rdb)); err != nil { + return sqlite.SQLITE_ERROR, err + } + return sqlite.SQLITE_OK, nil + }) + + mock.ExpectGetRange("my-key", -4, -1).SetVal("key") + db, err := sqlx.Open("sqlite3", ":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + row := db.QueryRow("SELECT getrange('my-key',-4,-1)") + 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) + } +} +func TestGetrangeErr(t *testing.T) { + rdb, _ := redismock.NewClientMock() + + sqlite.Register(func(api *sqlite.ExtensionApi) (sqlite.ErrorCode, error) { + if err := api.CreateFunction("getrange", getrange.New(rdb)); err != nil { + return sqlite.SQLITE_ERROR, err + } + return sqlite.SQLITE_OK, nil + }) + + db, err := sqlx.Open("sqlite3", ":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + var s string + row := db.QueryRow("SELECT getrange()") + err = row.Scan(&s) + if err == nil && s != "must supply (key,start,end) to getrange" { + t.Fatal("no error returned when no input provided") + } + +} diff --git a/pkg/ext/ext.go b/pkg/ext/ext.go index 77850c5..11d1246 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/getrange" "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("getrange", getrange.New(rdb)); err != nil { + return sqlite.SQLITE_ERROR, err + } + if err := api.CreateFunction("llen", llen.New(rdb)); err != nil { return sqlite.SQLITE_ERROR, err }