From 59ed80ce1a6748146cf91b032addb3b22c4a3f6d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 24 Sep 2019 16:05:35 +0800 Subject: [PATCH] fix insert where with bool bug on mssql (#1432) --- session_insert.go | 6 +++--- session_insert_test.go | 1 + statement_args.go | 29 ++++++++++++++++++++++++++--- test_mssql.sh | 2 +- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/session_insert.go b/session_insert.go index 44cae7c40..de6452909 100644 --- a/session_insert.go +++ b/session_insert.go @@ -382,7 +382,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) { return 0, err } - if err := writeArgs(buf, args); err != nil { + if err := session.statement.writeArgs(buf, args); err != nil { return 0, err } @@ -739,7 +739,7 @@ func (session *Session) insertMapInterface(m map[string]interface{}) (int64, err return 0, err } - if err := writeArgs(w, args); err != nil { + if err := session.statement.writeArgs(w, args); err != nil { return 0, err } @@ -825,7 +825,7 @@ func (session *Session) insertMapString(m map[string]string) (int64, error) { return 0, err } - if err := writeArgs(w, args); err != nil { + if err := session.statement.writeArgs(w, args); err != nil { return 0, err } diff --git a/session_insert_test.go b/session_insert_test.go index f9c990712..cc3e663b6 100644 --- a/session_insert_test.go +++ b/session_insert_test.go @@ -846,6 +846,7 @@ func TestInsertWhere(t *testing.T) { Width uint32 Height uint32 Name string + IsTrue bool } assert.NoError(t, prepareEngine()) diff --git a/statement_args.go b/statement_args.go index 5353ae1ad..4ce336f48 100644 --- a/statement_args.go +++ b/statement_args.go @@ -8,14 +8,37 @@ import ( "fmt" "xorm.io/builder" + "xorm.io/core" ) -func writeArg(w *builder.BytesWriter, arg interface{}) error { +func (statement *Statement) writeArg(w *builder.BytesWriter, arg interface{}) error { switch argv := arg.(type) { case string: if _, err := w.WriteString("'" + argv + "'"); err != nil { return err } + case bool: + if statement.Engine.dialect.DBType() == core.MSSQL { + if argv { + if _, err := w.WriteString("1"); err != nil { + return err + } + } else { + if _, err := w.WriteString("0"); err != nil { + return err + } + } + } else { + if argv { + if _, err := w.WriteString("true"); err != nil { + return err + } + } else { + if _, err := w.WriteString("false"); err != nil { + return err + } + } + } case *builder.Builder: if _, err := w.WriteString("("); err != nil { return err @@ -34,9 +57,9 @@ func writeArg(w *builder.BytesWriter, arg interface{}) error { return nil } -func writeArgs(w *builder.BytesWriter, args []interface{}) error { +func (statement *Statement) writeArgs(w *builder.BytesWriter, args []interface{}) error { for i, arg := range args { - if err := writeArg(w, arg); err != nil { + if err := statement.writeArg(w, arg); err != nil { return err } diff --git a/test_mssql.sh b/test_mssql.sh index 7f060cff3..e26e16411 100755 --- a/test_mssql.sh +++ b/test_mssql.sh @@ -1 +1 @@ -go test -db=mssql -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" \ No newline at end of file +go test -db=mssql -conn_str="server=localhost;user id=sa;password=MwantsaSecurePassword1;database=xorm_test" \ No newline at end of file