Only run actual query without any comments
This commit is contained in:
parent
540613645f
commit
6edc384c05
@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -148,10 +147,10 @@ func GetObjects(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RunQuery(c *gin.Context) {
|
func RunQuery(c *gin.Context) {
|
||||||
query := strings.TrimSpace(c.Request.FormValue("query"))
|
query := cleanQuery(c.Request.FormValue("query"))
|
||||||
|
|
||||||
if query == "" {
|
if query == "" {
|
||||||
c.JSON(400, errors.New("Query parameter is missing"))
|
c.JSON(400, NewError(errors.New("Query parameter is missing")))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,10 +158,10 @@ func RunQuery(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExplainQuery(c *gin.Context) {
|
func ExplainQuery(c *gin.Context) {
|
||||||
query := strings.TrimSpace(c.Request.FormValue("query"))
|
query := cleanQuery(c.Request.FormValue("query"))
|
||||||
|
|
||||||
if query == "" {
|
if query == "" {
|
||||||
c.JSON(400, errors.New("Query parameter is missing"))
|
c.JSON(400, NewError(errors.New("Query parameter is missing")))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +45,21 @@ func NewError(err error) Error {
|
|||||||
return Error{err.Error()}
|
return Error{err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns a clean query without any comment statements
|
||||||
|
func cleanQuery(query string) string {
|
||||||
|
lines := []string{}
|
||||||
|
|
||||||
|
for _, line := range strings.Split(query, "\n") {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if strings.HasPrefix(line, "--") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
lines = append(lines, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(strings.Join(lines, "\n"))
|
||||||
|
}
|
||||||
|
|
||||||
func desanitize64(query string) string {
|
func desanitize64(query string) string {
|
||||||
// Before feeding the string into decoded, we must "reconstruct" the base64 data.
|
// Before feeding the string into decoded, we must "reconstruct" the base64 data.
|
||||||
// Javascript replaces a few characters to be url-safe.
|
// Javascript replaces a few characters to be url-safe.
|
||||||
|
@ -17,3 +17,9 @@ func Test_desanitize64(t *testing.T) {
|
|||||||
assert.Equal(t, expected, desanitize64(example))
|
assert.Equal(t, expected, desanitize64(example))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_cleanQuery(t *testing.T) {
|
||||||
|
assert.Equal(t, "a\nb\nc", cleanQuery("a\nb\nc"))
|
||||||
|
assert.Equal(t, "", cleanQuery("--something"))
|
||||||
|
assert.Equal(t, "test", cleanQuery("--test\ntest\n -- test\n"))
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user