2020-02-24 08:53:18 +00: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"
|
|
|
|
|
|
|
|
"xorm.io/xorm/schemas"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Filter is an interface to filter SQL
|
|
|
|
type Filter interface {
|
2020-02-27 03:33:40 +00:00
|
|
|
Do(sql string) string
|
2020-02-24 08:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// QuoteFilter filter SQL replace ` to database's own quote character
|
|
|
|
type QuoteFilter struct {
|
2020-02-27 03:33:40 +00:00
|
|
|
quoter schemas.Quoter
|
2020-02-24 08:53:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 03:33:40 +00:00
|
|
|
func (s *QuoteFilter) Do(sql string) string {
|
|
|
|
if s.quoter.IsEmpty() {
|
2020-02-24 08:53:18 +00:00
|
|
|
return sql
|
|
|
|
}
|
2020-02-25 00:01:36 +00:00
|
|
|
|
2020-02-27 03:33:40 +00:00
|
|
|
prefix, suffix := s.quoter[0][0], s.quoter[1][0]
|
2020-02-24 08:53:18 +00:00
|
|
|
raw := []byte(sql)
|
|
|
|
for i, cnt := 0, 0; i < len(raw); i = i + 1 {
|
|
|
|
if raw[i] == '`' {
|
|
|
|
if cnt%2 == 0 {
|
|
|
|
raw[i] = prefix
|
|
|
|
} else {
|
|
|
|
raw[i] = suffix
|
|
|
|
}
|
|
|
|
cnt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(raw)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
var index = start
|
|
|
|
for _, c := range sql {
|
|
|
|
if !beginSingleQuote && c == '?' {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s%v", prefix, index))
|
|
|
|
index++
|
|
|
|
} else {
|
|
|
|
if c == '\'' {
|
|
|
|
beginSingleQuote = !beginSingleQuote
|
|
|
|
}
|
|
|
|
buf.WriteRune(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2020-02-27 03:33:40 +00:00
|
|
|
func (s *SeqFilter) Do(sql string) string {
|
2020-02-24 08:53:18 +00:00
|
|
|
return convertQuestionMark(sql, s.Prefix, s.Start)
|
|
|
|
}
|