AddVar accept writer

This commit is contained in:
Jinzhu 2020-03-09 17:59:54 +08:00
parent 504f42760a
commit 2a0c3e39f2
7 changed files with 15 additions and 14 deletions

View File

@ -38,8 +38,9 @@ func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
}}} }}}
} }
func (dialector Dialector) BindVar(stmt *gorm.Statement, v interface{}) string { func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
return "@p" + strconv.Itoa(len(stmt.Vars)) writer.WriteString("@p")
writer.WriteString(strconv.Itoa(len(stmt.Vars)))
} }
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) { func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {

View File

@ -36,8 +36,8 @@ func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
}}} }}}
} }
func (dialector Dialector) BindVar(stmt *gorm.Statement, v interface{}) string { func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
return "?" writer.WriteByte('?')
} }
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) { func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {

View File

@ -38,8 +38,9 @@ func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
}}} }}}
} }
func (dialector Dialector) BindVar(stmt *gorm.Statement, v interface{}) string { func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
return "$" + strconv.Itoa(len(stmt.Vars)) writer.WriteByte('$')
writer.WriteString(strconv.Itoa(len(stmt.Vars)))
} }
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) { func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {

View File

@ -35,8 +35,8 @@ func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
}}} }}}
} }
func (dialector Dialector) BindVar(stmt *gorm.Statement, v interface{}) string { func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
return "?" writer.WriteByte('?')
} }
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) { func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {

View File

@ -13,7 +13,7 @@ type Dialector interface {
Initialize(*DB) error Initialize(*DB) error
Migrator(db *DB) Migrator Migrator(db *DB) Migrator
DataTypeOf(*schema.Field) string DataTypeOf(*schema.Field) string
BindVar(stmt *Statement, v interface{}) string BindVarTo(writer clause.Writer, stmt *Statement, v interface{})
QuoteTo(clause.Writer, string) QuoteTo(clause.Writer, string)
Explain(sql string, vars ...interface{}) string Explain(sql string, vars ...interface{}) string
} }

View File

@ -120,7 +120,7 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
writer.WriteString(v.Name) writer.WriteString(v.Name)
} else { } else {
stmt.Vars = append(stmt.Vars, v.Value) stmt.Vars = append(stmt.Vars, v.Value)
writer.WriteString(stmt.DB.Dialector.BindVar(stmt, v.Value)) stmt.DB.Dialector.BindVarTo(writer, stmt, v.Value)
} }
case clause.Column, clause.Table: case clause.Column, clause.Table:
stmt.QuoteTo(writer, v) stmt.QuoteTo(writer, v)
@ -130,7 +130,6 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
case []interface{}: case []interface{}:
if len(v) > 0 { if len(v) > 0 {
writer.WriteByte('(') writer.WriteByte('(')
stmt.skipResetPlacehodler = true
stmt.AddVar(writer, v...) stmt.AddVar(writer, v...)
writer.WriteByte(')') writer.WriteByte(')')
} else { } else {
@ -138,7 +137,7 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
} }
default: default:
stmt.Vars = append(stmt.Vars, v) stmt.Vars = append(stmt.Vars, v)
writer.WriteString(stmt.DB.Dialector.BindVar(stmt, v)) stmt.DB.Dialector.BindVarTo(writer, stmt, v)
} }
} }
} }

View File

@ -18,8 +18,8 @@ func (DummyDialector) Migrator(*gorm.DB) gorm.Migrator {
return nil return nil
} }
func (DummyDialector) BindVar(stmt *gorm.Statement, v interface{}) string { func (DummyDialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
return "?" writer.WriteByte('?')
} }
func (DummyDialector) QuoteTo(writer clause.Writer, str string) { func (DummyDialector) QuoteTo(writer clause.Writer, str string) {