Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Fix insert where with bool bug on mssql #1432

Merged
merged 1 commit into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions session_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions session_insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ func TestInsertWhere(t *testing.T) {
Width uint32
Height uint32
Name string
IsTrue bool
}

assert.NoError(t, prepareEngine())
Expand Down
29 changes: 26 additions & 3 deletions statement_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion test_mssql.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
go test -db=mssql -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test"
go test -db=mssql -conn_str="server=localhost;user id=sa;password=MwantsaSecurePassword1;database=xorm_test"