Topic/fix cache uint (#635)
* modify cache_test for issues #330 * fix bugs cache key when pk is defined as uint (#330) * add comment
This commit is contained in:
parent
32d688fd96
commit
39a812d59d
|
@ -15,7 +15,7 @@ func TestCacheFind(t *testing.T) {
|
|||
assert.NoError(t, prepareEngine())
|
||||
|
||||
type MailBox struct {
|
||||
Id int64
|
||||
Id int64 `xorm:"pk"`
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
@ -27,10 +27,12 @@ func TestCacheFind(t *testing.T) {
|
|||
|
||||
var inserts = []*MailBox{
|
||||
{
|
||||
Id: 0,
|
||||
Username: "user1",
|
||||
Password: "pass1",
|
||||
},
|
||||
{
|
||||
Id: 1,
|
||||
Username: "user2",
|
||||
Password: "pass2",
|
||||
},
|
||||
|
@ -63,7 +65,7 @@ func TestCacheFind2(t *testing.T) {
|
|||
assert.NoError(t, prepareEngine())
|
||||
|
||||
type MailBox2 struct {
|
||||
Id uint64
|
||||
Id uint64 `xorm:"pk"`
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
@ -75,10 +77,12 @@ func TestCacheFind2(t *testing.T) {
|
|||
|
||||
var inserts = []*MailBox2{
|
||||
{
|
||||
Id: 0,
|
||||
Username: "user1",
|
||||
Password: "pass1",
|
||||
},
|
||||
{
|
||||
Id: 1,
|
||||
Username: "user2",
|
||||
Password: "pass2",
|
||||
},
|
||||
|
|
26
engine.go
26
engine.go
|
@ -1104,19 +1104,39 @@ func (engine *Engine) idOfV(rv reflect.Value) (core.PK, error) {
|
|||
|
||||
pk := make([]interface{}, len(table.PrimaryKeys))
|
||||
for i, col := range table.PKColumns() {
|
||||
var err error
|
||||
pkField := v.FieldByName(col.FieldName)
|
||||
switch pkField.Kind() {
|
||||
case reflect.String:
|
||||
pk[i] = pkField.String()
|
||||
pk[i], err = engine.idTypeAssertion(col, pkField.String())
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
pk[i] = pkField.Int()
|
||||
pk[i], err = engine.idTypeAssertion(col, strconv.FormatInt(pkField.Int(), 10))
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
pk[i] = pkField.Uint()
|
||||
// id of uint will be converted to int64
|
||||
pk[i], err = engine.idTypeAssertion(col, strconv.FormatUint(pkField.Uint(), 10))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return core.PK(pk), nil
|
||||
}
|
||||
|
||||
func (engine *Engine) idTypeAssertion(col *core.Column, sid string) (interface{}, error) {
|
||||
if col.SQLType.IsNumeric() {
|
||||
n, err := strconv.ParseInt(sid, 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return n, nil
|
||||
} else if col.SQLType.IsText() {
|
||||
return sid, nil
|
||||
} else {
|
||||
return nil, errors.New("not supported")
|
||||
}
|
||||
}
|
||||
|
||||
// CreateIndexes create indexes
|
||||
func (engine *Engine) CreateIndexes(bean interface{}) error {
|
||||
session := engine.NewSession()
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-xorm/builder"
|
||||
|
@ -323,20 +322,12 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var pk core.PK = make([]interface{}, len(table.PrimaryKeys))
|
||||
for i, col := range table.PKColumns() {
|
||||
if col.SQLType.IsNumeric() {
|
||||
n, err := strconv.ParseInt(res[i], 10, 64)
|
||||
pk[i], err = session.Engine.idTypeAssertion(col, res[i])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pk[i] = n
|
||||
} else if col.SQLType.IsText() {
|
||||
pk[i] = res[i]
|
||||
} else {
|
||||
return errors.New("not supported")
|
||||
}
|
||||
}
|
||||
|
||||
ids = append(ids, pk)
|
||||
|
|
Loading…
Reference in New Issue