Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
remove QuoteStr() usage in engine.go
Browse files Browse the repository at this point in the history
  • Loading branch information
BetaCat0 committed Jul 22, 2019
1 parent 62d884e commit ffebeae
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func (engine *Engine) SupportInsertMany() bool {

// QuoteStr Engine's database use which character as quote.
// mysql, sqlite use ` and postgres use "
// Deprecated, use Quote() instead
func (engine *Engine) QuoteStr() string {
return engine.dialect.QuoteStr()
}
Expand All @@ -196,13 +197,23 @@ func (engine *Engine) Quote(value string) string {
return value
}

if string(value[0]) == engine.dialect.QuoteStr() || value[0] == '`' {
dummy := engine.dialect.Quote("")
prefix, suffix := dummy[0], dummy[1]

if len(dummy) != 2 || value[0] == prefix || value[0] == '`' { // no quote
return value
} else {
raw := []byte{prefix}
for i := 0; i < len(value); i++ {
if value[i] == '.' {
raw = append(raw, suffix, '.', prefix)
} else {
raw = append(raw, value[i])
}
}
raw = append(raw, suffix)
return string(raw)
}

value = strings.Replace(value, ".", engine.dialect.QuoteStr()+"."+engine.dialect.QuoteStr(), -1)

return engine.dialect.QuoteStr() + value + engine.dialect.QuoteStr()
}

// QuoteTo quotes string and writes into the buffer
Expand All @@ -216,20 +227,29 @@ func (engine *Engine) QuoteTo(buf *builder.StringBuilder, value string) {
return
}

if string(value[0]) == engine.dialect.QuoteStr() || value[0] == '`' {
buf.WriteString(value)
dummy := engine.dialect.Quote("")
prefix, suffix := dummy[0], dummy[1]

if len(dummy) != 2 || value[0] == prefix || value[0] == '`' { // no quote
_, _ = buf.WriteString(value)
return
} else {
_ = buf.WriteByte(prefix)
for i := 0; i < len(value); i++ {
if value[i] == '.' {
_ = buf.WriteByte(suffix)
_ = buf.WriteByte('.')
_ = buf.WriteByte(prefix)
} else {
_ = buf.WriteByte(value[i])
}
}
_ = buf.WriteByte(suffix)
}

value = strings.Replace(value, ".", engine.dialect.QuoteStr()+"."+engine.dialect.QuoteStr(), -1)

buf.WriteString(engine.dialect.QuoteStr())
buf.WriteString(value)
buf.WriteString(engine.dialect.QuoteStr())
}

func (engine *Engine) quote(sql string) string {
return engine.dialect.QuoteStr() + sql + engine.dialect.QuoteStr()
return engine.dialect.Quote(sql)
}

// SqlType will be deprecated, please use SQLType instead
Expand Down Expand Up @@ -1581,7 +1601,7 @@ func (engine *Engine) formatColTime(col *core.Column, t time.Time) (v interface{
func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{}) {
switch sqlTypeName {
case core.Time:
s := t.Format("2006-01-02 15:04:05") //time.RFC3339
s := t.Format("2006-01-02 15:04:05") // time.RFC3339
v = s[11:19]
case core.Date:
v = t.Format("2006-01-02")
Expand Down

0 comments on commit ffebeae

Please sign in to comment.