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())
|
assert.NoError(t, prepareEngine())
|
||||||
|
|
||||||
type MailBox struct {
|
type MailBox struct {
|
||||||
Id int64
|
Id int64 `xorm:"pk"`
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
}
|
}
|
||||||
|
@ -27,10 +27,12 @@ func TestCacheFind(t *testing.T) {
|
||||||
|
|
||||||
var inserts = []*MailBox{
|
var inserts = []*MailBox{
|
||||||
{
|
{
|
||||||
|
Id: 0,
|
||||||
Username: "user1",
|
Username: "user1",
|
||||||
Password: "pass1",
|
Password: "pass1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Id: 1,
|
||||||
Username: "user2",
|
Username: "user2",
|
||||||
Password: "pass2",
|
Password: "pass2",
|
||||||
},
|
},
|
||||||
|
@ -63,7 +65,7 @@ func TestCacheFind2(t *testing.T) {
|
||||||
assert.NoError(t, prepareEngine())
|
assert.NoError(t, prepareEngine())
|
||||||
|
|
||||||
type MailBox2 struct {
|
type MailBox2 struct {
|
||||||
Id uint64
|
Id uint64 `xorm:"pk"`
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
}
|
}
|
||||||
|
@ -75,10 +77,12 @@ func TestCacheFind2(t *testing.T) {
|
||||||
|
|
||||||
var inserts = []*MailBox2{
|
var inserts = []*MailBox2{
|
||||||
{
|
{
|
||||||
|
Id: 0,
|
||||||
Username: "user1",
|
Username: "user1",
|
||||||
Password: "pass1",
|
Password: "pass1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Id: 1,
|
||||||
Username: "user2",
|
Username: "user2",
|
||||||
Password: "pass2",
|
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))
|
pk := make([]interface{}, len(table.PrimaryKeys))
|
||||||
for i, col := range table.PKColumns() {
|
for i, col := range table.PKColumns() {
|
||||||
|
var err error
|
||||||
pkField := v.FieldByName(col.FieldName)
|
pkField := v.FieldByName(col.FieldName)
|
||||||
switch pkField.Kind() {
|
switch pkField.Kind() {
|
||||||
case reflect.String:
|
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:
|
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:
|
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
|
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
|
// CreateIndexes create indexes
|
||||||
func (engine *Engine) CreateIndexes(bean interface{}) error {
|
func (engine *Engine) CreateIndexes(bean interface{}) error {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-xorm/builder"
|
"github.com/go-xorm/builder"
|
||||||
|
@ -323,19 +322,11 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var pk core.PK = make([]interface{}, len(table.PrimaryKeys))
|
var pk core.PK = make([]interface{}, len(table.PrimaryKeys))
|
||||||
for i, col := range table.PKColumns() {
|
for i, col := range table.PKColumns() {
|
||||||
if col.SQLType.IsNumeric() {
|
pk[i], err = session.Engine.idTypeAssertion(col, res[i])
|
||||||
n, err := strconv.ParseInt(res[i], 10, 64)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
pk[i] = n
|
|
||||||
} else if col.SQLType.IsText() {
|
|
||||||
pk[i] = res[i]
|
|
||||||
} else {
|
|
||||||
return errors.New("not supported")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue