@@ -10,14 +10,23 @@ import (
10
10
"github.com/grafana/grafana-plugin-sdk-go/backend"
11
11
)
12
12
13
+ const (
14
+ schemas = "schemas"
15
+ tables = "tables"
16
+ columns = "columns"
17
+ )
18
+
13
19
// ErrorNotImplemented is returned if the function is not implemented by the provided Driver (the Completable pointer is nil)
14
20
var ErrorNotImplemented = errors .New ("not implemented" )
15
21
22
+ // Options are used to query schemas, tables and columns. They will be encoded in the request body (e.g. {"database": "mydb"})
23
+ type Options map [string ]string
24
+
16
25
// Completable will be used to autocomplete Tables Schemas and Columns for SQL languages
17
26
type Completable interface {
18
- Schemas (ctx context.Context ) ([]string , error )
19
- Tables (ctx context.Context , schema string ) ([]string , error )
20
- Columns (ctx context.Context , table string ) ([]string , error )
27
+ Schemas (ctx context.Context , options Options ) ([]string , error )
28
+ Tables (ctx context.Context , options Options ) ([]string , error )
29
+ Columns (ctx context.Context , options Options ) ([]string , error )
21
30
}
22
31
23
32
func handleError (rw http.ResponseWriter , err error ) {
@@ -36,74 +45,48 @@ func sendResourceResponse(rw http.ResponseWriter, res []string) {
36
45
}
37
46
}
38
47
39
- type tableRequest struct {
40
- Schema string `json:"schema"`
41
- }
42
-
43
- type columnRequest struct {
44
- Table string `json:"table"`
45
- }
46
-
47
- func (ds * sqldatasource ) getSchemas (rw http.ResponseWriter , req * http.Request ) {
48
- if ds .Completable == nil {
49
- handleError (rw , ErrorNotImplemented )
50
- return
51
- }
52
-
53
- res , err := ds .Completable .Schemas (req .Context ())
54
- if err != nil {
55
- handleError (rw , err )
56
- return
57
- }
58
-
59
- sendResourceResponse (rw , res )
60
- }
61
-
62
- func (ds * sqldatasource ) getTables (rw http.ResponseWriter , req * http.Request ) {
63
- if ds .Completable == nil {
64
- handleError (rw , ErrorNotImplemented )
65
- return
66
- }
67
-
68
- reqBody := tableRequest {}
69
- if err := json .NewDecoder (req .Body ).Decode (& reqBody ); err != nil {
70
- handleError (rw , err )
71
- return
72
- }
73
- res , err := ds .Completable .Tables (req .Context (), reqBody .Schema )
74
- if err != nil {
75
- handleError (rw , err )
76
- return
77
- }
48
+ func (ds * sqldatasource ) getResources (rtype string ) func (rw http.ResponseWriter , req * http.Request ) {
49
+ return func (rw http.ResponseWriter , req * http.Request ) {
50
+ if ds .Completable == nil {
51
+ handleError (rw , ErrorNotImplemented )
52
+ return
53
+ }
78
54
79
- sendResourceResponse (rw , res )
80
- }
55
+ options := Options {}
56
+ if req .Body != nil {
57
+ err := json .NewDecoder (req .Body ).Decode (& options )
58
+ if err != nil {
59
+ handleError (rw , err )
60
+ return
61
+ }
62
+ }
81
63
82
- func (ds * sqldatasource ) getColumns (rw http.ResponseWriter , req * http.Request ) {
83
- if ds .Completable == nil {
84
- handleError (rw , ErrorNotImplemented )
85
- return
86
- }
64
+ var res []string
65
+ var err error
66
+ switch rtype {
67
+ case schemas :
68
+ res , err = ds .Completable .Schemas (req .Context (), options )
69
+ case tables :
70
+ res , err = ds .Completable .Tables (req .Context (), options )
71
+ case columns :
72
+ res , err = ds .Completable .Columns (req .Context (), options )
73
+ default :
74
+ err = fmt .Errorf ("unexpected resource type: %s" , rtype )
75
+ }
76
+ if err != nil {
77
+ handleError (rw , err )
78
+ return
79
+ }
87
80
88
- column := columnRequest {}
89
- if err := json .NewDecoder (req .Body ).Decode (& column ); err != nil {
90
- handleError (rw , err )
91
- return
81
+ sendResourceResponse (rw , res )
92
82
}
93
- res , err := ds .Completable .Columns (req .Context (), column .Table )
94
- if err != nil {
95
- handleError (rw , err )
96
- return
97
- }
98
-
99
- sendResourceResponse (rw , res )
100
83
}
101
84
102
85
func (ds * sqldatasource ) registerRoutes (mux * http.ServeMux ) error {
103
86
defaultRoutes := map [string ]func (http.ResponseWriter , * http.Request ){
104
- "/tables" : ds .getTables ,
105
- "/schemas" : ds .getSchemas ,
106
- "/columns" : ds .getColumns ,
87
+ "/tables" : ds .getResources ( tables ) ,
88
+ "/schemas" : ds .getResources ( schemas ) ,
89
+ "/columns" : ds .getResources ( columns ) ,
107
90
}
108
91
for route , handler := range defaultRoutes {
109
92
mux .HandleFunc (route , handler )
0 commit comments