Skip to content

Commit f777326

Browse files
authored
Evaluate the schema when requesting tables (#24)
1 parent 521fc51 commit f777326

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

completion.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111

1212
// Completable will be used to autocomplete Tables Schemas and Columns for SQL languages
1313
type Completable interface {
14-
Tables(ctx context.Context) ([]string, error)
1514
Schemas(ctx context.Context) ([]string, error)
15+
Tables(ctx context.Context, schema string) ([]string, error)
1616
Columns(ctx context.Context, table string) ([]string, error)
1717
}
1818

@@ -32,17 +32,21 @@ func sendResourceResponse(rw http.ResponseWriter, res []string) {
3232
}
3333
}
3434

35+
type tableRequest struct {
36+
Schema string `json:"schema"`
37+
}
38+
3539
type columnRequest struct {
3640
Table string `json:"table"`
3741
}
3842

39-
func (ds *sqldatasource) getTables(rw http.ResponseWriter, req *http.Request) {
43+
func (ds *sqldatasource) getSchemas(rw http.ResponseWriter, req *http.Request) {
4044
if ds.Completable == nil {
4145
handleError(rw, errors.New("not implemented"))
4246
return
4347
}
4448

45-
res, err := ds.Completable.Tables(req.Context())
49+
res, err := ds.Completable.Schemas(req.Context())
4650
if err != nil {
4751
handleError(rw, err)
4852
return
@@ -51,13 +55,18 @@ func (ds *sqldatasource) getTables(rw http.ResponseWriter, req *http.Request) {
5155
sendResourceResponse(rw, res)
5256
}
5357

54-
func (ds *sqldatasource) getSchemas(rw http.ResponseWriter, req *http.Request) {
58+
func (ds *sqldatasource) getTables(rw http.ResponseWriter, req *http.Request) {
5559
if ds.Completable == nil {
5660
handleError(rw, errors.New("not implemented"))
5761
return
5862
}
5963

60-
res, err := ds.Completable.Schemas(req.Context())
64+
reqBody := tableRequest{}
65+
if err := json.NewDecoder(req.Body).Decode(&reqBody); err != nil {
66+
handleError(rw, err)
67+
return
68+
}
69+
res, err := ds.Completable.Tables(req.Context(), reqBody.Schema)
6170
if err != nil {
6271
handleError(rw, err)
6372
return

completion_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ func Test_sendResourceResponse(t *testing.T) {
5050
}
5151

5252
type fakeCompletable struct {
53-
tables []string
5453
schemas []string
54+
tables map[string][]string
5555
columns map[string][]string
5656
err error
5757
}
5858

59-
func (f *fakeCompletable) Tables(ctx context.Context) ([]string, error) {
60-
return f.tables, f.err
61-
}
6259
func (f *fakeCompletable) Schemas(ctx context.Context) ([]string, error) {
6360
return f.schemas, f.err
61+
}
6462

63+
func (f *fakeCompletable) Tables(ctx context.Context, schema string) ([]string, error) {
64+
return f.tables[schema], f.err
6565
}
66+
6667
func (f *fakeCompletable) Columns(ctx context.Context, table string) ([]string, error) {
6768
return f.columns[table], f.err
6869
}
@@ -75,20 +76,20 @@ func TestCompletable(t *testing.T) {
7576
reqBody string
7677
expectedRes string
7778
}{
78-
{
79-
"it should return tables",
80-
"tables",
81-
&fakeCompletable{tables: []string{"foo", "bar"}},
82-
"",
83-
`["foo","bar"]` + "\n",
84-
},
8579
{
8680
"it should return schemas",
8781
"schemas",
8882
&fakeCompletable{schemas: []string{"foo", "bar"}},
8983
"",
9084
`["foo","bar"]` + "\n",
9185
},
86+
{
87+
"it should return tables of a schema",
88+
"tables",
89+
&fakeCompletable{tables: map[string][]string{"foobar": {"foo", "bar"}}},
90+
`{"schema":"foobar"}`,
91+
`["foo","bar"]` + "\n",
92+
},
9293
{
9394
"it should return columns of a table",
9495
"columns",
@@ -104,13 +105,15 @@ func TestCompletable(t *testing.T) {
104105
sqlds := &sqldatasource{}
105106
sqlds.Completable = test.fakeImpl
106107

108+
b := ioutil.NopCloser(bytes.NewReader([]byte(test.reqBody)))
107109
switch test.method {
108-
case "tables":
109-
sqlds.getTables(w, &http.Request{})
110110
case "schemas":
111111
sqlds.getSchemas(w, &http.Request{})
112+
case "tables":
113+
sqlds.getTables(w, &http.Request{
114+
Body: b,
115+
})
112116
case "columns":
113-
b := ioutil.NopCloser(bytes.NewReader([]byte(test.reqBody)))
114117
sqlds.getColumns(w, &http.Request{
115118
Body: b,
116119
})

0 commit comments

Comments
 (0)