Ignore comments when deciding when to replace quetion marks.
This commit is contained in:
parent
bc25b4128b
commit
898e7b6fb5
|
@ -23,13 +23,45 @@ type SeqFilter struct {
|
||||||
func convertQuestionMark(sql, prefix string, start int) string {
|
func convertQuestionMark(sql, prefix string, start int) string {
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
var beginSingleQuote bool
|
var beginSingleQuote bool
|
||||||
|
var isLineComment bool
|
||||||
|
var isComment bool
|
||||||
|
var isMaybeLineComment bool
|
||||||
|
var isMaybeComment bool
|
||||||
|
var isMaybeCommentEnd bool
|
||||||
var index = start
|
var index = start
|
||||||
for _, c := range sql {
|
for _, c := range sql {
|
||||||
if !beginSingleQuote && c == '?' {
|
if !beginSingleQuote && !isLineComment && !isComment && c == '?' {
|
||||||
buf.WriteString(fmt.Sprintf("%s%v", prefix, index))
|
buf.WriteString(fmt.Sprintf("%s%v", prefix, index))
|
||||||
index++
|
index++
|
||||||
} else {
|
} else {
|
||||||
if c == '\'' {
|
if isMaybeLineComment {
|
||||||
|
if c == '-' {
|
||||||
|
isLineComment = true
|
||||||
|
}
|
||||||
|
isMaybeLineComment = false
|
||||||
|
} else if isMaybeComment {
|
||||||
|
if c == '*' {
|
||||||
|
isComment = true
|
||||||
|
}
|
||||||
|
isMaybeComment = false
|
||||||
|
} else if isMaybeCommentEnd {
|
||||||
|
if c == '/' {
|
||||||
|
isComment = false
|
||||||
|
}
|
||||||
|
isMaybeCommentEnd = false
|
||||||
|
} else if isLineComment {
|
||||||
|
if c == '\n' {
|
||||||
|
isLineComment = false
|
||||||
|
}
|
||||||
|
} else if isComment {
|
||||||
|
if c == '*' {
|
||||||
|
isMaybeCommentEnd = true
|
||||||
|
}
|
||||||
|
} else if !beginSingleQuote && c == '-' {
|
||||||
|
isMaybeLineComment = true
|
||||||
|
} else if !beginSingleQuote && c == '/' {
|
||||||
|
isMaybeComment = true
|
||||||
|
} else if c == '\'' {
|
||||||
beginSingleQuote = !beginSingleQuote
|
beginSingleQuote = !beginSingleQuote
|
||||||
}
|
}
|
||||||
buf.WriteRune(c)
|
buf.WriteRune(c)
|
||||||
|
|
|
@ -19,3 +19,60 @@ func TestSeqFilter(t *testing.T) {
|
||||||
assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1))
|
assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSeqFilterLineComment(t *testing.T) {
|
||||||
|
var kases = map[string]string{
|
||||||
|
`SELECT *
|
||||||
|
FROM TABLE1
|
||||||
|
WHERE foo='bar'
|
||||||
|
AND a=? -- it's a comment
|
||||||
|
AND b=?`: `SELECT *
|
||||||
|
FROM TABLE1
|
||||||
|
WHERE foo='bar'
|
||||||
|
AND a=$1 -- it's a comment
|
||||||
|
AND b=$2`,
|
||||||
|
`SELECT *
|
||||||
|
FROM TABLE1
|
||||||
|
WHERE foo='bar'
|
||||||
|
AND a=? -- it's a comment?
|
||||||
|
AND b=?`: `SELECT *
|
||||||
|
FROM TABLE1
|
||||||
|
WHERE foo='bar'
|
||||||
|
AND a=$1 -- it's a comment?
|
||||||
|
AND b=$2`,
|
||||||
|
`SELECT *
|
||||||
|
FROM TABLE1
|
||||||
|
WHERE a=? -- it's a comment? and that's okay?
|
||||||
|
AND b=?`: `SELECT *
|
||||||
|
FROM TABLE1
|
||||||
|
WHERE a=$1 -- it's a comment? and that's okay?
|
||||||
|
AND b=$2`,
|
||||||
|
}
|
||||||
|
for sql, result := range kases {
|
||||||
|
assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeqFilterComment(t *testing.T) {
|
||||||
|
var kases = map[string]string{
|
||||||
|
`SELECT *
|
||||||
|
FROM TABLE1
|
||||||
|
WHERE a=? /* it's a comment */
|
||||||
|
AND b=?`: `SELECT *
|
||||||
|
FROM TABLE1
|
||||||
|
WHERE a=$1 /* it's a comment */
|
||||||
|
AND b=$2`,
|
||||||
|
`SELECT /* it's a comment * ?
|
||||||
|
More comment on the next line! */ *
|
||||||
|
FROM TABLE1
|
||||||
|
WHERE a=? /**/
|
||||||
|
AND b=?`: `SELECT /* it's a comment * ?
|
||||||
|
More comment on the next line! */ *
|
||||||
|
FROM TABLE1
|
||||||
|
WHERE a=$1 /**/
|
||||||
|
AND b=$2`,
|
||||||
|
}
|
||||||
|
for sql, result := range kases {
|
||||||
|
assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue