2020-11-25 20:36:07 +01:00
|
|
|
// Copyright 2019 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package dialects
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Filter is an interface to filter SQL
|
|
|
|
type Filter interface {
|
|
|
|
Do(sql string) string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SeqFilter filter SQL replace ?, ? ... to $1, $2 ...
|
|
|
|
type SeqFilter struct {
|
|
|
|
Prefix string
|
|
|
|
Start int
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertQuestionMark(sql, prefix string, start int) string {
|
|
|
|
var buf strings.Builder
|
|
|
|
var beginSingleQuote bool
|
2021-08-30 20:04:15 +02:00
|
|
|
var isLineComment bool
|
|
|
|
var isComment bool
|
|
|
|
var isMaybeLineComment bool
|
|
|
|
var isMaybeComment bool
|
|
|
|
var isMaybeCommentEnd bool
|
2020-11-25 20:36:07 +01:00
|
|
|
var index = start
|
|
|
|
for _, c := range sql {
|
2021-08-30 20:04:15 +02:00
|
|
|
if !beginSingleQuote && !isLineComment && !isComment && c == '?' {
|
2020-11-25 20:36:07 +01:00
|
|
|
buf.WriteString(fmt.Sprintf("%s%v", prefix, index))
|
|
|
|
index++
|
|
|
|
} else {
|
2021-08-30 20:04:15 +02:00
|
|
|
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 == '\'' {
|
2020-11-25 20:36:07 +01:00
|
|
|
beginSingleQuote = !beginSingleQuote
|
|
|
|
}
|
|
|
|
buf.WriteRune(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2021-08-30 20:04:15 +02:00
|
|
|
// Do implements Filter
|
2020-11-25 20:36:07 +01:00
|
|
|
func (s *SeqFilter) Do(sql string) string {
|
|
|
|
return convertQuestionMark(sql, s.Prefix, s.Start)
|
|
|
|
}
|