Support get dataSourceName on ContextHook for monitor which DB executed SQL (#1740)
Merge branch 'log_context_add_db_info'
add session to context
Revert "accept lunny's suggestion"
This reverts commit 57fd669942
.
Merge branch 'master' of https://gitea.com/Thomas_An/thomasan_xorm
accept lunny's suggestion
Merge branch 'master' into master
add test code
session add Engine func
Used to monitor which DB executed this SQL
Co-authored-by: yong.an <yong.an@shopee.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/1740
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
parent
946fb57e1c
commit
0fae64bb3b
|
@ -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)
|
||||
}
|
|
@ -120,6 +120,7 @@ type EngineInterface interface {
|
|||
TableInfo(bean interface{}) (*schemas.Table, error)
|
||||
TableName(interface{}, ...bool) string
|
||||
UnMapType(reflect.Type)
|
||||
EnableSessionID(bool)
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
|
@ -42,6 +42,7 @@ var (
|
|||
// enumerate all the context keys
|
||||
var (
|
||||
SessionIDKey = "__xorm_session_id"
|
||||
SessionKey = "__xorm_session_key"
|
||||
SessionShowSQLKey = "__xorm_show_sql"
|
||||
)
|
||||
|
||||
|
|
10
session.go
10
session.go
|
@ -107,7 +107,7 @@ func newSession(engine *Engine) *Session {
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue