Browse Source

Add version uint type support (#1125)

* fix panic when use version with uint32 type

* fix panic when use version with uint32 type

* Code refactoring
tags/v.0.7.3
mars Lunny Xiao <xiaolunwen@gmail.com> 1 year ago
parent
commit
3add351809
4 changed files with 129 additions and 6 deletions
  1. +9
    -0
      session.go
  2. +3
    -3
      session_insert.go
  3. +2
    -2
      session_update.go
  4. +115
    -1
      tag_version_test.go

+ 9
- 0
session.go View File

@@ -845,3 +845,12 @@ func (session *Session) Unscoped() *Session {
session.statement.Unscoped()
return session
}

func (session *Session) incrVersionFieldValue(fieldValue *reflect.Value) {
switch fieldValue.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue.SetInt(fieldValue.Int() + 1)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fieldValue.SetUint(fieldValue.Uint() + 1)
}
}

+ 3
- 3
session_insert.go View File

@@ -397,7 +397,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
if err != nil {
session.engine.logger.Error(err)
} else if verValue.IsValid() && verValue.CanSet() {
verValue.SetInt(1)
session.incrVersionFieldValue(verValue)
}
}

@@ -440,7 +440,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
if err != nil {
session.engine.logger.Error(err)
} else if verValue.IsValid() && verValue.CanSet() {
verValue.SetInt(1)
session.incrVersionFieldValue(verValue)
}
}

@@ -481,7 +481,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
if err != nil {
session.engine.logger.Error(err)
} else if verValue.IsValid() && verValue.CanSet() {
verValue.SetInt(1)
session.incrVersionFieldValue(verValue)
}
}



+ 2
- 2
session_update.go View File

@@ -116,7 +116,7 @@ func (session *Session) cacheUpdate(table *core.Table, tableName, sqlStr string,
} else {
session.engine.logger.Debug("[cacheUpdate] set bean field", bean, colName, fieldValue.Interface())
if col.IsVersion && session.statement.checkVersion {
fieldValue.SetInt(fieldValue.Int() + 1)
session.incrVersionFieldValue(fieldValue)
} else {
fieldValue.Set(reflect.ValueOf(args[idx]))
}
@@ -357,7 +357,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
return 0, err
} else if doIncVer {
if verValue != nil && verValue.IsValid() && verValue.CanSet() {
verValue.SetInt(verValue.Int() + 1)
session.incrVersionFieldValue(verValue)
}
}



+ 115
- 1
tag_version_test.go View File

@@ -85,7 +85,7 @@ func TestVersion1(t *testing.T) {
}
fmt.Println(newVer)
if newVer.Ver != 2 {
err = errors.New("insert error")
err = errors.New("update error")
t.Error(err)
panic(err)
}
@@ -126,3 +126,117 @@ func TestVersion2(t *testing.T) {
}
}
}

type VersionUintS struct {
Id int64
Name string
Ver uint `xorm:"version"`
Created time.Time `xorm:"created"`
}

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

err := testEngine.DropTables(new(VersionUintS))
if err != nil {
t.Error(err)
panic(err)
}

err = testEngine.CreateTables(new(VersionUintS))
if err != nil {
t.Error(err)
panic(err)
}

ver := &VersionUintS{Name: "sfsfdsfds"}
_, err = testEngine.Insert(ver)
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(ver)
if ver.Ver != 1 {
err = errors.New("insert error")
t.Error(err)
panic(err)
}

newVer := new(VersionUintS)
has, err := testEngine.ID(ver.Id).Get(newVer)
if err != nil {
t.Error(err)
panic(err)
}

if !has {
t.Error(errors.New(fmt.Sprintf("no version id is %v", ver.Id)))
panic(err)
}
fmt.Println(newVer)
if newVer.Ver != 1 {
err = errors.New("insert error")
t.Error(err)
panic(err)
}

newVer.Name = "-------"
_, err = testEngine.ID(ver.Id).Update(newVer)
if err != nil {
t.Error(err)
panic(err)
}
if newVer.Ver != 2 {
err = errors.New("update should set version back to struct")
t.Error(err)
}

newVer = new(VersionUintS)
has, err = testEngine.ID(ver.Id).Get(newVer)
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(newVer)
if newVer.Ver != 2 {
err = errors.New("update error")
t.Error(err)
panic(err)
}
}

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

err := testEngine.DropTables(new(VersionUintS))
if err != nil {
t.Error(err)
panic(err)
}

err = testEngine.CreateTables(new(VersionUintS))
if err != nil {
t.Error(err)
panic(err)
}

var vers = []VersionUintS{
{Name: "sfsfdsfds"},
{Name: "xxxxx"},
}
_, err = testEngine.Insert(vers)
if err != nil {
t.Error(err)
panic(err)
}

fmt.Println(vers)

for _, v := range vers {
if v.Ver != 1 {
err := errors.New("version should be 1")
t.Error(err)
panic(err)
}
}
}

Loading…
Cancel
Save