1
1
mirror of https://github.com/go-gitea/gitea synced 2025-10-28 01:48:25 +00:00

Refactor db package (#35380)

Remove unnecessary code
This commit is contained in:
wxiaoguang
2025-08-30 01:04:06 +08:00
committed by GitHub
parent aef4a3514c
commit 1f50048ac9
11 changed files with 37 additions and 52 deletions

View File

@@ -61,17 +61,19 @@ func contextSafetyCheck(e Engine) {
callerNum := runtime.Callers(3, callers) // skip 3: runtime.Callers, contextSafetyCheck, GetEngine
for i := range callerNum {
if slices.Contains(contextSafetyDeniedFuncPCs, callers[i]) {
panic(errors.New("using database context in an iterator would cause corrupted results"))
panic(errors.New("using session context in an iterator would cause corrupted results"))
}
}
}
// GetEngine gets an existing db Engine/Statement or creates a new Session
func GetEngine(ctx context.Context) (e Engine) {
defer func() { contextSafetyCheck(e) }()
func GetEngine(ctx context.Context) Engine {
if engine, ok := ctx.Value(engineContextKey).(Engine); ok {
// if reusing the existing session, need to do "contextSafetyCheck" because the Iterate creates a "autoResetStatement=false" session
contextSafetyCheck(engine)
return engine
}
// no need to do "contextSafetyCheck" because it's a new Session
return xormEngine.Context(ctx)
}
@@ -301,11 +303,6 @@ func CountByBean(ctx context.Context, bean any) (int64, error) {
return GetEngine(ctx).Count(bean)
}
// TableName returns the table name according a bean object
func TableName(bean any) string {
return xormEngine.TableName(bean)
}
// InTransaction returns true if the engine is in a transaction otherwise return false
func InTransaction(ctx context.Context) bool {
return getTransactionSession(ctx) != nil