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 setexpr missing big quotes #1431

Merged
merged 3 commits 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
35 changes: 31 additions & 4 deletions session_cols_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,38 @@ package xorm
import (
"testing"

"xorm.io/core"
"github.com/stretchr/testify/assert"
"xorm.io/builder"
"xorm.io/core"
)

func TestSetExpr(t *testing.T) {
assert.NoError(t, prepareEngine())

type UserExprIssue struct {
Id int64
Title string
}

assert.NoError(t, testEngine.Sync2(new(UserExprIssue)))

var issue = UserExprIssue{
Title: "my issue",
}
cnt, err := testEngine.Insert(&issue)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
assert.EqualValues(t, 1, issue.Id)

type UserExpr struct {
Id int64
Show bool
Id int64
IssueId int64 `xorm:"index"`
Show bool
}

assert.NoError(t, testEngine.Sync2(new(UserExpr)))

cnt, err := testEngine.Insert(&UserExpr{
cnt, err = testEngine.Insert(&UserExpr{
Show: true,
})
assert.NoError(t, err)
Expand All @@ -34,6 +51,16 @@ func TestSetExpr(t *testing.T) {
cnt, err = testEngine.SetExpr("show", not+" `show`").ID(1).Update(new(UserExpr))
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

tableName := testEngine.TableName(new(UserExprIssue), true)
cnt, err = testEngine.SetExpr("issue_id",
builder.Select("id").
From(tableName).
Where(builder.Eq{"id": issue.Id})).
ID(1).
Update(new(UserExpr))
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
}

func TestCols(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion session_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
if err != nil {
return 0, err
}
colNames = append(colNames, session.engine.Quote(colName)+" = "+subQuery)
colNames = append(colNames, session.engine.Quote(colName)+" = ("+subQuery+")")
args = append(args, subArgs...)
}
}
Expand Down
6 changes: 6 additions & 0 deletions statement_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ func writeArg(w *builder.BytesWriter, arg interface{}) error {
return err
}
case *builder.Builder:
if _, err := w.WriteString("("); err != nil {
return err
}
if err := argv.WriteTo(w); err != nil {
return err
}
if _, err := w.WriteString(")"); err != nil {
return err
}
default:
if _, err := w.WriteString(fmt.Sprintf("%v", argv)); err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions statement_exprparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,15 @@ func (exprs *exprParams) writeArgs(w *builder.BytesWriter) error {
for _, expr := range exprs.args {
switch arg := expr.(type) {
case *builder.Builder:
if _, err := w.WriteString("("); err != nil {
return err
}
if err := arg.WriteTo(w); err != nil {
return err
}
if _, err := w.WriteString(")"); err != nil {
return err
}
default:
if _, err := w.WriteString(fmt.Sprintf("%v", arg)); err != nil {
return err
Expand All @@ -83,9 +89,15 @@ func (exprs *exprParams) writeNameArgs(w *builder.BytesWriter) error {

switch arg := exprs.args[i].(type) {
case *builder.Builder:
if _, err := w.WriteString("("); err != nil {
return err
}
if err := arg.WriteTo(w); err != nil {
return err
}
if _, err := w.WriteString("("); err != nil {
return err
}
default:
w.Append(exprs.args[i])
}
Expand Down