Fix SetExprs

This commit is contained in:
Lunny Xiao 2021-06-09 11:42:30 +08:00
parent 7864cb5ae6
commit 4b57901c42
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 44 additions and 1 deletions

View File

@ -6,6 +6,7 @@ package integrations
import ( import (
"fmt" "fmt"
"strings"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -1414,3 +1415,43 @@ func TestNilFromDB(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, 1, cnt) assert.EqualValues(t, 1, cnt)
} }
func TestUpdateSetExprs2(t *testing.T) {
type TblPassiveHost struct {
Id int64 `xorm:"not null pk autoincr INT(11)"`
Host string `xorm:"varchar(255) not null unique"`
Backend string `xorm:"text"`
Status uint32 `xorm:"not null tinyint default 0"`
RecentCount int64 `xorm:"bigint default 0"`
TotalCount int64 `xorm:"bigint default 0"`
UrlCount int64 `xorm:"bigint default 0"` //number of urls
VulnCount int64 `xorm:"bigint default 0"` //number of vulns
Atime time.Time `xorm:"DATETIME created"`
Utime time.Time `xorm:"DATETIME updated"`
}
assert.NoError(t, PrepareEngine())
assertSync(t, new(TblPassiveHost))
var b = TblPassiveHost{
Host: "192.168.1.1",
Backend: "linux",
Status: 1,
RecentCount: 2,
TotalCount: 3,
UrlCount: 4,
VulnCount: 5,
}
cnt, err := testEngine.Insert(&b)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var passiveHost = TblPassiveHost{}
_, err = testEngine.Where(`host=?`, b.Host).
Incr(`total_count`, 2).
SetExpr(`recent_count`, 2).
SetExpr(`backend`, strings.Join([]string{"192.168.2.1", "192.168.3.1"}, ",")).Update(&passiveHost)
assert.NoError(t, err)
}

View File

@ -241,8 +241,10 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
case string: case string:
if len(tp) == 0 { if len(tp) == 0 {
tp = "''" tp = "''"
} else {
tp = strings.Replace(tp, "'", "''", -1)
} }
colNames = append(colNames, session.engine.Quote(expr.ColName)+"="+tp) colNames = append(colNames, session.engine.Quote(expr.ColName)+"='"+tp+"'")
case *builder.Builder: case *builder.Builder:
subQuery, subArgs, err := session.statement.GenCondSQL(tp) subQuery, subArgs, err := session.statement.GenCondSQL(tp)
if err != nil { if err != nil {