Refactor dummy driver QuoteTo method

This commit is contained in:
Jinzhu 2021-09-11 16:22:35 +08:00
parent 04f049c1da
commit d41fb3acdc
1 changed files with 45 additions and 3 deletions

View File

@ -31,9 +31,51 @@ func (DummyDialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v in
} }
func (DummyDialector) QuoteTo(writer clause.Writer, str string) { func (DummyDialector) QuoteTo(writer clause.Writer, str string) {
var (
underQuoted, selfQuoted bool
continuousBacktick int8
shiftDelimiter int8
)
for _, v := range []byte(str) {
switch v {
case '`':
continuousBacktick++
if continuousBacktick == 2 {
writer.WriteString("``")
continuousBacktick = 0
}
case '.':
if continuousBacktick > 0 || !selfQuoted {
shiftDelimiter = 0
underQuoted = false
continuousBacktick = 0
writer.WriteString("`")
}
writer.WriteByte(v)
continue
default:
if shiftDelimiter-continuousBacktick <= 0 && !underQuoted {
writer.WriteByte('`') writer.WriteByte('`')
writer.WriteString(str) underQuoted = true
writer.WriteByte('`') if selfQuoted = continuousBacktick > 0; selfQuoted {
continuousBacktick -= 1
}
}
for ; continuousBacktick > 0; continuousBacktick -= 1 {
writer.WriteString("``")
}
writer.WriteByte(v)
}
shiftDelimiter++
}
if continuousBacktick > 0 && !selfQuoted {
writer.WriteString("``")
}
writer.WriteString("`")
} }
func (DummyDialector) Explain(sql string, vars ...interface{}) string { func (DummyDialector) Explain(sql string, vars ...interface{}) string {