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 update map with version #1448

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ 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)
SetMapper(core.IMapper)
SetMaxOpenConns(int)
SetMaxIdleConns(int)
SetSchema(string)
SetTableMapper(core.IMapper)
SetTZDatabase(tz *time.Location)
SetTZLocation(tz *time.Location)
ShowExecTime(...bool)
Expand Down
22 changes: 13 additions & 9 deletions session_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions session_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}