fix arg conversion #61

Merged
lunny merged 1 commits from lunny/fix_arg into master 2019-09-28 05:59:37 +00:00
2 changed files with 6 additions and 9 deletions
Showing only changes of commit d4a504706e - Show all commits

View File

@ -73,20 +73,14 @@ type SeqFilter struct {
func convertQuestionMark(sql, prefix string, start int) string {
var buf strings.Builder
var beginSingleQuote bool
var beginTransfer bool
var index = start
for _, c := range sql {
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
if c == '\'' {
beginSingleQuote = !beginSingleQuote
}
buf.WriteRune(c)
}

View File

@ -28,7 +28,10 @@ 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",
"select '1''?' from issue": "select '1''?' from issue",
"select '1\\??' from issue": "select '1\\??' from issue",
"select '1\\\\',? from issue": "select '1\\\\',$1 from issue",
"select '1\\''?',? from issue": "select '1\\''?',$1 from issue",
}
for sql, result := range kases {
assert.EqualValues(t, result, convertQuestionMark(sql, "$", 1))