Fix question mark conversion bug (#60)

This commit is contained in:
Lunny Xiao 2019-09-28 01:43:44 +00:00 committed by Gitea
parent 0f412da7e9
commit c96a57cc40
2 changed files with 35 additions and 9 deletions

View File

@ -70,15 +70,30 @@ type SeqFilter struct {
Start int Start int
} }
func (s *SeqFilter) Do(sql string, dialect Dialect, table *Table) string { func convertQuestionMark(sql, prefix string, start int) string {
segs := strings.Split(sql, "?") var buf strings.Builder
size := len(segs) var beginSingleQuote bool
res := "" var beginTransfer bool
for i, c := range segs { var index = start
if i < size-1 { for _, c := range sql {
res += c + fmt.Sprintf("%s%v", s.Prefix, i+s.Start) if !beginSingleQuote && c == '?' {
buf.WriteString(fmt.Sprintf("%s%v", prefix, index))
index++
} else {
if c == '\\' {
beginTransfer = true
} else {
if !beginTransfer && c == '\'' {
beginSingleQuote = !beginSingleQuote
}
beginTransfer = false
}
buf.WriteRune(c)
} }
} }
res += segs[size-1] return buf.String()
return res }
func (s *SeqFilter) Do(sql string, dialect Dialect, table *Table) string {
return convertQuestionMark(sql, s.Prefix, s.Start)
} }

View File

@ -23,3 +23,14 @@ func TestQuoteFilter_Do(t *testing.T) {
res, res,
) )
} }
func TestSeqFilter(t *testing.T) {
var kases = map[string]string{
"SELECT * FROM TABLE1 WHERE a=? AND b=?": "SELECT * FROM TABLE1 WHERE a=$1 AND b=$2",
"SELECT 1, '???', '2006-01-02 15:04:05' FROM TABLE1 WHERE a=? AND b=?": "SELECT 1, '???', '2006-01-02 15:04:05' FROM TABLE1 WHERE a=$1 AND b=$2",
"select '1\\'?' from issue": "select '1\\'?' from issue",
}
for sql, result := range kases {
assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1))
}
}