export tag identifier (#1865)
#1864 Export tag identifier so we can change that form `xorm` to others This will be useful when in some migration scenarios such as migrate `sqlx` to `xorm`, the former uses `db` as the identifier and has a very simple rule which can be covered by xorm parser rules Co-authored-by: clannadxr <clannadxr@hotmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1865 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: clannadxr <clannadxr@noreply.gitea.io> Co-committed-by: clannadxr <clannadxr@noreply.gitea.io>
This commit is contained in:
parent
ffac8e3577
commit
407da9ddd8
|
@ -209,6 +209,11 @@ func (engine *Engine) SetColumnMapper(mapper names.Mapper) {
|
||||||
engine.tagParser.SetColumnMapper(mapper)
|
engine.tagParser.SetColumnMapper(mapper)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetTagIdentifier set the tag identifier
|
||||||
|
func (engine *Engine) SetTagIdentifier(tagIdentifier string) {
|
||||||
|
engine.tagParser.SetIdentifier(tagIdentifier)
|
||||||
|
}
|
||||||
|
|
||||||
// Quote Use QuoteStr quote the string sql
|
// Quote Use QuoteStr quote the string sql
|
||||||
func (engine *Engine) Quote(value string) string {
|
func (engine *Engine) Quote(value string) string {
|
||||||
value = strings.TrimSpace(value)
|
value = strings.TrimSpace(value)
|
||||||
|
|
|
@ -167,6 +167,14 @@ func (eg *EngineGroup) SetMapper(mapper names.Mapper) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetTagIdentifier set the tag identifier
|
||||||
|
func (eg *EngineGroup) SetTagIdentifier(tagIdentifier string) {
|
||||||
|
eg.Engine.SetTagIdentifier(tagIdentifier)
|
||||||
|
for i := 0; i < len(eg.slaves); i++ {
|
||||||
|
eg.slaves[i].SetTagIdentifier(tagIdentifier)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SetMaxIdleConns set the max idle connections on pool, default is 2
|
// SetMaxIdleConns set the max idle connections on pool, default is 2
|
||||||
func (eg *EngineGroup) SetMaxIdleConns(conns int) {
|
func (eg *EngineGroup) SetMaxIdleConns(conns int) {
|
||||||
eg.Engine.DB().SetMaxIdleConns(conns)
|
eg.Engine.DB().SetMaxIdleConns(conns)
|
||||||
|
|
|
@ -101,6 +101,7 @@ type EngineInterface interface {
|
||||||
SetCacher(string, caches.Cacher)
|
SetCacher(string, caches.Cacher)
|
||||||
SetConnMaxLifetime(time.Duration)
|
SetConnMaxLifetime(time.Duration)
|
||||||
SetColumnMapper(names.Mapper)
|
SetColumnMapper(names.Mapper)
|
||||||
|
SetTagIdentifier(string)
|
||||||
SetDefaultCacher(caches.Cacher)
|
SetDefaultCacher(caches.Cacher)
|
||||||
SetLogger(logger interface{})
|
SetLogger(logger interface{})
|
||||||
SetLogLevel(log.LogLevel)
|
SetLogLevel(log.LogLevel)
|
||||||
|
|
|
@ -63,6 +63,11 @@ func (parser *Parser) SetColumnMapper(mapper names.Mapper) {
|
||||||
parser.columnMapper = mapper
|
parser.columnMapper = mapper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (parser *Parser) SetIdentifier(identifier string) {
|
||||||
|
parser.ClearCaches()
|
||||||
|
parser.identifier = identifier
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -80,3 +80,26 @@ func TestUnexportField(t *testing.T) {
|
||||||
assert.NotEqual(t, "public", col.Name)
|
assert.NotEqual(t, "public", col.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseWithOtherIdentifier(t *testing.T) {
|
||||||
|
parser := NewParser(
|
||||||
|
"xorm",
|
||||||
|
dialects.QueryDialect("mysql"),
|
||||||
|
names.GonicMapper{},
|
||||||
|
names.SnakeMapper{},
|
||||||
|
caches.NewManager(),
|
||||||
|
)
|
||||||
|
|
||||||
|
type StructWithDBTag struct {
|
||||||
|
FieldFoo string `db:"foo"`
|
||||||
|
}
|
||||||
|
parser.SetIdentifier("db")
|
||||||
|
table, err := parser.Parse(reflect.ValueOf(new(StructWithDBTag)))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "struct_with_db_tag", table.Name)
|
||||||
|
assert.EqualValues(t, 1, len(table.Columns()))
|
||||||
|
|
||||||
|
for _, col := range table.Columns() {
|
||||||
|
assert.EqualValues(t, "foo", col.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue