Skip to content

Commit

Permalink
fix FORMAT in request body
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Pavlovich committed Oct 22, 2019
1 parent 2df1036 commit 1ab635c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 8 additions & 8 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ func (c *Collector) ParseQuery(queryString string, body string) (params string,
// Parse - parsing text for query and data
func (c *Collector) Parse(text string) (prefix string, content string) {
i := strings.Index(text, "FORMAT")
if i >= 0 {
k := strings.Index(text, "VALUES")
if k == -1 {
k = strings.Index(text, "values")
}
if i >= 0 && i < k {
w := false
off := -1
for c := i + 7; c < len(text); c++ {
Expand All @@ -285,13 +289,9 @@ func (c *Collector) Parse(text string) (prefix string, content string) {
content = text[off:]
}
} else {
i = strings.Index(text, "VALUES")
if i == -1 {
i = strings.Index(text, "values")
}
if i >= 0 {
prefix = strings.TrimSpace(text[:i+6])
content = strings.TrimSpace(text[i+6:])
if k >= 0 {
prefix = strings.TrimSpace(text[:k+6])
content = strings.TrimSpace(text[k+6:])
} else {
off := regexFormat.FindStringSubmatchIndex(text)
if len(off) > 3 {
Expand Down
13 changes: 13 additions & 0 deletions collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var escTitle = url.QueryEscape(qTitle)
var escSelect = url.QueryEscape(qSelect)
var escParamsAndSelect = qParams + "&query=" + escSelect

var qFormatInQuotesQuery = "INSERT INTO test (date, args) VALUES"
var qFormatInQuotesValues = "('2019-06-13', 'query=select%20args%20from%20test%20group%20by%20date%20FORMAT%20JSON')"

func BenchmarkCollector_Push(t *testing.B) {
c := NewCollector(&fakeSender{}, 1000, 1000)
for i := 0; i < 30000; i++ {
Expand Down Expand Up @@ -120,6 +123,16 @@ func TestCollector_ParseQuery(t *testing.T) {
params, content, insert = c.ParseQuery(badEscQuery, qValuesTitleUpper+" "+qValuesContent)

assert.False(t, insert)

params, content, insert = c.ParseQuery("", qFormatInQuotesQuery+" "+qFormatInQuotesValues)
assert.Equal(t, "query="+url.QueryEscape(qFormatInQuotesQuery), params)
assert.Equal(t, qFormatInQuotesValues, content)
assert.Equal(t, true, insert)

params, content, insert = c.ParseQuery("query="+url.QueryEscape(qFormatInQuotesQuery+" "+qFormatInQuotesValues), "")
assert.Equal(t, "query="+url.QueryEscape(qFormatInQuotesQuery), params)
assert.Equal(t, qFormatInQuotesValues, content)
assert.Equal(t, true, insert)
}

func TestCollector_separateQuery(t *testing.T) {
Expand Down

0 comments on commit 1ab635c

Please sign in to comment.