Add tests for table name #1517
|
@ -215,7 +215,7 @@ func quoteTo(buf *strings.Builder, quotePair string, value string) {
|
|||
_, _ = buf.WriteString(value)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
prefix, suffix := quotePair[0], quotePair[1]
|
||||
|
||||
i := 0
|
||||
|
@ -921,7 +921,7 @@ func (engine *Engine) mapType(v reflect.Value) (*core.Table, error) {
|
|||
t := v.Type()
|
||||
table := core.NewEmptyTable()
|
||||
table.Type = t
|
||||
table.Name = engine.tbNameForMap(v)
|
||||
table.Name = getTableName(engine.TableMapper, v)
|
||||
|
||||
var idFieldColName string
|
||||
var hasCacheTag, hasNoCacheTag bool
|
||||
|
|
|
@ -44,20 +44,6 @@ func (session *Session) tbNameNoSchema(table *core.Table) string {
|
|||
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 {
|
||||
switch tablename.(type) {
|
||||
case []string:
|
||||
|
@ -82,7 +68,7 @@ func (engine *Engine) tbNameNoSchema(tablename interface{}) string {
|
|||
v := rValue(f)
|
||||
t := v.Type()
|
||||
if t.Kind() == reflect.Struct {
|
||||
table = engine.tbNameForMap(v)
|
||||
table = getTableName(engine.TableMapper, v)
|
||||
} else {
|
||||
table = engine.Quote(fmt.Sprintf("%v", f))
|
||||
}
|
||||
|
@ -100,12 +86,12 @@ func (engine *Engine) tbNameNoSchema(tablename interface{}) string {
|
|||
return tablename.(string)
|
||||
case reflect.Value:
|
||||
v := tablename.(reflect.Value)
|
||||
return engine.tbNameForMap(v)
|
||||
return getTableName(engine.TableMapper, v)
|
||||
default:
|
||||
v := rValue(tablename)
|
||||
t := v.Type()
|
||||
if t.Kind() == reflect.Struct {
|
||||
return engine.tbNameForMap(v)
|
||||
return getTableName(engine.TableMapper, v)
|
||||
}
|
||||
return engine.Quote(fmt.Sprintf("%v", tablename))
|
||||
}
|
||||
|
|
|
@ -480,7 +480,7 @@ type MyGetCustomTableImpletation struct {
|
|||
|
||||
const getCustomTableName = "GetCustomTableInterface"
|
||||
|
||||
func (m *MyGetCustomTableImpletation) TableName() string {
|
||||
func (MyGetCustomTableImpletation) TableName() string {
|
||||
return getCustomTableName
|
||||
}
|
||||
|
||||
|
|
31
table_name.go
Normal file
31
table_name.go
Normal file
|
@ -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())
|
||||
}
|
73
table_name_test.go
Normal file
73
table_name_test.go
Normal file
|
@ -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
Block a user