-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathstatements.go
More file actions
64 lines (56 loc) · 2.2 KB
/
Copy pathstatements.go
File metadata and controls
64 lines (56 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package testserver
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strconv"
"github.com/databricks/cli/libs/testserver/testsql"
"github.com/databricks/databricks-sdk-go/service/sql"
)
// HandleSQL registers a matcher that runs fn when a submitted statement equals
// statement exactly (after trimming).
func (s *Server) HandleSQL(statement string, fn func(testsql.Request) testsql.Result) {
s.sqlHandler.Handle(statement, fn)
}
// HandleSQLPattern registers a matcher that runs fn when re matches a submitted
// statement, passing the submatches through as Request.Match.
func (s *Server) HandleSQLPattern(re *regexp.Regexp, fn func(testsql.Request) testsql.Result) {
s.sqlHandler.HandlePattern(re, fn)
}
// sqlExecuteStatement handles POST /api/2.0/sql/statements. A statement that
// terminates as FAILED comes back as HTTP 200 with state=FAILED; the engine
// builds that response and this HTTP layer is just transport.
func (s *Server) sqlExecuteStatement(req Request) any {
var r sql.ExecuteStatementRequest
if err := json.Unmarshal(req.Body, &r); err != nil {
return Response{StatusCode: http.StatusBadRequest, Body: fmt.Sprintf("invalid execute statement request: %s", err)}
}
return s.sqlHandler.Submit(r.Statement, r.WaitTimeout, r.Parameters)
}
// sqlGetStatement handles GET /api/2.0/sql/statements/{statement_id}.
func (s *Server) sqlGetStatement(req Request) any {
resp := s.sqlHandler.Get(req.Vars["statement_id"])
if resp == nil {
return Response{StatusCode: http.StatusNotFound}
}
return resp
}
// sqlGetStatementResultChunk handles GET
// /api/2.0/sql/statements/{statement_id}/result/chunks/{chunk_index}.
func (s *Server) sqlGetStatementResultChunk(req Request) any {
idx, err := strconv.Atoi(req.Vars["chunk_index"])
if err != nil {
return Response{StatusCode: http.StatusBadRequest, Body: fmt.Sprintf("invalid chunk index: %s", err)}
}
data := s.sqlHandler.Chunk(req.Vars["statement_id"], idx)
if data == nil {
return Response{StatusCode: http.StatusNotFound}
}
return data
}
// sqlCancelStatement handles POST /api/2.0/sql/statements/{statement_id}/cancel.
func (s *Server) sqlCancelStatement(req Request) any {
s.sqlHandler.Cancel(req.Vars["statement_id"])
return map[string]any{}
}