Fix bug didn't reset statement on update #1939
|
@ -8,6 +8,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"xorm.io/xorm"
|
||||
"xorm.io/xorm/internal/utils"
|
||||
"xorm.io/xorm/names"
|
||||
|
||||
|
@ -1020,3 +1021,28 @@ func TestDistinctAndCols(t *testing.T) {
|
|||
assert.EqualValues(t, 1, len(names))
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -792,7 +792,7 @@ func TestNoUpdate(t *testing.T) {
|
|||
|
||||
_, err = testEngine.ID(1).Update(&NoUpdate{})
|
||||
assert.Error(t, err)
|
||||
assert.EqualValues(t, "No content found to be updated", err.Error())
|
||||
assert.EqualError(t, xorm.ErrNoColumnsTobeUpdated, err.Error())
|
||||
}
|
||||
|
||||
func TestNewUpdate(t *testing.T) {
|
||||
|
@ -922,7 +922,7 @@ func TestDeletedUpdate(t *testing.T) {
|
|||
assert.EqualValues(t, 1, cnt)
|
||||
|
||||
cnt, err = testEngine.ID(s.Id).Cols("deleted_at").Update(&DeletedUpdatedStruct{})
|
||||
assert.EqualValues(t, "No content found to be updated", err.Error())
|
||||
assert.EqualError(t, xorm.ErrNoColumnsTobeUpdated, err.Error())
|
||||
assert.EqualValues(t, 0, cnt)
|
||||
|
||||
cnt, err = testEngine.ID(s.Id).Unscoped().Cols("deleted_at").Update(&DeletedUpdatedStruct{})
|
||||
|
|
|
@ -17,6 +17,11 @@ import (
|
|||
"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 {
|
||||
if table == nil ||
|
||||
session.tx != nil {
|
||||
|
@ -144,6 +149,8 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
|||
defer session.Close()
|
||||
}
|
||||
|
||||
defer session.resetStatement()
|
||||
|
||||
if session.statement.LastError != nil {
|
||||
return 0, session.statement.LastError
|
||||
}
|
||||
|
@ -329,7 +336,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
|||
}
|
||||
|
||||
if len(colNames) <= 0 {
|
||||
return 0, errors.New("No content found to be updated")
|
||||
return 0, ErrNoColumnsTobeUpdated
|
||||
}
|
||||
|
||||
condSQL, condArgs, err = session.statement.GenCondSQL(cond)
|
||||
|
|
Loading…
Reference in New Issue
Block a user