From 7b9c666f0589cf9a184dfe625de1b6035bc798c9 Mon Sep 17 00:00:00 2001 From: gem Date: Thu, 16 Oct 2025 17:23:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9orm=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/orm/orm.go | 25 ++++++++++++++++++++ client/orm/orm_log.go | 54 ++++++++++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/client/orm/orm.go b/client/orm/orm.go index 3dd9f54f..383a65dc 100644 --- a/client/orm/orm.go +++ b/client/orm/orm.go @@ -659,3 +659,28 @@ func newDBWithAlias(al *alias) Ormer { } return o } + +// NewOrmUsingDBWithCtx create new orm with the name +func NewOrmUsingDBWithTraceId(traceId string, aliasName string) Ormer { + if al, ok := dataBaseCache.get(aliasName); ok { + return newDBWithAliasTraceId(traceId, al) + } + panic(fmt.Errorf(" unknown db alias name `%s`", aliasName)) +} +func newDBWithAliasTraceId(traceId string, al *alias) Ormer { + BootStrapWithAlias(al.Name) // execute only once + + o := new(orm) + o.alias = al + + if Debug { + o.db = newDbQueryLogWithTraceId(traceId, al, al.DB) + } else { + o.db = al.DB + } + + if len(globalFilterChains) > 0 { + return NewFilterOrmDecorator(o, globalFilterChains...) + } + return o +} diff --git a/client/orm/orm_log.go b/client/orm/orm_log.go index 25145944..82309f18 100644 --- a/client/orm/orm_log.go +++ b/client/orm/orm_log.go @@ -35,7 +35,7 @@ func NewLog(out io.Writer) *logs.Log { // LogFunc costomer log func var LogFunc func(query map[string]interface{}) -func debugLogQueies(alias *alias, operation, query string, t time.Time, err error, args ...interface{}) { +func debugLogQueies(traceId string, alias *alias, operation, query string, t time.Time, err error, args ...interface{}) { logMap := make(map[string]interface{}) sub := time.Since(t) / 1e5 elsp := float64(int(sub)) / 10.0 @@ -46,7 +46,7 @@ func debugLogQueies(alias *alias, operation, query string, t time.Time, err erro } logMap["flag"] = flag - con := fmt.Sprintf(" -[Queries/%s] - [ %s / %11s / %7.1fms] - [%s]", alias.Name, flag, operation, elsp, query) + con := fmt.Sprintf("TraceId[%s] -[Queries/%s] - [ %s / %11s / %7.1fms] - [%s]", traceId, alias.Name, flag, operation, elsp, query) logMap["alias_name"] = alias.Name logMap["operation"] = operation logMap["query"] = query @@ -72,9 +72,10 @@ func debugLogQueies(alias *alias, operation, query string, t time.Time, err erro // statement query logger struct. // if dev mode, use stmtQueryLog, or use stmtQuerier. type stmtQueryLog struct { - alias *alias - query string - stmt stmtQuerier + alias *alias + query string + stmt stmtQuerier + traceId string } var _ stmtQuerier = new(stmtQueryLog) @@ -82,7 +83,7 @@ var _ stmtQuerier = new(stmtQueryLog) func (d *stmtQueryLog) Close() error { a := time.Now() err := d.stmt.Close() - debugLogQueies(d.alias, "st.Close", d.query, a, err) + debugLogQueies(d.traceId, d.alias, "st.Close", d.query, a, err) return err } @@ -93,7 +94,7 @@ func (d *stmtQueryLog) Exec(args ...interface{}) (sql.Result, error) { func (d *stmtQueryLog) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error) { a := time.Now() res, err := d.stmt.ExecContext(ctx, args...) - debugLogQueies(d.alias, "st.Exec", d.query, a, err, args...) + debugLogQueies(d.traceId, d.alias, "st.Exec", d.query, a, err, args...) return res, err } @@ -104,7 +105,7 @@ func (d *stmtQueryLog) Query(args ...interface{}) (*sql.Rows, error) { func (d *stmtQueryLog) QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error) { a := time.Now() res, err := d.stmt.QueryContext(ctx, args...) - debugLogQueies(d.alias, "st.Query", d.query, a, err, args...) + debugLogQueies(d.traceId, d.alias, "st.Query", d.query, a, err, args...) return res, err } @@ -115,7 +116,7 @@ func (d *stmtQueryLog) QueryRow(args ...interface{}) *sql.Row { func (d *stmtQueryLog) QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row { a := time.Now() res := d.stmt.QueryRow(args...) - debugLogQueies(d.alias, "st.QueryRow", d.query, a, nil, args...) + debugLogQueies(d.traceId, d.alias, "st.QueryRow", d.query, a, nil, args...) return res } @@ -124,16 +125,18 @@ func newStmtQueryLog(alias *alias, stmt stmtQuerier, query string) stmtQuerier { d.stmt = stmt d.alias = alias d.query = query + d.traceId = "" return d } // database query logger struct. // if dev mode, use dbQueryLog, or use dbQuerier. type dbQueryLog struct { - alias *alias - db dbQuerier - tx txer - txe txEnder + alias *alias + db dbQuerier + tx txer + txe txEnder + traceId string } var ( @@ -149,7 +152,7 @@ func (d *dbQueryLog) Prepare(query string) (*sql.Stmt, error) { func (d *dbQueryLog) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) { a := time.Now() stmt, err := d.db.PrepareContext(ctx, query) - debugLogQueies(d.alias, "db.Prepare", query, a, err) + debugLogQueies(d.traceId, d.alias, "db.Prepare", query, a, err) return stmt, err } @@ -160,7 +163,7 @@ func (d *dbQueryLog) Exec(query string, args ...interface{}) (sql.Result, error) func (d *dbQueryLog) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { a := time.Now() res, err := d.db.ExecContext(ctx, query, args...) - debugLogQueies(d.alias, "db.Exec", query, a, err, args...) + debugLogQueies(d.traceId, d.alias, "db.Exec", query, a, err, args...) return res, err } @@ -171,7 +174,7 @@ func (d *dbQueryLog) Query(query string, args ...interface{}) (*sql.Rows, error) func (d *dbQueryLog) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { a := time.Now() res, err := d.db.QueryContext(ctx, query, args...) - debugLogQueies(d.alias, "db.Query", query, a, err, args...) + debugLogQueies(d.traceId, d.alias, "db.Query", query, a, err, args...) return res, err } @@ -182,7 +185,7 @@ func (d *dbQueryLog) QueryRow(query string, args ...interface{}) *sql.Row { func (d *dbQueryLog) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row { a := time.Now() res := d.db.QueryRowContext(ctx, query, args...) - debugLogQueies(d.alias, "db.QueryRow", query, a, nil, args...) + debugLogQueies(d.traceId, d.alias, "db.QueryRow", query, a, nil, args...) return res } @@ -193,28 +196,28 @@ func (d *dbQueryLog) Begin() (*sql.Tx, error) { func (d *dbQueryLog) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) { a := time.Now() tx, err := d.db.(txer).BeginTx(ctx, opts) - debugLogQueies(d.alias, "db.BeginTx", "START TRANSACTION", a, err) + debugLogQueies(d.traceId, d.alias, "db.BeginTx", "START TRANSACTION", a, err) return tx, err } func (d *dbQueryLog) Commit() error { a := time.Now() err := d.db.(txEnder).Commit() - debugLogQueies(d.alias, "tx.Commit", "COMMIT", a, err) + debugLogQueies(d.traceId, d.alias, "tx.Commit", "COMMIT", a, err) return err } func (d *dbQueryLog) Rollback() error { a := time.Now() err := d.db.(txEnder).Rollback() - debugLogQueies(d.alias, "tx.Rollback", "ROLLBACK", a, err) + debugLogQueies(d.traceId, d.alias, "tx.Rollback", "ROLLBACK", a, err) return err } func (d *dbQueryLog) RollbackUnlessCommit() error { a := time.Now() err := d.db.(txEnder).RollbackUnlessCommit() - debugLogQueies(d.alias, "tx.RollbackUnlessCommit", "ROLLBACK UNLESS COMMIT", a, err) + debugLogQueies(d.traceId, d.alias, "tx.RollbackUnlessCommit", "ROLLBACK UNLESS COMMIT", a, err) return err } @@ -226,5 +229,14 @@ func newDbQueryLog(alias *alias, db dbQuerier) dbQuerier { d := new(dbQueryLog) d.alias = alias d.db = db + d.traceId = "" + return d +} + +func newDbQueryLogWithTraceId(traceId string, alias *alias, db dbQuerier) dbQuerier { + d := new(dbQueryLog) + d.alias = alias + d.db = db + d.traceId = traceId return d }