Skip to content

Commit

Permalink
AddVar accept writer
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Mar 9, 2020
1 parent 504f427 commit 2a0c3e3
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 14 deletions.
5 changes: 3 additions & 2 deletions dialects/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
}}}
}

func (dialector Dialector) BindVar(stmt *gorm.Statement, v interface{}) string {
return "@p" + strconv.Itoa(len(stmt.Vars))
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
writer.WriteString("@p")
writer.WriteString(strconv.Itoa(len(stmt.Vars)))
}

func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
Expand Down
4 changes: 2 additions & 2 deletions dialects/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
}}}
}

func (dialector Dialector) BindVar(stmt *gorm.Statement, v interface{}) string {
return "?"
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
writer.WriteByte('?')
}

func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
Expand Down
5 changes: 3 additions & 2 deletions dialects/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
}}}
}

func (dialector Dialector) BindVar(stmt *gorm.Statement, v interface{}) string {
return "$" + strconv.Itoa(len(stmt.Vars))
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
writer.WriteByte('$')
writer.WriteString(strconv.Itoa(len(stmt.Vars)))
}

func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
Expand Down
4 changes: 2 additions & 2 deletions dialects/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
}}}
}

func (dialector Dialector) BindVar(stmt *gorm.Statement, v interface{}) string {
return "?"
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
writer.WriteByte('?')
}

func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
Expand Down
2 changes: 1 addition & 1 deletion interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Dialector interface {
Initialize(*DB) error
Migrator(db *DB) Migrator
DataTypeOf(*schema.Field) string
BindVar(stmt *Statement, v interface{}) string
BindVarTo(writer clause.Writer, stmt *Statement, v interface{})
QuoteTo(clause.Writer, string)
Explain(sql string, vars ...interface{}) string
}
Expand Down
5 changes: 2 additions & 3 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
writer.WriteString(v.Name)
} else {
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:
stmt.QuoteTo(writer, v)
Expand All @@ -130,15 +130,14 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
case []interface{}:
if len(v) > 0 {
writer.WriteByte('(')
stmt.skipResetPlacehodler = true
stmt.AddVar(writer, v...)
writer.WriteByte(')')
} else {
writer.WriteString("(NULL)")
}
default:
stmt.Vars = append(stmt.Vars, v)
writer.WriteString(stmt.DB.Dialector.BindVar(stmt, v))
stmt.DB.Dialector.BindVarTo(writer, stmt, v)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/dummy_dialecter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func (DummyDialector) Migrator(*gorm.DB) gorm.Migrator {
return nil
}

func (DummyDialector) BindVar(stmt *gorm.Statement, v interface{}) string {
return "?"
func (DummyDialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
writer.WriteByte('?')
}

func (DummyDialector) QuoteTo(writer clause.Writer, str string) {
Expand Down

0 comments on commit 2a0c3e3

Please sign in to comment.