Support get dataSourceName on ContextHook for monitor which DB executed SQL #1740

Merged
lunny merged 9 commits from Thomas_An/thomasan_xorm:master into master 2020-07-13 13:30:29 +00:00
4 changed files with 20 additions and 2 deletions

View File

@ -54,3 +54,11 @@ func TestMustLogSQL(t *testing.T) {
_, err := testEngine.Table("userinfo").MustLogSQL(true).Get(new(Userinfo))
assert.NoError(t, err)
}
func TestEnableSessionId(t *testing.T) {
assert.NoError(t, PrepareEngine())
testEngine.EnableSessionID(true)
assertSync(t, new(Userinfo))
_, err := testEngine.Table("userinfo").MustLogSQL(true).Get(new(Userinfo))
assert.NoError(t, err)
}

View File

@ -120,6 +120,7 @@ type EngineInterface interface {
TableInfo(bean interface{}) (*schemas.Table, error)
TableName(interface{}, ...bool) string
UnMapType(reflect.Type)
EnableSessionID(bool)
}
var (

View File

@ -42,6 +42,7 @@ var (
// enumerate all the context keys
var (
SessionIDKey = "__xorm_session_id"
SessionKey = "__xorm_session_key"
SessionShowSQLKey = "__xorm_show_sql"
)

View File

@ -102,12 +102,12 @@ func newSessionID() string {
func newSession(engine *Engine) *Session {
var ctx context.Context
if engine.logSessionID {
ctx = context.WithValue(engine.defaultContext, log.SessionIDKey, newSessionID())
ctx = context.WithValue(engine.defaultContext, log.SessionIDKey, newSessionID())
} else {
ctx = engine.defaultContext
}
return &Session{
session := &Session{
ctx: ctx,
engine: engine,
tx: nil,
@ -136,6 +136,10 @@ func newSession(engine *Engine) *Session {
sessionType: engineSession,
}
if engine.logSessionID {
session.ctx = context.WithValue(session.ctx, log.SessionKey, session)
}
return session
}
// Close release the connection from pool
@ -165,6 +169,10 @@ func (session *Session) db() *core.DB {
return session.engine.db
}
func (session *Session) Engine() *Engine {
return session.engine
}
func (session *Session) getQueryer() core.Queryer {
if session.tx != nil {
return session.tx