Skip to content
Merged
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
109 changes: 46 additions & 63 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
)

const (
schemas = "schemas"
tables = "tables"
columns = "columns"
)

// ErrorNotImplemented is returned if the function is not implemented by the provided Driver (the Completable pointer is nil)
var ErrorNotImplemented = errors.New("not implemented")

// Options are used to query schemas, tables and columns. They will be encoded in the request body (e.g. {"database": "mydb"})
type Options map[string]string

// Completable will be used to autocomplete Tables Schemas and Columns for SQL languages
type Completable interface {
Schemas(ctx context.Context) ([]string, error)
Tables(ctx context.Context, schema string) ([]string, error)
Columns(ctx context.Context, table string) ([]string, error)
Schemas(ctx context.Context, options Options) ([]string, error)
Tables(ctx context.Context, options Options) ([]string, error)
Columns(ctx context.Context, options Options) ([]string, error)
}

func handleError(rw http.ResponseWriter, err error) {
Expand All @@ -36,74 +45,48 @@ func sendResourceResponse(rw http.ResponseWriter, res []string) {
}
}

type tableRequest struct {
Schema string `json:"schema"`
}

type columnRequest struct {
Table string `json:"table"`
}

func (ds *sqldatasource) getSchemas(rw http.ResponseWriter, req *http.Request) {
if ds.Completable == nil {
handleError(rw, ErrorNotImplemented)
return
}

res, err := ds.Completable.Schemas(req.Context())
if err != nil {
handleError(rw, err)
return
}

sendResourceResponse(rw, res)
}

func (ds *sqldatasource) getTables(rw http.ResponseWriter, req *http.Request) {
if ds.Completable == nil {
handleError(rw, ErrorNotImplemented)
return
}

reqBody := tableRequest{}
if err := json.NewDecoder(req.Body).Decode(&reqBody); err != nil {
handleError(rw, err)
return
}
res, err := ds.Completable.Tables(req.Context(), reqBody.Schema)
if err != nil {
handleError(rw, err)
return
}
func (ds *sqldatasource) getResources(rtype string) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
if ds.Completable == nil {
handleError(rw, ErrorNotImplemented)
return
}

sendResourceResponse(rw, res)
}
options := Options{}
if req.Body != nil {
err := json.NewDecoder(req.Body).Decode(&options)
if err != nil {
handleError(rw, err)
return
}
}

func (ds *sqldatasource) getColumns(rw http.ResponseWriter, req *http.Request) {
if ds.Completable == nil {
handleError(rw, ErrorNotImplemented)
return
}
var res []string
var err error
switch rtype {
case schemas:
res, err = ds.Completable.Schemas(req.Context(), options)
case tables:
res, err = ds.Completable.Tables(req.Context(), options)
case columns:
res, err = ds.Completable.Columns(req.Context(), options)
default:
err = fmt.Errorf("unexpected resource type: %s", rtype)
}
if err != nil {
handleError(rw, err)
return
}

column := columnRequest{}
if err := json.NewDecoder(req.Body).Decode(&column); err != nil {
handleError(rw, err)
return
sendResourceResponse(rw, res)
}
res, err := ds.Completable.Columns(req.Context(), column.Table)
if err != nil {
handleError(rw, err)
return
}

sendResourceResponse(rw, res)
}

func (ds *sqldatasource) registerRoutes(mux *http.ServeMux) error {
defaultRoutes := map[string]func(http.ResponseWriter, *http.Request){
"/tables": ds.getTables,
"/schemas": ds.getSchemas,
"/columns": ds.getColumns,
"/tables": ds.getResources(tables),
"/schemas": ds.getResources(schemas),
"/columns": ds.getResources(columns),
}
for route, handler := range defaultRoutes {
mux.HandleFunc(route, handler)
Expand Down
38 changes: 13 additions & 25 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ func Test_sendResourceResponse(t *testing.T) {
}

type fakeCompletable struct {
schemas []string
schemas map[string][]string
tables map[string][]string
columns map[string][]string
err error
}

func (f *fakeCompletable) Schemas(ctx context.Context) ([]string, error) {
return f.schemas, f.err
func (f *fakeCompletable) Schemas(ctx context.Context, options Options) ([]string, error) {
return f.schemas[options["database"]], f.err
}

func (f *fakeCompletable) Tables(ctx context.Context, schema string) ([]string, error) {
return f.tables[schema], f.err
func (f *fakeCompletable) Tables(ctx context.Context, options Options) ([]string, error) {
return f.tables[options["schema"]], f.err
}

func (f *fakeCompletable) Columns(ctx context.Context, table string) ([]string, error) {
return f.columns[table], f.err
func (f *fakeCompletable) Columns(ctx context.Context, options Options) ([]string, error) {
return f.columns[options["table"]], f.err
}

func TestCompletable(t *testing.T) {
Expand All @@ -78,21 +78,21 @@ func TestCompletable(t *testing.T) {
}{
{
"it should return schemas",
"schemas",
&fakeCompletable{schemas: []string{"foo", "bar"}},
"",
schemas,
&fakeCompletable{schemas: map[string][]string{"foobar": {"foo", "bar"}}},
`{"database":"foobar"}`,
`["foo","bar"]` + "\n",
},
{
"it should return tables of a schema",
"tables",
tables,
&fakeCompletable{tables: map[string][]string{"foobar": {"foo", "bar"}}},
`{"schema":"foobar"}`,
`["foo","bar"]` + "\n",
},
{
"it should return columns of a table",
"columns",
columns,
&fakeCompletable{columns: map[string][]string{"foobar": {"foo", "bar"}}},
`{"table":"foobar"}`,
`["foo","bar"]` + "\n",
Expand All @@ -106,19 +106,7 @@ func TestCompletable(t *testing.T) {
sqlds.Completable = test.fakeImpl

b := ioutil.NopCloser(bytes.NewReader([]byte(test.reqBody)))
switch test.method {
case "schemas":
sqlds.getSchemas(w, &http.Request{})
case "tables":
sqlds.getTables(w, &http.Request{
Body: b,
})
case "columns":
sqlds.getColumns(w, &http.Request{
Body: b,
})
}

sqlds.getResources(test.method)(w, &http.Request{Body: b})
resp := w.Result()
body, _ := io.ReadAll(resp.Body)

Expand Down