Fix bug didn't reset statement on update

This commit is contained in:
Lunny Xiao 2021-06-09 11:32:38 +08:00
parent 5b624ed051
commit 54ec855eca
2 changed files with 34 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import (
"testing" "testing"
"time" "time"
"xorm.io/xorm"
"xorm.io/xorm/internal/utils" "xorm.io/xorm/internal/utils"
"xorm.io/xorm/names" "xorm.io/xorm/names"
@ -1020,3 +1021,28 @@ func TestDistinctAndCols(t *testing.T) {
assert.EqualValues(t, 1, len(names)) assert.EqualValues(t, 1, len(names))
assert.EqualValues(t, "test", names[0]) assert.EqualValues(t, "test", names[0])
} }
func TestUpdateFind(t *testing.T) {
type TestUpdateFind struct {
Id int64
Name string
}
assert.NoError(t, PrepareEngine())
assertSync(t, new(TestUpdateFind))
session := testEngine.NewSession()
defer session.Close()
var tuf = TestUpdateFind{
Name: "test",
}
_, err := session.Insert(&tuf)
assert.NoError(t, err)
_, err = session.Where("id = ?", tuf.Id).Update(&TestUpdateFind{})
assert.EqualError(t, xorm.ErrNoColumnsTobeUpdated, err.Error())
var tufs []TestUpdateFind
err = session.Where("id = ?", tuf.Id).Find(&tufs)
assert.NoError(t, err)
}

View File

@ -17,6 +17,11 @@ import (
"xorm.io/xorm/schemas" "xorm.io/xorm/schemas"
) )
// enumerated all errors
var (
ErrNoColumnsTobeUpdated = errors.New("no columns found to be updated")
)
func (session *Session) cacheUpdate(table *schemas.Table, tableName, sqlStr string, args ...interface{}) error { func (session *Session) cacheUpdate(table *schemas.Table, tableName, sqlStr string, args ...interface{}) error {
if table == nil || if table == nil ||
session.tx != nil { session.tx != nil {
@ -144,6 +149,8 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
defer session.Close() defer session.Close()
} }
defer session.resetStatement()
if session.statement.LastError != nil { if session.statement.LastError != nil {
return 0, session.statement.LastError return 0, session.statement.LastError
} }
@ -329,7 +336,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
} }
if len(colNames) <= 0 { if len(colNames) <= 0 {
return 0, errors.New("No content found to be updated") return 0, ErrNoColumnsTobeUpdated
} }
condSQL, condArgs, err = session.statement.GenCondSQL(cond) condSQL, condArgs, err = session.statement.GenCondSQL(cond)