Add tests for table name (#1517)
add tests for table name Fix test (#1526) Fix test Reviewed-on: https://gitea.com/xorm/xorm/pulls/1526
This commit is contained in:
parent
aefe7fd6da
commit
189e272774
|
@ -215,7 +215,7 @@ func quoteTo(buf *strings.Builder, quotePair string, value string) {
|
||||||
_, _ = buf.WriteString(value)
|
_, _ = buf.WriteString(value)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix, suffix := quotePair[0], quotePair[1]
|
prefix, suffix := quotePair[0], quotePair[1]
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
|
@ -921,7 +921,7 @@ func (engine *Engine) mapType(v reflect.Value) (*core.Table, error) {
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
table := core.NewEmptyTable()
|
table := core.NewEmptyTable()
|
||||||
table.Type = t
|
table.Type = t
|
||||||
table.Name = engine.tbNameForMap(v)
|
table.Name = getTableName(engine.TableMapper, v)
|
||||||
|
|
||||||
var idFieldColName string
|
var idFieldColName string
|
||||||
var hasCacheTag, hasNoCacheTag bool
|
var hasCacheTag, hasNoCacheTag bool
|
||||||
|
|
|
@ -44,20 +44,6 @@ func (session *Session) tbNameNoSchema(table *core.Table) string {
|
||||||
return table.Name
|
return table.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) tbNameForMap(v reflect.Value) string {
|
|
||||||
if v.Type().Implements(tpTableName) {
|
|
||||||
return v.Interface().(TableName).TableName()
|
|
||||||
}
|
|
||||||
if v.Kind() == reflect.Ptr {
|
|
||||||
v = v.Elem()
|
|
||||||
if v.Type().Implements(tpTableName) {
|
|
||||||
return v.Interface().(TableName).TableName()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return engine.TableMapper.Obj2Table(v.Type().Name())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (engine *Engine) tbNameNoSchema(tablename interface{}) string {
|
func (engine *Engine) tbNameNoSchema(tablename interface{}) string {
|
||||||
switch tablename.(type) {
|
switch tablename.(type) {
|
||||||
case []string:
|
case []string:
|
||||||
|
@ -82,7 +68,7 @@ func (engine *Engine) tbNameNoSchema(tablename interface{}) string {
|
||||||
v := rValue(f)
|
v := rValue(f)
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
if t.Kind() == reflect.Struct {
|
if t.Kind() == reflect.Struct {
|
||||||
table = engine.tbNameForMap(v)
|
table = getTableName(engine.TableMapper, v)
|
||||||
} else {
|
} else {
|
||||||
table = engine.Quote(fmt.Sprintf("%v", f))
|
table = engine.Quote(fmt.Sprintf("%v", f))
|
||||||
}
|
}
|
||||||
|
@ -100,12 +86,12 @@ func (engine *Engine) tbNameNoSchema(tablename interface{}) string {
|
||||||
return tablename.(string)
|
return tablename.(string)
|
||||||
case reflect.Value:
|
case reflect.Value:
|
||||||
v := tablename.(reflect.Value)
|
v := tablename.(reflect.Value)
|
||||||
return engine.tbNameForMap(v)
|
return getTableName(engine.TableMapper, v)
|
||||||
default:
|
default:
|
||||||
v := rValue(tablename)
|
v := rValue(tablename)
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
if t.Kind() == reflect.Struct {
|
if t.Kind() == reflect.Struct {
|
||||||
return engine.tbNameForMap(v)
|
return getTableName(engine.TableMapper, v)
|
||||||
}
|
}
|
||||||
return engine.Quote(fmt.Sprintf("%v", tablename))
|
return engine.Quote(fmt.Sprintf("%v", tablename))
|
||||||
}
|
}
|
||||||
|
|
|
@ -480,7 +480,7 @@ type MyGetCustomTableImpletation struct {
|
||||||
|
|
||||||
const getCustomTableName = "GetCustomTableInterface"
|
const getCustomTableName = "GetCustomTableInterface"
|
||||||
|
|
||||||
func (m *MyGetCustomTableImpletation) TableName() string {
|
func (MyGetCustomTableImpletation) TableName() string {
|
||||||
return getCustomTableName
|
return getCustomTableName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
// Copyright 2020 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package xorm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"xorm.io/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getTableName(mapper core.IMapper, v reflect.Value) string {
|
||||||
|
if t, ok := v.Interface().(TableName); ok {
|
||||||
|
return t.TableName()
|
||||||
|
}
|
||||||
|
if v.Type().Implements(tpTableName) {
|
||||||
|
return v.Interface().(TableName).TableName()
|
||||||
|
}
|
||||||
|
if v.Kind() == reflect.Ptr {
|
||||||
|
v = v.Elem()
|
||||||
|
if t, ok := v.Interface().(TableName); ok {
|
||||||
|
return t.TableName()
|
||||||
|
}
|
||||||
|
if v.Type().Implements(tpTableName) {
|
||||||
|
return v.Interface().(TableName).TableName()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapper.Obj2Table(v.Type().Name())
|
||||||
|
}
|
|
@ -0,0 +1,73 @@
|
||||||
|
// Copyright 2020 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package xorm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"xorm.io/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TestTableNameStruct struct{}
|
||||||
|
|
||||||
|
func (t *TestTableNameStruct) TableName() string {
|
||||||
|
return "my_test_table_name_struct"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetTableName(t *testing.T) {
|
||||||
|
var kases = []struct {
|
||||||
|
mapper core.IMapper
|
||||||
|
v reflect.Value
|
||||||
|
expectedTableName string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
core.SnakeMapper{},
|
||||||
|
reflect.ValueOf(new(Userinfo)),
|
||||||
|
"userinfo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
core.SnakeMapper{},
|
||||||
|
reflect.ValueOf(Userinfo{}),
|
||||||
|
"userinfo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
core.SameMapper{},
|
||||||
|
reflect.ValueOf(new(Userinfo)),
|
||||||
|
"Userinfo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
core.SameMapper{},
|
||||||
|
reflect.ValueOf(Userinfo{}),
|
||||||
|
"Userinfo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
core.SnakeMapper{},
|
||||||
|
reflect.ValueOf(new(MyGetCustomTableImpletation)),
|
||||||
|
getCustomTableName,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
core.SnakeMapper{},
|
||||||
|
reflect.ValueOf(MyGetCustomTableImpletation{}),
|
||||||
|
getCustomTableName,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
core.SnakeMapper{},
|
||||||
|
reflect.ValueOf(MyGetCustomTableImpletation{}),
|
||||||
|
getCustomTableName,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
core.SnakeMapper{},
|
||||||
|
reflect.ValueOf(new(TestTableNameStruct)),
|
||||||
|
new(TestTableNameStruct).TableName(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, kase := range kases {
|
||||||
|
assert.EqualValues(t, kase.expectedTableName, getTableName(kase.mapper, kase.v))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue