From 2a0c3e39f22cc840019fb42287d130b9c4cf2609 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 9 Mar 2020 17:59:54 +0800 Subject: [PATCH] AddVar accept writer --- dialects/mssql/mssql.go | 5 +++-- dialects/mysql/mysql.go | 4 ++-- dialects/postgres/postgres.go | 5 +++-- dialects/sqlite/sqlite.go | 4 ++-- interfaces.go | 2 +- statement.go | 5 ++--- tests/dummy_dialecter.go | 4 ++-- 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/dialects/mssql/mssql.go b/dialects/mssql/mssql.go index 0842fa79..8cf1e2e2 100644 --- a/dialects/mssql/mssql.go +++ b/dialects/mssql/mssql.go @@ -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) { diff --git a/dialects/mysql/mysql.go b/dialects/mysql/mysql.go index cff779e3..514dfc14 100644 --- a/dialects/mysql/mysql.go +++ b/dialects/mysql/mysql.go @@ -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) { diff --git a/dialects/postgres/postgres.go b/dialects/postgres/postgres.go index 99569f06..c2ddd82c 100644 --- a/dialects/postgres/postgres.go +++ b/dialects/postgres/postgres.go @@ -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) { diff --git a/dialects/sqlite/sqlite.go b/dialects/sqlite/sqlite.go index 4105863f..c4837463 100644 --- a/dialects/sqlite/sqlite.go +++ b/dialects/sqlite/sqlite.go @@ -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) { diff --git a/interfaces.go b/interfaces.go index 310f801a..9dd00c15 100644 --- a/interfaces.go +++ b/interfaces.go @@ -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 } diff --git a/statement.go b/statement.go index e632b409..6bc8b384 100644 --- a/statement.go +++ b/statement.go @@ -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) @@ -130,7 +130,6 @@ 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 { @@ -138,7 +137,7 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) { } default: stmt.Vars = append(stmt.Vars, v) - writer.WriteString(stmt.DB.Dialector.BindVar(stmt, v)) + stmt.DB.Dialector.BindVarTo(writer, stmt, v) } } } diff --git a/tests/dummy_dialecter.go b/tests/dummy_dialecter.go index f6e9d9f9..63af0c9c 100644 --- a/tests/dummy_dialecter.go +++ b/tests/dummy_dialecter.go @@ -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) {