Browse Source

Support session id (#1632)

small nit

Support session id

Reviewed-on: #1632
pull/1630/head
Lunny Xiao 3 months ago
parent
commit
b78418daa5
5 changed files with 60 additions and 8 deletions
  1. +1
    -1
      core/db.go
  2. +7
    -0
      engine.go
  3. +26
    -2
      log/logger_context.go
  4. +25
    -5
      session.go
  5. +1
    -0
      xorm.go

+ 1
- 1
core/db.go View File

@ -118,7 +118,7 @@ func (db *DB) NeedLogSQL(ctx context.Context) bool {
return false
}
v := ctx.Value("__xorm_show_sql")
v := ctx.Value(log.SessionShowSQLKey)
if showSQL, ok := v.(bool); ok {
return showSQL
}

+ 7
- 0
engine.go View File

@ -42,6 +42,13 @@ type Engine struct {
TZLocation *time.Location // The timezone of the application
DatabaseTZ *time.Location // The timezone of the database
logSessionID bool // create session id
}
// EnableSessionID if enable session id
func (engine *Engine) EnableSessionID(enable bool) {
engine.logSessionID = enable
}
// SetCacher sets cacher for the table

+ 26
- 2
log/logger_context.go View File

@ -6,6 +6,7 @@ package log
import (
"context"
"fmt"
"time"
)
@ -18,6 +19,7 @@ type LogContext struct {
Err error // SQL executed error
}
// SQLLogger represents an interface to log SQL
type SQLLogger interface {
BeforeSQL(context LogContext) // only invoked when IsShowSQL is true
AfterSQL(context LogContext) // only invoked when IsShowSQL is true
@ -43,55 +45,77 @@ var (
_ ContextLogger = &LoggerAdapter{}
)
// enumerate all the context keys
var (
SessionIDKey = "__xorm_session_id"
SessionShowSQLKey = "__xorm_show_sql"
)
// LoggerAdapter wraps a Logger interafce as LoggerContext interface
type LoggerAdapter struct {
logger Logger
}
// NewLoggerAdapter creates an adapter for old xorm logger interface
func NewLoggerAdapter(logger Logger) ContextLogger {
return &LoggerAdapter{
logger: logger,
}
}
// BeforeSQL implements ContextLogger
func (l *LoggerAdapter) BeforeSQL(ctx LogContext) {}
// AfterSQL implements ContextLogger
func (l *LoggerAdapter) AfterSQL(ctx LogContext) {
var sessionPart string
v := ctx.Ctx.Value(SessionIDKey)
if key, ok := v.(string); ok {
sessionPart = fmt.Sprintf(" [%s]", key)
}
if ctx.ExecuteTime > 0 {
l.logger.Infof("[SQL] %v %v - %v", ctx.SQL, ctx.Args, ctx.ExecuteTime)
l.logger.Infof("[SQL]%s %s %v - %v", sessionPart, ctx.SQL, ctx.Args, ctx.ExecuteTime)
} else {
l.logger.Infof("[SQL] %v %v", ctx.SQL, ctx.Args)
l.logger.Infof("[SQL]%s %s %v", sessionPart, ctx.SQL, ctx.Args)
}
}
// Debugf implements ContextLogger
func (l *LoggerAdapter) Debugf(format string, v ...interface{}) {
l.logger.Debugf(format, v...)
}
// Errorf implements ContextLogger
func (l *LoggerAdapter) Errorf(format string, v ...interface{}) {
l.logger.Errorf(format, v...)
}
// Infof implements ContextLogger
func (l *LoggerAdapter) Infof(format string, v ...interface{}) {
l.logger.Infof(format, v...)
}
// Warnf implements ContextLogger
func (l *LoggerAdapter) Warnf(format string, v ...interface{}) {
l.logger.Warnf(format, v...)
}
// Level implements ContextLogger
func (l *LoggerAdapter) Level() LogLevel {
return l.logger.Level()
}
// SetLevel implements ContextLogger
func (l *LoggerAdapter) SetLevel(lv LogLevel) {
l.logger.SetLevel(lv)
}
// ShowSQL implements ContextLogger
func (l *LoggerAdapter) ShowSQL(show ...bool) {
l.logger.ShowSQL(show...)
}
// IsShowSQL implements ContextLogger
func (l *LoggerAdapter) IsShowSQL() bool {
return l.logger.IsShowSQL()
}

+ 25
- 5
session.go View File

@ -6,10 +6,14 @@ package xorm
import (
"context"
"crypto/rand"
"crypto/sha256"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"hash/crc32"
"io"
"reflect"
"strings"
"time"
@ -19,6 +23,7 @@ import (
"xorm.io/xorm/core"
"xorm.io/xorm/internal/json"
"xorm.io/xorm/internal/statements"
"xorm.io/xorm/log"
"xorm.io/xorm/schemas"
)
@ -92,6 +97,17 @@ func (session *Session) Clone() *Session {
return &sess
}
func newSessionID() string {
hash := sha256.New()
_, err := io.CopyN(hash, rand.Reader, 50)
if err != nil {
return "????????????????????"
}
md := hash.Sum(nil)
mdStr := hex.EncodeToString(md)
return mdStr[0:20]
}
// Init reset the session as the init status.
func (session *Session) Init() {
session.statement = statements.NewStatement(
@ -119,7 +135,11 @@ func (session *Session) Init() {
session.lastSQL = ""
session.lastSQLArgs = []interface{}{}
session.ctx = session.engine.defaultContext
if session.engine.logSessionID {
session.ctx = context.WithValue(session.engine.defaultContext, log.SessionIDKey, newSessionID())
} else {
session.ctx = session.engine.defaultContext
}
}
// Close release the connection from pool
@ -264,12 +284,12 @@ func (session *Session) Cascade(trueOrFalse ...bool) *Session {
}
// MustLogSQL means record SQL or not and don't follow engine's setting
func (session *Session) MustLogSQL(log ...bool) *Session {
func (session *Session) MustLogSQL(logs ...bool) *Session {
var showSQL = true
if len(log) > 0 {
showSQL = log[0]
if len(logs) > 0 {
showSQL = logs[0]
}
session.ctx = context.WithValue(session.ctx, "__xorm_show_sql", showSQL)
session.ctx = context.WithValue(session.ctx, log.SessionShowSQLKey, showSQL)
return session
}

+ 1
- 0
xorm.go View File

@ -51,6 +51,7 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
driverName: driverName,
dataSourceName: dataSourceName,
db: db,
logSessionID: false,
}
if dialect.URI().DBType == schemas.SQLITE {

Loading…
Cancel
Save