From e3cf77c60f703b85452f88556495a01568df64db Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 2 Oct 2019 13:11:49 +0800 Subject: [PATCH] fix update map with version --- interface.go | 2 ++ session_update.go | 22 +++++++++++++--------- session_update_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/interface.go b/interface.go index a564db126..81a4b68a3 100644 --- a/interface.go +++ b/interface.go @@ -92,6 +92,7 @@ type EngineInterface interface { Quote(string) string SetCacher(string, core.Cacher) SetConnMaxLifetime(time.Duration) + SetColumnMapper(core.IMapper) SetDefaultCacher(core.Cacher) SetLogger(logger core.ILogger) SetLogLevel(core.LogLevel) @@ -99,6 +100,7 @@ type EngineInterface interface { SetMaxOpenConns(int) SetMaxIdleConns(int) SetSchema(string) + SetTableMapper(core.IMapper) SetTZDatabase(tz *time.Location) SetTZLocation(tz *time.Location) ShowExecTime(...bool) diff --git a/session_update.go b/session_update.go index c5c65a452..4c04badf5 100644 --- a/session_update.go +++ b/session_update.go @@ -294,21 +294,25 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 st := &session.statement - var sqlStr string - var condArgs []interface{} - var condSQL string - cond := session.statement.cond.And(autoCond) - - var doIncVer = (table != nil && table.Version != "" && session.statement.checkVersion) - var verValue *reflect.Value + var ( + sqlStr string + condArgs []interface{} + condSQL string + cond = session.statement.cond.And(autoCond) + + doIncVer = isStruct && (table != nil && table.Version != "" && session.statement.checkVersion) + verValue *reflect.Value + ) if doIncVer { verValue, err = table.VersionColumn().ValueOf(bean) if err != nil { return 0, err } - cond = cond.And(builder.Eq{session.engine.Quote(table.Version): verValue.Interface()}) - colNames = append(colNames, session.engine.Quote(table.Version)+" = "+session.engine.Quote(table.Version)+" + 1") + if verValue != nil { + cond = cond.And(builder.Eq{session.engine.Quote(table.Version): verValue.Interface()}) + colNames = append(colNames, session.engine.Quote(table.Version)+" = "+session.engine.Quote(table.Version)+" + 1") + } } condSQL, condArgs, err = builder.ToSQL(cond) diff --git a/session_update_test.go b/session_update_test.go index e6c88bf8c..4311789e3 100644 --- a/session_update_test.go +++ b/session_update_test.go @@ -1421,3 +1421,45 @@ func TestUpdateExprs(t *testing.T) { assert.EqualValues(t, 2, ue.NumIssues) assert.EqualValues(t, "lunny xiao", ue.Name) } + +func TestUpdateMap3(t *testing.T) { + assert.NoError(t, prepareEngine()) + + type UpdateMapUser struct { + Id uint64 `xorm:"PK"` + Name string `xorm:""` + Ver uint64 `xorm:"version"` + } + + oldMapper := testEngine.GetColumnMapper() + defer func() { + testEngine.SetColumnMapper(oldMapper) + }() + + mapper := core.NewPrefixMapper(core.SnakeMapper{}, "F") + testEngine.SetColumnMapper(mapper) + + assertSync(t, new(UpdateMapUser)) + + _, err := testEngine.Table(new(UpdateMapUser)).Insert(map[string]interface{}{ + "Fname": "first user name", + "Fver": 1, + }) + assert.NoError(t, err) + + update := map[string]interface{}{ + "Fname": "user name", + "Fver": 1, + } + rows, err := testEngine.Table(new(UpdateMapUser)).ID(1).Update(update) + assert.NoError(t, err) + assert.EqualValues(t, 1, rows) + + update = map[string]interface{}{ + "Name": "user name", + "Ver": 1, + } + rows, err = testEngine.Table(new(UpdateMapUser)).ID(1).Update(update) + assert.Error(t, err) + assert.EqualValues(t, 0, rows) +}