Add SetTagIdentifier
This commit is contained in:
parent
4629bb1717
commit
ae48cdeca5
|
@ -183,6 +183,10 @@ func (engine *Engine) SetDisableGlobalCache(disable bool) {
|
||||||
engine.cacherMgr.SetDisableGlobalCache(disable)
|
engine.cacherMgr.SetDisableGlobalCache(disable)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) SetTagIdentifier(tagName string) {
|
||||||
|
engine.tagParser.SetIdentifier(tagName)
|
||||||
|
}
|
||||||
|
|
||||||
// DriverName return the current sql driver's name
|
// DriverName return the current sql driver's name
|
||||||
func (engine *Engine) DriverName() string {
|
func (engine *Engine) DriverName() string {
|
||||||
return engine.driverName
|
return engine.driverName
|
||||||
|
|
|
@ -34,6 +34,7 @@ type Parser struct {
|
||||||
tableCache sync.Map // map[reflect.Type]*schemas.Table
|
tableCache sync.Map // map[reflect.Type]*schemas.Table
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewParser create a new tag parser
|
||||||
func NewParser(identifier string, dialect dialects.Dialect, tableMapper, columnMapper names.Mapper, cacherMgr *caches.Manager) *Parser {
|
func NewParser(identifier string, dialect dialects.Dialect, tableMapper, columnMapper names.Mapper, cacherMgr *caches.Manager) *Parser {
|
||||||
return &Parser{
|
return &Parser{
|
||||||
identifier: identifier,
|
identifier: identifier,
|
||||||
|
@ -45,24 +46,34 @@ func NewParser(identifier string, dialect dialects.Dialect, tableMapper, columnM
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTableMapper returns the table mapper
|
||||||
func (parser *Parser) GetTableMapper() names.Mapper {
|
func (parser *Parser) GetTableMapper() names.Mapper {
|
||||||
return parser.tableMapper
|
return parser.tableMapper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetTableMapper sets table mapper
|
||||||
func (parser *Parser) SetTableMapper(mapper names.Mapper) {
|
func (parser *Parser) SetTableMapper(mapper names.Mapper) {
|
||||||
parser.ClearCaches()
|
parser.ClearCaches()
|
||||||
parser.tableMapper = mapper
|
parser.tableMapper = mapper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetColumnMapper returns the column mapper
|
||||||
func (parser *Parser) GetColumnMapper() names.Mapper {
|
func (parser *Parser) GetColumnMapper() names.Mapper {
|
||||||
return parser.columnMapper
|
return parser.columnMapper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetColumnMapper set column mapper
|
||||||
func (parser *Parser) SetColumnMapper(mapper names.Mapper) {
|
func (parser *Parser) SetColumnMapper(mapper names.Mapper) {
|
||||||
parser.ClearCaches()
|
parser.ClearCaches()
|
||||||
parser.columnMapper = mapper
|
parser.columnMapper = mapper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetIdentifier set tag identifier
|
||||||
|
func (parser *Parser) SetIdentifier(tagName string) {
|
||||||
|
parser.identifier = tagName
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseWithCache parse with cache
|
||||||
func (parser *Parser) ParseWithCache(v reflect.Value) (*schemas.Table, error) {
|
func (parser *Parser) ParseWithCache(v reflect.Value) (*schemas.Table, error) {
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
tableI, ok := parser.tableCache.Load(t)
|
tableI, ok := parser.tableCache.Load(t)
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"xorm.io/xorm/caches"
|
"xorm.io/xorm/caches"
|
||||||
"xorm.io/xorm/dialects"
|
"xorm.io/xorm/dialects"
|
||||||
"xorm.io/xorm/names"
|
"xorm.io/xorm/names"
|
||||||
|
"xorm.io/xorm/schemas"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ParseTableName1 struct{}
|
type ParseTableName1 struct{}
|
||||||
|
@ -80,3 +81,28 @@ func TestUnexportField(t *testing.T) {
|
||||||
assert.NotEqual(t, "public", col.Name)
|
assert.NotEqual(t, "public", col.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ParseTableName3 struct {
|
||||||
|
Name string `orm:"varchar(50)"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseTableNameWithOtherIdentifier(t *testing.T) {
|
||||||
|
parser := NewParser(
|
||||||
|
"xorm",
|
||||||
|
dialects.QueryDialect("mysql"),
|
||||||
|
names.SnakeMapper{},
|
||||||
|
names.SnakeMapper{},
|
||||||
|
caches.NewManager(),
|
||||||
|
)
|
||||||
|
parser.SetIdentifier("orm")
|
||||||
|
table, err := parser.Parse(reflect.ValueOf(new(ParseTableName3)))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
cols := table.Columns()
|
||||||
|
assert.EqualValues(t, 1, len(cols))
|
||||||
|
assert.EqualValues(t, "name", cols[0].Name)
|
||||||
|
assert.EqualValues(t, schemas.SQLType{
|
||||||
|
Name: "VARCHAR",
|
||||||
|
}, cols[0].SQLType)
|
||||||
|
assert.EqualValues(t, 50, cols[0].Length)
|
||||||
|
assert.EqualValues(t, true, cols[0].Nullable)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue