@@ -16,7 +16,7 @@ import (
16
16
17
17
type queryRequest struct {
18
18
Query string `json:"query"`
19
- Limit int `json:"limit"`
19
+ Limit int `json:"limit,omitempty "`
20
20
}
21
21
22
22
// genericVals returns a slice of interface{}, each one a pointer to the proper
@@ -50,12 +50,14 @@ func Query(db *sql.DB) RequestProcessFunc {
50
50
return func (r * http.Request ) (* serializer.Response , error ) {
51
51
var queryRequest queryRequest
52
52
body , err := ioutil .ReadAll (r .Body )
53
- if err = = nil {
54
- err = json . Unmarshal ( body , & queryRequest )
53
+ if err ! = nil {
54
+ return nil , err
55
55
}
56
56
57
+ err = json .Unmarshal (body , & queryRequest )
57
58
if err != nil {
58
- return nil , err
59
+ return nil , serializer .NewHTTPError (http .StatusBadRequest ,
60
+ `Bad Request. Expected body: { "query": "SQL statement", "limit": 1234 }` )
59
61
}
60
62
61
63
query := addLimit (queryRequest .Query , queryRequest .Limit )
@@ -184,9 +186,14 @@ func unmarshallUAST(data interface{}) ([]*uast.Node, error) {
184
186
// addLimit adds LIMIT to the query, performing basic tests to skip it
185
187
// for DESCRIBE TABLE, SHOW TABLES, and avoid '; limit'
186
188
func addLimit (query string , limit int ) string {
189
+ if limit <= 0 {
190
+ return query
191
+ }
192
+
187
193
query = strings .TrimRight (strings .TrimSpace (query ), ";" )
188
194
if strings .HasPrefix (strings .ToUpper (query ), "SELECT" ) {
189
195
return fmt .Sprintf ("%s LIMIT %d" , query , limit )
190
196
}
197
+
191
198
return query
192
199
}
0 commit comments