master #1

Merged
whybangbang merged 3 commits from xorm/xorm:master into master 2021-02-23 03:09:12 +00:00
6 changed files with 49 additions and 0 deletions

View File

@ -168,6 +168,8 @@ func (engine *Engine) SetLogger(logger interface{}) {
realLogger = t
case log.Logger:
realLogger = log.NewLoggerAdapter(t)
default:
panic("logger should implement either log.ContextLogger or log.Logger")
}
engine.logger = realLogger
engine.DB().Logger = realLogger
@ -209,6 +211,11 @@ func (engine *Engine) SetColumnMapper(mapper names.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
func (engine *Engine) Quote(value string) string {
value = strings.TrimSpace(value)

View File

@ -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
func (eg *EngineGroup) SetMaxIdleConns(conns int) {
eg.Engine.DB().SetMaxIdleConns(conns)

View File

@ -101,6 +101,7 @@ type EngineInterface interface {
SetCacher(string, caches.Cacher)
SetConnMaxLifetime(time.Duration)
SetColumnMapper(names.Mapper)
SetTagIdentifier(string)
SetDefaultCacher(caches.Cacher)
SetLogger(logger interface{})
SetLogLevel(log.LogLevel)

View File

@ -84,3 +84,8 @@ func (session *Session) Commit() error {
}
return nil
}
// if current session is in a transaction
func (session *Session) IsInTx() bool {
return !session.isAutoCommit
}

View File

@ -63,6 +63,11 @@ func (parser *Parser) SetColumnMapper(mapper names.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) {
t := v.Type()
tableI, ok := parser.tableCache.Load(t)

View File

@ -80,3 +80,26 @@ func TestUnexportField(t *testing.T) {
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)
}
}