fix: QuoteTo not fully support raw mode

This commit is contained in:
riverchu 2021-09-29 12:30:09 +08:00
parent c4a2e891da
commit 43fb16a21f

View File

@ -75,30 +75,36 @@ func (stmt *Statement) WriteQuoted(value interface{}) {
// QuoteTo write quoted value to writer // QuoteTo write quoted value to writer
func (stmt *Statement) QuoteTo(writer clause.Writer, field interface{}) { func (stmt *Statement) QuoteTo(writer clause.Writer, field interface{}) {
write := func(raw bool, str string) {
if raw {
writer.WriteString(str)
} else {
stmt.DB.Dialector.QuoteTo(writer, str)
}
}
switch v := field.(type) { switch v := field.(type) {
case clause.Table: case clause.Table:
if v.Name == clause.CurrentTable { if v.Name == clause.CurrentTable {
if stmt.TableExpr != nil { if stmt.TableExpr != nil {
stmt.TableExpr.Build(stmt) stmt.TableExpr.Build(stmt)
} else { } else {
stmt.DB.Dialector.QuoteTo(writer, stmt.Table) write(v.Raw, stmt.Table)
} }
} else if v.Raw {
writer.WriteString(v.Name)
} else { } else {
stmt.DB.Dialector.QuoteTo(writer, v.Name) write(v.Raw, v.Name)
} }
if v.Alias != "" { if v.Alias != "" {
writer.WriteByte(' ') writer.WriteByte(' ')
stmt.DB.Dialector.QuoteTo(writer, v.Alias) write(v.Raw, v.Alias)
} }
case clause.Column: case clause.Column:
if v.Table != "" { if v.Table != "" {
if v.Table == clause.CurrentTable { if v.Table == clause.CurrentTable {
stmt.DB.Dialector.QuoteTo(writer, stmt.Table) write(v.Raw, stmt.Table)
} else { } else {
stmt.DB.Dialector.QuoteTo(writer, v.Table) write(v.Raw, v.Table)
} }
writer.WriteByte('.') writer.WriteByte('.')
} }
@ -107,19 +113,17 @@ func (stmt *Statement) QuoteTo(writer clause.Writer, field interface{}) {
if stmt.Schema == nil { if stmt.Schema == nil {
stmt.DB.AddError(ErrModelValueRequired) stmt.DB.AddError(ErrModelValueRequired)
} else if stmt.Schema.PrioritizedPrimaryField != nil { } else if stmt.Schema.PrioritizedPrimaryField != nil {
stmt.DB.Dialector.QuoteTo(writer, stmt.Schema.PrioritizedPrimaryField.DBName) write(v.Raw, stmt.Schema.PrioritizedPrimaryField.DBName)
} else if len(stmt.Schema.DBNames) > 0 { } else if len(stmt.Schema.DBNames) > 0 {
stmt.DB.Dialector.QuoteTo(writer, stmt.Schema.DBNames[0]) write(v.Raw, stmt.Schema.DBNames[0])
} }
} else if v.Raw {
writer.WriteString(v.Name)
} else { } else {
stmt.DB.Dialector.QuoteTo(writer, v.Name) write(v.Raw, v.Name)
} }
if v.Alias != "" { if v.Alias != "" {
writer.WriteString(" AS ") writer.WriteString(" AS ")
stmt.DB.Dialector.QuoteTo(writer, v.Alias) write(v.Raw, v.Alias)
} }
case []clause.Column: case []clause.Column:
writer.WriteByte('(') writer.WriteByte('(')