Skip to content

Commit

Permalink
fix update map with version (go-xorm#1448)
Browse files Browse the repository at this point in the history
fix test

fix update map with version

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499

fix vet

fix drone lint

remove go1.10 test on drone

Fix update with Alias (go-xorm#1455)

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1448
  • Loading branch information
lunny committed Jan 20, 2020
1 parent c37aff9 commit bd20ffb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
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 @@ -300,21 +300,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 @@ -1404,3 +1404,45 @@ func TestUpdateExprs2(t *testing.T) {
assert.EqualValues(t, 0, uer2.NumCommits)
assert.EqualValues(t, "", uer2.Sha1)
}

func TestUpdateMap3(t *testing.T) {
assert.NoError(t, prepareEngine())

type UpdateMapUser struct {
Id uint64 `xorm:"PK autoincr"`
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)
}

0 comments on commit bd20ffb

Please sign in to comment.