Browse Source

fix update map with version (#1448)

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: #1499

fix vet

fix drone lint

remove go1.10 test on drone

Fix update with Alias (#1455)

Reviewed-on: #1448
tags/v0.8.2
Lunny Xiao 1 month ago
parent
commit
bd20ffba3b
3 changed files with 57 additions and 9 deletions
  1. +2
    -0
      interface.go
  2. +13
    -9
      session_update.go
  3. +42
    -0
      session_update_test.go

+ 2
- 0
interface.go View File

@@ -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)


+ 13
- 9
session_update.go View File

@@ -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)


+ 42
- 0
session_update_test.go View File

@@ -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)
}

Loading…
Cancel
Save