mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2025-12-13 17:10:36 +00:00
Added EnableQueryDebug log
This commit is contained in:
parent
8fcb065b42
commit
f0962ea1ec
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
|
|
||||||
@ -15,6 +16,24 @@ import (
|
|||||||
"github.com/bitechdev/ResolveSpec/pkg/reflection"
|
"github.com/bitechdev/ResolveSpec/pkg/reflection"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// QueryDebugHook is a Bun query hook that logs all SQL queries including preloads
|
||||||
|
type QueryDebugHook struct{}
|
||||||
|
|
||||||
|
func (h *QueryDebugHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context {
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *QueryDebugHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
|
||||||
|
query := event.Query
|
||||||
|
duration := time.Since(event.StartTime)
|
||||||
|
|
||||||
|
if event.Err != nil {
|
||||||
|
logger.Error("SQL Query Failed [%s]: %s. Error: %v", duration, query, event.Err)
|
||||||
|
} else {
|
||||||
|
logger.Debug("SQL Query Success [%s]: %s", duration, query)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// BunAdapter adapts Bun to work with our Database interface
|
// BunAdapter adapts Bun to work with our Database interface
|
||||||
// This demonstrates how the abstraction works with different ORMs
|
// This demonstrates how the abstraction works with different ORMs
|
||||||
type BunAdapter struct {
|
type BunAdapter struct {
|
||||||
@ -26,6 +45,20 @@ func NewBunAdapter(db *bun.DB) *BunAdapter {
|
|||||||
return &BunAdapter{db: db}
|
return &BunAdapter{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnableQueryDebug enables query debugging which logs all SQL queries including preloads
|
||||||
|
// This is useful for debugging preload queries that may be failing
|
||||||
|
func (b *BunAdapter) EnableQueryDebug() {
|
||||||
|
b.db.AddQueryHook(&QueryDebugHook{})
|
||||||
|
logger.Info("Bun query debug mode enabled - all SQL queries will be logged")
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableQueryDebug removes all query hooks
|
||||||
|
func (b *BunAdapter) DisableQueryDebug() {
|
||||||
|
// Create a new DB without hooks
|
||||||
|
// Note: Bun doesn't have a RemoveQueryHook, so we'd need to track hooks manually
|
||||||
|
logger.Info("To disable query debug, recreate the BunAdapter without adding the hook")
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BunAdapter) NewSelect() common.SelectQuery {
|
func (b *BunAdapter) NewSelect() common.SelectQuery {
|
||||||
return &BunSelectQuery{
|
return &BunSelectQuery{
|
||||||
query: b.db.NewSelect(),
|
query: b.db.NewSelect(),
|
||||||
|
|||||||
@ -23,6 +23,22 @@ func NewGormAdapter(db *gorm.DB) *GormAdapter {
|
|||||||
return &GormAdapter{db: db}
|
return &GormAdapter{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnableQueryDebug enables query debugging which logs all SQL queries including preloads
|
||||||
|
// This is useful for debugging preload queries that may be failing
|
||||||
|
func (g *GormAdapter) EnableQueryDebug() *GormAdapter {
|
||||||
|
g.db = g.db.Debug()
|
||||||
|
logger.Info("GORM query debug mode enabled - all SQL queries will be logged")
|
||||||
|
return g
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableQueryDebug disables query debugging
|
||||||
|
func (g *GormAdapter) DisableQueryDebug() *GormAdapter {
|
||||||
|
// GORM's Debug() creates a new session, so we need to get the base DB
|
||||||
|
// This is a simplified implementation
|
||||||
|
logger.Info("GORM debug mode - create a new adapter without Debug() to disable")
|
||||||
|
return g
|
||||||
|
}
|
||||||
|
|
||||||
func (g *GormAdapter) NewSelect() common.SelectQuery {
|
func (g *GormAdapter) NewSelect() common.SelectQuery {
|
||||||
return &GormSelectQuery{db: g.db}
|
return &GormSelectQuery{db: g.db}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user